Skip to content

Commit 82f3e99

Browse files
committed
Code clean-up
- Dialect encapsulates Keywords - AbstractDataBaseSchema and SybaseAnywhereColumnMetaData return case insensitive hash set (in fact, they can return an array/IEnumerable<string> instead of ISet<string>) - SchemaMetadataUpdater does not do unnecessary hash sets recast - SchemaMetadataUpdater just sets IsQuoted for table and column names, so it does not do unnecessary string operations
1 parent 507239f commit 82f3e99

File tree

5 files changed

+51
-69
lines changed

5 files changed

+51
-69
lines changed

src/NHibernate/Dialect/Dialect.cs

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,6 @@ public abstract class Dialect
4444
private readonly TypeNames _hibernateTypeNames = new TypeNames();
4545
private readonly IDictionary<string, string> _properties = new Dictionary<string, string>();
4646
private readonly IDictionary<string, ISQLFunction> _sqlFunctions;
47-
private readonly HashSet<string> _sqlKeywords;
4847

4948
private static readonly IDictionary<string, ISQLFunction> StandardAggregateFunctions = CollectionHelper.CreateCaseInsensitiveHashtable<ISQLFunction>();
5049

@@ -82,7 +81,7 @@ protected Dialect()
8281

8382
_sqlFunctions = CollectionHelper.CreateCaseInsensitiveHashtable(StandardAggregateFunctions);
8483

85-
_sqlKeywords = new HashSet<string>(AnsiSqlKeywords.Sql2003, StringComparer.OrdinalIgnoreCase);
84+
Keywords = new HashSet<string>(AnsiSqlKeywords.Sql2003, StringComparer.OrdinalIgnoreCase);
8685

8786
// standard sql92 functions (can be overridden by subclasses)
8887
RegisterFunction("substring", new AnsiSubstringFunction());
@@ -2088,10 +2087,7 @@ public virtual IDictionary<string, ISQLFunction> Functions
20882087
get { return _sqlFunctions; }
20892088
}
20902089

2091-
public HashSet<string> Keywords
2092-
{
2093-
get { return _sqlKeywords; }
2094-
}
2090+
public HashSet<string> Keywords { get; }
20952091

20962092
/// <summary>
20972093
/// Get the command used to select a GUID from the underlying database.
@@ -2248,11 +2244,21 @@ protected void RegisterKeyword(string word)
22482244
Keywords.Add(word);
22492245
}
22502246

2251-
protected void RegisterKeywords(params string[] keywords)
2247+
protected internal void RegisterKeywords(params string[] keywords)
22522248
{
22532249
Keywords.UnionWith(keywords);
22542250
}
22552251

2252+
protected internal void RegisterKeywords(IEnumerable<string> keywords)
2253+
{
2254+
Keywords.UnionWith(keywords);
2255+
}
2256+
2257+
public bool IsKeyword(string str)
2258+
{
2259+
return Keywords.Contains(str);
2260+
}
2261+
22562262
protected void RegisterFunction(string name, ISQLFunction function)
22572263
{
22582264
_sqlFunctions[name] = function;
@@ -2288,4 +2294,4 @@ public virtual ISQLExceptionConverter BuildSQLExceptionConverter()
22882294
return new SQLStateConverter(ViolatedConstraintNameExtracter);
22892295
}
22902296
}
2291-
}
2297+
}

src/NHibernate/Dialect/Schema/AbstractDataBaseSchema.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
using System;
12
using System.Collections.Generic;
23
using System.Data;
34
using System.Data.Common;
@@ -95,7 +96,7 @@ public virtual DataTable GetForeignKeys(string catalog, string schema, string ta
9596

9697
public virtual ISet<string> GetReservedWords()
9798
{
98-
var result = new HashSet<string>();
99+
var result = new HashSet<string>(StringComparer.OrdinalIgnoreCase);
99100
DataTable dtReservedWords = connection.GetSchema(DbMetaDataCollectionNames.ReservedWords);
100101
foreach (DataRow row in dtReservedWords.Rows)
101102
{
@@ -121,4 +122,4 @@ protected virtual string ForeignKeysSchemaName
121122

122123
#endregion
123124
}
124-
}
125+
}

src/NHibernate/Dialect/Schema/SybaseAnywhereMetaData.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ public override ITableMetadata GetTableMetadata(DataRow rs, bool extras)
1818

1919
public override ISet<string> GetReservedWords()
2020
{
21-
var result = new HashSet<string>();
21+
var result = new HashSet<string>(StringComparer.OrdinalIgnoreCase);
2222
DataTable dtReservedWords = Connection.GetSchema(DbMetaDataCollectionNames.ReservedWords);
2323
foreach (DataRow row in dtReservedWords.Rows)
2424
{
@@ -143,4 +143,4 @@ public SybaseAnywhereForeignKeyMetaData(DataRow rs) : base(rs)
143143
Name = (string) rs["COLUMN_NAME"];
144144
}
145145
}
146-
}
146+
}

src/NHibernate/SqlCommand/Template.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -308,7 +308,7 @@ private static bool IsFunctionOrKeyword(string lcToken, string nextToken, Dialec
308308
return "(".Equals(nextToken) ||
309309
Keywords.Contains(lcToken) ||
310310
functionRegistry.HasFunction(lcToken) ||
311-
dialect.Keywords.Contains(lcToken) ||
311+
dialect.IsKeyword(lcToken) ||
312312
dialect.IsKnownToken(lcToken, nextToken) ||
313313
FunctionKeywords.Contains(lcToken);
314314
}
@@ -321,4 +321,4 @@ private static bool IsIdentifier(string token, Dialect.Dialect dialect)
321321
);
322322
}
323323
}
324-
}
324+
}

src/NHibernate/Tool/hbm2ddl/SchemaMetadataUpdater.cs

Lines changed: 30 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
using NHibernate.Engine;
44
using NHibernate.Mapping;
55
using System.Collections.Generic;
6-
using NHibernate.Connection;
76

87
namespace NHibernate.Tool.hbm2ddl
98
{
@@ -12,95 +11,71 @@ public static class SchemaMetadataUpdater
1211
{
1312
public static void Update(ISessionFactoryImplementor sessionFactory)
1413
{
15-
var reservedWords = GetReservedWords(sessionFactory.ConnectionProvider, sessionFactory.Dialect);
16-
sessionFactory.Dialect.Keywords.UnionWith(reservedWords);
14+
UpdateDialectKeywords(
15+
sessionFactory.Dialect,
16+
new SuppliedConnectionProviderConnectionHelper(sessionFactory.ConnectionProvider));
1717
}
1818

1919
public static void Update(Configuration configuration, Dialect.Dialect dialect)
2020
{
21-
dialect.Keywords.UnionWith(GetReservedWords(configuration, dialect));
21+
UpdateDialectKeywords(
22+
dialect,
23+
new ManagedProviderConnectionHelper(configuration.GetDerivedProperties()));
2224
}
2325

24-
[Obsolete("Use the overload that passes dialect so keywords will be updated and persisted before auto-quoting")]
25-
public static void QuoteTableAndColumns(Configuration configuration)
26-
{
27-
// Instantiates a new instance of the dialect so doesn't benefit from the Update call.
28-
var dialect = Dialect.Dialect.GetDialect(configuration.GetDerivedProperties());
29-
Update(configuration, dialect);
30-
QuoteTableAndColumns(configuration, dialect);
31-
}
32-
33-
public static void QuoteTableAndColumns(Configuration configuration, Dialect.Dialect dialect)
26+
static void UpdateDialectKeywords(Dialect.Dialect dialect, IConnectionHelper connectionHelper)
3427
{
35-
ISet<string> reservedDb = dialect.Keywords;
36-
37-
foreach (var cm in configuration.ClassMappings)
38-
{
39-
QuoteTable(cm.Table, reservedDb);
40-
}
41-
foreach (var cm in configuration.CollectionMappings)
42-
{
43-
QuoteTable(cm.Table, reservedDb);
44-
}
28+
dialect.RegisterKeywords(GetReservedWords(dialect, connectionHelper));
4529
}
4630

47-
private static ISet<string> GetReservedWords(Configuration configuration, Dialect.Dialect dialect)
31+
static IEnumerable<string> GetReservedWords(Dialect.Dialect dialect, IConnectionHelper connectionHelper)
4832
{
49-
IConnectionHelper connectionHelper = new ManagedProviderConnectionHelper(configuration.GetDerivedProperties());
5033
connectionHelper.Prepare();
5134
try
5235
{
53-
return GetReservedWords(dialect, connectionHelper);
36+
var metaData = dialect.GetDataBaseSchema(connectionHelper.Connection);
37+
return metaData.GetReservedWords();
5438
}
5539
finally
5640
{
5741
connectionHelper.Release();
5842
}
5943
}
6044

61-
private static ISet<string> GetReservedWords(IConnectionProvider connectionProvider, Dialect.Dialect dialect)
45+
[Obsolete("Use the overload that passes dialect so keywords will be updated and persisted before auto-quoting")]
46+
public static void QuoteTableAndColumns(Configuration configuration)
6247
{
63-
IConnectionHelper connectionHelper = new SuppliedConnectionProviderConnectionHelper(connectionProvider);
64-
connectionHelper.Prepare();
65-
try
66-
{
67-
return GetReservedWords(dialect, connectionHelper);
68-
}
69-
finally
70-
{
71-
connectionHelper.Release();
72-
}
48+
// Instantiates a new instance of the dialect so doesn't benefit from the Update call.
49+
var dialect = Dialect.Dialect.GetDialect(configuration.GetDerivedProperties());
50+
Update(configuration, dialect);
51+
QuoteTableAndColumns(configuration, dialect);
7352
}
7453

75-
private static ISet<string> GetReservedWords(Dialect.Dialect dialect, IConnectionHelper connectionHelper)
54+
public static void QuoteTableAndColumns(Configuration configuration, Dialect.Dialect dialect)
7655
{
77-
ISet<string> reservedWords = new HashSet<string>(dialect.Keywords, StringComparer.OrdinalIgnoreCase);
78-
var metaData = dialect.GetDataBaseSchema(connectionHelper.Connection);
79-
foreach (var rw in metaData.GetReservedWords())
56+
foreach (var cm in configuration.ClassMappings)
8057
{
81-
reservedWords.Add(rw);
58+
QuoteTable(cm.Table, dialect);
59+
}
60+
foreach (var cm in configuration.CollectionMappings)
61+
{
62+
QuoteTable(cm.Table, dialect);
8263
}
83-
return reservedWords;
8464
}
8565

86-
private static void QuoteTable(Table table, ICollection<string> reservedDb)
66+
private static void QuoteTable(Table table, Dialect.Dialect dialect)
8767
{
88-
if (!table.IsQuoted && reservedDb.Contains(table.Name))
68+
if (!table.IsQuoted && dialect.IsKeyword(table.Name))
8969
{
90-
table.Name = GetNhQuoted(table.Name);
70+
table.IsQuoted = true;
9171
}
9272
foreach (var column in table.ColumnIterator)
9373
{
94-
if (!column.IsQuoted && reservedDb.Contains(column.Name))
74+
if (!column.IsQuoted && dialect.IsKeyword(column.Name))
9575
{
96-
column.Name = GetNhQuoted(column.Name);
76+
column.IsQuoted = true;
9777
}
9878
}
9979
}
100-
101-
private static string GetNhQuoted(string name)
102-
{
103-
return "`" + name + "`";
104-
}
10580
}
106-
}
81+
}

0 commit comments

Comments
 (0)