示例#1
0
        /// <summary>
        /// Deletes records from source query into target table and outputs deleted records into <paramref name="outputTable"/>.
        /// </summary>
        /// <typeparam name="TSource">Source query record type.</typeparam>
        /// <typeparam name="TOutput">Output table record type.</typeparam>
        /// <param name="source">Source query, that returns data for delete operation.</param>
        /// <param name="outputTable">Output table.</param>
        /// <param name="outputExpression">Output record constructor expression.
        /// Expression supports only record new expression with field initializers.</param>
        /// <returns>Number of affected records.</returns>
        public static int DeleteWithOutputInto <TSource, TOutput>(
            this IQueryable <TSource> source,
            ITable <TOutput> outputTable,
            Expression <Func <TSource, TOutput> > outputExpression)
        {
            if (source == null)
            {
                throw new ArgumentNullException(nameof(source));
            }
            if (outputTable == null)
            {
                throw new ArgumentNullException(nameof(outputTable));
            }
            if (outputExpression == null)
            {
                throw new ArgumentNullException(nameof(outputExpression));
            }

            var currentSource = ProcessSourceQueryable?.Invoke(source) ?? source;

            return(source.Provider.Execute <int>(
                       Expression.Call(
                           null,
                           MethodHelper.GetMethodInfo(DeleteWithOutputInto, source, outputTable, outputExpression),
                           currentSource.Expression,
                           ((IQueryable <TOutput>)outputTable).Expression,
                           Expression.Quote(outputExpression))));
        }
示例#2
0
        /// <summary>
        /// Inserts records from source query into target table and outputs newly created records into <paramref name="outputTable"/>.
        /// </summary>
        /// <typeparam name="TSource">Source query record type.</typeparam>
        /// <typeparam name="TTarget">Target table record type.</typeparam>
        /// <param name="source">Source query, that returns data for insert operation.</param>
        /// <param name="target">Target table.</param>
        /// <param name="setter">Inserted record constructor expression.
        /// Expression supports only target table record new expression with field initializers.</param>
        /// <param name="outputTable">Output table.</param>
        /// <returns>Number of affected records.</returns>
        /// <remarks>
        /// Database support:
        /// <list type="bullet">
        /// <item>SQL Server 2005+</item>
        /// </list>
        /// </remarks>
        public static int InsertWithOutputInto <TSource, TTarget>(
            this IQueryable <TSource> source,
            ITable <TTarget> target,
            [InstantHandle] Expression <Func <TSource, TTarget> > setter,
            ITable <TTarget> outputTable)
            where TTarget : notnull
        {
            if (source == null)
            {
                throw new ArgumentNullException(nameof(source));
            }
            if (target == null)
            {
                throw new ArgumentNullException(nameof(target));
            }
            if (setter == null)
            {
                throw new ArgumentNullException(nameof(setter));
            }
            if (outputTable == null)
            {
                throw new ArgumentNullException(nameof(outputTable));
            }

            var currentSource = ProcessSourceQueryable?.Invoke(source) ?? source;

            return(currentSource.Provider.Execute <int>(
                       Expression.Call(
                           null,
                           MethodHelper.GetMethodInfo(InsertWithOutputInto, source, target, setter, outputTable),
                           currentSource.Expression, ((IQueryable <TTarget>)target).Expression, Expression.Quote(setter), ((IQueryable <TTarget>)outputTable).Expression)));
        }
示例#3
0
        /// <summary>
        /// Deletes records from source query into target table asynchronously and outputs deleted records into <paramref name="outputTable"/>.
        /// </summary>
        /// <typeparam name="TSource">Source query record type.</typeparam>
        /// <typeparam name="TOutput">Output table record type.</typeparam>
        /// <param name="source">Source query, that returns data for delete operation.</param>
        /// <param name="outputTable">Output table.</param>
        /// <param name="token">Optional asynchronous operation cancellation token.</param>
        /// <returns>Number of affected records.</returns>
        public static Task <int> DeleteWithOutputIntoAsync <TSource, TOutput>(
            this IQueryable <TSource> source,
            ITable <TOutput> outputTable,
            CancellationToken token = default)
        {
            if (source == null)
            {
                throw new ArgumentNullException(nameof(source));
            }
            if (outputTable == null)
            {
                throw new ArgumentNullException(nameof(outputTable));
            }

            var currentSource = ProcessSourceQueryable?.Invoke(source) ?? source;

            var expr =
                Expression.Call(
                    null,
                    MethodHelper.GetMethodInfo(DeleteWithOutputInto, source, outputTable),
                    currentSource.Expression,
                    ((IQueryable <TOutput>)outputTable).Expression);

            if (source is IQueryProviderAsync queryAsync)
            {
                return(queryAsync.ExecuteAsync <int>(expr, token));
            }

            return(TaskEx.Run(() => source.Provider.Execute <int>(expr), token));
        }
示例#4
0
        /// <summary>
        /// Inserts records from source query into target table asynchronously and returns newly created records.
        /// </summary>
        /// <typeparam name="TSource">Source query record type.</typeparam>
        /// <typeparam name="TTarget">Target table record type.</typeparam>
        /// <typeparam name="TOutput">Output table record type.</typeparam>
        /// <param name="source">Source query, that returns data for insert operation.</param>
        /// <param name="target">Target table.</param>
        /// <param name="setter">Inserted record constructor expression.
        /// Expression supports only target table record new expression with field initializers.</param>
        /// <param name="outputExpression">Output record constructor expression.
        /// Expression supports only record new expression with field initializers.</param>
        /// <param name="token">Optional asynchronous operation cancellation token.</param>
        /// <returns>Array of records.</returns>
        /// <remarks>
        /// Database support:
        /// <list type="bullet">
        /// <item>SQL Server 2005+</item>
        /// <item>Firebird 2.5+ (doesn't support more than one record; database limitation)</item>
        /// <item>PostgreSQL</item>
        /// <item>SQLite 3.35+</item>
        /// <item>MariaDB 10.5+</item>
        /// </list>
        /// </remarks>
        public static Task <TOutput[]> InsertWithOutputAsync <TSource, TTarget, TOutput>(
            this IQueryable <TSource> source,
            ITable <TTarget> target,
            [InstantHandle] Expression <Func <TSource, TTarget> > setter,
            Expression <Func <TTarget, TOutput> > outputExpression,
            CancellationToken token = default)
            where TTarget : notnull
        {
            if (source == null)
            {
                throw new ArgumentNullException(nameof(source));
            }
            if (target == null)
            {
                throw new ArgumentNullException(nameof(target));
            }
            if (setter == null)
            {
                throw new ArgumentNullException(nameof(setter));
            }
            if (outputExpression == null)
            {
                throw new ArgumentNullException(nameof(outputExpression));
            }

            var currentSource = ProcessSourceQueryable?.Invoke(source) ?? source;

            return(currentSource.Provider.CreateQuery <TOutput>(
                       Expression.Call(
                           null,
                           MethodHelper.GetMethodInfo(InsertWithOutput, source, target, setter, outputExpression),
                           currentSource.Expression, ((IQueryable <TTarget>)target).Expression, Expression.Quote(setter),
                           Expression.Quote(outputExpression)))
                   .ToArrayAsync(token));
        }
示例#5
0
        public static IEnumerable <TOutput> InsertWithOutput <TSource, TTarget, TOutput>(
            this IQueryable <TSource> source,
            ITable <TTarget> target,
            [InstantHandle] Expression <Func <TSource, TTarget> > setter,
            Expression <Func <TTarget, TOutput> > outputExpression)
        {
            if (source == null)
            {
                throw new ArgumentNullException(nameof(source));
            }
            if (target == null)
            {
                throw new ArgumentNullException(nameof(target));
            }
            if (setter == null)
            {
                throw new ArgumentNullException(nameof(setter));
            }
            if (outputExpression == null)
            {
                throw new ArgumentNullException(nameof(outputExpression));
            }

            var currentSource = ProcessSourceQueryable?.Invoke(source) ?? source;

            return(currentSource.Provider.CreateQuery <TOutput>(
                       Expression.Call(
                           null,
                           MethodHelper.GetMethodInfo(InsertWithOutput, source, target, setter, outputExpression),
                           currentSource.Expression, ((IQueryable <TTarget>)target).Expression, Expression.Quote(setter),
                           Expression.Quote(outputExpression)))
                   .AsEnumerable());
        }
示例#6
0
        /// <summary>
        /// Deletes records from source query and returns deleted records.
        /// </summary>
        /// <typeparam name="TSource">Source query record type.</typeparam>
        /// <param name="source">Source query, that returns data for delete operation.</param>
        /// <returns>Enumeration of records.</returns>
        /// <remarks>
        /// Database support:
        /// <list type="bullet">
        /// <item>SQL Server 2005+</item>
        /// <item>Firebird 2.5+ (doesn't support more than one record; database limitation)</item>
        /// <item>PostgreSQL</item>
        /// <item>SQLite 3.35+</item>
        /// <item>MariaDB 10.0+ (doesn't support multi-table statements; database limitation)</item>
        /// </list>
        /// </remarks>
        public static IEnumerable <TSource> DeleteWithOutput <TSource>(this IQueryable <TSource> source)
        {
            if (source == null)
            {
                throw new ArgumentNullException(nameof(source));
            }

            var currentSource = ProcessSourceQueryable?.Invoke(source) ?? source;

            return(currentSource.Provider.CreateQuery <TSource>(
                       Expression.Call(
                           null,
                           MethodHelper.GetMethodInfo(DeleteWithOutput, source),
                           currentSource.Expression))
                   .AsEnumerable());
        }
示例#7
0
        /// <summary>
        /// Inserts records from source query into target table asynchronously and outputs inserted records into <paramref name="outputTable"/>.
        /// </summary>
        /// <typeparam name="TSource">Source query record type.</typeparam>
        /// <typeparam name="TTarget">Target table record type.</typeparam>
        /// <typeparam name="TOutput">Output table record type.</typeparam>
        /// <param name="source">Source query, that returns data for insert operation.</param>
        /// <param name="target">Target table.</param>
        /// <param name="setter">Inserted record constructor expression.
        /// Expression supports only target table record new expression with field initializers.</param>
        /// <param name="outputTable">Output table.</param>
        /// <param name="outputExpression">Output record constructor expression.
        /// Expression supports only record new expression with field initializers.</param>
        /// <param name="token">Optional asynchronous operation cancellation token.</param>
        /// <returns>Number of affected records.</returns>
        /// <remarks>
        /// Database support:
        /// <list type="bullet">
        /// <item>SQL Server 2005+</item>
        /// </list>
        /// </remarks>
        public static Task <int> InsertWithOutputIntoAsync <TSource, TTarget, TOutput>(
            this IQueryable <TSource> source,
            ITable <TTarget> target,
            [InstantHandle] Expression <Func <TSource, TTarget> > setter,
            ITable <TOutput> outputTable,
            Expression <Func <TTarget, TOutput> > outputExpression,
            CancellationToken token = default)
            where TOutput : notnull
            where TTarget : notnull
        {
            if (source == null)
            {
                throw new ArgumentNullException(nameof(source));
            }
            if (target == null)
            {
                throw new ArgumentNullException(nameof(target));
            }
            if (setter == null)
            {
                throw new ArgumentNullException(nameof(setter));
            }
            if (outputTable == null)
            {
                throw new ArgumentNullException(nameof(outputTable));
            }
            if (outputExpression == null)
            {
                throw new ArgumentNullException(nameof(outputExpression));
            }

            var currentSource = ProcessSourceQueryable?.Invoke(source) ?? source;

            var expr =
                Expression.Call(
                    null,
                    MethodHelper.GetMethodInfo(InsertWithOutputInto, source, target, setter, outputTable, outputExpression),
                    currentSource.Expression, ((IQueryable <TTarget>)target).Expression, Expression.Quote(setter),
                    ((IQueryable <TTarget>)outputTable).Expression, Expression.Quote(outputExpression));

            if (currentSource is IQueryProviderAsync queryAsync)
            {
                return(queryAsync.ExecuteAsync <int>(expr, token));
            }

            return(Task.Run(() => currentSource.Provider.Execute <int>(expr), token));
        }
示例#8
0
        /// <summary>
        /// Deletes records from source query into target table asynchronously and returns deleted records.
        /// </summary>
        /// <typeparam name="TSource">Source query record type.</typeparam>
        /// <param name="source">Source query, that returns data for delete operation.</param>
        /// <param name="token">Optional asynchronous operation cancellation token.</param>
        /// <returns>Array of records.</returns>
        public static Task <TSource[]> DeleteWithOutput <TSource>(
            this IQueryable <TSource> source,
            CancellationToken token = default)
        {
            if (source == null)
            {
                throw new ArgumentNullException(nameof(source));
            }

            var currentSource = ProcessSourceQueryable?.Invoke(source) ?? source;

            return(currentSource.Provider.CreateQuery <TSource>(
                       Expression.Call(
                           null,
                           MethodHelper.GetMethodInfo(DeleteWithOutput, source),
                           currentSource.Expression))
                   .ToArrayAsync(token));
        }
        /// <summary>
        /// Inserts single record into target table.
        /// </summary>
        /// <typeparam name="T">Inserted record type.</typeparam>
        /// <param name="target">Target table.</param>
        /// <param name="setter">Insert expression. Expression supports only target table record new expression with field initializers.</param>
        /// <returns>Number of affected records.</returns>
        public static int Insert <T>(
            this IQueryable <T> target,
            Expression <Func <T> > setter)
        {
            if (target == null)
            {
                throw new ArgumentNullException(nameof(target));
            }
            if (setter == null)
            {
                throw new ArgumentNullException(nameof(setter));
            }

            IQueryable <T> query = target;

            var currentQuery = ProcessSourceQueryable?.Invoke(query) ?? query;

            return(currentQuery.Provider.Execute <int>(
                       Expression.Call(
                           null,
                           _insertMethodInfo.MakeGenericMethod(typeof(T)),
                           new[] { currentQuery.Expression, Expression.Quote(setter) })));
        }
示例#10
0
        public static ILoadWithQueryable <TEntity, TProperty> ThenLoad <TEntity, TPreviousProperty, TProperty>(
            this ILoadWithQueryable <TEntity, IEnumerable <TPreviousProperty> > source,
            [InstantHandle] Expression <Func <TPreviousProperty, TProperty> > selector)
            where TEntity : class
        {
            if (source == null)
            {
                throw new ArgumentNullException(nameof(source));
            }
            if (selector == null)
            {
                throw new ArgumentNullException(nameof(selector));
            }

            var currentSource = ProcessSourceQueryable?.Invoke(source) ?? source;

            var result = currentSource.Provider.CreateQuery <TEntity>(
                Expression.Call(null,
                                MethodHelper.GetMethodInfo(ThenLoad, source, selector),
                                new[] { currentSource.Expression, Expression.Quote(selector) }));

            return(new LoadWithQueryable <TEntity, TProperty>(result));
        }
示例#11
0
        public static ILoadWithQueryable <TEntity, TProperty> LoadWith <TEntity, TProperty>(
            this IQueryable <TEntity> source,
            [InstantHandle] Expression <Func <TEntity, TProperty> > selector,
            [InstantHandle] Expression <Func <IQueryable <TProperty>, IQueryable <TProperty> > > loadFunc)
            where TEntity : class
        {
            if (source == null)
            {
                throw new ArgumentNullException(nameof(source));
            }
            if (selector == null)
            {
                throw new ArgumentNullException(nameof(selector));
            }

            var currentSource = ProcessSourceQueryable?.Invoke(source) ?? source;

            var result = currentSource.Provider.CreateQuery <TEntity>(
                Expression.Call(null,
                                MethodHelper.GetMethodInfo(LoadWith, source, selector, loadFunc),
                                currentSource.Expression, Expression.Quote(selector), Expression.Quote(loadFunc)));

            return(new LoadWithQueryable <TEntity, TProperty>(result));
        }