Skip to content

Commit

Permalink
Adding support to primary key columns with serial type
Browse files Browse the repository at this point in the history
  • Loading branch information
lerocha committed Feb 10, 2024
1 parent b997dde commit b921f8c
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 7 deletions.
11 changes: 8 additions & 3 deletions ChinookDatabase/DataSources/ChinookDatabase.tt
Original file line number Diff line number Diff line change
Expand Up @@ -285,7 +285,7 @@ CREATE INDEX <#= ifkName #> ON <#= strategy.GetFullyQualifiedName(fromTableName)
foreach (DataColumn col in table.Columns)
{
string value = row[col.ColumnName].ToString();
if ((col.AutoIncrement && strategy.PrimaryKeyStrategy == PrimaryKeyStrategy.Identity) || value.Length==0) continue;
if ((col.AutoIncrement && strategy.PrimaryKeyStrategy != PrimaryKeyStrategy.None) || value.Length==0) continue;

if (col.DataType == typeof(DateTime))
{
Expand Down Expand Up @@ -453,8 +453,13 @@ public class OutputFile {

private static string GetFileName(IDdlStrategy strategy, string extension)
{
var suffix = (strategy.PrimaryKeyStrategy == PrimaryKeyStrategy.Identity ? "_AutoIncrementPKs" : string.Empty);
return string.Format("Chinook_{0}{1}.{2}", strategy.Name, suffix, extension);
var suffix = strategy.PrimaryKeyStrategy switch {
PrimaryKeyStrategy.Identity => "_AutoIncrementPKs",
PrimaryKeyStrategy.Serial => "_SerialPKs",
_ => string.Empty
};

return $"Chinook_{strategy.Name}{suffix}.{extension}";
}

private static string GetValues(IEnumerable<string> values, char delimiter)
Expand Down
5 changes: 3 additions & 2 deletions ChinookDatabase/DdlStrategies/AbstractDdlStrategy.cs
Original file line number Diff line number Diff line change
Expand Up @@ -88,12 +88,13 @@ public virtual string GetColumns(IEnumerable<String> keys, char delimiter)

public virtual string WriteCreateColumn(DataColumn column)
{
var notnull = (column.AllowDBNull ? "" : "NOT NULL");
var isPrimaryKey = column.Table?.PrimaryKey.Length == 1 && column.Table?.PrimaryKey.Contains(column) == true;
var type = isPrimaryKey && (PrimaryKeyStrategy == PrimaryKeyStrategy.Serial) ? "SERIAL" : GetStoreType(column);
var notnull = (column.AllowDBNull ? "" : "NOT NULL");
var identity = (PrimaryKeyStrategy == PrimaryKeyStrategy.Identity) && isPrimaryKey ? Identity : String.Empty;
return string.Format("{0} {1} {2} {3}",
FormatName(column.ColumnName),
GetStoreType(column),
type,
notnull, identity).Trim();
}

Expand Down
5 changes: 3 additions & 2 deletions ChinookDatabase/_T4Templates/Chinook.ttinclude
Original file line number Diff line number Diff line change
Expand Up @@ -6338,12 +6338,13 @@

public virtual string WriteCreateColumn(DataColumn column)
{
var notnull = (column.AllowDBNull ? "" : "NOT NULL");
var isPrimaryKey = column.Table?.PrimaryKey.Length == 1 && column.Table?.PrimaryKey.Contains(column) == true;
var type = isPrimaryKey && (PrimaryKeyStrategy == PrimaryKeyStrategy.Serial) ? "SERIAL" : GetStoreType(column);
var notnull = (column.AllowDBNull ? "" : "NOT NULL");
var identity = (PrimaryKeyStrategy == PrimaryKeyStrategy.Identity) && isPrimaryKey ? Identity : String.Empty;
return string.Format("{0} {1} {2} {3}",
FormatName(column.ColumnName),
GetStoreType(column),
type,
notnull, identity).Trim();
}

Expand Down

0 comments on commit b921f8c

Please sign in to comment.