-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSQLiteDataReader.cs
More file actions
executable file
·2104 lines (1775 loc) · 71.9 KB
/
Copy pathSQLiteDataReader.cs
File metadata and controls
executable file
·2104 lines (1775 loc) · 71.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
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 (robert@blackcastlesoft.com)
*
* Released to the public domain, use at your own risk!
********************************************************/
namespace System.Data.SQLite
{
using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Data;
using System.Data.Common;
using System.Globalization;
/// <summary>
/// SQLite implementation of DbDataReader.
/// </summary>
public sealed class SQLiteDataReader : DbDataReader
{
/// <summary>
/// Underlying command this reader is attached to
/// </summary>
private SQLiteCommand _command;
/// <summary>
/// The flags pertaining to the associated connection (via the command).
/// </summary>
private SQLiteConnectionFlags _flags;
/// <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>
/// The number of calls to Step() that have returned true (i.e. the number of rows that
/// have been read in the current result set).
/// </summary>
private int _stepCount;
/// <summary>
/// Maps the field (column) names to their corresponding indexes within the results.
/// </summary>
private Dictionary<string, int> _fieldIndexes;
/// <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>
/// If set, then raise an exception when the object is accessed after being disposed.
/// </summary>
internal bool _throwOnDisposed;
/// <summary>
/// An array of rowid's for the active statement if CommandBehavior.KeyInfo is specified
/// </summary>
private SQLiteKeyReader _keyInfo;
/// <summary>
/// Matches the version of the connection.
/// </summary>
internal int _version;
/// <summary>
/// The "stub" (i.e. placeholder) base schema name to use when returning
/// column schema information. Matches the base schema name used by the
/// associated connection.
/// </summary>
private string _baseSchemaName;
/// <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)
{
_throwOnDisposed = true;
_command = cmd;
_version = _command.Connection._version;
_baseSchemaName = _command.Connection._baseSchemaName;
_commandBehavior = behave;
_activeStatementIndex = -1;
_rowsAffected = -1;
RefreshFlags();
SQLiteConnection.OnChanged(GetConnection(this),
new ConnectionEventArgs(SQLiteConnectionEventType.NewDataReader,
null, null, _command, this, null, null, new object[] { behave }));
if (_command != null)
NextResult();
}
///////////////////////////////////////////////////////////////////////////////////////////////
#region IDisposable "Pattern" Members
private bool disposed;
private void CheckDisposed() /* throw */
{
#if THROW_ON_DISPOSED
if (disposed && _throwOnDisposed)
throw new ObjectDisposedException(typeof(SQLiteDataReader).Name);
#endif
}
///////////////////////////////////////////////////////////////////////////////////////////////
/// <summary>
/// Dispose of all resources used by this datareader.
/// </summary>
/// <param name="disposing"></param>
protected override void Dispose(bool disposing)
{
SQLiteConnection.OnChanged(GetConnection(this),
new ConnectionEventArgs(SQLiteConnectionEventType.DisposingDataReader,
null, null, _command, this, null, null, new object[] { disposing,
disposed, _commandBehavior, _readingState, _rowsAffected, _stepCount,
_fieldCount, _disposeCommand, _throwOnDisposed }));
try
{
if (!disposed)
{
//if (disposing)
//{
// ////////////////////////////////////
// // dispose managed resources here...
// ////////////////////////////////////
//}
//////////////////////////////////////
// release unmanaged resources here...
//////////////////////////////////////
//
// NOTE: Fix for ticket [e1b2e0f769], do NOT throw exceptions
// while we are being disposed.
//
_throwOnDisposed = false;
}
}
finally
{
base.Dispose(disposing);
//
// NOTE: Everything should be fully disposed at this point.
//
disposed = true;
}
}
#endregion
///////////////////////////////////////////////////////////////////////////////////////////////
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()
{
CheckDisposed();
SQLiteConnection.OnChanged(GetConnection(this),
new ConnectionEventArgs(SQLiteConnectionEventType.ClosingDataReader,
null, null, _command, this, null, null, new object[] { _commandBehavior,
_readingState, _rowsAffected, _stepCount, _fieldCount, _disposeCommand,
_throwOnDisposed }));
try
{
if (_command != null)
{
try
{
try
{
// Make sure we've not been canceled
if (_version != 0)
{
try
{
while (NextResult())
{
}
}
catch(SQLiteException)
{
}
}
_command.ResetDataReader();
}
finally
{
// If the datareader's behavior includes closing the connection, then do so here.
if ((_commandBehavior & CommandBehavior.CloseConnection) != 0 && _command.Connection != null)
_command.Connection.Close();
}
}
finally
{
if (_disposeCommand)
_command.Dispose();
}
}
_command = null;
_activeStatement = null;
_fieldIndexes = 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 (!_throwOnDisposed)
return;
if (_command == null)
throw new InvalidOperationException("DataReader has been closed");
if (_version == 0)
throw new SQLiteException("Execution was aborted by the user");
SQLiteConnection connection = _command.Connection;
if (connection._version != _version || connection.State != ConnectionState.Open)
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 Collections.IEnumerator GetEnumerator()
{
CheckDisposed();
return new DbEnumerator(this, ((_commandBehavior & CommandBehavior.CloseConnection) == CommandBehavior.CloseConnection));
}
/// <summary>
/// Not implemented. Returns 0
/// </summary>
public override int Depth
{
get
{
CheckDisposed();
CheckClosed();
return 0;
}
}
/// <summary>
/// Returns the number of columns in the current resultset
/// </summary>
public override int FieldCount
{
get
{
CheckDisposed();
CheckClosed();
if (_keyInfo == null)
return _fieldCount;
return _fieldCount + _keyInfo.Count;
}
}
/// <summary>
/// Forces the connection flags cached by this data reader to be refreshed
/// from the underlying connection.
/// </summary>
public void RefreshFlags()
{
CheckDisposed();
_flags = SQLiteCommand.GetFlags(_command);
}
/// <summary>
/// Returns the number of rows seen so far in the current result set.
/// </summary>
public int StepCount
{
get
{
CheckDisposed();
CheckClosed();
return _stepCount;
}
}
private int PrivateVisibleFieldCount
{
get { return _fieldCount; }
}
/// <summary>
/// Returns the number of visible fields in the current resultset
/// </summary>
public override int VisibleFieldCount
{
get
{
CheckDisposed();
CheckClosed();
return PrivateVisibleFieldCount;
}
}
/// <summary>
/// This method is used to make sure the result set is open and a row is currently available.
/// </summary>
private void VerifyForGet()
{
CheckClosed();
CheckValidRow();
}
/// <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)
{
if ((_flags & SQLiteConnectionFlags.NoVerifyTypeAffinity) == SQLiteConnectionFlags.NoVerifyTypeAffinity)
return TypeAffinity.None;
TypeAffinity affinity = GetSQLiteType(_flags, i).Affinity;
switch (affinity)
{
case TypeAffinity.Int64:
if (typ == DbType.Int64) return affinity;
if (typ == DbType.Int32) return affinity;
if (typ == DbType.Int16) return affinity;
if (typ == DbType.Byte) return affinity;
if (typ == DbType.SByte) return affinity;
if (typ == DbType.Boolean) return affinity;
if (typ == DbType.DateTime) return affinity;
if (typ == DbType.Double) return affinity;
if (typ == DbType.Single) return affinity;
if (typ == DbType.Decimal) return affinity;
break;
case TypeAffinity.Double:
if (typ == DbType.Double) return affinity;
if (typ == DbType.Single) return affinity;
if (typ == DbType.Decimal) return affinity;
if (typ == DbType.DateTime) return affinity;
break;
case TypeAffinity.Text:
if (typ == DbType.String) 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.Binary) return affinity;
if (typ == DbType.String) return affinity;
break;
}
throw new InvalidCastException();
}
/// <summary>
/// Invokes the data reader value callback configured for the database
/// type name associated with the specified column. If no data reader
/// value callback is available for the database type name, do nothing.
/// </summary>
/// <param name="index">
/// The index of the column being read.
/// </param>
/// <param name="eventArgs">
/// The extra event data to pass into the callback.
/// </param>
/// <param name="complete">
/// Non-zero if the default handling for the data reader call should be
/// skipped. If this is set to non-zero and the necessary return value
/// is unavailable or unsuitable, an exception will be thrown.
/// </param>
private void InvokeReadValueCallback(
int index,
SQLiteReadEventArgs eventArgs,
out bool complete
)
{
complete = false;
SQLiteConnectionFlags oldFlags = _flags;
_flags &= ~SQLiteConnectionFlags.UseConnectionReadValueCallbacks;
try
{
string typeName = GetDataTypeName(index);
if (typeName == null)
return;
SQLiteConnection connection = GetConnection(this);
if (connection == null)
return;
SQLiteTypeCallbacks callbacks;
if (!connection.TryGetTypeCallbacks(typeName, out callbacks) ||
(callbacks == null))
{
return;
}
SQLiteReadValueCallback callback = callbacks.ReadValueCallback;
if (callback == null)
return;
object userData = callbacks.ReadValueUserData;
callback(
_activeStatement._sql, this, oldFlags, eventArgs, typeName,
index, userData, out complete); /* throw */
}
finally
{
_flags |= SQLiteConnectionFlags.UseConnectionReadValueCallbacks;
}
}
/// <summary>
/// Attempts to query the integer identifier for the current row. This
/// will not work for tables that were created WITHOUT ROWID -OR- if the
/// query does not include the "rowid" column or one of its aliases -OR-
/// if the <see cref="SQLiteDataReader" /> was not created with the
/// <see cref="CommandBehavior.KeyInfo"/> flag.
/// </summary>
/// <param name="i">
/// The index of the BLOB column.
/// </param>
/// <returns>
/// The integer identifier for the current row -OR- null if it could not
/// be determined.
/// </returns>
internal long? GetRowId(
int i
)
{
// CheckDisposed();
VerifyForGet();
if (_keyInfo == null)
return null;
int iRowId = _keyInfo.GetRowIdIndex(
GetDatabaseName(i), GetTableName(i));
if (iRowId == -1)
return null;
return GetInt64(iRowId);
}
/// <summary>
/// Retrieves the column as a <see cref="SQLiteBlob" /> object.
/// This will not work for tables that were created WITHOUT ROWID
/// -OR- if the query does not include the "rowid" column or one
/// of its aliases -OR- if the <see cref="SQLiteDataReader" /> was
/// not created with the <see cref="CommandBehavior.KeyInfo" />
/// flag.
/// </summary>
/// <param name="i">The index of the column.</param>
/// <param name="readOnly">
/// Non-zero to open the blob object for read-only access.
/// </param>
/// <returns>A new <see cref="SQLiteBlob" /> object.</returns>
public SQLiteBlob GetBlob(int i, bool readOnly)
{
CheckDisposed();
VerifyForGet();
if ((_flags & SQLiteConnectionFlags.UseConnectionReadValueCallbacks) == SQLiteConnectionFlags.UseConnectionReadValueCallbacks)
{
SQLiteDataReaderValue value = new SQLiteDataReaderValue();
bool complete;
InvokeReadValueCallback(i, new SQLiteReadValueEventArgs(
"GetBlob", new SQLiteReadBlobEventArgs(readOnly), value),
out complete);
if (complete)
return (SQLiteBlob)value.BlobValue;
}
if (i >= PrivateVisibleFieldCount && _keyInfo != null)
return _keyInfo.GetBlob(i - PrivateVisibleFieldCount, readOnly);
return SQLiteBlob.Create(this, i, readOnly);
}
/// <summary>
/// Retrieves the column as a boolean value
/// </summary>
/// <param name="i">The index of the column.</param>
/// <returns>bool</returns>
public override bool GetBoolean(int i)
{
CheckDisposed();
VerifyForGet();
if ((_flags & SQLiteConnectionFlags.UseConnectionReadValueCallbacks) == SQLiteConnectionFlags.UseConnectionReadValueCallbacks)
{
SQLiteDataReaderValue value = new SQLiteDataReaderValue();
bool complete;
InvokeReadValueCallback(i, new SQLiteReadValueEventArgs(
"GetBoolean", null, value), out complete);
if (complete)
{
if (value.BooleanValue == null)
throw new SQLiteException("missing boolean return value");
return (bool)value.BooleanValue;
}
}
if (i >= PrivateVisibleFieldCount && _keyInfo != null)
return _keyInfo.GetBoolean(i - PrivateVisibleFieldCount);
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.</param>
/// <returns>byte</returns>
public override byte GetByte(int i)
{
CheckDisposed();
VerifyForGet();
if ((_flags & SQLiteConnectionFlags.UseConnectionReadValueCallbacks) == SQLiteConnectionFlags.UseConnectionReadValueCallbacks)
{
SQLiteDataReaderValue value = new SQLiteDataReaderValue();
bool complete;
InvokeReadValueCallback(i, new SQLiteReadValueEventArgs(
"GetByte", null, value), out complete);
if (complete)
{
if (value.ByteValue == null)
throw new SQLiteException("missing byte return value");
return (byte)value.ByteValue;
}
}
if (i >= PrivateVisibleFieldCount && _keyInfo != null)
return _keyInfo.GetByte(i - PrivateVisibleFieldCount);
VerifyType(i, DbType.Byte);
return _activeStatement._sql.GetByte(_activeStatement, i);
}
/// <summary>
/// Retrieves a column as an array of bytes (blob)
/// </summary>
/// <param name="i">The index of the column.</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)
{
CheckDisposed();
VerifyForGet();
if ((_flags & SQLiteConnectionFlags.UseConnectionReadValueCallbacks) == SQLiteConnectionFlags.UseConnectionReadValueCallbacks)
{
SQLiteReadArrayEventArgs eventArgs = new SQLiteReadArrayEventArgs(
fieldOffset, buffer, bufferoffset, length);
SQLiteDataReaderValue value = new SQLiteDataReaderValue();
bool complete;
InvokeReadValueCallback(i, new SQLiteReadValueEventArgs(
"GetBytes", eventArgs, value), out complete);
if (complete)
{
byte[] bytes = value.BytesValue;
if (bytes != null)
{
#if !PLATFORM_COMPACTFRAMEWORK
Array.Copy(bytes, /* throw */
eventArgs.DataOffset, eventArgs.ByteBuffer,
eventArgs.BufferOffset, eventArgs.Length);
#else
Array.Copy(bytes, /* throw */
(int)eventArgs.DataOffset, eventArgs.ByteBuffer,
eventArgs.BufferOffset, eventArgs.Length);
#endif
return eventArgs.Length;
}
else
{
return -1;
}
}
}
if (i >= PrivateVisibleFieldCount && _keyInfo != null)
return _keyInfo.GetBytes(i - PrivateVisibleFieldCount, 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.</param>
/// <returns>char</returns>
public override char GetChar(int i)
{
CheckDisposed();
VerifyForGet();
if ((_flags & SQLiteConnectionFlags.UseConnectionReadValueCallbacks) == SQLiteConnectionFlags.UseConnectionReadValueCallbacks)
{
SQLiteDataReaderValue value = new SQLiteDataReaderValue();
bool complete;
InvokeReadValueCallback(i, new SQLiteReadValueEventArgs(
"GetChar", null, value), out complete);
if (complete)
{
if (value.CharValue == null)
throw new SQLiteException("missing character return value");
return (char)value.CharValue;
}
}
if (i >= PrivateVisibleFieldCount && _keyInfo != null)
return _keyInfo.GetChar(i - PrivateVisibleFieldCount);
VerifyType(i, DbType.SByte);
return _activeStatement._sql.GetChar(_activeStatement, i);
}
/// <summary>
/// Retrieves a column as an array of chars (blob)
/// </summary>
/// <param name="i">The index of the column.</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)
{
CheckDisposed();
VerifyForGet();
if ((_flags & SQLiteConnectionFlags.UseConnectionReadValueCallbacks) == SQLiteConnectionFlags.UseConnectionReadValueCallbacks)
{
SQLiteReadArrayEventArgs eventArgs = new SQLiteReadArrayEventArgs(
fieldoffset, buffer, bufferoffset, length);
SQLiteDataReaderValue value = new SQLiteDataReaderValue();
bool complete;
InvokeReadValueCallback(i, new SQLiteReadValueEventArgs(
"GetChars", eventArgs, value), out complete);
if (complete)
{
char[] chars = value.CharsValue;
if (chars != null)
{
#if !PLATFORM_COMPACTFRAMEWORK
Array.Copy(chars, /* throw */
eventArgs.DataOffset, eventArgs.CharBuffer,
eventArgs.BufferOffset, eventArgs.Length);
#else
Array.Copy(chars, /* throw */
(int)eventArgs.DataOffset, eventArgs.CharBuffer,
eventArgs.BufferOffset, eventArgs.Length);
#endif
return eventArgs.Length;
}
else
{
return -1;
}
}
}
if (i >= PrivateVisibleFieldCount && _keyInfo != null)
return _keyInfo.GetChars(i - PrivateVisibleFieldCount, fieldoffset, buffer, bufferoffset, length);
if ((_flags & SQLiteConnectionFlags.NoVerifyTextAffinity) != SQLiteConnectionFlags.NoVerifyTextAffinity)
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.</param>
/// <returns>string</returns>
public override string GetDataTypeName(int i)
{
CheckDisposed();
if (i >= PrivateVisibleFieldCount && _keyInfo != null)
return _keyInfo.GetDataTypeName(i - PrivateVisibleFieldCount);
TypeAffinity affin = TypeAffinity.Uninitialized;
return _activeStatement._sql.ColumnType(_activeStatement, i, ref affin);
}
/// <summary>
/// Retrieve the column as a date/time value
/// </summary>
/// <param name="i">The index of the column.</param>
/// <returns>DateTime</returns>
public override DateTime GetDateTime(int i)
{
CheckDisposed();
VerifyForGet();
if ((_flags & SQLiteConnectionFlags.UseConnectionReadValueCallbacks) == SQLiteConnectionFlags.UseConnectionReadValueCallbacks)
{
SQLiteDataReaderValue value = new SQLiteDataReaderValue();
bool complete;
InvokeReadValueCallback(i, new SQLiteReadValueEventArgs(
"GetDateTime", null, value), out complete);
if (complete)
{
if (value.DateTimeValue == null)
throw new SQLiteException("missing date/time return value");
return (DateTime)value.DateTimeValue;
}
}
if (i >= PrivateVisibleFieldCount && _keyInfo != null)
return _keyInfo.GetDateTime(i - PrivateVisibleFieldCount);
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.</param>
/// <returns>decimal</returns>
public override decimal GetDecimal(int i)
{
CheckDisposed();
VerifyForGet();
if ((_flags & SQLiteConnectionFlags.UseConnectionReadValueCallbacks) == SQLiteConnectionFlags.UseConnectionReadValueCallbacks)
{
SQLiteDataReaderValue value = new SQLiteDataReaderValue();
bool complete;
InvokeReadValueCallback(i, new SQLiteReadValueEventArgs(
"GetDecimal", null, value), out complete);
if (complete)
{
if (value.DecimalValue == null)
throw new SQLiteException("missing decimal return value");
return (decimal)value.DecimalValue;
}
}
if (i >= PrivateVisibleFieldCount && _keyInfo != null)
return _keyInfo.GetDecimal(i - PrivateVisibleFieldCount);
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.</param>
/// <returns>double</returns>
public override double GetDouble(int i)
{
CheckDisposed();
VerifyForGet();
if ((_flags & SQLiteConnectionFlags.UseConnectionReadValueCallbacks) == SQLiteConnectionFlags.UseConnectionReadValueCallbacks)
{
SQLiteDataReaderValue value = new SQLiteDataReaderValue();
bool complete;
InvokeReadValueCallback(i, new SQLiteReadValueEventArgs(
"GetDouble", null, value), out complete);
if (complete)
{
if (value.DoubleValue == null)
throw new SQLiteException("missing double return value");
return (double)value.DoubleValue;
}
}
if (i >= PrivateVisibleFieldCount && _keyInfo != null)
return _keyInfo.GetDouble(i - PrivateVisibleFieldCount);
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.</param>
/// <returns>Type</returns>
public override Type GetFieldType(int i)
{
CheckDisposed();
if (i >= PrivateVisibleFieldCount && _keyInfo != null)
return _keyInfo.GetFieldType(i - PrivateVisibleFieldCount);
return SQLiteConvert.SQLiteTypeToType(GetSQLiteType(_flags, i));
}
/// <summary>
/// Returns a column as a float value
/// </summary>
/// <param name="i">The index of the column.</param>
/// <returns>float</returns>
public override float GetFloat(int i)
{
CheckDisposed();
VerifyForGet();
if ((_flags & SQLiteConnectionFlags.UseConnectionReadValueCallbacks) == SQLiteConnectionFlags.UseConnectionReadValueCallbacks)
{
SQLiteDataReaderValue value = new SQLiteDataReaderValue();
bool complete;
InvokeReadValueCallback(i, new SQLiteReadValueEventArgs(
"GetFloat", null, value), out complete);
if (complete)
{
if (value.FloatValue == null)
throw new SQLiteException("missing float return value");
return (float)value.FloatValue;
}
}
if (i >= PrivateVisibleFieldCount && _keyInfo != null)
return _keyInfo.GetFloat(i - PrivateVisibleFieldCount);
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.</param>
/// <returns>Guid</returns>
public override Guid GetGuid(int i)
{
CheckDisposed();
VerifyForGet();
if ((_flags & SQLiteConnectionFlags.UseConnectionReadValueCallbacks) == SQLiteConnectionFlags.UseConnectionReadValueCallbacks)
{
SQLiteDataReaderValue value = new SQLiteDataReaderValue();
bool complete;
InvokeReadValueCallback(i, new SQLiteReadValueEventArgs(
"GetGuid", null, value), out complete);
if (complete)
{
if (value.GuidValue == null)
throw new SQLiteException("missing guid return value");
return (Guid)value.GuidValue;
}
}
if (i >= PrivateVisibleFieldCount && _keyInfo != null)
return _keyInfo.GetGuid(i - PrivateVisibleFieldCount);
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>