forked from jstedfast/MimeKit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSQLiteDataReader.cs
1057 lines (927 loc) · 38 KB
/
SQLiteDataReader.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
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/********************************************************
* 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;
using System.Globalization;
using System.Reflection;
/// <summary>
/// SQLite implementation of DbDataReader.
/// </summary>
public sealed class SqliteDataReader : DbDataReader
{
/// <summary>
/// Underlying command this reader is attached to
/// </summary>
private SqliteCommand _command;
/// <summary>
/// Index of the current statement in the command being processed
/// </summary>
private int _activeStatementIndex;
/// <summary>
/// Current statement being Read()
/// </summary>
private SqliteStatement _activeStatement;
/// <summary>
/// State of the current statement being processed.
/// -1 = First Step() executed, so the first Read() will be ignored
/// 0 = Actively reading
/// 1 = Finished reading
/// 2 = Non-row-returning statement, no records
/// </summary>
private int _readingState;
/// <summary>
/// Number of records affected by the insert/update statements executed on the command
/// </summary>
private int _rowsAffected;
/// <summary>
/// Count of fields (columns) in the row-returning statement currently being processed
/// </summary>
private int _fieldCount;
/// <summary>
/// Datatypes of active fields (columns) in the current statement, used for type-restricting data
/// </summary>
private SQLiteType[] _fieldTypeArray;
/// <summary>
/// The behavior of the datareader
/// </summary>
private CommandBehavior _commandBehavior;
/// <summary>
/// If set, then dispose of the command object when the reader is finished
/// </summary>
internal bool _disposeCommand;
/// <summary>
/// An array of rowid's for the active statement if CommandBehavior.KeyInfo is specified
/// </summary>
private SqliteKeyReader _keyInfo;
internal long _version; // Matches the version of the connection
/// <summary>
/// Internal constructor, initializes the datareader and sets up to begin executing statements
/// </summary>
/// <param name="cmd">The SqliteCommand this data reader is for</param>
/// <param name="behave">The expected behavior of the data reader</param>
internal SqliteDataReader(SqliteCommand cmd, CommandBehavior behave)
{
_command = cmd;
_version = _command.Connection._version;
_commandBehavior = behave;
_activeStatementIndex = -1;
_activeStatement = null;
_rowsAffected = -1;
_fieldCount = 0;
if (_command != null)
NextResult();
}
internal void Cancel()
{
_version = 0;
}
/// <summary>
/// Closes the datareader, potentially closing the connection as well if CommandBehavior.CloseConnection was specified.
/// </summary>
public override void Close()
{
try
{
if (_command != null)
{
try
{
try
{
// Make sure we've not been canceled
if (_version != 0)
{
try
{
while (NextResult())
{
}
}
catch
{
}
}
_command.ClearDataReader();
}
finally
{
// If the datareader's behavior includes closing the connection, then do so here.
if ((_commandBehavior & CommandBehavior.CloseConnection) != 0 && _command.Connection != null) {
// We need to call Dispose on the command before we call Dispose on the Connection,
// otherwise we'll get a SQLITE_LOCKED exception.
var conn = _command.Connection;
_command.Dispose ();
conn.Close();
_disposeCommand = false;
}
}
}
finally
{
if (_disposeCommand)
_command.Dispose();
}
}
_command = null;
_activeStatement = null;
_fieldTypeArray = null;
}
finally
{
if (_keyInfo != null)
{
_keyInfo.Dispose();
_keyInfo = null;
}
}
}
/// <summary>
/// Throw an error if the datareader is closed
/// </summary>
private void CheckClosed()
{
if (_command == null)
throw new InvalidOperationException("DataReader has been closed");
if (_version == 0)
throw new SqliteException((int)SQLiteErrorCode.Abort, "Execution was aborted by the user");
if (_command.Connection.State != ConnectionState.Open || _command.Connection._version != _version)
throw new InvalidOperationException("Connection was closed, statement was terminated");
}
/// <summary>
/// Throw an error if a row is not loaded
/// </summary>
private void CheckValidRow()
{
if (_readingState != 0)
throw new InvalidOperationException("No current row");
}
/// <summary>
/// Enumerator support
/// </summary>
/// <returns>Returns a DbEnumerator object.</returns>
public override global::System.Collections.IEnumerator GetEnumerator()
{
return new DbEnumerator(this, ((_commandBehavior & CommandBehavior.CloseConnection) == CommandBehavior.CloseConnection));
}
/// <summary>
/// Not implemented. Returns 0
/// </summary>
public override int Depth
{
get
{
CheckClosed();
return 0;
}
}
/// <summary>
/// Returns the number of columns in the current resultset
/// </summary>
public override int FieldCount
{
get
{
CheckClosed();
if (_keyInfo == null)
return _fieldCount;
return _fieldCount + _keyInfo.Count;
}
}
/// <summary>
/// Returns the number of visible fielsd in the current resultset
/// </summary>
public override int VisibleFieldCount
{
get
{
CheckClosed();
return _fieldCount;
}
}
/// <summary>
/// SQLite is inherently un-typed. All datatypes in SQLite are natively strings. The definition of the columns of a table
/// and the affinity of returned types are all we have to go on to type-restrict data in the reader.
///
/// This function attempts to verify that the type of data being requested of a column matches the datatype of the column. In
/// the case of columns that are not backed into a table definition, we attempt to match up the affinity of a column (int, double, string or blob)
/// to a set of known types that closely match that affinity. It's not an exact science, but its the best we can do.
/// </summary>
/// <returns>
/// This function throws an InvalidTypeCast() exception if the requested type doesn't match the column's definition or affinity.
/// </returns>
/// <param name="i">The index of the column to type-check</param>
/// <param name="typ">The type we want to get out of the column</param>
private TypeAffinity VerifyType(int i, DbType typ)
{
CheckClosed();
CheckValidRow();
TypeAffinity affinity = GetSQLiteType(i).Affinity;
switch (affinity)
{
case TypeAffinity.Int64:
if (typ == DbType.Int16) return affinity;
if (typ == DbType.Int32) return affinity;
if (typ == DbType.Int64) return affinity;
if (typ == DbType.Boolean) return affinity;
if (typ == DbType.Byte) return affinity;
if (typ == DbType.DateTime) return affinity;
if (typ == DbType.Single) return affinity;
if (typ == DbType.Double) return affinity;
if (typ == DbType.Decimal) return affinity;
break;
case TypeAffinity.Double:
if (typ == DbType.Single) return affinity;
if (typ == DbType.Double) return affinity;
if (typ == DbType.Decimal) return affinity;
if (typ == DbType.DateTime) return affinity;
break;
case TypeAffinity.Text:
if (typ == DbType.SByte) return affinity;
if (typ == DbType.String) return affinity;
if (typ == DbType.SByte) return affinity;
if (typ == DbType.Guid) return affinity;
if (typ == DbType.DateTime) return affinity;
if (typ == DbType.Decimal) return affinity;
break;
case TypeAffinity.Blob:
if (typ == DbType.Guid) return affinity;
if (typ == DbType.String) return affinity;
if (typ == DbType.Binary) return affinity;
break;
}
throw new InvalidCastException();
}
/// <summary>
/// Retrieves the column as a boolean value
/// </summary>
/// <param name="i">The index of the column to retrieve</param>
/// <returns>bool</returns>
public override bool GetBoolean(int i)
{
if (i >= VisibleFieldCount && _keyInfo != null)
return _keyInfo.GetBoolean(i - VisibleFieldCount);
VerifyType(i, DbType.Boolean);
return Convert.ToBoolean(GetValue(i), CultureInfo.CurrentCulture);
}
/// <summary>
/// Retrieves the column as a single byte value
/// </summary>
/// <param name="i">The index of the column to retrieve</param>
/// <returns>byte</returns>
public override byte GetByte(int i)
{
if (i >= VisibleFieldCount && _keyInfo != null)
return _keyInfo.GetByte(i - VisibleFieldCount);
VerifyType(i, DbType.Byte);
return Convert.ToByte(_activeStatement._sql.GetInt32(_activeStatement, i));
}
/// <summary>
/// Retrieves a column as an array of bytes (blob)
/// </summary>
/// <param name="i">The index of the column to retrieve</param>
/// <param name="fieldOffset">The zero-based index of where to begin reading the data</param>
/// <param name="buffer">The buffer to write the bytes into</param>
/// <param name="bufferoffset">The zero-based index of where to begin writing into the array</param>
/// <param name="length">The number of bytes to retrieve</param>
/// <returns>The actual number of bytes written into the array</returns>
/// <remarks>
/// To determine the number of bytes in the column, pass a null value for the buffer. The total length will be returned.
/// </remarks>
public override long GetBytes(int i, long fieldOffset, byte[] buffer, int bufferoffset, int length)
{
if (i >= VisibleFieldCount && _keyInfo != null)
return _keyInfo.GetBytes(i - VisibleFieldCount, fieldOffset, buffer, bufferoffset, length);
VerifyType(i, DbType.Binary);
return _activeStatement._sql.GetBytes(_activeStatement, i, (int)fieldOffset, buffer, bufferoffset, length);
}
/// <summary>
/// Returns the column as a single character
/// </summary>
/// <param name="i">The index of the column to retrieve</param>
/// <returns>char</returns>
public override char GetChar(int i)
{
if (i >= VisibleFieldCount && _keyInfo != null)
return _keyInfo.GetChar(i - VisibleFieldCount);
VerifyType(i, DbType.SByte);
return Convert.ToChar(_activeStatement._sql.GetInt32(_activeStatement, i));
}
/// <summary>
/// Retrieves a column as an array of chars (blob)
/// </summary>
/// <param name="i">The index of the column to retrieve</param>
/// <param name="fieldoffset">The zero-based index of where to begin reading the data</param>
/// <param name="buffer">The buffer to write the characters into</param>
/// <param name="bufferoffset">The zero-based index of where to begin writing into the array</param>
/// <param name="length">The number of bytes to retrieve</param>
/// <returns>The actual number of characters written into the array</returns>
/// <remarks>
/// To determine the number of characters in the column, pass a null value for the buffer. The total length will be returned.
/// </remarks>
public override long GetChars(int i, long fieldoffset, char[] buffer, int bufferoffset, int length)
{
if (i >= VisibleFieldCount && _keyInfo != null)
return _keyInfo.GetChars(i - VisibleFieldCount, fieldoffset, buffer, bufferoffset, length);
VerifyType(i, DbType.String);
return _activeStatement._sql.GetChars(_activeStatement, i, (int)fieldoffset, buffer, bufferoffset, length);
}
/// <summary>
/// Retrieves the name of the back-end datatype of the column
/// </summary>
/// <param name="i">The index of the column to retrieve</param>
/// <returns>string</returns>
public override string GetDataTypeName(int i)
{
if (i >= VisibleFieldCount && _keyInfo != null)
return _keyInfo.GetDataTypeName(i - VisibleFieldCount);
SQLiteType typ = GetSQLiteType(i);
return _activeStatement._sql.ColumnType(_activeStatement, i, out typ.Affinity);
}
/// <summary>
/// Retrieve the column as a date/time value
/// </summary>
/// <param name="i">The index of the column to retrieve</param>
/// <returns>DateTime</returns>
public override DateTime GetDateTime(int i)
{
if (i >= VisibleFieldCount && _keyInfo != null)
return _keyInfo.GetDateTime(i - VisibleFieldCount);
VerifyType(i, DbType.DateTime);
return _activeStatement._sql.GetDateTime(_activeStatement, i);
}
/// <summary>
/// Retrieve the column as a decimal value
/// </summary>
/// <param name="i">The index of the column to retrieve</param>
/// <returns>decimal</returns>
public override decimal GetDecimal(int i)
{
if (i >= VisibleFieldCount && _keyInfo != null)
return _keyInfo.GetDecimal(i - VisibleFieldCount);
VerifyType(i, DbType.Decimal);
return Decimal.Parse(_activeStatement._sql.GetText(_activeStatement, i), NumberStyles.AllowDecimalPoint | NumberStyles.AllowExponent | NumberStyles.AllowLeadingSign, CultureInfo.InvariantCulture);
}
/// <summary>
/// Returns the column as a double
/// </summary>
/// <param name="i">The index of the column to retrieve</param>
/// <returns>double</returns>
public override double GetDouble(int i)
{
if (i >= VisibleFieldCount && _keyInfo != null)
return _keyInfo.GetDouble(i - VisibleFieldCount);
VerifyType(i, DbType.Double);
return _activeStatement._sql.GetDouble(_activeStatement, i);
}
/// <summary>
/// Returns the .NET type of a given column
/// </summary>
/// <param name="i">The index of the column to retrieve</param>
/// <returns>Type</returns>
public override Type GetFieldType(int i)
{
if (i >= VisibleFieldCount && _keyInfo != null)
return _keyInfo.GetFieldType(i - VisibleFieldCount);
return SqliteConvert.SQLiteTypeToType(GetSQLiteType(i));
}
/// <summary>
/// Returns a column as a float value
/// </summary>
/// <param name="i">The index of the column to retrieve</param>
/// <returns>float</returns>
public override float GetFloat(int i)
{
if (i >= VisibleFieldCount && _keyInfo != null)
return _keyInfo.GetFloat(i - VisibleFieldCount);
VerifyType(i, DbType.Single);
return Convert.ToSingle(_activeStatement._sql.GetDouble(_activeStatement, i));
}
/// <summary>
/// Returns the column as a Guid
/// </summary>
/// <param name="i">The index of the column to retrieve</param>
/// <returns>Guid</returns>
public override Guid GetGuid(int i)
{
if (i >= VisibleFieldCount && _keyInfo != null)
return _keyInfo.GetGuid(i - VisibleFieldCount);
TypeAffinity affinity = VerifyType(i, DbType.Guid);
if (affinity == TypeAffinity.Blob)
{
byte[] buffer = new byte[16];
_activeStatement._sql.GetBytes(_activeStatement, i, 0, buffer, 0, 16);
return new Guid(buffer);
}
else
return new Guid(_activeStatement._sql.GetText(_activeStatement, i));
}
/// <summary>
/// Returns the column as a short
/// </summary>
/// <param name="i">The index of the column to retrieve</param>
/// <returns>Int16</returns>
public override Int16 GetInt16(int i)
{
if (i >= VisibleFieldCount && _keyInfo != null)
return _keyInfo.GetInt16(i - VisibleFieldCount);
VerifyType(i, DbType.Int16);
return Convert.ToInt16(_activeStatement._sql.GetInt32(_activeStatement, i));
}
/// <summary>
/// Retrieves the column as an int
/// </summary>
/// <param name="i">The index of the column to retrieve</param>
/// <returns>Int32</returns>
public override Int32 GetInt32(int i)
{
if (i >= VisibleFieldCount && _keyInfo != null)
return _keyInfo.GetInt32(i - VisibleFieldCount);
VerifyType(i, DbType.Int32);
return _activeStatement._sql.GetInt32(_activeStatement, i);
}
/// <summary>
/// Retrieves the column as a long
/// </summary>
/// <param name="i">The index of the column to retrieve</param>
/// <returns>Int64</returns>
public override Int64 GetInt64(int i)
{
if (i >= VisibleFieldCount && _keyInfo != null)
return _keyInfo.GetInt64(i - VisibleFieldCount);
VerifyType(i, DbType.Int64);
return _activeStatement._sql.GetInt64(_activeStatement, i);
}
/// <summary>
/// Retrieves the name of the column
/// </summary>
/// <param name="i">The index of the column to retrieve</param>
/// <returns>string</returns>
public override string GetName(int i)
{
if (i >= VisibleFieldCount && _keyInfo != null)
return _keyInfo.GetName(i - VisibleFieldCount);
return _activeStatement._sql.ColumnName(_activeStatement, i);
}
/// <summary>
/// Retrieves the i of a column, given its name
/// </summary>
/// <param name="name">The name of the column to retrieve</param>
/// <returns>The int i of the column</returns>
public override int GetOrdinal(string name)
{
CheckClosed();
int r = _activeStatement._sql.ColumnIndex(_activeStatement, name);
if (r == -1 && _keyInfo != null)
{
r = _keyInfo.GetOrdinal(name);
if (r > -1) r += VisibleFieldCount;
}
return r;
}
/// <summary>
/// Schema information in SQLite is difficult to map into .NET conventions, so a lot of work must be done
/// to gather the necessary information so it can be represented in an ADO.NET manner.
/// </summary>
/// <returns>Returns a DataTable containing the schema information for the active SELECT statement being processed.</returns>
public override DataTable GetSchemaTable()
{
return GetSchemaTable(true, false);
}
static bool hasColumnMetadataSupport = true;
internal DataTable GetSchemaTable(bool wantUniqueInfo, bool wantDefaultValue)
{
CheckClosed();
DataTable tbl = new DataTable("SchemaTable");
DataTable tblIndexes = null;
DataTable tblIndexColumns;
DataRow row;
string temp;
string strCatalog = "";
string strTable = "";
string strColumn = "";
tbl.Locale = CultureInfo.InvariantCulture;
tbl.Columns.Add(SchemaTableColumn.ColumnName, typeof(String));
tbl.Columns.Add(SchemaTableColumn.ColumnOrdinal, typeof(int));
tbl.Columns.Add(SchemaTableColumn.ColumnSize, typeof(int));
tbl.Columns.Add(SchemaTableColumn.NumericPrecision, typeof(short));
tbl.Columns.Add(SchemaTableColumn.NumericScale, typeof(short));
tbl.Columns.Add(SchemaTableColumn.IsUnique, typeof(Boolean));
tbl.Columns.Add(SchemaTableColumn.IsKey, typeof(Boolean));
tbl.Columns.Add(SchemaTableOptionalColumn.BaseServerName, typeof(string));
tbl.Columns.Add(SchemaTableOptionalColumn.BaseCatalogName, typeof(String));
tbl.Columns.Add(SchemaTableColumn.BaseColumnName, typeof(String));
tbl.Columns.Add(SchemaTableColumn.BaseSchemaName, typeof(String));
tbl.Columns.Add(SchemaTableColumn.BaseTableName, typeof(String));
tbl.Columns.Add(SchemaTableColumn.DataType, typeof(Type));
tbl.Columns.Add(SchemaTableColumn.AllowDBNull, typeof(Boolean));
tbl.Columns.Add(SchemaTableColumn.ProviderType, typeof(int));
tbl.Columns.Add(SchemaTableColumn.IsAliased, typeof(Boolean));
tbl.Columns.Add(SchemaTableColumn.IsExpression, typeof(Boolean));
tbl.Columns.Add(SchemaTableOptionalColumn.IsAutoIncrement, typeof(Boolean));
tbl.Columns.Add(SchemaTableOptionalColumn.IsRowVersion, typeof(Boolean));
tbl.Columns.Add(SchemaTableOptionalColumn.IsHidden, typeof(Boolean));
tbl.Columns.Add(SchemaTableColumn.IsLong, typeof(Boolean));
tbl.Columns.Add(SchemaTableOptionalColumn.IsReadOnly, typeof(Boolean));
tbl.Columns.Add(SchemaTableOptionalColumn.ProviderSpecificDataType, typeof(Type));
tbl.Columns.Add(SchemaTableOptionalColumn.DefaultValue, typeof(object));
tbl.Columns.Add("DataTypeName", typeof(string));
tbl.Columns.Add("CollationType", typeof(string));
tbl.BeginLoadData();
for (int n = 0; n < _fieldCount; n++)
{
row = tbl.NewRow();
DbType typ = GetSQLiteType(n).Type;
// Default settings for the column
row[SchemaTableColumn.ColumnName] = GetName(n);
row[SchemaTableColumn.ColumnOrdinal] = n;
row[SchemaTableColumn.ColumnSize] = SqliteConvert.DbTypeToColumnSize(typ);
row[SchemaTableColumn.NumericPrecision] = SqliteConvert.DbTypeToNumericPrecision(typ);
row[SchemaTableColumn.NumericScale] = SqliteConvert.DbTypeToNumericScale(typ);
row[SchemaTableColumn.ProviderType] = GetSQLiteType(n).Type;
row[SchemaTableColumn.IsLong] = false;
row[SchemaTableColumn.AllowDBNull] = true;
row[SchemaTableOptionalColumn.IsReadOnly] = false;
row[SchemaTableOptionalColumn.IsRowVersion] = false;
row[SchemaTableColumn.IsUnique] = false;
row[SchemaTableColumn.IsKey] = false;
row[SchemaTableOptionalColumn.IsAutoIncrement] = false;
row[SchemaTableColumn.DataType] = GetFieldType(n);
row[SchemaTableOptionalColumn.IsHidden] = false;
// HACK: Prevent exploding if Sqlite was built without the SQLITE_ENABLE_COLUMN_METADATA option.
//
// This code depends on sqlite3_column_origin_name, which only exists if Sqlite was built with
// the SQLITE_ENABLE_COLUMN_METADATA option. This is not the case on iOS, MacOS or (most?)
// Androids, so we exclude it from the MONOTOUCH build, and degrade on other systems by simply
// omitting the metadata from the result.
//
// TODO: we could implement better fallbacks as proposed in
// https://bugzilla.xamarin.com/show_bug.cgi?id=2128
//
#if !MONOTOUCH
if (hasColumnMetadataSupport) {
try {
strColumn = _command.Connection._sql.ColumnOriginalName(_activeStatement, n);
if (String.IsNullOrEmpty(strColumn) == false) row[SchemaTableColumn.BaseColumnName] = strColumn;
row[SchemaTableColumn.IsExpression] = String.IsNullOrEmpty(strColumn);
row[SchemaTableColumn.IsAliased] = (String.Compare(GetName(n), strColumn, true, CultureInfo.InvariantCulture) != 0);
temp = _command.Connection._sql.ColumnTableName(_activeStatement, n);
if (String.IsNullOrEmpty(temp) == false) row[SchemaTableColumn.BaseTableName] = temp;
temp = _command.Connection._sql.ColumnDatabaseName(_activeStatement, n);
if (String.IsNullOrEmpty(temp) == false) row[SchemaTableOptionalColumn.BaseCatalogName] = temp;
} catch (EntryPointNotFoundException) {
hasColumnMetadataSupport = false;
}
}
#endif
string dataType = null;
// If we have a table-bound column, extract the extra information from it
if (String.IsNullOrEmpty(strColumn) == false)
{
string collSeq;
bool bNotNull;
bool bPrimaryKey;
bool bAutoIncrement;
string[] arSize;
// Get the column meta data
_command.Connection._sql.ColumnMetaData(
(string)row[SchemaTableOptionalColumn.BaseCatalogName],
(string)row[SchemaTableColumn.BaseTableName],
strColumn,
out dataType, out collSeq, out bNotNull, out bPrimaryKey, out bAutoIncrement);
if (bNotNull || bPrimaryKey) row[SchemaTableColumn.AllowDBNull] = false;
row[SchemaTableColumn.IsKey] = bPrimaryKey;
row[SchemaTableOptionalColumn.IsAutoIncrement] = bAutoIncrement;
row["CollationType"] = collSeq;
// For types like varchar(50) and such, extract the size
arSize = dataType.Split('(');
if (arSize.Length > 1)
{
dataType = arSize[0];
arSize = arSize[1].Split(')');
if (arSize.Length > 1)
{
arSize = arSize[0].Split(',', '.');
if (GetSQLiteType(n).Type == DbType.String || GetSQLiteType(n).Type == DbType.Binary)
{
row[SchemaTableColumn.ColumnSize] = Convert.ToInt32(arSize[0], CultureInfo.InvariantCulture);
}
else
{
row[SchemaTableColumn.NumericPrecision] = Convert.ToInt32(arSize[0], CultureInfo.InvariantCulture);
if (arSize.Length > 1)
row[SchemaTableColumn.NumericScale] = Convert.ToInt32(arSize[1], CultureInfo.InvariantCulture);
}
}
}
if (wantDefaultValue)
{
// Determine the default value for the column, which sucks because we have to query the schema for each column
using (SqliteCommand cmdTable = new SqliteCommand(String.Format(CultureInfo.InvariantCulture, "PRAGMA [{0}].TABLE_INFO([{1}])",
row[SchemaTableOptionalColumn.BaseCatalogName],
row[SchemaTableColumn.BaseTableName]
), _command.Connection))
using (DbDataReader rdTable = cmdTable.ExecuteReader())
{
// Find the matching column
while (rdTable.Read())
{
if (String.Compare((string)row[SchemaTableColumn.BaseColumnName], rdTable.GetString(1), true, CultureInfo.InvariantCulture) == 0)
{
if (rdTable.IsDBNull(4) == false)
row[SchemaTableOptionalColumn.DefaultValue] = rdTable[4];
break;
}
}
}
}
// Determine IsUnique properly, which is a pain in the butt!
if (wantUniqueInfo)
{
if ((string)row[SchemaTableOptionalColumn.BaseCatalogName] != strCatalog
|| (string)row[SchemaTableColumn.BaseTableName] != strTable)
{
strCatalog = (string)row[SchemaTableOptionalColumn.BaseCatalogName];
strTable = (string)row[SchemaTableColumn.BaseTableName];
tblIndexes = _command.Connection.GetSchema("Indexes", new string[] {
(string)row[SchemaTableOptionalColumn.BaseCatalogName],
null,
(string)row[SchemaTableColumn.BaseTableName],
null });
}
foreach (DataRow rowIndexes in tblIndexes.Rows)
{
tblIndexColumns = _command.Connection.GetSchema("IndexColumns", new string[] {
(string)row[SchemaTableOptionalColumn.BaseCatalogName],
null,
(string)row[SchemaTableColumn.BaseTableName],
(string)rowIndexes["INDEX_NAME"],
null
});
foreach (DataRow rowColumnIndex in tblIndexColumns.Rows)
{
if (String.Compare((string)rowColumnIndex["COLUMN_NAME"], strColumn, true, CultureInfo.InvariantCulture) == 0)
{
if (tblIndexColumns.Rows.Count == 1 && (bool)row[SchemaTableColumn.AllowDBNull] == false)
row[SchemaTableColumn.IsUnique] = rowIndexes["UNIQUE"];
// If its an integer primary key and the only primary key in the table, then its a rowid alias and is autoincrement
// NOTE: Currently commented out because this is not always the desired behavior. For example, a 1:1 relationship with
// another table, where the other table is autoincrement, but this one is not, and uses the rowid from the other.
// It is safer to only set Autoincrement on tables where we're SURE the user specified AUTOINCREMENT, even if its a rowid column.
if (tblIndexColumns.Rows.Count == 1 && (bool)rowIndexes["PRIMARY_KEY"] == true && String.IsNullOrEmpty(dataType) == false &&
String.Compare(dataType, "integer", true, CultureInfo.InvariantCulture) == 0)
{
// row[SchemaTableOptionalColumn.IsAutoIncrement] = true;
}
break;
}
}
}
}
if (String.IsNullOrEmpty(dataType))
{
TypeAffinity affin;
dataType = _activeStatement._sql.ColumnType(_activeStatement, n, out affin);
}
if (String.IsNullOrEmpty(dataType) == false)
row["DataTypeName"] = dataType;
}
tbl.Rows.Add(row);
}
if (_keyInfo != null)
_keyInfo.AppendSchemaTable(tbl);
tbl.AcceptChanges();
tbl.EndLoadData();
return tbl;
}
/// <summary>
/// Retrieves the column as a string
/// </summary>
/// <param name="i">The index of the column to retrieve</param>
/// <returns>string</returns>
public override string GetString(int i)
{
if (i >= VisibleFieldCount && _keyInfo != null)
return _keyInfo.GetString(i - VisibleFieldCount);
VerifyType(i, DbType.String);
return _activeStatement._sql.GetText(_activeStatement, i);
}
/// <summary>
/// Retrieves the column as an object corresponding to the underlying datatype of the column
/// </summary>
/// <param name="i">The index of the column to retrieve</param>
/// <returns>object</returns>
public override object GetValue(int i)
{
if (i >= VisibleFieldCount && _keyInfo != null)
return _keyInfo.GetValue(i - VisibleFieldCount);
SQLiteType typ = GetSQLiteType(i);
return _activeStatement._sql.GetValue(_activeStatement, i, typ);
}
/// <summary>
/// Retreives the values of multiple columns, up to the size of the supplied array
/// </summary>
/// <param name="values">The array to fill with values from the columns in the current resultset</param>
/// <returns>The number of columns retrieved</returns>
public override int GetValues(object[] values)
{
int nMax = FieldCount;
if (values.Length < nMax) nMax = values.Length;
for (int n = 0; n < nMax; n++)
{
values[n] = GetValue(n);
}
return nMax;
}
/// <summary>
/// Returns True if the resultset has rows that can be fetched
/// </summary>
public override bool HasRows
{
get
{
CheckClosed();
return (_readingState != 1);
}
}
/// <summary>
/// Returns True if the data reader is closed
/// </summary>
public override bool IsClosed
{
get { return (_command == null); }
}
/// <summary>
/// Returns True if the specified column is null
/// </summary>
/// <param name="i">The index of the column to retrieve</param>
/// <returns>True or False</returns>
public override bool IsDBNull(int i)
{
if (i >= VisibleFieldCount && _keyInfo != null)
return _keyInfo.IsDBNull(i - VisibleFieldCount);
return _activeStatement._sql.IsNull(_activeStatement, i);
}
/// <summary>
/// Moves to the next resultset in multiple row-returning SQL command.
/// </summary>
/// <returns>True if the command was successful and a new resultset is available, False otherwise.</returns>
public override bool NextResult()
{
CheckClosed();
SqliteStatement stmt = null;
int fieldCount;
while (true)
{
if (_activeStatement != null && stmt == null)
{
// Reset the previously-executed statement
_activeStatement._sql.Reset(_activeStatement);
// If we're only supposed to return a single rowset, step through all remaining statements once until
// they are all done and return false to indicate no more resultsets exist.
if ((_commandBehavior & CommandBehavior.SingleResult) != 0)
{
for (; ; )
{
stmt = _command.GetStatement(_activeStatementIndex + 1);
if (stmt == null) break;
_activeStatementIndex++;
stmt._sql.Step(stmt);
if (stmt._sql.ColumnCount(stmt) == 0)
{
if (_rowsAffected == -1) _rowsAffected = 0;
_rowsAffected += stmt._sql.Changes;
}
stmt._sql.Reset(stmt); // Gotta reset after every step to release any locks and such!
}
return false;
}
}
// Get the next statement to execute
stmt = _command.GetStatement(_activeStatementIndex + 1);
// If we've reached the end of the statements, return false, no more resultsets
if (stmt == null)
return false;
// If we were on a current resultset, set the state to "done reading" for it
if (_readingState < 1)
_readingState = 1;
_activeStatementIndex++;
fieldCount = stmt._sql.ColumnCount(stmt);
// If the statement is not a select statement or we're not retrieving schema only, then perform the initial step
if ((_commandBehavior & CommandBehavior.SchemaOnly) == 0 || fieldCount == 0)
{
if (stmt._sql.Step(stmt))
{
_readingState = -1;
}
else if (fieldCount == 0) // No rows returned, if fieldCount is zero, skip to the next statement
{
if (_rowsAffected == -1) _rowsAffected = 0;
_rowsAffected += stmt._sql.Changes;
stmt._sql.Reset(stmt);
continue; // Skip this command and move to the next, it was not a row-returning resultset
}
else // No rows, fieldCount is non-zero so stop here
{
_readingState = 1; // This command returned columns but no rows, so return true, but HasRows = false and Read() returns false
}
}
// Ahh, we found a row-returning resultset eligible to be returned!
_activeStatement = stmt;
_fieldCount = fieldCount;
_fieldTypeArray = null;
if ((_commandBehavior & CommandBehavior.KeyInfo) != 0)
LoadKeyInfo();
return true;
}
}
/// <summary>
/// Retrieves the SQLiteType for a given column, and caches it to avoid repetetive interop calls.
/// </summary>
/// <param name="i">The index of the column to retrieve</param>
/// <returns>A SQLiteType structure</returns>
private SQLiteType GetSQLiteType(int i)
{
SQLiteType typ;
// Initialize the field types array if not already initialized
if (_fieldTypeArray == null)
_fieldTypeArray = new SQLiteType[VisibleFieldCount];
// Initialize this column's field type instance
if (_fieldTypeArray[i] == null) _fieldTypeArray[i] = new SQLiteType();
typ = _fieldTypeArray[i];
// If not initialized, then fetch the declared column datatype and attempt to convert it
// to a known DbType.
if (typ.Affinity == TypeAffinity.Uninitialized)
typ.Type = SqliteConvert.TypeNameToDbType(_activeStatement._sql.ColumnType(_activeStatement, i, out typ.Affinity));
else
typ.Affinity = _activeStatement._sql.ColumnAffinity(_activeStatement, i);
return typ;
}
/// <summary>
/// Reads the next row from the resultset
/// </summary>
/// <returns>True if a new row was successfully loaded and is ready for processing</returns>
public override bool Read()
{
CheckClosed();
if (_readingState == -1) // First step was already done at the NextResult() level, so don't step again, just return true.
{
_readingState = 0;
return true;
}