forked from nhibernate/nhibernate-core
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSQLiteDialect.cs
596 lines (520 loc) · 18.3 KB
/
SQLiteDialect.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
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Data.Common;
using System.Linq;
using System.Text;
using NHibernate.Dialect.Function;
using NHibernate.Engine;
using NHibernate.SqlCommand;
using NHibernate.Type;
using NHibernate.Util;
namespace NHibernate.Dialect
{
/// <summary>
/// A SQL dialect for SQLite.
/// </summary>
/// <remarks>
/// <p>
/// Author: <a href="mailto:ib@stalker.ro"> Ioan Bizau </a>
/// </p>
/// </remarks>
public class SQLiteDialect : Dialect
{
/// <summary>
/// The effective value of the BinaryGuid connection string parameter.
/// The default value in SQLite is true.
/// </summary>
private bool _binaryGuid = true;
/// <summary>
///
/// </summary>
public SQLiteDialect()
{
RegisterColumnTypes();
RegisterFunctions();
RegisterKeywords();
RegisterDefaultProperties();
}
protected virtual void RegisterColumnTypes()
{
// SQLite really has only five types, and a very lax typing system, see https://www.sqlite.org/datatype3.html
// Please do not map (again) fancy types that do not actually exist in SQLite, as this is kind of supported by
// SQLite but creates bugs in convert operations.
RegisterColumnType(DbType.Binary, "BLOB");
RegisterColumnType(DbType.Byte, "INTEGER");
RegisterColumnType(DbType.Int16, "INTEGER");
RegisterColumnType(DbType.Int32, "INTEGER");
RegisterColumnType(DbType.Int64, "INTEGER");
RegisterColumnType(DbType.SByte, "INTEGER");
RegisterColumnType(DbType.UInt16, "INTEGER");
RegisterColumnType(DbType.UInt32, "INTEGER");
RegisterColumnType(DbType.UInt64, "INTEGER");
// NUMERIC and REAL are almost the same, they are binary floating point numbers. There is only a slight difference
// for values without a floating part. They will be represented as integers with numeric, but still as floating
// values with real. The side-effect of this is numeric being able of storing exactly bigger integers than real.
// But it also creates bugs in division, when dividing two numeric happening to be integers, the result is then
// never fractional. So we use "REAL" for all.
RegisterColumnType(DbType.Currency, "REAL");
RegisterColumnType(DbType.Decimal, "REAL");
RegisterColumnType(DbType.Double, "REAL");
RegisterColumnType(DbType.Single, "REAL");
RegisterColumnType(DbType.VarNumeric, "REAL");
RegisterColumnType(DbType.AnsiString, "TEXT");
RegisterColumnType(DbType.String, "TEXT");
RegisterColumnType(DbType.AnsiStringFixedLength, "TEXT");
RegisterColumnType(DbType.StringFixedLength, "TEXT");
// https://www.sqlite.org/datatype3.html#boolean_datatype
RegisterColumnType(DbType.Boolean, "INTEGER");
// See https://www.sqlite.org/datatype3.html#date_and_time_datatype, we have three choices for date and time
// The one causing the less issues in case of an explicit cast is text. Beware, System.Data.SQLite has an
// internal use only "DATETIME" type. Using it causes it to directly convert the text stored into SQLite to
// a .Net DateTime, but also causes columns in SQLite to have numeric affinity and convert to destroy the
// value. As said in their chm documentation, this "DATETIME" type is for System.Data.SQLite internal use only.
RegisterColumnType(DbType.Date, "TEXT");
RegisterColumnType(DbType.DateTime, "TEXT");
RegisterColumnType(DbType.Time, "TEXT");
RegisterColumnType(DbType.Guid, _binaryGuid ? "BLOB" : "TEXT");
}
protected virtual void RegisterFunctions()
{
// Using strftime returns 0-padded strings. '07' <> 7, so it is better to convert to an integer.
RegisterFunction("second", new SQLFunctionTemplate(NHibernateUtil.Int32, "cast(strftime('%S', ?1) as int)"));
RegisterFunction("minute", new SQLFunctionTemplate(NHibernateUtil.Int32, "cast(strftime('%M', ?1) as int)"));
RegisterFunction("hour", new SQLFunctionTemplate(NHibernateUtil.Int32, "cast(strftime('%H', ?1) as int)"));
RegisterFunction("day", new SQLFunctionTemplate(NHibernateUtil.Int32, "cast(strftime('%d', ?1) as int)"));
RegisterFunction("month", new SQLFunctionTemplate(NHibernateUtil.Int32, "cast(strftime('%m', ?1) as int)"));
RegisterFunction("year", new SQLFunctionTemplate(NHibernateUtil.Int32, "cast(strftime('%Y', ?1) as int)"));
// Uses local time like MSSQL and PostgreSQL.
RegisterFunction("current_timestamp", new SQLFunctionTemplate(NHibernateUtil.LocalDateTime, "datetime(current_timestamp, 'localtime')"));
RegisterFunction("current_utctimestamp", new SQLFunctionTemplate(NHibernateUtil.UtcDateTime, "datetime(current_timestamp)"));
// The System.Data.SQLite driver stores both Date and DateTime as 'YYYY-MM-DD HH:MM:SS'
// The SQLite date() function returns YYYY-MM-DD, which unfortunately SQLite does not consider
// as equal to 'YYYY-MM-DD 00:00:00'. Because of this, it is best to return the
// 'YYYY-MM-DD 00:00:00' format for the date function.
RegisterFunction("date", new SQLFunctionTemplate(NHibernateUtil.Date, "datetime(date(?1))"));
// SQLite has current_date, but as current_timestamp, it is in UTC. So converting the timestamp to
// localtime then to date then, like the above date function, go back to datetime format for comparisons
// sake.
RegisterFunction("current_date", new SQLFunctionTemplate(NHibernateUtil.LocalDate, "datetime(date(current_timestamp, 'localtime'))"));
RegisterFunction("substring", new StandardSQLFunction("substr", NHibernateUtil.String));
RegisterFunction("left", new SQLFunctionTemplate(NHibernateUtil.String, "substr(?1,1,?2)"));
RegisterFunction("right", new SQLFunctionTemplate(NHibernateUtil.String, "substr(?1,-?2)"));
RegisterFunction("trim", new AnsiTrimEmulationFunction());
RegisterFunction("replace", new StandardSafeSQLFunction("replace", NHibernateUtil.String, 3));
RegisterFunction("chr", new StandardSQLFunction("char", NHibernateUtil.Character));
RegisterFunction("locate", new LocateFunction());
RegisterFunction("mod", new ModulusFunctionTemplate(false));
RegisterFunction("iif", new IifSQLFunction());
RegisterFunction("round", new StandardSQLFunction("round"));
// SQLite has no built-in support of bitwise xor, but can emulate it.
// http://sqlite.1065341.n5.nabble.com/XOR-operator-td98004.html
RegisterFunction("bxor", new SQLFunctionTemplate(null, "((?1 | ?2) - (?1 & ?2))"));
// NH-3787: SQLite requires the cast in SQL too for not defaulting to string.
RegisterFunction("transparentcast", new CastFunction());
if (_binaryGuid)
RegisterFunction("strguid", new SQLFunctionTemplate(NHibernateUtil.String, "substr(hex(?1), 7, 2) || substr(hex(?1), 5, 2) || substr(hex(?1), 3, 2) || substr(hex(?1), 1, 2) || '-' || substr(hex(?1), 11, 2) || substr(hex(?1), 9, 2) || '-' || substr(hex(?1), 15, 2) || substr(hex(?1), 13, 2) || '-' || substr(hex(?1), 17, 4) || '-' || substr(hex(?1), 21) "));
else
RegisterFunction("strguid", new SQLFunctionTemplate(NHibernateUtil.String, "cast(?1 as text)"));
// SQLite random function yields a long, ranging form MinValue to MaxValue. (-9223372036854775808 to
// 9223372036854775807). HQL random requires a float from 0 inclusive to 1 exclusive, so we divide by
// 9223372036854775808 then 2 for having a value between -0.5 included to 0.5 excluded, and finally
// add 0.5. The division is written as "/ 4611686018427387904 / 4" for avoiding overflowing long.
RegisterFunction(
"random",
new SQLFunctionTemplate(
NHibernateUtil.Double,
"(cast(random() as real) / 4611686018427387904 / 4 + 0.5)"));
}
public override void Configure(IDictionary<string, string> settings)
{
base.Configure(settings);
ConfigureBinaryGuid(settings);
// Re-register functions and types depending on settings.
RegisterColumnTypes();
RegisterFunctions();
}
private void ConfigureBinaryGuid(IDictionary<string, string> settings)
{
// We can use a SQLite specific setting to force it, but in common cases it
// should be detected automatically from the connection string below.
settings.TryGetValue(Cfg.Environment.SqliteBinaryGuid, out var strBinaryGuid);
if (string.IsNullOrWhiteSpace(strBinaryGuid))
{
string connectionString = Cfg.Environment.GetConfiguredConnectionString(settings);
if (!string.IsNullOrWhiteSpace(connectionString))
{
var builder = new DbConnectionStringBuilder {ConnectionString = connectionString};
strBinaryGuid = GetConnectionStringProperty(builder, "BinaryGuid");
}
}
// Note that "BinaryGuid=false" is supported by System.Data.SQLite but not Microsoft.Data.Sqlite.
_binaryGuid = string.IsNullOrWhiteSpace(strBinaryGuid) || bool.Parse(strBinaryGuid);
}
private string GetConnectionStringProperty(DbConnectionStringBuilder builder, string propertyName)
{
builder.TryGetValue(propertyName, out object propertyValue);
return (string) propertyValue;
}
#region private static readonly string[] DialectKeywords = { ... }
private static readonly string[] DialectKeywords =
{
"abort",
"action",
"after",
"analyze",
"asc",
"attach",
"autoincrement",
"before",
"bit",
"bool",
"boolean",
"cascade",
"conflict",
"counter",
"currency",
"database",
"datetime",
"deferrable",
"deferred",
"desc",
"detach",
"exclusive",
"explain",
"fail",
"general",
"glob",
"guid",
"ignore",
"image",
"index",
"indexed",
"initially",
"instead",
"isnull",
"key",
"limit",
"logical",
"long",
"longtext",
"mediumint",
"memo",
"money",
"note",
"nothing",
"notnull",
"ntext",
"nvarchar",
"offset",
"oleobject",
"plan",
"pragma",
"query",
"raise",
"regexp",
"reindex",
"rename",
"replace",
"restrict",
"single",
"smalldate",
"smalldatetime",
"smallmoney",
"sql_variant",
"string",
"temp",
"temporary",
"text",
"tinyint",
"transaction",
"uniqueidentifier",
"vacuum",
"varbinary",
"view",
"virtual",
"yesno",
};
#endregion
protected virtual void RegisterKeywords()
{
RegisterKeywords(DialectKeywords);
}
protected virtual void RegisterDefaultProperties()
{
DefaultProperties[Cfg.Environment.ConnectionDriver] = "NHibernate.Driver.SQLite20Driver";
DefaultProperties[Cfg.Environment.QuerySubstitutions] = "true 1, false 0, yes 'Y', no 'N'";
}
public override Schema.IDataBaseSchema GetDataBaseSchema(DbConnection connection)
{
return new Schema.SQLiteDataBaseMetaData(connection, this);
}
public override string AddColumnString
{
get
{
return "add column";
}
}
public override string IdentitySelectString
{
get { return "select last_insert_rowid()"; }
}
public override SqlString AppendIdentitySelectToInsert(SqlString insertSql)
{
return insertSql.Append("; " + IdentitySelectString);
}
public override bool SupportsInsertSelectIdentity
{
get { return true; }
}
public override bool DropConstraints
{
get { return false; }
}
public override string ForUpdateString
{
get { return string.Empty; }
}
public override bool SupportsSubSelects
{
get { return true; }
}
public override bool SupportsIfExistsBeforeTableName
{
get { return true; }
}
public override bool HasDataTypeInIdentityColumn
{
get { return false; }
}
public override bool SupportsIdentityColumns
{
get { return true; }
}
public override bool SupportsLimit
{
get { return true; }
}
public override bool SupportsLimitOffset
{
get { return true; }
}
public override string IdentityColumnString
{
get
{
// Adding the "autoincrement" keyword ensures that the same id will
// not be generated twice. When just utilizing "integer primary key",
// SQLite just takes the max value currently in the table and adds one.
// This causes problems with caches that use primary keys of deleted
// entities.
return "integer primary key autoincrement";
}
}
public override bool GenerateTablePrimaryKeyConstraintForIdentityColumn
{
get { return false; }
}
/// <inheritdoc />
public override bool IsDecimalStoredAsFloatingPointNumber => true;
public override string Qualify(string catalog, string schema, string table)
{
StringBuilder qualifiedName = new StringBuilder();
bool quoted = false;
if (!string.IsNullOrEmpty(catalog))
{
if (catalog.StartsWith(OpenQuote))
{
catalog = catalog.Substring(1, catalog.Length - 1);
quoted = true;
}
if (catalog.EndsWith(CloseQuote))
{
catalog = catalog.Substring(0, catalog.Length - 1);
quoted = true;
}
qualifiedName.Append(catalog).Append(StringHelper.Underscore);
}
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;
}
qualifiedName.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 = qualifiedName.Append(table).ToString();
if (quoted)
return OpenQuote + name + CloseQuote;
return name;
}
public override string NoColumnsInsertString
{
get { return "DEFAULT VALUES"; }
}
public override SqlString GetLimitString(SqlString queryString, SqlString offset, SqlString limit)
{
SqlStringBuilder pagingBuilder = new SqlStringBuilder();
pagingBuilder.Add(queryString);
pagingBuilder.Add(" limit ");
if (limit != null)
pagingBuilder.Add(limit);
else
// We must have a limit present if we have an offset.
pagingBuilder.Add(int.MaxValue.ToString());
if (offset != null)
{
pagingBuilder.Add(" offset ");
pagingBuilder.Add(offset);
}
return pagingBuilder.ToSqlString();
}
public override bool SupportsTemporaryTables
{
get { return true; }
}
public override string CreateTemporaryTableString
{
get { return "create temp table"; }
}
public override bool DropTemporaryTableAfterUse()
{
return true;
}
public override string SelectGUIDString
{
get { return "select randomblob(16)"; }
}
/// <summary>
/// SQLite does not currently support dropping foreign key constraints by alter statements.
/// This means that tables cannot be dropped if there are any rows that depend on those.
/// If there are cycles between tables, it would even be excessively difficult to delete
/// the data in the right order first. Because of this, we just turn off the foreign
/// constraints before we drop the schema and hope that we're not going to break anything. :(
/// We could theoretically check for data consistency afterwards, but we don't currently.
/// </summary>
public override string DisableForeignKeyConstraintsString
{
get { return "PRAGMA foreign_keys = OFF"; }
}
public override string EnableForeignKeyConstraintsString
{
get { return "PRAGMA foreign_keys = ON"; }
}
public override bool SupportsForeignKeyConstraintInAlterTable
{
get { return false; }
}
/// <summary>
/// Does this dialect support concurrent writing connections?
/// </summary>
/// <remarks>
/// As documented at https://www.sqlite.org/faq.html#q5
/// </remarks>
public override bool SupportsConcurrentWritingConnections => false;
/// <summary>
/// Does this dialect supports distributed transaction? <c>false</c>.
/// </summary>
/// <remarks>
/// SQLite does not have a two phases commit and as such does not respect distributed transaction semantic.
/// But moreover, it fails handling the threading involved with distributed transactions (see
/// https://system.data.sqlite.org/index.html/tktview/5cee5409f84da5f62172 ).
/// It has moreover some flakyness in tests due to seemingly highly delayed (> 500ms) commits when distributed.
/// </remarks>
public override bool SupportsDistributedTransactions => false;
// Said to be unlimited. http://sqlite.1065341.n5.nabble.com/Max-limits-on-the-following-td37859.html
/// <inheritdoc />
public override int MaxAliasLength => 128;
// Since v5.3
[Obsolete("This class has no usage in NHibernate anymore and will be removed in a future version. Use or extend CastFunction instead.")]
[Serializable]
protected class SQLiteCastFunction : CastFunction
{
// Since v5.3
[Obsolete("This method has no usages and will be removed in a future version")]
protected override bool CastingIsRequired(string sqlType)
{
if (StringHelper.ContainsCaseInsensitive(sqlType, "date") ||
StringHelper.ContainsCaseInsensitive(sqlType, "time"))
return false;
return true;
}
}
[Serializable]
private class LocateFunction : ISQLFunction, ISQLFunctionExtended
{
// Since v5.3
[Obsolete("Use GetReturnType method instead.")]
public IType ReturnType(IType columnType, IMapping mapping)
{
return NHibernateUtil.Int32;
}
/// <inheritdoc />
public IType GetReturnType(IEnumerable<IType> argumentTypes, IMapping mapping, bool throwOnError)
{
#pragma warning disable 618
return ReturnType(argumentTypes.FirstOrDefault(), mapping);
#pragma warning restore 618
}
/// <inheritdoc />
public IType GetEffectiveReturnType(IEnumerable<IType> argumentTypes, IMapping mapping, bool throwOnError)
{
return GetReturnType(argumentTypes, mapping, throwOnError);
}
/// <inheritdoc />
public string Name => "instr";
public bool HasArguments => true;
public bool HasParenthesesIfNoArguments => true;
public SqlString Render(IList args, ISessionFactoryImplementor factory)
{
if (args.Count != 2 && args.Count != 3)
{
throw new QueryException("'locate' function takes 2 or 3 arguments. Provided count: " + args.Count);
}
if (args.Count == 2)
{
return new SqlString("instr(", args[1], ", ", args[0], ")");
}
var text = args[1];
var value = args[0];
var startIndex = args[2];
//ifnull(
// nullif(
// instr(substr(text, startIndex), value)
// , 0)
// + startIndex -1
//, 0)
return
new SqlString(
"ifnull(nullif(instr(substr(",
text,
", ",
startIndex,
"), ",
value,
"), 0) + ",
startIndex,
" -1, 0)"
);
}
}
}
}