-
Notifications
You must be signed in to change notification settings - Fork 3.2k
/
SqliteDataRecord.cs
514 lines (423 loc) · 16.5 KB
/
SqliteDataRecord.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
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
using System.IO;
using System.Linq;
using System.Text;
using Microsoft.Data.Sqlite.Properties;
using SQLitePCL;
using static SQLitePCL.raw;
namespace Microsoft.Data.Sqlite
{
internal class SqliteDataRecord : SqliteValueReader, IDisposable
{
private readonly SqliteConnection _connection;
private readonly Action<int> _addChanges;
private byte[][]? _blobCache;
private int?[]? _typeCache;
private Dictionary<string, int>? _columnNameOrdinalCache;
private string[]? _columnNameCache;
private bool _stepped;
private int? _rowidOrdinal;
private bool _alreadyThrown;
private bool _alreadyAddedChanges;
public SqliteDataRecord(sqlite3_stmt stmt, bool hasRows, SqliteConnection connection, Action<int> addChanges)
{
Handle = stmt;
HasRows = hasRows;
_connection = connection;
_addChanges = addChanges;
}
public virtual object this[string name]
=> GetValue(GetOrdinal(name));
public virtual object this[int ordinal]
=> GetValue(ordinal);
public override int FieldCount
=> sqlite3_column_count(Handle);
public sqlite3_stmt Handle { get; }
public bool HasRows { get; }
public override bool IsDBNull(int ordinal)
=> !_stepped || sqlite3_data_count(Handle) == 0
? throw new InvalidOperationException(Resources.NoData)
: base.IsDBNull(ordinal);
public override object GetValue(int ordinal)
=> !_stepped || sqlite3_data_count(Handle) == 0
? throw new InvalidOperationException(Resources.NoData)
: base.GetValue(ordinal)!;
protected override double GetDoubleCore(int ordinal)
=> sqlite3_column_double(Handle, ordinal);
protected override long GetInt64Core(int ordinal)
=> sqlite3_column_int64(Handle, ordinal);
protected override string GetStringCore(int ordinal)
=> sqlite3_column_text(Handle, ordinal).utf8_to_string();
public override T GetFieldValue<T>(int ordinal)
{
if (typeof(T) == typeof(Stream))
{
return (T)(object)GetStream(ordinal);
}
if (typeof(T) == typeof(TextReader))
{
return (T)(object)GetTextReader(ordinal);
}
return base.GetFieldValue<T>(ordinal)!;
}
protected override byte[] GetBlob(int ordinal)
=> base.GetBlob(ordinal)!;
protected override byte[] GetBlobCore(int ordinal)
=> sqlite3_column_blob(Handle, ordinal).ToArray();
protected override int GetSqliteType(int ordinal)
{
var type = sqlite3_column_type(Handle, ordinal);
if (type == SQLITE_NULL
&& (ordinal < 0 || ordinal >= FieldCount))
{
// NB: Message is provided by the framework
throw new ArgumentOutOfRangeException(nameof(ordinal), ordinal, message: null);
}
return type;
}
protected override T GetNull<T>(int ordinal)
=> typeof(T) == typeof(DBNull) || typeof(T) == typeof(object)
? (T)(object)DBNull.Value
: throw new InvalidOperationException(GetOnNullErrorMsg(ordinal));
public virtual string GetName(int ordinal)
{
var name = _columnNameCache?[ordinal] ?? sqlite3_column_name(Handle, ordinal).utf8_to_string();
if (name == null
&& (ordinal < 0 || ordinal >= FieldCount))
{
// NB: Message is provided by the framework
throw new ArgumentOutOfRangeException(nameof(ordinal), ordinal, message: null);
}
_columnNameCache ??= new string[FieldCount];
_columnNameCache[ordinal] = name!;
return name!;
}
public virtual int GetOrdinal(string name)
{
if (_columnNameOrdinalCache == null)
{
_columnNameOrdinalCache = new Dictionary<string, int>();
for (var i = 0; i < FieldCount; i++)
{
_columnNameOrdinalCache[GetName(i)] = i;
}
}
if (_columnNameOrdinalCache.TryGetValue(name, out var ordinal))
{
return ordinal;
}
KeyValuePair<string, int>? match = null;
foreach (var item in _columnNameOrdinalCache)
{
if (string.Equals(name, item.Key, StringComparison.OrdinalIgnoreCase))
{
if (match != null)
{
throw new InvalidOperationException(
Resources.AmbiguousColumnName(name, match.Value.Key, item.Key));
}
match = item;
}
}
if (match != null)
{
_columnNameOrdinalCache.Add(name, match.Value.Value);
return match.Value.Value;
}
// NB: Message is provided by framework
throw new ArgumentOutOfRangeException(nameof(name), name, message: null);
}
public virtual string GetDataTypeName(int ordinal)
{
var typeName = sqlite3_column_decltype(Handle, ordinal).utf8_to_string();
if (typeName != null)
{
var i = typeName.IndexOf('(');
return i == -1
? typeName
: typeName.Substring(0, i);
}
var sqliteType = GetSqliteType(ordinal);
switch (sqliteType)
{
case SQLITE_INTEGER:
return "INTEGER";
case SQLITE_FLOAT:
return "REAL";
case SQLITE_TEXT:
return "TEXT";
default:
Debug.Assert(sqliteType is SQLITE_BLOB or SQLITE_NULL, "Unexpected column type: " + sqliteType);
return "BLOB";
}
}
#if NET6_0_OR_GREATER
[return: DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicProperties | DynamicallyAccessedMemberTypes.PublicFields)]
#endif
public virtual Type GetFieldType(int ordinal)
{
var sqliteType = GetSqliteType(ordinal);
if (sqliteType == SQLITE_NULL)
{
sqliteType = _typeCache?[ordinal] ?? Sqlite3AffinityType(GetDataTypeName(ordinal));
}
else
{
_typeCache ??= new int?[FieldCount];
_typeCache[ordinal] = sqliteType;
}
return GetFieldTypeFromSqliteType(sqliteType);
}
#if NET6_0_OR_GREATER
[return: DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicProperties | DynamicallyAccessedMemberTypes.PublicFields)]
#endif
internal static Type GetFieldTypeFromSqliteType(int sqliteType)
{
switch (sqliteType)
{
case SQLITE_INTEGER:
return typeof(long);
case SQLITE_FLOAT:
return typeof(double);
case SQLITE_TEXT:
return typeof(string);
default:
Debug.Assert(sqliteType is SQLITE_BLOB or SQLITE_NULL, "Unexpected column type: " + sqliteType);
return typeof(byte[]);
}
}
#if NET6_0_OR_GREATER
[return: DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicProperties | DynamicallyAccessedMemberTypes.PublicFields)]
#endif
public static Type GetFieldType(string type)
{
switch (type)
{
case "integer":
return typeof(long);
case "real":
return typeof(double);
case "text":
return typeof(string);
default:
Debug.Assert(type is "blob" or null, "Unexpected column type: " + type);
return typeof(byte[]);
}
}
public virtual long GetBytes(int ordinal, long dataOffset, byte[]? buffer, int bufferOffset, int length)
{
using var stream = GetStream(ordinal);
if (buffer == null)
{
return stream.Length;
}
stream.Position = dataOffset;
return stream.Read(buffer, bufferOffset, length);
}
public virtual long GetChars(int ordinal, long dataOffset, char[]? buffer, int bufferOffset, int length)
{
using var reader = new StreamReader(GetStream(ordinal), Encoding.UTF8);
if (buffer == null)
{
// TODO: Consider using a stackalloc buffer and reading blocks instead
var charCount = 0;
while (reader.Read() != -1)
{
charCount++;
}
return charCount;
}
for (var position = 0; position < dataOffset; position++)
{
if (reader.Read() == -1)
{
// NB: Message is provided by the framework
throw new ArgumentOutOfRangeException(nameof(dataOffset), dataOffset, message: null);
}
}
return reader.Read(buffer, bufferOffset, length);
}
public virtual Stream GetStream(int ordinal)
{
if (ordinal < 0
|| ordinal >= FieldCount)
{
throw new ArgumentOutOfRangeException(nameof(ordinal), ordinal, message: null);
}
var blobDatabaseName = sqlite3_column_database_name(Handle, ordinal).utf8_to_string();
var blobTableName = sqlite3_column_table_name(Handle, ordinal).utf8_to_string();
if (!_rowidOrdinal.HasValue)
{
_rowidOrdinal = -1;
var pkColumns = -1L;
for (var i = 0; i < FieldCount; i++)
{
if (i == ordinal)
{
continue;
}
var databaseName = sqlite3_column_database_name(Handle, i).utf8_to_string();
if (databaseName != blobDatabaseName)
{
continue;
}
var tableName = sqlite3_column_table_name(Handle, i).utf8_to_string();
if (tableName != blobTableName)
{
continue;
}
var columnName = sqlite3_column_origin_name(Handle, i).utf8_to_string();
if (columnName == "rowid")
{
_rowidOrdinal = i;
break;
}
var rc = sqlite3_table_column_metadata(
_connection.Handle,
databaseName,
tableName,
columnName,
out var dataType,
out var collSeq,
out var notNull,
out var primaryKey,
out var autoInc);
SqliteException.ThrowExceptionForRC(rc, _connection.Handle);
if (string.Equals(dataType, "INTEGER", StringComparison.OrdinalIgnoreCase)
&& primaryKey != 0)
{
if (pkColumns < 0L)
{
using (var command = _connection.CreateCommand())
{
command.CommandText = "SELECT COUNT(*) FROM pragma_table_info($table) WHERE pk != 0;";
command.Parameters.AddWithValue("$table", tableName);
pkColumns = (long)command.ExecuteScalar()!;
}
}
if (pkColumns == 1L)
{
_rowidOrdinal = i;
break;
}
}
}
Debug.Assert(_rowidOrdinal.HasValue);
}
if (_rowidOrdinal.Value < 0)
{
return new MemoryStream(GetCachedBlob(ordinal), false);
}
var blobColumnName = sqlite3_column_origin_name(Handle, ordinal).utf8_to_string();
var rowid = GetInt64(_rowidOrdinal.Value);
return new SqliteBlob(_connection, blobDatabaseName, blobTableName, blobColumnName, rowid, readOnly: true);
}
public virtual TextReader GetTextReader(int ordinal)
=> IsDBNull(ordinal)
? new StringReader(string.Empty)
: new StreamReader(GetStream(ordinal), Encoding.UTF8);
public bool Read()
{
if (!_stepped)
{
_stepped = true;
return HasRows;
}
if (sqlite3_data_count(Handle) == 0)
{
return false;
}
int rc;
try
{
rc = sqlite3_step(Handle);
SqliteException.ThrowExceptionForRC(rc, _connection.Handle);
}
catch
{
_alreadyThrown = true;
throw;
}
if (_blobCache != null)
{
Array.Clear(_blobCache, 0, _blobCache.Length);
}
if (rc != SQLITE_DONE)
{
return true;
}
AddChanges();
_alreadyAddedChanges = true;
return false;
}
public void Dispose()
{
var rc = sqlite3_reset(Handle);
if (!_alreadyThrown)
{
SqliteException.ThrowExceptionForRC(rc, _connection.Handle);
}
if (!_alreadyAddedChanges)
{
AddChanges();
}
}
private void AddChanges()
{
if (sqlite3_stmt_readonly(Handle) != 0)
{
return;
}
var changes = sqlite3_changes(_connection.Handle);
_addChanges(changes);
}
private byte[] GetCachedBlob(int ordinal)
{
if (ordinal < 0
|| ordinal >= FieldCount)
{
// NB: Message is provided by the framework
throw new ArgumentOutOfRangeException(nameof(ordinal), ordinal, message: null);
}
var blob = _blobCache?[ordinal];
if (blob == null)
{
blob = GetBlob(ordinal);
_blobCache ??= new byte[FieldCount][];
_blobCache[ordinal] = blob;
}
return blob;
}
internal static int Sqlite3AffinityType(string dataTypeName)
{
if (dataTypeName == null)
{
// if no type is specified then the column has affinity BLOB
return SQLITE_BLOB;
}
var typeRules = new Func<string, int?>[]
{
name => Contains(name, "INT") ? SQLITE_INTEGER : (int?)null,
name => Contains(name, "CHAR")
|| Contains(name, "CLOB")
|| Contains(name, "TEXT")
? SQLITE_TEXT
: (int?)null,
name => Contains(name, "BLOB") ? SQLITE_BLOB : (int?)null,
name => Contains(name, "REAL")
|| Contains(name, "FLOA")
|| Contains(name, "DOUB")
? SQLITE_FLOAT
: (int?)null
};
return typeRules.Select(r => r(dataTypeName)).FirstOrDefault(r => r != null) ?? SQLITE_TEXT; // code NUMERICAL affinity as TEXT
}
private static bool Contains(string haystack, string needle)
=> haystack.IndexOf(needle, StringComparison.OrdinalIgnoreCase) >= 0;
}
}