System.Data.SQLite

Check-in [6fa1fe8314]
Login

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

Overview
Comment:Fix implementation and tests for SQLiteCommand.Reset method. Remove (now) superfluous new code.
Downloads: Tarball | ZIP archive
Timelines: family | ancestors | descendants | both | cmdReset
Files: files | file ages | folders
SHA1: 6fa1fe8314bac00de20b3f01457cefe6d022cee8
User & Date: mistachkin 2015-08-28 22:16:46.887
Context
2015-09-09
23:44
Add Reset method to the SQLiteCommand class. check-in: 3e82d3be5b user: mistachkin tags: trunk
2015-08-28
22:16
Fix implementation and tests for SQLiteCommand.Reset method. Remove (now) superfluous new code. Closed-Leaf check-in: 6fa1fe8314 user: mistachkin tags: cmdReset
20:26
Work in progress on exerpimental method to reset the statements associated with a SQLiteCommand. check-in: f12609cfe9 user: mistachkin tags: cmdReset
Changes
Unified Diff Ignore Whitespace Patch
Changes to System.Data.SQLite/SQLiteCommand.cs.
1
2
3
4
5
6
7
8
9
10
11
/********************************************************
 * ADO.NET 2.0 Data Provider for SQLite Version 3.X
 * Written by Robert Simpson (robert@blackcastlesoft.com)
 * 
 * Released to the public domain, use at your own risk!
 ********************************************************/

namespace System.Data.SQLite
{
  using System;
  using System.Data;



|







1
2
3
4
5
6
7
8
9
10
11
/********************************************************
 * ADO.NET 2.0 Data Provider for SQLite Version 3.X
 * Written by Robert Simpson (robert@blackcastlesoft.com)
 *
 * Released to the public domain, use at your own risk!
 ********************************************************/

namespace System.Data.SQLite
{
  using System;
  using System.Data;
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
111
112
113
114
115
116
117
118
119
120
    {
    }

    /// <summary>
    /// Initializes the command with the given command text
    /// </summary>
    /// <param name="commandText">The SQL command text</param>
    public SQLiteCommand(string commandText) 
      : this(commandText, null, null)
    {
    }

    /// <summary>
    /// Initializes the command with the given SQL command text and attach the command to the specified
    /// connection.
    /// </summary>
    /// <param name="commandText">The SQL command text</param>
    /// <param name="connection">The connection to associate with the command</param>
    public SQLiteCommand(string commandText, SQLiteConnection connection)
      : this(commandText, connection, null)
    {
    }

    /// <summary>
    /// Initializes the command and associates it with the specified connection.
    /// </summary>
    /// <param name="connection">The connection to associate with the command</param>
    public SQLiteCommand(SQLiteConnection connection) 
      : this(null, connection, null)
    {
    }

    private SQLiteCommand(SQLiteCommand source) : this(source.CommandText, source.Connection, source.Transaction)
    {
      CommandTimeout = source.CommandTimeout;







|



















|







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
111
112
113
114
115
116
117
118
119
120
    {
    }

    /// <summary>
    /// Initializes the command with the given command text
    /// </summary>
    /// <param name="commandText">The SQL command text</param>
    public SQLiteCommand(string commandText)
      : this(commandText, null, null)
    {
    }

    /// <summary>
    /// Initializes the command with the given SQL command text and attach the command to the specified
    /// connection.
    /// </summary>
    /// <param name="commandText">The SQL command text</param>
    /// <param name="connection">The connection to associate with the command</param>
    public SQLiteCommand(string commandText, SQLiteConnection connection)
      : this(commandText, connection, null)
    {
    }

    /// <summary>
    /// Initializes the command and associates it with the specified connection.
    /// </summary>
    /// <param name="connection">The connection to associate with the command</param>
    public SQLiteCommand(SQLiteConnection connection)
      : this(null, connection, null)
    {
    }

    private SQLiteCommand(SQLiteCommand source) : this(source.CommandText, source.Connection, source.Transaction)
    {
      CommandTimeout = source.CommandTimeout;
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316

317
318
319
320
321
322

323
324
325
326
327
328
329
330









331
332
333
334
335
336
337
        }

        _statementList = null;
    }

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

    /// <summary>
    /// Clears and destroys all statements currently prepared
    /// </summary>
    internal void ClearCommands()
    {
      if (_activeReader != null)
      {
        SQLiteDataReader reader = null;

        try
        {
          reader = _activeReader.Target as SQLiteDataReader;
        }
        catch(InvalidOperationException)
        {

        }

        if (reader != null)
          reader.Close();

        _activeReader = null;
      }










      DisposeStatements();

      _parameterCollection.Unbind();
    }

    /// <summary>
    /// Builds an array of prepared statements for each complete SQL statement in the command text







<
<
<
|




>






>



|



|
>
>
>
>
>
>
>
>
>







302
303
304
305
306
307
308



309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
        }

        _statementList = null;
    }

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




    private void ClearDataReader()
    {
      if (_activeReader != null)
      {
        SQLiteDataReader reader = null;

        try
        {
          reader = _activeReader.Target as SQLiteDataReader;
        }
        catch(InvalidOperationException)
        {
          // do nothing.
        }

        if (reader != null)
          reader.Close(); /* Dispose */

        _activeReader = null;
      }
    }

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

    /// <summary>
    /// Clears and destroys all statements currently prepared
    /// </summary>
    internal void ClearCommands()
    {
      ClearDataReader();
      DisposeStatements();

      _parameterCollection.Unbind();
    }

    /// <summary>
    /// Builds an array of prepared statements for each complete SQL statement in the command text
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
      SQLiteConnection.Check(_cnn);
      return ExecuteReader(CommandBehavior.Default);
    }

    /// <summary>
    /// Called by the SQLiteDataReader when the data reader is closed.
    /// </summary>
    internal void ClearDataReader()
    {
      _activeReader = null;
    }

    /// <summary>
    /// Execute the command and return the number of rows inserted/updated affected by it.
    /// </summary>







|







885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
      SQLiteConnection.Check(_cnn);
      return ExecuteReader(CommandBehavior.Default);
    }

    /// <summary>
    /// Called by the SQLiteDataReader when the data reader is closed.
    /// </summary>
    internal void ResetDataReader()
    {
      _activeReader = null;
    }

    /// <summary>
    /// Execute the command and return the number of rows inserted/updated affected by it.
    /// </summary>
982
983
984
985
986
987
988


989
990
991
992
993
994
995
        )
    {
        CheckDisposed();
        SQLiteConnection.Check(_cnn);

        if (clearBindings && (_parameterCollection != null))
            _parameterCollection.Unbind();



        if (_statementList == null)
            return;

        SQLiteBase sqlBase = _cnn._sql;
        SQLiteErrorCode rc;








>
>







990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
        )
    {
        CheckDisposed();
        SQLiteConnection.Check(_cnn);

        if (clearBindings && (_parameterCollection != null))
            _parameterCollection.Unbind();

        ClearDataReader();

        if (_statementList == null)
            return;

        SQLiteBase sqlBase = _cnn._sql;
        SQLiteErrorCode rc;

Changes to System.Data.SQLite/SQLiteDataReader.cs.
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
                  {
                  }
                }
                catch(SQLiteException)
                {
                }
              }
              _command.ClearDataReader();
            }
            finally
            {
              // If the datareader's behavior includes closing the connection, then do so here.
              if ((_commandBehavior & CommandBehavior.CloseConnection) != 0 && _command.Connection != null)
                _command.Connection.Close();
            }







|







220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
                  {
                  }
                }
                catch(SQLiteException)
                {
                }
              }
              _command.ResetDataReader();
            }
            finally
            {
              // If the datareader's behavior includes closing the connection, then do so here.
              if ((_commandBehavior & CommandBehavior.CloseConnection) != 0 && _command.Connection != null)
                _command.Connection.Close();
            }
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
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
            typ.Affinity = _activeStatement._sql.ColumnAffinity(
                _activeStatement, i);
        }

        return typ;
    }

    /// <summary>
    /// This method resets all the prepared statements referenced by this
    /// instance back to their initial states, ready to be re-executed.
    /// </summary>
    public void Reset()
    {
        CheckDisposed();
        CheckClosed();

        if (_command == null)
            return;

        _command.Reset();
    }

    /// <summary>
    /// This method resets all the prepared statements referenced by this
    /// instance back to their initial states, ready to be re-executed.
    /// </summary>
    /// <param name="clearBindings">
    /// Non-zero if the parameter bindings should be cleared as well.
    /// </param>
    /// <param name="ignoreErrors">
    /// If this is zero, a <see cref="SQLiteException" /> may be thrown for
    /// any unsuccessful return codes from the native library; otherwise, a
    /// <see cref="SQLiteException" /> will only be thrown if the connection
    /// or its state is invalid.
    /// </param>
    public void Reset(
        bool clearBindings,
        bool ignoreErrors
        )
    {
        CheckDisposed();
        CheckClosed();

        if (_command == null)
            return;

        _command.Reset(clearBindings, ignoreErrors);
    }

    /// <summary>
    /// Reads the next row from the resultset
    /// </summary>
    /// <returns>True if a new row was successfully loaded and is ready for processing</returns>
    public override bool Read()
    {
      CheckDisposed();







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







1545
1546
1547
1548
1549
1550
1551










































1552
1553
1554
1555
1556
1557
1558
            typ.Affinity = _activeStatement._sql.ColumnAffinity(
                _activeStatement, i);
        }

        return typ;
    }











































    /// <summary>
    /// Reads the next row from the resultset
    /// </summary>
    /// <returns>True if a new row was successfully loaded and is ready for processing</returns>
    public override bool Read()
    {
      CheckDisposed();
Changes to Tests/basic.eagle.
3853
3854
3855
3856
3857
3858
3859
3860
3861
3862
3863
3864
3865
3866
3867
3868
3869
3870
3871
3872
3873
3874


3875




3876

3877

3878
3879
3880
3881
3882
3883
3884
3885
3886
3887
3888
3889
3890
3891
3892

3893
3894
3895


3896
3897
3898
3899
3900
3901
3902
3903
3904
3905
3906
3907
3908
} -constraints {eagle command.object monoBug28 command.sql compile.DATA SQLite\
System.Data.SQLite} -match regexp -result {^0 -1 0 -1 0 -1 0 -1 0 -1 0 -1 0 \{1\
2 3 A a M m Z z\} True 1 True 1 True 1 True 1 True 1 True 1 True 1 True 1\
\{(?:-)?\d+ (?:-)?\d+\}$}}

###############################################################################

runTest {test data-1.75 {Reset method} -setup {
  setupDb [set fileName data-1.75.db]
} -body {
  set connection [getDbConnection]

  set result [list]

  sql execute $db {
    CREATE TABLE t1(x);
    INSERT INTO t1 (x) VALUES(1);
    INSERT INTO t1 (x) VALUES(2);
    INSERT INTO t1 (x) VALUES(3);
    INSERT INTO t1 (x) VALUES(4);
  }



  set dataReader [sql execute -execute reader -format datareader \




      -alias $db "SELECT x FROM t1 WHERE x < ? ORDER BY x;" \

      [list param1 Int32 4]]


  $dataReader Read; lappend result [$dataReader Item x]
  $dataReader Read; lappend result [$dataReader Item x]

  $dataReader Reset; $dataReader NextResult; # TODO: Fix me.
  $dataReader Read; lappend result [$dataReader Item x]
  $dataReader Read; lappend result [$dataReader Item x]

  $dataReader Reset; $dataReader NextResult; # TODO: Fix me.
  $dataReader Read; lappend result [$dataReader Item x]
  $dataReader Read; lappend result [$dataReader Item x]

  set result
} -cleanup {
  cleanupDb $fileName


  freeDbConnection



  unset -nocomplain result connection db fileName
} -constraints {eagle command.object monoBug28 command.sql compile.DATA SQLite\
System.Data.SQLite} -result {1 2 1 2 1 2}}

###############################################################################

reportSQLiteResources $test_channel

###############################################################################

runSQLiteTestFilesEpilogue
runSQLiteTestEpilogue
runTestEpilogue







|














>
>
|
>
>
>
>
|
>
|
>

|
|

|
|
|

|
|
|



<
>



>
>
|












3853
3854
3855
3856
3857
3858
3859
3860
3861
3862
3863
3864
3865
3866
3867
3868
3869
3870
3871
3872
3873
3874
3875
3876
3877
3878
3879
3880
3881
3882
3883
3884
3885
3886
3887
3888
3889
3890
3891
3892
3893
3894
3895
3896
3897
3898
3899

3900
3901
3902
3903
3904
3905
3906
3907
3908
3909
3910
3911
3912
3913
3914
3915
3916
3917
3918
} -constraints {eagle command.object monoBug28 command.sql compile.DATA SQLite\
System.Data.SQLite} -match regexp -result {^0 -1 0 -1 0 -1 0 -1 0 -1 0 -1 0 \{1\
2 3 A a M m Z z\} True 1 True 1 True 1 True 1 True 1 True 1 True 1 True 1\
\{(?:-)?\d+ (?:-)?\d+\}$}}

###############################################################################

runTest {test data-1.75 {SQLiteCommand.Reset method} -setup {
  setupDb [set fileName data-1.75.db]
} -body {
  set connection [getDbConnection]

  set result [list]

  sql execute $db {
    CREATE TABLE t1(x);
    INSERT INTO t1 (x) VALUES(1);
    INSERT INTO t1 (x) VALUES(2);
    INSERT INTO t1 (x) VALUES(3);
    INSERT INTO t1 (x) VALUES(4);
  }

  set command [$connection -alias CreateCommand]
  set parameter [$command -alias CreateParameter]

  $parameter ParameterName param1
  $parameter DbType Int32
  $parameter Value 4

  $command CommandText "SELECT x FROM t1 WHERE x < ? ORDER BY x;"
  $command Parameters.Add $parameter

  set dataReader(1) [$command -alias ExecuteReader]

  $dataReader(1) Read; lappend result [$dataReader(1) Item x]
  $dataReader(1) Read; lappend result [$dataReader(1) Item x]

  $command Reset; set dataReader(2) [$command -alias ExecuteReader]
  $dataReader(2) Read; lappend result [$dataReader(2) Item x]
  $dataReader(2) Read; lappend result [$dataReader(2) Item x]

  $command Reset; set dataReader(3) [$command -alias ExecuteReader]
  $dataReader(3) Read; lappend result [$dataReader(3) Item x]
  $dataReader(3) Read; lappend result [$dataReader(3) Item x]

  set result
} -cleanup {

  unset -nocomplain dataReader parameter command connection

  freeDbConnection

  cleanupDb $fileName

  unset -nocomplain result db fileName
} -constraints {eagle command.object monoBug28 command.sql compile.DATA SQLite\
System.Data.SQLite} -result {1 2 1 2 1 2}}

###############################################################################

reportSQLiteResources $test_channel

###############################################################################

runSQLiteTestFilesEpilogue
runSQLiteTestEpilogue
runTestEpilogue