Ejemplo n.º 1
0
        public static ISelectInsertable <TSource, TTarget> Value <TSource, TTarget, TValue>(
            [NotNull]                this ISelectInsertable <TSource, TTarget> source,
            [NotNull, InstantHandle] Expression <Func <TTarget, TValue> > field,
            TValue value)
        {
            if (source == null)
            {
                throw new ArgumentNullException("source");
            }
            if (field == null)
            {
                throw new ArgumentNullException("field");
            }

            var query = ((SelectInsertable <TSource, TTarget>)source).Query;

            var q = query.Provider.CreateQuery <TSource>(
                Expression.Call(
                    null,
                    _valueMethodInfo7.MakeGenericMethod(new[] { typeof(TSource), typeof(TTarget), typeof(TValue) }),
                    new[] { query.Expression, Expression.Quote(field), Expression.Constant(value, typeof(TValue)) }));

            return(new SelectInsertable <TSource, TTarget> {
                Query = q
            });
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Executes configured insert query asynchronously and returns inserted record.
        /// </summary>
        /// <typeparam name="TSource">Source query record type.</typeparam>
        /// <typeparam name="TTarget">Target table record type.</typeparam>
        /// <param name="source">Insert query.</param>
        /// <param name="outputTable">Output table.</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>(
            this ISelectInsertable <TSource, TTarget> source,
            ITable <TTarget> outputTable,
            CancellationToken token = default)
            where TTarget : notnull

        {
            if (source == null)
            {
                throw new ArgumentNullException(nameof(source));
            }
            if (outputTable == null)
            {
                throw new ArgumentNullException(nameof(outputTable));
            }

            var query = ((SelectInsertable <TSource, TTarget>)source).Query;

            var expr =
                Expression.Call(
                    null,
                    MethodHelper.GetMethodInfo(InsertWithOutputInto, source, outputTable),
                    query.Expression, ((IQueryable <TTarget>)outputTable).Expression);

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

            return(Task.Run(() => query.Provider.Execute <int>(expr), token));
        }
Ejemplo n.º 3
0
        public static int Insert <TSource, TTarget>([NotNull] this ISelectInsertable <TSource, TTarget> source)
        {
            if (source == null)
            {
                throw new ArgumentNullException(nameof(source));
            }

            var query = ((SelectInsertable <TSource, TTarget>)source).Query;

            return(query.Provider.Execute <int>(
                       Expression.Call(
                           null,
                           _insertMethodInfo4.MakeGenericMethod(typeof(TSource), typeof(TTarget)), query.Expression)));
        }
Ejemplo n.º 4
0
        public static object InsertWithIdentity <TSource, TTarget>([NotNull] this ISelectInsertable <TSource, TTarget> source)
        {
            if (source == null)
            {
                throw new ArgumentNullException("source");
            }

            var query = ((SelectInsertable <TSource, TTarget>)source).Query;

            return(query.Provider.Execute <object>(
                       Expression.Call(
                           null,
                           _insertWithIdentityMethodInfo4.MakeGenericMethod(new[] { typeof(TSource), typeof(TTarget) }),
                           new[] { query.Expression })));
        }
Ejemplo n.º 5
0
        public static int Insert <TSource, TTarget>([NotNull] this ISelectInsertable <TSource, TTarget> source)
        {
            if (source == null)
            {
                throw new ArgumentNullException("source");
            }

            var query = ((SelectInsertable <TSource, TTarget>)source).Query;

            return(query.Provider.Execute <int>(
                       Expression.Call(
                           null,
                           ((MethodInfo)MethodBase.GetCurrentMethod()).MakeGenericMethod(new[] { typeof(TSource), typeof(TTarget) }),
                           new[] { query.Expression })));
        }
Ejemplo n.º 6
0
        /// <summary>
        /// Executes configured insert query and returns inserted record.
        /// </summary>
        /// <typeparam name="TSource">Source query record type.</typeparam>
        /// <typeparam name="TTarget">Target table record type.</typeparam>
        /// <param name="source">Insert query.</param>
        /// <returns>Inserted record.</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 TTarget InsertWithOutput <TSource, TTarget>(this ISelectInsertable <TSource, TTarget> source)
        {
            if (source == null)
            {
                throw new ArgumentNullException(nameof(source));
            }

            var query = ((SelectInsertable <TSource, TTarget>)source).Query;

            var items = query.Provider.CreateQuery <TTarget>(
                Expression.Call(
                    null,
                    MethodHelper.GetMethodInfo(InsertWithOutput, source),
                    query.Expression));

            return(items.AsEnumerable().First());
        }
Ejemplo n.º 7
0
        /// <summary>
        /// Executes configured insert query and returns inserted record.
        /// </summary>
        /// <typeparam name="TSource">Source query record type.</typeparam>
        /// <typeparam name="TTarget">Target table record type.</typeparam>
        /// <param name="source">Insert query.</param>
        /// <param name="outputTable">Output table.</param>
        /// <returns>Number of affected records.</returns>
        public static int InsertWithOutputInto <TSource, TTarget>(
            this ISelectInsertable <TSource, TTarget> source,
            ITable <TTarget> outputTable)
        {
            if (source == null)
            {
                throw new ArgumentNullException(nameof(source));
            }
            if (outputTable == null)
            {
                throw new ArgumentNullException(nameof(outputTable));
            }

            var query = ((SelectInsertable <TSource, TTarget>)source).Query;

            return(query.Provider.Execute <int>(
                       Expression.Call(
                           null,
                           MethodHelper.GetMethodInfo(InsertWithOutputInto, source, outputTable),
                           query.Expression, ((IQueryable <TTarget>)outputTable).Expression)));
        }
Ejemplo n.º 8
0
 /// <summary>
 /// This is internal API and is not intended for use by Linq To DB applications.
 /// It may change or be removed without further notice.
 /// </summary>
 public static IDataContext GetDataContext <TSource, TTarget>(ISelectInsertable <TSource, TTarget> insertable)
 {
     return(GetDataContext(((LinqExtensions.SelectInsertable <TSource, TTarget>)insertable).Query));
 }