Skip to content

Commit

Permalink
Cache Dialect in all public Configuration methods (#3399)
Browse files Browse the repository at this point in the history
Wrap mapping usages in StaticDialectMappingWrapper

Fixes #3397
  • Loading branch information
bahusoid authored Aug 12, 2023
1 parent 218379c commit 737e2c8
Showing 1 changed file with 19 additions and 12 deletions.
31 changes: 19 additions & 12 deletions src/NHibernate/Cfg/Configuration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -246,10 +246,14 @@ private class StaticDialectMappingWrapper : IMapping
{
private readonly IMapping _mapping;

public StaticDialectMappingWrapper(IMapping mapping)
public StaticDialectMappingWrapper(IMapping mapping): this(mapping, mapping.Dialect)
{
}

public StaticDialectMappingWrapper(IMapping mapping, Dialect.Dialect dialect)
{
_mapping = mapping;
Dialect = mapping.Dialect;
Dialect = dialect;
}

public IType GetIdentifierType(string className)
Expand Down Expand Up @@ -941,11 +945,12 @@ public string[] GenerateSchemaCreationScript(Dialect.Dialect dialect)

var script = new List<string>();

var staticDialectMapping = new StaticDialectMappingWrapper(mapping, dialect);
foreach (var table in TableMappings)
{
if (table.IsPhysicalTable && IncludeAction(table.SchemaActions, SchemaAction.Export))
{
script.Add(table.SqlCreateString(dialect, mapping, defaultCatalog, defaultSchema));
script.Add(table.SqlCreateString(dialect, staticDialectMapping, defaultCatalog, defaultSchema));
script.AddRange(table.SqlCommentStrings(dialect, defaultCatalog, defaultSchema));
}
}
Expand All @@ -958,7 +963,7 @@ public string[] GenerateSchemaCreationScript(Dialect.Dialect dialect)
{
foreach (var uk in table.UniqueKeyIterator)
{
string constraintString = uk.SqlCreateString(dialect, mapping, defaultCatalog, defaultSchema);
string constraintString = uk.SqlCreateString(dialect, staticDialectMapping, defaultCatalog, defaultSchema);
if (constraintString != null)
{
script.Add(constraintString);
Expand All @@ -968,7 +973,7 @@ public string[] GenerateSchemaCreationScript(Dialect.Dialect dialect)

foreach (var index in table.IndexIterator)
{
script.Add(index.SqlCreateString(dialect, mapping, defaultCatalog, defaultSchema));
script.Add(index.SqlCreateString(dialect, staticDialectMapping, defaultCatalog, defaultSchema));
}

if (dialect.SupportsForeignKeyConstraintInAlterTable)
Expand All @@ -977,7 +982,7 @@ public string[] GenerateSchemaCreationScript(Dialect.Dialect dialect)
{
if (fk.IsGenerated(dialect) && IncludeAction(fk.ReferencedTable.SchemaActions, SchemaAction.Export))
{
script.Add(fk.SqlCreateString(dialect, mapping, defaultCatalog, defaultSchema));
script.Add(fk.SqlCreateString(dialect, staticDialectMapping, defaultCatalog, defaultSchema));
}
}
}
Expand All @@ -994,7 +999,7 @@ public string[] GenerateSchemaCreationScript(Dialect.Dialect dialect)
{
if (auxDbObj.AppliesToDialect(dialect))
{
script.Add(auxDbObj.SqlCreateString(dialect, mapping, defaultCatalog, defaultSchema));
script.Add(auxDbObj.SqlCreateString(dialect, staticDialectMapping, defaultCatalog, defaultSchema));
}
}

Expand Down Expand Up @@ -2364,6 +2369,7 @@ public string[] GenerateSchemaUpdateScript(Dialect.Dialect dialect, IDatabaseMet
var defaultSchema = GetQuotedDefaultSchema(dialect);

var script = new List<string>(50);
var staticDialectMapping = new StaticDialectMappingWrapper(mapping, dialect);
foreach (var table in TableMappings)
{
if (table.IsPhysicalTable && IncludeAction(table.SchemaActions, SchemaAction.Update))
Expand All @@ -2372,11 +2378,11 @@ public string[] GenerateSchemaUpdateScript(Dialect.Dialect dialect, IDatabaseMet
table.Catalog ?? defaultCatalog, table.IsQuoted);
if (tableInfo == null)
{
script.Add(table.SqlCreateString(dialect, mapping, defaultCatalog, defaultSchema));
script.Add(table.SqlCreateString(dialect, staticDialectMapping, defaultCatalog, defaultSchema));
}
else
{
string[] alterDDL = table.SqlAlterStrings(dialect, mapping, tableInfo, defaultCatalog, defaultSchema);
string[] alterDDL = table.SqlAlterStrings(dialect, staticDialectMapping, tableInfo, defaultCatalog, defaultSchema);
script.AddRange(alterDDL);
}

Expand Down Expand Up @@ -2404,7 +2410,7 @@ public string[] GenerateSchemaUpdateScript(Dialect.Dialect dialect, IDatabaseMet
&& (!(dialect is MySQLDialect) || tableInfo.GetIndexMetadata(fk.Name) == null));
if (create)
{
script.Add(fk.SqlCreateString(dialect, mapping, defaultCatalog, defaultSchema));
script.Add(fk.SqlCreateString(dialect, staticDialectMapping, defaultCatalog, defaultSchema));
}
}
}
Expand All @@ -2414,7 +2420,7 @@ public string[] GenerateSchemaUpdateScript(Dialect.Dialect dialect, IDatabaseMet
{
if (tableInfo == null || tableInfo.GetIndexMetadata(index.Name) == null)
{
script.Add(index.SqlCreateString(dialect, mapping, defaultCatalog, defaultSchema));
script.Add(index.SqlCreateString(dialect, staticDialectMapping, defaultCatalog, defaultSchema));
}
}
}
Expand All @@ -2440,6 +2446,7 @@ public void ValidateSchema(Dialect.Dialect dialect, IDatabaseMetadata databaseMe
{
SecondPassCompile();

var staticDialectMapping = new StaticDialectMappingWrapper(mapping, dialect);
var defaultCatalog = GetQuotedDefaultCatalog(dialect);
var defaultSchema = GetQuotedDefaultSchema(dialect);

Expand All @@ -2466,7 +2473,7 @@ public void ValidateSchema(Dialect.Dialect dialect, IDatabaseMetadata databaseMe
}
else
{
validationErrors.AddRange(table.ValidateColumns(dialect, mapping, tableInfo));
validationErrors.AddRange(table.ValidateColumns(dialect, staticDialectMapping, tableInfo));
}
}
}
Expand Down

0 comments on commit 737e2c8

Please sign in to comment.