Skip to content

Commit

Permalink
#1143 Reconsidered the '__RepoDb_OrderColumn' in the RepoDb.SqlServer…
Browse files Browse the repository at this point in the history
… InsertAll operation.
  • Loading branch information
mikependon committed Mar 19, 2023
1 parent 02b8edf commit 1014788
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 27 deletions.
3 changes: 0 additions & 3 deletions RepoDb.Core/RepoDb/Extensions/DbConnectionExtension.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2250,15 +2250,12 @@ internal static void SetOutputParameter(QueryField queryField)

#region Order Columns

// TODO: Remove this

/// <summary>
///
/// </summary>
/// <typeparam name="TEntity"></typeparam>
/// <param name="command"></param>
/// <param name="entities"></param>
[Obsolete("Can be removed during the fixes to #1143.")]
private static void AddOrderColumnParameters<TEntity>(DbCommand command,
IEnumerable<TEntity> entities)
where TEntity : class
Expand Down
25 changes: 23 additions & 2 deletions RepoDb.Core/RepoDb/StatementBuilders/BaseStatementBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using System;
using RepoDb.Exceptions;
using RepoDb.Enumerations;
using System.Reflection;

namespace RepoDb.StatementBuilders
{
Expand Down Expand Up @@ -467,13 +468,18 @@ public virtual string CreateInsertAll(string tableName,
builder.Clear();

// Compose
builder.Insert()
builder
.Insert()
.Into()
.TableNameFrom(tableName, DbSetting)
.HintsFrom(hints)
.OpenParen()
.FieldsFrom(insertableFields, DbSetting)
.CloseParen()
.Select()
.FieldsFrom(insertableFields, DbSetting)
.From()
.OpenParen()
.Values();

// Iterate the indexes
Expand All @@ -482,6 +488,9 @@ public virtual string CreateInsertAll(string tableName,
builder
.OpenParen()
.ParametersFrom(insertableFields, index, DbSetting)
.WriteText(
string.Concat(", ",
$"{DbSetting.ParameterPrefix}__RepoDb_OrderColumn_{index}"))
.CloseParen();

if (index < batchSize - 1)
Expand All @@ -491,8 +500,20 @@ public virtual string CreateInsertAll(string tableName,
}
}

// Close
builder
.CloseParen()
.As("T")
.OpenParen()
.FieldsFrom(insertableFields, DbSetting)
.WriteText(string.Concat(", ", "__RepoDb_OrderColumn".AsQuoted(DbSetting)))
.CloseParen()
.OrderBy()
.WriteText("__RepoDb_OrderColumn".AsQuoted(DbSetting))
.End();

// Return the query
return builder.End().GetString();
return builder.GetString();
}

#endregion
Expand Down
72 changes: 52 additions & 20 deletions RepoDb.SqlServer/RepoDb.SqlServer.UnitTests/StatementBuilderTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -530,7 +530,11 @@ public void TestSqlServerStatementBuilderCreateInsertAllWithIdentity()
var expected = $"" +
$"INSERT INTO [Table] ( [Field2], [Field3] ) " +
$"OUTPUT CONVERT(INT, [INSERTED].[Field1]) AS [Result] " +
$"VALUES ( @Field2, @Field3 ) ;";
$"SELECT [Field2], [Field3] FROM " +
$"( VALUES " +
$"( @Field2, @Field3 , @__RepoDb_OrderColumn_0 ) ) AS T " +
$"( [Field2], [Field3] , [__RepoDb_OrderColumn] ) " +
$"ORDER BY [__RepoDb_OrderColumn] ;";

// Assert
Assert.AreEqual(expected, actual);
Expand All @@ -554,7 +558,11 @@ public void TestSqlServerStatementBuilderCreateInsertAllWithIdentityAsBigInt()
var expected = $"" +
$"INSERT INTO [Table] ( [Field2], [Field3] ) " +
$"OUTPUT CONVERT(BIGINT, [INSERTED].[Field1]) AS [Result] " +
$"VALUES ( @Field2, @Field3 ) ;";
$"SELECT [Field2], [Field3] FROM " +
$"( VALUES " +
$"( @Field2, @Field3 , @__RepoDb_OrderColumn_0 ) ) AS T " +
$"( [Field2], [Field3] , [__RepoDb_OrderColumn] ) " +
$"ORDER BY [__RepoDb_OrderColumn] ;";

// Assert
Assert.AreEqual(expected, actual);
Expand All @@ -578,7 +586,11 @@ public void TestSqlServerStatementBuilderCreateInsertAllWithPrimaryAndIdentity()
var expected = $"" +
$"INSERT INTO [Table] ( [Field1], [Field3] ) " +
$"OUTPUT CONVERT(INT, [INSERTED].[Field2]) AS [Result] " +
$"VALUES ( @Field1, @Field3 ) ;";
$"SELECT [Field1], [Field3] FROM " +
$"( VALUES " +
$"( @Field1, @Field3 , @__RepoDb_OrderColumn_0 ) ) AS T " +
$"( [Field1], [Field3] , [__RepoDb_OrderColumn] ) " +
$"ORDER BY [__RepoDb_OrderColumn] ;";

// Assert
Assert.AreEqual(expected, actual);
Expand All @@ -603,7 +615,11 @@ public void TestSqlServerStatementBuilderCreateInsertAllWithPrimaryAndIdentityAs
var expected = $"" +
$"INSERT INTO [Table] ( [Field1], [Field3] ) " +
$"OUTPUT CONVERT(BIGINT, [INSERTED].[Field2]) AS [Result] " +
$"VALUES ( @Field1, @Field3 ) ;";
$"SELECT [Field1], [Field3] FROM " +
$"( VALUES " +
$"( @Field1, @Field3 , @__RepoDb_OrderColumn_0 ) ) AS T " +
$"( [Field1], [Field3] , [__RepoDb_OrderColumn] ) " +
$"ORDER BY [__RepoDb_OrderColumn] ;";

// Assert
Assert.AreEqual(expected, actual);
Expand All @@ -627,7 +643,13 @@ public void TestSqlServerStatementBuilderCreateInsertAllWithIdentityForThreeBatc
var expected = $"" +
$"INSERT INTO [Table] ( [Field2], [Field3] ) " +
$"OUTPUT CONVERT(INT, [INSERTED].[Field1]) AS [Result] " +
$"VALUES ( @Field2, @Field3 ) , ( @Field2_1, @Field3_1 ) , ( @Field2_2, @Field3_2 ) ;";
$"SELECT [Field2], [Field3] FROM " +
$"( VALUES " +
$"( @Field2, @Field3 , @__RepoDb_OrderColumn_0 ) , " +
$"( @Field2_1, @Field3_1 , @__RepoDb_OrderColumn_1 ) , " +
$"( @Field2_2, @Field3_2 , @__RepoDb_OrderColumn_2 ) ) AS T " +
$"( [Field2], [Field3] , [__RepoDb_OrderColumn] ) " +
$"ORDER BY [__RepoDb_OrderColumn] ;";

// Assert
Assert.AreEqual(expected, actual);
Expand All @@ -652,7 +674,11 @@ public void TestSqlServerStatementBuilderCreateInsertAllWithIdentityWithHints()
var expected = $"" +
$"INSERT INTO [Table] WITH (TABLOCK) ( [Field2], [Field3] ) " +
$"OUTPUT CONVERT(INT, [INSERTED].[Field1]) AS [Result] " +
$"VALUES ( @Field2, @Field3 ) ;";
$"SELECT [Field2], [Field3] FROM " +
$"( VALUES " +
$"( @Field2, @Field3 , @__RepoDb_OrderColumn_0 ) ) AS T " +
$"( [Field2], [Field3] , [__RepoDb_OrderColumn] ) " +
$"ORDER BY [__RepoDb_OrderColumn] ;";

// Assert
Assert.AreEqual(expected, actual);
Expand All @@ -677,7 +703,13 @@ public void TestSqlServerStatementBuilderCreateInsertAllWithIdentityForThreeBatc
var expected = $"" +
$"INSERT INTO [Table] WITH (TABLOCK) ( [Field2], [Field3] ) " +
$"OUTPUT CONVERT(INT, [INSERTED].[Field1]) AS [Result] " +
$"VALUES ( @Field2, @Field3 ) , ( @Field2_1, @Field3_1 ) , ( @Field2_2, @Field3_2 ) ;";
$"SELECT [Field2], [Field3] FROM " +
$"( VALUES " +
$"( @Field2, @Field3 , @__RepoDb_OrderColumn_0 ) , " +
$"( @Field2_1, @Field3_1 , @__RepoDb_OrderColumn_1 ) , " +
$"( @Field2_2, @Field3_2 , @__RepoDb_OrderColumn_2 ) ) AS T " +
$"( [Field2], [Field3] , [__RepoDb_OrderColumn] ) " +
$"ORDER BY [__RepoDb_OrderColumn] ;";

// Assert
Assert.AreEqual(expected, actual);
Expand Down Expand Up @@ -1031,7 +1063,7 @@ public void TestSqlServerStatementBuilderCreateMergeAllWithCoveredPrimary()
$"VALUES ( S.[Field1], S.[Field2], S.[Field3] ) " +
$"WHEN MATCHED THEN " +
$"UPDATE SET T.[Field2] = S.[Field2], T.[Field3] = S.[Field3] " +
$"OUTPUT INSERTED.[Field1] AS [Result], @__RepoDb_OrderColumn_0 AS [OrderColumn] ;";
$"OUTPUT INSERTED.[Field1] AS [Result], @__RepoDb_OrderColumn_0 AS [__RepoDb_OrderColumn] ;";

// Assert
Assert.AreEqual(expected, actual);
Expand Down Expand Up @@ -1064,7 +1096,7 @@ public void TestSqlServerStatementBuilderCreateMergeAllWithCoveredPrimaryAsIdent
$"VALUES ( S.[Field2], S.[Field3] ) " +
$"WHEN MATCHED THEN " +
$"UPDATE SET T.[Field2] = S.[Field2], T.[Field3] = S.[Field3] " +
$"OUTPUT INSERTED.[Field1] AS [Result], @__RepoDb_OrderColumn_0 AS [OrderColumn] ;";
$"OUTPUT INSERTED.[Field1] AS [Result], @__RepoDb_OrderColumn_0 AS [__RepoDb_OrderColumn] ;";

// Assert
Assert.AreEqual(expected, actual);
Expand Down Expand Up @@ -1096,7 +1128,7 @@ public void TestSqlServerStatementBuilderCreateMergeAllWithUncoveredPrimary()
$"VALUES ( S.[Field1], S.[Field2], S.[Field3] ) " +
$"WHEN MATCHED THEN " +
$"UPDATE SET T.[Field1] = S.[Field1], T.[Field2] = S.[Field2], T.[Field3] = S.[Field3] " +
$"OUTPUT INSERTED.[Id] AS [Result], @__RepoDb_OrderColumn_0 AS [OrderColumn] ;";
$"OUTPUT INSERTED.[Id] AS [Result], @__RepoDb_OrderColumn_0 AS [__RepoDb_OrderColumn] ;";

// Assert
Assert.AreEqual(expected, actual);
Expand Down Expand Up @@ -1128,7 +1160,7 @@ public void TestSqlServerStatementBuilderCreateMergeAllWithCoveredIdentity()
$"VALUES ( S.[Field2], S.[Field3] ) " +
$"WHEN MATCHED THEN " +
$"UPDATE SET T.[Field2] = S.[Field2], T.[Field3] = S.[Field3] " +
$"OUTPUT INSERTED.[Field1] AS [Result], @__RepoDb_OrderColumn_0 AS [OrderColumn] ;";
$"OUTPUT INSERTED.[Field1] AS [Result], @__RepoDb_OrderColumn_0 AS [__RepoDb_OrderColumn] ;";

// Assert
Assert.AreEqual(expected, actual);
Expand Down Expand Up @@ -1160,7 +1192,7 @@ public void TestSqlServerStatementBuilderCreateMergeAllWithUncoveredIdentity()
$"VALUES ( S.[Field1], S.[Field2], S.[Field3] ) " +
$"WHEN MATCHED THEN " +
$"UPDATE SET T.[Field1] = S.[Field1], T.[Field2] = S.[Field2], T.[Field3] = S.[Field3] " +
$"OUTPUT INSERTED.[Id] AS [Result], @__RepoDb_OrderColumn_0 AS [OrderColumn] ;";
$"OUTPUT INSERTED.[Id] AS [Result], @__RepoDb_OrderColumn_0 AS [__RepoDb_OrderColumn] ;";

// Assert
Assert.AreEqual(expected, actual);
Expand Down Expand Up @@ -1191,7 +1223,7 @@ public void TestSqlServerStatementBuilderCreateMergeAllWithCoveredPrimaryButWith
$"VALUES ( S.[Field1], S.[Field2], S.[Field3] ) " +
$"WHEN MATCHED THEN " +
$"UPDATE SET T.[Field2] = S.[Field2], T.[Field3] = S.[Field3] " +
$"OUTPUT INSERTED.[Field1] AS [Result], @__RepoDb_OrderColumn_0 AS [OrderColumn] ;";
$"OUTPUT INSERTED.[Field1] AS [Result], @__RepoDb_OrderColumn_0 AS [__RepoDb_OrderColumn] ;";

// Assert
Assert.AreEqual(expected, actual);
Expand Down Expand Up @@ -1223,7 +1255,7 @@ public void TestSqlServerStatementBuilderCreateMergeAllWithCoveredPrimaryAndWith
$"VALUES ( S.[Field1], S.[Field3] ) " +
$"WHEN MATCHED THEN " +
$"UPDATE SET T.[Field3] = S.[Field3] " +
$"OUTPUT INSERTED.[Field2] AS [Result], @__RepoDb_OrderColumn_0 AS [OrderColumn] ;";
$"OUTPUT INSERTED.[Field2] AS [Result], @__RepoDb_OrderColumn_0 AS [__RepoDb_OrderColumn] ;";

// Assert
Assert.AreEqual(expected, actual);
Expand Down Expand Up @@ -1255,7 +1287,7 @@ public void TestSqlServerStatementBuilderCreateMergeAllWithIdentityForThreeBatch
$"VALUES ( S.[Field2], S.[Field3] ) " +
$"WHEN MATCHED THEN " +
$"UPDATE SET T.[Field2] = S.[Field2], T.[Field3] = S.[Field3] " +
$"OUTPUT INSERTED.[Field1] AS [Result], @__RepoDb_OrderColumn_0 AS [OrderColumn] ; " +
$"OUTPUT INSERTED.[Field1] AS [Result], @__RepoDb_OrderColumn_0 AS [__RepoDb_OrderColumn] ; " +
$"MERGE [Table] AS T " +
$"USING ( SELECT @Field1_1 AS [Field1], @Field2_1 AS [Field2], @Field3_1 AS [Field3] ) " +
$"AS S ON ( (S.[Field1] = T.[Field1] OR (S.[Field1] IS NULL AND T.[Field1] IS NULL)) ) " +
Expand All @@ -1264,7 +1296,7 @@ public void TestSqlServerStatementBuilderCreateMergeAllWithIdentityForThreeBatch
$"VALUES ( S.[Field2], S.[Field3] ) " +
$"WHEN MATCHED THEN " +
$"UPDATE SET T.[Field2] = S.[Field2], T.[Field3] = S.[Field3] " +
$"OUTPUT INSERTED.[Field1] AS [Result], @__RepoDb_OrderColumn_1 AS [OrderColumn] ; " +
$"OUTPUT INSERTED.[Field1] AS [Result], @__RepoDb_OrderColumn_1 AS [__RepoDb_OrderColumn] ; " +
$"MERGE [Table] AS T " +
$"USING ( SELECT @Field1_2 AS [Field1], @Field2_2 AS [Field2], @Field3_2 AS [Field3] ) " +
$"AS S ON ( (S.[Field1] = T.[Field1] OR (S.[Field1] IS NULL AND T.[Field1] IS NULL)) ) " +
Expand All @@ -1273,7 +1305,7 @@ public void TestSqlServerStatementBuilderCreateMergeAllWithIdentityForThreeBatch
$"VALUES ( S.[Field2], S.[Field3] ) " +
$"WHEN MATCHED THEN " +
$"UPDATE SET T.[Field2] = S.[Field2], T.[Field3] = S.[Field3] " +
$"OUTPUT INSERTED.[Field1] AS [Result], @__RepoDb_OrderColumn_2 AS [OrderColumn] ;";
$"OUTPUT INSERTED.[Field1] AS [Result], @__RepoDb_OrderColumn_2 AS [__RepoDb_OrderColumn] ;";

// Assert
Assert.AreEqual(expected, actual);
Expand Down Expand Up @@ -1337,7 +1369,7 @@ public void TestSqlServerStatementBuilderCreateMergeAllWithIdentityForThreeBatch
$"VALUES ( S.[Field2], S.[Field3] ) " +
$"WHEN MATCHED THEN " +
$"UPDATE SET T.[Field2] = S.[Field2], T.[Field3] = S.[Field3] " +
$"OUTPUT INSERTED.[Field1] AS [Result], @__RepoDb_OrderColumn_0 AS [OrderColumn] ; " +
$"OUTPUT INSERTED.[Field1] AS [Result], @__RepoDb_OrderColumn_0 AS [__RepoDb_OrderColumn] ; " +
$"MERGE [Table] WITH (TABLOCK) AS T " +
$"USING ( SELECT @Field1_1 AS [Field1], @Field2_1 AS [Field2], @Field3_1 AS [Field3] ) " +
$"AS S ON ( (S.[Field1] = T.[Field1] OR (S.[Field1] IS NULL AND T.[Field1] IS NULL)) ) " +
Expand All @@ -1346,7 +1378,7 @@ public void TestSqlServerStatementBuilderCreateMergeAllWithIdentityForThreeBatch
$"VALUES ( S.[Field2], S.[Field3] ) " +
$"WHEN MATCHED THEN " +
$"UPDATE SET T.[Field2] = S.[Field2], T.[Field3] = S.[Field3] " +
$"OUTPUT INSERTED.[Field1] AS [Result], @__RepoDb_OrderColumn_1 AS [OrderColumn] ; " +
$"OUTPUT INSERTED.[Field1] AS [Result], @__RepoDb_OrderColumn_1 AS [__RepoDb_OrderColumn] ; " +
$"MERGE [Table] WITH (TABLOCK) AS T " +
$"USING ( SELECT @Field1_2 AS [Field1], @Field2_2 AS [Field2], @Field3_2 AS [Field3] ) " +
$"AS S ON ( (S.[Field1] = T.[Field1] OR (S.[Field1] IS NULL AND T.[Field1] IS NULL)) ) " +
Expand All @@ -1355,7 +1387,7 @@ public void TestSqlServerStatementBuilderCreateMergeAllWithIdentityForThreeBatch
$"VALUES ( S.[Field2], S.[Field3] ) " +
$"WHEN MATCHED THEN " +
$"UPDATE SET T.[Field2] = S.[Field2], T.[Field3] = S.[Field3] " +
$"OUTPUT INSERTED.[Field1] AS [Result], @__RepoDb_OrderColumn_2 AS [OrderColumn] ;";
$"OUTPUT INSERTED.[Field1] AS [Result], @__RepoDb_OrderColumn_2 AS [__RepoDb_OrderColumn] ;";

// Assert
Assert.AreEqual(expected, actual);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -286,8 +286,7 @@ public override string CreateInsertAll(string tableName,
string.Concat("[INSERTED].", keyColumn.Name.AsQuoted(DbSetting)) :
string.Concat("CONVERT(", databaseType, ", [INSERTED].", keyColumn.Name.AsQuoted(DbSetting), ")");
var result = string.Concat("OUTPUT ", returnValue, $" AS [Result] ");
//$", {DbSetting.ParameterPrefix}__RepoDb_OrderColumn_{0} AS [OrderColumn] ");
commandText = commandText.Insert(commandText.IndexOf("VALUES"), result);
commandText = commandText.Insert(commandText.IndexOf("SELECT"), result);
}

// Return the query
Expand Down

0 comments on commit 1014788

Please sign in to comment.