Ejemplo n.º 1
0
        public MultipleRowDbCommandBuilder <MySqlCommand, MySqlParameter> DeleteByKeyList <TKey>(MySqlObjectName tableName, IEnumerable <TKey> keys, DeleteOptions options = DeleteOptions.None)
        {
            var primaryKeys = DatabaseMetadata.GetTableOrView(tableName).PrimaryKeyColumns;

            if (primaryKeys.Count != 1)
            {
                throw new MappingException($"{nameof(DeleteByKeyList)} operation isn't allowed on {tableName} because it doesn't have a single primary key.");
            }

            var keyList        = keys.AsList();
            var columnMetadata = primaryKeys.Single();

            string where;
            if (keys.Count() > 1)
            {
                where = columnMetadata.SqlName + " IN (" + string.Join(", ", keyList.Select((s, i) => "@Param" + i)) + ")";
            }
            else
            {
                where = columnMetadata.SqlName + " = @Param0";
            }

            var parameters = new List <MySqlParameter>();

            for (var i = 0; i < keyList.Count; i++)
            {
                var param = new MySqlParameter("@Param" + i, keyList[i]);
                if (columnMetadata.DbType.HasValue)
                {
                    param.MySqlDbType = columnMetadata.DbType.Value;
                }
                parameters.Add(param);
            }

            var table = DatabaseMetadata.GetTableOrView(tableName);

            if (!AuditRules.UseSoftDelete(table))
            {
                return(new MySqlDeleteMany(this, tableName, where, parameters, parameters.Count, options));
            }

            UpdateOptions effectiveOptions = UpdateOptions.SoftDelete;

            if (!options.HasFlag(DeleteOptions.CheckRowsAffected))
            {
                effectiveOptions |= UpdateOptions.IgnoreRowsAffected;
            }

            if (options.HasFlag(DeleteOptions.UseKeyAttribute))
            {
                effectiveOptions |= UpdateOptions.UseKeyAttribute;
            }

            return(new MySqlUpdateMany(this, tableName, null, where, parameters, parameters.Count, effectiveOptions));
        }
        /// <summary>
        /// Delete multiple records using a where expression.
        /// </summary>
        /// <param name="tableName">Name of the table.</param>
        /// <param name="whereClause">The where clause.</param>
        /// <param name="argumentValue">The argument value for the where clause.</param>
        public MultipleRowDbCommandBuilder <AbstractCommand, AbstractParameter> DeleteWithFilter(AbstractObjectName tableName, string whereClause, object argumentValue)
        {
            var table = DatabaseMetadata.GetTableOrView(tableName);

            if (!AuditRules.UseSoftDelete(table))
            {
                return(OnDeleteMany(tableName, whereClause, argumentValue));
            }

            return(OnUpdateMany(tableName, null, UpdateOptions.SoftDelete | UpdateOptions.IgnoreRowsAffected).WithFilter(whereClause, argumentValue));
        }
        /// <summary>
        /// Delete multiple records using a filter object.
        /// </summary>
        /// <param name="tableName">Name of the table.</param>
        /// <param name="filterValue">The filter value.</param>
        /// <param name="filterOptions">The options.</param>
        public MultipleRowDbCommandBuilder <AbstractCommand, AbstractParameter> DeleteWithFilter(AbstractObjectName tableName, object filterValue, FilterOptions filterOptions = FilterOptions.None)
        {
            var table = DatabaseMetadata.GetTableOrView(tableName);

            if (!AuditRules.UseSoftDelete(table))
            {
                return(OnDeleteMany(tableName, filterValue, filterOptions));
            }

            return(OnUpdateMany(tableName, null, UpdateOptions.SoftDelete | UpdateOptions.IgnoreRowsAffected).WithFilter(filterValue, filterOptions));
        }
Ejemplo n.º 4
0
        /// <summary>
        /// Deletes multiple records using a where expression.
        /// </summary>
        /// <param name="tableName">Name of the table.</param>
        /// <param name="whereClause">The where clause.</param>
        public MultipleRowDbCommandBuilder <SQLiteCommand, SQLiteParameter> DeleteWithFilter(SQLiteObjectName tableName, string whereClause)
        {
            var table = DatabaseMetadata.GetTableOrView(tableName);

            if (!AuditRules.UseSoftDelete(table))
            {
                return(new SQLiteDeleteMany(this, tableName, whereClause, null));
            }

            return(new SQLiteUpdateMany(this, tableName, null, UpdateOptions.SoftDelete | UpdateOptions.IgnoreRowsAffected).WithFilter(whereClause, null));
        }
Ejemplo n.º 5
0
        /// <summary>
        /// Deletes multiple records using a filter object.
        /// </summary>
        /// <param name="tableName">Name of the table.</param>
        /// <param name="filterValue">The filter value.</param>
        /// <param name="filterOptions">The options.</param>
        public MultipleRowDbCommandBuilder <OleDbCommand, OleDbParameter> DeleteWithFilter(SqlServerObjectName tableName, object filterValue, FilterOptions filterOptions = FilterOptions.None)
        {
            var table = DatabaseMetadata.GetTableOrView(tableName);

            if (!AuditRules.UseSoftDelete(table))
            {
                return(new OleDbSqlServerDeleteMany(this, tableName, filterValue, filterOptions));
            }

            return(new OleDbSqlServerUpdateMany(this, tableName, null, UpdateOptions.SoftDelete | UpdateOptions.IgnoreRowsAffected).WithFilter(filterValue, filterOptions));
        }
Ejemplo n.º 6
0
        /// <summary>
        /// Deletes multiple records using a where expression.
        /// </summary>
        /// <param name="tableName">Name of the table.</param>
        /// <param name="whereClause">The where clause.</param>
        /// <param name="argumentValue">The argument value for the where clause.</param>
        public MultipleRowDbCommandBuilder <OleDbCommand, OleDbParameter> DeleteWithFilter(SqlServerObjectName tableName, string whereClause, object argumentValue)
        {
            var table = DatabaseMetadata.GetTableOrView(tableName);

            if (!AuditRules.UseSoftDelete(table))
            {
                return(new OleDbSqlServerDeleteMany(this, tableName, whereClause, argumentValue));
            }

            return(new OleDbSqlServerUpdateMany(this, tableName, null, UpdateOptions.SoftDelete | UpdateOptions.IgnoreRowsAffected).WithFilter(whereClause, argumentValue));
        }
Ejemplo n.º 7
0
    MultipleRowDbCommandBuilder <AbstractCommand, AbstractParameter> IUpdateDeleteByKeyHelper <AbstractCommand, AbstractParameter, AbstractObjectName, AbstractDbType> .OnDeleteByKeyList <TKey>(AbstractObjectName tableName, IEnumerable <TKey> keys, DeleteOptions options)
    {
        var primaryKeys = DatabaseMetadata.GetTableOrView(tableName).PrimaryKeyColumns;

        if (primaryKeys.Count != 1)
        {
            throw new MappingException($"{nameof(DeleteByKeyList)} operation isn't allowed on {tableName} because it doesn't have a single primary key.");
        }

        var keyList        = keys.AsList();
        var columnMetadata = primaryKeys.Single();

        string where;
        if (keys.Count() > 1)
        {
            where = columnMetadata.SqlName + " IN (" + string.Join(", ", keyList.Select((s, i) => "?")) + ")";
        }
        else
        {
            where = columnMetadata.SqlName + " = ?";
        }

        var parameters = new List <OleDbParameter>();

        for (var i = 0; i < keyList.Count; i++)
        {
            var param = new OleDbParameter("@Param" + i, keyList[i]);
            if (columnMetadata.DbType.HasValue)
            {
                param.OleDbType = columnMetadata.DbType.Value;
            }
            parameters.Add(param);
        }

        var table = DatabaseMetadata.GetTableOrView(tableName);

        if (!AuditRules.UseSoftDelete(table))
        {
            return(new OleDbSqlServerDeleteSet(this, tableName, where, parameters, parameters.Count, options));
        }

        UpdateOptions effectiveOptions = UpdateOptions.SoftDelete | UpdateOptions.IgnoreRowsAffected;

        if (options.HasFlag(DeleteOptions.UseKeyAttribute))
        {
            effectiveOptions = effectiveOptions | UpdateOptions.UseKeyAttribute;
        }

        return(new OleDbSqlServerUpdateSet(this, tableName, null, where, parameters, parameters.Count, effectiveOptions));
    }
Ejemplo n.º 8
0
        /// <summary>
        /// Deletes an object model from the table indicated by the class's Table attribute.
        /// </summary>
        /// <typeparam name="TArgument"></typeparam>
        /// <param name="argumentValue">The argument value.</param>
        /// <param name="options">The delete options.</param>
        /// <returns></returns>
        public ObjectDbCommandBuilder <SqlCommand, SqlParameter, TArgument> Delete <TArgument>(TArgument argumentValue, DeleteOptions options = DeleteOptions.None) where TArgument : class
        {
            var table = DatabaseMetadata.GetTableOrViewFromClass <TArgument>();

            if (!AuditRules.UseSoftDelete(table))
            {
                return(new SqlServerDeleteObject <TArgument>(this, table.Name, argumentValue, options));
            }

            UpdateOptions effectiveOptions = UpdateOptions.SoftDelete;

            if (options.HasFlag(DeleteOptions.UseKeyAttribute))
            {
                effectiveOptions = effectiveOptions | UpdateOptions.UseKeyAttribute;
            }

            return(new SqlServerUpdateObject <TArgument>(this, table.Name, argumentValue, effectiveOptions));
        }
        /// <summary>
        /// Creates a command to perform a delete operation.
        /// </summary>
        /// <param name="tableName"></param>
        /// <param name="argumentValue"></param>
        /// <param name="options"></param>
        /// <returns></returns>
        public ObjectDbCommandBuilder <AbstractCommand, AbstractParameter, TArgument> Delete <TArgument>(AbstractObjectName tableName, TArgument argumentValue, DeleteOptions options = DeleteOptions.None)
            where TArgument : class
        {
            var table = DatabaseMetadata.GetTableOrView(tableName);

            if (!AuditRules.UseSoftDelete(table))
            {
                return(OnDeleteObject <TArgument>(tableName, argumentValue, options));
            }

            UpdateOptions effectiveOptions = UpdateOptions.SoftDelete | UpdateOptions.IgnoreRowsAffected;

            if (options.HasFlag(DeleteOptions.UseKeyAttribute))
            {
                effectiveOptions = effectiveOptions | UpdateOptions.UseKeyAttribute;
            }

            return(OnUpdateObject <TArgument>(tableName, argumentValue, effectiveOptions));
        }