System.Data.SQLite

Login
This project makes use of Eagle, provided by Mistachkin Systems.
Eagle: Secure Software Automation

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

Changes In Branch safeHandle Excluding Merge-Ins

This is equivalent to a diff from c063096071 to cb270f5cd8

2012-05-03
13:04
Stop creating the CriticalHandle derived classes via implicit operator conversions. Simplify test case for ticket [996d13cd87]. check-in: 29b506224a user: mistachkin tags: trunk
06:35
Experimental code to use SafeHandle instead of CriticalHandle for connection handles because they should not be closed until their statements and backup objects are all finalized. Closed-Leaf check-in: cb270f5cd8 user: mistachkin tags: safeHandle
2012-05-02
22:29
Update version history with all the latest changes. check-in: c063096071 user: mistachkin tags: trunk
18:52
Update Eagle script library in externals to latest trunk. check-in: fd95f9dd1d user: mistachkin tags: trunk

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

188
189
190
191
192
193
194
195

196
197
198
199
200
201
202
188
189
190
191
192
193
194

195
196
197
198
199
200
201
202







-
+







      }
    }

    internal override bool AutoCommit
    {
      get
      {
        return IsAutocommit(_sql);
        return IsAutocommit(_sql, _sql);
      }
    }

    internal override long LastInsertRowId
    {
      get
      {
272
273
274
275
276
277
278
279






280
281
282
283
284
285
286
272
273
274
275
276
277
278

279
280
281
282
283
284
285
286
287
288
289
290
291







-
+
+
+
+
+
+








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

        if (n > 0) throw new SQLiteException(n, null);

        _sql = db;
        _sql = new SQLiteConnectionHandle(db);

        lock (_sql)
        {
            // do nothing.
        }
      }
      // 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);
    }

529
530
531
532
533
534
535
536

537
538
539
540
541
542
543
534
535
536
537
538
539
540

541
542
543
544
545
546
547
548







-
+







          }
        }

        if (n > 0) throw new SQLiteException(n, GetLastError());

        strRemain = UTF8ToString(ptr, len);

        if (stmt != IntPtr.Zero) cmd = new SQLiteStatement(this, flags, stmt, strSql.Substring(0, strSql.Length - strRemain.Length), previous);
        if (stmt != IntPtr.Zero) cmd = new SQLiteStatement(this, flags, new SQLiteStatementHandle(_sql, stmt), strSql.Substring(0, strSql.Length - strRemain.Length), previous);

        return cmd;
      }
      finally
      {
        handle.Free();
      }
1471
1472
1473
1474
1475
1476
1477

1478

1479
1480
1481
1482
1483
1484
1485
1476
1477
1478
1479
1480
1481
1482
1483

1484
1485
1486
1487
1488
1489
1490
1491







+
-
+







        IntPtr backup = UnsafeNativeMethods.sqlite3_backup_init(
            destHandle, zDestName, sourceHandle, zSourceName);

        if (backup == IntPtr.Zero)
            throw new SQLiteException(ResultCode(), GetLastError());

        return new SQLiteBackup(
            this, new SQLiteBackupHandle(destHandle, backup),
            this, backup, destHandle, zDestName, sourceHandle, zSourceName);
            destHandle, zDestName, sourceHandle, zSourceName);
    }

    /// <summary>
    /// Copies up to N pages from the source database to the destination
    /// database associated with the specified backup object.
    /// </summary>
    /// <param name="backup">The backup object to use.</param>

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

119
120
121
122
123
124
125
126






127
128
129
130
131
132
133
119
120
121
122
123
124
125

126
127
128
129
130
131
132
133
134
135
136
137
138







-
+
+
+
+
+
+








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

        if (n > 0) throw new SQLiteException(n, null);

        _sql = db;
        _sql = new SQLiteConnectionHandle(db);

        lock (_sql)
        {
            // do nothing.
        }
      }
      _functionsArray = SQLiteFunction.BindFunctions(this, connectionFlags);
      SetTimeout(0);
    }

    internal override void Bind_DateTime(SQLiteStatement stmt, SQLiteConnectionFlags flags, int index, DateTime dt)
    {

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

343
344
345
346
347
348
349
350




351
352
353
354
355
356
357
358
359
360
361
362
363

364
365
366
367
368
369
370










371
372




373
374

375
376

377
378


379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398

399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416

417
418
419
420
421
422
423
424

425
426
427
428
429







430
431
432
433
434
435
436
343
344
345
346
347
348
349

350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365

366
367






368
369
370
371
372
373
374
375
376
377
378

379
380
381
382
383

384
385

386
387

388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427

428
429
430
431
432
433
434
435

436
437




438
439
440
441
442
443
444
445
446
447
448
449
450
451







-
+
+
+
+












-
+

-
-
-
-
-
-
+
+
+
+
+
+
+
+
+
+

-
+
+
+
+

-
+

-
+

-
+
+




















+

















-
+







-
+

-
-
-
-
+
+
+
+
+
+
+







    // They exist here because they are called during the finalization of
    // a SQLiteStatementHandle, SQLiteConnectionHandle, and SQLiteFunctionCookieHandle.
    // Therefore these functions have to be static, and have to be low-level.

    internal static string GetLastError(SQLiteConnectionHandle hdl, IntPtr db)
    {
        if ((hdl == null) || (db == IntPtr.Zero))
            return "invalid connection or database handle";
            return "null connection or database handle";

        if (hdl.IsClosed || hdl.IsInvalid)
            return "closed or invalid connection handle";

        lock (hdl)
        {
#if !SQLITE_STANDARD
            int len;
            return UTF8ToString(UnsafeNativeMethods.sqlite3_errmsg_interop(db, out len), len);
#else
            return UTF8ToString(UnsafeNativeMethods.sqlite3_errmsg(db), -1);
#endif
        }
    }

    internal static void FinishBackup(IntPtr backup)
    internal static void FinishBackup(SQLiteConnectionHandle hdl, IntPtr backup)
    {
        if (backup == IntPtr.Zero) return;
        int n = UnsafeNativeMethods.sqlite3_backup_finish(backup);
        if (n > 0) throw new SQLiteException(n, null);
    }

    internal static void FinalizeStatement(IntPtr stmt)
        if ((hdl == null) || (backup == IntPtr.Zero)) return;
        if (hdl.IsClosed || hdl.IsInvalid) return;
        lock (hdl)
        {
            int n = UnsafeNativeMethods.sqlite3_backup_finish(backup);
            if (n > 0) throw new SQLiteException(n, null);
        }
    }

    internal static void FinalizeStatement(SQLiteConnectionHandle hdl, IntPtr stmt)
    {
        if (stmt == IntPtr.Zero) return;
        if ((hdl == null) || (stmt == IntPtr.Zero)) return;
        if (hdl.IsClosed || hdl.IsInvalid) return;
        lock (hdl)
        {
#if !SQLITE_STANDARD
        int n = UnsafeNativeMethods.sqlite3_finalize_interop(stmt);
            int n = UnsafeNativeMethods.sqlite3_finalize_interop(stmt);
#else
        int n = UnsafeNativeMethods.sqlite3_finalize(stmt);
            int n = UnsafeNativeMethods.sqlite3_finalize(stmt);
#endif
        if (n > 0) throw new SQLiteException(n, null);
            if (n > 0) throw new SQLiteException(n, null);
        }
    }

    internal static void CloseConnection(SQLiteConnectionHandle hdl, IntPtr db)
    {
        if ((hdl == null) || (db == IntPtr.Zero)) return;
        lock (hdl)
        {
#if !SQLITE_STANDARD
            int n = UnsafeNativeMethods.sqlite3_close_interop(db);
#else
            ResetConnection(hdl, db);
            int n = UnsafeNativeMethods.sqlite3_close(db);
#endif
            if (n > 0) throw new SQLiteException(n, GetLastError(hdl, db));
        }
    }

    internal static void ResetConnection(SQLiteConnectionHandle hdl, IntPtr db)
    {
        if ((hdl == null) || (db == IntPtr.Zero)) return;
        if (hdl.IsClosed || hdl.IsInvalid) return;
        lock (hdl)
        {
            IntPtr stmt = IntPtr.Zero;
            int n;
            do
            {
                stmt = UnsafeNativeMethods.sqlite3_next_stmt(db, stmt);
                if (stmt != IntPtr.Zero)
                {
#if !SQLITE_STANDARD
                    n = UnsafeNativeMethods.sqlite3_reset_interop(stmt);
#else
                    n = UnsafeNativeMethods.sqlite3_reset(stmt);
#endif
                }
            } while (stmt != IntPtr.Zero);

            if (IsAutocommit(db) == false) // a transaction is pending on the connection
            if (IsAutocommit(hdl, db) == false) // a transaction is pending on the connection
            {
                n = UnsafeNativeMethods.sqlite3_exec(db, ToUTF8("ROLLBACK"), IntPtr.Zero, IntPtr.Zero, out stmt);
                if (n > 0) throw new SQLiteException(n, GetLastError(hdl, db));
            }
        }
    }

    internal static bool IsAutocommit(IntPtr db)
    internal static bool IsAutocommit(SQLiteConnectionHandle hdl, IntPtr db)
    {
      if (db == IntPtr.Zero) return false;
      return (UnsafeNativeMethods.sqlite3_get_autocommit(db) == 1);
    }

        if ((hdl == null) || (db == IntPtr.Zero)) return false;
        if (hdl.IsClosed || hdl.IsInvalid) return false;
        lock (hdl)
        {
            return (UnsafeNativeMethods.sqlite3_get_autocommit(db) == 1);
        }
    }
  }

  internal interface ISQLiteSchemaExtensions
  {
    void BuildTempSchema(SQLiteConnection cnn);
  }

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

1351
1352
1353
1354
1355
1356
1357
1358

1359
1360
1361
1362
1363
1364

1365
1366
1367
1368
1369
1370
1371


1372
1373
1374
1375
1376

1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1351
1352
1353
1354
1355
1356
1357

1358


1359
1360
1361

1362
1363
1364
1365
1366
1367
1368

1369
1370
1371

1372
1373

1374
1375
1376
1377





1378
1379
1380
1381
1382
1383
1384







-
+
-
-



-
+






-
+
+

-


-
+



-
-
-
-
-







      get { return _isClosed; }
    }

    public abstract bool IsInvalid
    {
      get;
    }

  }
  }

#endif

  // Handles the unmanaged database pointer, and provides finalization support for it.
  internal class SQLiteConnectionHandle : CriticalHandle
  internal class SQLiteConnectionHandle : SafeHandle
  {
    public static implicit operator IntPtr(SQLiteConnectionHandle db)
    {
      return (db != null) ? db.handle : IntPtr.Zero;
    }

    public static implicit operator SQLiteConnectionHandle(IntPtr db)
    private SQLiteConnectionHandle()
        : base(IntPtr.Zero, true)
    {
      return new SQLiteConnectionHandle(db);
    }

    private SQLiteConnectionHandle(IntPtr db)
    internal SQLiteConnectionHandle(IntPtr db)
      : this()
    {
      SetHandle(db);
    }

    internal SQLiteConnectionHandle()
      : base(IntPtr.Zero)
    {
    }

    protected override bool ReleaseHandle()
    {
      try
      {
#if !PLATFORM_COMPACTFRAMEWORK
1458
1459
1460
1461
1462
1463
1464



1465
1466
1467
1468
1469
1470


1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482


1483




1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495




1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510

1511
1512
1513
1514
1515
1516
1517
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465

1466
1467
1468

1469
1470
1471
1472
1473
1474
1475
1476


1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494

1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512

1513
1514
1515
1516
1517
1518
1519
1520







+
+
+





-
+
+

-








-
-
+
+

+
+
+
+











-
+
+
+
+














-
+







    }
#endif
  }

  // Provides finalization support for unmanaged SQLite statements.
  internal class SQLiteStatementHandle : CriticalHandle
  {
    internal SQLiteConnectionHandle cnn;
    private bool releaseNeeded;

    public static implicit operator IntPtr(SQLiteStatementHandle stmt)
    {
      return (stmt != null) ? stmt.handle : IntPtr.Zero;
    }

    public static implicit operator SQLiteStatementHandle(IntPtr stmt)
    private SQLiteStatementHandle()
        : base(IntPtr.Zero)
    {
      return new SQLiteStatementHandle(stmt);
    }

    private SQLiteStatementHandle(IntPtr stmt)
      : this()
    {
      SetHandle(stmt);
    }

    internal SQLiteStatementHandle()
      : base(IntPtr.Zero)
    internal SQLiteStatementHandle(SQLiteConnectionHandle cnn, IntPtr stmt)
        : this(stmt)
    {
      if (cnn != null)
        cnn.DangerousAddRef(ref releaseNeeded);

      this.cnn = cnn;
    }

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

        if (localHandle != IntPtr.Zero)
          SQLiteBase.FinalizeStatement(localHandle);
          SQLiteBase.FinalizeStatement(cnn, localHandle);

        if ((cnn != null) && releaseNeeded)
          cnn.DangerousRelease();

#if DEBUG && !NET_COMPACT_20
        try
        {
          Trace.WriteLine(String.Format(
              "FinalizeStatement: {0}", localHandle));
        }
        catch
        {
        }
#endif
#else
        if (handle != IntPtr.Zero)
        {
          SQLiteBase.FinalizeStatement(handle);
          SQLiteBase.FinalizeStatement(cnn, handle);
          SetHandle(IntPtr.Zero);
        }
#endif

#if DEBUG
        return true;
#endif
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
1605
1606
1607
1608
1609

1610
1611
1612
1613
1614
1615
1616
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
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621

1622
1623
1624
1625
1626
1627
1628
1629







+
+
+





-
+
+

-








-
-
+
+

+
+
+
+











-
+
+
+
+














-
+







    }
#endif
  }

  // Provides finalization support for unmanaged SQLite backup objects.
  internal class SQLiteBackupHandle : CriticalHandle
  {
      internal SQLiteConnectionHandle cnn;
      private bool releaseNeeded;

      public static implicit operator IntPtr(SQLiteBackupHandle backup)
      {
          return (backup != null) ? backup.handle : IntPtr.Zero;
      }

      public static implicit operator SQLiteBackupHandle(IntPtr backup)
      private SQLiteBackupHandle()
          : base(IntPtr.Zero)
      {
          return new SQLiteBackupHandle(backup);
      }

      private SQLiteBackupHandle(IntPtr backup)
          : this()
      {
          SetHandle(backup);
      }

      internal SQLiteBackupHandle()
          : base(IntPtr.Zero)
      internal SQLiteBackupHandle(SQLiteConnectionHandle cnn, IntPtr backup)
          : this(backup)
      {
          if (cnn != null)
              cnn.DangerousAddRef(ref releaseNeeded);

          this.cnn = cnn;
      }

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

              if (localHandle != IntPtr.Zero)
                  SQLiteBase.FinishBackup(localHandle);
                  SQLiteBase.FinishBackup(cnn, localHandle);

              if ((cnn != null) && releaseNeeded)
                  cnn.DangerousRelease();

#if DEBUG && !NET_COMPACT_20
              try
              {
                  Trace.WriteLine(String.Format(
                      "FinishBackup: {0}", localHandle));
              }
              catch
              {
              }
#endif
#else
              if (handle != IntPtr.Zero)
              {
                SQLiteBase.FinishBackup(handle);
                SQLiteBase.FinishBackup(cnn, handle);
                SetHandle(IntPtr.Zero);
              }
#endif

#if DEBUG
              return true;
#endif