System.Data.SQLite

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

Artifact 97a3894f0556a395ed81f2345735f347810a85ea:


     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    12
    13
    14
    15
    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
   225
   226
   227
   228
   229
   230
   231
   232
   233
   234
   235
   236
   237
   238
   239
   240
   241
   242
   243
   244
   245
   246
   247
   248
   249
   250
   251
   252
   253
   254
   255
   256
   257
   258
   259
   260
   261
   262
   263
   264
   265
   266
   267
   268
   269
   270
   271
   272
   273
   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
   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
   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
###############################################################################
#
# tkt-343d392b51.eagle --
#
# Written by Joe Mistachkin.
# Released to the public domain, use at your own risk!
#
###############################################################################

package require Eagle
package require Eagle.Library
package require Eagle.Test

runTestPrologue

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

package require System.Data.SQLite.Test
runSQLiteTestPrologue

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

set dateTimeFormats [list "" Ticks ISO8601 JulianDay UnixEpoch]

for {set i 1} {$i < 5} {incr i} {
  set dateTimeFormat [lindex $dateTimeFormats $i]

  runTest {test [appendArgs tkt-343d392b51-1. $i] [subst {DateTime\
      binding $dateTimeFormat format}] -setup {
    setupDb [set fileName [appendArgs tkt-343d392b51-1. $i .db]] "" \
        $dateTimeFormat Utc

    set dateTime "4 October, 2011 3:27:50 PM GMT"
  } -body {
    sql execute $db "CREATE TABLE t1(x DATETIME);"

    set paramDateTime1 [clock format [clock scan $dateTime] -format \
        [getDateTimeFormat] -gmt true]

    switch -exact -- $dateTimeFormat {
      Ticks {
        set paramDateTime1 [object invoke \
            -create -alias DateTime Parse $paramDateTime1]

        set paramDateTime1 [$paramDateTime1 ToUniversalTime.Ticks]
        set paramDateTime2 $paramDateTime1
      }
      ISO8601 {
        set paramDateTime2 [appendArgs ' $paramDateTime1 ']
      }
      JulianDay {
        set paramDateTime1 [object invoke \
            -create -alias DateTime Parse $paramDateTime1]

        set paramDateTime1 [$paramDateTime1 -create -alias ToUniversalTime]

        set paramDateTime1 [expr {[$paramDateTime1 ToOADate] + \
            [object invoke -flags +NonPublic System.Data.SQLite.SQLiteConvert \
            OleAutomationEpochAsJulianDay]}]

        set paramDateTime2 $paramDateTime1
      }
      UnixEpoch {
        set paramDateTime1 [clock scan $dateTime]
        set paramDateTime2 $paramDateTime1
      }
    }

    sql execute $db [appendArgs "INSERT INTO t1 (x) VALUES(" $paramDateTime2 \
        ");"]

    list [sql execute -verbatim -execute reader -format list -datetimeformat \
        [getDateTimeFormat] $db "SELECT x FROM t1 WHERE x = ?;" \
        [list param1 String $paramDateTime1]] \
        [sql execute -verbatim -execute reader -format list -datetimeformat \
        [getDateTimeFormat] $db "SELECT x FROM t1 WHERE x = ?;" \
        [list param1 DateTime $paramDateTime1]]
  } -cleanup {
    cleanupDb $fileName

    unset -nocomplain paramDateTime2 paramDateTime1 dateTime db fileName
  } -constraints {eagle command.object threadCulture.en_US monoBug28\
command.sql compile.DATA SQLite System.Data.SQLite} -result \
{{{2011-10-04 15:27:50Z}} {{2011-10-04 15:27:50Z}}}}
}

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

unset -nocomplain dateTimeFormat i dateTimeFormats

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

runTest {test tkt-343d392b51-2.1 {SQLiteDataAdapter update fail} -setup {
  setupDb [set fileName tkt-343d392b51-2.1.db]
  set otherFileName tkt-343d392b51-2.1-otherDb.db
} -body {
  set id [object invoke Interpreter.GetActive NextId]
  set dataSource [file join [getDatabaseDirectory] $fileName]
  set otherDataSource [file join [getDatabaseDirectory] $otherFileName]
  set otherDbName otherDb
  set otherTable [appendArgs $otherDbName .t1]

  set sql(inserts) ""
  set sql(1) [subst { \
    ATTACH DATABASE '${otherDataSource}' AS ${otherDbName}; \
    CREATE TABLE ${otherTable}(x INTEGER PRIMARY KEY, y DATETIME); \
    [for {set i 1} {$i < 3} {incr i} {
      append sql(inserts) [appendArgs \
          "INSERT INTO " ${otherTable} " (x, y) VALUES(" $i ", '" \
          [clock format $i -format [getDateTimeFormat]] "'); "]
    }; return [expr {[info exists sql(inserts)] ? $sql(inserts) : ""}]] \
  }]

  set sql(2) [subst { \
    SELECT x, y FROM ${otherTable} ORDER BY x; \
  }]

  unset -nocomplain results errors

  set code [compileCSharpWith [subst {
    using System;
    using System.Data;
    using System.Data.SQLite;

    namespace _Dynamic${id}
    {
      public static class Test${id}
      {
        public static void Main()
        {
          using (SQLiteConnection connection = new SQLiteConnection(
              "Data Source=${dataSource};[getTestProperties]"))
          {
            connection.Open();

            using (SQLiteCommand command = connection.CreateCommand())
            {
              command.CommandText = "${sql(1)}";
              command.ExecuteNonQuery();
            }

            using (SQLiteDataAdapter dataAdapter = new SQLiteDataAdapter(
                "${sql(2)}", connection))
            {
              using (DataSet dataSet = new DataSet())
              {
                dataAdapter.Fill(dataSet, "${otherTable}");

                DataTable dataTable = dataSet.Tables\["${otherTable}"\];

                dataTable.Columns\["x"\].Unique = true;
                dataTable.PrimaryKey = new DataColumn\[\] {
                  dataTable.Columns\["x"\]
                };

                [expr {[isMono] ? "#pragma warning disable 219" : ""}]
                SQLiteCommandBuilder commandBuilder =
                    new SQLiteCommandBuilder(dataAdapter);
                [expr {[isMono] ? "#pragma warning restore 219" : ""}]

                foreach (DataRow dataRow in dataTable.Rows)
                {
                  //
                  // NOTE: Update even rows and delete odd rows.
                  //
                  if ((long)dataRow\["x"\] % 2 == 0)
                    dataRow\["y"\] =
                        DateTime.UtcNow.ToString("[getDateTimeFormat]");
                  else
                    dataRow.Delete();
                }

                dataAdapter.Update(dataTable); // DBConcurrencyException (?)
              }
            }
          }
        }
      }
    }
  }] true false true results errors System.Data.SQLite.dll]

  list $code $results \
      [expr {[info exists errors] ? $errors : ""}] \
      [expr {$code eq "Ok" ? [catchCSharp {
        object invoke _Dynamic${id}.Test${id} Main
      } result] : [set result ""]}] $result
} -cleanup {
  cleanupDb $fileName; unset -nocomplain db fileName
  cleanupDb $otherFileName

  unset -nocomplain result code results errors i sql otherTable otherDbName \
      otherDataSource dataSource id otherFileName
} -constraints {eagle command.object monoBug28 command.sql compile.DATA SQLite\
System.Data.SQLite compileCSharp} -match glob -result {*\
System.Data.DBConcurrencyException: *}}

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

runTest {test tkt-343d392b51-2.2 {SQLiteDataAdapter update success} -setup {
  setupDb [set fileName tkt-343d392b51-2.2.db] "" JulianDay
  set otherFileName tkt-343d392b51-2.2-otherDb.db
} -body {
  set id [object invoke Interpreter.GetActive NextId]
  set dataSource [file join [getDatabaseDirectory] $fileName]
  set otherDataSource [file join [getDatabaseDirectory] $otherFileName]
  set otherDbName otherDb
  set otherTable [appendArgs $otherDbName .t1]

  set sql(inserts) ""
  set sql(1) [subst { \
    ATTACH DATABASE '${otherDataSource}' AS ${otherDbName}; \
    CREATE TABLE ${otherTable}(x INTEGER PRIMARY KEY, y DATETIME); \
    [for {set i 1} {$i < 3} {incr i} {
      append sql(inserts) [appendArgs \
          "INSERT INTO " ${otherTable} " (x, y) VALUES(" $i ", JULIANDAY('" \
          [clock format $i -format [getDateTimeFormat]] "')); "]
    }; return [expr {[info exists sql(inserts)] ? $sql(inserts) : ""}]] \
  }]

  set sql(2) [subst { \
    SELECT x, y FROM ${otherTable} ORDER BY x; \
  }]

  unset -nocomplain results errors

  set code [compileCSharpWith [subst {
    using System;
    using System.Data;
    using System.Data.SQLite;

    namespace _Dynamic${id}
    {
      public static class Test${id}
      {
        public static void Main()
        {
          using (SQLiteConnection connection = new SQLiteConnection(
              "Data Source=${dataSource};DateTimeFormat=JulianDay;" +
              "[getTestProperties]"))
          {
            connection.Open();

            using (SQLiteCommand command = connection.CreateCommand())
            {
              command.CommandText = "${sql(1)}";
              command.ExecuteNonQuery();
            }

            using (SQLiteDataAdapter dataAdapter = new SQLiteDataAdapter(
                "${sql(2)}", connection))
            {
              using (DataSet dataSet = new DataSet())
              {
                dataAdapter.Fill(dataSet, "${otherTable}");

                DataTable dataTable = dataSet.Tables\["${otherTable}"\];

                dataTable.Columns\["x"\].Unique = true;
                dataTable.PrimaryKey = new DataColumn\[\] {
                  dataTable.Columns\["x"\]
                };

                [expr {[isMono] ? "#pragma warning disable 219" : ""}]
                SQLiteCommandBuilder commandBuilder =
                    new SQLiteCommandBuilder(dataAdapter);
                [expr {[isMono] ? "#pragma warning restore 219" : ""}]

                foreach (DataRow dataRow in dataTable.Rows)
                {
                  //
                  // NOTE: Update even rows and delete odd rows.
                  //
                  if ((long)dataRow\["x"\] % 2 == 0)
                    dataRow\["y"\] =
                        DateTime.UtcNow.ToString("[getDateTimeFormat]");
                  else
                    dataRow.Delete();
                }

                dataAdapter.Update(dataTable); // DBConcurrencyException (?)
              }
            }
          }
        }
      }
    }
  }] true false true results errors System.Data.SQLite.dll]

  list $code $results \
      [expr {[info exists errors] ? $errors : ""}] \
      [expr {$code eq "Ok" ? [catchCSharp {
        object invoke _Dynamic${id}.Test${id} Main
      } result] : [set result ""]}] $result
} -cleanup {
  cleanupDb $fileName; unset -nocomplain db fileName
  cleanupDb $otherFileName

  unset -nocomplain result code results errors i sql otherTable otherDbName \
      otherDataSource dataSource id otherFileName
} -constraints {eagle command.object monoBug28 command.sql compile.DATA SQLite\
System.Data.SQLite compileCSharp} -match regexp -result {^Ok\
System#CodeDom#Compiler#CompilerResults#\d+ \{\} 0 \{\}$}}

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

runTest {test tkt-343d392b51-3.1 {attached database, same table name} -setup {
  setupDb [set fileName tkt-343d392b51-3.1.db]
  set otherFileName tkt-343d392b51-3.1-otherDb.db
} -body {
  set otherDataSource [file join [getDatabaseDirectory] $otherFileName]
  set otherDbName otherDb
  set otherTable [appendArgs $otherDbName .t1]

  set sql(inserts) ""
  set sql(1) [subst { \
    CREATE TABLE t1(x INTEGER PRIMARY KEY); \
    ATTACH DATABASE '${otherDataSource}' AS ${otherDbName}; \
    CREATE TABLE ${otherTable}(x INTEGER PRIMARY KEY); \
    [for {set i 1} {$i < 3} {incr i} {
      append sql(inserts) [appendArgs \
          "INSERT INTO t1 (x) VALUES(" $i "); "]

      append sql(inserts) [appendArgs \
          "INSERT INTO " ${otherTable} " (x) VALUES(" [expr {$i * 2}] "); "]
    }; return [expr {[info exists sql(inserts)] ? $sql(inserts) : ""}]] \
  }]

  sql execute $db $sql(1)

  list [sql execute -execute reader -format list $db "SELECT x FROM t1;"] \
      [sql execute -execute reader -format list $db [appendArgs \
      "SELECT x FROM " ${otherTable} ";"]]
} -cleanup {
  cleanupDb $fileName; unset -nocomplain db fileName
  cleanupDb $otherFileName

  unset -nocomplain i sql otherTable otherDbName otherDataSource otherFileName
} -constraints \
{eagle monoBug28 command.sql compile.DATA SQLite System.Data.SQLite} -result \
{{1 2} {2 4}}}

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

runTest {test tkt-343d392b51-3.2 {adapter, attached db, table names} -setup {
  setupDb [set fileName tkt-343d392b51-3.2.db]
  set otherFileName tkt-343d392b51-3.2-otherDb.db
} -body {
  set id [object invoke Interpreter.GetActive NextId]
  set dataSource [file join [getDatabaseDirectory] $fileName]
  set otherDataSource [file join [getDatabaseDirectory] $otherFileName]
  set otherDbName otherDb
  set otherTable [appendArgs $otherDbName .t1]

  set sql(inserts) ""
  set sql(1) [subst { \
    CREATE TABLE t1(x INTEGER PRIMARY KEY); \
    ATTACH DATABASE '${otherDataSource}' AS ${otherDbName}; \
    CREATE TABLE ${otherTable}(x INTEGER PRIMARY KEY); \
    [for {set i 1} {$i < 3} {incr i} {
      append sql(inserts) [appendArgs \
          "INSERT INTO t1 (x) VALUES(" $i ");"]
      append sql(inserts) [appendArgs \
          "INSERT INTO " ${otherTable} " (x) VALUES(" [expr {$i * 2}] "); "]
    }; return [expr {[info exists sql(inserts)] ? $sql(inserts) : ""}]] \
  }]

  set sql(2) [subst { \
    SELECT x FROM ${otherTable} ORDER BY x; \
  }]

  unset -nocomplain results errors

  set code [compileCSharpWith [subst {
    using System.Data;
    using System.Data.SQLite;

    namespace _Dynamic${id}
    {
      public static class Test${id}
      {
        public static void Main()
        {
          using (SQLiteConnection connection = new SQLiteConnection(
              "Data Source=${dataSource};[getTestProperties]"))
          {
            connection.Open();

            using (SQLiteCommand command = connection.CreateCommand())
            {
              command.CommandText = "${sql(1)}";
              command.ExecuteNonQuery();
            }

            using (SQLiteDataAdapter dataAdapter = new SQLiteDataAdapter(
                "${sql(2)}", connection))
            {
              using (DataSet dataSet = new DataSet())
              {
                dataAdapter.Fill(dataSet, "${otherTable}");

                DataTable dataTable = dataSet.Tables\["${otherTable}"\];

                dataTable.Columns\["x"\].Unique = true;
                dataTable.PrimaryKey = new DataColumn\[\] {
                  dataTable.Columns\["x"\]
                };

                [expr {[isMono] ? "#pragma warning disable 219" : ""}]
                SQLiteCommandBuilder commandBuilder =
                    new SQLiteCommandBuilder(dataAdapter);
                [expr {[isMono] ? "#pragma warning restore 219" : ""}]

                foreach (DataRow dataRow in dataTable.Rows)
                  dataRow.Delete();

                dataAdapter.Update(dataTable); // DBConcurrencyException (?)
              }
            }
          }
        }
      }
    }
  }] true false true results errors System.Data.SQLite.dll]

  list $code $results \
      [expr {[info exists errors] ? $errors : ""}] \
      [expr {$code eq "Ok" ? [catchCSharp {
        object invoke _Dynamic${id}.Test${id} Main
      } result] : [set result ""]}] $result
} -cleanup {
  cleanupDb $fileName; unset -nocomplain db fileName
  cleanupDb $otherFileName

  unset -nocomplain result code results errors i sql otherTable otherDbName \
      otherDataSource dataSource id otherFileName
} -constraints {eagle command.object monoBug28 monoBug211 command.sql\
compile.DATA SQLite System.Data.SQLite compileCSharp} -match regexp -result \
{^Ok System#CodeDom#Compiler#CompilerResults#\d+ \{\} 0 \{\}$}}

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

runSQLiteTestEpilogue
runTestEpilogue