Skip to content

Commit 383c1f7

Browse files
authored
feat: auto generate SQL scripts for serial primary keys (#43)
* Creating PrimaryKeyStrategy * Adding support to primary key columns with serial type
1 parent 554b65d commit 383c1f7

File tree

6 files changed

+56
-28
lines changed

6 files changed

+56
-28
lines changed

‎ChinookDatabase/DataSources/ChinookDatabase.tt‎

Lines changed: 21 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -12,18 +12,18 @@ var options = new
1212
DataSource = Path.Combine(Path.GetDirectoryName(Host.TemplateFile), "_Xml", "ChinookData.xml"),
1313
DdlStrategies = new IDdlStrategy[]
1414
{
15-
new SqlServerStrategy { IsIdentityEnabled = false, Encoding = Encoding.Unicode },
16-
new SqlServerStrategy { IsIdentityEnabled = true, Encoding = Encoding.Unicode },
17-
new SqlServerCompactStrategy { IsIdentityEnabled = false },
18-
new SqlServerCompactStrategy { IsIdentityEnabled = true },
19-
new SqliteStrategy { IsIdentityEnabled = false },
20-
new SqliteStrategy { IsIdentityEnabled = true, PrimaryKeyDef = KeyDefinition.OnCreateTableColumn },
21-
new MySqlStrategy { IsIdentityEnabled = false },
22-
new MySqlStrategy { IsIdentityEnabled = true },
23-
new OracleStrategy { IsIdentityEnabled = false, Encoding = Encoding.UTF8 },
24-
new PostgreSqlStrategy { IsIdentityEnabled = false, Encoding = Encoding.Default },
25-
new PostgreSqlStrategy { IsIdentityEnabled = true, Encoding = Encoding.Default },
26-
new Db2Strategy { IsIdentityEnabled = false, Encoding = Encoding.Default }
15+
new SqlServerStrategy { PrimaryKeyStrategy = PrimaryKeyStrategy.None, Encoding = Encoding.Unicode },
16+
new SqlServerStrategy { PrimaryKeyStrategy = PrimaryKeyStrategy.Identity, Encoding = Encoding.Unicode },
17+
new SqlServerCompactStrategy { PrimaryKeyStrategy = PrimaryKeyStrategy.None },
18+
new SqlServerCompactStrategy { PrimaryKeyStrategy = PrimaryKeyStrategy.Identity },
19+
new SqliteStrategy { PrimaryKeyStrategy = PrimaryKeyStrategy.None },
20+
new SqliteStrategy { PrimaryKeyStrategy = PrimaryKeyStrategy.Identity, PrimaryKeyDef = KeyDefinition.OnCreateTableColumn },
21+
new MySqlStrategy { PrimaryKeyStrategy = PrimaryKeyStrategy.None },
22+
new MySqlStrategy { PrimaryKeyStrategy = PrimaryKeyStrategy.Identity },
23+
new OracleStrategy { PrimaryKeyStrategy = PrimaryKeyStrategy.None, Encoding = Encoding.UTF8 },
24+
new PostgreSqlStrategy { PrimaryKeyStrategy = PrimaryKeyStrategy.None, Encoding = Encoding.Default },
25+
new PostgreSqlStrategy { PrimaryKeyStrategy = PrimaryKeyStrategy.Identity, Encoding = Encoding.Default },
26+
new Db2Strategy { PrimaryKeyStrategy = PrimaryKeyStrategy.None, Encoding = Encoding.Default }
2727
},
2828
OutputFiles = new List<OutputFile>()
2929
};
@@ -44,7 +44,7 @@ foreach (IDdlStrategy strategy in options.DdlStrategies)
4444
var filename = GetFileName(strategy, strategy.ScriptFileExtension);
4545
fileManager.StartNewFile(filename, strategy.Encoding);
4646

47-
var details = (strategy.IsIdentityEnabled ? "Auto incremented primary keys." : string.Empty);
47+
var details = (strategy.PrimaryKeyStrategy == PrimaryKeyStrategy.Identity ? "Auto incremented primary keys." : string.Empty);
4848

4949
// Add the script file to the package list.
5050
options.OutputFiles.Add(new OutputFile { Name = filename, Package = strategy.Name, Description = "SQL script to create the Chinook database. " + details });
@@ -285,7 +285,7 @@ CREATE INDEX <#= ifkName #> ON <#= strategy.GetFullyQualifiedName(fromTableName)
285285
foreach (DataColumn col in table.Columns)
286286
{
287287
string value = row[col.ColumnName].ToString();
288-
if ((col.AutoIncrement && strategy.IsIdentityEnabled) || value.Length==0) continue;
288+
if ((col.AutoIncrement && strategy.PrimaryKeyStrategy != PrimaryKeyStrategy.None) || value.Length==0) continue;
289289

290290
if (col.DataType == typeof(DateTime))
291291
{
@@ -453,8 +453,13 @@ public class OutputFile {
453453

454454
private static string GetFileName(IDdlStrategy strategy, string extension)
455455
{
456-
var suffix = (strategy.IsIdentityEnabled ? "_AutoIncrementPKs" : string.Empty);
457-
return string.Format("Chinook_{0}{1}.{2}", strategy.Name, suffix, extension);
456+
var suffix = strategy.PrimaryKeyStrategy switch {
457+
PrimaryKeyStrategy.Identity => "_AutoIncrementPKs",
458+
PrimaryKeyStrategy.Serial => "_SerialPKs",
459+
_ => string.Empty
460+
};
461+
462+
return $"Chinook_{strategy.Name}{suffix}.{extension}";
458463
}
459464

460465
private static string GetValues(IEnumerable<string> values, char delimiter)

‎ChinookDatabase/DdlStrategies/AbstractDdlStrategy.cs‎

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ protected AbstractDdlStrategy()
2929

3030
public KeyDefinition PrimaryKeyDef { get; set; }
3131
public KeyDefinition ForeignKeyDef { get; set; }
32-
public bool IsIdentityEnabled { get; set; }
32+
public PrimaryKeyStrategy PrimaryKeyStrategy { get; set; }
3333
public bool IsReCreateDatabaseEnabled { get; set; }
3434
public string CommandLineFormat { get; set; }
3535
public Encoding Encoding { get; set; }
@@ -88,12 +88,13 @@ public virtual string GetColumns(IEnumerable<String> keys, char delimiter)
8888

8989
public virtual string WriteCreateColumn(DataColumn column)
9090
{
91-
var notnull = (column.AllowDBNull ? "" : "NOT NULL");
9291
var isPrimaryKey = column.Table?.PrimaryKey.Length == 1 && column.Table?.PrimaryKey.Contains(column) == true;
93-
var identity = IsIdentityEnabled && isPrimaryKey ? Identity : String.Empty;
92+
var type = isPrimaryKey && (PrimaryKeyStrategy == PrimaryKeyStrategy.Serial) ? "SERIAL" : GetStoreType(column);
93+
var notnull = (column.AllowDBNull ? "" : "NOT NULL");
94+
var identity = (PrimaryKeyStrategy == PrimaryKeyStrategy.Identity) && isPrimaryKey ? Identity : String.Empty;
9495
return string.Format("{0} {1} {2} {3}",
9596
FormatName(column.ColumnName),
96-
GetStoreType(column),
97+
type,
9798
notnull, identity).Trim();
9899
}
99100

‎ChinookDatabase/DdlStrategies/IDdlStrategy.cs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ public interface IDdlStrategy
1919

2020
KeyDefinition PrimaryKeyDef { get; set; }
2121
KeyDefinition ForeignKeyDef { get; set; }
22-
bool IsIdentityEnabled { get; set; }
22+
public PrimaryKeyStrategy PrimaryKeyStrategy { get; set; }
2323
bool IsReCreateDatabaseEnabled { get; set; }
2424
string CommandLineFormat { get; set; }
2525
Encoding Encoding { get; set; }
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
namespace ChinookDatabase.DdlStrategies
2+
{
3+
public enum PrimaryKeyStrategy
4+
{
5+
None,
6+
Identity,
7+
Serial
8+
}
9+
}

‎ChinookDatabase/DdlStrategies/SqliteStrategy.cs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ public override string WriteCreateColumn(DataColumn column)
3434
{
3535
var notnull = (column.AllowDBNull ? "" : "NOT NULL");
3636
var isPrimaryKey = column.Table?.PrimaryKey.Length == 1 && column.Table?.PrimaryKey.Contains(column) == true;
37-
var identity = IsIdentityEnabled && isPrimaryKey ? Identity : String.Empty;
37+
var identity = (PrimaryKeyStrategy == PrimaryKeyStrategy.Identity) && isPrimaryKey ? Identity : String.Empty;
3838
return string.Format("{0} {1} {2} {3}",
3939
FormatName(column.ColumnName),
4040
GetStoreType(column),

‎ChinookDatabase/_T4Templates/Chinook.ttinclude‎

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@
4242
// MySqlStrategy.cs
4343
// OracleStrategy.cs
4444
// PostgreSqlStrategy.cs
45+
// PrimaryKeyStrategy.cs
4546
// SqliteStrategy.cs
4647
// SqlServerCompactStrategy.cs
4748
// SqlServerStrategy.cs
@@ -6278,7 +6279,7 @@
62786279

62796280
public KeyDefinition PrimaryKeyDef { get; set; }
62806281
public KeyDefinition ForeignKeyDef { get; set; }
6281-
public bool IsIdentityEnabled { get; set; }
6282+
public PrimaryKeyStrategy PrimaryKeyStrategy { get; set; }
62826283
public bool IsReCreateDatabaseEnabled { get; set; }
62836284
public string CommandLineFormat { get; set; }
62846285
public Encoding Encoding { get; set; }
@@ -6337,12 +6338,13 @@
63376338

63386339
public virtual string WriteCreateColumn(DataColumn column)
63396340
{
6340-
var notnull = (column.AllowDBNull ? "" : "NOT NULL");
63416341
var isPrimaryKey = column.Table?.PrimaryKey.Length == 1 && column.Table?.PrimaryKey.Contains(column) == true;
6342-
var identity = IsIdentityEnabled && isPrimaryKey ? Identity : String.Empty;
6342+
var type = isPrimaryKey && (PrimaryKeyStrategy == PrimaryKeyStrategy.Serial) ? "SERIAL" : GetStoreType(column);
6343+
var notnull = (column.AllowDBNull ? "" : "NOT NULL");
6344+
var identity = (PrimaryKeyStrategy == PrimaryKeyStrategy.Identity) && isPrimaryKey ? Identity : String.Empty;
63436345
return string.Format("{0} {1} {2} {3}",
63446346
FormatName(column.ColumnName),
6345-
GetStoreType(column),
6347+
type,
63466348
notnull, identity).Trim();
63476349
}
63486350

@@ -6424,7 +6426,7 @@
64246426

64256427
KeyDefinition PrimaryKeyDef { get; set; }
64266428
KeyDefinition ForeignKeyDef { get; set; }
6427-
bool IsIdentityEnabled { get; set; }
6429+
public PrimaryKeyStrategy PrimaryKeyStrategy { get; set; }
64286430
bool IsReCreateDatabaseEnabled { get; set; }
64296431
string CommandLineFormat { get; set; }
64306432
Encoding Encoding { get; set; }
@@ -6635,6 +6637,17 @@
66356637
private static string ToSnakeCase(string text) => snakeCaseNamingStrategy.GetPropertyName(text, false);
66366638
}
66376639

6640+
//------------------------------------------------------------------------------
6641+
// Filename: PrimaryKeyStrategy.cs
6642+
//------------------------------------------------------------------------------
6643+
6644+
public enum PrimaryKeyStrategy
6645+
{
6646+
None,
6647+
Identity,
6648+
Serial
6649+
}
6650+
66386651
//------------------------------------------------------------------------------
66396652
// Filename: SqliteStrategy.cs
66406653
//------------------------------------------------------------------------------
@@ -6672,7 +6685,7 @@
66726685
{
66736686
var notnull = (column.AllowDBNull ? "" : "NOT NULL");
66746687
var isPrimaryKey = column.Table?.PrimaryKey.Length == 1 && column.Table?.PrimaryKey.Contains(column) == true;
6675-
var identity = IsIdentityEnabled && isPrimaryKey ? Identity : String.Empty;
6688+
var identity = (PrimaryKeyStrategy == PrimaryKeyStrategy.Identity) && isPrimaryKey ? Identity : String.Empty;
66766689
return string.Format("{0} {1} {2} {3}",
66776690
FormatName(column.ColumnName),
66786691
GetStoreType(column),

0 commit comments

Comments
 (0)