System.Data.SQLite

Check-in [7aae5042f8]
Login

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

Overview
Comment:Make the error codes used with the SQLiteException class consistent. Throw an exception if the SQLite3.Open method is called when already open.
Downloads: Tarball | ZIP archive
Timelines: family | ancestors | descendants | both | virtualTables
Files: files | file ages | folders
SHA1: 7aae5042f8f7773f6602158aec1b9341f1e151e6
User & Date: mistachkin 2013-06-22 01:35:35.476
Context
2013-06-22
01:39
Cleanup several test variables. check-in: b9dfe7578d user: mistachkin tags: virtualTables
01:35
Make the error codes used with the SQLiteException class consistent. Throw an exception if the SQLite3.Open method is called when already open. check-in: 7aae5042f8 user: mistachkin tags: virtualTables
00:52
Improve diagnostics for test data-1.1. Fix connection handle leak by removing superfluous _ownHandle fields and add properties instead. check-in: 0e4ebe697a user: mistachkin tags: virtualTables
Changes
Unified Diff Ignore Whitespace Patch
Changes to System.Data.SQLite/SQLite3.cs.
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
    /// by this instance.
    /// </summary>
    internal override bool OwnHandle
    {
        get
        {
            if (_sql == null)
            {
                throw new SQLiteException(SQLiteErrorCode.Error,
                    "no connection handle available");
            }

            return _sql.OwnHandle;
        }
    }

    internal override SQLiteErrorCode SetMemoryStatus(bool value)
    {







<
|
<
<







343
344
345
346
347
348
349

350


351
352
353
354
355
356
357
    /// by this instance.
    /// </summary>
    internal override bool OwnHandle
    {
        get
        {
            if (_sql == null)

                throw new SQLiteException("no connection handle available");



            return _sql.OwnHandle;
        }
    }

    internal override SQLiteErrorCode SetMemoryStatus(bool value)
    {
383
384
385
386
387
388
389
390



391
392
393
394
395
396
397
    internal override bool IsOpen()
    {
        return (_sql != null) && !_sql.IsInvalid && !_sql.IsClosed;
    }

    internal override void Open(string strFilename, SQLiteConnectionFlags connectionFlags, SQLiteOpenFlagsEnum openFlags, int maxPoolSize, bool usePool)
    {
      if (_sql != null) return;




      _usePool = usePool;
      _fileName = strFilename;

      if (usePool)
      {
        _sql = SQLiteConnectionPool.Remove(strFilename, maxPoolSize, out _poolVersion);







|
>
>
>







380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
    internal override bool IsOpen()
    {
        return (_sql != null) && !_sql.IsInvalid && !_sql.IsClosed;
    }

    internal override void Open(string strFilename, SQLiteConnectionFlags connectionFlags, SQLiteOpenFlagsEnum openFlags, int maxPoolSize, bool usePool)
    {
      if (_sql != null) Close(true);

      if (_sql != null)
          throw new SQLiteException("connection handle is still active");

      _usePool = usePool;
      _fileName = strFilename;

      if (usePool)
      {
        _sql = SQLiteConnectionPool.Remove(strFilename, maxPoolSize, out _poolVersion);
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
          // Reassign a new statement pointer to the old statement and clear the temporary one
          stmt._sqlite_stmt = tmp._sqlite_stmt;
          tmp._sqlite_stmt = null;

          // Reapply parameters
          stmt.BindParameters();
        }
        return (SQLiteErrorCode)(-1); // Reset was OK, with schema change
      }
      else if (n == SQLiteErrorCode.Locked || n == SQLiteErrorCode.Busy)
        return n;

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








|







533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
          // Reassign a new statement pointer to the old statement and clear the temporary one
          stmt._sqlite_stmt = tmp._sqlite_stmt;
          tmp._sqlite_stmt = null;

          // Reapply parameters
          stmt.BindParameters();
        }
        return SQLiteErrorCode.Unknown; // Reset was OK, with schema change
      }
      else if (n == SQLiteErrorCode.Locked || n == SQLiteErrorCode.Busy)
        return n;

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

1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
                if (_modules == null)
                    _modules = new Dictionary<string, SQLiteModule>();

                _modules.Add(module.Name, module);
            }
            else
            {
                throw new SQLiteException(SQLiteErrorCode.Error, GetLastError());
            }
        }
        finally
        {
            if (pName != IntPtr.Zero)
            {
                SQLiteMemory.Free(pName);







|







1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
                if (_modules == null)
                    _modules = new Dictionary<string, SQLiteModule>();

                _modules.Add(module.Name, module);
            }
            else
            {
                throw new SQLiteException(GetLastError());
            }
        }
        finally
        {
            if (pName != IntPtr.Zero)
            {
                SQLiteMemory.Free(pName);
Changes to System.Data.SQLite/SQLite3_UTF16.cs.
96
97
98
99
100
101
102
103



104
105
106
107
108
109
110
        return Marshal.PtrToStringUni(b);
      else
        return Marshal.PtrToStringUni(b, nbytelen / 2);
    }

    internal override void Open(string strFilename, SQLiteConnectionFlags connectionFlags, SQLiteOpenFlagsEnum openFlags, int maxPoolSize, bool usePool)
    {
      if (_sql != null) return;




      _usePool = usePool;
      _fileName = strFilename;

      if (usePool)
      {
        _sql = SQLiteConnectionPool.Remove(strFilename, maxPoolSize, out _poolVersion);







|
>
>
>







96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
        return Marshal.PtrToStringUni(b);
      else
        return Marshal.PtrToStringUni(b, nbytelen / 2);
    }

    internal override void Open(string strFilename, SQLiteConnectionFlags connectionFlags, SQLiteOpenFlagsEnum openFlags, int maxPoolSize, bool usePool)
    {
      if (_sql != null) Close(true);

      if (_sql != null)
          throw new SQLiteException("connection handle is still active");

      _usePool = usePool;
      _fileName = strFilename;

      if (usePool)
      {
        _sql = SQLiteConnectionPool.Remove(strFilename, maxPoolSize, out _poolVersion);
Changes to System.Data.SQLite/SQLiteDataReader.cs.
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
      if (!_throwOnDisposed)
        return;

      if (_command == null)
        throw new InvalidOperationException("DataReader has been closed");

      if (_version == 0)
        throw new SQLiteException(SQLiteErrorCode.Abort, "Execution was aborted by the user");

      if (_command.Connection.State != ConnectionState.Open || _command.Connection._version != _version)
        throw new InvalidOperationException("Connection was closed, statement was terminated");
    }

    /// <summary>
    /// Throw an error if a row is not loaded







|







232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
      if (!_throwOnDisposed)
        return;

      if (_command == null)
        throw new InvalidOperationException("DataReader has been closed");

      if (_version == 0)
        throw new SQLiteException("Execution was aborted by the user");

      if (_command.Connection.State != ConnectionState.Open || _command.Connection._version != _version)
        throw new InvalidOperationException("Connection was closed, statement was terminated");
    }

    /// <summary>
    /// Throw an error if a row is not loaded
Changes to System.Data.SQLite/SQLiteException.cs.
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77

    /// <summary>
    /// Public constructor that uses the base class constructor for the error
    /// message.
    /// </summary>
    /// <param name="message">Error message text.</param>
    public SQLiteException(string message)
      : base(message)
    {
    }

    /// <summary>
    /// Public constructor that uses the default base class constructor.
    /// </summary>
    public SQLiteException()







|







63
64
65
66
67
68
69
70
71
72
73
74
75
76
77

    /// <summary>
    /// Public constructor that uses the base class constructor for the error
    /// message.
    /// </summary>
    /// <param name="message">Error message text.</param>
    public SQLiteException(string message)
      : this(SQLiteErrorCode.Unknown, message)
    {
    }

    /// <summary>
    /// Public constructor that uses the default base class constructor.
    /// </summary>
    public SQLiteException()
196
197
198
199
200
201
202





203
204
205
206
207
208
209
  /// SQLite error codes.  Actually, this enumeration represents a return code,
  /// which may also indicate success in one of several ways (e.g. SQLITE_OK,
  /// SQLITE_ROW, and SQLITE_DONE).  Therefore, the name of this enumeration is
  /// something of a misnomer.
  /// </summary>
  public enum SQLiteErrorCode
  {





    /// <summary>
    /// Successful result
    /// </summary>
    Ok /* 0 */,
    /// <summary>
    /// SQL error or missing database
    /// </summary>







>
>
>
>
>







196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
  /// SQLite error codes.  Actually, this enumeration represents a return code,
  /// which may also indicate success in one of several ways (e.g. SQLITE_OK,
  /// SQLITE_ROW, and SQLITE_DONE).  Therefore, the name of this enumeration is
  /// something of a misnomer.
  /// </summary>
  public enum SQLiteErrorCode
  {
    /// <summary>
    /// The error code is unknown.  This error code
    /// is only used by the managed wrapper itself.
    /// </summary>
    Unknown = -1,
    /// <summary>
    /// Successful result
    /// </summary>
    Ok /* 0 */,
    /// <summary>
    /// SQL error or missing database
    /// </summary>
Changes to System.Data.SQLite/SQLiteModule.cs.
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
            )
        {
            CheckDisposed();

            if ((values != null) &&
                (TryPersistValues(values) != values.Length))
            {
                throw new SQLiteException(SQLiteErrorCode.Error,
                    "failed to persist one or more values");
            }

            this.indexNumber = indexNumber;
            this.indexString = indexString;
            this.values = values;
        }







|







910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
            )
        {
            CheckDisposed();

            if ((values != null) &&
                (TryPersistValues(values) != values.Length))
            {
                throw new SQLiteException(
                    "failed to persist one or more values");
            }

            this.indexNumber = indexNumber;
            this.indexString = indexString;
            this.values = values;
        }
Changes to System.Data.SQLite/SQLiteStatement.cs.
274
275
276
277
278
279
280
281
282
283

284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
            case TypeCode.Double:
                return ((double)obj) != (double)0.0 ? true : false;
            case TypeCode.Decimal:
                return ((decimal)obj) != Decimal.Zero ? true : false;
            case TypeCode.String:
                return Convert.ToBoolean(obj, provider);
            default:
                throw new SQLiteException(SQLiteErrorCode.Error,
                    String.Format(CultureInfo.CurrentCulture,
                    "Cannot convert type {0} to boolean", typeCode));

        }
    }

    /// <summary>
    /// Perform the bind operation for an individual parameter
    /// </summary>
    /// <param name="index">The index of the parameter to bind</param>
    /// <param name="param">The parameter we're binding</param>
    private void BindParameter(int index, SQLiteParameter param)
    {
      if (param == null)
        throw new SQLiteException(SQLiteErrorCode.Error, "Insufficient parameters supplied to the command");

      object obj = param.Value;
      DbType objType = param.DbType;

      if ((obj != null) && (objType == DbType.Object))
          objType = SQLiteConvert.TypeToDbType(obj.GetType());








|
|
|
>











|







274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
            case TypeCode.Double:
                return ((double)obj) != (double)0.0 ? true : false;
            case TypeCode.Decimal:
                return ((decimal)obj) != Decimal.Zero ? true : false;
            case TypeCode.String:
                return Convert.ToBoolean(obj, provider);
            default:
                throw new SQLiteException(String.Format(
                    CultureInfo.CurrentCulture,
                    "Cannot convert type {0} to boolean",
                    typeCode));
        }
    }

    /// <summary>
    /// Perform the bind operation for an individual parameter
    /// </summary>
    /// <param name="index">The index of the parameter to bind</param>
    /// <param name="param">The parameter we're binding</param>
    private void BindParameter(int index, SQLiteParameter param)
    {
      if (param == null)
        throw new SQLiteException("Insufficient parameters supplied to the command");

      object obj = param.Value;
      DbType objType = param.DbType;

      if ((obj != null) && (objType == DbType.Object))
          objType = SQLiteConvert.TypeToDbType(obj.GetType());

Changes to System.Data.SQLite/SQLiteTransaction.cs.
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
      {
        if (throwError == true) throw new ArgumentNullException("No connection associated with this transaction");
        else return false;
      }

      if (_cnn._version != _version)
      {
        if (throwError == true) throw new SQLiteException(SQLiteErrorCode.Misuse, "The connection was closed and re-opened, changes were already rolled back");
        else return false;
      }
      if (_cnn.State != ConnectionState.Open)
      {
        if (throwError == true) throw new SQLiteException(SQLiteErrorCode.Misuse, "Connection was closed");
        else return false;
      }

      if (_cnn._transactionLevel == 0 || _cnn._sql.AutoCommit == true)
      {
        _cnn._transactionLevel = 0; // Make sure the transaction level is reset before returning
        if (throwError == true) throw new SQLiteException(SQLiteErrorCode.Misuse, "No transaction is active on this connection");
        else return false;
      }

      return true;
    }
  }
}







|




|






|







195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
      {
        if (throwError == true) throw new ArgumentNullException("No connection associated with this transaction");
        else return false;
      }

      if (_cnn._version != _version)
      {
        if (throwError == true) throw new SQLiteException("The connection was closed and re-opened, changes were already rolled back");
        else return false;
      }
      if (_cnn.State != ConnectionState.Open)
      {
        if (throwError == true) throw new SQLiteException("Connection was closed");
        else return false;
      }

      if (_cnn._transactionLevel == 0 || _cnn._sql.AutoCommit == true)
      {
        _cnn._transactionLevel = 0; // Make sure the transaction level is reset before returning
        if (throwError == true) throw new SQLiteException("No transaction is active on this connection");
        else return false;
      }

      return true;
    }
  }
}