Skip to content

Commit 4a944a9

Browse files
authored
Merge pull request #45 from lerocha/feat/insert-multiple-values
feat: updating SQL scripts to insert multiple rows with a single insert statement
2 parents 3fc5ab8 + 9ad8813 commit 4a944a9

35 files changed

+203590
-202956
lines changed

‎ChinookDatabase.Test/DatabaseTests/ChinookDb2Fixture.cs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*******************************************************************************
2-
* Chinook Database - Version 1.4.4
2+
* Chinook Database - Version 1.4.5
33
* Description: Test fixture for Chinook database.
44
* DB Server: Db2
55
* Author: Luis Rocha

‎ChinookDatabase.Test/DatabaseTests/ChinookMySqlFixture.cs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*******************************************************************************
2-
* Chinook Database - Version 1.4.4
2+
* Chinook Database - Version 1.4.5
33
* Description: Test fixture for Chinook database.
44
* DB Server: MySql
55
* Author: Luis Rocha

‎ChinookDatabase.Test/DatabaseTests/ChinookOracleFixture.cs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*******************************************************************************
2-
* Chinook Database - Version 1.4.4
2+
* Chinook Database - Version 1.4.5
33
* Description: Test fixture for Chinook database.
44
* DB Server: Oracle
55
* Author: Luis Rocha

‎ChinookDatabase.Test/DatabaseTests/ChinookPostgreSqlFixture.cs‎

Lines changed: 71 additions & 1 deletion
Large diffs are not rendered by default.

‎ChinookDatabase.Test/DatabaseTests/ChinookSqlServerCompactFixture.cs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*******************************************************************************
2-
* Chinook Database - Version 1.4.4
2+
* Chinook Database - Version 1.4.5
33
* Description: Test fixture for Chinook database.
44
* DB Server: SqlServerCompact
55
* Author: Luis Rocha

‎ChinookDatabase.Test/DatabaseTests/ChinookSqlServerFixture.cs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*******************************************************************************
2-
* Chinook Database - Version 1.4.4
2+
* Chinook Database - Version 1.4.5
33
* Description: Test fixture for Chinook database.
44
* DB Server: SqlServer
55
* Author: Luis Rocha

‎ChinookDatabase.Test/DatabaseTests/ChinookSqliteFixture.cs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*******************************************************************************
2-
* Chinook Database - Version 1.4.4
2+
* Chinook Database - Version 1.4.5
33
* Description: Test fixture for Chinook database.
44
* DB Server: Sqlite
55
* Author: Luis Rocha

‎ChinookDatabase.Test/DatabaseTests/DatabaseFixture.tt‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ var options = new [] {
5454
ConnectionClass = "NpgsqlConnection",
5555
AdapterClass = "NpgsqlDataAdapter",
5656
Namespace = "Npgsql",
57-
ConnectionNames = new[] {"Chinook_PostgreSql"}
57+
ConnectionNames = new[] {"Chinook_PostgreSql", "Chinook_PostgreSql_AutoIncrement", "Chinook_PostgreSql_Serial"}
5858
},
5959
new Option
6060
{

‎ChinookDatabase.Test/appsettings.test.json‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,11 @@
22
"ConnectionStrings": {
33
"Chinook_Db2": "Server=localhost;Database=Chinook;User Id=chinook;Password=chinook",
44
"Chinook_MySql": "Server=localhost;Database=Chinook;Uid=chinook;Pwd=chinook;",
5-
"Chinook_MySql_AutoIncrement": "Server=localhost;Database=Chinook_AutoIncrement;Uid=chinook;Pwd=chinook;",
5+
"Chinook_MySql_AutoIncrement": "Server=localhost;Database=Chinook_AutoIncrement;Uid=root;Pwd=chinook;",
66
"Chinook_Oracle": "Data Source=localhost:1521/XEPDB1;User Id=c##chinook;Password=chinook;",
77
"Chinook_PostgreSql": "Server=localhost;Database=chinook;User Id=chinook;Password=chinook;",
8+
"Chinook_PostgreSql_AutoIncrement": "Server=localhost;Database=chinook_auto_increment;User Id=chinook;Password=chinook;",
9+
"Chinook_PostgreSql_Serial": "Server=localhost;Database=chinook_serial;User Id=chinook;Password=chinook;",
810
"Chinook_Sqlite": "Data Source=..\\..\\..\\ChinookDatabase\\DataSources\\Chinook_Sqlite.sqlite",
911
"Chinook_Sqlite_AutoIncrement": "Data Source=..\\..\\..\\ChinookDatabase\\DataSources\\Chinook_Sqlite_AutoIncrementPKs.sqlite",
1012
"Chinook_SqlServer": "Data Source=localhost\\SQLEXPRESS;Initial Catalog=Chinook;Integrated Security=SSPI;Persist Security Info=False;",

‎ChinookDatabase/DataSources/ChinookDatabase.tt‎

Lines changed: 44 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ foreach (IDdlStrategy strategy in options.DdlStrategies)
4242
//************************************************************************
4343
// Start of SQL file.
4444
//************************************************************************
45+
var databaseName = GetDatabaseName(strategy, options.DatabaseName);
4546
var filename = GetFileName(strategy, strategy.ScriptFileExtension);
4647
fileManager.StartNewFile(filename, strategy.Encoding);
4748

@@ -75,7 +76,7 @@ foreach (IDdlStrategy strategy in options.DdlStrategies)
7576
<#
7677
if (strategy.IsReCreateDatabaseEnabled)
7778
{
78-
var dropdb = strategy.WriteDropDatabase(options.DatabaseName);
79+
var dropdb = strategy.WriteDropDatabase(databaseName);
7980
if (!string.IsNullOrEmpty(dropdb))
8081
{
8182
#>
@@ -88,7 +89,7 @@ foreach (IDdlStrategy strategy in options.DdlStrategies)
8889
<#
8990
}
9091

91-
var createdb = strategy.WriteCreateDatabase(options.DatabaseName);
92+
var createdb = strategy.WriteCreateDatabase(databaseName);
9293
if (!string.IsNullOrEmpty(createdb))
9394
{
9495
#>
@@ -101,7 +102,7 @@ foreach (IDdlStrategy strategy in options.DdlStrategies)
101102
<#
102103
}
103104

104-
var usedb = strategy.WriteUseDatabase(options.DatabaseName);
105+
var usedb = strategy.WriteUseDatabase(databaseName);
105106
if (!string.IsNullOrEmpty(usedb))
106107
{
107108
#>
@@ -269,47 +270,59 @@ CREATE INDEX <#= ifkName #> ON <#= strategy.GetFullyQualifiedName(fromTableName)
269270
Populate Tables
270271
********************************************************************************/
271272
<#
272-
var sbFields = new StringBuilder();
273+
var maxRows = 1000;
273274
var sbValues = new StringBuilder();
274275
char delimiter = ',';
275276

276277
foreach (DataTable table in ds.Tables)
277278
{
278279
var tableName = strategy.GetFullyQualifiedName(table.TableName);
279-
#>
280-
<#
281-
foreach (DataRow row in table.Rows)
280+
281+
var columns = table.Columns.Cast<DataColumn>()
282+
.Where(col => !col.AutoIncrement || strategy.PrimaryKeyStrategy == PrimaryKeyStrategy.None)
283+
.ToList();
284+
285+
var fields = string.Join($"{delimiter} ", columns
286+
.Select(col => $"{strategy.FormatName(col.ColumnName)}")
287+
.ToList());
288+
289+
for (var i = 0; i < table.Rows.Count; i++)
282290
{
283-
sbFields.Length = 0;
291+
var row = table.Rows[i];
284292
sbValues.Length = 0;
285-
286-
foreach (DataColumn col in table.Columns)
293+
294+
foreach (var col in columns)
287295
{
288-
string value = row[col.ColumnName].ToString();
289-
if ((col.AutoIncrement && strategy.PrimaryKeyStrategy != PrimaryKeyStrategy.None) || value.Length==0) continue;
290-
291-
if (col.DataType == typeof(DateTime))
296+
var value = row[col.ColumnName].ToString();
297+
if (value.Length == 0 && col.AllowDBNull)
298+
{
299+
value = "NULL";
300+
}
301+
else if (col.DataType == typeof(DateTime))
292302
{
293303
value = strategy.FormatDateValue(value);
294304
}
295-
else if (col.DataType == typeof(String))
305+
else if (col.DataType == typeof(string))
296306
{
297307
value = strategy.FormatStringValue(value);
298308
}
299309

300310
sbValues.AppendFormat("{0}{1} ", value, delimiter);
301-
sbFields.AppendFormat("{0}{1} ", strategy.FormatName(col.ColumnName), delimiter);
302311
}
303312

304-
var fields = sbFields.ToString().Trim().TrimEnd(delimiter);
305313
var values = sbValues.ToString().Trim().TrimEnd(delimiter);
314+
var terminator = (i == table.Rows.Count - 1 || (i > 0 && (i + 1) % maxRows == 0)) ? ";" : ",";
315+
if (i % maxRows == 0)
316+
{
306317
#>
307-
<#= string.Format("INSERT INTO {0} ({1}) VALUES ({2});", tableName, fields, values) #>
318+
319+
<#= $"INSERT INTO {tableName} ({fields}) VALUES" #>
308320
<#
309-
} // foreach DataRow
321+
}
310322
#>
311-
323+
<#= $" ({values}){terminator}" #>
312324
<#
325+
} // foreach DataRow
313326
} // foreach DataTable
314327
#>
315328
<#= strategy.WriteFinishCommit() #>
@@ -405,6 +418,17 @@ public class OutputFile {
405418
public string Description { get; set; }
406419
}
407420

421+
private static string GetDatabaseName(IDdlStrategy strategy, string name)
422+
{
423+
var suffix = strategy.PrimaryKeyStrategy switch {
424+
PrimaryKeyStrategy.Identity => "_AutoIncrement",
425+
PrimaryKeyStrategy.Serial => "_Serial",
426+
_ => string.Empty
427+
};
428+
429+
return $"{name}{suffix}";
430+
}
431+
408432
private static string GetFileName(IDdlStrategy strategy, string extension)
409433
{
410434
var suffix = strategy.PrimaryKeyStrategy switch {

0 commit comments

Comments
 (0)