forked from CsharpDatabase/CsharpSQLite
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSQLiteDataReader.cs
More file actions
executable file
·497 lines (443 loc) · 13.9 KB
/
SQLiteDataReader.cs
File metadata and controls
executable file
·497 lines (443 loc) · 13.9 KB
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
//
// System.Data.SQLite.SqliteDataReader.cs
//
// Provides a means of reading a forward-only stream of rows from a Sqlite
// database file.
//
// Author(s): Vladimir Vukicevic <[email protected]>
// Everaldo Canuto <[email protected]>
// Joshua Tauberer <[email protected]>
//
// Copyright (C) 2002 Vladimir Vukicevic
//
// Permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to
// permit persons to whom the Software is furnished to do so, subject to
// the following conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Data.Common;
using System.Data.SQLite;
namespace System.Data.SQLite
{
public class SQLiteDataReader : DbDataReader, IDataReader, IDisposable, IDataRecord
{
#region Fields
private SQLiteCommand command;
private List<object[]> rows;
private string[] columns;
private Dictionary<String, Object> column_names_sens, column_names_insens;
private int current_row;
private bool closed;
private bool reading;
private int records_affected;
private string[] decltypes;
#endregion
#region Constructors and destructors
internal SQLiteDataReader(SQLiteCommand cmd, Sqlite3.Vdbe pVm, int version)
{
command = cmd;
rows = new List<object[]>();
column_names_sens = new Dictionary<String, Object>();
column_names_insens = new Dictionary<String, Object>(StringComparer.InvariantCultureIgnoreCase);
closed = false;
current_row = -1;
reading = true;
ReadpVm(pVm, version, cmd);
ReadingDone();
}
#endregion
#region Properties
public override int Depth
{
get { return 0; }
}
public override int FieldCount
{
get { return columns.Length; }
}
public override object this[string name]
{
get
{
return GetValue(GetOrdinal(name));
}
}
public override object this[int i]
{
get { return GetValue(i); }
}
public override bool IsClosed
{
get { return closed; }
}
public override int RecordsAffected
{
get { return records_affected; }
}
#endregion
#region Internal Methods
internal void ReadpVm(Sqlite3.Vdbe pVm, int version, SQLiteCommand cmd)
{
int pN;
IntPtr pazValue;
IntPtr pazColName;
bool first = true;
DeclaredMode[] declmode = null;
while(true)
{
bool hasdata = cmd.ExecuteStatement(pVm, out pN, out pazValue, out pazColName);
// For the first row, get the column information
if(first)
{
first = false;
if(version == 3)
{
// A decltype might be null if the type is unknown to sqlite.
decltypes = new string[pN];
declmode = new DeclaredMode[pN]; // 1 == integer, 2 == datetime
for(int i = 0; i < pN; i++)
{
string decl = Sqlite3.sqlite3_column_decltype(pVm, i);
if(decl != null)
{
decltypes[i] = decl.ToLower(System.Globalization.CultureInfo.InvariantCulture);
if(decltypes[i] == "int" || decltypes[i] == "integer")
declmode[i] = DeclaredMode.Integer;
else if(decltypes[i] == "date" || decltypes[i] == "datetime")
declmode[i] = DeclaredMode.DateTime;
else if(decltypes[i] == "uniqueidentifier" || decltypes[i] == "guid")
declmode[i] = DeclaredMode.Guid;
}
}
}
columns = new string[pN];
for(int i = 0; i < pN; i++)
{
string colName;
//if (version == 2) {
// IntPtr fieldPtr = Marshal.ReadIntPtr (pazColName, i*IntPtr.Size);
// colName = Sqlite.HeapToString (fieldPtr, ((SqliteConnection)cmd.Connection).Encoding);
//} else {
colName = Sqlite3.sqlite3_column_name(pVm, i);
//}
columns[i] = colName;
column_names_sens[colName] = i;
column_names_insens[colName] = i;
}
}
if(!hasdata)
break;
object[] data_row = new object [pN];
for(int i = 0; i < pN; i++)
{
switch(Sqlite3.sqlite3_column_type(pVm, i))
{
case 1:
long val = Sqlite3.sqlite3_column_int64(pVm, i);
// If the column was declared as an 'int' or 'integer' OR the type of column is unknown
// let's play nice and return an int (version 3 only).
if((declmode[i] == DeclaredMode.Integer || decltypes[i] == null) && val >= int.MinValue && val <= int.MaxValue)
data_row[i] = (int)val;
// Or if it was declared a date or datetime, do the reverse of what we
// do for DateTime parameters.
else if(declmode[i] == DeclaredMode.DateTime)
data_row[i] = DateTime.FromFileTime(val);
else
data_row[i] = val;
break;
case 2:
data_row[i] = Sqlite3.sqlite3_column_double(pVm, i);
break;
case 3:
data_row[i] = Sqlite3.sqlite3_column_text(pVm, i);
// If the column was declared as a 'date' or 'datetime', let's play
// nice and return a DateTime (version 3 only).
if(declmode[i] == DeclaredMode.DateTime)
if(data_row[i] == null)
data_row[i] = null;
else
data_row[i] = DateTime.Parse((string)data_row[i], System.Globalization.CultureInfo.InvariantCulture);
else if(declmode[i] == DeclaredMode.Guid)
{
Guid g;
if(data_row[i] == null)
data_row[i] = null;
else if(Guid.TryParse((string)data_row[i], out g))
data_row[i] = g;
}
break;
case 4:
byte[] blob = Sqlite3.sqlite3_column_blob(pVm, i);
data_row[i] = blob;
break;
case 5:
data_row[i] = null;
break;
default:
throw new Exception("FATAL: Unknown sqlite3_column_type");
}
}
rows.Add(data_row);
}
}
internal void ReadingDone()
{
records_affected = command.NumChanges();
reading = false;
}
#endregion
#region Public Methods
public override void Close()
{
closed = true;
}
protected override void Dispose(bool disposing)
{
if(disposing)
Close();
}
public override IEnumerator GetEnumerator()
{
return new DbEnumerator(this);
}
public override DataTable GetSchemaTable()
{
DataTable dataTableSchema = new DataTable();
dataTableSchema.Columns.Add("ColumnName", typeof(String));
dataTableSchema.Columns.Add("ColumnOrdinal", typeof(Int32));
dataTableSchema.Columns.Add("ColumnSize", typeof(Int32));
dataTableSchema.Columns.Add("NumericPrecision", typeof(Int32));
dataTableSchema.Columns.Add("NumericScale", typeof(Int32));
dataTableSchema.Columns.Add("IsUnique", typeof(Boolean));
dataTableSchema.Columns.Add("IsKey", typeof(Boolean));
dataTableSchema.Columns.Add("BaseCatalogName", typeof(String));
dataTableSchema.Columns.Add("BaseColumnName", typeof(String));
dataTableSchema.Columns.Add("BaseSchemaName", typeof(String));
dataTableSchema.Columns.Add("BaseTableName", typeof(String));
dataTableSchema.Columns.Add("DataType", typeof(Type));
dataTableSchema.Columns.Add("AllowDBNull", typeof(Boolean));
dataTableSchema.Columns.Add("ProviderType", typeof(Int32));
dataTableSchema.Columns.Add("IsAliased", typeof(Boolean));
dataTableSchema.Columns.Add("IsExpression", typeof(Boolean));
dataTableSchema.Columns.Add("IsIdentity", typeof(Boolean));
dataTableSchema.Columns.Add("IsAutoIncrement", typeof(Boolean));
dataTableSchema.Columns.Add("IsRowVersion", typeof(Boolean));
dataTableSchema.Columns.Add("IsHidden", typeof(Boolean));
dataTableSchema.Columns.Add("IsLong", typeof(Boolean));
dataTableSchema.Columns.Add("IsReadOnly", typeof(Boolean));
dataTableSchema.BeginLoadData();
for(int i = 0; i < this.FieldCount; i += 1)
{
DataRow schemaRow = dataTableSchema.NewRow();
schemaRow["ColumnName"] = columns[i];
schemaRow["ColumnOrdinal"] = i;
schemaRow["ColumnSize"] = 0;
schemaRow["NumericPrecision"] = 0;
schemaRow["NumericScale"] = 0;
schemaRow["IsUnique"] = false;
schemaRow["IsKey"] = false;
schemaRow["BaseCatalogName"] = "";
schemaRow["BaseColumnName"] = columns[i];
schemaRow["BaseSchemaName"] = "";
schemaRow["BaseTableName"] = "";
schemaRow["DataType"] = typeof(string);
schemaRow["AllowDBNull"] = true;
schemaRow["ProviderType"] = 0;
schemaRow["IsAliased"] = false;
schemaRow["IsExpression"] = false;
schemaRow["IsIdentity"] = false;
schemaRow["IsAutoIncrement"] = false;
schemaRow["IsRowVersion"] = false;
schemaRow["IsHidden"] = false;
schemaRow["IsLong"] = false;
schemaRow["IsReadOnly"] = false;
dataTableSchema.Rows.Add(schemaRow);
schemaRow.AcceptChanges();
}
dataTableSchema.EndLoadData();
return dataTableSchema;
}
public override bool NextResult()
{
current_row++;
return (current_row < rows.Count);
}
public override bool Read()
{
return NextResult();
}
#endregion
#region IDataRecord getters
public override bool GetBoolean(int i)
{
return Convert.ToBoolean(((object[])rows[current_row])[i]);
}
public override byte GetByte(int i)
{
return Convert.ToByte(((object[])rows[current_row])[i]);
}
public override long GetBytes(int i, long fieldOffset, byte[] buffer, int bufferOffset, int length)
{
byte[] data = (byte[])(((object[])rows[current_row])[i]);
if(buffer != null)
Array.Copy(data, (int)fieldOffset, buffer, bufferOffset, length);
return data.LongLength - fieldOffset;
}
public override char GetChar(int i)
{
return Convert.ToChar(((object[])rows[current_row])[i]);
}
public override long GetChars(int i, long fieldOffset, char[] buffer, int bufferOffset, int length)
{
char[] data = (char[])(((object[])rows[current_row])[i]);
if(buffer != null)
Array.Copy(data, (int)fieldOffset, buffer, bufferOffset, length);
return data.LongLength - fieldOffset;
}
public override string GetDataTypeName(int i)
{
if(decltypes != null && decltypes[i] != null)
return decltypes[i];
return "text"; // SQL Lite data type
}
public override DateTime GetDateTime(int i)
{
return Convert.ToDateTime(((object[])rows[current_row])[i]);
}
public override decimal GetDecimal(int i)
{
return Convert.ToDecimal(((object[])rows[current_row])[i]);
}
public override double GetDouble(int i)
{
return Convert.ToDouble(((object[])rows[current_row])[i]);
}
public override Type GetFieldType(int i)
{
int row = current_row;
if(row == -1 && rows.Count == 0)
return typeof(string);
if(row == -1)
row = 0;
object element = ((object[])rows[row])[i];
if(element != null)
return element.GetType();
else
return typeof(string);
// Note that the return value isn't guaranteed to
// be the same as the rows are read if different
// types of information are stored in the column.
}
public override float GetFloat(int i)
{
return Convert.ToSingle(((object[])rows[current_row])[i]);
}
public override Guid GetGuid(int i)
{
object value = GetValue(i);
if(!(value is Guid))
{
if(value is DBNull)
throw new SQLiteExecutionException("Column value must not be null");
//throw new InvalidCastException ("Type is " + value.GetType ().ToString ());
return new Guid(value.ToString());
}
return ((Guid)value);
}
public override short GetInt16(int i)
{
return Convert.ToInt16(((object[])rows[current_row])[i]);
}
public override int GetInt32(int i)
{
return Convert.ToInt32(((object[])rows[current_row])[i]);
}
public override long GetInt64(int i)
{
return Convert.ToInt64(((object[])rows[current_row])[i]);
}
public override string GetName(int i)
{
return columns[i];
}
public override int GetOrdinal(string name)
{
object v = column_names_sens.ContainsKey(name) ? column_names_sens[name] : null;
if(v == null)
v = column_names_insens.ContainsKey(name) ? column_names_insens[name] : null;
if(v == null)
throw new ArgumentException("Column does not exist.");
return (int)v;
}
public override string GetString(int i)
{
if(((object[])rows[current_row])[i] != null)
return (((object[])rows[current_row])[i]).ToString();
else
return null;
}
public override object GetValue(int i)
{
if(decltypes[i] == null && rows[current_row][i] != null && rows[current_row][i].GetType() == typeof(long))
{
// NOTE: .NET default integer types to 32 bit integers but in SQLite integers are 64 be default
return GetInt32(i);
}
return ((object[])rows[current_row])[i];
}
public override int GetValues(object[] values)
{
int num_to_fill = System.Math.Min(values.Length, columns.Length);
for(int i = 0; i < num_to_fill; i++)
{
if(((object[])rows[current_row])[i] != null)
{
values[i] = ((object[])rows[current_row])[i];
}
else
{
values[i] = DBNull.Value;
}
}
return num_to_fill;
}
public override bool IsDBNull(int i)
{
return (((object[])rows[current_row])[i] == null);
}
public override bool HasRows
{
get { return rows.Count > 0; }
}
public override int VisibleFieldCount
{
get { return FieldCount; }
}
#endregion
private enum DeclaredMode
{
Native = 0,
Integer = 101,
DateTime = 102,
Guid = 103,
}
}
}