protected override IBuildContext BuildMethodCall(ExpressionBuilder builder, MethodCallExpression methodCall, BuildInfo buildInfo) { var sequence = builder.BuildSequence(new BuildInfo(buildInfo, methodCall.Arguments[0])); var isSubQuery = sequence.SelectQuery.Select.IsDistinct; if (isSubQuery) { sequence = new SubQueryContext(sequence); } if (!(sequence.Statement is SqlInsertStatement insertStatement)) { insertStatement = new SqlInsertStatement(sequence.SelectQuery); sequence.Statement = insertStatement; } var insertType = InsertContext.InsertType.Insert; switch (methodCall.Method.Name) { case "Insert": insertType = InsertContext.InsertType.Insert; break; case "InsertWithIdentity": insertType = InsertContext.InsertType.InsertWithIdentity; break; case "InsertWithOutput": insertType = InsertContext.InsertType.InsertOutput; break; case "InsertWithOutputInto": insertType = InsertContext.InsertType.InsertOutputInto; break; } var indexedParameters = methodCall.Method.GetParameters().Select((p, i) => Tuple.Create(p, i)).ToDictionary(t => t.Item1.Name, t => t.Item2); Expression GetArgumentByName(string name) { return(methodCall.Arguments[indexedParameters[name]]); } LambdaExpression GetOutputExpression(Type outputType) { if (!indexedParameters.TryGetValue("outputExpression", out var index)) { var param = Expression.Parameter(outputType); return(Expression.Lambda(param, param)); } return((LambdaExpression)methodCall.Arguments[index].Unwrap()); } IBuildContext? outputContext = null; LambdaExpression?outputExpression = null; if (methodCall.Arguments.Count > 0) { var argument = methodCall.Arguments[0]; if (typeof(IValueInsertable <>).IsSameOrParentOf(argument.Type) || typeof(ISelectInsertable <,>).IsSameOrParentOf(argument.Type)) { // static int Insert<T> (this IValueInsertable<T> source) // static int Insert<TSource,TTarget>(this ISelectInsertable<TSource,TTarget> source) sequence.SelectQuery.Select.Columns.Clear(); foreach (var item in insertStatement.Insert.Items) { sequence.SelectQuery.Select.ExprNew(item.Expression !); } } else if (methodCall.Arguments.Count > 1 && typeof(IQueryable <>).IsSameOrParentOf(argument.Type) && typeof(ITable <>).IsSameOrParentOf(methodCall.Arguments[1].Type)) { // static int Insert<TSource,TTarget>(this IQueryable<TSource> source, Table<TTarget> target, Expression<Func<TSource,TTarget>> setter) var into = builder.BuildSequence(new BuildInfo(buildInfo, methodCall.Arguments[1], new SelectQuery())); var setter = (LambdaExpression)GetArgumentByName("setter").Unwrap(); UpdateBuilder.BuildSetter( builder, buildInfo, setter, into, insertStatement.Insert.Items, sequence); sequence.SelectQuery.Select.Columns.Clear(); foreach (var item in insertStatement.Insert.Items) { sequence.SelectQuery.Select.Columns.Add(new SqlColumn(sequence.SelectQuery, item.Expression !)); } insertStatement.Insert.Into = ((TableBuilder.TableContext)into).SqlTable; } else if (typeof(ITable <>).IsSameOrParentOf(argument.Type)) { // static int Insert<T>(this Table<T> target, Expression<Func<T>> setter) // static TTarget InsertWithOutput<TTarget>(this ITable<TTarget> target, Expression<Func<TTarget>> setter) // static TTarget InsertWithOutput<TTarget>(this ITable<TTarget> target, Expression<Func<TTarget>> setter, Expression<Func<TTarget,TOutput>> outputExpression) var argIndex = 1; var arg = methodCall.Arguments[argIndex].Unwrap(); LambdaExpression?setter = null; switch (arg) { case LambdaExpression lambda: { setter = lambda; UpdateBuilder.BuildSetter( builder, buildInfo, setter, sequence, insertStatement.Insert.Items, sequence); break; } default: { var objType = arg.Type; var ed = builder.MappingSchema.GetEntityDescriptor(objType); var into = sequence; var ctx = new TableBuilder.TableContext(builder, buildInfo, objType); var table = new SqlTable(objType); foreach (ColumnDescriptor c in ed.Columns.Where(c => !c.SkipOnInsert)) { if (!table.Fields.TryGetValue(c.ColumnName, out var field)) { continue; } var pe = Expression.MakeMemberAccess(arg, c.MemberInfo); var column = into.ConvertToSql(pe, 1, ConvertFlags.Field); var parameter = builder.BuildParameterFromArgumentProperty(methodCall, argIndex, field.ColumnDescriptor); insertStatement.Insert.Items.Add(new SqlSetExpression(column[0].Sql, parameter.SqlParameter)); } var insertedTable = SqlTable.Inserted(methodCall.Method.GetGenericArguments()[0]); break; } } insertStatement.Insert.Into = ((TableBuilder.TableContext)sequence).SqlTable; sequence.SelectQuery.From.Tables.Clear(); } if (insertType == InsertContext.InsertType.InsertOutput || insertType == InsertContext.InsertType.InsertOutputInto) { outputExpression = GetOutputExpression(methodCall.Method.GetGenericArguments().Last()); insertStatement.Output = new SqlOutputClause(); var insertedTable = SqlTable.Inserted(outputExpression.Parameters[0].Type); outputContext = new TableBuilder.TableContext(builder, new SelectQuery(), insertedTable); insertStatement.Output.InsertedTable = insertedTable; if (insertType == InsertContext.InsertType.InsertOutputInto) { var outputTable = GetArgumentByName("outputTable"); var destination = builder.BuildSequence(new BuildInfo(buildInfo, outputTable, new SelectQuery())); UpdateBuilder.BuildSetter( builder, buildInfo, outputExpression, destination, insertStatement.Output.OutputItems, outputContext); insertStatement.Output.OutputTable = ((TableBuilder.TableContext)destination).SqlTable; } } } var insert = insertStatement.Insert; var q = insert.Into !.Fields.Values .Except(insert.Items.Select(e => e.Column)) .OfType <SqlField>() .Where(f => f.IsIdentity); foreach (var field in q) { var expr = builder.DataContext.CreateSqlProvider().GetIdentityExpression(insert.Into); if (expr != null) { insert.Items.Insert(0, new SqlSetExpression(field, expr)); if (methodCall.Arguments.Count == 3) { sequence.SelectQuery.Select.Columns.Insert(0, new SqlColumn(sequence.SelectQuery, insert.Items[0].Expression !)); } } } insertStatement.Insert.WithIdentity = insertType == InsertContext.InsertType.InsertWithIdentity; sequence.Statement = insertStatement; if (insertType == InsertContext.InsertType.InsertOutput) { return(new InsertWithOutputContext(buildInfo.Parent, sequence, outputContext !, outputExpression !)); } return(new InsertContext(buildInfo.Parent, sequence, insertType, outputExpression)); }
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); }