os/persistentdata/persistentstorage/sqlite3api/TEST/TclScript/capi3.test
author sl@SLION-WIN7.fritz.box
Fri, 15 Jun 2012 03:10:57 +0200
changeset 0 bde4ae8d615e
permissions -rw-r--r--
First public contribution.
sl@0
     1
# 2003 January 29
sl@0
     2
#
sl@0
     3
# The author disclaims copyright to this source code.  In place of
sl@0
     4
# a legal notice, here is a blessing:
sl@0
     5
#
sl@0
     6
#    May you do good and not evil.
sl@0
     7
#    May you find forgiveness for yourself and forgive others.
sl@0
     8
#    May you share freely, never taking more than you give.
sl@0
     9
#
sl@0
    10
#***********************************************************************
sl@0
    11
# This file implements regression tests for SQLite library.  The
sl@0
    12
# focus of this script testing the callback-free C/C++ API.
sl@0
    13
#
sl@0
    14
# $Id: capi3.test,v 1.67 2008/07/12 15:55:55 danielk1977 Exp $
sl@0
    15
#
sl@0
    16
sl@0
    17
set testdir [file dirname $argv0]
sl@0
    18
source $testdir/tester.tcl
sl@0
    19
sl@0
    20
# Return the UTF-16 representation of the supplied UTF-8 string $str.
sl@0
    21
# If $nt is true, append two 0x00 bytes as a nul terminator.
sl@0
    22
proc utf16 {str {nt 1}} {
sl@0
    23
  set r [encoding convertto unicode $str]
sl@0
    24
  if {$nt} {
sl@0
    25
    append r "\x00\x00"
sl@0
    26
  }
sl@0
    27
  return $r
sl@0
    28
}
sl@0
    29
sl@0
    30
# Return the UTF-8 representation of the supplied UTF-16 string $str. 
sl@0
    31
proc utf8 {str} {
sl@0
    32
  # If $str ends in two 0x00 0x00 bytes, knock these off before
sl@0
    33
  # converting to UTF-8 using TCL.
sl@0
    34
  binary scan $str \c* vals
sl@0
    35
  if {[lindex $vals end]==0 && [lindex $vals end-1]==0} {
sl@0
    36
    set str [binary format \c* [lrange $vals 0 end-2]]
sl@0
    37
  }
sl@0
    38
sl@0
    39
  set r [encoding convertfrom unicode $str]
sl@0
    40
  return $r
sl@0
    41
}
sl@0
    42
sl@0
    43
# These tests complement those in capi2.test. They are organized
sl@0
    44
# as follows:
sl@0
    45
#
sl@0
    46
# capi3-1.*: Test sqlite3_prepare 
sl@0
    47
# capi3-2.*: Test sqlite3_prepare16
sl@0
    48
# capi3-3.*: Test sqlite3_open
sl@0
    49
# capi3-4.*: Test sqlite3_open16
sl@0
    50
# capi3-5.*: Test the various sqlite3_result_* APIs
sl@0
    51
# capi3-6.*: Test that sqlite3_close fails if there are outstanding VMs.
sl@0
    52
#
sl@0
    53
sl@0
    54
set DB [sqlite3_connection_pointer db]
sl@0
    55
sl@0
    56
do_test capi3-1.0 {
sl@0
    57
  sqlite3_get_autocommit $DB
sl@0
    58
} 1
sl@0
    59
do_test capi3-1.1 {
sl@0
    60
  set STMT [sqlite3_prepare $DB {SELECT name FROM sqlite_master} -1 TAIL]
sl@0
    61
  sqlite3_finalize $STMT
sl@0
    62
  set TAIL
sl@0
    63
} {}
sl@0
    64
do_test capi3-1.2 {
sl@0
    65
  sqlite3_errcode $DB
sl@0
    66
} {SQLITE_OK}
sl@0
    67
do_test capi3-1.3 {
sl@0
    68
  sqlite3_errmsg $DB
sl@0
    69
} {not an error}
sl@0
    70
do_test capi3-1.4 {
sl@0
    71
  set sql {SELECT name FROM sqlite_master;SELECT 10}
sl@0
    72
  set STMT [sqlite3_prepare $DB $sql -1 TAIL]
sl@0
    73
  sqlite3_finalize $STMT
sl@0
    74
  set TAIL
sl@0
    75
} {SELECT 10}
sl@0
    76
do_test capi3-1.5 {
sl@0
    77
  set sql {SELECT name FROM sqlite_master;SELECT 10}
sl@0
    78
  set STMT [sqlite3_prepare $DB $sql [string length $sql] TAIL]
sl@0
    79
  sqlite3_finalize $STMT
sl@0
    80
  set TAIL
sl@0
    81
} {SELECT 10}
sl@0
    82
do_test capi3-1.6 {
sl@0
    83
  set sql {SELECT name FROM sqlite_master;SELECT 10}
sl@0
    84
  set STMT [sqlite3_prepare $DB $sql [expr [string length $sql]+1] TAIL]
sl@0
    85
  sqlite3_finalize $STMT
sl@0
    86
  set TAIL
sl@0
    87
} {SELECT 10}
sl@0
    88
sl@0
    89
do_test capi3-1.7 {
sl@0
    90
  set sql {SELECT namex FROM sqlite_master}
sl@0
    91
  catch {
sl@0
    92
    set STMT [sqlite3_prepare $DB $sql -1 TAIL]
sl@0
    93
  }
sl@0
    94
} {1}
sl@0
    95
do_test capi3-1.8 {
sl@0
    96
  sqlite3_errcode $DB
sl@0
    97
} {SQLITE_ERROR}
sl@0
    98
do_test capi3-1.9 {
sl@0
    99
  sqlite3_errmsg $DB
sl@0
   100
} {no such column: namex}
sl@0
   101
sl@0
   102
ifcapable {utf16} {
sl@0
   103
  do_test capi3-2.1 {
sl@0
   104
    set sql16 [utf16 {SELECT name FROM sqlite_master}]
sl@0
   105
    set STMT [sqlite3_prepare16 $DB $sql16 -1 ::TAIL]
sl@0
   106
    sqlite3_finalize $STMT
sl@0
   107
    utf8 $::TAIL
sl@0
   108
  } {}
sl@0
   109
  do_test capi3-2.2 {
sl@0
   110
    set sql [utf16 {SELECT name FROM sqlite_master;SELECT 10}]
sl@0
   111
    set STMT [sqlite3_prepare16 $DB $sql -1 TAIL]
sl@0
   112
    sqlite3_finalize $STMT
sl@0
   113
    utf8 $TAIL
sl@0
   114
  } {SELECT 10}
sl@0
   115
  do_test capi3-2.3 {
sl@0
   116
    set sql [utf16 {SELECT namex FROM sqlite_master}]
sl@0
   117
    catch {
sl@0
   118
      set STMT [sqlite3_prepare16 $DB $sql -1 TAIL]
sl@0
   119
    }
sl@0
   120
  } {1}
sl@0
   121
  do_test capi3-2.4 {
sl@0
   122
    sqlite3_errcode $DB
sl@0
   123
  } {SQLITE_ERROR}
sl@0
   124
  do_test capi3-2.5 {
sl@0
   125
    sqlite3_errmsg $DB
sl@0
   126
  } {no such column: namex}
sl@0
   127
sl@0
   128
  ifcapable schema_pragmas {
sl@0
   129
    do_test capi3-2.6 {
sl@0
   130
      execsql {CREATE TABLE tablename(x)}
sl@0
   131
      set sql16 [utf16 {PRAGMA table_info("TableName")}]
sl@0
   132
      set STMT [sqlite3_prepare16 $DB $sql16 -1 TAIL]
sl@0
   133
      sqlite3_step $STMT
sl@0
   134
    } SQLITE_ROW
sl@0
   135
    do_test capi3-2.7 {
sl@0
   136
      sqlite3_step $STMT
sl@0
   137
    } SQLITE_DONE
sl@0
   138
    do_test capi3-2.8 {
sl@0
   139
      sqlite3_finalize $STMT
sl@0
   140
    } SQLITE_OK
sl@0
   141
  }
sl@0
   142
sl@0
   143
} ;# endif utf16
sl@0
   144
sl@0
   145
# rename sqlite3_open sqlite3_open_old
sl@0
   146
# proc sqlite3_open {fname options} {sqlite3_open_new $fname $options}
sl@0
   147
sl@0
   148
do_test capi3-3.1 {
sl@0
   149
  set db2 [sqlite3_open test.db {}]
sl@0
   150
  sqlite3_errcode $db2
sl@0
   151
} {SQLITE_OK}
sl@0
   152
# FIX ME: Should test the db handle works.
sl@0
   153
do_test capi3-3.2 {
sl@0
   154
  sqlite3_close $db2
sl@0
   155
} {SQLITE_OK}
sl@0
   156
# Symbian OS: '/' in the file name replaced with '\\'
sl@0
   157
do_test capi3-3.3 {
sl@0
   158
  catch {
sl@0
   159
    set db2 [sqlite3_open \\bogus\\path\\test.db {}]
sl@0
   160
  }
sl@0
   161
  sqlite3_errcode $db2
sl@0
   162
} {SQLITE_CANTOPEN}
sl@0
   163
do_test capi3-3.4 {
sl@0
   164
  sqlite3_errmsg $db2
sl@0
   165
} {unable to open database file}
sl@0
   166
do_test capi3-3.5 {
sl@0
   167
  sqlite3_close $db2
sl@0
   168
} {SQLITE_OK}
sl@0
   169
do_test capi3-3.6.1-misuse {
sl@0
   170
  sqlite3_close $db2
sl@0
   171
} {SQLITE_MISUSE}
sl@0
   172
do_test capi3-3.6.2-misuse {
sl@0
   173
  sqlite3_errmsg $db2
sl@0
   174
} {library routine called out of sequence}
sl@0
   175
ifcapable {utf16} {
sl@0
   176
  do_test capi3-3.6.3-misuse {
sl@0
   177
    utf8 [sqlite3_errmsg16 $db2]
sl@0
   178
  } {library routine called out of sequence}
sl@0
   179
}
sl@0
   180
sl@0
   181
do_test capi3-3.7 {
sl@0
   182
  set db2 [sqlite3_open]
sl@0
   183
  sqlite3_errcode $db2
sl@0
   184
} {SQLITE_OK}
sl@0
   185
do_test capi3-3.8 {
sl@0
   186
  sqlite3_close $db2
sl@0
   187
} {SQLITE_OK}
sl@0
   188
sl@0
   189
# rename sqlite3_open ""
sl@0
   190
# rename sqlite3_open_old sqlite3_open
sl@0
   191
sl@0
   192
ifcapable {utf16} {
sl@0
   193
do_test capi3-4.1 {
sl@0
   194
  set db2 [sqlite3_open16 [utf16 test.db] {}]
sl@0
   195
  sqlite3_errcode $db2
sl@0
   196
} {SQLITE_OK}
sl@0
   197
# FIX ME: Should test the db handle works.
sl@0
   198
do_test capi3-4.2 {
sl@0
   199
  sqlite3_close $db2
sl@0
   200
} {SQLITE_OK}
sl@0
   201
# Symbian OS: '/' in the file name replaced with '\\'
sl@0
   202
do_test capi3-4.3 {
sl@0
   203
  catch {
sl@0
   204
    set db2 [sqlite3_open16 [utf16 \\bogus\\path\\test.db] {}]
sl@0
   205
  }
sl@0
   206
  sqlite3_errcode $db2
sl@0
   207
} {SQLITE_CANTOPEN}
sl@0
   208
do_test capi3-4.4 {
sl@0
   209
  utf8 [sqlite3_errmsg16 $db2]
sl@0
   210
} {unable to open database file}
sl@0
   211
do_test capi3-4.5 {
sl@0
   212
  sqlite3_close $db2
sl@0
   213
} {SQLITE_OK}
sl@0
   214
} ;# utf16
sl@0
   215
sl@0
   216
# This proc is used to test the following API calls:
sl@0
   217
#
sl@0
   218
# sqlite3_column_count
sl@0
   219
# sqlite3_column_name
sl@0
   220
# sqlite3_column_name16
sl@0
   221
# sqlite3_column_decltype
sl@0
   222
# sqlite3_column_decltype16
sl@0
   223
#
sl@0
   224
# $STMT is a compiled SQL statement. $test is a prefix
sl@0
   225
# to use for test names within this proc. $names is a list
sl@0
   226
# of the column names that should be returned by $STMT.
sl@0
   227
# $decltypes is a list of column declaration types for $STMT.
sl@0
   228
#
sl@0
   229
# Example:
sl@0
   230
#
sl@0
   231
# set STMT [sqlite3_prepare "SELECT 1, 2, 2;" -1 DUMMY]
sl@0
   232
# check_header test1.1 {1 2 3} {"" "" ""}
sl@0
   233
#
sl@0
   234
proc check_header {STMT test names decltypes} {
sl@0
   235
sl@0
   236
  # Use the return value of sqlite3_column_count() to build
sl@0
   237
  # a list of column indexes. i.e. If sqlite3_column_count
sl@0
   238
  # is 3, build the list {0 1 2}.
sl@0
   239
  set ::idxlist [list]
sl@0
   240
  set ::numcols [sqlite3_column_count $STMT]
sl@0
   241
  for {set i 0} {$i < $::numcols} {incr i} {lappend ::idxlist $i}
sl@0
   242
sl@0
   243
  # Column names in UTF-8
sl@0
   244
  do_test $test.1 {
sl@0
   245
    set cnamelist [list]
sl@0
   246
    foreach i $idxlist {lappend cnamelist [sqlite3_column_name $STMT $i]} 
sl@0
   247
    set cnamelist
sl@0
   248
  } $names
sl@0
   249
sl@0
   250
  # Column names in UTF-16
sl@0
   251
  ifcapable {utf16} {
sl@0
   252
    do_test $test.2 {
sl@0
   253
      set cnamelist [list]
sl@0
   254
      foreach i $idxlist {
sl@0
   255
        lappend cnamelist [utf8 [sqlite3_column_name16 $STMT $i]]
sl@0
   256
      }
sl@0
   257
      set cnamelist
sl@0
   258
    } $names
sl@0
   259
  }
sl@0
   260
sl@0
   261
  # Column names in UTF-8
sl@0
   262
  do_test $test.3 {
sl@0
   263
    set cnamelist [list]
sl@0
   264
    foreach i $idxlist {lappend cnamelist [sqlite3_column_name $STMT $i]} 
sl@0
   265
    set cnamelist
sl@0
   266
  } $names
sl@0
   267
sl@0
   268
  # Column names in UTF-16
sl@0
   269
  ifcapable {utf16} {
sl@0
   270
    do_test $test.4 {
sl@0
   271
      set cnamelist [list]
sl@0
   272
      foreach i $idxlist {
sl@0
   273
        lappend cnamelist [utf8 [sqlite3_column_name16 $STMT $i]]
sl@0
   274
      }
sl@0
   275
      set cnamelist
sl@0
   276
    } $names
sl@0
   277
  }
sl@0
   278
sl@0
   279
  # Column names in UTF-8
sl@0
   280
  do_test $test.5 {
sl@0
   281
    set cnamelist [list]
sl@0
   282
    foreach i $idxlist {lappend cnamelist [sqlite3_column_decltype $STMT $i]} 
sl@0
   283
    set cnamelist
sl@0
   284
  } $decltypes
sl@0
   285
sl@0
   286
  # Column declaration types in UTF-16
sl@0
   287
  ifcapable {utf16} {
sl@0
   288
    do_test $test.6 {
sl@0
   289
      set cnamelist [list]
sl@0
   290
      foreach i $idxlist {
sl@0
   291
        lappend cnamelist [utf8 [sqlite3_column_decltype16 $STMT $i]]
sl@0
   292
      }
sl@0
   293
      set cnamelist
sl@0
   294
    } $decltypes
sl@0
   295
  }
sl@0
   296
sl@0
   297
sl@0
   298
  # Test some out of range conditions:
sl@0
   299
  ifcapable {utf16} {
sl@0
   300
    do_test $test.7 {
sl@0
   301
      list \
sl@0
   302
        [sqlite3_column_name $STMT -1] \
sl@0
   303
        [sqlite3_column_name16 $STMT -1] \
sl@0
   304
        [sqlite3_column_decltype $STMT -1] \
sl@0
   305
        [sqlite3_column_decltype16 $STMT -1] \
sl@0
   306
        [sqlite3_column_name $STMT $numcols] \
sl@0
   307
        [sqlite3_column_name16 $STMT $numcols] \
sl@0
   308
        [sqlite3_column_decltype $STMT $numcols] \
sl@0
   309
        [sqlite3_column_decltype16 $STMT $numcols]
sl@0
   310
    } {{} {} {} {} {} {} {} {}}
sl@0
   311
  }
sl@0
   312
} 
sl@0
   313
sl@0
   314
# This proc is used to test the following API calls:
sl@0
   315
#
sl@0
   316
# sqlite3_column_origin_name
sl@0
   317
# sqlite3_column_origin_name16
sl@0
   318
# sqlite3_column_table_name
sl@0
   319
# sqlite3_column_table_name16
sl@0
   320
# sqlite3_column_database_name
sl@0
   321
# sqlite3_column_database_name16
sl@0
   322
#
sl@0
   323
# $STMT is a compiled SQL statement. $test is a prefix
sl@0
   324
# to use for test names within this proc. $names is a list
sl@0
   325
# of the column names that should be returned by $STMT.
sl@0
   326
# $decltypes is a list of column declaration types for $STMT.
sl@0
   327
#
sl@0
   328
# Example:
sl@0
   329
#
sl@0
   330
# set STMT [sqlite3_prepare "SELECT 1, 2, 2;" -1 DUMMY]
sl@0
   331
# check_header test1.1 {1 2 3} {"" "" ""}
sl@0
   332
#
sl@0
   333
proc check_origin_header {STMT test dbs tables cols} {
sl@0
   334
  # If sqlite3_column_origin_name() and friends are not compiled into
sl@0
   335
  # this build, this proc is a no-op.
sl@0
   336
  ifcapable columnmetadata {
sl@0
   337
    # Use the return value of sqlite3_column_count() to build
sl@0
   338
    # a list of column indexes. i.e. If sqlite3_column_count
sl@0
   339
    # is 3, build the list {0 1 2}.
sl@0
   340
    set ::idxlist [list]
sl@0
   341
    set ::numcols [sqlite3_column_count $STMT]
sl@0
   342
    for {set i 0} {$i < $::numcols} {incr i} {lappend ::idxlist $i}
sl@0
   343
  
sl@0
   344
    # Database names in UTF-8
sl@0
   345
    do_test $test.8 {
sl@0
   346
      set cnamelist [list]
sl@0
   347
      foreach i $idxlist {
sl@0
   348
        lappend cnamelist [sqlite3_column_database_name $STMT $i]
sl@0
   349
      } 
sl@0
   350
      set cnamelist
sl@0
   351
    } $dbs
sl@0
   352
  
sl@0
   353
    # Database names in UTF-16
sl@0
   354
    ifcapable {utf16} {
sl@0
   355
      do_test $test.9 {
sl@0
   356
        set cnamelist [list]
sl@0
   357
        foreach i $idxlist {
sl@0
   358
          lappend cnamelist [utf8 [sqlite3_column_database_name16 $STMT $i]]
sl@0
   359
        }
sl@0
   360
        set cnamelist
sl@0
   361
      } $dbs
sl@0
   362
    }
sl@0
   363
  
sl@0
   364
    # Table names in UTF-8
sl@0
   365
    do_test $test.10 {
sl@0
   366
      set cnamelist [list]
sl@0
   367
      foreach i $idxlist {
sl@0
   368
        lappend cnamelist [sqlite3_column_table_name $STMT $i]
sl@0
   369
      } 
sl@0
   370
      set cnamelist
sl@0
   371
    } $tables
sl@0
   372
  
sl@0
   373
    # Table names in UTF-16
sl@0
   374
    ifcapable {utf16} {
sl@0
   375
      do_test $test.11 {
sl@0
   376
        set cnamelist [list]
sl@0
   377
        foreach i $idxlist {
sl@0
   378
          lappend cnamelist [utf8 [sqlite3_column_table_name16 $STMT $i]]
sl@0
   379
        }
sl@0
   380
        set cnamelist
sl@0
   381
      } $tables
sl@0
   382
    }
sl@0
   383
  
sl@0
   384
    # Origin names in UTF-8
sl@0
   385
    do_test $test.12 {
sl@0
   386
      set cnamelist [list]
sl@0
   387
      foreach i $idxlist {
sl@0
   388
        lappend cnamelist [sqlite3_column_origin_name $STMT $i]
sl@0
   389
      } 
sl@0
   390
      set cnamelist
sl@0
   391
    } $cols
sl@0
   392
  
sl@0
   393
    # Origin declaration types in UTF-16
sl@0
   394
    ifcapable {utf16} {
sl@0
   395
      do_test $test.13 {
sl@0
   396
        set cnamelist [list]
sl@0
   397
        foreach i $idxlist {
sl@0
   398
          lappend cnamelist [utf8 [sqlite3_column_origin_name16 $STMT $i]]
sl@0
   399
        }
sl@0
   400
        set cnamelist
sl@0
   401
      } $cols
sl@0
   402
    }
sl@0
   403
  }
sl@0
   404
}
sl@0
   405
sl@0
   406
# This proc is used to test the following APIs:
sl@0
   407
#
sl@0
   408
# sqlite3_data_count
sl@0
   409
# sqlite3_column_type
sl@0
   410
# sqlite3_column_int
sl@0
   411
# sqlite3_column_text
sl@0
   412
# sqlite3_column_text16
sl@0
   413
# sqlite3_column_double
sl@0
   414
#
sl@0
   415
# $STMT is a compiled SQL statement for which the previous call 
sl@0
   416
# to sqlite3_step returned SQLITE_ROW. $test is a prefix to use 
sl@0
   417
# for test names within this proc. $types is a list of the 
sl@0
   418
# manifest types for the current row. $ints, $doubles and $strings
sl@0
   419
# are lists of the integer, real and string representations of
sl@0
   420
# the values in the current row.
sl@0
   421
#
sl@0
   422
# Example:
sl@0
   423
#
sl@0
   424
# set STMT [sqlite3_prepare "SELECT 'hello', 1.1, NULL" -1 DUMMY]
sl@0
   425
# sqlite3_step $STMT
sl@0
   426
# check_data test1.2 {TEXT REAL NULL} {0 1 0} {0 1.1 0} {hello 1.1 {}}
sl@0
   427
#
sl@0
   428
proc check_data {STMT test types ints doubles strings} {
sl@0
   429
sl@0
   430
  # Use the return value of sqlite3_column_count() to build
sl@0
   431
  # a list of column indexes. i.e. If sqlite3_column_count
sl@0
   432
  # is 3, build the list {0 1 2}.
sl@0
   433
  set ::idxlist [list]
sl@0
   434
  set numcols [sqlite3_data_count $STMT]
sl@0
   435
  for {set i 0} {$i < $numcols} {incr i} {lappend ::idxlist $i}
sl@0
   436
sl@0
   437
# types
sl@0
   438
do_test $test.1 {
sl@0
   439
  set types [list]
sl@0
   440
  foreach i $idxlist {lappend types [sqlite3_column_type $STMT $i]}
sl@0
   441
  set types
sl@0
   442
} $types
sl@0
   443
sl@0
   444
# Integers
sl@0
   445
do_test $test.2 {
sl@0
   446
  set ints [list]
sl@0
   447
  foreach i $idxlist {lappend ints [sqlite3_column_int64 $STMT $i]}
sl@0
   448
  set ints
sl@0
   449
} $ints
sl@0
   450
sl@0
   451
# bytes
sl@0
   452
set lens [list]
sl@0
   453
foreach i $::idxlist {
sl@0
   454
  lappend lens [string length [lindex $strings $i]]
sl@0
   455
}
sl@0
   456
do_test $test.3 {
sl@0
   457
  set bytes [list]
sl@0
   458
  set lens [list]
sl@0
   459
  foreach i $idxlist {
sl@0
   460
    lappend bytes [sqlite3_column_bytes $STMT $i]
sl@0
   461
  }
sl@0
   462
  set bytes
sl@0
   463
} $lens
sl@0
   464
sl@0
   465
# bytes16
sl@0
   466
ifcapable {utf16} {
sl@0
   467
  set lens [list]
sl@0
   468
  foreach i $::idxlist {
sl@0
   469
    lappend lens [expr 2 * [string length [lindex $strings $i]]]
sl@0
   470
  }
sl@0
   471
  do_test $test.4 {
sl@0
   472
    set bytes [list]
sl@0
   473
    set lens [list]
sl@0
   474
    foreach i $idxlist {
sl@0
   475
      lappend bytes [sqlite3_column_bytes16 $STMT $i]
sl@0
   476
    }
sl@0
   477
    set bytes
sl@0
   478
  } $lens
sl@0
   479
}
sl@0
   480
sl@0
   481
# Blob
sl@0
   482
do_test $test.5 {
sl@0
   483
  set utf8 [list]
sl@0
   484
  foreach i $idxlist {lappend utf8 [sqlite3_column_blob $STMT $i]}
sl@0
   485
  set utf8
sl@0
   486
} $strings
sl@0
   487
sl@0
   488
# UTF-8
sl@0
   489
do_test $test.6 {
sl@0
   490
  set utf8 [list]
sl@0
   491
  foreach i $idxlist {lappend utf8 [sqlite3_column_text $STMT $i]}
sl@0
   492
  set utf8
sl@0
   493
} $strings
sl@0
   494
sl@0
   495
# Floats
sl@0
   496
do_test $test.7 {
sl@0
   497
  set utf8 [list]
sl@0
   498
  foreach i $idxlist {lappend utf8 [sqlite3_column_double $STMT $i]}
sl@0
   499
  set utf8
sl@0
   500
} $doubles
sl@0
   501
sl@0
   502
# UTF-16
sl@0
   503
ifcapable {utf16} {
sl@0
   504
  do_test $test.8 {
sl@0
   505
    set utf8 [list]
sl@0
   506
    foreach i $idxlist {lappend utf8 [utf8 [sqlite3_column_text16 $STMT $i]]}
sl@0
   507
    set utf8
sl@0
   508
  } $strings
sl@0
   509
}
sl@0
   510
sl@0
   511
# Integers
sl@0
   512
do_test $test.9 {
sl@0
   513
  set ints [list]
sl@0
   514
  foreach i $idxlist {lappend ints [sqlite3_column_int $STMT $i]}
sl@0
   515
  set ints
sl@0
   516
} $ints
sl@0
   517
sl@0
   518
# Floats
sl@0
   519
do_test $test.10 {
sl@0
   520
  set utf8 [list]
sl@0
   521
  foreach i $idxlist {lappend utf8 [sqlite3_column_double $STMT $i]}
sl@0
   522
  set utf8
sl@0
   523
} $doubles
sl@0
   524
sl@0
   525
# UTF-8
sl@0
   526
do_test $test.11 {
sl@0
   527
  set utf8 [list]
sl@0
   528
  foreach i $idxlist {lappend utf8 [sqlite3_column_text $STMT $i]}
sl@0
   529
  set utf8
sl@0
   530
} $strings
sl@0
   531
sl@0
   532
# Types
sl@0
   533
do_test $test.12 {
sl@0
   534
  set types [list]
sl@0
   535
  foreach i $idxlist {lappend types [sqlite3_column_type $STMT $i]}
sl@0
   536
  set types
sl@0
   537
} $types
sl@0
   538
sl@0
   539
# Test that an out of range request returns the equivalent of NULL
sl@0
   540
do_test $test.13 {
sl@0
   541
  sqlite3_column_int $STMT -1
sl@0
   542
} {0}
sl@0
   543
do_test $test.13 {
sl@0
   544
  sqlite3_column_text $STMT -1
sl@0
   545
} {}
sl@0
   546
sl@0
   547
}
sl@0
   548
sl@0
   549
ifcapable !floatingpoint {
sl@0
   550
  finish_test
sl@0
   551
  return
sl@0
   552
}
sl@0
   553
sl@0
   554
do_test capi3-5.0 {
sl@0
   555
  execsql {
sl@0
   556
    CREATE TABLE t1(a VARINT, b BLOB, c VARCHAR(16));
sl@0
   557
    INSERT INTO t1 VALUES(1, 2, 3);
sl@0
   558
    INSERT INTO t1 VALUES('one', 'two', NULL);
sl@0
   559
    INSERT INTO t1 VALUES(1.2, 1.3, 1.4);
sl@0
   560
  }
sl@0
   561
  set sql "SELECT * FROM t1"
sl@0
   562
  set STMT [sqlite3_prepare $DB $sql -1 TAIL]
sl@0
   563
  sqlite3_column_count $STMT
sl@0
   564
} 3
sl@0
   565
sl@0
   566
check_header $STMT capi3-5.1 {a b c} {VARINT BLOB VARCHAR(16)}
sl@0
   567
check_origin_header $STMT capi3-5.1 {main main main} {t1 t1 t1} {a b c}
sl@0
   568
do_test capi3-5.2 {
sl@0
   569
  sqlite3_step $STMT
sl@0
   570
} SQLITE_ROW
sl@0
   571
sl@0
   572
check_header $STMT capi3-5.3 {a b c} {VARINT BLOB VARCHAR(16)}
sl@0
   573
check_origin_header $STMT capi3-5.3 {main main main} {t1 t1 t1} {a b c}
sl@0
   574
check_data $STMT capi3-5.4 {INTEGER INTEGER TEXT} {1 2 3} {1.0 2.0 3.0} {1 2 3}
sl@0
   575
sl@0
   576
do_test capi3-5.5 {
sl@0
   577
  sqlite3_step $STMT
sl@0
   578
} SQLITE_ROW
sl@0
   579
sl@0
   580
check_header $STMT capi3-5.6 {a b c} {VARINT BLOB VARCHAR(16)}
sl@0
   581
check_origin_header $STMT capi3-5.6 {main main main} {t1 t1 t1} {a b c}
sl@0
   582
check_data $STMT capi3-5.7 {TEXT TEXT NULL} {0 0 0} {0.0 0.0 0.0} {one two {}}
sl@0
   583
sl@0
   584
do_test capi3-5.8 {
sl@0
   585
  sqlite3_step $STMT
sl@0
   586
} SQLITE_ROW
sl@0
   587
sl@0
   588
check_header $STMT capi3-5.9 {a b c} {VARINT BLOB VARCHAR(16)}
sl@0
   589
check_origin_header $STMT capi3-5.9 {main main main} {t1 t1 t1} {a b c}
sl@0
   590
check_data $STMT capi3-5.10 {FLOAT FLOAT TEXT} {1 1 1} {1.2 1.3 1.4} {1.2 1.3 1.4}
sl@0
   591
sl@0
   592
do_test capi3-5.11 {
sl@0
   593
  sqlite3_step $STMT
sl@0
   594
} SQLITE_DONE
sl@0
   595
sl@0
   596
do_test capi3-5.12 {
sl@0
   597
  sqlite3_finalize $STMT
sl@0
   598
} SQLITE_OK
sl@0
   599
sl@0
   600
do_test capi3-5.20 {
sl@0
   601
  set sql "SELECT a, sum(b), max(c) FROM t1 GROUP BY a"
sl@0
   602
  set STMT [sqlite3_prepare $DB $sql -1 TAIL]
sl@0
   603
  sqlite3_column_count $STMT
sl@0
   604
} 3
sl@0
   605
sl@0
   606
check_header $STMT capi3-5.21 {a sum(b) max(c)} {VARINT {} {}}
sl@0
   607
check_origin_header $STMT capi3-5.22 {main {} {}} {t1 {} {}} {a {} {}}
sl@0
   608
do_test capi3-5.23 {
sl@0
   609
  sqlite3_finalize $STMT
sl@0
   610
} SQLITE_OK
sl@0
   611
sl@0
   612
do_test capi3-5.30 {
sl@0
   613
  set sql "SELECT a AS x, sum(b) AS y, max(c) AS z FROM t1 AS m GROUP BY x"
sl@0
   614
  set STMT [sqlite3_prepare $DB $sql -1 TAIL]
sl@0
   615
  sqlite3_column_count $STMT
sl@0
   616
} 3
sl@0
   617
sl@0
   618
check_header $STMT capi3-5.31 {x y z} {VARINT {} {}}
sl@0
   619
check_origin_header $STMT capi3-5.32 {main {} {}} {t1 {} {}} {a {} {}}
sl@0
   620
do_test capi3-5.33 {
sl@0
   621
  sqlite3_finalize $STMT
sl@0
   622
} SQLITE_OK
sl@0
   623
sl@0
   624
sl@0
   625
set ::ENC [execsql {pragma encoding}]
sl@0
   626
db close
sl@0
   627
sl@0
   628
do_test capi3-6.0 {
sl@0
   629
  sqlite3 db test.db
sl@0
   630
  set DB [sqlite3_connection_pointer db]
sl@0
   631
  sqlite3_key $DB xyzzy
sl@0
   632
  set sql {SELECT a FROM t1 order by rowid}
sl@0
   633
  set STMT [sqlite3_prepare $DB $sql -1 TAIL]
sl@0
   634
  expr 0
sl@0
   635
} {0}
sl@0
   636
do_test capi3-6.1 {
sl@0
   637
  db cache flush
sl@0
   638
  sqlite3_close $DB
sl@0
   639
} {SQLITE_BUSY}
sl@0
   640
do_test capi3-6.2 {
sl@0
   641
  sqlite3_step $STMT
sl@0
   642
} {SQLITE_ROW}
sl@0
   643
check_data $STMT capi3-6.3 {INTEGER} {1} {1.0} {1}
sl@0
   644
do_test capi3-6.3 {
sl@0
   645
  sqlite3_finalize $STMT
sl@0
   646
} {SQLITE_OK}
sl@0
   647
do_test capi3-6.4-misuse {
sl@0
   648
  db cache flush
sl@0
   649
  sqlite3_close $DB
sl@0
   650
} {SQLITE_OK}
sl@0
   651
db close
sl@0
   652
sl@0
   653
# This procedure sets the value of the file-format in file 'test.db'
sl@0
   654
# to $newval. Also, the schema cookie is incremented.
sl@0
   655
# 
sl@0
   656
proc set_file_format {newval} {
sl@0
   657
  hexio_write test.db 44 [hexio_render_int32 $newval]
sl@0
   658
  set schemacookie [hexio_get_int [hexio_read test.db 40 4]]
sl@0
   659
  incr schemacookie
sl@0
   660
  hexio_write test.db 40 [hexio_render_int32 $schemacookie]
sl@0
   661
  return {}
sl@0
   662
}
sl@0
   663
sl@0
   664
# This procedure returns the value of the file-format in file 'test.db'.
sl@0
   665
# 
sl@0
   666
proc get_file_format {{fname test.db}} {
sl@0
   667
  return [hexio_get_int [hexio_read $fname 44 4]]
sl@0
   668
}
sl@0
   669
sl@0
   670
if {![sqlite3 -has-codec]} {
sl@0
   671
  # Test what happens when the library encounters a newer file format.
sl@0
   672
  do_test capi3-7.1 {
sl@0
   673
    set_file_format 5
sl@0
   674
  } {}
sl@0
   675
  do_test capi3-7.2 {
sl@0
   676
    sqlite3 db test.db
sl@0
   677
    catchsql {
sl@0
   678
      SELECT * FROM sqlite_master;
sl@0
   679
    }
sl@0
   680
  } {1 {unsupported file format}}
sl@0
   681
  db close
sl@0
   682
}
sl@0
   683
sl@0
   684
if {![sqlite3 -has-codec]} {
sl@0
   685
  # Now test that the library correctly handles bogus entries in the
sl@0
   686
  # sqlite_master table (schema corruption).
sl@0
   687
  do_test capi3-8.1 {
sl@0
   688
    file delete -force test.db test.db-journal
sl@0
   689
    sqlite3 db test.db
sl@0
   690
    execsql {
sl@0
   691
      CREATE TABLE t1(a);
sl@0
   692
    }
sl@0
   693
    db close
sl@0
   694
  } {}
sl@0
   695
  do_test capi3-8.2 {
sl@0
   696
    sqlite3 db test.db
sl@0
   697
    execsql {
sl@0
   698
      PRAGMA writable_schema=ON;
sl@0
   699
      INSERT INTO sqlite_master VALUES(NULL,NULL,NULL,NULL,NULL);
sl@0
   700
    }
sl@0
   701
    db close
sl@0
   702
  } {}
sl@0
   703
  do_test capi3-8.3 {
sl@0
   704
    sqlite3 db test.db
sl@0
   705
    catchsql {
sl@0
   706
      SELECT * FROM sqlite_master;
sl@0
   707
    }
sl@0
   708
  } {1 {malformed database schema (?)}}
sl@0
   709
  do_test capi3-8.4 {
sl@0
   710
    # Build a 5-field row record. The first field is a string 'table', and
sl@0
   711
    # subsequent fields are all NULL.
sl@0
   712
    db close
sl@0
   713
    file delete -force test.db test.db-journal
sl@0
   714
    sqlite3 db test.db
sl@0
   715
    execsql {
sl@0
   716
      CREATE TABLE t1(a);
sl@0
   717
      PRAGMA writable_schema=ON;
sl@0
   718
      INSERT INTO sqlite_master VALUES('table',NULL,NULL,NULL,NULL);
sl@0
   719
    }
sl@0
   720
    db close
sl@0
   721
  } {};
sl@0
   722
  do_test capi3-8.5 {
sl@0
   723
    sqlite3 db test.db
sl@0
   724
    catchsql {
sl@0
   725
      SELECT * FROM sqlite_master;
sl@0
   726
    }
sl@0
   727
  } {1 {malformed database schema (?)}}
sl@0
   728
  db close
sl@0
   729
}
sl@0
   730
file delete -force test.db
sl@0
   731
file delete -force test.db-journal
sl@0
   732
sl@0
   733
sl@0
   734
# Test the english language string equivalents for sqlite error codes
sl@0
   735
set code2english [list \
sl@0
   736
SQLITE_OK         {not an error} \
sl@0
   737
SQLITE_ERROR      {SQL logic error or missing database} \
sl@0
   738
SQLITE_PERM       {access permission denied} \
sl@0
   739
SQLITE_ABORT      {callback requested query abort} \
sl@0
   740
SQLITE_BUSY       {database is locked} \
sl@0
   741
SQLITE_LOCKED     {database table is locked} \
sl@0
   742
SQLITE_NOMEM      {out of memory} \
sl@0
   743
SQLITE_READONLY   {attempt to write a readonly database} \
sl@0
   744
SQLITE_INTERRUPT  {interrupted} \
sl@0
   745
SQLITE_IOERR      {disk I/O error} \
sl@0
   746
SQLITE_CORRUPT    {database disk image is malformed} \
sl@0
   747
SQLITE_FULL       {database or disk is full} \
sl@0
   748
SQLITE_CANTOPEN   {unable to open database file} \
sl@0
   749
SQLITE_EMPTY      {table contains no data} \
sl@0
   750
SQLITE_SCHEMA     {database schema has changed} \
sl@0
   751
SQLITE_CONSTRAINT {constraint failed} \
sl@0
   752
SQLITE_MISMATCH   {datatype mismatch} \
sl@0
   753
SQLITE_MISUSE     {library routine called out of sequence} \
sl@0
   754
SQLITE_NOLFS      {large file support is disabled} \
sl@0
   755
SQLITE_AUTH       {authorization denied} \
sl@0
   756
SQLITE_FORMAT     {auxiliary database format error} \
sl@0
   757
SQLITE_RANGE      {bind or column index out of range} \
sl@0
   758
SQLITE_NOTADB     {file is encrypted or is not a database} \
sl@0
   759
unknownerror      {unknown error} \
sl@0
   760
]
sl@0
   761
sl@0
   762
set test_number 1
sl@0
   763
foreach {code english} $code2english {
sl@0
   764
  do_test capi3-9.$test_number "sqlite3_test_errstr $code" $english
sl@0
   765
  incr test_number
sl@0
   766
}
sl@0
   767
sl@0
   768
# Test the error message when a "real" out of memory occurs.
sl@0
   769
ifcapable memdebug {
sl@0
   770
  do_test capi3-10-1 {
sl@0
   771
    sqlite3 db test.db
sl@0
   772
    set DB [sqlite3_connection_pointer db]
sl@0
   773
    sqlite3_memdebug_fail 1
sl@0
   774
    catchsql {
sl@0
   775
      select * from sqlite_master;
sl@0
   776
    }
sl@0
   777
  } {1 {out of memory}}
sl@0
   778
  do_test capi3-10-2 {
sl@0
   779
    sqlite3_errmsg $::DB
sl@0
   780
  } {out of memory}
sl@0
   781
  ifcapable {utf16} {
sl@0
   782
    do_test capi3-10-3 {
sl@0
   783
      utf8 [sqlite3_errmsg16 $::DB]
sl@0
   784
    } {out of memory}
sl@0
   785
  }
sl@0
   786
  db close
sl@0
   787
  sqlite3_memdebug_fail -1
sl@0
   788
  do_test capi3-10-4 {
sl@0
   789
    sqlite3 db test.db
sl@0
   790
    set DB [sqlite3_connection_pointer db]
sl@0
   791
    sqlite3_memdebug_fail 1
sl@0
   792
    catchsql {
sl@0
   793
      select * from sqlite_master where rowid>5;
sl@0
   794
    }
sl@0
   795
  } {1 {out of memory}}
sl@0
   796
  do_test capi3-10-5 {
sl@0
   797
    sqlite3_errmsg $::DB
sl@0
   798
  } {out of memory}
sl@0
   799
  ifcapable {utf16} {
sl@0
   800
    do_test capi3-10-6 {
sl@0
   801
      utf8 [sqlite3_errmsg16 $::DB]
sl@0
   802
    } {out of memory}
sl@0
   803
  }
sl@0
   804
  db close
sl@0
   805
  sqlite3_memdebug_fail -1
sl@0
   806
}
sl@0
   807
sl@0
   808
# The following tests - capi3-11.* - test that a COMMIT or ROLLBACK
sl@0
   809
# statement issued while there are still outstanding VMs that are part of
sl@0
   810
# the transaction fails.
sl@0
   811
sqlite3 db test.db
sl@0
   812
set DB [sqlite3_connection_pointer db]
sl@0
   813
sqlite_register_test_function $DB func
sl@0
   814
do_test capi3-11.1 {
sl@0
   815
  execsql {
sl@0
   816
    BEGIN;
sl@0
   817
    CREATE TABLE t1(a, b);
sl@0
   818
    INSERT INTO t1 VALUES(1, 'int');
sl@0
   819
    INSERT INTO t1 VALUES(2, 'notatype');
sl@0
   820
  }
sl@0
   821
} {}
sl@0
   822
do_test capi3-11.1.1 {
sl@0
   823
  sqlite3_get_autocommit $DB
sl@0
   824
} 0
sl@0
   825
do_test capi3-11.2 {
sl@0
   826
  set STMT [sqlite3_prepare $DB "SELECT func(b, a) FROM t1" -1 TAIL]
sl@0
   827
  sqlite3_step $STMT
sl@0
   828
} {SQLITE_ROW}
sl@0
   829
do_test capi3-11.3 {
sl@0
   830
  catchsql {
sl@0
   831
    COMMIT;
sl@0
   832
  }
sl@0
   833
} {1 {cannot commit transaction - SQL statements in progress}}
sl@0
   834
do_test capi3-11.3.1 {
sl@0
   835
  sqlite3_get_autocommit $DB
sl@0
   836
} 0
sl@0
   837
do_test capi3-11.4 {
sl@0
   838
  sqlite3_step $STMT
sl@0
   839
} {SQLITE_ERROR}
sl@0
   840
do_test capi3-11.5 {
sl@0
   841
  sqlite3_finalize $STMT
sl@0
   842
} {SQLITE_ERROR}
sl@0
   843
do_test capi3-11.6 {
sl@0
   844
  catchsql {
sl@0
   845
    SELECT * FROM t1;
sl@0
   846
  }
sl@0
   847
} {0 {1 int 2 notatype}}
sl@0
   848
do_test capi3-11.6.1 {
sl@0
   849
  sqlite3_get_autocommit $DB
sl@0
   850
} 0
sl@0
   851
do_test capi3-11.7 {
sl@0
   852
  catchsql {
sl@0
   853
    COMMIT;
sl@0
   854
  }
sl@0
   855
} {0 {}}
sl@0
   856
do_test capi3-11.7.1 {
sl@0
   857
  sqlite3_get_autocommit $DB
sl@0
   858
} 1
sl@0
   859
do_test capi3-11.8 {
sl@0
   860
  execsql {
sl@0
   861
    CREATE TABLE t2(a);
sl@0
   862
    INSERT INTO t2 VALUES(1);
sl@0
   863
    INSERT INTO t2 VALUES(2);
sl@0
   864
    BEGIN;
sl@0
   865
    INSERT INTO t2 VALUES(3);
sl@0
   866
  }
sl@0
   867
} {}
sl@0
   868
do_test capi3-11.8.1 {
sl@0
   869
  sqlite3_get_autocommit $DB
sl@0
   870
} 0
sl@0
   871
do_test capi3-11.9 {
sl@0
   872
  set STMT [sqlite3_prepare $DB "SELECT a FROM t2" -1 TAIL]
sl@0
   873
  sqlite3_step $STMT
sl@0
   874
} {SQLITE_ROW}
sl@0
   875
do_test capi3-11.9.1 {
sl@0
   876
  sqlite3_get_autocommit $DB
sl@0
   877
} 0
sl@0
   878
do_test capi3-11.9.2 {
sl@0
   879
  catchsql {
sl@0
   880
    ROLLBACK;
sl@0
   881
  }
sl@0
   882
} {1 {cannot rollback transaction - SQL statements in progress}}
sl@0
   883
do_test capi3-11.9.3 {
sl@0
   884
  sqlite3_get_autocommit $DB
sl@0
   885
} 0
sl@0
   886
do_test capi3-11.10 {
sl@0
   887
  sqlite3_step $STMT
sl@0
   888
} {SQLITE_ROW}
sl@0
   889
do_test capi3-11.11 {
sl@0
   890
  sqlite3_step $STMT
sl@0
   891
} {SQLITE_ROW}
sl@0
   892
do_test capi3-11.12 {
sl@0
   893
  sqlite3_step $STMT
sl@0
   894
} {SQLITE_DONE}
sl@0
   895
do_test capi3-11.13 {
sl@0
   896
  sqlite3_finalize $STMT
sl@0
   897
} {SQLITE_OK}
sl@0
   898
do_test capi3-11.14 {
sl@0
   899
  execsql {
sl@0
   900
    SELECT a FROM t2;
sl@0
   901
  }
sl@0
   902
} {1 2 3}
sl@0
   903
do_test capi3-11.14.1 {
sl@0
   904
  sqlite3_get_autocommit $DB
sl@0
   905
} 0
sl@0
   906
do_test capi3-11.15 {
sl@0
   907
  catchsql {
sl@0
   908
    ROLLBACK;
sl@0
   909
  }
sl@0
   910
} {0 {}}
sl@0
   911
do_test capi3-11.15.1 {
sl@0
   912
  sqlite3_get_autocommit $DB
sl@0
   913
} 1
sl@0
   914
do_test capi3-11.16 {
sl@0
   915
  execsql {
sl@0
   916
    SELECT a FROM t2;
sl@0
   917
  }
sl@0
   918
} {1 2}
sl@0
   919
sl@0
   920
# Sanity check on the definition of 'outstanding VM'. This means any VM
sl@0
   921
# that has had sqlite3_step() called more recently than sqlite3_finalize() or
sl@0
   922
# sqlite3_reset(). So a VM that has just been prepared or reset does not
sl@0
   923
# count as an active VM.
sl@0
   924
do_test capi3-11.17 {
sl@0
   925
  execsql {
sl@0
   926
    BEGIN;
sl@0
   927
  }
sl@0
   928
} {}
sl@0
   929
do_test capi3-11.18 {
sl@0
   930
  set STMT [sqlite3_prepare $DB "SELECT a FROM t1" -1 TAIL]
sl@0
   931
  catchsql {
sl@0
   932
    COMMIT;
sl@0
   933
  }
sl@0
   934
} {0 {}}
sl@0
   935
do_test capi3-11.19 {
sl@0
   936
  sqlite3_step $STMT
sl@0
   937
} {SQLITE_ROW}
sl@0
   938
do_test capi3-11.20 {
sl@0
   939
  catchsql {
sl@0
   940
    BEGIN;
sl@0
   941
    COMMIT;
sl@0
   942
  }
sl@0
   943
} {1 {cannot commit transaction - SQL statements in progress}}
sl@0
   944
do_test capi3-11.20 {
sl@0
   945
  sqlite3_reset $STMT
sl@0
   946
  catchsql {
sl@0
   947
    COMMIT;
sl@0
   948
  }
sl@0
   949
} {0 {}}
sl@0
   950
do_test capi3-11.21 {
sl@0
   951
  sqlite3_finalize $STMT
sl@0
   952
} {SQLITE_OK}
sl@0
   953
sl@0
   954
# The following tests - capi3-12.* - check that its Ok to start a
sl@0
   955
# transaction while other VMs are active, and that its Ok to execute
sl@0
   956
# atomic updates in the same situation 
sl@0
   957
#
sl@0
   958
do_test capi3-12.1 {
sl@0
   959
  set STMT [sqlite3_prepare $DB "SELECT a FROM t2" -1 TAIL]
sl@0
   960
  sqlite3_step $STMT
sl@0
   961
} {SQLITE_ROW}
sl@0
   962
do_test capi3-12.2 {
sl@0
   963
  catchsql {
sl@0
   964
    INSERT INTO t1 VALUES(3, NULL);
sl@0
   965
  }
sl@0
   966
} {0 {}}
sl@0
   967
do_test capi3-12.3 {
sl@0
   968
  catchsql {
sl@0
   969
    INSERT INTO t2 VALUES(4);
sl@0
   970
  }
sl@0
   971
} {0 {}}
sl@0
   972
do_test capi3-12.4 {
sl@0
   973
  catchsql {
sl@0
   974
    BEGIN;
sl@0
   975
    INSERT INTO t1 VALUES(4, NULL);
sl@0
   976
  }
sl@0
   977
} {0 {}}
sl@0
   978
do_test capi3-12.5 {
sl@0
   979
  sqlite3_step $STMT
sl@0
   980
} {SQLITE_ROW}
sl@0
   981
do_test capi3-12.5.1 {
sl@0
   982
  sqlite3_step $STMT
sl@0
   983
} {SQLITE_ROW}
sl@0
   984
do_test capi3-12.6 {
sl@0
   985
  sqlite3_step $STMT
sl@0
   986
} {SQLITE_DONE}
sl@0
   987
do_test capi3-12.7 {
sl@0
   988
  sqlite3_finalize $STMT
sl@0
   989
} {SQLITE_OK}
sl@0
   990
do_test capi3-12.8 {
sl@0
   991
  execsql {
sl@0
   992
    COMMIT;
sl@0
   993
    SELECT a FROM t1;
sl@0
   994
  }
sl@0
   995
} {1 2 3 4}
sl@0
   996
sl@0
   997
# Test cases capi3-13.* test the sqlite3_clear_bindings() and 
sl@0
   998
# sqlite3_sleep APIs.
sl@0
   999
#
sl@0
  1000
if {[llength [info commands sqlite3_clear_bindings]]>0} {
sl@0
  1001
  do_test capi3-13.1 {
sl@0
  1002
    execsql {
sl@0
  1003
      DELETE FROM t1;
sl@0
  1004
    }
sl@0
  1005
    set STMT [sqlite3_prepare $DB "INSERT INTO t1 VALUES(?, ?)" -1 TAIL]
sl@0
  1006
    sqlite3_step $STMT
sl@0
  1007
  } {SQLITE_DONE}
sl@0
  1008
  do_test capi3-13.2 {
sl@0
  1009
    sqlite3_reset $STMT
sl@0
  1010
    sqlite3_bind_text $STMT 1 hello 5
sl@0
  1011
    sqlite3_bind_text $STMT 2 world 5
sl@0
  1012
    sqlite3_step $STMT
sl@0
  1013
  } {SQLITE_DONE}
sl@0
  1014
  do_test capi3-13.3 {
sl@0
  1015
    sqlite3_reset $STMT
sl@0
  1016
    sqlite3_clear_bindings $STMT
sl@0
  1017
    sqlite3_step $STMT
sl@0
  1018
  } {SQLITE_DONE}
sl@0
  1019
  do_test capi3-13-4 {
sl@0
  1020
    sqlite3_finalize $STMT
sl@0
  1021
    execsql {
sl@0
  1022
      SELECT * FROM t1;
sl@0
  1023
    }
sl@0
  1024
  } {{} {} hello world {} {}}
sl@0
  1025
}
sl@0
  1026
if {[llength [info commands sqlite3_sleep]]>0} {
sl@0
  1027
  do_test capi3-13-5 {
sl@0
  1028
    set ms [sqlite3_sleep 80]
sl@0
  1029
    expr {$ms==80 || $ms==1000}
sl@0
  1030
  } {1}
sl@0
  1031
}
sl@0
  1032
sl@0
  1033
# Ticket #1219:  Make sure binding APIs can handle a NULL pointer.
sl@0
  1034
#
sl@0
  1035
do_test capi3-14.1-misuse {
sl@0
  1036
  set rc [catch {sqlite3_bind_text 0 1 hello 5} msg]
sl@0
  1037
  lappend rc $msg
sl@0
  1038
} {1 SQLITE_MISUSE}
sl@0
  1039
sl@0
  1040
# Ticket #1650:  Honor the nBytes parameter to sqlite3_prepare.
sl@0
  1041
#
sl@0
  1042
do_test capi3-15.1 {
sl@0
  1043
  set sql {SELECT * FROM t2}
sl@0
  1044
  set nbytes [string length $sql]
sl@0
  1045
  append sql { WHERE a==1}
sl@0
  1046
  set STMT [sqlite3_prepare $DB $sql $nbytes TAIL]
sl@0
  1047
  sqlite3_step $STMT
sl@0
  1048
  sqlite3_column_int $STMT 0
sl@0
  1049
} {1}
sl@0
  1050
do_test capi3-15.2 {
sl@0
  1051
  sqlite3_step $STMT
sl@0
  1052
  sqlite3_column_int $STMT 0
sl@0
  1053
} {2}
sl@0
  1054
do_test capi3-15.3 {
sl@0
  1055
  sqlite3_finalize $STMT
sl@0
  1056
} {SQLITE_OK}
sl@0
  1057
do_test capi3-15.4 {
sl@0
  1058
  #        123456789 1234567
sl@0
  1059
  set sql {SELECT 1234567890}
sl@0
  1060
  set STMT [sqlite3_prepare $DB $sql 8 TAIL]
sl@0
  1061
  sqlite3_step $STMT
sl@0
  1062
  set v1 [sqlite3_column_int $STMT 0]
sl@0
  1063
  sqlite3_finalize $STMT
sl@0
  1064
  set v1
sl@0
  1065
} {1}
sl@0
  1066
do_test capi3-15.5 {
sl@0
  1067
  #        123456789 1234567
sl@0
  1068
  set sql {SELECT 1234567890}
sl@0
  1069
  set STMT [sqlite3_prepare $DB $sql 9 TAIL]
sl@0
  1070
  sqlite3_step $STMT
sl@0
  1071
  set v1 [sqlite3_column_int $STMT 0]
sl@0
  1072
  sqlite3_finalize $STMT
sl@0
  1073
  set v1
sl@0
  1074
} {12}
sl@0
  1075
do_test capi3-15.6 {
sl@0
  1076
  #        123456789 1234567
sl@0
  1077
  set sql {SELECT 1234567890}
sl@0
  1078
  set STMT [sqlite3_prepare $DB $sql 12 TAIL]
sl@0
  1079
  sqlite3_step $STMT
sl@0
  1080
  set v1 [sqlite3_column_int $STMT 0]
sl@0
  1081
  sqlite3_finalize $STMT
sl@0
  1082
  set v1
sl@0
  1083
} {12345}
sl@0
  1084
do_test capi3-15.7 {
sl@0
  1085
  #        123456789 1234567
sl@0
  1086
  set sql {SELECT 12.34567890}
sl@0
  1087
  set STMT [sqlite3_prepare $DB $sql 12 TAIL]
sl@0
  1088
  sqlite3_step $STMT
sl@0
  1089
  set v1 [sqlite3_column_double $STMT 0]
sl@0
  1090
  sqlite3_finalize $STMT
sl@0
  1091
  set v1
sl@0
  1092
} {12.34}
sl@0
  1093
do_test capi3-15.8 {
sl@0
  1094
  #        123456789 1234567
sl@0
  1095
  set sql {SELECT 12.34567890}
sl@0
  1096
  set STMT [sqlite3_prepare $DB $sql 14 TAIL]
sl@0
  1097
  sqlite3_step $STMT
sl@0
  1098
  set v1 [sqlite3_column_double $STMT 0]
sl@0
  1099
  sqlite3_finalize $STMT
sl@0
  1100
  set v1
sl@0
  1101
} {12.3456}
sl@0
  1102
sl@0
  1103
# Make sure code is always generated even if an IF EXISTS or 
sl@0
  1104
# IF NOT EXISTS clause is present that the table does not or
sl@0
  1105
# does exists.  That way we will always have a prepared statement
sl@0
  1106
# to expire when the schema changes.
sl@0
  1107
#
sl@0
  1108
do_test capi3-16.1 {
sl@0
  1109
  set sql {DROP TABLE IF EXISTS t3}
sl@0
  1110
  set STMT [sqlite3_prepare $DB $sql -1 TAIL]
sl@0
  1111
  sqlite3_finalize $STMT
sl@0
  1112
  expr {$STMT!=""}
sl@0
  1113
} {1}
sl@0
  1114
do_test capi3-16.2 {
sl@0
  1115
  set sql {CREATE TABLE IF NOT EXISTS t1(x,y)}
sl@0
  1116
  set STMT [sqlite3_prepare $DB $sql -1 TAIL]
sl@0
  1117
  sqlite3_finalize $STMT
sl@0
  1118
  expr {$STMT!=""}
sl@0
  1119
} {1}
sl@0
  1120
sl@0
  1121
# But still we do not generate code if there is no SQL
sl@0
  1122
#
sl@0
  1123
do_test capi3-16.3 {
sl@0
  1124
  set STMT [sqlite3_prepare $DB {} -1 TAIL]
sl@0
  1125
  sqlite3_finalize $STMT
sl@0
  1126
  expr {$STMT==""}
sl@0
  1127
} {1}
sl@0
  1128
do_test capi3-16.4 {
sl@0
  1129
  set STMT [sqlite3_prepare $DB {;} -1 TAIL]
sl@0
  1130
  sqlite3_finalize $STMT
sl@0
  1131
  expr {$STMT==""}
sl@0
  1132
} {1}
sl@0
  1133
sl@0
  1134
# Ticket #2426:  Misuse of sqlite3_column_* by calling it after
sl@0
  1135
# a sqlite3_reset should be harmless.
sl@0
  1136
#
sl@0
  1137
do_test capi3-17.1 {
sl@0
  1138
  set STMT [sqlite3_prepare $DB {SELECT * FROM t2} -1 TAIL]
sl@0
  1139
  sqlite3_step $STMT
sl@0
  1140
  sqlite3_column_int $STMT 0
sl@0
  1141
} {1}
sl@0
  1142
do_test capi3-17.2 {
sl@0
  1143
  sqlite3_reset $STMT
sl@0
  1144
  sqlite3_column_int $STMT 0
sl@0
  1145
} {0}
sl@0
  1146
do_test capi3-17.3 {
sl@0
  1147
  sqlite3_finalize $STMT
sl@0
  1148
} {SQLITE_OK}
sl@0
  1149
sl@0
  1150
# Verify that sqlite3_step() fails with an SQLITE_SCHEMA error
sl@0
  1151
# when the statement is prepared with sqlite3_prepare() (not
sl@0
  1152
# sqlite3_prepare_v2()) and the schema has changed.
sl@0
  1153
#
sl@0
  1154
do_test capi3-18.1 {
sl@0
  1155
  set STMT [sqlite3_prepare db {SELECT * FROM t2} -1 TAIL]
sl@0
  1156
  sqlite3 db2 test.db
sl@0
  1157
  db2 eval {CREATE TABLE t3(x)}
sl@0
  1158
  db2 close
sl@0
  1159
  sqlite3_step $STMT
sl@0
  1160
} {SQLITE_ERROR}
sl@0
  1161
do_test capi3-18.2 {
sl@0
  1162
  sqlite3_reset $STMT
sl@0
  1163
  sqlite3_errcode db
sl@0
  1164
} {SQLITE_SCHEMA}
sl@0
  1165
do_test capi3-18.3 {
sl@0
  1166
  sqlite3_errmsg db
sl@0
  1167
} {database schema has changed}
sl@0
  1168
# The error persist on retry when sqlite3_prepare() has been used.
sl@0
  1169
do_test capi3-18.4 {
sl@0
  1170
  sqlite3_step $STMT
sl@0
  1171
} {SQLITE_ERROR}
sl@0
  1172
do_test capi3-18.5 {
sl@0
  1173
  sqlite3_reset $STMT
sl@0
  1174
  sqlite3_errcode db
sl@0
  1175
} {SQLITE_SCHEMA}
sl@0
  1176
do_test capi3-18.6 {
sl@0
  1177
  sqlite3_errmsg db
sl@0
  1178
} {database schema has changed}
sl@0
  1179
sqlite3_finalize $STMT
sl@0
  1180
sl@0
  1181
# Ticket #3134.  Prepare a statement with an nBytes parameter of 0.
sl@0
  1182
# Make sure this works correctly and does not reference memory out of
sl@0
  1183
# range.
sl@0
  1184
#
sl@0
  1185
do_test capi3-19.1 {
sl@0
  1186
  sqlite3_prepare_tkt3134 db
sl@0
  1187
} {}
sl@0
  1188
sl@0
  1189
# Tests of the interface when no VFS is registered.
sl@0
  1190
#
sl@0
  1191
if {![info exists tester_do_binarylog]} {
sl@0
  1192
  db close
sl@0
  1193
  vfs_unregister_all
sl@0
  1194
  do_test capi3-20.1 {
sl@0
  1195
    sqlite3_sleep 100
sl@0
  1196
  } {0}
sl@0
  1197
  vfs_reregister_all
sl@0
  1198
}
sl@0
  1199
sl@0
  1200
finish_test