public string GenerateUpdate(ICollection <DbUpdateCommandTree> commandTrees, string rowsAffectedParameter) { DebugCheck.NotNull(commandTrees); if (!commandTrees.Any()) { return(null); } List <SqlParameter> _; var sql = new StringBuilder(); sql.AppendLine( DmlSqlGenerator.GenerateUpdateSql( commandTrees.First(), _sqlGenerator, out _, generateReturningSql: false)); foreach (var commandTree in commandTrees.Skip(1)) { sql.Append( DmlSqlGenerator.GenerateUpdateSql( commandTree, _sqlGenerator, out _, generateReturningSql: false)); sql.AppendLine("AND @@ROWCOUNT > 0"); 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); commandTree.Predicate.Accept(returningSelectSqlGenerator); } sql.AppendLine(returningSelectSqlGenerator.Sql); sql.AppendLine(); } AppendSetRowsAffected(sql, rowsAffectedParameter); return(sql.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()); }
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(); }
public string GenerateUpdate(ICollection<DbUpdateCommandTree> commandTrees, string rowsAffectedParameter) { DebugCheck.NotNull(commandTrees); if (!commandTrees.Any()) { return null; } List<SqlParameter> _; var sql = new StringBuilder(); sql.AppendLine( DmlSqlGenerator.GenerateUpdateSql( commandTrees.First(), _sqlGenerator, out _, generateReturningSql: false)); foreach (var commandTree in commandTrees.Skip(1)) { sql.Append( DmlSqlGenerator.GenerateUpdateSql( commandTree, _sqlGenerator, out _, generateReturningSql: false)); sql.AppendLine("AND @@ROWCOUNT > 0"); 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); commandTree.Predicate.Accept(returningSelectSqlGenerator); } sql.AppendLine(returningSelectSqlGenerator.Sql); sql.AppendLine(); } AppendSetRowsAffected(sql, rowsAffectedParameter); return sql.ToString().TrimEnd(); }