Ejemplo n.º 1
0
        public override void SetCommandParameters(DbCommandType commandType, DbCommand command, SyncFilter filter = null)
        {
            switch (commandType)
            {
            case DbCommandType.SelectChanges:
            case DbCommandType.SelectChangesWithFilters:
                this.SetSelecteChangesParameters(command);
                break;

            case DbCommandType.SelectRow:
                this.SetSelectRowParameters(command);
                break;

            case DbCommandType.DeleteMetadata:
                this.SetDeleteMetadataParameters(command);
                break;

            case DbCommandType.DeleteRow:
                this.SetDeleteRowParameters(command);
                break;

            case DbCommandType.UpdateRow:
                this.SetUpdateRowParameters(command);
                break;

            case DbCommandType.Reset:
                this.SetResetParameters(command);
                break;

            default:
                break;
            }
        }
Ejemplo n.º 2
0
        public override DbCommand GetCommand(DbCommandType nameType, IEnumerable <string> additionals = null)
        {
            var command = this.Connection.CreateCommand();

            string text;

            if (additionals != null)
            {
                text = this.sqlObjectNames.GetCommandName(nameType, additionals);
            }
            else
            {
                text = this.sqlObjectNames.GetCommandName(nameType);
            }

            // on Sql Server, everything is Stored Procedure
            command.CommandType = CommandType.StoredProcedure;
            command.CommandText = text;
            command.Connection  = Connection;

            if (Transaction != null)
            {
                command.Transaction = Transaction;
            }

            return(command);
        }
Ejemplo n.º 3
0
        public override DbCommand GetCommand(DbCommandType commandType, IEnumerable <FilterClause> additionals = null)
        {
            var    command = this.Connection.CreateCommand();
            string text;
            bool   isStoredProc;

            if (additionals != null)
            {
                (text, isStoredProc) = this.mySqlObjectNames.GetCommandName(commandType, additionals);
            }
            else
            {
                (text, isStoredProc) = this.mySqlObjectNames.GetCommandName(commandType);
            }

            var textName = ParserName.Parse(text, "`");

            command.CommandType = isStoredProc ? CommandType.StoredProcedure : CommandType.Text;
            command.CommandText = isStoredProc ? textName.Quoted().ToString() : text;
            command.Connection  = Connection;

            if (Transaction != null)
            {
                command.Transaction = Transaction;
            }

            return(command);
        }
        public override Task AddCommandParametersAsync(DbCommandType commandType, DbCommand command, DbConnection connection, DbTransaction transaction = null, SyncFilter filter = null)
        {
            if (command == null)
            {
                return(Task.CompletedTask);
            }

            if (command.Parameters != null && command.Parameters.Count > 0)
            {
                return(Task.CompletedTask);
            }

            switch (commandType)
            {
            case DbCommandType.DeleteRow:
                this.SetDeleteRowParameters(command);
                return(Task.CompletedTask);

            case DbCommandType.UpdateRow:
            case DbCommandType.InsertRow:
                this.SetUpdateRowParameters(command);
                return(Task.CompletedTask);

            default:
                break;
            }

            return(base.AddCommandParametersAsync(commandType, command, connection, transaction, filter));
        }
Ejemplo n.º 5
0
        public override Task SetCommandParametersAsync(DbCommandType commandType, DbCommand command, SyncFilter filter = null)
        {
            switch (commandType)
            {
            case DbCommandType.SelectChanges:
            case DbCommandType.SelectChangesWithFilters:
            case DbCommandType.SelectInitializedChanges:
            case DbCommandType.SelectInitializedChangesWithFilters:
                this.SetSelecteChangesParameters(command, filter);
                break;

            case DbCommandType.SelectRow:
                this.SetSelectRowParameters(command);
                break;

            case DbCommandType.DeleteMetadata:
                this.SetDeleteMetadataParameters(command);
                break;

            case DbCommandType.DeleteRow:
                this.SetDeleteRowParameters(command);
                break;

            case DbCommandType.UpdateRow:
                this.SetUpdateRowParameters(command);
                break;

            default:
                break;
            }

            return(Task.CompletedTask);
        }
Ejemplo n.º 6
0
        public string GetCommandName(DbCommandType objectType, IEnumerable<string> adds = null)
        {
            if (!names.ContainsKey(objectType))
                throw new NotSupportedException($"MySql provider does not support the command type {objectType.ToString()}");

            return names[objectType];
        }
Ejemplo n.º 7
0
        public override DbCommand GetCommand(DbCommandType commandType, IEnumerable <string> additionals = null)
        {
            var    command = this.Connection.CreateCommand();
            string text;

            if (additionals != null)
            {
                text = this.postgreSqlObjectNames.GetCommandName(commandType, additionals);
            }
            else
            {
                text = this.postgreSqlObjectNames.GetCommandName(commandType);
            }

            // on MySql, everything is text based :)
            command.CommandType = CommandType.Text;
            command.CommandText = text;
            command.Connection  = Connection;

            if (Transaction != null)
            {
                command.Transaction = Transaction;
            }

            return(command);
        }
Ejemplo n.º 8
0
        public override DbCommand GetCommand(DbCommandType commandType, IEnumerable <string> additionals = null)
        {
            var    command = Connection.CreateCommand();
            string text;

            if (additionals != null)
            {
                text = _postgreSqlObjectNames.GetCommandName(commandType, additionals);
            }
            else
            {
                text = _postgreSqlObjectNames.GetCommandName(commandType);
            }


            command.CommandType = CommandType.StoredProcedure;
            command.CommandText = text;
            command.Connection  = Connection;

            if (Transaction != null)
            {
                command.Transaction = Transaction;
            }

            return(command);
        }
Ejemplo n.º 9
0
        public void AddName(DbCommandType objectType, string name)
        {
            if (names.ContainsKey(objectType))
                throw new Exception("Yous can't add an objectType multiple times");

            names.Add(objectType, name);
        }
        /// <summary>
        /// return null for all no used commands
        /// </summary>
        public override (DbCommand, bool) GetCommand(DbCommandType nameType, SyncFilter filter)
        {
            var command = new SqliteCommand();

            switch (nameType)
            {
            case DbCommandType.UpdateRow:
                return(CreateUpdateCommand(), false);

            case DbCommandType.InsertRows:
                return(CreateInitiliazeRowCommand(), false);

            case DbCommandType.DeleteRow:
                return(CreateDeleteCommand(), false);

            case DbCommandType.DisableConstraints:
                command.CommandType = CommandType.Text;
                command.CommandText = this.sqliteObjectNames.GetCommandName(DbCommandType.DisableConstraints, filter);
                break;

            case DbCommandType.EnableConstraints:
                command.CommandType = CommandType.Text;
                command.CommandText = this.sqliteObjectNames.GetCommandName(DbCommandType.EnableConstraints, filter);
                break;

            case DbCommandType.Reset:
                return(CreateResetCommand(), false);

            default:
                return(default, default);
            }

            return(command, false);
        }
Ejemplo n.º 11
0
        public string GetCommandName(DbCommandType objectType, IEnumerable <FilterClause> filters = null)
        {
            if (!names.ContainsKey(objectType))
            {
                throw new NotSupportedException($"Sqlite provider does not support the command type {objectType.ToString()}");
            }

            var commandName = names[objectType];

            if (filters != null)
            {
                string name = "";
                string sep  = "";
                foreach (var c in filters)
                {
                    var columnName = ParserName.Parse(c.ColumnName).Unquoted().Normalized().ToString();
                    name += $"{columnName}{sep}";
                    sep   = "_";
                }

                commandName = string.Format(commandName, name);
            }

            return(commandName);
        }
Ejemplo n.º 12
0
        public override DbCommand GetCommand(DbCommandType commandType, IEnumerable <string> additionals = null)
        {
            var    command = this.Connection.CreateCommand();
            string text;

            if (additionals != null)
            {
                text = this.mySqlObjectNames.GetCommandName(commandType, additionals);
            }
            else
            {
                text = this.mySqlObjectNames.GetCommandName(commandType);
            }

            var textName = new ObjectNameParser(text, "`", "`");

            // on MySql, everything is text based :)
            command.CommandType = CommandType.StoredProcedure;
            command.CommandText = textName.ObjectName;
            command.Connection  = Connection;

            //if (commandType == DbCommandType.UpdateRow)
            //{
            //    command.CommandType = CommandType.StoredProcedure;
            //    command.CommandText = "customers_update";
            //    command.Connection = Connection;
            //}

            if (Transaction != null)
            {
                command.Transaction = Transaction;
            }

            return(command);
        }
Ejemplo n.º 13
0
        public override DbCommand GetCommand(DbCommandType nameType, IEnumerable <FilterClause> filters = null)
        {
            var command = this.Connection.CreateCommand();

            string text;
            bool   isStoredProc;

            if (filters != null)
            {
                (text, isStoredProc) = this.sqlObjectNames.GetCommandName(nameType, filters);
            }
            else
            {
                (text, isStoredProc) = this.sqlObjectNames.GetCommandName(nameType);
            }

            command.CommandType = isStoredProc ? CommandType.StoredProcedure : CommandType.Text;
            command.CommandText = text;
            command.Connection  = Connection;

            if (Transaction != null)
            {
                command.Transaction = Transaction;
            }

            return(command);
        }
Ejemplo n.º 14
0
        public override DbCommand GetCommand(DbCommandType commandType, IEnumerable <FilterClause> filters = null)
        {
            var    command = this.Connection.CreateCommand();
            string text;

            if (filters != null)
            {
                text = this.sqliteObjectNames.GetCommandName(commandType, filters);
            }
            else
            {
                text = this.sqliteObjectNames.GetCommandName(commandType);
            }

            // on Sqlite, everything is text :)
            command.CommandType = CommandType.Text;
            command.CommandText = text;
            command.Connection  = Connection;

            if (Transaction != null)
            {
                command.Transaction = Transaction;
            }

            return(command);
        }
Ejemplo n.º 15
0
#pragma warning restore CS1998 // Async method lacks 'await' operators and will run synchronously


        private async Task DropProcedureAsync(DbCommandType procType, SyncFilter filter = null)
        {
            var commandName = this.mySqlObjectNames.GetCommandName(procType).name;
            var commandText = $"drop procedure if exists {commandName}";

            bool alreadyOpened = connection.State == ConnectionState.Open;

            try
            {
                if (!alreadyOpened)
                {
                    await connection.OpenAsync().ConfigureAwait(false);
                }

                using (var command = new MySqlCommand(commandText, connection))
                {
                    if (transaction != null)
                    {
                        command.Transaction = transaction;
                    }

                    await command.ExecuteNonQueryAsync().ConfigureAwait(false);
                }

                if (filter != null)
                {
                    using (var command = new MySqlCommand())
                    {
                        if (!alreadyOpened)
                        {
                            await connection.OpenAsync().ConfigureAwait(false);
                        }

                        if (this.transaction != null)
                        {
                            command.Transaction = this.transaction;
                        }

                        var commandNameWithFilter = this.mySqlObjectNames.GetCommandName(DbCommandType.SelectChangesWithFilters, filter).name;

                        command.CommandText = $"DROP PROCEDURE {commandNameWithFilter};";
                        command.Connection  = this.connection;
                        await command.ExecuteNonQueryAsync().ConfigureAwait(false);
                    }
                }
            }
            catch (Exception ex)
            {
                Debug.WriteLine($"Error during DropProcedureCommand : {ex}");
                throw;
            }
            finally
            {
                if (!alreadyOpened && connection.State != ConnectionState.Closed)
                {
                    connection.Close();
                }
            }
        }
Ejemplo n.º 16
0
        public string CreateDropTriggerScriptText(DbCommandType triggerType)
        {
            var    triggerName = string.Format(this.sqliteObjectNames.GetCommandName(triggerType), tableName.UnquotedStringWithUnderScore);
            string dropTrigger = $"DROP TRIGGER IF EXISTS {triggerName}";
            string str         = $"Drop Trigger {triggerName} for table {tableName.QuotedString}";

            return(SqliteBuilder.WrapScriptTextWithComments(dropTrigger, str));
        }
Ejemplo n.º 17
0
 private void UpdateMetadatas(DbCommandType dbCommandType, DmRow dmRow, ScopeInfo scope)
 {
     using (var dbCommand = this.GetCommand(dbCommandType))
     {
         this.SetCommandParameters(dbCommandType, dbCommand);
         this.InsertOrUpdateMetadatas(dbCommand, dmRow, scope.Id);
     }
 }
Ejemplo n.º 18
0
        private string DropProcedureText(DbCommandType procType)
        {
            var commandName = this.sqlObjectNames.GetCommandName(procType);
            var commandText = $"drop procedure if exists {commandName}";

            var str1 = $"Drop procedure {commandName} for table {tableName.FullQuotedString}";

            return(MySqlBuilder.WrapScriptTextWithComments(commandText, str1));
        }
Ejemplo n.º 19
0
        private string DropTriggerText(DbCommandType triggerType)
        {
            var commandName = this.mySqlObjectNames.GetCommandName(triggerType).name;
            var commandText = $"drop trigger if exists {commandName}";

            var str1 = $"Drop trigger {commandName} for table {tableName.Quoted().ToString()}";

            return(MySqlBuilder.WrapScriptTextWithComments(commandText, str1));
        }
Ejemplo n.º 20
0
        private string DropTriggerText(DbCommandType triggerType)
        {
            var commandName = _postgreSqlObjectNames.GetCommandName(triggerType);
            var commandText = $"drop trigger if exists {commandName}";

            var str1 = $"Drop trigger {commandName} for table {_tableName.QuotedString}";

            return(PostgreSqlBuilder.WrapScriptTextWithComments(commandText, str1));
        }
        public override Task AddCommandParametersAsync(DbCommandType commandType, DbCommand command, DbConnection connection, DbTransaction transaction = null, SyncFilter filter = null)
        {
            if (command == null)
            {
                return(Task.CompletedTask);
            }

            if (command.Parameters != null && command.Parameters.Count > 0)
            {
                return(Task.CompletedTask);
            }

            switch (commandType)
            {
            case DbCommandType.SelectChanges:
            case DbCommandType.SelectChangesWithFilters:
                this.SetSelecteChangesParameters(command);
                break;

            case DbCommandType.SelectRow:
                this.SetSelectRowParameters(command);
                break;

            case DbCommandType.DeleteMetadata:
                this.SetDeleteMetadataParameters(command);
                break;

            case DbCommandType.DeleteRow:
            case DbCommandType.DeleteRows:
                this.SetDeleteRowParameters(command);
                break;

            case DbCommandType.UpdateRow:
            case DbCommandType.UpdateRows:
                this.SetUpdateRowParameters(command);
                break;

            case DbCommandType.InsertRow:
            case DbCommandType.InsertRows:
                this.SetInitializeRowParameters(command);
                break;

            case DbCommandType.Reset:
                this.SetResetParameters(command);
                break;

            case DbCommandType.UpdateMetadata:
                this.SetUpdateMetadataParameters(command);
                break;

            default:
                break;
            }

            return(Task.CompletedTask);
        }
        /// <summary>
        /// Get the command from provider, check connection is opened, affect connection and transaction
        /// Prepare the command parameters and add scope parameters
        /// </summary>
        public async Task <(DbCommand Command, bool IsBatch)> GetCommandAsync(DbCommandType commandType, DbConnection connection, DbTransaction transaction, SyncFilter filter = null)
        {
            // Create the key
            var commandKey = $"{connection.DataSource}-{connection.Database}-{this.TableDescription.GetFullName()}-{commandType}";

            var(command, isBatch) = GetCommand(commandType, filter);

            if (command == null)
            {
                return(null, false);
            }

            // Add Parameters
            await this.AddCommandParametersAsync(commandType, command, connection, transaction, filter).ConfigureAwait(false);

            if (command == null)
            {
                throw new MissingCommandException(commandType.ToString());
            }

            if (connection == null)
            {
                throw new MissingConnectionException();
            }

            if (connection.State != ConnectionState.Open)
            {
                throw new ConnectionClosedException(connection);
            }

            command.Connection  = connection;
            command.Transaction = transaction;

            // Get a lazy command instance
            var lazyCommand = commands.GetOrAdd(commandKey, k => new Lazy <SyncCommand>(() =>
            {
                var syncCommand = new SyncCommand(commandKey);
                return(syncCommand);
            }));

            // lazyCommand.Metadata is a boolean indicating if the command is already prepared on the server
            if (lazyCommand.Value.IsPrepared == true)
            {
                return(command, isBatch);
            }

            // Testing The Prepare() performance increase
            command.Prepare();

            // Adding this command as prepared
            lazyCommand.Value.IsPrepared = true;

            commands.AddOrUpdate(commandKey, lazyCommand, (key, lc) => new Lazy <SyncCommand>(() => lc.Value));

            return(command, isBatch);
        }
Ejemplo n.º 23
0
        public async Task DropTriggerAsync(DbCommandType triggerType, DbConnection connection, DbTransaction transaction)
        {
            var triggerName = string.Format(this.mySqlObjectNames.GetCommandName(triggerType).name, tableName.Unquoted().Normalized().ToString());
            var commandText = $"DROP TRIGGER IF EXISTS {triggerName}";

            using (var command = new MySqlCommand(commandText, (MySqlConnection)connection, (MySqlTransaction)transaction))
            {
                await command.ExecuteNonQueryAsync().ConfigureAwait(false);
            }
        }
Ejemplo n.º 24
0
        private async Task DropTriggerAsync(DbCommandType triggerType, DbConnection connection, DbTransaction transaction)
        {
            var triggerName = string.Format(this.sqliteObjectNames.GetCommandName(triggerType), tableName.Unquoted().ToString());
            var dropTrigger = $"DROP TRIGGER IF EXISTS {triggerName}";

            using (var command = new SqliteCommand(dropTrigger, (SqliteConnection)connection, (SqliteTransaction)transaction))
            {
                await command.ExecuteNonQueryAsync().ConfigureAwait(false);
            }
        }
Ejemplo n.º 25
0
        /// <summary>
        /// Check if we need to create the stored procedure
        /// </summary>
        public bool NeedToCreateProcedure(DbCommandType commandType)
        {
            if (connection.State != ConnectionState.Open)
            {
                throw new ArgumentException("Here, we need an opened connection please");
            }

            var commandName = this.sqlObjectNames.GetCommandName(commandType);

            return(!MySqlManagementUtils.ProcedureExists(connection, transaction, commandName));
        }
Ejemplo n.º 26
0
        /// <summary>
        /// Overriding adapter since the update metadata is not a stored proc that we can override
        /// </summary>
        public override DbCommand GetCommand(DbCommandType nameType, SyncFilter filter)
        {
            if (nameType == DbCommandType.UpdateMetadata)
            {
                var c = new SqlCommand("Set @sync_row_count = 1;");
                c.Parameters.Add("@sync_row_count", SqlDbType.Int);
                return(c);
            }

            return(base.GetCommand(nameType, filter));
        }
Ejemplo n.º 27
0
        /// <summary>
        /// Check if we need to create the stored procedure
        /// </summary>
        public async Task <bool> NeedToCreateProcedureAsync(DbCommandType commandType)
        {
            if (connection.State != ConnectionState.Open)
            {
                throw new ArgumentException("Here, we need an opened connection please");
            }

            var commandName = this.mySqlObjectNames.GetCommandName(commandType).name;

            return(!(await MySqlManagementUtils.ProcedureExistsAsync(connection, transaction, commandName).ConfigureAwait(false)));
        }
        public override (DbCommand, bool) GetCommand(DbCommandType commandType, SyncFilter filter = null)
        {
            var    command = new SqliteCommand();
            string text;

            text = this.sqliteObjectNames.GetCommandName(commandType, filter);

            // on Sqlite, everything is text :)
            command.CommandType = CommandType.Text;
            command.CommandText = text;

            return(command, false);
        }
Ejemplo n.º 29
0
        public override DbCommand GetCommand(DbCommandType nameType, SyncFilter filter)
        {
            var command = new SqlCommand();

            string text;
            bool   isStoredProc;

            (text, isStoredProc) = this.sqlObjectNames.GetCommandName(nameType, filter);

            command.CommandType = isStoredProc ? CommandType.StoredProcedure : CommandType.Text;
            command.CommandText = text;

            return(command);
        }
Ejemplo n.º 30
0
        public string GetCommandName(DbCommandType objectType, SyncFilter filter = null)
        {
            if (!names.ContainsKey(objectType))
            {
                throw new NotSupportedException($"Sqlite provider does not support the command type {objectType.ToString()}");
            }

            var commandName = names[objectType];

            // concat filter name
            //if (filter != null)
            //    commandName = string.Format(commandName, filter.GetFilterName());

            return(commandName);
        }