Skip to content

Commit

Permalink
Merge pull request #45 from lerocha/feat/insert-multiple-values
Browse files Browse the repository at this point in the history
feat: updating SQL scripts to insert multiple rows with a single insert statement
  • Loading branch information
lerocha authored Feb 12, 2024
2 parents 3fc5ab8 + 9ad8813 commit 4a944a9
Show file tree
Hide file tree
Showing 35 changed files with 203,590 additions and 202,956 deletions.
2 changes: 1 addition & 1 deletion ChinookDatabase.Test/DatabaseTests/ChinookDb2Fixture.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*******************************************************************************
* Chinook Database - Version 1.4.4
* Chinook Database - Version 1.4.5
* Description: Test fixture for Chinook database.
* DB Server: Db2
* Author: Luis Rocha
Expand Down
2 changes: 1 addition & 1 deletion ChinookDatabase.Test/DatabaseTests/ChinookMySqlFixture.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*******************************************************************************
* Chinook Database - Version 1.4.4
* Chinook Database - Version 1.4.5
* Description: Test fixture for Chinook database.
* DB Server: MySql
* Author: Luis Rocha
Expand Down
2 changes: 1 addition & 1 deletion ChinookDatabase.Test/DatabaseTests/ChinookOracleFixture.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*******************************************************************************
* Chinook Database - Version 1.4.4
* Chinook Database - Version 1.4.5
* Description: Test fixture for Chinook database.
* DB Server: Oracle
* Author: Luis Rocha
Expand Down
72 changes: 71 additions & 1 deletion ChinookDatabase.Test/DatabaseTests/ChinookPostgreSqlFixture.cs

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*******************************************************************************
* Chinook Database - Version 1.4.4
* Chinook Database - Version 1.4.5
* Description: Test fixture for Chinook database.
* DB Server: SqlServerCompact
* Author: Luis Rocha
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*******************************************************************************
* Chinook Database - Version 1.4.4
* Chinook Database - Version 1.4.5
* Description: Test fixture for Chinook database.
* DB Server: SqlServer
* Author: Luis Rocha
Expand Down
2 changes: 1 addition & 1 deletion ChinookDatabase.Test/DatabaseTests/ChinookSqliteFixture.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*******************************************************************************
* Chinook Database - Version 1.4.4
* Chinook Database - Version 1.4.5
* Description: Test fixture for Chinook database.
* DB Server: Sqlite
* Author: Luis Rocha
Expand Down
2 changes: 1 addition & 1 deletion ChinookDatabase.Test/DatabaseTests/DatabaseFixture.tt
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ var options = new [] {
ConnectionClass = "NpgsqlConnection",
AdapterClass = "NpgsqlDataAdapter",
Namespace = "Npgsql",
ConnectionNames = new[] {"Chinook_PostgreSql"}
ConnectionNames = new[] {"Chinook_PostgreSql", "Chinook_PostgreSql_AutoIncrement", "Chinook_PostgreSql_Serial"}
},
new Option
{
Expand Down
4 changes: 3 additions & 1 deletion ChinookDatabase.Test/appsettings.test.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@
"ConnectionStrings": {
"Chinook_Db2": "Server=localhost;Database=Chinook;User Id=chinook;Password=chinook",
"Chinook_MySql": "Server=localhost;Database=Chinook;Uid=chinook;Pwd=chinook;",
"Chinook_MySql_AutoIncrement": "Server=localhost;Database=Chinook_AutoIncrement;Uid=chinook;Pwd=chinook;",
"Chinook_MySql_AutoIncrement": "Server=localhost;Database=Chinook_AutoIncrement;Uid=root;Pwd=chinook;",
"Chinook_Oracle": "Data Source=localhost:1521/XEPDB1;User Id=c##chinook;Password=chinook;",
"Chinook_PostgreSql": "Server=localhost;Database=chinook;User Id=chinook;Password=chinook;",
"Chinook_PostgreSql_AutoIncrement": "Server=localhost;Database=chinook_auto_increment;User Id=chinook;Password=chinook;",
"Chinook_PostgreSql_Serial": "Server=localhost;Database=chinook_serial;User Id=chinook;Password=chinook;",
"Chinook_Sqlite": "Data Source=..\\..\\..\\ChinookDatabase\\DataSources\\Chinook_Sqlite.sqlite",
"Chinook_Sqlite_AutoIncrement": "Data Source=..\\..\\..\\ChinookDatabase\\DataSources\\Chinook_Sqlite_AutoIncrementPKs.sqlite",
"Chinook_SqlServer": "Data Source=localhost\\SQLEXPRESS;Initial Catalog=Chinook;Integrated Security=SSPI;Persist Security Info=False;",
Expand Down
64 changes: 44 additions & 20 deletions ChinookDatabase/DataSources/ChinookDatabase.tt
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ foreach (IDdlStrategy strategy in options.DdlStrategies)
//************************************************************************
// Start of SQL file.
//************************************************************************
var databaseName = GetDatabaseName(strategy, options.DatabaseName);
var filename = GetFileName(strategy, strategy.ScriptFileExtension);
fileManager.StartNewFile(filename, strategy.Encoding);

Expand Down Expand Up @@ -75,7 +76,7 @@ foreach (IDdlStrategy strategy in options.DdlStrategies)
<#
if (strategy.IsReCreateDatabaseEnabled)
{
var dropdb = strategy.WriteDropDatabase(options.DatabaseName);
var dropdb = strategy.WriteDropDatabase(databaseName);
if (!string.IsNullOrEmpty(dropdb))
{
#>
Expand All @@ -88,7 +89,7 @@ foreach (IDdlStrategy strategy in options.DdlStrategies)
<#
}

var createdb = strategy.WriteCreateDatabase(options.DatabaseName);
var createdb = strategy.WriteCreateDatabase(databaseName);
if (!string.IsNullOrEmpty(createdb))
{
#>
Expand All @@ -101,7 +102,7 @@ foreach (IDdlStrategy strategy in options.DdlStrategies)
<#
}

var usedb = strategy.WriteUseDatabase(options.DatabaseName);
var usedb = strategy.WriteUseDatabase(databaseName);
if (!string.IsNullOrEmpty(usedb))
{
#>
Expand Down Expand Up @@ -269,47 +270,59 @@ CREATE INDEX <#= ifkName #> ON <#= strategy.GetFullyQualifiedName(fromTableName)
Populate Tables
********************************************************************************/
<#
var sbFields = new StringBuilder();
var maxRows = 1000;
var sbValues = new StringBuilder();
char delimiter = ',';

foreach (DataTable table in ds.Tables)
{
var tableName = strategy.GetFullyQualifiedName(table.TableName);
#>
<#
foreach (DataRow row in table.Rows)

var columns = table.Columns.Cast<DataColumn>()
.Where(col => !col.AutoIncrement || strategy.PrimaryKeyStrategy == PrimaryKeyStrategy.None)
.ToList();

var fields = string.Join($"{delimiter} ", columns
.Select(col => $"{strategy.FormatName(col.ColumnName)}")
.ToList());

for (var i = 0; i < table.Rows.Count; i++)
{
sbFields.Length = 0;
var row = table.Rows[i];
sbValues.Length = 0;
foreach (DataColumn col in table.Columns)

foreach (var col in columns)
{
string value = row[col.ColumnName].ToString();
if ((col.AutoIncrement && strategy.PrimaryKeyStrategy != PrimaryKeyStrategy.None) || value.Length==0) continue;

if (col.DataType == typeof(DateTime))
var value = row[col.ColumnName].ToString();
if (value.Length == 0 && col.AllowDBNull)
{
value = "NULL";
}
else if (col.DataType == typeof(DateTime))
{
value = strategy.FormatDateValue(value);
}
else if (col.DataType == typeof(String))
else if (col.DataType == typeof(string))
{
value = strategy.FormatStringValue(value);
}

sbValues.AppendFormat("{0}{1} ", value, delimiter);
sbFields.AppendFormat("{0}{1} ", strategy.FormatName(col.ColumnName), delimiter);
}

var fields = sbFields.ToString().Trim().TrimEnd(delimiter);
var values = sbValues.ToString().Trim().TrimEnd(delimiter);
var terminator = (i == table.Rows.Count - 1 || (i > 0 && (i + 1) % maxRows == 0)) ? ";" : ",";
if (i % maxRows == 0)
{
#>
<#= string.Format("INSERT INTO {0} ({1}) VALUES ({2});", tableName, fields, values) #>

<#= $"INSERT INTO {tableName} ({fields}) VALUES" #>
<#
} // foreach DataRow
}
#>

<#= $" ({values}){terminator}" #>
<#
} // foreach DataRow
} // foreach DataTable
#>
<#= strategy.WriteFinishCommit() #>
Expand Down Expand Up @@ -405,6 +418,17 @@ public class OutputFile {
public string Description { get; set; }
}

private static string GetDatabaseName(IDdlStrategy strategy, string name)
{
var suffix = strategy.PrimaryKeyStrategy switch {
PrimaryKeyStrategy.Identity => "_AutoIncrement",
PrimaryKeyStrategy.Serial => "_Serial",
_ => string.Empty
};

return $"{name}{suffix}";
}

private static string GetFileName(IDdlStrategy strategy, string extension)
{
var suffix = strategy.PrimaryKeyStrategy switch {
Expand Down
2 changes: 1 addition & 1 deletion ChinookDatabase/DataSources/ChinookDatabase.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
Chinook Database 1.4.4
Chinook Database 1.4.5

This is the list of generated files.
|| Data Source || File || Description ||
Expand Down
Loading

0 comments on commit 4a944a9

Please sign in to comment.