Exemplo n.º 1
0
        /// <summary>
        /// Create and return a command associated with the current connection.
        /// </summary>
        /// <param name="Connection">The connection.</param>
        /// <param name="Query">The query.</param>
        /// <param name="Arguments">The arguments.</param>
        private static DbCommand _CreateCommand(this DbConnection Connection, string Query, params object[] Arguments)
        {
            // Create a command associated with the connection.
            DbCommand Command = Connection.CreateCommand();

            // Set the text command.
            Command.CommandText = Query;
            // Add a number of arguments to the command.
            Command.Add(Arguments);
            // Return the command.
            return(Command);
        }
Exemplo n.º 2
0
 /// <summary>
 /// Add a number of arguments to the command.
 /// </summary>
 /// <param name="Command">The command.</param>
 /// <param name="Arguments">The arguments.</param>
 public static void Add(this DbCommand Command, params object[] Arguments)
 {
     // Check if an argument is available.
     if (Arguments != null)
     {
         // Iterate through each argument.
         foreach (object Argument in Arguments)
         {
             // Add an arguments to the command.
             Command.Add(Argument);
         }
     }
 }
Exemplo n.º 3
0
        /// <summary>
        /// Delete each object.
        /// </summary>
        /// <param name="Connection">The connection.</param>
        /// <param name="Transaction">The transaction.</param>
        /// <param name="Objects">The objects.</param>
        public async Task DeleteAsync(DbConnection Connection, DbTransaction Transaction, IEnumerable <T> Objects)
        {
            // Initialize a new instance of the List class.
            List <KeyValuePair <DbCommand, T> > CommandSet = new List <KeyValuePair <DbCommand, T> >();

            // Iterate through each object.
            foreach (T Object in Objects)
            {
                // Check if the object is valid.
                if (Object != null)
                {
                    // Retrieve the primary key.
                    KeyValuePair <string, object> PrimaryKey = _HasValidPrimaryKey(Object);
                    // Check whether the object has a valid primary key.
                    if (PrimaryKey.Value != null)
                    {
                        // Create and return a command associated with the current connection.
                        DbCommand Command = Connection.CreateCommand();
                        // Set the command.
                        Command.CommandText = string.Format("DELETE FROM {0} WHERE {1} = @{2}",
                                                            // ... with the table name ...
                                                            _TableName,
                                                            // ... with the primary key ...
                                                            PrimaryKey.Key,
                                                            // ... and with the parameter count.
                                                            Command.Parameters.Count
                                                            );
                        // Set the transaction.
                        Command.Transaction = Transaction;
                        // Add the argument to the command.
                        Command.Add(PrimaryKey.Value);
                        // Add the command to the command set.
                        CommandSet.Add(new KeyValuePair <DbCommand, T>(Command, null));
                    }
                }
            }
            // Execute a batch of commands and map an identity.
            await _BatchAsync(Connection, CommandSet);
        }
Exemplo n.º 4
0
        /// <summary>
        /// Save each object.
        /// </summary>
        /// <param name="Connection">The connection.</param>
        /// <param name="Transaction">The transaction.</param>
        /// <param name="Objects">The objects.</param>
        public async Task SaveAsync(DbConnection Connection, DbTransaction Transaction, IEnumerable <T> Objects)
        {
            // Initialize a new instance of the List class.
            List <KeyValuePair <DbCommand, T> > CommandSet = new List <KeyValuePair <DbCommand, T> >();

            // Iterate through each object.
            foreach (T Object in Objects)
            {
                // Check if the object is valid.
                if (Object != null)
                {
                    // Create a command associated with the connection.
                    DbCommand Command = Connection.CreateCommand();
                    // Initialize the validation boolean.
                    bool IsValid = false;
                    // Retrieve the primary key.
                    KeyValuePair <string, object> PrimaryKey = _HasValidPrimaryKey(Object);
                    // Check whether the object has a valid primary key.
                    if (PrimaryKey.Value != null)
                    {
                        // Initialize a new instance of the StringBuilder class.
                        StringBuilder StringBuilder = new StringBuilder();
                        // Create an update command.
                        _CreateUpdateValues(Object, (Key, Value) => {
                            // Set the validation boolean.
                            IsValid = true;
                            // Append the set command.
                            StringBuilder.AppendFormat("{0} = @{1},", Key, Command.Parameters.Count);
                            // Add the argument to the command.
                            Command.Add(Value);
                        });
                        // Check if the validation boolean has been set.
                        if (IsValid)
                        {
                            // Set the command.
                            Command.CommandText = string.Format("UPDATE {0} SET {1} WHERE {2} = @{3}",
                                                                // ... with the table name ...
                                                                _TableName,
                                                                // ... with the values ...
                                                                StringBuilder.ToString().Substring(0, StringBuilder.Length - 1),
                                                                // ... with the primary key ...
                                                                PrimaryKey.Key,
                                                                // ... and with the parameter count.
                                                                Command.Parameters.Count
                                                                );
                            // Add the argument to the command.
                            Command.Add(PrimaryKey.Value);
                            // Add the command to the command set.
                            CommandSet.Add(new KeyValuePair <DbCommand, T>(Command, null));
                        }
                    }
                    else
                    {
                        // Initialize a new instance of the StringBuilder class.
                        StringBuilder StringBuilderColumns = new StringBuilder();
                        // Initialize a new instance of the StringBuilder class.
                        StringBuilder StringBuilderValues = new StringBuilder();
                        // Create an insert command.
                        _CreateInsertValues(Object, (Key, Value) => {
                            // Set the validation boolean.
                            IsValid = true;
                            // Append the column to the command.
                            StringBuilderColumns.AppendFormat("{0},", Key);
                            // Append the value to the command.
                            StringBuilderValues.AppendFormat("@{0},", Command.Parameters.Count);
                            // Add the argument to the command.
                            Command.Add(Value);
                        });
                        // Check if the validation boolean has been set.
                        if (IsValid)
                        {
                            // Set the command ...
                            Command.CommandText = string.Format("INSERT INTO {0} ({1}) VALUES ({2})",
                                                                // ... with the table name ...
                                                                _TableName,
                                                                // ... with the columns ...
                                                                StringBuilderColumns.ToString().Substring(0, StringBuilderColumns.Length - 1),
                                                                // ... and with the values.
                                                                StringBuilderValues.ToString().Substring(0, StringBuilderValues.Length - 1)
                                                                );
                            // Add the command to the command set.
                            CommandSet.Add(new KeyValuePair <DbCommand, T>(Command, _Identity ? Object : null));
                        }
                    }
                }
            }
            // Execute a batch of commands.
            await _BatchAsync(Connection, CommandSet);
        }