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