System.Data.SQLite

Check-in [5f8913872b]
Login

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

Overview
Comment:Add more backup API test cases. Modify BackupDatabase method to prevent looping when the number of pages is zero. Modify SQLiteBackupCallback delegate to accept additional context information and move it outside the scope of the SQLiteConnection class.
Downloads: Tarball | ZIP archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA1: 5f8913872b9b87ea5032cd5de22fc904b79cbe82
User & Date: mistachkin 2012-03-26 09:14:57.698
Context
2012-03-26
09:18
Make all the dynamic test classes static, when possible. check-in: 3f6ed3b646 user: mistachkin tags: trunk
09:14
Add more backup API test cases. Modify BackupDatabase method to prevent looping when the number of pages is zero. Modify SQLiteBackupCallback delegate to accept additional context information and move it outside the scope of the SQLiteConnection class. check-in: 5f8913872b user: mistachkin tags: trunk
2012-03-24
16:48
Add support for the native SQLite Online Backup API. check-in: 4926b5d713 user: mistachkin tags: trunk
Changes
Unified Diff Show Whitespace Changes Patch
Changes to System.Data.SQLite/SQLiteConnection.cs.
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
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
        }
      }
    }

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

    #region Backup API Members
    /// <summary>
    /// Raised between each backup step.
    /// </summary>
    /// <param name="source">
    /// The source database connection.
    /// </param>
    /// <param name="destination">
    /// The destination database connection.
    /// </param>
    /// <param name="remainingPages">
    /// The number of pages remaining to be copied.
    /// </param>
    /// <param name="totalPages">
    /// The total number of pages in the source database.
    /// </param>
    /// <param name="retry">
    /// Set to true if the operation needs to be retried due to database
    /// locking issues; otherwise, set to false.
    /// </param>
    /// <returns>
    /// True to continue with the backup process or false to halt the backup
    /// process, rolling back any changes that have been made so far.
    /// </returns>
    public delegate bool SQLiteBackupCallback(
        SQLiteConnection source, SQLiteConnection destination,
        int remainingPages, int totalPages, bool retry);

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

    /// <summary>
    /// Backs up the database, using the specified database connection as the
    /// destination.
    /// </summary>
    /// <param name="destination">The destination database connection.</param>
    /// <param name="destinationName">The destination database name.</param>
    /// <param name="sourceName">The source database name.</param>
    /// <param name="nPage">
    /// The number of pages to copy or negative to copy all remaining pages.
    /// </param>
    /// <param name="callback">
    /// The method to invoke between each step of the backup process.  This
    /// parameter may be null (i.e. no callbacks will be performed).
    /// </param>
    /// <param name="retryMilliseconds">
    /// The number of milliseconds to sleep after encountering a locking error
    /// during the backup process.  A value less than zero means that no sleep
    /// should be performed.
    /// </param>
    public void BackupDatabase(
        SQLiteConnection destination,
        string destinationName,
        string sourceName,
        int nPage,
        SQLiteBackupCallback callback,
        int retryMilliseconds
        )
    {
        CheckDisposed();

        if (_connectionState != ConnectionState.Open)







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







|















|







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
        }
      }
    }

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

    #region Backup API Members





























    /// <summary>
    /// Backs up the database, using the specified database connection as the
    /// destination.
    /// </summary>
    /// <param name="destination">The destination database connection.</param>
    /// <param name="destinationName">The destination database name.</param>
    /// <param name="sourceName">The source database name.</param>
    /// <param name="pages">
    /// The number of pages to copy or negative to copy all remaining pages.
    /// </param>
    /// <param name="callback">
    /// The method to invoke between each step of the backup process.  This
    /// parameter may be null (i.e. no callbacks will be performed).
    /// </param>
    /// <param name="retryMilliseconds">
    /// The number of milliseconds to sleep after encountering a locking error
    /// during the backup process.  A value less than zero means that no sleep
    /// should be performed.
    /// </param>
    public void BackupDatabase(
        SQLiteConnection destination,
        string destinationName,
        string sourceName,
        int pages,
        SQLiteBackupCallback callback,
        int retryMilliseconds
        )
    {
        CheckDisposed();

        if (_connectionState != ConnectionState.Open)
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
        try
        {
            backup = sqliteBase.InitializeBackup(
                destination, destinationName, sourceName); /* throw */

            bool retry;

            while (sqliteBase.StepBackup(backup, nPage, out retry)) /* throw */
            {
                //
                // NOTE: If a callback was supplied by our caller, call it.
                //       If it returns false, halt the backup process.
                //
                if ((callback != null) && !callback(this,

                        destination, sqliteBase.RemainingBackup(backup),
                        sqliteBase.PageCountBackup(backup), retry))
                {
                    break;
                }

                //
                // NOTE: If we need to retry the previous operation, wait for
                //       the number of milliseconds specified by our caller
                //       unless the caller used a negative number, in that case
                //       skip sleeping at all because we do not want to block
                //       this thread forever.
                //
                if (retry && (retryMilliseconds >= 0))
                    System.Threading.Thread.Sleep(retryMilliseconds);







            }
        }
        catch (Exception e)
        {
#if !PLATFORM_COMPACTFRAMEWORK
            if ((_flags & SQLiteConnectionFlags.LogBackup) == SQLiteConnectionFlags.LogBackup)
            {







|





|
>
|














>
>
>
>
>
>
>







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
        try
        {
            backup = sqliteBase.InitializeBackup(
                destination, destinationName, sourceName); /* throw */

            bool retry;

            while (sqliteBase.StepBackup(backup, pages, out retry)) /* throw */
            {
                //
                // NOTE: If a callback was supplied by our caller, call it.
                //       If it returns false, halt the backup process.
                //
                if ((callback != null) && !callback(this, sourceName,
                        destination, destinationName, pages,
                        sqliteBase.RemainingBackup(backup),
                        sqliteBase.PageCountBackup(backup), retry))
                {
                    break;
                }

                //
                // NOTE: If we need to retry the previous operation, wait for
                //       the number of milliseconds specified by our caller
                //       unless the caller used a negative number, in that case
                //       skip sleeping at all because we do not want to block
                //       this thread forever.
                //
                if (retry && (retryMilliseconds >= 0))
                    System.Threading.Thread.Sleep(retryMilliseconds);

                //
                // NOTE: There is no point in calling the native API to copy
                //       zero pages as it does nothing; therefore, stop now.
                //
                if (pages == 0)
                    break;
            }
        }
        catch (Exception e)
        {
#if !PLATFORM_COMPACTFRAMEWORK
            if ((_flags & SQLiteConnectionFlags.LogBackup) == SQLiteConnectionFlags.LogBackup)
            {
2859
2860
2861
2862
2863
2864
2865

















































2866
2867
2868
2869
2870
2871
2872

  /// <summary>
  /// Raised when a statement first begins executing on a given connection
  /// </summary>
  /// <param name="sender">The connection executing the statement</param>
  /// <param name="e">Event arguments of the trace</param>
  public delegate void SQLiteTraceEventHandler(object sender, TraceEventArgs e);


















































  /// <summary>
  /// Whenever an update event is triggered on a connection, this enum will indicate
  /// exactly what type of operation is being performed.
  /// </summary>
  public enum UpdateEventType
  {







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







2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900

  /// <summary>
  /// Raised when a statement first begins executing on a given connection
  /// </summary>
  /// <param name="sender">The connection executing the statement</param>
  /// <param name="e">Event arguments of the trace</param>
  public delegate void SQLiteTraceEventHandler(object sender, TraceEventArgs e);

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

  #region Backup API Members
  /// <summary>
  /// Raised between each backup step.
  /// </summary>
  /// <param name="source">
  /// The source database connection.
  /// </param>
  /// <param name="sourceName">
  /// The source database name.
  /// </param>
  /// <param name="destination">
  /// The destination database connection.
  /// </param>
  /// <param name="destinationName">
  /// The destination database name.
  /// </param>
  /// <param name="pages">
  /// The number of pages copied with each step.
  /// </param>
  /// <param name="remainingPages">
  /// The number of pages remaining to be copied.
  /// </param>
  /// <param name="totalPages">
  /// The total number of pages in the source database.
  /// </param>
  /// <param name="retry">
  /// Set to true if the operation needs to be retried due to database
  /// locking issues; otherwise, set to false.
  /// </param>
  /// <returns>
  /// True to continue with the backup process or false to halt the backup
  /// process, rolling back any changes that have been made so far.
  /// </returns>
  public delegate bool SQLiteBackupCallback(
    SQLiteConnection source,
    string sourceName,
    SQLiteConnection destination,
    string destinationName,
    int pages,
    int remainingPages,
    int totalPages,
    bool retry
  );
  #endregion

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

  /// <summary>
  /// Whenever an update event is triggered on a connection, this enum will indicate
  /// exactly what type of operation is being performed.
  /// </summary>
  public enum UpdateEventType
  {
Changes to Tests/backup.eagle.
16
17
18
19
20
21
22

23





















































24
25
26
27
28
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
58
59
60



61












62
63
64
65
66
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

111


112

113

114
115
116
117
118
###############################################################################

package require System.Data.SQLite.Test
runSQLiteTestPrologue

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


runTest {test backup-1.1 {BackupDatabase method, memory to disk} -setup {





















































  setupDb [set fileName(1) :memory:] "" "" "" "" "" false memDb
  setupDb [set fileName(2) backup-1.1.db]
} -body {
  set id [object invoke Interpreter.GetActive NextId]
  set dataSource [file join [getDatabaseDirectory] $fileName(2)]

  sql execute $memDb {
    CREATE TABLE t1(x TEXT);
  }

  for {set index 0} {$index < 10} {incr index} {
    sql execute $memDb [subst {
      INSERT INTO t1 (x) VALUES('[string repeat ! 1048576]');
    }]
  }

  set memSource [object invoke -flags +NonPublic -objectflags +NoDispose \
      Interpreter.GetActive.connections get_Item $memDb]

  unset -nocomplain results errors

  set code [compileCSharpWith [subst {

    using System.Data.SQLite;
    using System.Text;


    namespace _Dynamic${id}
    {
      public class Test${id}
      {



























        public static string GetRows(

          SQLiteConnection source
          )
        {
          using (SQLiteConnection destination = new SQLiteConnection(
              "Data Source=${dataSource};"))
          {
            destination.Open();



            source.BackupDatabase(destination, "main", "main", -1, null, 0);













            using (SQLiteCommand command = new SQLiteCommand(
                "SELECT length(x) FROM t1;", destination))
            {
              using (SQLiteDataReader dataReader = command.ExecuteReader())
              {
                int rowCount = 0;
                StringBuilder builder = new StringBuilder();


                builder.Append(dataReader.FieldCount);
                builder.Append(' ');

                while (dataReader.Read())
                {
                  builder.Append(dataReader.GetInt64(0));
                  builder.Append(' ');
                  rowCount++;
                }

                builder.Append(rowCount);
                return builder.ToString();
              }
            }
          }
        }

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

        public static void Main()
        {
          // do nothing.
        }
      }
    }
  }] true true true results errors System.Data.SQLite.dll]



  list $code $results \
      [expr {[info exists errors] ? $errors : ""}] \
      [expr {$code eq "Ok" ? [catch {
        object invoke _Dynamic${id}.Test${id} GetRows $memSource
      } result] : [set result ""]}] $result
} -cleanup {
  cleanupDb $fileName(2)
  cleanupDb $fileName(1) memDb

  unset -nocomplain result results errors code index memSource dataSource id \
      memDb db fileName
} -constraints \
{eagle monoBug28 command.sql compile.DATA SQLite System.Data.SQLite} \

-match regexp -result {^Ok System#CodeDom#Compiler#CompilerResults#\d+ \{\} 0\


\{1 1048576 1048576 1048576 1048576 1048576 1048576 1048576 1048576 1048576\

1048576 10\}$}}


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

runSQLiteTestEpilogue
runTestEpilogue







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

|




















>


>



|

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







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






<

>


















|







|
>
>




|
|




|
|


>
|
>
>
|
>
|
>





16
17
18
19
20
21
22
23
24
25
26
27
28
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
58
59
60
61
62
63
64
65
66
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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166

167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
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
222
223
224
###############################################################################

package require System.Data.SQLite.Test
runSQLiteTestPrologue

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

set params(pages) [list -1 -1 0 0 1 1 1000 1000]

set params(callbacks) [list null "new SQLiteBackupCallback(BackupCallback)" \
                            null "new SQLiteBackupCallback(BackupCallback)" \
                            null "new SQLiteBackupCallback(BackupCallback)" \
                            null "new SQLiteBackupCallback(BackupCallback)"]

set params(results) [list \
    "0 \\{1 1048576 1048576 1048576 1048576 1048576 1048576 1048576 1048576\
    1048576 1048576 10\\} 0\$" \
    "0 \\{1 1048576 1048576 1048576 1048576 1048576 1048576 1048576 1048576\
    1048576 1048576 10\\} 0\$" \
    "1 \\{System\\.Reflection\\.TargetInvocationException: Exception has been\
    thrown by the target of an invocation\\. --->\
    System\\.Data\\.SQLite\\.SQLiteException: SQLite error\\r\\nno such table:\
    t1\\r\\n.*?" \
    "1 \\{System\\.Reflection\\.TargetInvocationException: Exception has been\
    thrown by the target of an invocation\\. --->\
    System\\.Data\\.SQLite\\.SQLiteException: SQLite error\\r\\nno such table:\
    t1\\r\\n.*?" \
    "0 \\{1 1048576 1048576 1048576 1048576 1048576 1048576 1048576 1048576\
    1048576 1048576 10\\} 0\$" \
    "0 \\{1 1048576 1048576 1048576 1048576 1048576 1048576 1048576 1048576\
    1048576 1048576 10\\} 10283\$" \
    "0 \\{1 1048576 1048576 1048576 1048576 1048576 1048576 1048576 1048576\
    1048576 1048576 10\\} \\{\\}\$" \
    "0 \\{1 1048576 1048576 1048576 1048576 1048576 1048576 1048576 1048576\
    1048576 1048576 10\\} \\{System\\.Data\\.SQLite\\.SQLiteConnection main\
    System\\.Data\\.SQLite\\.SQLiteConnection main 1000 9284 10284 False\
    System\\.Data\\.SQLite\\.SQLiteConnection main\
    System\\.Data\\.SQLite\\.SQLiteConnection main 1000 8284 10284 False\
    System\\.Data\\.SQLite\\.SQLiteConnection main\
    System\\.Data\\.SQLite\\.SQLiteConnection main 1000 7284 10284 False\
    System\\.Data\\.SQLite\\.SQLiteConnection main\
    System\\.Data\\.SQLite\\.SQLiteConnection main 1000 6284 10284 False\
    System\\.Data\\.SQLite\\.SQLiteConnection main\
    System\\.Data\\.SQLite\\.SQLiteConnection main 1000 5284 10284 False\
    System\\.Data\\.SQLite\\.SQLiteConnection main\
    System\\.Data\\.SQLite\\.SQLiteConnection main 1000 4284 10284 False\
    System\\.Data\\.SQLite\\.SQLiteConnection main\
    System\\.Data\\.SQLite\\.SQLiteConnection main 1000 3284 10284 False\
    System\\.Data\\.SQLite\\.SQLiteConnection main\
    System\\.Data\\.SQLite\\.SQLiteConnection main 1000 2284 10284 False\
    System\\.Data\\.SQLite\\.SQLiteConnection main\
    System\\.Data\\.SQLite\\.SQLiteConnection main 1000 1284 10284 False\
    System\\.Data\\.SQLite\\.SQLiteConnection main\
    System\\.Data\\.SQLite\\.SQLiteConnection main 1000 284 10284 False\\}\$"]

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

for {set i 0} {$i < [llength $params(pages)]} {incr i} {
  set pages [lindex $params(pages) $i]
  set callback [lindex $params(callbacks) $i]

  runTest {test [appendArgs backup-1. $i] {BackupDatabase method} -setup {
  setupDb [set fileName(1) :memory:] "" "" "" "" "" false memDb
    setupDb [set fileName(2) [appendArgs backup-1. $i .db]]
} -body {
  set id [object invoke Interpreter.GetActive NextId]
  set dataSource [file join [getDatabaseDirectory] $fileName(2)]

  sql execute $memDb {
    CREATE TABLE t1(x TEXT);
  }

  for {set index 0} {$index < 10} {incr index} {
    sql execute $memDb [subst {
      INSERT INTO t1 (x) VALUES('[string repeat ! 1048576]');
    }]
  }

  set memSource [object invoke -flags +NonPublic -objectflags +NoDispose \
      Interpreter.GetActive.connections get_Item $memDb]

  unset -nocomplain results errors

  set code [compileCSharpWith [subst {
      using System;
    using System.Data.SQLite;
    using System.Text;
      using Eagle._Components.Public;

    namespace _Dynamic${id}
    {
        public static class Test${id}
      {
          public static int count = 0;
          public static readonly StringBuilder results = new StringBuilder();

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

          public static bool BackupCallback(
            SQLiteConnection source,
            string sourceName,
            SQLiteConnection destination,
            string destinationName,
            int pages,
            int remainingPages,
            int totalPages,
            bool retry
            )
          {
            results.AppendFormat("{0} {1} {2} {3} {4} {5} {6} {7} ", source,
              sourceName, destination, destinationName, pages, remainingPages,
              totalPages, retry);

            count++;

            return true;
          }

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

          public static string BackupAndGetData(
            Interpreter interpreter,
          SQLiteConnection source
          )
        {
          using (SQLiteConnection destination = new SQLiteConnection(
              "Data Source=${dataSource};"))
          {
            destination.Open();

              int pages = ${pages};

              source.BackupDatabase(destination, "main", "main", pages,
                ${callback}, 0);

              ReturnCode code;
              Result error = null;

              code = interpreter.SetVariableValue(
                VariableFlags.GlobalOnly, "callbackResults", (pages > 1) ?
                    results.ToString().Trim() : count.ToString(), null,
                ref error);

              if (code != ReturnCode.Ok)
                Utility.Complain(interpreter, code, error);

            using (SQLiteCommand command = new SQLiteCommand(
                "SELECT length(x) FROM t1;", destination))
            {
              using (SQLiteDataReader dataReader = command.ExecuteReader())
              {

                StringBuilder builder = new StringBuilder();
                  int rowCount = 0;

                builder.Append(dataReader.FieldCount);
                builder.Append(' ');

                while (dataReader.Read())
                {
                  builder.Append(dataReader.GetInt64(0));
                  builder.Append(' ');
                  rowCount++;
                }

                builder.Append(rowCount);
                return builder.ToString();
              }
            }
          }
        }

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

        public static void Main()
        {
          // do nothing.
        }
      }
    }
    }] true true true results errors [list System.Data.SQLite.dll Eagle.dll]]

    set callbackResults [list]

  list $code $results \
      [expr {[info exists errors] ? $errors : ""}] \
      [expr {$code eq "Ok" ? [catch {
          object invoke _Dynamic${id}.Test${id} BackupAndGetData "" $memSource
        } result] : [set result ""]}] $result $callbackResults
} -cleanup {
  cleanupDb $fileName(2)
  cleanupDb $fileName(1) memDb

    unset -nocomplain result results errors code index memSource dataSource \
        id memDb db fileName callbackResults
} -constraints \
{eagle monoBug28 command.sql compile.DATA SQLite System.Data.SQLite} \
-match regexp -result [appendArgs \
"^Ok System#CodeDom#Compiler#CompilerResults#\\d+ \\{\\} " \
[lindex $params(results) $i]]}
}

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

unset -nocomplain i params pages callback

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

runSQLiteTestEpilogue
runTestEpilogue