예제 #1
0
        /// <summary>
        /// Creates a command which will delete a range of entities, validating that the conditions has at least one property
        /// </summary>
        public static CommandDefinition MakeDeleteRangeCommand <TEntity>(
            object conditions,
            IDbTransaction transaction,
            IDialect dialect,
            int?commandTimeout,
            CancellationToken cancellationToken = default(CancellationToken))
        {
            Ensure.NotNull(conditions, nameof(conditions));
            var config = MicroCRUDConfig.Current;

            dialect = dialect ?? config.Dialect;

            var entityType       = typeof(TEntity);
            var tableSchema      = TableSchemaFactory.GetTableSchema(entityType, dialect, config.SchemaFactory);
            var conditionsSchema = TableSchemaFactory.GetConditionsSchema(entityType, tableSchema, conditions.GetType(), dialect, config.SchemaFactory);

            if (conditionsSchema.IsEmpty)
            {
                throw new ArgumentException(
                          "DeleteRange<TEntity> requires at least one condition, use DeleteAll<TEntity> to delete everything.");
            }

            var sql = dialect.MakeDeleteRangeStatement(tableSchema, dialect.MakeWhereClause(conditionsSchema, conditions));

            return(MakeCommandDefinition(sql, conditions, transaction, commandTimeout, cancellationToken));
        }
예제 #2
0
        /// <summary>
        /// Creates a command which will count how many entities are in the table.
        /// </summary>
        public static CommandDefinition MakeCountCommand <TEntity>(
            object conditions,
            IDbTransaction transaction,
            IDialect dialect,
            int?commandTimeout,
            CancellationToken cancellationToken = default(CancellationToken))
        {
            Ensure.NotNull(conditions, nameof(conditions));
            var config = MicroCRUDConfig.Current;

            dialect = dialect ?? config.Dialect;

            var entityType       = typeof(TEntity);
            var tableSchema      = TableSchemaFactory.GetTableSchema(entityType, dialect, config.SchemaFactory);
            var conditionsSchema = TableSchemaFactory.GetConditionsSchema(entityType, tableSchema, conditions.GetType(), dialect, config.SchemaFactory);
            var sql = dialect.MakeCountStatement(tableSchema, dialect.MakeWhereClause(conditionsSchema, conditions));

            return(MakeCommandDefinition(sql, conditions, transaction, commandTimeout, cancellationToken));
        }