System.Data.SQLite
Check-in [b6c0bcadb3]
Not logged in

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

Overview
Comment:Support creating a SQLiteConnection instance from a pre-existing native connection handle (i.e. one that cannot be disposed).
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | virtualTables
Files: files | file ages | folders
SHA1: b6c0bcadb303dd609e5ef8b1a2b5df3334936e68
User & Date: mistachkin 2013-06-06 19:36:14
Context
2013-06-07
00:04
Add initial marshalling infrastructure and start implementing the xCreate and xConnect virtual table methods. check-in: bf5505be7a user: mistachkin tags: virtualTables
2013-06-06
19:36
Support creating a SQLiteConnection instance from a pre-existing native connection handle (i.e. one that cannot be disposed). check-in: b6c0bcadb3 user: mistachkin tags: virtualTables
2013-06-03
01:57
Initial phase of experimental support for implementing virtual tables in managed code. No docs, no tests, does not work yet, and may not even compile. check-in: 273b0c601f user: mistachkin tags: virtualTables
Changes
Hide Diffs Unified Diffs Ignore Whitespace Patch

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

68
69
70
71
72
73
74






75
76
77
78
79


80
81
82

83
84
85
86
87
88
89
...
129
130
131
132
133
134
135



136
137
138
139
140
141
142
...
362
363
364
365
366
367
368

369
370
371
372
373
374
375
376
    protected bool _usePool;
    protected int _poolVersion;

#if (NET_35 || NET_40 || NET_45) && !PLATFORM_COMPACTFRAMEWORK
    private bool _buildingSchema;
#endif







    /// <summary>
    /// The user-defined functions registered on this connection
    /// </summary>
    protected SQLiteFunction[] _functionsArray;



    internal SQLite3(SQLiteDateFormats fmt, DateTimeKind kind, string fmtString)
      : base(fmt, kind, fmtString)
    {

    }

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

    #region IDisposable "Pattern" Members
    private bool disposed;
    private void CheckDisposed() /* throw */
................................................................................

    // It isn't necessary to cleanup any functions we've registered.  If the connection
    // goes to the pool and is resurrected later, re-registered functions will overwrite the
    // previous functions.  The SQLiteFunctionCookieHandle will take care of freeing unmanaged
    // resources belonging to the previously-registered functions.
    internal override void Close(bool canThrow)
    {



      if (_sql != null)
      {
          if (_usePool)
          {
              if (SQLiteBase.ResetConnection(_sql, _sql, canThrow))
              {
                  SQLiteConnectionPool.Add(_fileName, _sql, _poolVersion);
................................................................................
          }

#if !NET_COMPACT_20 && TRACE_CONNECTION
          Trace.WriteLine(String.Format("Open: {0}", db));
#endif

          if (n != SQLiteErrorCode.Ok) throw new SQLiteException(n, null);

          _sql = new SQLiteConnectionHandle(db);
        }
        lock (_sql) { /* HACK: Force the SyncBlock to be "created" now. */ }
      }
      // Bind functions to this connection.  If any previous functions of the same name
      // were already bound, then the new bindings replace the old.
      _functionsArray = SQLiteFunction.BindFunctions(this, connectionFlags);
      SetTimeout(0);







>
>
>
>
>
>





>
>
|


>







 







>
>
>







 







>
|







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
...
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
...
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
    protected bool _usePool;
    protected int _poolVersion;

#if (NET_35 || NET_40 || NET_45) && !PLATFORM_COMPACTFRAMEWORK
    private bool _buildingSchema;
#endif

    /// <summary>
    /// This field will be non-zero if this instance owns the native connection
    /// handle and should dispose of it when it is no longer needed.
    /// </summary>
    protected bool _ownHandle;

    /// <summary>
    /// The user-defined functions registered on this connection
    /// </summary>
    protected SQLiteFunction[] _functionsArray;

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

    internal SQLite3(SQLiteDateFormats fmt, DateTimeKind kind, string fmtString, bool ownHandle)
      : base(fmt, kind, fmtString)
    {
        _ownHandle = ownHandle;
    }

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

    #region IDisposable "Pattern" Members
    private bool disposed;
    private void CheckDisposed() /* throw */
................................................................................

    // It isn't necessary to cleanup any functions we've registered.  If the connection
    // goes to the pool and is resurrected later, re-registered functions will overwrite the
    // previous functions.  The SQLiteFunctionCookieHandle will take care of freeing unmanaged
    // resources belonging to the previously-registered functions.
    internal override void Close(bool canThrow)
    {
      if (!_ownHandle)
        return;

      if (_sql != null)
      {
          if (_usePool)
          {
              if (SQLiteBase.ResetConnection(_sql, _sql, canThrow))
              {
                  SQLiteConnectionPool.Add(_fileName, _sql, _poolVersion);
................................................................................
          }

#if !NET_COMPACT_20 && TRACE_CONNECTION
          Trace.WriteLine(String.Format("Open: {0}", db));
#endif

          if (n != SQLiteErrorCode.Ok) throw new SQLiteException(n, null);
          _ownHandle = true;
          _sql = new SQLiteConnectionHandle(db, _ownHandle);
        }
        lock (_sql) { /* HACK: Force the SyncBlock to be "created" now. */ }
      }
      // Bind functions to this connection.  If any previous functions of the same name
      // were already bound, then the new bindings replace the old.
      _functionsArray = SQLiteFunction.BindFunctions(this, connectionFlags);
      SetTimeout(0);

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

17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
...
139
140
141
142
143
144
145

146
147
148
149
150
151
152
153
  using System.Runtime.InteropServices;

  /// <summary>
  /// Alternate SQLite3 object, overriding many text behaviors to support UTF-16 (Unicode)
  /// </summary>
  internal sealed class SQLite3_UTF16 : SQLite3
  {
    internal SQLite3_UTF16(SQLiteDateFormats fmt, DateTimeKind kind, string fmtString)
      : base(fmt, kind, fmtString)
    {
    }

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

    #region IDisposable "Pattern" Members
    private bool disposed;
................................................................................
          }

#if !NET_COMPACT_20 && TRACE_CONNECTION
          Trace.WriteLine(String.Format("Open16: {0}", db));
#endif

          if (n != SQLiteErrorCode.Ok) throw new SQLiteException(n, null);

          _sql = new SQLiteConnectionHandle(db);
        }
        lock (_sql) { /* HACK: Force the SyncBlock to be "created" now. */ }
      }
      _functionsArray = SQLiteFunction.BindFunctions(this, connectionFlags);
      SetTimeout(0);
      GC.KeepAlive(_sql);
    }







|
|







 







>
|







17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
...
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
  using System.Runtime.InteropServices;

  /// <summary>
  /// Alternate SQLite3 object, overriding many text behaviors to support UTF-16 (Unicode)
  /// </summary>
  internal sealed class SQLite3_UTF16 : SQLite3
  {
    internal SQLite3_UTF16(SQLiteDateFormats fmt, DateTimeKind kind, string fmtString, bool ownHandle)
      : base(fmt, kind, fmtString, ownHandle)
    {
    }

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

    #region IDisposable "Pattern" Members
    private bool disposed;
................................................................................
          }

#if !NET_COMPACT_20 && TRACE_CONNECTION
          Trace.WriteLine(String.Format("Open16: {0}", db));
#endif

          if (n != SQLiteErrorCode.Ok) throw new SQLiteException(n, null);
          base._ownHandle = true;
          _sql = new SQLiteConnectionHandle(db, base._ownHandle);
        }
        lock (_sql) { /* HACK: Force the SyncBlock to be "created" now. */ }
      }
      _functionsArray = SQLiteFunction.BindFunctions(this, connectionFlags);
      SetTimeout(0);
      GC.KeepAlive(_sql);
    }

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

399
400
401
402
403
404
405







406
407
408
409
410
411
412
...
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
...
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
...
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
....
1899
1900
1901
1902
1903
1904
1905


1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
....
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352

#if !PLATFORM_COMPACTFRAMEWORK
    /// <summary>
    /// Whether or not the connection is enlisted in a distrubuted transaction
    /// </summary>
    internal SQLiteEnlistment _enlistment;
#endif







    /// <summary>
    /// The base SQLite object to interop with
    /// </summary>
    internal SQLiteBase _sql;
    /// <summary>
    /// The database filename minus path and extension
    /// </summary>
................................................................................
    ///<overloads>
    /// Constructs a new SQLiteConnection object
    /// </overloads>
    /// <summary>
    /// Default constructor
    /// </summary>
    public SQLiteConnection()
      : this("")
    {
    }

    /// <summary>
    /// Initializes the connection with the specified connection string.
    /// </summary>
    /// <param name="connectionString">The connection string to use.</param>
    public SQLiteConnection(string connectionString)
        : this(connectionString, false)
    {
        // do nothing.
    }





















    /// <summary>
    /// Initializes the connection with the specified connection string.
    /// </summary>
    /// <param name="connectionString">
    /// The connection string to use on.
    /// </param>
    /// <param name="parseViaFramework">
    /// Non-zero to parse the connection string using the built-in (i.e.
    /// framework provided) parser when opening the connection.
    /// </param>
    public SQLiteConnection(string connectionString, bool parseViaFramework)
    {
................................................................................
      {
          UnsafeNativeMethods.sqlite3_log(
              SQLiteErrorCode.Ok, SQLiteConvert.ToUTF8("logging initialized."));
      }
#endif

      _parseViaFramework = parseViaFramework;

      _flags = SQLiteConnectionFlags.Default;
      _connectionState = ConnectionState.Closed;
      _connectionString = "";
      //_commandList = new List<WeakReference>();

      if (connectionString != null)
        ConnectionString = connectionString;
    }

    /// <summary>
    /// Clones the settings and connection string from an existing connection.  If the existing connection is already open, this
    /// function will open its own connection, enumerate any attached databases of the original connection, and automatically
    /// attach to them.
    /// </summary>
    /// <param name="connection">The connection to copy the settings from.</param>
    public SQLiteConnection(SQLiteConnection connection)
      : this(connection.ConnectionString)
    {
      string str;

      if (connection.State == ConnectionState.Open)
      {
        Open();

................................................................................
    /// created.
    /// </returns>
    public static object CreateHandle(
        IntPtr nativeHandle
        )
    {
        if (nativeHandle == IntPtr.Zero) return null;
        return new SQLiteConnectionHandle(nativeHandle);
    }

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

    #region Backup API Members
    /// <summary>
    /// Backs up the database, using the specified database connection as the
................................................................................
            string dateTimeFormat = FindKey(opts, "DateTimeFormatString",
                DefaultDateTimeFormatString);

            //
            // NOTE: SQLite automatically sets the encoding of the database to
            //       UTF16 if called from sqlite3_open16().
            //


            if (SQLiteConvert.ToBoolean(FindKey(opts, "UseUTF16Encoding",
                      DefaultUseUTF16Encoding.ToString())))
            {
                _sql = new SQLite3_UTF16(dateFormat, kind, dateTimeFormat);
            }
            else
            {
                _sql = new SQLite3(dateFormat, kind, dateTimeFormat);
            }
        }

        SQLiteOpenFlagsEnum flags = SQLiteOpenFlagsEnum.None;

        if (!SQLiteConvert.ToBoolean(FindKey(opts, "FailIfMissing", DefaultFailIfMissing.ToString())))
          flags |= SQLiteOpenFlagsEnum.Create;
................................................................................
            //
            // NOTE: SQLite automatically sets the encoding of the database to
            //       UTF16 if called from sqlite3_open16().
            //
            if (SQLiteConvert.ToBoolean(FindKey(opts,
                    "UseUTF16Encoding", DefaultUseUTF16Encoding.ToString())))
            {
                _sql = new SQLite3_UTF16(dateFormat, kind, dateTimeFormat);
            }
            else
            {
                _sql = new SQLite3(dateFormat, kind, dateTimeFormat);
            }
        }
        if (_sql != null) return _sql.Shutdown();
        throw new InvalidOperationException("Database connection not active.");
    }

    /// Enables or disabled extended result codes returned by SQLite







>
>
>
>
>
>
>







 







|












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





|







 







>


|
<












|







 







|







 







>
>



|



|







 







|



|







399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
...
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
...
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
...
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
....
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
....
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381

#if !PLATFORM_COMPACTFRAMEWORK
    /// <summary>
    /// Whether or not the connection is enlisted in a distrubuted transaction
    /// </summary>
    internal SQLiteEnlistment _enlistment;
#endif

    /// <summary>
    /// This field will be non-zero if this instance owns the native connection
    /// handle and should dispose of it when it is no longer needed.
    /// </summary>
    private bool _ownHandle;

    /// <summary>
    /// The base SQLite object to interop with
    /// </summary>
    internal SQLiteBase _sql;
    /// <summary>
    /// The database filename minus path and extension
    /// </summary>
................................................................................
    ///<overloads>
    /// Constructs a new SQLiteConnection object
    /// </overloads>
    /// <summary>
    /// Default constructor
    /// </summary>
    public SQLiteConnection()
      : this((string)null)
    {
    }

    /// <summary>
    /// Initializes the connection with the specified connection string.
    /// </summary>
    /// <param name="connectionString">The connection string to use.</param>
    public SQLiteConnection(string connectionString)
        : this(connectionString, false)
    {
        // do nothing.
    }

    /// <summary>
    /// Initializes the connection with a pre-existing native connection handle.
    /// </summary>
    /// <param name="db">
    /// The native connection handle to use.
    /// </param>
    /// <param name="ownHandle">
    /// Non-zero if this instance owns the native connection handle and
    /// should dispose of it when it is no longer needed.
    /// </param>
    internal SQLiteConnection(IntPtr db, bool ownHandle)
        : this()
    {
        _ownHandle = ownHandle;

        _sql = new SQLite3(
            SQLiteDateFormats.Default, DateTimeKind.Unspecified, null,
            _ownHandle);
    }

    /// <summary>
    /// Initializes the connection with the specified connection string.
    /// </summary>
    /// <param name="connectionString">
    /// The connection string to use.
    /// </param>
    /// <param name="parseViaFramework">
    /// Non-zero to parse the connection string using the built-in (i.e.
    /// framework provided) parser when opening the connection.
    /// </param>
    public SQLiteConnection(string connectionString, bool parseViaFramework)
    {
................................................................................
      {
          UnsafeNativeMethods.sqlite3_log(
              SQLiteErrorCode.Ok, SQLiteConvert.ToUTF8("logging initialized."));
      }
#endif

      _parseViaFramework = parseViaFramework;
      _ownHandle = true;
      _flags = SQLiteConnectionFlags.Default;
      _connectionState = ConnectionState.Closed;
      _connectionString = null;


      if (connectionString != null)
        ConnectionString = connectionString;
    }

    /// <summary>
    /// Clones the settings and connection string from an existing connection.  If the existing connection is already open, this
    /// function will open its own connection, enumerate any attached databases of the original connection, and automatically
    /// attach to them.
    /// </summary>
    /// <param name="connection">The connection to copy the settings from.</param>
    public SQLiteConnection(SQLiteConnection connection)
      : this(connection.ConnectionString, connection.ParseViaFramework)
    {
      string str;

      if (connection.State == ConnectionState.Open)
      {
        Open();

................................................................................
    /// created.
    /// </returns>
    public static object CreateHandle(
        IntPtr nativeHandle
        )
    {
        if (nativeHandle == IntPtr.Zero) return null;
        return new SQLiteConnectionHandle(nativeHandle, true);
    }

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

    #region Backup API Members
    /// <summary>
    /// Backs up the database, using the specified database connection as the
................................................................................
            string dateTimeFormat = FindKey(opts, "DateTimeFormatString",
                DefaultDateTimeFormatString);

            //
            // NOTE: SQLite automatically sets the encoding of the database to
            //       UTF16 if called from sqlite3_open16().
            //
            _ownHandle = true;

            if (SQLiteConvert.ToBoolean(FindKey(opts, "UseUTF16Encoding",
                      DefaultUseUTF16Encoding.ToString())))
            {
                _sql = new SQLite3_UTF16(dateFormat, kind, dateTimeFormat, _ownHandle);
            }
            else
            {
                _sql = new SQLite3(dateFormat, kind, dateTimeFormat, _ownHandle);
            }
        }

        SQLiteOpenFlagsEnum flags = SQLiteOpenFlagsEnum.None;

        if (!SQLiteConvert.ToBoolean(FindKey(opts, "FailIfMissing", DefaultFailIfMissing.ToString())))
          flags |= SQLiteOpenFlagsEnum.Create;
................................................................................
            //
            // NOTE: SQLite automatically sets the encoding of the database to
            //       UTF16 if called from sqlite3_open16().
            //
            if (SQLiteConvert.ToBoolean(FindKey(opts,
                    "UseUTF16Encoding", DefaultUseUTF16Encoding.ToString())))
            {
                _sql = new SQLite3_UTF16(dateFormat, kind, dateTimeFormat, _ownHandle);
            }
            else
            {
                _sql = new SQLite3(dateFormat, kind, dateTimeFormat, _ownHandle);
            }
        }
        if (_sql != null) return _sql.Shutdown();
        throw new InvalidOperationException("Database connection not active.");
    }

    /// Enables or disabled extended result codes returned by SQLite

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

177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
#endif

                //
                // NOTE: Create an instance of the SQLite wrapper class.
                //
                if (_sql == null)
                    _sql = new SQLite3(SQLiteDateFormats.Default,
                        DateTimeKind.Unspecified, null);

                //
                // NOTE: Create a single "global" (i.e. per-process) callback
                //       to register with SQLite.  This callback will pass the
                //       event on to any registered handler.  We only want to
                //       do this once.
                //







|







177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
#endif

                //
                // NOTE: Create an instance of the SQLite wrapper class.
                //
                if (_sql == null)
                    _sql = new SQLite3(SQLiteDateFormats.Default,
                        DateTimeKind.Unspecified, null, true);

                //
                // NOTE: Create a single "global" (i.e. per-process) callback
                //       to register with SQLite.  This callback will pass the
                //       event on to any registered handler.  We only want to
                //       do this once.
                //

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

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
..
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
...
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
...
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
...
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
...
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
    {
    }

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

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

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

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

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

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

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

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

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

    public class SQLiteVirtualTableCursor
    {
        private SQLiteModuleBase.UnsafeNativeMethods.sqlite3_vtab_cursor cursor;
    }

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

    public interface ISQLiteNativeModule
    {
        SQLiteErrorCode xCreate(IntPtr db, IntPtr pAux, int argc, ref IntPtr[] argv, ref IntPtr vTab, ref IntPtr error);
        SQLiteErrorCode xConnect(IntPtr db, IntPtr pAux, int argc, ref IntPtr[] argv, ref IntPtr vTab, ref IntPtr error);
        SQLiteErrorCode xBestIndex(IntPtr vTab, IntPtr index);
        SQLiteErrorCode xDisconnect(IntPtr vTab);
        SQLiteErrorCode xDestroy(IntPtr vTab);
        SQLiteErrorCode xOpen(IntPtr vTab, ref IntPtr cursor);
        SQLiteErrorCode xClose(IntPtr cursor);
        SQLiteErrorCode xFilter(IntPtr cursor, int idxNum, IntPtr idxStr, int argc, IntPtr[] argv);
        SQLiteErrorCode xNext(IntPtr cursor);
        SQLiteErrorCode xEof(IntPtr cursor);
        SQLiteErrorCode xColumn(IntPtr cursor, IntPtr context, int index);
        SQLiteErrorCode xRowId(IntPtr cursor, ref long rowId);
        SQLiteErrorCode xUpdate(IntPtr vtab, int nData, ref IntPtr apData, ref long rowId);
        SQLiteErrorCode xBegin(IntPtr vtab);
        SQLiteErrorCode xSync(IntPtr vtab);
        SQLiteErrorCode xCommit(IntPtr vtab);
        SQLiteErrorCode xRollback(IntPtr vtab);
        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);
    }

................................................................................
#else
    [SecurityPermission(SecurityAction.LinkDemand, UnmanagedCode = true)]
#endif
    public abstract class SQLiteModuleBase : ISQLiteManagedModule, ISQLiteNativeModule,  IDisposable
    {
        #region Unsafe Native Methods Class
        [SuppressUnmanagedCodeSecurity()]
        internal static class UnsafeNativeMethods
        {
            // https://www.sqlite.org/vtab.html


            [StructLayout(LayoutKind.Sequential)]
            public struct sqlite3_module
            {
................................................................................
                sqlite3_index_constraint_usage[] aConstraintUsage;
                int idxNum;                /* Number used to identify the index */
                string idxStr;              /* String, possibly obtained from sqlite3_malloc */
                int needToFreeIdxStr;      /* Free idxStr using sqlite3_free() if true */
                int orderByConsumed;       /* True if output is already ordered */
                double estimatedCost;      /* Estimated cost of using this index */
            }





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

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

            [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
            public delegate SQLiteErrorCode xBestIndex(
                [MarshalAs(UnmanagedType.LPStruct)]
                IntPtr vTab,
                [MarshalAs(UnmanagedType.LPStruct)]
                IntPtr index
            );

            [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
            public delegate SQLiteErrorCode xDisconnect(
                [MarshalAs(UnmanagedType.LPStruct)]
                IntPtr vTab
            );

            [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
            public delegate SQLiteErrorCode xDestroy(
                [MarshalAs(UnmanagedType.LPStruct)]
                IntPtr vTab
            );

            [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
            public delegate SQLiteErrorCode xOpen(
                [MarshalAs(UnmanagedType.LPStruct)]
                IntPtr vTab,
                ref IntPtr cursor
            );

            [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
            public delegate SQLiteErrorCode xClose(
                [MarshalAs(UnmanagedType.LPStruct)]
                IntPtr cursor
................................................................................
                IntPtr cursor,
                ref long rowId
            );

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

            [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
            public delegate SQLiteErrorCode xBegin(
                [MarshalAs(UnmanagedType.LPStruct)]
                IntPtr vtab
            );

            [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
            public delegate SQLiteErrorCode xSync(
                [MarshalAs(UnmanagedType.LPStruct)]
                IntPtr vtab
            );

            [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
            public delegate SQLiteErrorCode xCommit(
                [MarshalAs(UnmanagedType.LPStruct)]
                IntPtr vtab
            );

            [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
            public delegate SQLiteErrorCode xRollback(
                [MarshalAs(UnmanagedType.LPStruct)]
                IntPtr vtab
            );

            [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
            public delegate SQLiteErrorCode xFindFunction(
                [MarshalAs(UnmanagedType.LPStruct)]
                IntPtr pVtab,
                int nArg,
................................................................................
            [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
            public delegate SQLiteErrorCode xRollbackTo(
                [MarshalAs(UnmanagedType.LPStruct)]
                IntPtr pVtab,
                int iSavepoint
            );

            [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
            public delegate void xDestroyModule(
                IntPtr pClientData
            );

            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;

            [DllImport("sqlite3", CallingConvention = CallingConvention.Cdecl)]
            internal static extern void sqlite3_create_module_v2(
                IntPtr db,
                IntPtr name,
                IntPtr pModule,
                IntPtr pClientData,
                xDestroyModule xDestroy
                );

            [DllImport("sqlite3", CallingConvention = CallingConvention.Cdecl)]
            internal static extern SQLiteErrorCode sqlite3_declare_vtab(IntPtr db, IntPtr zSQL);

            [DllImport("sqlite3", CallingConvention = CallingConvention.Cdecl)]
            internal static extern IntPtr sqlite3_mprintf(IntPtr format, __arglist);
        }
        #endregion

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

        #region Private Data
        private IntPtr database;
        #endregion

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

        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);

            IntPtr foo = IntPtr.Zero;


            Marshal.PtrToStructure(foo, typeof(UnsafeNativeMethods.sqlite3_module));


            return module;

        }






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


        public SQLiteModuleBase()







        {


        }








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

        public SQLiteErrorCode xCreate(
            IntPtr db,
            IntPtr pAux,
            int argc,
            ref IntPtr[] argv,
            ref IntPtr vTab,
            ref IntPtr error
            )
        {
            return SQLiteErrorCode.Ok;
        }

        public SQLiteErrorCode xConnect(
            IntPtr db,
            IntPtr pAux,
            int argc,
            ref IntPtr[] argv,
            ref IntPtr vTab,
            ref IntPtr error

        )
        {
            return SQLiteErrorCode.Ok;
        }

        public SQLiteErrorCode xBestIndex(
            IntPtr vTab,
            IntPtr index
        )
        {
            return SQLiteErrorCode.Ok;
        }



        public SQLiteErrorCode xDisconnect(
                IntPtr vTab
        )
        {
            return SQLiteErrorCode.Ok;
        }



        public SQLiteErrorCode xDestroy(
                IntPtr vTab
        )
        {
            return SQLiteErrorCode.Ok;
        }



        public SQLiteErrorCode xOpen(
            IntPtr vTab,
            ref IntPtr cursor
        )
        {
            return SQLiteErrorCode.Ok;
        }



        public SQLiteErrorCode xClose(
                IntPtr cursor
        )
        {
            return SQLiteErrorCode.Ok;
        }



        public SQLiteErrorCode xFilter(
            IntPtr cursor,
            int idxNum,
            IntPtr idxStr,
            int argc,
            IntPtr[] argv
        )
        {
            return SQLiteErrorCode.Ok;
        }



        public SQLiteErrorCode xNext(
            IntPtr cursor
        )
        {
            return SQLiteErrorCode.Ok;
        }



        public SQLiteErrorCode xEof(
                IntPtr cursor
        )
        {
            return SQLiteErrorCode.Ok;
        }



        public SQLiteErrorCode xColumn(
            IntPtr cursor,
            IntPtr context,
            int index
        )
        {
            return SQLiteErrorCode.Ok;
        }



        public SQLiteErrorCode xRowId(
            IntPtr cursor,
            ref long rowId
        )
        {
            return SQLiteErrorCode.Ok;
        }



        public SQLiteErrorCode xUpdate(
            IntPtr vtab,
            int nData,
            ref IntPtr apData,
            ref long rowId
        )



























        {
            return SQLiteErrorCode.Ok;
        }

        public SQLiteErrorCode xBegin(
            IntPtr vtab

        )
        {
            return SQLiteErrorCode.Ok;
        }


        public SQLiteErrorCode xSync(
            IntPtr vtab
        )
        {
            return SQLiteErrorCode.Ok;
        }

        public SQLiteErrorCode xCommit(
            IntPtr vtab
        )
        {
            return SQLiteErrorCode.Ok;
        }


        public SQLiteErrorCode xRollback(
            IntPtr vtab
        )
        {
            return SQLiteErrorCode.Ok;
        }



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



        public SQLiteErrorCode xRename(
            IntPtr pVtab,
            IntPtr zNew
        )
        {
            return SQLiteErrorCode.Ok;
        }



        public SQLiteErrorCode xSavepoint(
            IntPtr pVtab,
            int iSavepoint
        )
        {
            return SQLiteErrorCode.Ok;
        }



        public SQLiteErrorCode xRelease(
            IntPtr pVtab,
            int iSavepoint
        )
        {
            return SQLiteErrorCode.Ok;
        }



        public SQLiteErrorCode xRollbackTo(
            IntPtr pVtab,
            int iSavepoint
        )
        {
            return SQLiteErrorCode.Ok;
        }


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


        public abstract SQLiteErrorCode Create(SQLiteConnection connection, IntPtr pClientData, string[] argv, ref string error);








        public abstract SQLiteErrorCode Connect(SQLiteConnection connection, IntPtr pClientData, string[] argv, ref string error);








        public abstract SQLiteErrorCode BestIndex(ref SQLiteIndex index);





        public abstract SQLiteErrorCode Disconnect();



        public abstract SQLiteErrorCode Destroy();



        public abstract SQLiteErrorCode Open(ref SQLiteVirtualTableCursor cursor);





        public abstract SQLiteErrorCode Close(SQLiteVirtualTableCursor cursor);





        public abstract SQLiteErrorCode Filter(SQLiteVirtualTableCursor cursor, int idxNum, string idxStr, SQLiteValue[] values);








        public abstract SQLiteErrorCode Next(SQLiteVirtualTableCursor cursor);





        public abstract SQLiteErrorCode Eof(SQLiteVirtualTableCursor cursor);





        public abstract SQLiteErrorCode Column(SQLiteVirtualTableCursor cursor, SQLiteContext context, int index);







        public abstract SQLiteErrorCode RowId(SQLiteVirtualTableCursor cursor, ref long rowId);






        public abstract SQLiteErrorCode Update(SQLiteValue[] values, ref long rowId);






        public abstract SQLiteErrorCode Begin();



        public abstract SQLiteErrorCode Sync();



        public abstract SQLiteErrorCode Commit();



        public abstract SQLiteErrorCode Rollback();



        public abstract SQLiteErrorCode FindFunction(string zName, ref SQLiteFunction function, object[] args);







        public abstract SQLiteErrorCode Rename(string zNew);





        public abstract SQLiteErrorCode Savepoint(int iSavepoint);





        public abstract SQLiteErrorCode Release(int iSavepoint);





        public abstract SQLiteErrorCode RollbackTo(int iSavepoint);




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

        #region IDisposable Members
        public void Dispose()
        {
            Dispose(true);







|






|






|







 







|












|
|
|
|
|
|






|
|
|
|
|







 







|







 







>
>
>
>








|










|






|







|





|





|







 







|








|





|





|





|







 







<
<
<
<
<






|
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<




|
<
<
<
<
<
|

|
|


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

<
>
|
<
>

<
>
|
>
>
|
>
>
>


>
|
>
>
>
>
>
>
>

>

|
>
>

>
>
>
>
>


|




|






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

|

|




>


|
|




>


|
|




>


|

|




>


|
|




>







|




>



|




>


|
|




>





|




>




|




>


|



|
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>




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




<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
>







|




>




|




>




|




>




|



>
>




|



>



>
|
>
>
>
>
>
>
>
>
|
>
>
>
>
>
>
>
>
|
>
>
>
>
>

>
>
>


>
>
|
>
>
>
>
>
|
>
>
>
>
>
|
>
>
>
>
>
>
>
>
|
>
>
>
>
>
|
>
>

>
>
|
>
>
>
>
>
>
>
|
>
>
>

>
>
|
>
>
>

>
>

>
>
>

>
>
>

>
>
>


>
>
|
>
>
>
>

>
>
|
>
>

>
>
|
>
>
>
>
>
|
>
>
>
>
>
|
>
>
>







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
..
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
...
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
...
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
...
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
...
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
    {
    }

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

    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
    {
        private SQLiteModuleBase.UnsafeNativeMethods2.sqlite3_vtab_cursor cursor;
    }

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

    public interface ISQLiteNativeModule
    {
        SQLiteErrorCode xCreate(IntPtr db, IntPtr pAux, int argc, ref IntPtr[] argv, ref IntPtr pVtab, ref IntPtr error);
        SQLiteErrorCode xConnect(IntPtr db, IntPtr pAux, int argc, ref IntPtr[] argv, ref IntPtr pVtab, ref IntPtr error);
        SQLiteErrorCode xBestIndex(IntPtr pVtab, IntPtr index);
        SQLiteErrorCode xDisconnect(IntPtr pVtab);
        SQLiteErrorCode xDestroy(IntPtr pVtab);
        SQLiteErrorCode xOpen(IntPtr pVtab, ref IntPtr cursor);
        SQLiteErrorCode xClose(IntPtr cursor);
        SQLiteErrorCode xFilter(IntPtr cursor, int idxNum, IntPtr idxStr, int argc, IntPtr[] argv);
        SQLiteErrorCode xNext(IntPtr cursor);
        SQLiteErrorCode xEof(IntPtr cursor);
        SQLiteErrorCode xColumn(IntPtr cursor, IntPtr context, int index);
        SQLiteErrorCode xRowId(IntPtr cursor, 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);
    }

................................................................................
#else
    [SecurityPermission(SecurityAction.LinkDemand, UnmanagedCode = true)]
#endif
    public abstract class SQLiteModuleBase : ISQLiteManagedModule, ISQLiteNativeModule,  IDisposable
    {
        #region Unsafe Native Methods Class
        [SuppressUnmanagedCodeSecurity()]
        internal static class UnsafeNativeMethods2
        {
            // https://www.sqlite.org/vtab.html


            [StructLayout(LayoutKind.Sequential)]
            public struct sqlite3_module
            {
................................................................................
                sqlite3_index_constraint_usage[] aConstraintUsage;
                int idxNum;                /* Number used to identify the index */
                string idxStr;              /* String, possibly obtained from sqlite3_malloc */
                int needToFreeIdxStr;      /* Free idxStr using sqlite3_free() if true */
                int orderByConsumed;       /* True if output is already ordered */
                double estimatedCost;      /* Estimated cost of using this index */
            }





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

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

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

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

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

            [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
            public delegate SQLiteErrorCode xOpen(
                [MarshalAs(UnmanagedType.LPStruct)]
                IntPtr pVtab,
                ref IntPtr cursor
            );

            [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
            public delegate SQLiteErrorCode xClose(
                [MarshalAs(UnmanagedType.LPStruct)]
                IntPtr cursor
................................................................................
                IntPtr cursor,
                ref long rowId
            );

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

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

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

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

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

            [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
            public delegate SQLiteErrorCode xFindFunction(
                [MarshalAs(UnmanagedType.LPStruct)]
                IntPtr pVtab,
                int nArg,
................................................................................
            [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
            public delegate SQLiteErrorCode xRollbackTo(
                [MarshalAs(UnmanagedType.LPStruct)]
                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;
        }

        #endregion


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

        #region Public Constructors
        public SQLiteModuleBase()
        {
        }
        #endregion

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

        #region ISQLiteNativeModule Members
        public SQLiteErrorCode xCreate(
            IntPtr db,
            IntPtr pAux,
            int argc,
            ref IntPtr[] argv,
            ref IntPtr pVtab,
            ref IntPtr error
            )
        {
            SQLiteConnection connection = new SQLiteConnection(db, false);


            pVtab = UnsafeNativeMethods.sqlite3_malloc(Marshal.SizeOf(typeof(
                UnsafeNativeMethods2.sqlite3_vtab)));

            string errorStr = null;

            return Create(null, IntPtr.Zero, null, ref errorStr);
        }

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

        public SQLiteErrorCode xConnect(
            IntPtr db,
            IntPtr pAux,
            int argc,
            ref IntPtr[] argv,
            ref IntPtr pVtab,
            ref IntPtr error
            )
        {
            return SQLiteErrorCode.Ok;
        }








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





        public SQLiteErrorCode xBestIndex(
            IntPtr pVtab,
            IntPtr index
            )
        {
            return SQLiteErrorCode.Ok;
        }

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

        public SQLiteErrorCode xDisconnect(
            IntPtr pVtab
            )
        {
            return SQLiteErrorCode.Ok;
        }

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

        public SQLiteErrorCode xDestroy(
            IntPtr pVtab
            )
        {
            return SQLiteErrorCode.Ok;
        }

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

        public SQLiteErrorCode xOpen(
            IntPtr pVtab,
            ref IntPtr cursor
            )
        {
            return SQLiteErrorCode.Ok;
        }

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

        public SQLiteErrorCode xClose(
            IntPtr cursor
            )
        {
            return SQLiteErrorCode.Ok;
        }

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

        public SQLiteErrorCode xFilter(
            IntPtr cursor,
            int idxNum,
            IntPtr idxStr,
            int argc,
            IntPtr[] argv
            )
        {
            return SQLiteErrorCode.Ok;
        }

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

        public SQLiteErrorCode xNext(
            IntPtr cursor
            )
        {
            return SQLiteErrorCode.Ok;
        }

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

        public SQLiteErrorCode xEof(
            IntPtr cursor
            )
        {
            return SQLiteErrorCode.Ok;
        }

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

        public SQLiteErrorCode xColumn(
            IntPtr cursor,
            IntPtr context,
            int index
            )
        {
            return SQLiteErrorCode.Ok;
        }

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

        public SQLiteErrorCode xRowId(
            IntPtr cursor,
            ref long rowId
            )
        {
            return SQLiteErrorCode.Ok;
        }

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

        public SQLiteErrorCode xUpdate(
            IntPtr pVtab,
            int nData,
            ref IntPtr apData,
            ref long rowId
            )
        {
            return SQLiteErrorCode.Ok;
        }

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

        public SQLiteErrorCode xBegin(
            IntPtr pVtab
            )
        {
            return SQLiteErrorCode.Ok;
        }

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

        public SQLiteErrorCode xSync(
            IntPtr pVtab
            )
        {
            return SQLiteErrorCode.Ok;
        }

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

        public SQLiteErrorCode xCommit(
            IntPtr pVtab
            )
        {
            return SQLiteErrorCode.Ok;
        }



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






        public SQLiteErrorCode xRollback(
            IntPtr pVtab
            )
        {
            return SQLiteErrorCode.Ok;
        }
















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

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

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

        public SQLiteErrorCode xRename(
            IntPtr pVtab,
            IntPtr zNew
            )
        {
            return SQLiteErrorCode.Ok;
        }

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

        public SQLiteErrorCode xSavepoint(
            IntPtr pVtab,
            int iSavepoint
            )
        {
            return SQLiteErrorCode.Ok;
        }

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

        public SQLiteErrorCode xRelease(
            IntPtr pVtab,
            int iSavepoint
            )
        {
            return SQLiteErrorCode.Ok;
        }

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

        public SQLiteErrorCode xRollbackTo(
            IntPtr pVtab,
            int iSavepoint
            )
        {
            return SQLiteErrorCode.Ok;
        }
        #endregion

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

        #region ISQLiteManagedModule Members
        public abstract SQLiteErrorCode Create(
            SQLiteConnection connection,
            IntPtr pClientData,
            string[] argv,
            ref string error
            );

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

        public abstract SQLiteErrorCode Connect(
            SQLiteConnection connection,
            IntPtr pClientData,
            string[] argv,
            ref string error
            );

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

        public abstract SQLiteErrorCode BestIndex(
            ref SQLiteIndex index
            );

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

        public abstract SQLiteErrorCode Disconnect();

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

        public abstract SQLiteErrorCode Destroy();

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

        public abstract SQLiteErrorCode Open(
            ref SQLiteVirtualTableCursor cursor
            );

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

        public abstract SQLiteErrorCode Close(
            SQLiteVirtualTableCursor cursor
            );

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

        public abstract SQLiteErrorCode Filter(
            SQLiteVirtualTableCursor cursor,
            int idxNum,
            string idxStr,
            SQLiteValue[] values
            );

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

        public abstract SQLiteErrorCode Next(
            SQLiteVirtualTableCursor cursor
            );

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

        public abstract SQLiteErrorCode Eof(
            SQLiteVirtualTableCursor cursor
            );

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

        public abstract SQLiteErrorCode Column(
            SQLiteVirtualTableCursor cursor,
            SQLiteContext context,
            int index
            );

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

        public abstract SQLiteErrorCode RowId(
            SQLiteVirtualTableCursor cursor,
            ref long rowId
            );

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

        public abstract SQLiteErrorCode Update(
            SQLiteValue[] values,
            ref long rowId
            );

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

        public abstract SQLiteErrorCode Begin();

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

        public abstract SQLiteErrorCode Sync();

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

        public abstract SQLiteErrorCode Commit();

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

        public abstract SQLiteErrorCode Rollback();

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

        public abstract SQLiteErrorCode FindFunction(
            string zName,
            ref SQLiteFunction function,
            object[] args
            );

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

        public abstract SQLiteErrorCode Rename(
            string zNew
            );

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

        public abstract SQLiteErrorCode Savepoint(
            int iSavepoint
            );

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

        public abstract SQLiteErrorCode Release(
            int iSavepoint
            );

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

        public abstract SQLiteErrorCode RollbackTo(
            int iSavepoint
            );
        #endregion

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

        #region IDisposable Members
        public void Dispose()
        {
            Dispose(true);

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

1555
1556
1557
1558
1559
1560
1561




































1562
1563
1564
1565
1566
1567
1568
....
1653
1654
1655
1656
1657
1658
1659




1660
1661
1662
1663
1664
1665
1666
....
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

#if !PLATFORM_COMPACTFRAMEWORK
    [DllImport(SQLITE_DLL, CallingConvention = CallingConvention.Cdecl)]
#else
    [DllImport(SQLITE_DLL)]
#endif
    internal static extern int sqlite3_backup_pagecount(IntPtr backup);




































    #endregion
  }

#if PLATFORM_COMPACTFRAMEWORK
  internal abstract class CriticalHandle : IDisposable
  {
    private bool _isClosed;
................................................................................

#if PLATFORM_COMPACTFRAMEWORK
        internal readonly object syncRoot = new object();
#endif

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





        public static implicit operator IntPtr(SQLiteConnectionHandle db)
        {
            if (db != null)
            {
#if PLATFORM_COMPACTFRAMEWORK
                lock (db.syncRoot)
#endif
................................................................................
                }
            }
            return IntPtr.Zero;
        }

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

        internal SQLiteConnectionHandle(IntPtr db)
            : this()
        {
#if PLATFORM_COMPACTFRAMEWORK
            lock (syncRoot)
#endif
            {

                SetHandle(db);
            }
        }

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

        private SQLiteConnectionHandle()
            : base(IntPtr.Zero)
        {
#if COUNT_HANDLE
            Interlocked.Increment(
                ref UnsafeNativeMethods.connectionCount);
#endif
        }

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

        protected override bool ReleaseHandle()
        {







            try
            {
#if !PLATFORM_COMPACTFRAMEWORK
                IntPtr localHandle = Interlocked.Exchange(
                    ref handle, IntPtr.Zero);

#if SQLITE_STANDARD







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







 







>
>
>
>







 







|
|





>






|



|
|







>
>
>
>
>
>
>







1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
....
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
....
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

#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)]
    public delegate void xDestroyModule(IntPtr pClientData);
#endif

#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,
#if !PLATFORM_COMPACTFRAMEWORK
        xDestroyModule xDestroy
#else
        IntPtr xDestroy
#endif
    );

#if !PLATFORM_COMPACTFRAMEWORK
    [DllImport(SQLITE_DLL, CallingConvention = CallingConvention.Cdecl)]
#else
    [DllImport(SQLITE_DLL)]
#endif
    internal static extern SQLiteErrorCode sqlite3_declare_vtab(IntPtr db, IntPtr zSQL);

#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;
................................................................................

#if PLATFORM_COMPACTFRAMEWORK
        internal readonly object syncRoot = new object();
#endif

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

        private bool ownHandle;

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

        public static implicit operator IntPtr(SQLiteConnectionHandle db)
        {
            if (db != null)
            {
#if PLATFORM_COMPACTFRAMEWORK
                lock (db.syncRoot)
#endif
................................................................................
                }
            }
            return IntPtr.Zero;
        }

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

        internal SQLiteConnectionHandle(IntPtr db, bool ownHandle)
            : this(ownHandle)
        {
#if PLATFORM_COMPACTFRAMEWORK
            lock (syncRoot)
#endif
            {
                this.ownHandle = ownHandle;
                SetHandle(db);
            }
        }

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

        private SQLiteConnectionHandle(bool ownHandle)
            : base(IntPtr.Zero)
        {
#if COUNT_HANDLE
            if (ownHandle)
                Interlocked.Increment(ref UnsafeNativeMethods.connectionCount);
#endif
        }

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

        protected override bool ReleaseHandle()
        {
#if PLATFORM_COMPACTFRAMEWORK
            lock (syncRoot)
#endif
            {
                if (!ownHandle) return true;
            }

            try
            {
#if !PLATFORM_COMPACTFRAMEWORK
                IntPtr localHandle = Interlocked.Exchange(
                    ref handle, IntPtr.Zero);

#if SQLITE_STANDARD