forked from jstedfast/MimeKit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSQLiteKeyReader.cs
552 lines (489 loc) · 18.1 KB
/
SQLiteKeyReader.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
/********************************************************
* ADO.NET 2.0 Data Provider for SQLite Version 3.X
* Written by Robert Simpson ([email protected])
*
* Released to the public domain, use at your own risk!
********************************************************/
namespace Mono.Data.Sqlite
{
using System;
using System.Data;
using System.Data.Common;
using System.Collections.Generic;
/// <summary>
/// This class provides key info for a given SQLite statement.
/// <remarks>
/// Providing key information for a given statement is non-trivial :(
/// </remarks>
/// </summary>
internal sealed class SqliteKeyReader : IDisposable
{
private KeyInfo[] _keyInfo;
private SqliteStatement _stmt;
private bool _isValid;
/// <summary>
/// Used to support CommandBehavior.KeyInfo
/// </summary>
private struct KeyInfo
{
internal string databaseName;
internal string tableName;
internal string columnName;
internal int database;
internal int rootPage;
internal int cursor;
internal KeyQuery query;
internal int column;
}
/// <summary>
/// A single sub-query for a given table/database.
/// </summary>
private sealed class KeyQuery : IDisposable
{
private SqliteCommand _command;
internal SqliteDataReader _reader;
internal KeyQuery(SqliteConnection cnn, string database, string table, params string[] columns)
{
using (SqliteCommandBuilder builder = new SqliteCommandBuilder())
{
_command = cnn.CreateCommand();
for (int n = 0; n < columns.Length; n++)
{
columns[n] = builder.QuoteIdentifier(columns[n]);
}
}
_command.CommandText = String.Format("SELECT {0} FROM [{1}].[{2}] WHERE ROWID = ?", String.Join(",", columns), database, table);
_command.Parameters.AddWithValue(null, (long)0);
}
internal bool IsValid
{
get { return (_reader != null); }
set
{
if (value != false) throw new ArgumentException();
if (_reader != null)
{
_reader.Dispose();
_reader = null;
}
}
}
internal void Sync(long rowid)
{
IsValid = false;
_command.Parameters[0].Value = rowid;
_reader = _command.ExecuteReader();
_reader.Read();
}
public void Dispose()
{
IsValid = false;
if (_command != null) _command.Dispose();
_command = null;
}
}
/// <summary>
/// This function does all the nasty work at determining what keys need to be returned for
/// a given statement.
/// </summary>
/// <param name="cnn"></param>
/// <param name="reader"></param>
/// <param name="stmt"></param>
internal SqliteKeyReader(SqliteConnection cnn, SqliteDataReader reader, SqliteStatement stmt)
{
Dictionary<string, int> catalogs = new Dictionary<string, int>();
Dictionary<string, List<string>> tables = new Dictionary<string, List<string>>();
List<string> list;
List<KeyInfo> keys = new List<KeyInfo>();
// Record the statement so we can use it later for sync'ing
_stmt = stmt;
// Fetch all the attached databases on this connection
using (DataTable tbl = cnn.GetSchema("Catalogs"))
{
foreach (DataRow row in tbl.Rows)
{
catalogs.Add((string)row["CATALOG_NAME"], Convert.ToInt32(row["ID"]));
}
}
// Fetch all the unique tables and catalogs used by the current statement
using (DataTable schema = reader.GetSchemaTable(false, false))
{
foreach (DataRow row in schema.Rows)
{
// Check if column is backed to a table
if (row[SchemaTableOptionalColumn.BaseCatalogName] == DBNull.Value)
continue;
// Record the unique table so we can look up its keys
string catalog = (string)row[SchemaTableOptionalColumn.BaseCatalogName];
string table = (string)row[SchemaTableColumn.BaseTableName];
if (tables.ContainsKey(catalog) == false)
{
list = new List<string>();
tables.Add(catalog, list);
}
else
list = tables[catalog];
if (list.Contains(table) == false)
list.Add(table);
}
// For each catalog and each table, query the indexes for the table.
// Find a primary key index if there is one. If not, find a unique index instead
foreach (KeyValuePair<string, List<string>> pair in tables)
{
for (int i = 0; i < pair.Value.Count; i++)
{
string table = pair.Value[i];
DataRow preferredRow = null;
using (DataTable tbl = cnn.GetSchema("Indexes", new string[] { pair.Key, null, table }))
{
// Loop twice. The first time looking for a primary key index,
// the second time looking for a unique index
for (int n = 0; n < 2 && preferredRow == null; n++)
{
foreach (DataRow row in tbl.Rows)
{
if (n == 0 && (bool)row["PRIMARY_KEY"] == true)
{
preferredRow = row;
break;
}
else if (n == 1 && (bool)row["UNIQUE"] == true)
{
preferredRow = row;
break;
}
}
}
if (preferredRow == null) // Unable to find any suitable index for this table so remove it
{
pair.Value.RemoveAt(i);
i--;
}
else // We found a usable index, so fetch the necessary table details
{
using (DataTable tblTables = cnn.GetSchema("Tables", new string[] { pair.Key, null, table }))
{
// Find the root page of the table in the current statement and get the cursor that's iterating it
int database = catalogs[pair.Key];
int rootPage = Convert.ToInt32(tblTables.Rows[0]["TABLE_ROOTPAGE"]);
int cursor = stmt._sql.GetCursorForTable(stmt, database, rootPage);
// Now enumerate the members of the index we're going to use
using (DataTable indexColumns = cnn.GetSchema("IndexColumns", new string[] { pair.Key, null, table, (string)preferredRow["INDEX_NAME"] }))
{
KeyQuery query = null;
List<string> cols = new List<string>();
for (int x = 0; x < indexColumns.Rows.Count; x++)
{
bool addKey = true;
// If the column in the index already appears in the query, skip it
foreach (DataRow row in schema.Rows)
{
if (row.IsNull(SchemaTableColumn.BaseColumnName))
continue;
if ((string)row[SchemaTableColumn.BaseColumnName] == (string)indexColumns.Rows[x]["COLUMN_NAME"] &&
(string)row[SchemaTableColumn.BaseTableName] == table &&
(string)row[SchemaTableOptionalColumn.BaseCatalogName] == pair.Key)
{
indexColumns.Rows.RemoveAt(x);
x--;
addKey = false;
break;
}
}
if (addKey == true)
cols.Add((string)indexColumns.Rows[x]["COLUMN_NAME"]);
}
// If the index is not a rowid alias, record all the columns
// needed to make up the unique index and construct a SQL query for it
if ((string)preferredRow["INDEX_NAME"] != "sqlite_master_PK_" + table)
{
// Whatever remains of the columns we need that make up the index that are not
// already in the query need to be queried separately, so construct a subquery
if (cols.Count > 0)
{
string[] querycols = new string[cols.Count];
cols.CopyTo(querycols);
query = new KeyQuery(cnn, pair.Key, table, querycols);
}
}
// Create a KeyInfo struct for each column of the index
for (int x = 0; x < indexColumns.Rows.Count; x++)
{
string columnName = (string)indexColumns.Rows[x]["COLUMN_NAME"];
KeyInfo key = new KeyInfo();
key.rootPage = rootPage;
key.cursor = cursor;
key.database = database;
key.databaseName = pair.Key;
key.tableName = table;
key.columnName = columnName;
key.query = query;
key.column = x;
keys.Add(key);
}
}
}
}
}
}
}
}
// Now we have all the additional columns we have to return in order to support
// CommandBehavior.KeyInfo
_keyInfo = new KeyInfo[keys.Count];
keys.CopyTo(_keyInfo);
}
/// <summary>
/// How many additional columns of keyinfo we're holding
/// </summary>
internal int Count
{
get { return (_keyInfo == null) ? 0 : _keyInfo.Length; }
}
internal void Sync(int i)
{
Sync();
if (_keyInfo[i].cursor == -1)
throw new InvalidCastException();
}
/// <summary>
/// Make sure all the subqueries are open and ready and sync'd with the current rowid
/// of the table they're supporting
/// </summary>
internal void Sync()
{
if (_isValid == true) return;
KeyQuery last = null;
for (int n = 0; n < _keyInfo.Length; n++)
{
if (_keyInfo[n].query == null || _keyInfo[n].query != last)
{
last = _keyInfo[n].query;
if (last != null)
{
last.Sync(_stmt._sql.GetRowIdForCursor(_stmt, _keyInfo[n].cursor));
}
}
}
_isValid = true;
}
/// <summary>
/// Release any readers on any subqueries
/// </summary>
internal void Reset()
{
_isValid = false;
if (_keyInfo == null) return;
for (int n = 0; n < _keyInfo.Length; n++)
{
if (_keyInfo[n].query != null)
_keyInfo[n].query.IsValid = false;
}
}
public void Dispose()
{
_stmt = null;
if (_keyInfo == null) return;
for (int n = 0; n < _keyInfo.Length; n++)
{
if (_keyInfo[n].query != null)
_keyInfo[n].query.Dispose();
}
_keyInfo = null;
}
internal string GetDataTypeName(int i)
{
Sync();
if (_keyInfo[i].query != null) return _keyInfo[i].query._reader.GetDataTypeName(_keyInfo[i].column);
else return "integer";
}
internal Type GetFieldType(int i)
{
Sync();
if (_keyInfo[i].query != null) return _keyInfo[i].query._reader.GetFieldType(_keyInfo[i].column);
else return typeof(Int64);
}
internal string GetName(int i)
{
return _keyInfo[i].columnName;
}
internal int GetOrdinal(string name)
{
for (int n = 0; n < _keyInfo.Length; n++)
{
if (String.Compare(name, _keyInfo[n].columnName, StringComparison.OrdinalIgnoreCase) == 0) return n;
}
return -1;
}
internal bool GetBoolean(int i)
{
Sync(i);
if (_keyInfo[i].query != null) return _keyInfo[i].query._reader.GetBoolean(_keyInfo[i].column);
else throw new InvalidCastException();
}
internal byte GetByte(int i)
{
Sync(i);
if (_keyInfo[i].query != null) return _keyInfo[i].query._reader.GetByte(_keyInfo[i].column);
else throw new InvalidCastException();
}
internal long GetBytes(int i, long fieldOffset, byte[] buffer, int bufferoffset, int length)
{
Sync(i);
if (_keyInfo[i].query != null) return _keyInfo[i].query._reader.GetBytes(_keyInfo[i].column, fieldOffset, buffer, bufferoffset, length);
else throw new InvalidCastException();
}
internal char GetChar(int i)
{
Sync(i);
if (_keyInfo[i].query != null) return _keyInfo[i].query._reader.GetChar(_keyInfo[i].column);
else throw new InvalidCastException();
}
internal long GetChars(int i, long fieldOffset, char[] buffer, int bufferoffset, int length)
{
Sync(i);
if (_keyInfo[i].query != null) return _keyInfo[i].query._reader.GetChars(_keyInfo[i].column, fieldOffset, buffer, bufferoffset, length);
else throw new InvalidCastException();
}
internal DateTime GetDateTime(int i)
{
Sync(i);
if (_keyInfo[i].query != null) return _keyInfo[i].query._reader.GetDateTime(_keyInfo[i].column);
else throw new InvalidCastException();
}
internal decimal GetDecimal(int i)
{
Sync(i);
if (_keyInfo[i].query != null) return _keyInfo[i].query._reader.GetDecimal(_keyInfo[i].column);
else throw new InvalidCastException();
}
internal double GetDouble(int i)
{
Sync(i);
if (_keyInfo[i].query != null) return _keyInfo[i].query._reader.GetDouble(_keyInfo[i].column);
else throw new InvalidCastException();
}
internal float GetFloat(int i)
{
Sync(i);
if (_keyInfo[i].query != null) return _keyInfo[i].query._reader.GetFloat(_keyInfo[i].column);
else throw new InvalidCastException();
}
internal Guid GetGuid(int i)
{
Sync(i);
if (_keyInfo[i].query != null) return _keyInfo[i].query._reader.GetGuid(_keyInfo[i].column);
else throw new InvalidCastException();
}
internal Int16 GetInt16(int i)
{
Sync(i);
if (_keyInfo[i].query != null) return _keyInfo[i].query._reader.GetInt16(_keyInfo[i].column);
else
{
long rowid = _stmt._sql.GetRowIdForCursor(_stmt, _keyInfo[i].cursor);
if (rowid == 0) throw new InvalidCastException();
return Convert.ToInt16(rowid);
}
}
internal Int32 GetInt32(int i)
{
Sync(i);
if (_keyInfo[i].query != null) return _keyInfo[i].query._reader.GetInt32(_keyInfo[i].column);
else
{
long rowid = _stmt._sql.GetRowIdForCursor(_stmt, _keyInfo[i].cursor);
if (rowid == 0) throw new InvalidCastException();
return Convert.ToInt32(rowid);
}
}
internal Int64 GetInt64(int i)
{
Sync(i);
if (_keyInfo[i].query != null) return _keyInfo[i].query._reader.GetInt64(_keyInfo[i].column);
else
{
long rowid = _stmt._sql.GetRowIdForCursor(_stmt, _keyInfo[i].cursor);
if (rowid == 0) throw new InvalidCastException();
return Convert.ToInt64(rowid);
}
}
internal string GetString(int i)
{
Sync(i);
if (_keyInfo[i].query != null) return _keyInfo[i].query._reader.GetString(_keyInfo[i].column);
else throw new InvalidCastException();
}
internal object GetValue(int i)
{
if (_keyInfo[i].cursor == -1) return DBNull.Value;
Sync(i);
if (_keyInfo[i].query != null) return _keyInfo[i].query._reader.GetValue(_keyInfo[i].column);
if (IsDBNull(i) == true)
return DBNull.Value;
else return GetInt64(i);
}
internal bool IsDBNull(int i)
{
if (_keyInfo[i].cursor == -1) return true;
Sync(i);
if (_keyInfo[i].query != null) return _keyInfo[i].query._reader.IsDBNull(_keyInfo[i].column);
else return _stmt._sql.GetRowIdForCursor(_stmt, _keyInfo[i].cursor) == 0;
}
/// <summary>
/// Append all the columns we've added to the original query to the schema
/// </summary>
/// <param name="tbl"></param>
internal void AppendSchemaTable(DataTable tbl)
{
KeyQuery last = null;
for (int n = 0; n < _keyInfo.Length; n++)
{
if (_keyInfo[n].query == null || _keyInfo[n].query != last)
{
last = _keyInfo[n].query;
if (last == null) // ROWID aliases are treated special
{
DataRow row = tbl.NewRow();
row[SchemaTableColumn.ColumnName] = _keyInfo[n].columnName;
row[SchemaTableColumn.ColumnOrdinal] = tbl.Rows.Count;
row[SchemaTableColumn.ColumnSize] = 8;
row[SchemaTableColumn.NumericPrecision] = 255;
row[SchemaTableColumn.NumericScale] = 255;
row[SchemaTableColumn.ProviderType] = DbType.Int64;
row[SchemaTableColumn.IsLong] = false;
row[SchemaTableColumn.AllowDBNull] = false;
row[SchemaTableOptionalColumn.IsReadOnly] = false;
row[SchemaTableOptionalColumn.IsRowVersion] = false;
row[SchemaTableColumn.IsUnique] = false;
row[SchemaTableColumn.IsKey] = true;
row[SchemaTableColumn.DataType] = typeof(Int64);
row[SchemaTableOptionalColumn.IsHidden] = true;
row[SchemaTableColumn.BaseColumnName] = _keyInfo[n].columnName;
row[SchemaTableColumn.IsExpression] = false;
row[SchemaTableColumn.IsAliased] = false;
row[SchemaTableColumn.BaseTableName] = _keyInfo[n].tableName;
row[SchemaTableOptionalColumn.BaseCatalogName] = _keyInfo[n].databaseName;
row[SchemaTableOptionalColumn.IsAutoIncrement] = true;
row["DataTypeName"] = "integer";
tbl.Rows.Add(row);
}
else
{
last.Sync(0);
using (DataTable tblSub = last._reader.GetSchemaTable())
{
foreach (DataRow row in tblSub.Rows)
{
object[] o = row.ItemArray;
DataRow newrow = tbl.Rows.Add(o);
newrow[SchemaTableOptionalColumn.IsHidden] = true;
newrow[SchemaTableColumn.ColumnOrdinal] = tbl.Rows.Count - 1;
}
}
}
}
}
}
}
}