System.Data.SQLite
Check-in [229ecb1203]
Not logged in

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

Overview
Comment:More changes relating to IDisposable.
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | IDisposablePattern
Files: files | file ages | folders
SHA1: 229ecb1203485224a40356db5c31d3ba8beda4b0
User & Date: mistachkin 2011-12-16 04:15:07
Context
2011-12-16
04:25
Compilation error fixes. check-in: 8dfdf59c72 user: mistachkin tags: IDisposablePattern
04:15
More changes relating to IDisposable. check-in: 229ecb1203 user: mistachkin tags: IDisposablePattern
03:32
Start of making sure that all core components containing IDisposable objects implement IDisposable and that they follow the established best-practices when doing so. check-in: c532a87e4d user: mistachkin tags: IDisposablePattern
Changes
Hide Diffs Side-by-Side Diffs Ignore Whitespace Patch

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

    41     41       }
    42     42   
    43     43       /// <summary>
    44     44       /// A single sub-query for a given table/database.
    45     45       /// </summary>
    46     46       private sealed class KeyQuery : IDisposable
    47     47       {
    48         -      private SQLiteCommand _command;
    49         -      internal SQLiteDataReader _reader;
           48  +        private SQLiteCommand _command;
           49  +        internal SQLiteDataReader _reader;
    50     50   
    51         -      internal KeyQuery(SQLiteConnection cnn, string database, string table, params string[] columns)
    52         -      {
    53         -        using (SQLiteCommandBuilder builder = new SQLiteCommandBuilder())
           51  +        internal KeyQuery(SQLiteConnection cnn, string database, string table, params string[] columns)
           52  +        {
           53  +            using (SQLiteCommandBuilder builder = new SQLiteCommandBuilder())
           54  +            {
           55  +                _command = cnn.CreateCommand();
           56  +                for (int n = 0; n < columns.Length; n++)
           57  +                {
           58  +                    columns[n] = builder.QuoteIdentifier(columns[n]);
           59  +                }
           60  +            }
           61  +            _command.CommandText = String.Format(CultureInfo.InvariantCulture, "SELECT {0} FROM [{1}].[{2}] WHERE ROWID = ?", String.Join(",", columns), database, table);
           62  +            _command.Parameters.AddWithValue(null, (long)0);
           63  +        }
           64  +
           65  +        internal bool IsValid
           66  +        {
           67  +            set
           68  +            {
           69  +                if (value != false) throw new ArgumentException();
           70  +                if (_reader != null)
           71  +                {
           72  +                    _reader.Dispose();
           73  +                    _reader = null;
           74  +                }
           75  +            }
           76  +        }
           77  +
           78  +        internal void Sync(long rowid)
           79  +        {
           80  +            IsValid = false;
           81  +            _command.Parameters[0].Value = rowid;
           82  +            _reader = _command.ExecuteReader();
           83  +            _reader.Read();
           84  +        }
           85  +
           86  +        ///////////////////////////////////////////////////////////////////////////////////////////////
           87  +
           88  +        #region IDisposable Members
           89  +        public void Dispose()
           90  +        {
           91  +            Dispose(true);
           92  +            GC.SuppressFinalize(this);
           93  +        }
           94  +        #endregion
           95  +
           96  +        ///////////////////////////////////////////////////////////////////////////////////////////////
           97  +
           98  +        #region IDisposable "Pattern" Members
           99  +        private bool disposed;
          100  +        private void CheckDisposed() /* throw */
          101  +        {
          102  +#if THROW_ON_DISPOSED
          103  +            if (disposed)
          104  +                throw new ObjectDisposedException(typeof(KeyQuery).Name);
          105  +#endif
          106  +        }
          107  +
          108  +        ///////////////////////////////////////////////////////////////////////////////////////////////
          109  +
          110  +        private void Dispose(bool disposing)
    54    111           {
    55         -          _command = cnn.CreateCommand();
    56         -          for (int n = 0; n < columns.Length; n++)
    57         -          {
    58         -            columns[n] = builder.QuoteIdentifier(columns[n]);
    59         -          }
          112  +            if (!disposed)
          113  +            {
          114  +                if (disposing)
          115  +                {
          116  +                    ////////////////////////////////////
          117  +                    // dispose managed resources here...
          118  +                    ////////////////////////////////////
          119  +
          120  +                    IsValid = false;
          121  +
          122  +                    if (_command != null) _command.Dispose();
          123  +                    _command = null;
          124  +                }
          125  +
          126  +                //////////////////////////////////////
          127  +                // release unmanaged resources here...
          128  +                //////////////////////////////////////
          129  +
          130  +                disposed = true;
          131  +            }
    60    132           }
    61         -        _command.CommandText = String.Format(CultureInfo.InvariantCulture, "SELECT {0} FROM [{1}].[{2}] WHERE ROWID = ?", String.Join(",", columns), database, table);
    62         -        _command.Parameters.AddWithValue(null, (long)0);
    63         -      }
          133  +        #endregion
    64    134   
    65         -      internal bool IsValid
    66         -      {
    67         -        set
          135  +        ///////////////////////////////////////////////////////////////////////////////////////////////
          136  +
          137  +        #region Destructor
          138  +        ~KeyQuery()
    68    139           {
    69         -          if (value != false) throw new ArgumentException();
    70         -          if (_reader != null)
    71         -          {
    72         -            _reader.Dispose();
    73         -            _reader = null;
    74         -          }
          140  +            Dispose(false);
    75    141           }
    76         -      }
    77         -
    78         -      internal void Sync(long rowid)
    79         -      {
    80         -        IsValid = false;
    81         -        _command.Parameters[0].Value = rowid;
    82         -        _reader = _command.ExecuteReader();
    83         -        _reader.Read();
    84         -      }
    85         -
    86         -      public void Dispose()
    87         -      {
    88         -        IsValid = false;
    89         -
    90         -        if (_command != null) _command.Dispose();
    91         -        _command = null;
    92         -      }
          142  +        #endregion
    93    143       }
    94    144   
    95    145       /// <summary>
    96    146       /// This function does all the nasty work at determining what keys need to be returned for
    97    147       /// a given statement.
    98    148       /// </summary>
    99    149       /// <param name="cnn"></param>
................................................................................
   255    305   
   256    306         // Now we have all the additional columns we have to return in order to support
   257    307         // CommandBehavior.KeyInfo
   258    308         _keyInfo = new KeyInfo[keys.Count];
   259    309         keys.CopyTo(_keyInfo);
   260    310       }
   261    311   
          312  +    ///////////////////////////////////////////////////////////////////////////////////////////////
          313  +
          314  +    #region IDisposable Members
          315  +    public void Dispose()
          316  +    {
          317  +        Dispose(true);
          318  +        GC.SuppressFinalize(this);
          319  +    }
          320  +    #endregion
          321  +
          322  +    ///////////////////////////////////////////////////////////////////////////////////////////////
          323  +
          324  +    #region IDisposable "Pattern" Members
          325  +    private bool disposed;
          326  +    private void CheckDisposed() /* throw */
          327  +    {
          328  +#if THROW_ON_DISPOSED
          329  +        if (disposed)
          330  +            throw new ObjectDisposedException(typeof(SQLiteKeyReader).Name);
          331  +#endif
          332  +    }
          333  +
          334  +    ///////////////////////////////////////////////////////////////////////////////////////////////
          335  +
          336  +    private void Dispose(bool disposing)
          337  +    {
          338  +        if (!disposed)
          339  +        {
          340  +            if (disposing)
          341  +            {
          342  +                ////////////////////////////////////
          343  +                // dispose managed resources here...
          344  +                ////////////////////////////////////
          345  +
          346  +                _stmt = null;
          347  +
          348  +                if (_keyInfo == null) return;
          349  +
          350  +                for (int n = 0; n < _keyInfo.Length; n++)
          351  +                {
          352  +                    if (_keyInfo[n].query != null)
          353  +                        _keyInfo[n].query.Dispose();
          354  +                }
          355  +
          356  +                _keyInfo = null;
          357  +            }
          358  +
          359  +            //////////////////////////////////////
          360  +            // release unmanaged resources here...
          361  +            //////////////////////////////////////
          362  +
          363  +            disposed = true;
          364  +        }
          365  +    }
          366  +    #endregion
          367  +
          368  +    ///////////////////////////////////////////////////////////////////////////////////////////////
          369  +
          370  +    #region Destructor
          371  +    ~SQLiteKeyReader()
          372  +    {
          373  +        Dispose(false);
          374  +    }
          375  +    #endregion
          376  +
          377  +    ///////////////////////////////////////////////////////////////////////////////////////////////
          378  +
   262    379       /// <summary>
   263    380       /// How many additional columns of keyinfo we're holding
   264    381       /// </summary>
   265    382       internal int Count
   266    383       {
   267    384         get { return (_keyInfo == null) ? 0 : _keyInfo.Length; }
   268    385       }
................................................................................
   310    427         for (int n = 0; n < _keyInfo.Length; n++)
   311    428         {
   312    429           if (_keyInfo[n].query != null)
   313    430             _keyInfo[n].query.IsValid = false;
   314    431         }
   315    432       }
   316    433   
   317         -    public void Dispose()
   318         -    {
   319         -      _stmt = null;
   320         -
   321         -      if (_keyInfo == null) return;
   322         -
   323         -      for (int n = 0; n < _keyInfo.Length; n++)
   324         -      {
   325         -        if (_keyInfo[n].query != null)
   326         -          _keyInfo[n].query.Dispose();
   327         -      }
   328         -      _keyInfo = null;
   329         -    }
   330         -
   331    434       internal string GetDataTypeName(int i)
   332    435       {
   333    436         Sync();
   334    437         if (_keyInfo[i].query != null) return _keyInfo[i].query._reader.GetDataTypeName(_keyInfo[i].column);
   335    438         else return "integer";
   336    439       }
   337    440   

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

    84     84             }
    85     85             _paramNames[x] = s;
    86     86             _paramValues[x] = null;
    87     87           }
    88     88         }
    89     89       }
    90     90   
           91  +    ///////////////////////////////////////////////////////////////////////////////////////////////
           92  +
           93  +    #region IDisposable Members
           94  +    /// <summary>
           95  +    /// Disposes and finalizes the statement
           96  +    /// </summary>
           97  +    public void Dispose()
           98  +    {
           99  +        Dispose(true);
          100  +        GC.SuppressFinalize(this);
          101  +    }
          102  +    #endregion
          103  +
          104  +    ///////////////////////////////////////////////////////////////////////////////////////////////
          105  +
          106  +    #region IDisposable "Pattern" Members
          107  +    private bool disposed;
          108  +    private void CheckDisposed() /* throw */
          109  +    {
          110  +#if THROW_ON_DISPOSED
          111  +        if (disposed)
          112  +            throw new ObjectDisposedException(typeof(SQLiteStatement).Name);
          113  +#endif
          114  +    }
          115  +
          116  +    ///////////////////////////////////////////////////////////////////////////////////////////////
          117  +
          118  +    protected virtual void Dispose(bool disposing)
          119  +    {
          120  +        if (!disposed)
          121  +        {
          122  +            if (disposing)
          123  +            {
          124  +                ////////////////////////////////////
          125  +                // dispose managed resources here...
          126  +                ////////////////////////////////////
          127  +
          128  +                if (_sqlite_stmt != null)
          129  +                {
          130  +                    _sqlite_stmt.Dispose();
          131  +                    _sqlite_stmt = null;
          132  +                }
          133  +
          134  +                _paramNames = null;
          135  +                _paramValues = null;
          136  +                _sql = null;
          137  +                _sqlStatement = null;
          138  +            }
          139  +
          140  +            //////////////////////////////////////
          141  +            // release unmanaged resources here...
          142  +            //////////////////////////////////////
          143  +
          144  +            disposed = true;
          145  +        }
          146  +    }
          147  +    #endregion
          148  +
          149  +    ///////////////////////////////////////////////////////////////////////////////////////////////
          150  +
          151  +    #region Destructor
          152  +    ~SQLiteStatement()
          153  +    {
          154  +        Dispose(false);
          155  +    }
          156  +    #endregion
          157  +
          158  +    ///////////////////////////////////////////////////////////////////////////////////////////////
          159  +
    91    160       /// <summary>
    92    161       /// Called by SQLiteParameterCollection, this function determines if the specified parameter name belongs to
    93    162       /// this statement, and if so, keeps a reference to the parameter so it can be bound later.
    94    163       /// </summary>
    95    164       /// <param name="s">The parameter name to map</param>
    96    165       /// <param name="p">The parameter to assign it</param>
    97    166       internal bool MapParameter(string s, SQLiteParameter p)
................................................................................
   113    182             _paramValues[n] = p;
   114    183             return true;
   115    184           }
   116    185         }
   117    186         return false;
   118    187       }
   119    188   
   120         -    #region IDisposable Members
   121         -    /// <summary>
   122         -    /// Disposes and finalizes the statement
   123         -    /// </summary>
   124         -    public void Dispose()
   125         -    {
   126         -      if (_sqlite_stmt != null)
   127         -      {
   128         -        _sqlite_stmt.Dispose();
   129         -      }
   130         -      _sqlite_stmt = null;
   131         -      
   132         -      _paramNames = null;
   133         -      _paramValues = null;
   134         -      _sql = null;
   135         -      _sqlStatement = null;
   136         -    }
   137         -    #endregion
   138         -    
   139    189       /// <summary>
   140    190       ///  Bind all parameters, making sure the caller didn't miss any
   141    191       /// </summary>
   142    192       internal void BindParameters()
   143    193       {
   144    194         if (_paramNames == null) return;
   145    195   

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

    55     55             _cnn._transactionLevel--;
    56     56             _cnn = null;
    57     57             throw;
    58     58           }
    59     59         }
    60     60       }
    61     61   
           62  +    ///////////////////////////////////////////////////////////////////////////////////////////////
           63  +
           64  +    #region IDisposable "Pattern" Members
           65  +    private bool disposed;
           66  +    private void CheckDisposed() /* throw */
           67  +    {
           68  +#if THROW_ON_DISPOSED
           69  +        if (disposed)
           70  +            throw new ObjectDisposedException(typeof(SQLiteTransaction).Name);
           71  +#endif
           72  +    }
           73  +
           74  +    ///////////////////////////////////////////////////////////////////////////////////////////////
           75  +
           76  +    /// <summary>
           77  +    /// Disposes the transaction.  If it is currently active, any changes are rolled back.
           78  +    /// </summary>
           79  +    protected override void Dispose(bool disposing)
           80  +    {
           81  +        try
           82  +        {
           83  +            if (!disposed)
           84  +            {
           85  +                if (disposing)
           86  +                {
           87  +                    ////////////////////////////////////
           88  +                    // dispose managed resources here...
           89  +                    ////////////////////////////////////
           90  +
           91  +                    if (IsValid(false))
           92  +                    {
           93  +                        IssueRollback();
           94  +                    }
           95  +                }
           96  +
           97  +                //////////////////////////////////////
           98  +                // release unmanaged resources here...
           99  +                //////////////////////////////////////
          100  +
          101  +                disposed = true;
          102  +            }
          103  +        }
          104  +        finally
          105  +        {
          106  +            base.Dispose(disposing);
          107  +        }
          108  +    }
          109  +    #endregion
          110  +
          111  +    ///////////////////////////////////////////////////////////////////////////////////////////////
          112  +
    62    113       /// <summary>
    63    114       /// Commits the current transaction.
    64    115       /// </summary>
    65    116       public override void Commit()
    66    117       {
          118  +      CheckDisposed();
    67    119         IsValid(true);
    68    120   
    69    121         if (_cnn._transactionLevel - 1 == 0)
    70    122         {
    71    123           using (SQLiteCommand cmd = _cnn.CreateCommand())
    72    124           {
    73    125             cmd.CommandText = "COMMIT";
................................................................................
    79    131       }
    80    132   
    81    133       /// <summary>
    82    134       /// Returns the underlying connection to which this transaction applies.
    83    135       /// </summary>
    84    136       public new SQLiteConnection Connection
    85    137       {
    86         -      get { return _cnn; }
          138  +      get { CheckDisposed(); return _cnn; }
    87    139       }
    88    140   
    89    141       /// <summary>
    90    142       /// Forwards to the local Connection property
    91    143       /// </summary>
    92    144       protected override DbConnection DbConnection
    93    145       {
    94    146         get { return Connection; }
    95    147       }
    96    148   
    97         -    /// <summary>
    98         -    /// Disposes the transaction.  If it is currently active, any changes are rolled back.
    99         -    /// </summary>
   100         -    protected override void Dispose(bool disposing)
   101         -    {
   102         -      if (disposing)
   103         -      {
   104         -        if (IsValid(false))
   105         -        {
   106         -          IssueRollback();
   107         -        }
   108         -      }
   109         -      base.Dispose(disposing);
   110         -    }
   111         -
   112    149       /// <summary>
   113    150       /// Gets the isolation level of the transaction.  SQLite only supports Serializable transactions.
   114    151       /// </summary>
   115    152       public override IsolationLevel IsolationLevel
   116    153       {
   117         -      get { return _level; }
          154  +      get { CheckDisposed(); return _level; }
   118    155       }
   119    156   
   120    157       /// <summary>
   121    158       /// Rolls back the active transaction.
   122    159       /// </summary>
   123    160       public override void Rollback()
   124    161       {
          162  +      CheckDisposed();
   125    163         IsValid(true);
   126    164         IssueRollback();
   127    165       }
   128    166   
   129    167       internal void IssueRollback()
   130    168       {
   131    169         SQLiteConnection cnn = Interlocked.Exchange(ref _cnn, null);

Changes to System.Data.SQLite/SR.Designer.cs.

            1  +/********************************************************
            2  + * ADO.NET 2.0 Data Provider for SQLite Version 3.X
            3  + * Written by Robert Simpson (robert@blackcastlesoft.com)
            4  + * 
            5  + * Released to the public domain, use at your own risk!
            6  + ********************************************************/
            7  +
     1      8   //------------------------------------------------------------------------------
     2      9   // <auto-generated>
     3     10   //     This code was generated by a tool.
     4     11   //     Runtime Version:4.0.30319.1
     5     12   //
     6     13   //     Changes to this file may cause incorrect behavior and will be lost if
     7     14   //     the code is regenerated.