forked from nhibernate/nhibernate-core
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMsSqlCeDialect.cs
378 lines (331 loc) · 10.5 KB
/
MsSqlCeDialect.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
using System;
using System.Data;
using System.Data.Common;
using System.Text;
using NHibernate.Dialect.Function;
using NHibernate.Dialect.Schema;
using NHibernate.Id;
using NHibernate.SqlCommand;
using NHibernate.Util;
using Environment = NHibernate.Cfg.Environment;
namespace NHibernate.Dialect
{
/// <summary>
/// A dialect for SQL Server Everywhere (SQL Server CE).
/// </summary>
public class MsSqlCeDialect : Dialect
{
public MsSqlCeDialect()
{
RegisterTypeMapping();
RegisterFunctions();
RegisterKeywords();
RegisterDefaultProperties();
}
#region private static readonly string[] DialectKeywords = { ... }
private static readonly string[] DialectKeywords =
{
"apply",
"asc",
"backup",
"bit",
"break",
"browse",
"bulk",
"cascade",
"checkpoint",
"clustered",
"coalesce",
"compute",
"contains",
"containstable",
"convert",
"database",
"datetime",
"dbcc",
"deny",
"desc",
"disk",
"distributed",
"dump",
"errlvl",
"file",
"fillfactor",
"first",
"freetext",
"freetexttable",
"goto",
"holdlock",
"identity_insert",
"identitycol",
"image",
"index",
"key",
"kill",
"lineno",
"load",
"money",
"next",
"nocheck",
"nonclustered",
"ntext",
"nullif",
"nvarchar",
"off",
"offset",
"offsets",
"opendatasource",
"openquery",
"openrowset",
"openxml",
"option",
"percent",
"pivot",
"plan",
"print",
"proc",
"public",
"raiserror",
"read",
"readtext",
"reconfigure",
"replication",
"restore",
"restrict",
"revert",
"rowcount",
"rowguidcol",
"rowversion",
"rule",
"save",
"schema",
"session_user",
"setuser",
"shutdown",
"statistics",
"textsize",
"tinyint",
"top",
"tran",
"transaction",
"truncate",
"tsequal",
"uniqueidentifier",
"unpivot",
"updatetext",
"use",
"varbinary",
"view",
"waitfor",
"writetext",
"xmlunnest",
};
#endregion
protected virtual void RegisterKeywords()
{
RegisterKeywords(DialectKeywords);
}
protected virtual void RegisterTypeMapping()
{
RegisterColumnType(DbType.AnsiStringFixedLength, "NCHAR(255)");
RegisterColumnType(DbType.AnsiStringFixedLength, 4000, "NCHAR($l)");
RegisterColumnType(DbType.AnsiString, "NVARCHAR(255)");
RegisterColumnType(DbType.AnsiString, 4000, "NVARCHAR($l)");
RegisterColumnType(DbType.AnsiString, 1073741823, "NTEXT");
RegisterColumnType(DbType.Binary, "VARBINARY(8000)");
RegisterColumnType(DbType.Binary, 8000, "VARBINARY($l)");
RegisterColumnType(DbType.Binary, 1073741823, "IMAGE");
RegisterColumnType(DbType.Boolean, "BIT");
RegisterColumnType(DbType.Byte, "TINYINT");
RegisterColumnType(DbType.Currency, "MONEY");
RegisterColumnType(DbType.Date, "DATETIME");
RegisterColumnType(DbType.DateTime, "DATETIME");
RegisterColumnType(DbType.Decimal, "NUMERIC(19,5)");
// SQL Server CE max precision is 38, but .Net is limited to 28-29.
RegisterColumnType(DbType.Decimal, 29, "NUMERIC($p, $s)");
RegisterColumnType(DbType.Double, "FLOAT");
RegisterColumnType(DbType.Guid, "UNIQUEIDENTIFIER");
RegisterColumnType(DbType.Int16, "SMALLINT");
RegisterColumnType(DbType.Int32, "INT");
RegisterColumnType(DbType.Int64, "BIGINT");
RegisterColumnType(DbType.Single, "REAL"); //synonym for FLOAT(24)
RegisterColumnType(DbType.StringFixedLength, "NCHAR(255)");
RegisterColumnType(DbType.StringFixedLength, 4000, "NCHAR($l)");
RegisterColumnType(DbType.String, "NVARCHAR(255)");
RegisterColumnType(DbType.String, 4000, "NVARCHAR($l)");
RegisterColumnType(DbType.String, 1073741823, "NTEXT");
RegisterColumnType(DbType.Time, "DATETIME");
}
protected virtual void RegisterFunctions()
{
RegisterFunction("substring", new EmulatedLengthSubstringFunction());
RegisterFunction("str", new SQLFunctionTemplate(NHibernateUtil.String, "cast(?1 as nvarchar)"));
RegisterFunction("strguid", new SQLFunctionTemplate(NHibernateUtil.String, "cast(?1 as nvarchar)"));
RegisterFunction("current_timestamp", new NoArgSQLFunction("getdate", NHibernateUtil.LocalDateTime, true));
RegisterFunction("current_date", new SQLFunctionTemplate(NHibernateUtil.LocalDate, "dateadd(dd, 0, datediff(dd, 0, getdate()))"));
RegisterFunction("date", new SQLFunctionTemplate(NHibernateUtil.DateTime, "dateadd(dd, 0, datediff(dd, 0, ?1))"));
RegisterFunction("second", new SQLFunctionTemplate(NHibernateUtil.Int32, "datepart(second, ?1)"));
RegisterFunction("minute", new SQLFunctionTemplate(NHibernateUtil.Int32, "datepart(minute, ?1)"));
RegisterFunction("hour", new SQLFunctionTemplate(NHibernateUtil.Int32, "datepart(hour, ?1)"));
RegisterFunction("day", new SQLFunctionTemplate(NHibernateUtil.Int32, "datepart(day, ?1)"));
RegisterFunction("month", new SQLFunctionTemplate(NHibernateUtil.Int32, "datepart(month, ?1)"));
RegisterFunction("year", new SQLFunctionTemplate(NHibernateUtil.Int32, "datepart(year, ?1)"));
RegisterFunction("length", new StandardSQLFunction("len", NHibernateUtil.Int32));
RegisterFunction("locate", new StandardSQLFunction("charindex", NHibernateUtil.Int32));
RegisterFunction("replace", new StandardSafeSQLFunction("replace", NHibernateUtil.String, 3));
RegisterFunction("rtrim", new StandardSQLFunction("rtrim"));
RegisterFunction("ltrim", new StandardSQLFunction("ltrim"));
RegisterFunction("upper", new StandardSQLFunction("upper"));
RegisterFunction("lower", new StandardSQLFunction("lower"));
RegisterFunction("trim", new AnsiTrimEmulationFunction());
RegisterFunction("iif", new IifSQLFunction());
RegisterFunction("concat", new VarArgsSQLFunction(NHibernateUtil.String, "(", "+", ")"));
// Modulo is not supported on real, float, money, and numeric data types
RegisterFunction("mod", new ModulusFunctionTemplate(false));
RegisterFunction("round", new StandardSQLFunctionWithRequiredParameters("round", new object[] {null, "0"}));
RegisterFunction("truncate", new StandardSQLFunctionWithRequiredParameters("round", new object[] {null, "0", "1"}));
RegisterFunction("bit_length", new SQLFunctionTemplate(NHibernateUtil.Int32, "datalength(?1) * 8"));
RegisterFunction("extract", new SQLFunctionTemplate(NHibernateUtil.Int32, "datepart(?1, ?3)"));
RegisterFunction("new_uuid", new NoArgSQLFunction("newid", NHibernateUtil.Guid));
}
protected virtual void RegisterDefaultProperties()
{
DefaultProperties[Environment.ConnectionDriver] = "NHibernate.Driver.SqlServerCeDriver";
DefaultProperties[Environment.PrepareSql] = "false";
}
public override string AddColumnString
{
get { return "add"; }
}
public override string NullColumnString
{
get { return " null"; }
}
public override bool QualifyIndexName
{
get { return false; }
}
public override string ForUpdateString
{
get { return string.Empty; }
}
public override bool SupportsIdentityColumns
{
get { return true; }
}
public override string IdentitySelectString
{
get { return "select @@IDENTITY"; }
}
public override string IdentityColumnString
{
get { return "IDENTITY NOT NULL"; }
}
public override string SelectGUIDString
{
get { return "select newid()"; }
}
public override bool SupportsLimit
{
get { return true; }
}
public override bool SupportsLimitOffset
{
get { return false; }
}
public override System.Type NativeIdentifierGeneratorClass => typeof(TableHiLoGenerator);
public override bool SupportsCircularCascadeDeleteConstraints => false;
public override IDataBaseSchema GetDataBaseSchema(DbConnection connection)
{
return new MsSqlCeDataBaseSchema(connection, this);
}
public override SqlString GetLimitString(SqlString querySqlString, SqlString offset, SqlString limit)
{
var top = new SqlString(" top (", limit, ")");
return querySqlString.Insert(GetAfterSelectInsertPoint(querySqlString), top);
}
public override string Qualify(string catalog, string schema, string table)
{
// SQL Server Compact doesn't support Schemas. So join schema name and table name with underscores
// similar to the SQLLite dialect.
var qualifiedName = new StringBuilder();
bool quoted = false;
if (!string.IsNullOrEmpty(catalog))
{
qualifiedName.Append(catalog).Append(StringHelper.Dot);
}
var tableName = new StringBuilder();
if (!string.IsNullOrEmpty(schema))
{
if (schema.StartsWith(OpenQuote))
{
schema = schema.Substring(1, schema.Length - 1);
quoted = true;
}
if (schema.EndsWith(CloseQuote))
{
schema = schema.Substring(0, schema.Length - 1);
quoted = true;
}
tableName.Append(schema).Append(StringHelper.Underscore);
}
if (table.StartsWith(OpenQuote))
{
table = table.Substring(1, table.Length - 1);
quoted = true;
}
if (table.EndsWith(CloseQuote))
{
table = table.Substring(0, table.Length - 1);
quoted = true;
}
string name = tableName.Append(table).ToString();
if (quoted)
name = OpenQuote + name + CloseQuote;
return qualifiedName.Append(name).ToString();
}
private static int GetAfterSelectInsertPoint(SqlString sql)
{
if (sql.StartsWithCaseInsensitive("select distinct"))
{
return 15;
}
if (sql.StartsWithCaseInsensitive("select"))
{
return 6;
}
throw new NotSupportedException("The query should start with 'SELECT' or 'SELECT DISTINCT'");
}
/// <summary>
/// Does this dialect support concurrent writing connections in the same transaction?
/// </summary>
public override bool SupportsConcurrentWritingConnectionsInSameTransaction => false;
public override long TimestampResolutionInTicks
{
get
{
// MS SQL resolution is actually 3.33 ms, rounded here to 10 ms
return TimeSpan.TicksPerMillisecond*10L;
}
}
// SQL Server 3.5 supports 128.
/// <inheritdoc />
public override int MaxAliasLength => 128;
#region Informational metadata
/// <summary>
/// Does this dialect support pooling parameter in connection string?
/// </summary>
public override bool SupportsPoolingParameter => false;
/// <inheritdoc/>
public override bool SupportsScalarSubSelects => false;
/// <summary>
/// Does this dialect support distributed transaction?
/// </summary>
/// <remarks>
/// Fails enlisting a connection into a distributed transaction, fails promoting a transaction
/// to distributed when it has already a connection enlisted.
/// </remarks>
public override bool SupportsDistributedTransactions => false;
#endregion
}
}