public string GenerateInsert(ICollection <DbInsertCommandTree> commandTrees) { StringBuilder stringBuilder = new StringBuilder(); DbInsertCommandTree insertCommandTree1 = commandTrees.First <DbInsertCommandTree>(); List <SqlParameter> parameters; stringBuilder.Append(DmlSqlGenerator.GenerateInsertSql(insertCommandTree1, this._sqlGenerator, out parameters, false, true, false)); stringBuilder.AppendLine(); EntityType elementType = (EntityType)((DbScanExpression)insertCommandTree1.Target.Expression).Target.ElementType; stringBuilder.Append(this.IntroduceRequiredLocalVariables(elementType, insertCommandTree1)); foreach (DbInsertCommandTree tree in commandTrees.Skip <DbInsertCommandTree>(1)) { stringBuilder.Append(DmlSqlGenerator.GenerateInsertSql(tree, this._sqlGenerator, out parameters, false, true, false)); stringBuilder.AppendLine(); } List <DbInsertCommandTree> list = commandTrees.Where <DbInsertCommandTree>((Func <DbInsertCommandTree, bool>)(ct => ct.Returning != null)).ToList <DbInsertCommandTree>(); if (list.Any <DbInsertCommandTree>()) { DmlFunctionSqlGenerator.ReturningSelectSqlGenerator selectSqlGenerator = new DmlFunctionSqlGenerator.ReturningSelectSqlGenerator(); foreach (DbInsertCommandTree insertCommandTree2 in list) { insertCommandTree2.Target.Expression.Accept((DbExpressionVisitor)selectSqlGenerator); insertCommandTree2.Returning.Accept((DbExpressionVisitor)selectSqlGenerator); } foreach (EdmProperty keyProperty1 in elementType.KeyProperties) { EdmProperty keyProperty = keyProperty1; DbExpression right = insertCommandTree1.SetClauses.Cast <DbSetClause>().Where <DbSetClause>((Func <DbSetClause, bool>)(sc => ((DbPropertyExpression)sc.Property).Property == keyProperty)).Select <DbSetClause, DbExpression>((Func <DbSetClause, DbExpression>)(sc => sc.Value)).SingleOrDefault <DbExpression>() ?? (DbExpression)keyProperty.TypeUsage.Parameter(keyProperty.Name); insertCommandTree1.Target.Variable.Property(keyProperty).Equal(right).Accept((DbExpressionVisitor)selectSqlGenerator); } stringBuilder.Append(selectSqlGenerator.Sql); } return(stringBuilder.ToString().TrimEnd()); }
public string GenerateInsert(ICollection <DbInsertCommandTree> commandTrees) { DebugCheck.NotNull(commandTrees); var sql = new StringBuilder(); List <SqlParameter> _; var firstCommandTree = commandTrees.First(); sql.Append( DmlSqlGenerator.GenerateInsertSql( firstCommandTree, _sqlGenerator, out _, generateReturningSql: false, createParameters: false)); sql.AppendLine(); var firstTable = (EntityType)((DbScanExpression)firstCommandTree.Target.Expression).Target.ElementType; sql.Append(IntroduceRequiredLocalVariables(firstTable, firstCommandTree)); foreach (var commandTree in commandTrees.Skip(1)) { sql.Append( DmlSqlGenerator.GenerateInsertSql( commandTree, _sqlGenerator, out _, generateReturningSql: false, createParameters: false)); sql.AppendLine(); } var returningCommandTrees = commandTrees .Where(ct => ct.Returning != null) .ToList(); if (returningCommandTrees.Any()) { var returningSelectSqlGenerator = new ReturningSelectSqlGenerator(); foreach (var commandTree in returningCommandTrees) { commandTree.Target.Expression.Accept(returningSelectSqlGenerator); commandTree.Returning.Accept(returningSelectSqlGenerator); } foreach (var keyProperty in firstTable.KeyProperties) { var parameterReference = firstCommandTree .SetClauses .Cast <DbSetClause>() .Where(sc => ((DbPropertyExpression)sc.Property).Property == keyProperty) .Select(sc => sc.Value) .SingleOrDefault() ?? keyProperty.TypeUsage.Parameter(keyProperty.Name); firstCommandTree .Target .Variable .Property(keyProperty) .Equal(parameterReference) .Accept(returningSelectSqlGenerator); } sql.Append(returningSelectSqlGenerator.Sql); } return(sql.ToString().TrimEnd()); }