Skip to content

Commit 0ad0ba1

Browse files
ngbrownhazzik
authored andcommitted
NH-4043 - Expand keywords list saved in Dialect files
- Baseline all dialects with ANSI SQL 2003 keywords - Add TypeNames to returned keyword words list
1 parent 1734eb2 commit 0ad0ba1

16 files changed

+1251
-76
lines changed

src/NHibernate.Test/Tools/hbm2ddl/SchemaMetadataUpdaterTest/SchemaMetadataUpdaterFixture.cs

Lines changed: 90 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
using System;
22
using System.Collections.Generic;
33
using System.Linq;
4-
using NHibernate.Cfg;
4+
using NHibernate.Dialect;
5+
using NHibernate.Driver;
56
using NHibernate.Engine;
67
using NHibernate.Mapping;
78
using NHibernate.Tool.hbm2ddl;
@@ -58,9 +59,97 @@ public void UpdateReservedWordsInDialect()
5859
var sf = (ISessionFactoryImplementor) configuration.BuildSessionFactory();
5960
SchemaMetadataUpdater.Update(sf);
6061
var match = reservedDb.Intersect(sf.Dialect.Keywords, StringComparer.OrdinalIgnoreCase);
62+
63+
// tests that nothing in the first metaData.GetReservedWords() is left out of the second metaData.GetReservedWords() call.
64+
// i.e. always passes.
6165
Assert.That(match, Is.EquivalentTo(reservedDb));
6266
}
6367

68+
[Test]
69+
public void EnsureReservedWordsHardCodedInDialect()
70+
{
71+
var reservedDb = new HashSet<string>();
72+
var configuration = TestConfigurationHelper.GetDefaultConfiguration();
73+
var dialect = Dialect.Dialect.GetDialect(configuration.Properties);
74+
var connectionHelper = new ManagedProviderConnectionHelper(configuration.Properties);
75+
connectionHelper.Prepare();
76+
try
77+
{
78+
var metaData = dialect.GetDataBaseSchema(connectionHelper.Connection);
79+
foreach (var rw in metaData.GetReservedWords())
80+
{
81+
if (rw.Contains(" ")) continue;
82+
reservedDb.Add(rw.ToLowerInvariant());
83+
}
84+
}
85+
finally
86+
{
87+
connectionHelper.Release();
88+
}
89+
90+
var sf = (ISessionFactoryImplementor)configuration.BuildSessionFactory();
91+
92+
// use the dialect as configured, with no update
93+
var match = reservedDb.Intersect(sf.Dialect.Keywords).ToList();
94+
95+
// tests that nothing in metaData.GetReservedWords() is left out of the Dialect.Keywords (without a refresh).
96+
var differences = reservedDb.Except(match).ToList();
97+
if (differences.Count > 0)
98+
{
99+
Console.WriteLine("Update Dialect {0} with RegisterKeyword:", sf.Dialect.GetType().Name);
100+
foreach (var keyword in differences.OrderBy(x => x))
101+
{
102+
Console.WriteLine(" RegisterKeyword(\"{0}\");", keyword);
103+
}
104+
}
105+
106+
if (sf.ConnectionProvider.Driver is OdbcDriver)
107+
{
108+
Assert.Inconclusive("ODBC has excess keywords reserved");
109+
}
110+
111+
Assert.That(match, Is.EquivalentTo(reservedDb));
112+
}
113+
114+
[Test, Explicit]
115+
public void CheckForExcessReservedWordsHardCodedInDialect()
116+
{
117+
var reservedDb = new HashSet<string>();
118+
var configuration = TestConfigurationHelper.GetDefaultConfiguration();
119+
var dialect = Dialect.Dialect.GetDialect(configuration.Properties);
120+
var connectionHelper = new ManagedProviderConnectionHelper(configuration.Properties);
121+
connectionHelper.Prepare();
122+
try
123+
{
124+
var metaData = dialect.GetDataBaseSchema(connectionHelper.Connection);
125+
foreach (var rw in metaData.GetReservedWords())
126+
{
127+
reservedDb.Add(rw.ToLowerInvariant());
128+
}
129+
}
130+
finally
131+
{
132+
connectionHelper.Release();
133+
}
134+
135+
var sf = (ISessionFactoryImplementor)configuration.BuildSessionFactory();
136+
137+
// use the dialect as configured, with no update
138+
// tests that nothing in Dialect.Keyword is not in metaData.GetReservedWords()
139+
var differences = sf.Dialect.Keywords.Except(reservedDb).Except(AnsiSqlKeywords.Sql2003).ToList();
140+
if (differences.Count > 0)
141+
{
142+
Console.WriteLine("Excess RegisterKeyword in Dialect {0}:", sf.Dialect.GetType().Name);
143+
foreach (var keyword in differences.OrderBy(x => x))
144+
{
145+
Console.WriteLine(" RegisterKeyword(\"{0}\");", keyword);
146+
}
147+
}
148+
149+
// Don't fail incase the driver returns nothing.
150+
// This is an info-only test.
151+
}
152+
64153
[Test]
65154
public void ExplicitAutoQuote()
66155
{
Lines changed: 267 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,267 @@
1+
using System.Collections.Generic;
2+
3+
namespace NHibernate.Dialect
4+
{
5+
/// <summary>
6+
/// Maintains the set of ANSI SQL keywords
7+
/// </summary>
8+
public class AnsiSqlKeywords
9+
{
10+
/// <summary>
11+
/// Retrieve all keywords defined by ANSI SQL:2003
12+
/// </summary>
13+
public static readonly IReadOnlyCollection<string> Sql2003;
14+
15+
static AnsiSqlKeywords()
16+
{
17+
var keywordsSql2003 = new []
18+
{
19+
"add",
20+
"all",
21+
"allocate",
22+
"alter",
23+
"and",
24+
"any",
25+
"are",
26+
"array",
27+
"as",
28+
"asensitive",
29+
"asymmetric",
30+
"at",
31+
"atomic",
32+
"authorization",
33+
"begin",
34+
"between",
35+
"bigint",
36+
"binary",
37+
"blob",
38+
"both",
39+
"by",
40+
"call",
41+
"called",
42+
"cascaded",
43+
"case",
44+
"cast",
45+
"char",
46+
"character",
47+
"check",
48+
"clob",
49+
"close",
50+
"collate",
51+
"column",
52+
"commit",
53+
"condition",
54+
"connect",
55+
"constraint",
56+
"continue",
57+
"corresponding",
58+
"create",
59+
"cross",
60+
"cube",
61+
"current",
62+
"current_date",
63+
"current_path",
64+
"current_role",
65+
"current_time",
66+
"current_timestamp",
67+
"current_user",
68+
"cursor",
69+
"cycle",
70+
"date",
71+
"day",
72+
"deallocate",
73+
"dec",
74+
"decimal",
75+
"declare",
76+
"default",
77+
"delete",
78+
"deref",
79+
"describe",
80+
"deterministic",
81+
"disconnect",
82+
"distinct",
83+
"do",
84+
"double",
85+
"drop",
86+
"dynamic",
87+
"each",
88+
"element",
89+
"else",
90+
"elsif",
91+
"end",
92+
"escape",
93+
"except",
94+
"exec",
95+
"execute",
96+
"exists",
97+
"exit",
98+
"external",
99+
"false",
100+
"fetch",
101+
"filter",
102+
"float",
103+
"for",
104+
"foreign",
105+
"free",
106+
"from",
107+
"full",
108+
"function",
109+
"get",
110+
"global",
111+
"grant",
112+
"group",
113+
"grouping",
114+
"handler",
115+
"having",
116+
"hold",
117+
"hour",
118+
"identity",
119+
"if",
120+
"immediate",
121+
"in",
122+
"indicator",
123+
"inner",
124+
"inout",
125+
"input",
126+
"insensitive",
127+
"insert",
128+
"int",
129+
"integer",
130+
"intersect",
131+
"interval",
132+
"into",
133+
"is",
134+
"iterate",
135+
"join",
136+
"language",
137+
"large",
138+
"lateral",
139+
"leading",
140+
"leave",
141+
"left",
142+
"like",
143+
"local",
144+
"localtime",
145+
"localtimestamp",
146+
"loop",
147+
"match",
148+
"member",
149+
"merge",
150+
"method",
151+
"minute",
152+
"modifies",
153+
"module",
154+
"month",
155+
"multiset",
156+
"national",
157+
"natural",
158+
"nchar",
159+
"nclob",
160+
"new",
161+
"no",
162+
"none",
163+
"not",
164+
"null",
165+
"numeric",
166+
"of",
167+
"old",
168+
"on",
169+
"only",
170+
"open",
171+
"or",
172+
"order",
173+
"out",
174+
"outer",
175+
"output",
176+
"over",
177+
"overlaps",
178+
"parameter",
179+
"partition",
180+
"precision",
181+
"prepare",
182+
"primary",
183+
"procedure",
184+
"range",
185+
"reads",
186+
"real",
187+
"recursive",
188+
"ref",
189+
"references",
190+
"referencing",
191+
"release",
192+
"repeat",
193+
"resignal",
194+
"result",
195+
"return",
196+
"returns",
197+
"revoke",
198+
"right",
199+
"rollback",
200+
"rollup",
201+
"row",
202+
"rows",
203+
"savepoint",
204+
"scroll",
205+
"search",
206+
"second",
207+
"select",
208+
"sensitive",
209+
"session_use",
210+
"set",
211+
"signal",
212+
"similar",
213+
"smallint",
214+
"some",
215+
"specific",
216+
"specifictype",
217+
"sql",
218+
"sqlexception",
219+
"sqlstate",
220+
"sqlwarning",
221+
"start",
222+
"static",
223+
"submultiset",
224+
"symmetric",
225+
"system",
226+
"system_user",
227+
"table",
228+
"tablesample",
229+
"then",
230+
"time",
231+
"timestamp",
232+
"timezone_hour",
233+
"timezone_minute",
234+
"to",
235+
"trailing",
236+
"translation",
237+
"treat",
238+
"trigger",
239+
"true",
240+
"undo",
241+
"union",
242+
"unique",
243+
"unknown",
244+
"unnest",
245+
"until",
246+
"update",
247+
"user",
248+
"using",
249+
"value",
250+
"values",
251+
"varchar",
252+
"varying",
253+
"when",
254+
"whenever",
255+
"where",
256+
"while",
257+
"window",
258+
"with",
259+
"within",
260+
"without",
261+
"year",
262+
};
263+
264+
Sql2003 = keywordsSql2003;
265+
}
266+
}
267+
}

src/NHibernate/Dialect/Dialect.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ protected Dialect()
8181

8282
_sqlFunctions = CollectionHelper.CreateCaseInsensitiveHashtable(StandardAggregateFunctions);
8383

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

8686
// standard sql92 functions (can be overridden by subclasses)
8787
RegisterFunction("substring", new AnsiSubstringFunction());

0 commit comments

Comments
 (0)