System.Data.SQLite
Check-in [2a9118d124]
Not logged in

Many hyperlinks are disabled.
Use anonymous login to enable hyperlinks.

Overview
Comment:Reorganize native interop definitions into the UnsafeNativeMethods class. Enable compilation for the .NET Compact Framework.
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | virtualTables
Files: files | file ages | folders
SHA1: 2a9118d124af13c396a7e73cfcbfe90cad4646bb
User & Date: mistachkin 2013-06-08 02:36:14
Context
2013-06-08
04:15
Add disposal checks to the SQLiteModuleNoop class. check-in: 62d2f9e5bd user: mistachkin tags: virtualTables
02:36
Reorganize native interop definitions into the UnsafeNativeMethods class. Enable compilation for the .NET Compact Framework. check-in: 2a9118d124 user: mistachkin tags: virtualTables
2013-06-07
21:23
Implement the xFilter virtual table method. More cleanup. check-in: 08eaed4731 user: mistachkin tags: virtualTables
Changes
Hide Diffs Unified Diffs Ignore Whitespace Patch

Changes to System.Data.SQLite/SQLiteModuleBase.cs.

47
48
49
50
51
52
53

54



55
56
57
58
59
60
61
..
67
68
69
70
71
72
73

74



75
76
77
78
79
80
81
...
217
218
219
220
221
222
223


224





225
226
227
228
229
230
231


232





233
234
235
236
237
238
239
...
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
...
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
...
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
...
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
...
674
675
676
677
678
679
680

681



682
683
684
685
686
687
688
...
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
...
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
...
891
892
893
894
895
896
897
898


899
900
901
902
903
904
905
906
...
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
...
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
....
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
        ///////////////////////////////////////////////////////////////////////

        public void SetDouble(double value)
        {
            if (pContext == IntPtr.Zero)
                throw new InvalidOperationException();


            UnsafeNativeMethods.sqlite3_result_double(pContext, value);



        }

        ///////////////////////////////////////////////////////////////////////

        public void SetInt(int value)
        {
            if (pContext == IntPtr.Zero)
................................................................................
        ///////////////////////////////////////////////////////////////////////

        public void SetInt64(long value)
        {
            if (pContext == IntPtr.Zero)
                throw new InvalidOperationException();


            UnsafeNativeMethods.sqlite3_result_int64(pContext, value);



        }

        ///////////////////////////////////////////////////////////////////////

        public void SetString(string value)
        {
            if (pContext == IntPtr.Zero)
................................................................................
        }

        ///////////////////////////////////////////////////////////////////////

        public long GetInt64()
        {
            if (pValue == IntPtr.Zero) return default(long);


            return UnsafeNativeMethods.sqlite3_value_int64(pValue);





        }

        ///////////////////////////////////////////////////////////////////////

        public double GetDouble()
        {
            if (pValue == IntPtr.Zero) return default(double);


            return UnsafeNativeMethods.sqlite3_value_double(pValue);





        }

        ///////////////////////////////////////////////////////////////////////

        public string GetString()
        {
            if (pValue == IntPtr.Zero) return null;
................................................................................
    }
    #endregion

    ///////////////////////////////////////////////////////////////////////////

    public class SQLiteIndexConstraint
    {
        private SQLiteModuleBase.UnsafeNativeMethods2.sqlite3_index_constraint constraint;
    }

    ///////////////////////////////////////////////////////////////////////////

    public class SQLiteIndexOrderBy
    {
        private SQLiteModuleBase.UnsafeNativeMethods2.sqlite3_index_orderby orderBy;
    }

    ///////////////////////////////////////////////////////////////////////////

    public class SQLiteIndexConstraintUsage
    {
        private SQLiteModuleBase.UnsafeNativeMethods2.sqlite3_index_constraint_usage constraintUsage;
    }

    ///////////////////////////////////////////////////////////////////////////

    public class SQLiteIndex
    {
        SQLiteIndexConstraint[] Constraints;
................................................................................
        double estimatedCost; /* Estimated cost of using this index */
    }

    ///////////////////////////////////////////////////////////////////////////

    public class SQLiteVirtualTableCursor
    {
        internal SQLiteModuleBase.UnsafeNativeMethods2.sqlite3_vtab_cursor cursor;




    }

    ///////////////////////////////////////////////////////////////////////////

    [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
    public delegate int xFunc(
        IntPtr pContext,
        int argc,
        [MarshalAs(UnmanagedType.LPArray, SizeParamIndex = 1)]
        IntPtr[] argv
    );





    ///////////////////////////////////////////////////////////////////////////

    public interface ISQLiteNativeModule
    {


        SQLiteErrorCode xCreate(IntPtr pDb, IntPtr pAux, int argc, ref IntPtr[] argv, ref IntPtr pVtab, ref IntPtr pError);
        SQLiteErrorCode xConnect(IntPtr pDb, IntPtr pAux, int argc, ref IntPtr[] argv, ref IntPtr pVtab, ref IntPtr pError);
        SQLiteErrorCode xBestIndex(IntPtr pVtab, IntPtr index);
        SQLiteErrorCode xDisconnect(IntPtr pVtab);
        SQLiteErrorCode xDestroy(IntPtr pVtab);
        SQLiteErrorCode xOpen(IntPtr pVtab, ref IntPtr pCursor);
        SQLiteErrorCode xClose(IntPtr pCursor);
................................................................................
        SQLiteErrorCode xColumn(IntPtr pCursor, IntPtr pContext, int index);
        SQLiteErrorCode xRowId(IntPtr pCursor, ref long rowId);
        SQLiteErrorCode xUpdate(IntPtr pVtab, int nData, ref IntPtr apData, ref long rowId);
        SQLiteErrorCode xBegin(IntPtr pVtab);
        SQLiteErrorCode xSync(IntPtr pVtab);
        SQLiteErrorCode xCommit(IntPtr pVtab);
        SQLiteErrorCode xRollback(IntPtr pVtab);
        SQLiteErrorCode xFindFunction(IntPtr pVtab, int nArg, IntPtr zName, ref xFunc pxFunc, ref IntPtr ppArg);
        SQLiteErrorCode xRename(IntPtr pVtab, IntPtr zNew);
        SQLiteErrorCode xSavepoint(IntPtr pVtab, int iSavepoint);
        SQLiteErrorCode xRelease(IntPtr pVtab, int iSavepoint);
        SQLiteErrorCode xRollbackTo(IntPtr pVtab, int iSavepoint);
    }

    ///////////////////////////////////////////////////////////////////////////
................................................................................
        SQLiteErrorCode Savepoint(int iSavepoint);
        SQLiteErrorCode Release(int iSavepoint);
        SQLiteErrorCode RollbackTo(int iSavepoint);
    }

    ///////////////////////////////////////////////////////////////////////////

#if NET_40
    [SecurityCritical()]
#else
    [SecurityPermission(SecurityAction.LinkDemand, UnmanagedCode = true)]
#endif
    public abstract class SQLiteModuleBase : ISQLiteManagedModule, ISQLiteNativeModule,  IDisposable
    {
        private static Encoding Utf8Encoding = Encoding.UTF8;

        #region Unsafe Native Methods Class
        [SuppressUnmanagedCodeSecurity()]
        internal static class UnsafeNativeMethods2
        {
            // https://www.sqlite.org/vtab.html


            [StructLayout(LayoutKind.Sequential)]
            public struct sqlite3_module
            {
                public int iVersion;
                public xCreate xCreate;
                public xConnect xConnect;
                public xBestIndex xBestIndex;
                public xDisconnect xDisconnect;
                public xDestroy xDestroy;
                public xOpen xOpen;
                public xClose xClose;
                public xFilter xFilter;
                public xNext xNext;
                public xEof xEof;
                public xColumn xColumn;
                public xRowId xRowId;
                public xUpdate xUpdate;
                public xBegin xBegin;
                public xSync xSync;
                public xCommit xCommit;
                public xRollback xRollback;
                public xFindFunction xFindFunction;
                public xRename xRename;
                /* The methods above are in version 1 of the sqlite3_module
                 * object.  Those below are for version 2 and greater. */
                public xSavepoint xSavepoint;
                public xRelease xRelease;
                public xRollbackTo xRollbackTo;
            }

            [StructLayout(LayoutKind.Sequential)]
            public struct sqlite3_vtab
            {
                [MarshalAs(UnmanagedType.LPStruct)]
                public sqlite3_module pModule;
                public int nRef; /* NO LONGER USED */
                public IntPtr zErrMsg;
            }

            [StructLayout(LayoutKind.Sequential)]
            public struct sqlite3_vtab_cursor
            {
                [MarshalAs(UnmanagedType.LPStruct)]
                public sqlite3_vtab pVTab;
            }

            [StructLayout(LayoutKind.Sequential)]
            public struct sqlite3_index_constraint
            {
                public int iColumn;
                public byte op;
                public byte usable;
                public int iTermOffset;
            }

            [StructLayout(LayoutKind.Sequential)]
            public struct sqlite3_index_orderby
            {
                public int iColumn; /* Column number */
                public byte desc;   /* True for DESC.  False for ASC. */
            }

            [StructLayout(LayoutKind.Sequential)]
            public struct sqlite3_index_constraint_usage
            {
                public int argvIndex; /* if >0, constraint is part of argv to xFilter */
                public byte omit;     /* Do not code a test for this constraint */
            }

            [StructLayout(LayoutKind.Sequential)]
            public struct sqlite3_index_info
            {
                /* Inputs */
                public int nConstraint;           /* Number of entries in aConstraint */
                [MarshalAs(UnmanagedType.LPArray, SizeParamIndex = 0)]
                public sqlite3_index_constraint[] aConstraint;
                public int nOrderBy;
                public sqlite3_index_orderby[] aOrderBy;
                /* Outputs */
                public sqlite3_index_constraint_usage[] aConstraintUsage;
                public int idxNum;                /* Number used to identify the index */
                public string idxStr;              /* String, possibly obtained from sqlite3_malloc */
                public int needToFreeIdxStr;      /* Free idxStr using sqlite3_free() if true */
                public int orderByConsumed;       /* True if output is already ordered */
                public double estimatedCost;      /* Estimated cost of using this index */
            }





            [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
            public delegate SQLiteErrorCode xCreate(
                IntPtr pDb,
                IntPtr pAux,
                int argc,
                [MarshalAs(UnmanagedType.LPArray, SizeParamIndex = 2)]
                ref IntPtr[] argv,
                ref IntPtr pVtab,
                ref IntPtr pError
            );

            [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
            public delegate SQLiteErrorCode xConnect(
                IntPtr pDb,
                IntPtr pAux,
                int argc,
                [MarshalAs(UnmanagedType.LPArray, SizeParamIndex = 2)]
                ref IntPtr[] argv,
                ref IntPtr pVtab,
                ref IntPtr pError
            );

            [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
            public delegate SQLiteErrorCode xBestIndex(
                IntPtr pVtab,
                IntPtr index
            );

            [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
            public delegate SQLiteErrorCode xDisconnect(
                IntPtr pVtab
            );

            [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
            public delegate SQLiteErrorCode xDestroy(
                IntPtr pVtab
            );

            [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
            public delegate SQLiteErrorCode xOpen(
                IntPtr pVtab,
                ref IntPtr pCursor
            );

            [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
            public delegate SQLiteErrorCode xClose(
                IntPtr pCursor
            );

            [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
            public delegate SQLiteErrorCode xFilter(
                IntPtr pCursor,
                int idxNum,
                IntPtr idxStr,
                int argc,
                IntPtr[] argv
            );

            [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
            public delegate SQLiteErrorCode xNext(
                IntPtr pCursor
            );

            [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
            public delegate SQLiteErrorCode xEof(
                IntPtr pCursor
            );

            [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
            public delegate SQLiteErrorCode xColumn(
                IntPtr pCursor,
                IntPtr pContext,
                int index
            );

            [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
            public delegate SQLiteErrorCode xRowId(
                IntPtr pCursor,
                ref long rowId
            );

            [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
            public delegate SQLiteErrorCode xUpdate(
                IntPtr pVtab,
                int nData,
                ref IntPtr apData,
                ref long rowId
            );

            [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
            public delegate SQLiteErrorCode xBegin(
                IntPtr pVtab
            );

            [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
            public delegate SQLiteErrorCode xSync(
                IntPtr pVtab
            );

            [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
            public delegate SQLiteErrorCode xCommit(
                IntPtr pVtab
            );

            [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
            public delegate SQLiteErrorCode xRollback(
                IntPtr pVtab
            );

            [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
            public delegate SQLiteErrorCode xFindFunction(
                IntPtr pVtab,
                int nArg,
                IntPtr zName,
                ref xFunc pxFunc,
                ref IntPtr ppArg
            );

            [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
            public delegate SQLiteErrorCode xRename(
                IntPtr pVtab,
                IntPtr zNew
            );

            [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
            public delegate SQLiteErrorCode xSavepoint(
                IntPtr pVtab,
                int iSavepoint
            );

            [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
            public delegate SQLiteErrorCode xRelease(
                IntPtr pVtab,
                int iSavepoint
            );

            [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
            public delegate SQLiteErrorCode xRollbackTo(
                IntPtr pVtab,
                int iSavepoint
            );

            private static readonly int SQLITE_INDEX_CONSTRAINT_EQ = 2;
            private static readonly int SQLITE_INDEX_CONSTRAINT_GT = 4;
            private static readonly int SQLITE_INDEX_CONSTRAINT_LE = 8;
            private static readonly int SQLITE_INDEX_CONSTRAINT_LT = 16;
            private static readonly int SQLITE_INDEX_CONSTRAINT_GE = 32;
            private static readonly int SQLITE_INDEX_CONSTRAINT_MATCH = 64;
        }
        #endregion

        ///////////////////////////////////////////////////////////////////////

        #region Private Methods
        private UnsafeNativeMethods2.sqlite3_module CreateNativeModule()
        {
            UnsafeNativeMethods2.sqlite3_module module =
                new UnsafeNativeMethods2.sqlite3_module();

            module.iVersion = 2;
            module.xCreate = new UnsafeNativeMethods2.xCreate(xCreate);
            module.xConnect = new UnsafeNativeMethods2.xConnect(xConnect);
            module.xBestIndex = new UnsafeNativeMethods2.xBestIndex(xBestIndex);
            module.xDisconnect = new UnsafeNativeMethods2.xDisconnect(xDisconnect);
            module.xDestroy = new UnsafeNativeMethods2.xDestroy(xDestroy);
            module.xOpen = new UnsafeNativeMethods2.xOpen(xOpen);
            module.xClose = new UnsafeNativeMethods2.xClose(xClose);
            module.xFilter = new UnsafeNativeMethods2.xFilter(xFilter);
            module.xNext = new UnsafeNativeMethods2.xNext(xNext);
            module.xEof = new UnsafeNativeMethods2.xEof(xEof);
            module.xColumn = new UnsafeNativeMethods2.xColumn(xColumn);
            module.xRowId = new UnsafeNativeMethods2.xRowId(xRowId);
            module.xUpdate = new UnsafeNativeMethods2.xUpdate(xUpdate);
            module.xBegin = new UnsafeNativeMethods2.xBegin(xBegin);
            module.xSync = new UnsafeNativeMethods2.xSync(xSync);
            module.xCommit = new UnsafeNativeMethods2.xCommit(xCommit);
            module.xRollback = new UnsafeNativeMethods2.xRollback(xRollback);
            module.xFindFunction = new UnsafeNativeMethods2.xFindFunction(xFindFunction);
            module.xRename = new UnsafeNativeMethods2.xRename(xRename);
            module.xSavepoint = new UnsafeNativeMethods2.xSavepoint(xSavepoint);
            module.xRelease = new UnsafeNativeMethods2.xRelease(xRelease);
            module.xRollbackTo = new UnsafeNativeMethods2.xRollbackTo(xRollbackTo);

            return module;
        }

        private static int ThirtyBits = 0x3fffffff;

        ///////////////////////////////////////////////////////////////////////
................................................................................
        ///////////////////////////////////////////////////////////////////////

        private static string GetStringFromUtf8Bytes(byte[] bytes)
        {
            if (bytes == null)
                return null;


            return Utf8Encoding.GetString(bytes);



        }

        ///////////////////////////////////////////////////////////////////////

        private static IntPtr Allocate(int size)
        {
            return UnsafeNativeMethods.sqlite3_malloc(size);
................................................................................

        ///////////////////////////////////////////////////////////////////////

        #region Protected Members
        protected virtual IntPtr AllocateTable()
        {
            int size = Marshal.SizeOf(typeof(
                UnsafeNativeMethods2.sqlite3_vtab));

            return Allocate(size);
        }

        ///////////////////////////////////////////////////////////////////////

        protected virtual void FreeTable(IntPtr pVtab)
................................................................................
        }

        ///////////////////////////////////////////////////////////////////////

        protected virtual IntPtr AllocateCursor()
        {
            int size = Marshal.SizeOf(typeof(
                UnsafeNativeMethods2.sqlite3_vtab_cursor));

            return Allocate(size);
        }

        ///////////////////////////////////////////////////////////////////////

        protected virtual IntPtr GetTableFromCursor(
................................................................................
        protected virtual SQLiteVirtualTableCursor MarshalCursorFromIntPtr(
            IntPtr pCursor
            )
        {
            if (pCursor == IntPtr.Zero)
                return null;

            SQLiteVirtualTableCursor result = new SQLiteVirtualTableCursor();


            Marshal.PtrToStructure(pCursor, result.cursor);

            return result;
        }

        ///////////////////////////////////////////////////////////////////////

        protected virtual IntPtr MarshalCursorToIntPtr(
................................................................................

            try
            {
                result = AllocateCursor();

                if (result != IntPtr.Zero)
                {
                    Marshal.StructureToPtr(cursor.cursor, result, false);
                    success = true;
                }
            }
            finally
            {
                if (!success && (result != IntPtr.Zero))
                {
................................................................................
            }

            return sqliteBase.DeclareVirtualTable(this, sql, ref error);
        }

        ///////////////////////////////////////////////////////////////////////













        protected virtual bool SetTableError(
            IntPtr pVtab,
            string error
            )
        {
            if (pVtab == IntPtr.Zero)
                return false;

            int offset = Marshal.SizeOf(typeof(
                UnsafeNativeMethods2.sqlite3_module)) + sizeof(int);


            IntPtr pError = Marshal.ReadIntPtr(pVtab, offset);




            if (pError != IntPtr.Zero)
            {
                Free(pError); pError = IntPtr.Zero;


                Marshal.WriteIntPtr(pVtab, offset, pError);



            }


            Marshal.WriteIntPtr(pVtab, offset, Utf8IntPtrFromString(error));





            return true;
        }
        #endregion

        ///////////////////////////////////////////////////////////////////////

        #region ISQLiteNativeModule Members
................................................................................

        ///////////////////////////////////////////////////////////////////////

        public SQLiteErrorCode xFindFunction(
            IntPtr pVtab,
            int nArg,
            IntPtr zName,
            ref xFunc pxFunc,
            ref IntPtr ppArg
            )
        {
            return SQLiteErrorCode.Ok;
        }

        ///////////////////////////////////////////////////////////////////////







>

>
>
>







 







>

>
>
>







 







>
>

>
>
>
>
>







>
>

>
>
>
>
>







 







|






|






|







 







|
>
>
>
>
|

|

<
<
<
<
|
|
<
|
>
>
>
>




>
>







 







|







 







|
<
<
<
<
|



<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<



|

|
|


|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|







 







>

>
>
>







 







|







 







|







 







|
>
>
|







 







|







 







>
>
>
>
>
>
>
>
>
>
>
>









|

>

>
>
>




>
>

>
>
>


>

>
>
>
>
>







 







|







47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
..
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
...
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
...
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
...
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
...
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
...
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
...
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
...
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
...
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
...
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
...
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
...
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
....
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
        ///////////////////////////////////////////////////////////////////////

        public void SetDouble(double value)
        {
            if (pContext == IntPtr.Zero)
                throw new InvalidOperationException();

#if !PLATFORM_COMPACTFRAMEWORK
            UnsafeNativeMethods.sqlite3_result_double(pContext, value);
#else
            UnsafeNativeMethods.sqlite3_result_double_interop(pContext, ref value);
#endif
        }

        ///////////////////////////////////////////////////////////////////////

        public void SetInt(int value)
        {
            if (pContext == IntPtr.Zero)
................................................................................
        ///////////////////////////////////////////////////////////////////////

        public void SetInt64(long value)
        {
            if (pContext == IntPtr.Zero)
                throw new InvalidOperationException();

#if !PLATFORM_COMPACTFRAMEWORK
            UnsafeNativeMethods.sqlite3_result_int64(pContext, value);
#else
            UnsafeNativeMethods.sqlite3_result_int64_interop(pContext, ref value);
#endif
        }

        ///////////////////////////////////////////////////////////////////////

        public void SetString(string value)
        {
            if (pContext == IntPtr.Zero)
................................................................................
        }

        ///////////////////////////////////////////////////////////////////////

        public long GetInt64()
        {
            if (pValue == IntPtr.Zero) return default(long);

#if !PLATFORM_COMPACTFRAMEWORK
            return UnsafeNativeMethods.sqlite3_value_int64(pValue);
#else
            long value;
            UnsafeNativeMethods.sqlite3_value_int64_interop(pValue, out value);
            return value;
#endif
        }

        ///////////////////////////////////////////////////////////////////////

        public double GetDouble()
        {
            if (pValue == IntPtr.Zero) return default(double);

#if !PLATFORM_COMPACTFRAMEWORK
            return UnsafeNativeMethods.sqlite3_value_double(pValue);
#else
            double value;
            UnsafeNativeMethods.sqlite3_value_double_interop(pValue, out value);
            return value;
#endif
        }

        ///////////////////////////////////////////////////////////////////////

        public string GetString()
        {
            if (pValue == IntPtr.Zero) return null;
................................................................................
    }
    #endregion

    ///////////////////////////////////////////////////////////////////////////

    public class SQLiteIndexConstraint
    {
        private UnsafeNativeMethods.sqlite3_index_constraint constraint;
    }

    ///////////////////////////////////////////////////////////////////////////

    public class SQLiteIndexOrderBy
    {
        private UnsafeNativeMethods.sqlite3_index_orderby orderBy;
    }

    ///////////////////////////////////////////////////////////////////////////

    public class SQLiteIndexConstraintUsage
    {
        private UnsafeNativeMethods.sqlite3_index_constraint_usage constraintUsage;
    }

    ///////////////////////////////////////////////////////////////////////////

    public class SQLiteIndex
    {
        SQLiteIndexConstraint[] Constraints;
................................................................................
        double estimatedCost; /* Estimated cost of using this index */
    }

    ///////////////////////////////////////////////////////////////////////////

    public class SQLiteVirtualTableCursor
    {
        public SQLiteVirtualTableCursor(
            IntPtr nativeHandle
            )
        {
            this.nativeHandle = nativeHandle;
        }

        ///////////////////////////////////////////////////////////////////////





        private IntPtr nativeHandle;
        public IntPtr NativeHandle

        {
            get { return nativeHandle; }
        }
    }

    ///////////////////////////////////////////////////////////////////////////

    public interface ISQLiteNativeModule
    {
        // https://www.sqlite.org/vtab.html

        SQLiteErrorCode xCreate(IntPtr pDb, IntPtr pAux, int argc, ref IntPtr[] argv, ref IntPtr pVtab, ref IntPtr pError);
        SQLiteErrorCode xConnect(IntPtr pDb, IntPtr pAux, int argc, ref IntPtr[] argv, ref IntPtr pVtab, ref IntPtr pError);
        SQLiteErrorCode xBestIndex(IntPtr pVtab, IntPtr index);
        SQLiteErrorCode xDisconnect(IntPtr pVtab);
        SQLiteErrorCode xDestroy(IntPtr pVtab);
        SQLiteErrorCode xOpen(IntPtr pVtab, ref IntPtr pCursor);
        SQLiteErrorCode xClose(IntPtr pCursor);
................................................................................
        SQLiteErrorCode xColumn(IntPtr pCursor, IntPtr pContext, int index);
        SQLiteErrorCode xRowId(IntPtr pCursor, ref long rowId);
        SQLiteErrorCode xUpdate(IntPtr pVtab, int nData, ref IntPtr apData, ref long rowId);
        SQLiteErrorCode xBegin(IntPtr pVtab);
        SQLiteErrorCode xSync(IntPtr pVtab);
        SQLiteErrorCode xCommit(IntPtr pVtab);
        SQLiteErrorCode xRollback(IntPtr pVtab);
        SQLiteErrorCode xFindFunction(IntPtr pVtab, int nArg, IntPtr zName, ref IntPtr pxFunc, ref IntPtr ppArg);
        SQLiteErrorCode xRename(IntPtr pVtab, IntPtr zNew);
        SQLiteErrorCode xSavepoint(IntPtr pVtab, int iSavepoint);
        SQLiteErrorCode xRelease(IntPtr pVtab, int iSavepoint);
        SQLiteErrorCode xRollbackTo(IntPtr pVtab, int iSavepoint);
    }

    ///////////////////////////////////////////////////////////////////////////
................................................................................
        SQLiteErrorCode Savepoint(int iSavepoint);
        SQLiteErrorCode Release(int iSavepoint);
        SQLiteErrorCode RollbackTo(int iSavepoint);
    }

    ///////////////////////////////////////////////////////////////////////////

    public abstract class SQLiteModuleBase :




            ISQLiteManagedModule, ISQLiteNativeModule,  IDisposable
    {
        private static Encoding Utf8Encoding = Encoding.UTF8;


























































































































































































































































        ///////////////////////////////////////////////////////////////////////

        #region Private Methods
        private UnsafeNativeMethods.sqlite3_module CreateNativeModule()
        {
            UnsafeNativeMethods.sqlite3_module module =
                new UnsafeNativeMethods.sqlite3_module();

            module.iVersion = 2;
            module.xCreate = new UnsafeNativeMethods.xCreate(xCreate);
            module.xConnect = new UnsafeNativeMethods.xConnect(xConnect);
            module.xBestIndex = new UnsafeNativeMethods.xBestIndex(xBestIndex);
            module.xDisconnect = new UnsafeNativeMethods.xDisconnect(xDisconnect);
            module.xDestroy = new UnsafeNativeMethods.xDestroy(xDestroy);
            module.xOpen = new UnsafeNativeMethods.xOpen(xOpen);
            module.xClose = new UnsafeNativeMethods.xClose(xClose);
            module.xFilter = new UnsafeNativeMethods.xFilter(xFilter);
            module.xNext = new UnsafeNativeMethods.xNext(xNext);
            module.xEof = new UnsafeNativeMethods.xEof(xEof);
            module.xColumn = new UnsafeNativeMethods.xColumn(xColumn);
            module.xRowId = new UnsafeNativeMethods.xRowId(xRowId);
            module.xUpdate = new UnsafeNativeMethods.xUpdate(xUpdate);
            module.xBegin = new UnsafeNativeMethods.xBegin(xBegin);
            module.xSync = new UnsafeNativeMethods.xSync(xSync);
            module.xCommit = new UnsafeNativeMethods.xCommit(xCommit);
            module.xRollback = new UnsafeNativeMethods.xRollback(xRollback);
            module.xFindFunction = new UnsafeNativeMethods.xFindFunction(xFindFunction);
            module.xRename = new UnsafeNativeMethods.xRename(xRename);
            module.xSavepoint = new UnsafeNativeMethods.xSavepoint(xSavepoint);
            module.xRelease = new UnsafeNativeMethods.xRelease(xRelease);
            module.xRollbackTo = new UnsafeNativeMethods.xRollbackTo(xRollbackTo);

            return module;
        }

        private static int ThirtyBits = 0x3fffffff;

        ///////////////////////////////////////////////////////////////////////
................................................................................
        ///////////////////////////////////////////////////////////////////////

        private static string GetStringFromUtf8Bytes(byte[] bytes)
        {
            if (bytes == null)
                return null;

#if !PLATFORM_COMPACTFRAMEWORK
            return Utf8Encoding.GetString(bytes);
#else
            return Utf8Encoding.GetString(bytes, 0, bytes.Length);
#endif
        }

        ///////////////////////////////////////////////////////////////////////

        private static IntPtr Allocate(int size)
        {
            return UnsafeNativeMethods.sqlite3_malloc(size);
................................................................................

        ///////////////////////////////////////////////////////////////////////

        #region Protected Members
        protected virtual IntPtr AllocateTable()
        {
            int size = Marshal.SizeOf(typeof(
                UnsafeNativeMethods.sqlite3_vtab));

            return Allocate(size);
        }

        ///////////////////////////////////////////////////////////////////////

        protected virtual void FreeTable(IntPtr pVtab)
................................................................................
        }

        ///////////////////////////////////////////////////////////////////////

        protected virtual IntPtr AllocateCursor()
        {
            int size = Marshal.SizeOf(typeof(
                UnsafeNativeMethods.sqlite3_vtab_cursor));

            return Allocate(size);
        }

        ///////////////////////////////////////////////////////////////////////

        protected virtual IntPtr GetTableFromCursor(
................................................................................
        protected virtual SQLiteVirtualTableCursor MarshalCursorFromIntPtr(
            IntPtr pCursor
            )
        {
            if (pCursor == IntPtr.Zero)
                return null;

            SQLiteVirtualTableCursor result =
                new SQLiteVirtualTableCursor(pCursor);

            // Marshal.PtrToStructure(pCursor, result.cursor);

            return result;
        }

        ///////////////////////////////////////////////////////////////////////

        protected virtual IntPtr MarshalCursorToIntPtr(
................................................................................

            try
            {
                result = AllocateCursor();

                if (result != IntPtr.Zero)
                {
                    // Marshal.StructureToPtr(cursor.cursor, result, false);
                    success = true;
                }
            }
            finally
            {
                if (!success && (result != IntPtr.Zero))
                {
................................................................................
            }

            return sqliteBase.DeclareVirtualTable(this, sql, ref error);
        }

        ///////////////////////////////////////////////////////////////////////

#if PLATFORM_COMPACTFRAMEWORK
        protected virtual IntPtr IntPtrForOffset(
            IntPtr pointer,
            int offset
            )
        {
            return new IntPtr(pointer.ToInt64() + offset);
        }
#endif

        ///////////////////////////////////////////////////////////////////////

        protected virtual bool SetTableError(
            IntPtr pVtab,
            string error
            )
        {
            if (pVtab == IntPtr.Zero)
                return false;

            int offset = Marshal.SizeOf(typeof(
                UnsafeNativeMethods.sqlite3_module)) + sizeof(int);

#if !PLATFORM_COMPACTFRAMEWORK
            IntPtr pError = Marshal.ReadIntPtr(pVtab, offset);
#else
            IntPtr pError = Marshal.ReadIntPtr(IntPtrForOffset(pVtab, offset));
#endif

            if (pError != IntPtr.Zero)
            {
                Free(pError); pError = IntPtr.Zero;

#if !PLATFORM_COMPACTFRAMEWORK
                Marshal.WriteIntPtr(pVtab, offset, pError);
#else
                Marshal.WriteIntPtr(IntPtrForOffset(pVtab, offset), pError);
#endif
            }

#if !PLATFORM_COMPACTFRAMEWORK
            Marshal.WriteIntPtr(pVtab, offset, Utf8IntPtrFromString(error));
#else
            Marshal.WriteIntPtr(IntPtrForOffset(pVtab, offset),
                Utf8IntPtrFromString(error));
#endif

            return true;
        }
        #endregion

        ///////////////////////////////////////////////////////////////////////

        #region ISQLiteNativeModule Members
................................................................................

        ///////////////////////////////////////////////////////////////////////

        public SQLiteErrorCode xFindFunction(
            IntPtr pVtab,
            int nArg,
            IntPtr zName,
            ref IntPtr pxFunc,
            ref IntPtr ppArg
            )
        {
            return SQLiteErrorCode.Ok;
        }

        ///////////////////////////////////////////////////////////////////////

Changes to System.Data.SQLite/System.Data.SQLite.Files.targets.

39
40
41
42
43
44
45


46
47
48
49
50
51
52
..
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
    <Compile Include="SQLiteException.cs" />
    <Compile Include="SQLiteFactory.cs" />
    <Compile Include="SQLiteFunction.cs" />
    <Compile Include="SQLiteFunctionAttribute.cs" />
    <Compile Include="SQLiteKeyReader.cs" />
    <Compile Include="SQLiteLog.cs" />
    <Compile Include="SQLiteMetaDataCollectionNames.cs" />


    <Compile Include="SQLiteParameter.cs" />
    <Compile Include="SQLiteParameterCollection.cs" />
    <Compile Include="SQLiteStatement.cs" />
    <Compile Include="SQLiteTransaction.cs" />
    <Compile Include="SR.Designer.cs">
      <DependentUpon>SR.resx</DependentUpon>
      <AutoGen>True</AutoGen>
................................................................................
  ******************************************************************************
  **                        Core Files (Full Framework)                       **
  ******************************************************************************
  -->

  <ItemGroup Condition="'$(IsCompactFramework)' == 'false'">
    <Compile Include="SQLiteEnlistment.cs" />
    <Compile Include="SQLiteModuleBase.cs" />
    <Compile Include="SQLiteModuleNoop.cs" />
    <Compile Condition="'$(NetFx35)' != 'false' Or
                        '$(NetFx40)' != 'false' Or
                        '$(NetFx45)' != 'false'"
             Include="LINQ\SQLiteConnection_Linq.cs">
      <SubType>Component</SubType>
    </Compile>
    <Compile Condition="'$(NetFx35)' != 'false' Or







>
>







 







<
<







39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
..
66
67
68
69
70
71
72


73
74
75
76
77
78
79
    <Compile Include="SQLiteException.cs" />
    <Compile Include="SQLiteFactory.cs" />
    <Compile Include="SQLiteFunction.cs" />
    <Compile Include="SQLiteFunctionAttribute.cs" />
    <Compile Include="SQLiteKeyReader.cs" />
    <Compile Include="SQLiteLog.cs" />
    <Compile Include="SQLiteMetaDataCollectionNames.cs" />
    <Compile Include="SQLiteModuleBase.cs" />
    <Compile Include="SQLiteModuleNoop.cs" />
    <Compile Include="SQLiteParameter.cs" />
    <Compile Include="SQLiteParameterCollection.cs" />
    <Compile Include="SQLiteStatement.cs" />
    <Compile Include="SQLiteTransaction.cs" />
    <Compile Include="SR.Designer.cs">
      <DependentUpon>SR.resx</DependentUpon>
      <AutoGen>True</AutoGen>
................................................................................
  ******************************************************************************
  **                        Core Files (Full Framework)                       **
  ******************************************************************************
  -->

  <ItemGroup Condition="'$(IsCompactFramework)' == 'false'">
    <Compile Include="SQLiteEnlistment.cs" />


    <Compile Condition="'$(NetFx35)' != 'false' Or
                        '$(NetFx40)' != 'false' Or
                        '$(NetFx45)' != 'false'"
             Include="LINQ\SQLiteConnection_Linq.cs">
      <SubType>Component</SubType>
    </Compile>
    <Compile Condition="'$(NetFx35)' != 'false' Or

Changes to System.Data.SQLite/UnsafeNativeMethods.cs.

1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
....
1617
1618
1619
1620
1621
1622
1623











































































































































































































































































































































































1624
1625
1626
1627
1628
1629
1630
#if !PLATFORM_COMPACTFRAMEWORK
    [DllImport(SQLITE_DLL, CallingConvention = CallingConvention.Cdecl)]
#else
    [DllImport(SQLITE_DLL)]
#endif
    internal static extern int sqlite3_backup_pagecount(IntPtr backup);

#if !PLATFORM_COMPACTFRAMEWORK
    [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
#endif
    public delegate void xDestroyModule(IntPtr pClientData);

#if !PLATFORM_COMPACTFRAMEWORK
    [DllImport(SQLITE_DLL, CallingConvention = CallingConvention.Cdecl)]
#else
    [DllImport(SQLITE_DLL)]
#endif
    internal static extern SQLiteErrorCode sqlite3_create_module_v2(IntPtr db, IntPtr name, IntPtr pModule, IntPtr pClientData, xDestroyModule xDestroy);

................................................................................
#if !PLATFORM_COMPACTFRAMEWORK
    [DllImport(SQLITE_DLL, CallingConvention = CallingConvention.Cdecl)]
#else
    [DllImport(SQLITE_DLL)]
#endif
    internal static extern IntPtr sqlite3_mprintf(IntPtr format, __arglist);
    #endregion











































































































































































































































































































































































  }

#if PLATFORM_COMPACTFRAMEWORK
  internal abstract class CriticalHandle : IDisposable
  {
    private bool _isClosed;
    protected IntPtr handle;







<
<
<
<
<







 







>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>







1591
1592
1593
1594
1595
1596
1597





1598
1599
1600
1601
1602
1603
1604
....
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
#if !PLATFORM_COMPACTFRAMEWORK
    [DllImport(SQLITE_DLL, CallingConvention = CallingConvention.Cdecl)]
#else
    [DllImport(SQLITE_DLL)]
#endif
    internal static extern int sqlite3_backup_pagecount(IntPtr backup);






#if !PLATFORM_COMPACTFRAMEWORK
    [DllImport(SQLITE_DLL, CallingConvention = CallingConvention.Cdecl)]
#else
    [DllImport(SQLITE_DLL)]
#endif
    internal static extern SQLiteErrorCode sqlite3_create_module_v2(IntPtr db, IntPtr name, IntPtr pModule, IntPtr pClientData, xDestroyModule xDestroy);

................................................................................
#if !PLATFORM_COMPACTFRAMEWORK
    [DllImport(SQLITE_DLL, CallingConvention = CallingConvention.Cdecl)]
#else
    [DllImport(SQLITE_DLL)]
#endif
    internal static extern IntPtr sqlite3_mprintf(IntPtr format, __arglist);
    #endregion

    ///////////////////////////////////////////////////////////////////////////

    #region Native Delegates
#if !PLATFORM_COMPACTFRAMEWORK
    [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
#endif
    public delegate SQLiteErrorCode xCreate(
        IntPtr pDb,
        IntPtr pAux,
        int argc,
        [MarshalAs(UnmanagedType.LPArray, SizeParamIndex = 2)]
        ref IntPtr[] argv,
        ref IntPtr pVtab,
        ref IntPtr pError
    );

    ///////////////////////////////////////////////////////////////////////////

#if !PLATFORM_COMPACTFRAMEWORK
    [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
#endif
    public delegate SQLiteErrorCode xConnect(
        IntPtr pDb,
        IntPtr pAux,
        int argc,
        [MarshalAs(UnmanagedType.LPArray, SizeParamIndex = 2)]
        ref IntPtr[] argv,
        ref IntPtr pVtab,
        ref IntPtr pError
    );

    ///////////////////////////////////////////////////////////////////////////

#if !PLATFORM_COMPACTFRAMEWORK
    [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
#endif
    public delegate SQLiteErrorCode xBestIndex(
        IntPtr pVtab,
        IntPtr index
    );

    ///////////////////////////////////////////////////////////////////////////

#if !PLATFORM_COMPACTFRAMEWORK
    [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
#endif
    public delegate SQLiteErrorCode xDisconnect(
        IntPtr pVtab
    );

    ///////////////////////////////////////////////////////////////////////////

#if !PLATFORM_COMPACTFRAMEWORK
    [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
#endif
    public delegate SQLiteErrorCode xDestroy(
        IntPtr pVtab
    );

    ///////////////////////////////////////////////////////////////////////////

#if !PLATFORM_COMPACTFRAMEWORK
    [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
#endif
    public delegate SQLiteErrorCode xOpen(
        IntPtr pVtab,
        ref IntPtr pCursor
    );

    ///////////////////////////////////////////////////////////////////////////

#if !PLATFORM_COMPACTFRAMEWORK
    [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
#endif
    public delegate SQLiteErrorCode xClose(
        IntPtr pCursor
    );

    ///////////////////////////////////////////////////////////////////////////

#if !PLATFORM_COMPACTFRAMEWORK
    [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
#endif
    public delegate SQLiteErrorCode xFilter(
        IntPtr pCursor,
        int idxNum,
        IntPtr idxStr,
        int argc,
        IntPtr[] argv
    );

    ///////////////////////////////////////////////////////////////////////////

#if !PLATFORM_COMPACTFRAMEWORK
    [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
#endif
    public delegate SQLiteErrorCode xNext(
        IntPtr pCursor
    );

    ///////////////////////////////////////////////////////////////////////////

#if !PLATFORM_COMPACTFRAMEWORK
    [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
#endif
    public delegate SQLiteErrorCode xEof(
        IntPtr pCursor
    );

    ///////////////////////////////////////////////////////////////////////////

#if !PLATFORM_COMPACTFRAMEWORK
    [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
#endif
    public delegate SQLiteErrorCode xColumn(
        IntPtr pCursor,
        IntPtr pContext,
        int index
    );

    ///////////////////////////////////////////////////////////////////////////

#if !PLATFORM_COMPACTFRAMEWORK
    [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
#endif
    public delegate SQLiteErrorCode xRowId(
        IntPtr pCursor,
        ref long rowId
    );

    ///////////////////////////////////////////////////////////////////////////

#if !PLATFORM_COMPACTFRAMEWORK
    [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
#endif
    public delegate SQLiteErrorCode xUpdate(
        IntPtr pVtab,
        int nData,
        ref IntPtr apData,
        ref long rowId
    );

    ///////////////////////////////////////////////////////////////////////////

#if !PLATFORM_COMPACTFRAMEWORK
    [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
#endif
    public delegate SQLiteErrorCode xBegin(
        IntPtr pVtab
    );

    ///////////////////////////////////////////////////////////////////////////

#if !PLATFORM_COMPACTFRAMEWORK
    [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
#endif
    public delegate SQLiteErrorCode xSync(
        IntPtr pVtab
    );

    ///////////////////////////////////////////////////////////////////////////

#if !PLATFORM_COMPACTFRAMEWORK
    [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
#endif
    public delegate SQLiteErrorCode xCommit(
        IntPtr pVtab
    );

    ///////////////////////////////////////////////////////////////////////////

#if !PLATFORM_COMPACTFRAMEWORK
    [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
#endif
    public delegate SQLiteErrorCode xRollback(
        IntPtr pVtab
    );

    ///////////////////////////////////////////////////////////////////////////

#if !PLATFORM_COMPACTFRAMEWORK
    [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
#endif
    public delegate SQLiteErrorCode xFindFunction(
        IntPtr pVtab,
        int nArg,
        IntPtr zName,
        ref IntPtr pxFunc,
        ref IntPtr ppArg
    );

    ///////////////////////////////////////////////////////////////////////////

#if !PLATFORM_COMPACTFRAMEWORK
    [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
#endif
    public delegate SQLiteErrorCode xRename(
        IntPtr pVtab,
        IntPtr zNew
    );

    ///////////////////////////////////////////////////////////////////////////

#if !PLATFORM_COMPACTFRAMEWORK
    [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
#endif
    public delegate SQLiteErrorCode xSavepoint(
        IntPtr pVtab,
        int iSavepoint
    );

    ///////////////////////////////////////////////////////////////////////////

#if !PLATFORM_COMPACTFRAMEWORK
    [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
#endif
    public delegate SQLiteErrorCode xRelease(
        IntPtr pVtab,
        int iSavepoint
    );

    ///////////////////////////////////////////////////////////////////////////

#if !PLATFORM_COMPACTFRAMEWORK
    [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
#endif
    public delegate SQLiteErrorCode xRollbackTo(
        IntPtr pVtab,
        int iSavepoint
    );

    ///////////////////////////////////////////////////////////////////////////

#if !PLATFORM_COMPACTFRAMEWORK
    [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
#endif
    public delegate int xFunc(
        IntPtr pContext,
        int argc,
        [MarshalAs(UnmanagedType.LPArray, SizeParamIndex = 1)]
        IntPtr[] argv
    );

    ///////////////////////////////////////////////////////////////////////////

#if !PLATFORM_COMPACTFRAMEWORK
    [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
#endif
    public delegate void xDestroyModule(IntPtr pClientData);
    #endregion

    ///////////////////////////////////////////////////////////////////////////

    #region Native Structures
    [StructLayout(LayoutKind.Sequential)]
    internal struct sqlite3_module
    {
        public int iVersion;
        public xCreate xCreate;
        public xConnect xConnect;
        public xBestIndex xBestIndex;
        public xDisconnect xDisconnect;
        public xDestroy xDestroy;
        public xOpen xOpen;
        public xClose xClose;
        public xFilter xFilter;
        public xNext xNext;
        public xEof xEof;
        public xColumn xColumn;
        public xRowId xRowId;
        public xUpdate xUpdate;
        public xBegin xBegin;
        public xSync xSync;
        public xCommit xCommit;
        public xRollback xRollback;
        public xFindFunction xFindFunction;
        public xRename xRename;
        /* The methods above are in version 1 of the sqlite3_module
         * object.  Those below are for version 2 and greater. */
        public xSavepoint xSavepoint;
        public xRelease xRelease;
        public xRollbackTo xRollbackTo;
    }

    ///////////////////////////////////////////////////////////////////////////

    [StructLayout(LayoutKind.Sequential)]
    internal struct sqlite3_vtab
    {
        [MarshalAs(UnmanagedType.LPStruct)]
        public sqlite3_module pModule;
        public int nRef; /* NO LONGER USED */
        public IntPtr zErrMsg;
    }

    ///////////////////////////////////////////////////////////////////////////

    [StructLayout(LayoutKind.Sequential)]
    internal struct sqlite3_vtab_cursor
    {
        [MarshalAs(UnmanagedType.LPStruct)]
        public sqlite3_vtab pVTab;
    }

    ///////////////////////////////////////////////////////////////////////////

    [StructLayout(LayoutKind.Sequential)]
    internal struct sqlite3_index_constraint
    {
        public int iColumn;
        public byte op;
        public byte usable;
        public int iTermOffset;
    }

    ///////////////////////////////////////////////////////////////////////////

    [StructLayout(LayoutKind.Sequential)]
    internal struct sqlite3_index_orderby
    {
        public int iColumn; /* Column number */
        public byte desc;   /* True for DESC.  False for ASC. */
    }

    ///////////////////////////////////////////////////////////////////////////

    [StructLayout(LayoutKind.Sequential)]
    internal struct sqlite3_index_constraint_usage
    {
        public int argvIndex; /* if >0, constraint is part of argv to xFilter */
        public byte omit;     /* Do not code a test for this constraint */
    }

    ///////////////////////////////////////////////////////////////////////////

    [StructLayout(LayoutKind.Sequential)]
    internal struct sqlite3_index_info
    {
        /* Inputs */
        public int nConstraint; /* Number of entries in aConstraint */
        [MarshalAs(UnmanagedType.LPArray, SizeParamIndex = 0)]
        public sqlite3_index_constraint[] aConstraint;
        public int nOrderBy;
        public sqlite3_index_orderby[] aOrderBy;
        /* Outputs */
        public sqlite3_index_constraint_usage[] aConstraintUsage;
        public int idxNum;           /* Number used to identify the index */
        public string idxStr;        /* String, possibly obtained from sqlite3_malloc */
        public int needToFreeIdxStr; /* Free idxStr using sqlite3_free() if true */
        public int orderByConsumed;  /* True if output is already ordered */
        public double estimatedCost; /* Estimated cost of using this index */
    }
    #endregion

    ///////////////////////////////////////////////////////////////////////////

    private static readonly int SQLITE_INDEX_CONSTRAINT_EQ = 2;
    private static readonly int SQLITE_INDEX_CONSTRAINT_GT = 4;
    private static readonly int SQLITE_INDEX_CONSTRAINT_LE = 8;
    private static readonly int SQLITE_INDEX_CONSTRAINT_LT = 16;
    private static readonly int SQLITE_INDEX_CONSTRAINT_GE = 32;
    private static readonly int SQLITE_INDEX_CONSTRAINT_MATCH = 64;
  }

#if PLATFORM_COMPACTFRAMEWORK
  internal abstract class CriticalHandle : IDisposable
  {
    private bool _isClosed;
    protected IntPtr handle;