Ejemplo n.º 1
0
        public static IMultiInsertInto <TSource> Into <TSource, TTarget>(
            this            IMultiInsertInto <TSource> source,
            ITable <TTarget> target,
            [InstantHandle] Expression <Func <TSource, TTarget> > setter)
            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));
            }

            var query = ((MultiInsertQuery <TSource>)source).Query;

            query = query.Provider.CreateQuery <TSource>(
                Expression.Call(
                    null,
                    Methods.Into.MakeGenericMethod(typeof(TSource), typeof(TTarget)),
                    query.Expression,
                    target.Expression,
                    Expression.Quote(setter)));

            return(new MultiInsertQuery <TSource>(query));
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Inserts source data into every configured table.
        /// </summary>
        /// <typeparam name="TSource">Source query record type.</typeparam>
        /// <param name="insert">Multi-table insert to perform.</param>
        /// <returns>Number of inserted rows.</returns>
        public static int Insert <TSource>(this IMultiInsertInto <TSource> insert)
        {
            if (insert == null)
            {
                throw new ArgumentNullException(nameof(insert));
            }

            IQueryable query = ((MultiInsertQuery <TSource>)insert).Query;

            query = LinqExtensions.ProcessSourceQueryable?.Invoke(query) ?? query;

            return(query.Provider.Execute <int>(
                       Expression.Call(
                           null,
                           Methods.Insert.MakeGenericMethod(typeof(TSource)),
                           query.Expression)));
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Asynchronously inserts source data into every configured table.
        /// </summary>
        /// <typeparam name="TSource">Source query record type.</typeparam>
        /// <param name="insert">Multi-table insert to perform.</param>
        /// <param name="token">Cancellation token for async operation.</param>
        /// <returns>Number of inserted rows.</returns>
        public static Task <int> InsertAsync <TSource>(this IMultiInsertInto <TSource> insert, CancellationToken token = default)
        {
            if (insert == null)
            {
                throw new ArgumentNullException(nameof(insert));
            }

            IQueryable query = ((MultiInsertQuery <TSource>)insert).Query;

            query = LinqExtensions.ProcessSourceQueryable?.Invoke(query) ?? query;

            var expr = Expression.Call(
                null,
                Methods.Insert.MakeGenericMethod(typeof(TSource)),
                query.Expression);

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

            return(Task.Run(() => query.Provider.Execute <int>(expr), token));
        }