-
Notifications
You must be signed in to change notification settings - Fork 4
/
SQLiteDatabase.cs
504 lines (464 loc) · 16 KB
/
SQLiteDatabase.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
using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using System.Reflection;
using System.Data;
using System.Data.Common;
using Mono.Data.Sqlite;
using System.IO;
using Newtonsoft.Json.Linq;
namespace AccountServer {
/// <summary>
/// Interface to SQLite
/// </summary>
public class SQLiteDatabase : DbInterface {
static object _lock = new object();
SqliteConnection _conn;
SqliteTransaction _tran;
static SQLiteDatabase() {
SqliteDateDiff.RegisterFunction(typeof(SqliteDateDiff));
SqliteSum.RegisterFunction(typeof(SqliteSum));
}
public SQLiteDatabase(string connectionString) {
createDatabase(connectionString);
_conn = new SqliteConnection();
_conn.ConnectionString = connectionString;
_conn.Open();
}
public void BeginTransaction() {
lock (_lock) {
if (_tran == null)
_tran = _conn.BeginTransaction();
}
}
/// <summary>
/// Return SQL to cast a value to a type
/// </summary>
public string Cast(string value, string type) {
return value;
}
public void CleanDatabase() {
foreach (string table in Database.TableNames) {
Table t = Database.TableFor(table);
if(t.PrimaryKey.AutoIncrement)
Execute(string.Format("UPDATE sqlite_sequence SET seq = (SELECT MAX({1}) FROM {0}) WHERE name='{0}'",
table, t.PrimaryKey.Name));
}
Execute("VACUUM");
}
public void CreateTable(Table t) {
View v = t as View;
if (v != null) {
executeLog(string.Format("CREATE VIEW `{0}` AS {1}", v.Name, v.Sql));
return;
}
createTable(t, t.Name);
createIndexes(t);
}
public void CreateIndex(Table t, Index index) {
executeLog(string.Format("ALTER TABLE `{0}` ADD CONSTRAINT `{1}` UNIQUE ({2})", t.Name, index.Name,
string.Join(",", index.Fields.Select(f => "`" + f.Name + "` ASC").ToArray())));
}
public void Commit() {
if (_tran != null) {
lock (_lock) {
_tran.Commit();
_tran.Dispose();
_tran = null;
}
}
}
public void Dispose() {
Rollback();
if (_conn != null) {
_conn.Dispose();
_conn = null;
}
}
public void DropTable(Table t) {
executeLogSafe("DROP TABLE IF EXISTS " + t.Name);
executeLogSafe("DROP VIEW IF EXISTS " + t.Name);
}
public void DropIndex(Table t, Index index) {
executeLogSafe(string.Format("ALTER TABLE `{0}` DROP INDEX `{1}`", t.Name, index.Name));
}
int Execute(string sql) {
int lastInserttId;
return Execute(sql, out lastInserttId);
}
public int Execute(string sql, out int lastInsertId) {
lock (_lock) {
using (SqliteCommand cmd = command(sql)) {
var ret = cmd.ExecuteNonQuery();
cmd.CommandText = "select last_insert_rowid()";
lastInsertId = (int)(Int64)cmd.ExecuteScalar();
return ret;
}
}
}
public bool FieldsMatch(Table t, Field code, Field database) {
if (code.TypeName != database.TypeName) return false;
if (t.IsView) return true; // Database does not always give correct values for view columns
if (code.AutoIncrement != database.AutoIncrement) return false;
if (code.Length != database.Length) return false;
if (code.Nullable != database.Nullable) return false;
if (code.DefaultValue != database.DefaultValue) return false;
return true;
}
public IEnumerable<JObject> Query(string query) {
lock (_lock) {
using (SqliteCommand cmd = command(query)) {
using (SqliteDataReader r = executeReader(cmd, query)) {
JObject row;
while ((row = readRow(r, query)) != null) {
yield return row;
}
}
}
}
}
public JObject QueryOne(string query) {
return Query(query + " LIMIT 1").FirstOrDefault();
}
static public string Quote(object o) {
if (o == null || o == DBNull.Value) return "NULL";
if (o is int || o is long || o is double) return o.ToString();
if (o is decimal) return ((decimal)o).ToString("0.00");
if (o is double) return (Math.Round((decimal)o, 4)).ToString();
if (o is double) return ((decimal)o).ToString("0");
if (o is bool) return (bool)o ? "1" : "0";
if (o is DateTime) return "'" + ((DateTime)o).ToString("yyyy-MM-dd") + "'";
return "'" + o.ToString().Replace("'", "''") + "'";
}
public void Rollback() {
if (_tran != null) {
lock (_lock) {
_tran.Rollback();
_tran.Dispose();
_tran = null;
}
}
}
public Dictionary<string, Table> Tables() {
Dictionary<string, Table> tables = new Dictionary<string, Table>();
createDatabase(AppSettings.Default.ConnectionString);
using (SqliteConnection conn = new SqliteConnection(AppSettings.Default.ConnectionString)) {
conn.Open();
DataTable tabs = conn.GetSchema("Tables");
DataTable cols = conn.GetSchema("Columns");
DataTable fkeyCols = conn.GetSchema("ForeignKeys");
DataTable indexes = conn.GetSchema("Indexes");
DataTable indexCols = conn.GetSchema("IndexColumns");
DataTable views = conn.GetSchema("Views");
DataTable viewCols = conn.GetSchema("ViewColumns");
foreach(DataRow table in tabs.Rows) {
string name = table["TABLE_NAME"].ToString();
string filter = "TABLE_NAME = " + Quote(name);
Field[] fields = cols.Select(filter, "ORDINAL_POSITION")
.Select(c => new Field(c["COLUMN_NAME"].ToString(), typeFor(c["DATA_TYPE"].ToString()),
lengthFromColumn(c), c["IS_NULLABLE"].ToString() == "True", c["AUTOINCREMENT"].ToString() == "True",
defaultFromColumn(c))).ToArray();
List<Index> tableIndexes = new List<Index>();
foreach (DataRow ind in indexes.Select(filter + " AND PRIMARY_KEY = 'True'")) {
string indexName = ind["INDEX_NAME"].ToString();
tableIndexes.Add(new Index("PRIMARY",
indexCols.Select(filter + " AND INDEX_NAME = " + Quote(indexName), "ORDINAL_POSITION")
.Select(r => fields.First(f => f.Name == r["COLUMN_NAME"].ToString())).ToArray()));
}
foreach (DataRow ind in indexes.Select(filter + " AND PRIMARY_KEY = 'False' AND UNIQUE = 'True'")) {
string indexName = ind["INDEX_NAME"].ToString();
tableIndexes.Add(new Index(indexName,
indexCols.Select(filter + " AND INDEX_NAME = " + Quote(indexName), "ORDINAL_POSITION")
.Select(r => fields.First(f => f.Name == r["COLUMN_NAME"].ToString())).ToArray()));
}
tables[name] = new Table(name, fields, tableIndexes.ToArray());
}
foreach (DataRow fk in fkeyCols.Rows) {
Table detail = tables[fk["TABLE_NAME"].ToString()];
Table master = tables[fk["FKEY_TO_TABLE"].ToString()];
Field masterField = master.FieldFor(fk["FKEY_TO_COLUMN"].ToString());
detail.FieldFor(fk["FKEY_FROM_COLUMN"].ToString()).ForeignKey = new ForeignKey(master, masterField);
}
foreach (DataRow table in views.Select()) {
string name = table["TABLE_NAME"].ToString();
string filter = "VIEW_NAME = " + Quote(name);
Field[] fields = viewCols.Select(filter, "ORDINAL_POSITION")
.Select(c => new Field(c["VIEW_COLUMN_NAME"].ToString(), typeFor(c["DATA_TYPE"].ToString()),
lengthFromColumn(c), c["IS_NULLABLE"].ToString() == "True", false,
defaultFromColumn(c))).ToArray();
Table updateTable = null;
tables.TryGetValue(Regex.Replace(name, "^.*_", ""), out updateTable);
tables[name] = new View(name, fields, new Index[] { new Index("PRIMARY", fields[0]) },
table["VIEW_DEFINITION"].ToString(), updateTable);
}
}
return tables;
}
public void UpgradeTable(Table code, Table database, List<Field> insert, List<Field> update, List<Field> remove,
List<Field> insertFK, List<Field> dropFK, List<Index> insertIndex, List<Index> dropIndex) {
for (int i = dropIndex.Count; i-- > 0; ) {
Index ind = dropIndex[i];
if ((ind.Fields.Length == 1 && ind.Fields[0].Name == code.PrimaryKey.Name) || ind.Name.StartsWith("sqlite_autoindex_"))
dropIndex.RemoveAt(i);
}
if (update.Count > 0 || remove.Count > 0 || insertFK.Count > 0 || dropFK.Count > 0 || insertIndex.Count > 0 || dropIndex.Count > 0) {
reCreateTable(code, database);
return;
}
if (insert.Count != 0) {
foreach(string def in insert.Select(f => "ADD COLUMN " + fieldDef(f))) {
executeLog(string.Format("ALTER TABLE `{0}` {1}", code.Name, def));
}
}
}
public bool? ViewsMatch(View code, View database) {
string c = Regex.Replace(code.Sql, @"[ \r\n\t]+", " ", RegexOptions.Singleline).Trim();
string d = Regex.Replace(database.Sql, @"[ \r\n\t]+", " ", RegexOptions.Singleline).Trim();
return c == d;
}
SqliteCommand command(string sql) {
try {
return new SqliteCommand(sql, _conn, _tran);
} catch (Exception ex) {
throw new DatabaseException(ex, sql);
}
}
static void createDatabase(string connectionString) {
Match m = Regex.Match(connectionString, @"Data Source=([^;]+)", RegexOptions.IgnoreCase);
if (m.Success && !File.Exists(m.Groups[1].Value)) {
WebServer.Log("Creating SQLite database {0}", m.Groups[1].Value);
Directory.CreateDirectory(Path.GetDirectoryName(m.Groups[1].Value));
SqliteConnection.CreateFile(m.Groups[1].Value);
}
}
void createTable(Table t, string name) {
List<string> defs = new List<string>(t.Fields.Select(f => fieldDef(f)));
for (int i = 0; i < t.Indexes.Length; i++) {
Index index = t.Indexes[i];
if (i == 0) {
if (index.Fields.Length != 1 || !index.Fields[0].AutoIncrement)
defs.Add(string.Format("CONSTRAINT `PRIMARY` PRIMARY KEY ({0})", string.Join(",", index.Fields
.Select(f => "`" + f.Name + "`").ToArray())));
} else
defs.Add(string.Format("CONSTRAINT `{0}` UNIQUE ({1})", index.Name,
string.Join(",", index.Fields.Select(f => "`" + f.Name + "` ASC").ToArray())));
}
defs.AddRange(t.Fields.Where(f => f.ForeignKey != null).Select(f => string.Format(@"CONSTRAINT `fk_{0}_{1}_{2}`
FOREIGN KEY (`{2}`)
REFERENCES `{1}` (`{3}`)
ON DELETE NO ACTION
ON UPDATE NO ACTION", t.Name, f.ForeignKey.Table.Name, f.Name, f.ForeignKey.Table.PrimaryKey.Name)));
executeLog(string.Format("CREATE TABLE `{0}` ({1})", name, string.Join(",\r\n", defs.ToArray())));
}
void createIndexes(Table t) {
foreach (string sql in t.Fields.Where(f => f.ForeignKey != null && t.Indexes.FirstOrDefault(i => i.Fields[0] == f) == null)
.Select(f => string.Format(@"CREATE INDEX `fk_{0}_{1}_{2}_idx` ON {0} (`{2}` ASC)",
t.Name, f.ForeignKey.Table.Name, f.Name)))
executeLog(sql);
}
static string defaultFromColumn(DataRow def) {
if (def.IsNull("COLUMN_DEFAULT"))
return null;
string r = def["COLUMN_DEFAULT"].ToString();
Match m = Regex.Match(r, @"^'(.*)'$");
return m.Success ? m.Groups[1].Value : r;
}
int executeLog(string sql) {
WebServer.Log(sql);
lock (_lock) {
using (SqliteCommand cmd = command(sql)) {
return cmd.ExecuteNonQuery();
}
}
}
int executeLogSafe(string sql) {
try {
return executeLog(sql);
} catch (Exception ex) {
WebServer.Log(ex.Message);
return -1;
}
}
SqliteDataReader executeReader(SqliteCommand cmd, string sql) {
try {
return cmd.ExecuteReader();
} catch (Exception ex) {
throw new DatabaseException(ex, sql);
}
}
string fieldDef(Field f) {
StringBuilder b = new StringBuilder();
b.AppendFormat("`{0}` ", f.Name);
switch (f.Type.Name) {
case "Int32":
b.Append("INTEGER");
break;
case "Decimal":
b.AppendFormat("DECIMAL({0})", f.Length.ToString("0.0").Replace(System.Globalization.CultureInfo.CurrentCulture.NumberFormat.NumberDecimalSeparator, ","));
break;
case "Double":
b.Append("DOUBLE");
break;
case "Boolean":
b.Append("BIT");
break;
case "DateTime":
b.Append("DATETIME");
break;
case "String":
if (f.Length == 0)
b.Append("TEXT");
else
b.AppendFormat("VARCHAR({0})", f.Length);
b.Append(" COLLATE NOCASE");
break;
default:
throw new CheckException("Unknown type {0}", f.Type.Name);
}
if (f.AutoIncrement)
b.Append(" PRIMARY KEY AUTOINCREMENT");
else {
b.AppendFormat(" {0}NULL", f.Nullable ? "" : "NOT ");
if (f.DefaultValue != null)
b.AppendFormat(" DEFAULT {0}", Quote(f.DefaultValue));
}
return b.ToString();
}
decimal lengthFromColumn(DataRow c) {
try {
switch (c["DATA_TYPE"].ToString().ToLower()) {
case "int":
case "integer":
return 11;
case "tinyint":
case "bit":
return 1;
case "decimal":
string s = c["NUMERIC_PRECISION"] + System.Globalization.CultureInfo.CurrentCulture.NumberFormat.NumberDecimalSeparator + c["NUMERIC_SCALE"];
return s == "." ? 10.2M : decimal.Parse(s);
case "double":
case "float":
return 10.4M;
case "varchar":
return Convert.ToDecimal(c["CHARACTER_MAXIMUM_LENGTH"]);
default:
return 0;
}
} catch (Exception ex) {
WebServer.Log(ex.ToString());
return 0;
}
}
JObject readRow(SqliteDataReader r, string sql) {
try {
lock (_lock) {
if (!r.Read()) return null;
}
JObject row = new JObject();
for (int i = 0; i < r.FieldCount; i++) {
row.Add(Regex.Replace(r.GetName(i), @"^.*\.", ""), r[i].ToJToken());
}
return row;
} catch (Exception ex) {
throw new DatabaseException(ex, sql);
}
}
void reCreateTable(Table code, Table database) {
string newTable = "_NEW_" + code.Name;
try {
executeLogSafe("PRAGMA foreign_keys=OFF");
executeLog("BEGIN TRANSACTION");
createTable(code, newTable);
executeLog(string.Format("INSERT INTO {0} ({2}) SELECT {2} FROM {1}", newTable, database.Name,
string.Join(", ", code.Fields.Select(f => f.Name)
.Where(f => database.Fields.FirstOrDefault(d => d.Name == f) != null).ToArray())));
DropTable(database);
executeLog("ALTER TABLE " + newTable + " RENAME TO " + code.Name);
createIndexes(code);
executeLog("PRAGMA foreign_key_check");
executeLog("COMMIT TRANSACTION");
} catch (Exception ex) {
WebServer.Log("Exception: {0}", ex);
executeLogSafe("ROLLBACK TRANSACTION");
throw;
} finally {
executeLogSafe("PRAGMA foreign_keys=ON");
}
}
static Type typeFor(string s) {
switch (s.ToLower()) {
case "int":
case "integer":
return typeof(int);
case "tinyint":
case "bit":
return typeof(bool);
case "decimal":
return typeof(decimal);
case "double":
case "float":
return typeof(double);
case "datetime":
case "date":
return typeof(DateTime);
case "varchar":
case "text":
default:
return typeof(string);
}
}
}
/// <summary>
/// DATEDIFF function (like MySql's)
/// </summary>
[SqliteFunctionAttribute(Name = "DATEDIFF", Arguments = 2, FuncType = FunctionType.Scalar)]
class SqliteDateDiff : SqliteFunction {
public override object Invoke(object[] args) {
if (args.Length < 2 || args[0] == null || args[0] == DBNull.Value || args[1] == null || args[1] == DBNull.Value)
return null;
try {
DateTime d1 = DateTime.Parse(args[0].ToString());
DateTime d2 = DateTime.Parse(args[1].ToString());
return (d1 - d2).TotalDays;
} catch (Exception ex) {
WebServer.Log("Exception: {0}", ex);
return null;
}
}
}
[SqliteFunctionAttribute(Name = "NOW", Arguments = 0, FuncType = FunctionType.Scalar)]
class Now : SqliteFunction {
public override object Invoke(object[] args) {
try {
return Utils.Now.ToString("yyyy-MM-ddThh:mm:ss");
} catch (Exception ex) {
WebServer.Log("Exception: {0}", ex);
return null;
}
}
}
/// <summary>
/// SUM function which rounds as it sums, so it works like MySql's
/// </summary>
[SqliteFunctionAttribute(Name = "SUM", Arguments = 1, FuncType = FunctionType.Aggregate)]
class SqliteSum : SqliteFunction {
public override void Step(object[] args, int stepNumber, ref object contextData) {
if (args.Length < 1 || args[0] == null || args[0] == DBNull.Value)
return;
try {
decimal d = Math.Round(Convert.ToDecimal(args[0]), 4);
if (contextData != null) d += (Decimal)contextData;
contextData = d;
} catch (Exception ex) {
WebServer.Log("Exception: {0}", ex);
}
}
public override object Final(object contextData) {
return contextData;
}
}
}