Example #1
0
            protected override IBuildContext BuildMethodCall(ExpressionBuilder builder, MethodCallExpression methodCall, BuildInfo buildInfo)
            {
                var mergeContext = (MergeContext)builder.BuildSequence(new BuildInfo(buildInfo, methodCall.Arguments[0]));

                var statement = mergeContext.Merge;
                var operation = new SqlMergeOperationClause(MergeOperationType.Insert);

                statement.Operations.Add(operation);

                var predicate = methodCall.Arguments[1];
                var setter    = methodCall.Arguments[2];

                if (!(setter is ConstantExpression constSetter) || constSetter.Value != null)
                {
                    var setterExpression = (LambdaExpression)setter.Unwrap();
                    mergeContext.AddSourceParameter(setterExpression.Parameters[0]);

                    UpdateBuilder.BuildSetterWithContext(
                        builder,
                        buildInfo,
                        setterExpression,
                        mergeContext.TargetContext,
                        operation.Items,
                        mergeContext.SourceContext);
                }
Example #2
0
            protected override IBuildContext BuildMethodCall(ExpressionBuilder builder, MethodCallExpression methodCall, BuildInfo buildInfo)
            {
                // UpdateWhenMatchedAndThenDelete(merge, searchCondition, setter, deleteCondition)
                var mergeContext = (MergeContext)builder.BuildSequence(new BuildInfo(buildInfo, methodCall.Arguments[0]));

                var statement = mergeContext.Merge;
                var operation = new SqlMergeOperationClause(MergeOperationType.UpdateWithDelete);

                statement.Operations.Add(operation);

                var predicate       = methodCall.Arguments[1];
                var setter          = methodCall.Arguments[2];
                var deletePredicate = methodCall.Arguments[3];

                if (!(setter is ConstantExpression constSetter) || constSetter.Value != null)
                {
                    var setterExpression = (LambdaExpression)setter.Unwrap();
                    UpdateBuilder.BuildSetterWithContext(
                        builder,
                        buildInfo,
                        setterExpression,
                        mergeContext.TargetContext,
                        operation.Items,
                        mergeContext.TargetContext, mergeContext.SourceContext);
                }
            protected override IBuildContext BuildMethodCall(ExpressionBuilder builder, MethodCallExpression methodCall, BuildInfo buildInfo)
            {
                // UpdateWhenMatchedAndThenDelete(merge, searchCondition, setter, deleteCondition)
                var mergeContext = (MergeContext)builder.BuildSequence(new BuildInfo(buildInfo, methodCall.Arguments[0]));

                var statement = mergeContext.Merge;
                var operation = new SqlMergeOperationClause(MergeOperationType.UpdateWithDelete);

                statement.Operations.Add(operation);

                var predicate       = methodCall.Arguments[1];
                var setter          = methodCall.Arguments[2];
                var deletePredicate = methodCall.Arguments[3];

                if (!setter.IsNullValue())
                {
                    var setterExpression = (LambdaExpression)setter.Unwrap();
                    UpdateBuilder.BuildSetterWithContext(
                        builder,
                        buildInfo,
                        setterExpression,
                        mergeContext.TargetContext,
                        operation.Items,
                        mergeContext.TargetContext, mergeContext.SourceContext);
                }
                else
                {
                    // build setters like QueryRunner.Update
                    var sqlTable = (SqlTable)statement.Target.Source;
                    var param    = Expression.Parameter(sqlTable.ObjectType, "s");
                    var keys     = sqlTable.GetKeys(false).Cast <SqlField>().ToList();

                    foreach (var field in sqlTable.Fields.Where(f => f.IsUpdatable).Except(keys))
                    {
                        var expression = LinqToDB.Expressions.Extensions.GetMemberGetter(field.ColumnDescriptor.MemberInfo, param);
                        var tgtExpr    = mergeContext.TargetContext.ConvertToSql(builder.ConvertExpression(expression), 1, ConvertFlags.Field)[0].Sql;
                        var srcExpr    = mergeContext.SourceContext.ConvertToSql(builder.ConvertExpression(expression), 1, ConvertFlags.Field)[0].Sql;

                        operation.Items.Add(new SqlSetExpression(tgtExpr, srcExpr));
                    }
                }

                if (!predicate.IsNullValue())
                {
                    var predicateCondition = (LambdaExpression)predicate.Unwrap();

                    operation.Where = BuildSearchCondition(builder, statement, mergeContext.TargetContext, mergeContext.SourceContext, predicateCondition);
                }

                if (!deletePredicate.IsNullValue())
                {
                    var deleteCondition = (LambdaExpression)deletePredicate.Unwrap();

                    operation.WhereDelete = BuildSearchCondition(builder, statement, mergeContext.TargetContext, mergeContext.SourceContext, deleteCondition);;
                }

                return(mergeContext);
            }
Example #4
0
        protected override IBuildContext BuildMethodCall(ExpressionBuilder builder, MethodCallExpression methodCall, BuildInfo buildInfo)
        {
            var mergeContext = (MergeContext)builder.BuildSequence(new BuildInfo(buildInfo, methodCall.Arguments[0]));

            var kind = MergeKind.Merge;

            if (methodCall.IsSameGenericMethod(MergeWithOutputInto))
            {
                kind = MergeKind.MergeWithOutputInto;
            }
            else if (methodCall.IsSameGenericMethod(MergeWithOutput))
            {
                kind = MergeKind.MergeWithOutput;
            }

            if (kind != MergeKind.Merge)
            {
                var objectType = methodCall.Method.GetGenericArguments()[0];

                var actionField   = SqlField.FakeField(new DbDataType(typeof(string)), "$action", false);
                var insertedTable = SqlTable.Inserted(objectType);
                var deletedTable  = SqlTable.Deleted(objectType);

                mergeContext.Merge.Output = new SqlOutputClause()
                {
                    InsertedTable = insertedTable,
                    DeletedTable  = deletedTable,
                };

                var selectQuery = new SelectQuery();

                var actionFieldContext  = new SingleExpressionContext(null, builder, actionField, selectQuery);
                var deletedTableContext = new TableBuilder.TableContext(builder, selectQuery, deletedTable);
                var insertedTableConext = new TableBuilder.TableContext(builder, selectQuery, insertedTable);

                if (kind == MergeKind.MergeWithOutput)
                {
                    var outputExpression = (LambdaExpression)methodCall.Arguments[1].Unwrap();

                    var outputContext = new MergeOutputContext(
                        buildInfo.Parent,
                        outputExpression,
                        mergeContext,
                        actionFieldContext,
                        deletedTableContext,
                        insertedTableConext
                        );

                    return(outputContext);
                }
                else
                {
                    var outputExpression = (LambdaExpression)methodCall.Arguments[2].Unwrap();

                    var outputTable = methodCall.Arguments[1];
                    var destination = builder.BuildSequence(new BuildInfo(buildInfo, outputTable, new SelectQuery()));

                    UpdateBuilder.BuildSetterWithContext(
                        builder,
                        buildInfo,
                        outputExpression,
                        destination,
                        mergeContext.Merge.Output.OutputItems,
                        actionFieldContext,
                        deletedTableContext,
                        insertedTableConext
                        );

                    mergeContext.Merge.Output.OutputTable = ((TableBuilder.TableContext)destination).SqlTable;
                }
            }

            return(mergeContext);
        }
            protected override IBuildContext BuildMethodCall(ExpressionBuilder builder, MethodCallExpression methodCall, BuildInfo buildInfo)
            {
                var mergeContext = (MergeContext)builder.BuildSequence(new BuildInfo(buildInfo, methodCall.Arguments[0]));

                var statement = mergeContext.Merge;
                var operation = new SqlMergeOperationClause(MergeOperationType.Insert);

                statement.Operations.Add(operation);

                var predicate = methodCall.Arguments[1];
                var setter    = methodCall.Arguments[2];

                if (!setter.IsNullValue())
                {
                    var setterExpression = (LambdaExpression)setter.Unwrap();
                    mergeContext.AddSourceParameter(setterExpression.Parameters[0]);

                    UpdateBuilder.BuildSetterWithContext(
                        builder,
                        buildInfo,
                        setterExpression,
                        mergeContext.TargetContext,
                        operation.Items,
                        mergeContext.SourceContext);
                }
                else
                {
                    // build setters like QueryRunner.Insert
                    var sqlTable = (SqlTable)statement.Target.Source;
                    var param    = Expression.Parameter(sqlTable.ObjectType, "s");

                    foreach (var field in sqlTable.Fields)
                    {
                        if (field.IsInsertable)
                        {
                            var expression = LinqToDB.Expressions.Extensions.GetMemberGetter(field.ColumnDescriptor.MemberInfo, param);
                            var tgtExpr    = mergeContext.TargetContext.ConvertToSql(builder.ConvertExpression(expression), 1, ConvertFlags.Field)[0].Sql;
                            var srcExpr    = mergeContext.SourceContext.ConvertToSql(builder.ConvertExpression(expression), 1, ConvertFlags.Field)[0].Sql;

                            operation.Items.Add(new SqlSetExpression(tgtExpr, srcExpr));
                        }
                        else if (field.IsIdentity)
                        {
                            var expr = builder.DataContext.CreateSqlProvider().GetIdentityExpression(sqlTable);

                            if (expr != null)
                            {
                                operation.Items.Add(new SqlSetExpression(field, expr));
                            }
                        }
                    }
                }

                if (!predicate.IsNullValue())
                {
                    var condition     = (LambdaExpression)predicate.Unwrap();
                    var conditionExpr = builder.ConvertExpression(condition.Body.Unwrap());

                    operation.Where = new SqlSearchCondition();

                    builder.BuildSearchCondition(
                        new ExpressionContext(null, new[] { mergeContext.SourceContext }, condition),
                        conditionExpr,
                        operation.Where.Conditions);
                }

                return(mergeContext);
            }