os/persistentdata/persistentstorage/sqlite3api/TEST/TclScript/capi2.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: capi2.test,v 1.36 2008/09/01 15:52:11 drh 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 text values from the current row pointed at by STMT as a list.
sl@0
    21
proc get_row_values {STMT} {
sl@0
    22
  set VALUES [list]
sl@0
    23
  for {set i 0} {$i < [sqlite3_data_count $STMT]} {incr i} {
sl@0
    24
    lappend VALUES [sqlite3_column_text $STMT $i]
sl@0
    25
  }
sl@0
    26
  return $VALUES
sl@0
    27
}
sl@0
    28
sl@0
    29
# Return the column names followed by declaration types for the result set
sl@0
    30
# of the SQL statement STMT.
sl@0
    31
#
sl@0
    32
# i.e. for:
sl@0
    33
# CREATE TABLE abc(a text, b integer); 
sl@0
    34
# SELECT * FROM abc;
sl@0
    35
#
sl@0
    36
# The result is {a b text integer}
sl@0
    37
proc get_column_names {STMT} {
sl@0
    38
  set VALUES [list]
sl@0
    39
  for {set i 0} {$i < [sqlite3_column_count $STMT]} {incr i} {
sl@0
    40
    lappend VALUES [sqlite3_column_name $STMT $i]
sl@0
    41
  }
sl@0
    42
  for {set i 0} {$i < [sqlite3_column_count $STMT]} {incr i} {
sl@0
    43
    lappend VALUES [sqlite3_column_decltype $STMT $i]
sl@0
    44
  }
sl@0
    45
  return $VALUES
sl@0
    46
}
sl@0
    47
sl@0
    48
# Check basic functionality
sl@0
    49
#
sl@0
    50
do_test capi2-1.1 {
sl@0
    51
  set DB [sqlite3_connection_pointer db]
sl@0
    52
  execsql {CREATE TABLE t1(a,b,c)}
sl@0
    53
  set VM [sqlite3_prepare $DB {SELECT name, rowid FROM sqlite_master} -1 TAIL]
sl@0
    54
  set TAIL
sl@0
    55
} {}
sl@0
    56
do_test capi2-1.2 {
sl@0
    57
  sqlite3_step $VM
sl@0
    58
} {SQLITE_ROW}
sl@0
    59
do_test capi2-1.3 {
sl@0
    60
  sqlite3_data_count $VM
sl@0
    61
} {2}
sl@0
    62
do_test capi2-1.4 {
sl@0
    63
  get_row_values $VM
sl@0
    64
} {t1 1}
sl@0
    65
do_test capi2-1.5 {
sl@0
    66
  get_column_names $VM
sl@0
    67
} {name rowid text INTEGER}
sl@0
    68
do_test capi2-1.6 {
sl@0
    69
  sqlite3_step $VM 
sl@0
    70
} {SQLITE_DONE}
sl@0
    71
do_test capi2-1.7 {
sl@0
    72
  list [sqlite3_column_count $VM] [get_row_values $VM] [get_column_names $VM]
sl@0
    73
} {2 {} {name rowid text INTEGER}}
sl@0
    74
do_test capi2-1.8-misuse {
sl@0
    75
  sqlite3_step $VM
sl@0
    76
} {SQLITE_MISUSE}
sl@0
    77
sl@0
    78
# Update: In v2, once SQLITE_MISUSE is returned the statement handle cannot
sl@0
    79
# be interrogated for more information. However in v3, since the column
sl@0
    80
# count, names and types are determined at compile time, these are still
sl@0
    81
# accessible after an SQLITE_MISUSE error.
sl@0
    82
do_test capi2-1.9 {
sl@0
    83
  list [sqlite3_column_count $VM] [get_row_values $VM] [get_column_names $VM]
sl@0
    84
} {2 {} {name rowid text INTEGER}}
sl@0
    85
do_test capi2-1.10 {
sl@0
    86
  sqlite3_data_count $VM
sl@0
    87
} {0}
sl@0
    88
sl@0
    89
do_test capi2-1.11 {
sl@0
    90
  sqlite3_finalize $VM
sl@0
    91
} {SQLITE_OK}
sl@0
    92
sl@0
    93
# Check to make sure that the "tail" of a multi-statement SQL script
sl@0
    94
# is returned by sqlite3_prepare.
sl@0
    95
#
sl@0
    96
do_test capi2-2.1 {
sl@0
    97
  set SQL {
sl@0
    98
    SELECT name, rowid FROM sqlite_master;
sl@0
    99
    SELECT name, rowid FROM sqlite_master WHERE 0;
sl@0
   100
    -- A comment at the end
sl@0
   101
  }
sl@0
   102
  set VM [sqlite3_prepare $DB $SQL -1 SQL]
sl@0
   103
  set SQL
sl@0
   104
} {
sl@0
   105
    SELECT name, rowid FROM sqlite_master WHERE 0;
sl@0
   106
    -- A comment at the end
sl@0
   107
  }
sl@0
   108
do_test capi2-2.2 {
sl@0
   109
  set r [sqlite3_step $VM]
sl@0
   110
  lappend r [sqlite3_column_count $VM] \
sl@0
   111
            [get_row_values $VM] \
sl@0
   112
            [get_column_names $VM]
sl@0
   113
} {SQLITE_ROW 2 {t1 1} {name rowid text INTEGER}}
sl@0
   114
do_test capi2-2.3 {
sl@0
   115
  set r [sqlite3_step $VM]
sl@0
   116
  lappend r [sqlite3_column_count $VM] \
sl@0
   117
            [get_row_values $VM] \
sl@0
   118
            [get_column_names $VM]
sl@0
   119
} {SQLITE_DONE 2 {} {name rowid text INTEGER}}
sl@0
   120
do_test capi2-2.4 {
sl@0
   121
  sqlite3_finalize $VM
sl@0
   122
} {SQLITE_OK}
sl@0
   123
do_test capi2-2.5 {
sl@0
   124
  set VM [sqlite3_prepare $DB $SQL -1 SQL]
sl@0
   125
  set SQL
sl@0
   126
} {
sl@0
   127
    -- A comment at the end
sl@0
   128
  }
sl@0
   129
do_test capi2-2.6 {
sl@0
   130
  set r [sqlite3_step $VM]
sl@0
   131
  lappend r [sqlite3_column_count $VM] \
sl@0
   132
            [get_row_values $VM] \
sl@0
   133
            [get_column_names $VM]
sl@0
   134
} {SQLITE_DONE 2 {} {name rowid text INTEGER}}
sl@0
   135
do_test capi2-2.7 {
sl@0
   136
  sqlite3_finalize $VM
sl@0
   137
} {SQLITE_OK}
sl@0
   138
do_test capi2-2.8 {
sl@0
   139
  set VM [sqlite3_prepare $DB $SQL -1 SQL]
sl@0
   140
  list $SQL $VM
sl@0
   141
} {{} {}}
sl@0
   142
sl@0
   143
# Check the error handling.
sl@0
   144
#
sl@0
   145
do_test capi2-3.1 {
sl@0
   146
  set rc [catch {
sl@0
   147
      sqlite3_prepare $DB {select bogus from sqlite_master} -1 TAIL
sl@0
   148
  } msg]
sl@0
   149
  lappend rc $msg $TAIL
sl@0
   150
} {1 {(1) no such column: bogus} {}}
sl@0
   151
do_test capi2-3.2 {
sl@0
   152
  set rc [catch {
sl@0
   153
      sqlite3_prepare $DB {select bogus from } -1 TAIL
sl@0
   154
  } msg]
sl@0
   155
  lappend rc $msg $TAIL
sl@0
   156
} {1 {(1) near " ": syntax error} {}}
sl@0
   157
do_test capi2-3.3 {
sl@0
   158
  set rc [catch {
sl@0
   159
      sqlite3_prepare $DB {;;;;select bogus from sqlite_master} -1 TAIL
sl@0
   160
  } msg]
sl@0
   161
  lappend rc $msg $TAIL
sl@0
   162
} {1 {(1) no such column: bogus} {}}
sl@0
   163
do_test capi2-3.4 {
sl@0
   164
  set rc [catch {
sl@0
   165
      sqlite3_prepare $DB {select bogus from sqlite_master;x;} -1 TAIL
sl@0
   166
  } msg]
sl@0
   167
  lappend rc $msg $TAIL
sl@0
   168
} {1 {(1) no such column: bogus} {x;}}
sl@0
   169
do_test capi2-3.5 {
sl@0
   170
  set rc [catch {
sl@0
   171
      sqlite3_prepare $DB {select bogus from sqlite_master;;;x;} -1 TAIL
sl@0
   172
  } msg]
sl@0
   173
  lappend rc $msg $TAIL
sl@0
   174
} {1 {(1) no such column: bogus} {;;x;}}
sl@0
   175
do_test capi2-3.6 {
sl@0
   176
  set rc [catch {
sl@0
   177
      sqlite3_prepare $DB {select 5/0} -1 TAIL
sl@0
   178
  } VM]
sl@0
   179
  lappend rc $TAIL
sl@0
   180
} {0 {}}
sl@0
   181
do_test capi2-3.7 {
sl@0
   182
  list [sqlite3_step $VM] \
sl@0
   183
       [sqlite3_column_count $VM] \
sl@0
   184
       [get_row_values $VM] \
sl@0
   185
       [get_column_names $VM]
sl@0
   186
} {SQLITE_ROW 1 {{}} {5/0 {}}}
sl@0
   187
do_test capi2-3.8 {
sl@0
   188
  sqlite3_finalize $VM
sl@0
   189
} {SQLITE_OK}
sl@0
   190
do_test capi2-3.9 {
sl@0
   191
  execsql {CREATE UNIQUE INDEX i1 ON t1(a)}
sl@0
   192
  set VM [sqlite3_prepare $DB {INSERT INTO t1 VALUES(1,2,3)} -1 TAIL]
sl@0
   193
  set TAIL
sl@0
   194
} {}
sl@0
   195
do_test capi2-3.9b {db changes} {0}
sl@0
   196
do_test capi2-3.10 {
sl@0
   197
  list [sqlite3_step $VM] \
sl@0
   198
       [sqlite3_column_count $VM] \
sl@0
   199
       [get_row_values $VM] \
sl@0
   200
       [get_column_names $VM]
sl@0
   201
} {SQLITE_DONE 0 {} {}}
sl@0
   202
sl@0
   203
# Update for v3 - the change has not actually happened until the query is
sl@0
   204
# finalized. Is this going to cause trouble for anyone? Lee Nelson maybe?
sl@0
   205
# (Later:) The change now happens just before SQLITE_DONE is returned.
sl@0
   206
do_test capi2-3.10b {db changes} {1}
sl@0
   207
do_test capi2-3.11 {
sl@0
   208
  sqlite3_finalize $VM
sl@0
   209
} {SQLITE_OK}
sl@0
   210
do_test capi2-3.11b {db changes} {1}
sl@0
   211
#do_test capi2-3.12-misuse {
sl@0
   212
#  sqlite3_finalize $VM
sl@0
   213
#} {SQLITE_MISUSE}
sl@0
   214
do_test capi2-3.13 {
sl@0
   215
  set VM [sqlite3_prepare $DB {INSERT INTO t1 VALUES(1,3,4)} -1 TAIL]
sl@0
   216
  list [sqlite3_step $VM] \
sl@0
   217
       [sqlite3_column_count $VM] \
sl@0
   218
       [get_row_values $VM] \
sl@0
   219
       [get_column_names $VM]
sl@0
   220
} {SQLITE_ERROR 0 {} {}}
sl@0
   221
sl@0
   222
# Update for v3: Preparing a statement does not affect the change counter.
sl@0
   223
# (Test result changes from 0 to 1).  (Later:) change counter updates occur
sl@0
   224
# when sqlite3_step returns, not at finalize time.
sl@0
   225
do_test capi2-3.13b {db changes} {0}
sl@0
   226
sl@0
   227
do_test capi2-3.14 {
sl@0
   228
  list [sqlite3_finalize $VM] [sqlite3_errmsg $DB]
sl@0
   229
} {SQLITE_CONSTRAINT {column a is not unique}}
sl@0
   230
do_test capi2-3.15 {
sl@0
   231
  set VM [sqlite3_prepare $DB {CREATE TABLE t2(a NOT NULL, b)} -1 TAIL]
sl@0
   232
  set TAIL
sl@0
   233
} {}
sl@0
   234
do_test capi2-3.16 {
sl@0
   235
  list [sqlite3_step $VM] \
sl@0
   236
       [sqlite3_column_count $VM] \
sl@0
   237
       [get_row_values $VM] \
sl@0
   238
       [get_column_names $VM]
sl@0
   239
} {SQLITE_DONE 0 {} {}}
sl@0
   240
do_test capi2-3.17 {
sl@0
   241
  list [sqlite3_finalize $VM] [sqlite3_errmsg $DB]
sl@0
   242
} {SQLITE_OK {not an error}}
sl@0
   243
do_test capi2-3.18 {
sl@0
   244
  set VM [sqlite3_prepare $DB {INSERT INTO t2 VALUES(NULL,2)} -1 TAIL]
sl@0
   245
  list [sqlite3_step $VM] \
sl@0
   246
       [sqlite3_column_count $VM] \
sl@0
   247
       [get_row_values $VM] \
sl@0
   248
       [get_column_names $VM]
sl@0
   249
} {SQLITE_ERROR 0 {} {}}
sl@0
   250
do_test capi2-3.19 {
sl@0
   251
  list [sqlite3_finalize $VM] [sqlite3_errmsg $DB]
sl@0
   252
} {SQLITE_CONSTRAINT {t2.a may not be NULL}}
sl@0
   253
sl@0
   254
do_test capi2-3.20 {
sl@0
   255
  execsql {
sl@0
   256
    CREATE TABLE a1(message_id, name , UNIQUE(message_id, name) );
sl@0
   257
    INSERT INTO a1 VALUES(1, 1);
sl@0
   258
  }
sl@0
   259
} {}
sl@0
   260
do_test capi2-3.21 {
sl@0
   261
  set VM [sqlite3_prepare $DB {INSERT INTO a1 VALUES(1, 1)} -1 TAIL]
sl@0
   262
  sqlite3_step $VM
sl@0
   263
} {SQLITE_ERROR}
sl@0
   264
do_test capi2-3.22 {
sl@0
   265
  sqlite3_errcode $DB
sl@0
   266
} {SQLITE_ERROR}
sl@0
   267
do_test capi2-3.23 {
sl@0
   268
  sqlite3_finalize $VM
sl@0
   269
} {SQLITE_CONSTRAINT}
sl@0
   270
do_test capi2-3.24 {
sl@0
   271
  sqlite3_errcode $DB
sl@0
   272
} {SQLITE_CONSTRAINT}
sl@0
   273
sl@0
   274
# Two or more virtual machines exists at the same time.
sl@0
   275
#
sl@0
   276
do_test capi2-4.1 {
sl@0
   277
  set VM1 [sqlite3_prepare $DB {INSERT INTO t2 VALUES(1,2)} -1 TAIL]
sl@0
   278
  set TAIL
sl@0
   279
} {}
sl@0
   280
do_test capi2-4.2 {
sl@0
   281
  set VM2 [sqlite3_prepare $DB {INSERT INTO t2 VALUES(2,3)} -1 TAIL]
sl@0
   282
  set TAIL
sl@0
   283
} {}
sl@0
   284
do_test capi2-4.3 {
sl@0
   285
  set VM3 [sqlite3_prepare $DB {INSERT INTO t2 VALUES(3,4)} -1 TAIL]
sl@0
   286
  set TAIL
sl@0
   287
} {}
sl@0
   288
do_test capi2-4.4 {
sl@0
   289
  list [sqlite3_step $VM2] \
sl@0
   290
       [sqlite3_column_count $VM2] \
sl@0
   291
       [get_row_values $VM2] \
sl@0
   292
       [get_column_names $VM2]
sl@0
   293
} {SQLITE_DONE 0 {} {}}
sl@0
   294
do_test capi2-4.5 {
sl@0
   295
  execsql {SELECT * FROM t2 ORDER BY a}
sl@0
   296
} {2 3}
sl@0
   297
do_test capi2-4.6 {
sl@0
   298
  sqlite3_finalize $VM2
sl@0
   299
} {SQLITE_OK}
sl@0
   300
do_test capi2-4.7 {
sl@0
   301
  list [sqlite3_step $VM3] \
sl@0
   302
       [sqlite3_column_count $VM3] \
sl@0
   303
       [get_row_values $VM3] \
sl@0
   304
       [get_column_names $VM3]
sl@0
   305
} {SQLITE_DONE 0 {} {}}
sl@0
   306
do_test capi2-4.8 {
sl@0
   307
  execsql {SELECT * FROM t2 ORDER BY a}
sl@0
   308
} {2 3 3 4}
sl@0
   309
do_test capi2-4.9 {
sl@0
   310
  sqlite3_finalize $VM3
sl@0
   311
} {SQLITE_OK}
sl@0
   312
do_test capi2-4.10 {
sl@0
   313
  list [sqlite3_step $VM1] \
sl@0
   314
       [sqlite3_column_count $VM1] \
sl@0
   315
       [get_row_values $VM1] \
sl@0
   316
       [get_column_names $VM1]
sl@0
   317
} {SQLITE_DONE 0 {} {}}
sl@0
   318
do_test capi2-4.11 {
sl@0
   319
  execsql {SELECT * FROM t2 ORDER BY a}
sl@0
   320
} {1 2 2 3 3 4}
sl@0
   321
do_test capi2-4.12 {
sl@0
   322
  sqlite3_finalize $VM1
sl@0
   323
} {SQLITE_OK}
sl@0
   324
sl@0
   325
# Interleaved SELECTs
sl@0
   326
#
sl@0
   327
do_test capi2-5.1 {
sl@0
   328
  set VM1 [sqlite3_prepare $DB {SELECT * FROM t2} -1 TAIL]
sl@0
   329
  set VM2 [sqlite3_prepare $DB {SELECT * FROM t2} -1 TAIL]
sl@0
   330
  set VM3 [sqlite3_prepare $DB {SELECT * FROM t2} -1 TAIL]
sl@0
   331
  list [sqlite3_step $VM1] \
sl@0
   332
       [sqlite3_column_count $VM1] \
sl@0
   333
       [get_row_values $VM1] \
sl@0
   334
       [get_column_names $VM1]
sl@0
   335
} {SQLITE_ROW 2 {2 3} {a b {} {}}}
sl@0
   336
do_test capi2-5.2 {
sl@0
   337
  list [sqlite3_step $VM2] \
sl@0
   338
       [sqlite3_column_count $VM2] \
sl@0
   339
       [get_row_values $VM2] \
sl@0
   340
       [get_column_names $VM2]
sl@0
   341
} {SQLITE_ROW 2 {2 3} {a b {} {}}}
sl@0
   342
do_test capi2-5.3 {
sl@0
   343
  list [sqlite3_step $VM1] \
sl@0
   344
       [sqlite3_column_count $VM1] \
sl@0
   345
       [get_row_values $VM1] \
sl@0
   346
       [get_column_names $VM1]
sl@0
   347
} {SQLITE_ROW 2 {3 4} {a b {} {}}}
sl@0
   348
do_test capi2-5.4 {
sl@0
   349
  list [sqlite3_step $VM3] \
sl@0
   350
       [sqlite3_column_count $VM3] \
sl@0
   351
       [get_row_values $VM3] \
sl@0
   352
       [get_column_names $VM3]
sl@0
   353
} {SQLITE_ROW 2 {2 3} {a b {} {}}}
sl@0
   354
do_test capi2-5.5 {
sl@0
   355
  list [sqlite3_step $VM3] \
sl@0
   356
       [sqlite3_column_count $VM3] \
sl@0
   357
       [get_row_values $VM3] \
sl@0
   358
       [get_column_names $VM3]
sl@0
   359
} {SQLITE_ROW 2 {3 4} {a b {} {}}}
sl@0
   360
do_test capi2-5.6 {
sl@0
   361
  list [sqlite3_step $VM3] \
sl@0
   362
       [sqlite3_column_count $VM3] \
sl@0
   363
       [get_row_values $VM3] \
sl@0
   364
       [get_column_names $VM3]
sl@0
   365
} {SQLITE_ROW 2 {1 2} {a b {} {}}}
sl@0
   366
do_test capi2-5.7 {
sl@0
   367
  list [sqlite3_step $VM3] \
sl@0
   368
       [sqlite3_column_count $VM3] \
sl@0
   369
       [get_row_values $VM3] \
sl@0
   370
       [get_column_names $VM3]
sl@0
   371
} {SQLITE_DONE 2 {} {a b {} {}}}
sl@0
   372
do_test capi2-5.8 {
sl@0
   373
  sqlite3_finalize $VM3
sl@0
   374
} {SQLITE_OK}
sl@0
   375
do_test capi2-5.9 {
sl@0
   376
  list [sqlite3_step $VM1] \
sl@0
   377
       [sqlite3_column_count $VM1] \
sl@0
   378
       [get_row_values $VM1] \
sl@0
   379
       [get_column_names $VM1]
sl@0
   380
} {SQLITE_ROW 2 {1 2} {a b {} {}}}
sl@0
   381
do_test capi2-5.10 {
sl@0
   382
  sqlite3_finalize $VM1
sl@0
   383
} {SQLITE_OK}
sl@0
   384
do_test capi2-5.11 {
sl@0
   385
  list [sqlite3_step $VM2] \
sl@0
   386
       [sqlite3_column_count $VM2] \
sl@0
   387
       [get_row_values $VM2] \
sl@0
   388
       [get_column_names $VM2]
sl@0
   389
} {SQLITE_ROW 2 {3 4} {a b {} {}}}
sl@0
   390
do_test capi2-5.12 {
sl@0
   391
  list [sqlite3_step $VM2] \
sl@0
   392
       [sqlite3_column_count $VM2] \
sl@0
   393
       [get_row_values $VM2] \
sl@0
   394
       [get_column_names $VM2]
sl@0
   395
} {SQLITE_ROW 2 {1 2} {a b {} {}}}
sl@0
   396
do_test capi2-5.11 {
sl@0
   397
  sqlite3_finalize $VM2
sl@0
   398
} {SQLITE_OK}
sl@0
   399
sl@0
   400
# Check for proper SQLITE_BUSY returns.
sl@0
   401
#
sl@0
   402
do_test capi2-6.1 {
sl@0
   403
  execsql {
sl@0
   404
    BEGIN;
sl@0
   405
    CREATE TABLE t3(x counter);
sl@0
   406
    INSERT INTO t3 VALUES(1);
sl@0
   407
    INSERT INTO t3 VALUES(2);
sl@0
   408
    INSERT INTO t3 SELECT x+2 FROM t3;
sl@0
   409
    INSERT INTO t3 SELECT x+4 FROM t3;
sl@0
   410
    INSERT INTO t3 SELECT x+8 FROM t3;
sl@0
   411
    COMMIT;
sl@0
   412
  }
sl@0
   413
  set VM1 [sqlite3_prepare $DB {SELECT * FROM t3} -1 TAIL]
sl@0
   414
  sqlite3 db2 test.db
sl@0
   415
  execsql {BEGIN} db2
sl@0
   416
} {}
sl@0
   417
# Update for v3: BEGIN doesn't write-lock the database. It is quite
sl@0
   418
# difficult to get v3 to write-lock the database, which causes a few
sl@0
   419
# problems for test scripts.
sl@0
   420
#
sl@0
   421
# do_test capi2-6.2 {
sl@0
   422
#   list [sqlite3_step $VM1] \
sl@0
   423
#        [sqlite3_column_count $VM1] \
sl@0
   424
#        [get_row_values $VM1] \
sl@0
   425
#        [get_column_names $VM1]
sl@0
   426
# } {SQLITE_BUSY 0 {} {}}
sl@0
   427
do_test capi2-6.3 {
sl@0
   428
  execsql {COMMIT} db2
sl@0
   429
} {}
sl@0
   430
do_test capi2-6.4 {
sl@0
   431
  list [sqlite3_step $VM1] \
sl@0
   432
       [sqlite3_column_count $VM1] \
sl@0
   433
       [get_row_values $VM1] \
sl@0
   434
       [get_column_names $VM1]
sl@0
   435
} {SQLITE_ROW 1 1 {x counter}}
sl@0
   436
do_test capi2-6.5 {
sl@0
   437
  catchsql {INSERT INTO t3 VALUES(10);} db2
sl@0
   438
} {1 {database is locked}}
sl@0
   439
do_test capi2-6.6 {
sl@0
   440
  list [sqlite3_step $VM1] \
sl@0
   441
       [sqlite3_column_count $VM1] \
sl@0
   442
       [get_row_values $VM1] \
sl@0
   443
       [get_column_names $VM1]
sl@0
   444
} {SQLITE_ROW 1 2 {x counter}}
sl@0
   445
do_test capi2-6.7 {
sl@0
   446
  execsql {SELECT * FROM t2} db2
sl@0
   447
} {2 3 3 4 1 2}
sl@0
   448
do_test capi2-6.8 {
sl@0
   449
  list [sqlite3_step $VM1] \
sl@0
   450
       [sqlite3_column_count $VM1] \
sl@0
   451
       [get_row_values $VM1] \
sl@0
   452
       [get_column_names $VM1]
sl@0
   453
} {SQLITE_ROW 1 3 {x counter}}
sl@0
   454
do_test capi2-6.9 {
sl@0
   455
  execsql {SELECT * FROM t2} 
sl@0
   456
} {2 3 3 4 1 2}
sl@0
   457
do_test capi2-6.10 {
sl@0
   458
  list [sqlite3_step $VM1] \
sl@0
   459
       [sqlite3_column_count $VM1] \
sl@0
   460
       [get_row_values $VM1] \
sl@0
   461
       [get_column_names $VM1]
sl@0
   462
} {SQLITE_ROW 1 4 {x counter}}
sl@0
   463
do_test capi2-6.11 {
sl@0
   464
  execsql {BEGIN}
sl@0
   465
} {}
sl@0
   466
do_test capi2-6.12 {
sl@0
   467
  list [sqlite3_step $VM1] \
sl@0
   468
       [sqlite3_column_count $VM1] \
sl@0
   469
       [get_row_values $VM1] \
sl@0
   470
       [get_column_names $VM1]
sl@0
   471
} {SQLITE_ROW 1 5 {x counter}}
sl@0
   472
sl@0
   473
# A read no longer blocks a write in the same connection.
sl@0
   474
#do_test capi2-6.13 {
sl@0
   475
#  catchsql {UPDATE t3 SET x=x+1}
sl@0
   476
#} {1 {database table is locked}}
sl@0
   477
sl@0
   478
do_test capi2-6.14 {
sl@0
   479
  list [sqlite3_step $VM1] \
sl@0
   480
       [sqlite3_column_count $VM1] \
sl@0
   481
       [get_row_values $VM1] \
sl@0
   482
       [get_column_names $VM1]
sl@0
   483
} {SQLITE_ROW 1 6 {x counter}}
sl@0
   484
do_test capi2-6.15 {
sl@0
   485
  execsql {SELECT * FROM t1}
sl@0
   486
} {1 2 3}
sl@0
   487
do_test capi2-6.16 {
sl@0
   488
  list [sqlite3_step $VM1] \
sl@0
   489
       [sqlite3_column_count $VM1] \
sl@0
   490
       [get_row_values $VM1] \
sl@0
   491
       [get_column_names $VM1]
sl@0
   492
} {SQLITE_ROW 1 7 {x counter}}
sl@0
   493
do_test capi2-6.17 {
sl@0
   494
  catchsql {UPDATE t1 SET b=b+1}
sl@0
   495
} {0 {}}
sl@0
   496
do_test capi2-6.18 {
sl@0
   497
  list [sqlite3_step $VM1] \
sl@0
   498
       [sqlite3_column_count $VM1] \
sl@0
   499
       [get_row_values $VM1] \
sl@0
   500
       [get_column_names $VM1]
sl@0
   501
} {SQLITE_ROW 1 8 {x counter}}
sl@0
   502
do_test capi2-6.19 {
sl@0
   503
  execsql {SELECT * FROM t1}
sl@0
   504
} {1 3 3}
sl@0
   505
do_test capi2-6.20 {
sl@0
   506
  list [sqlite3_step $VM1] \
sl@0
   507
       [sqlite3_column_count $VM1] \
sl@0
   508
       [get_row_values $VM1] \
sl@0
   509
       [get_column_names $VM1]
sl@0
   510
} {SQLITE_ROW 1 9 {x counter}}
sl@0
   511
#do_test capi2-6.21 {
sl@0
   512
#  execsql {ROLLBACK; SELECT * FROM t1}
sl@0
   513
#} {1 2 3}
sl@0
   514
do_test capi2-6.22 {
sl@0
   515
  list [sqlite3_step $VM1] \
sl@0
   516
       [sqlite3_column_count $VM1] \
sl@0
   517
       [get_row_values $VM1] \
sl@0
   518
       [get_column_names $VM1]
sl@0
   519
} {SQLITE_ROW 1 10 {x counter}}
sl@0
   520
#do_test capi2-6.23 {
sl@0
   521
#  execsql {BEGIN TRANSACTION;}
sl@0
   522
#} {}
sl@0
   523
do_test capi2-6.24 {
sl@0
   524
  list [sqlite3_step $VM1] \
sl@0
   525
       [sqlite3_column_count $VM1] \
sl@0
   526
       [get_row_values $VM1] \
sl@0
   527
       [get_column_names $VM1]
sl@0
   528
} {SQLITE_ROW 1 11 {x counter}}
sl@0
   529
do_test capi2-6.25 {
sl@0
   530
  execsql {
sl@0
   531
    INSERT INTO t1 VALUES(2,3,4);
sl@0
   532
    SELECT * FROM t1;
sl@0
   533
  }
sl@0
   534
} {1 3 3 2 3 4}
sl@0
   535
do_test capi2-6.26 {
sl@0
   536
  list [sqlite3_step $VM1] \
sl@0
   537
       [sqlite3_column_count $VM1] \
sl@0
   538
       [get_row_values $VM1] \
sl@0
   539
       [get_column_names $VM1]
sl@0
   540
} {SQLITE_ROW 1 12 {x counter}}
sl@0
   541
do_test capi2-6.27 {
sl@0
   542
  catchsql {
sl@0
   543
    INSERT INTO t1 VALUES(2,4,5);
sl@0
   544
    SELECT * FROM t1;
sl@0
   545
  }
sl@0
   546
} {1 {column a is not unique}}
sl@0
   547
do_test capi2-6.28 {
sl@0
   548
  list [sqlite3_step $VM1] \
sl@0
   549
       [sqlite3_column_count $VM1] \
sl@0
   550
       [get_row_values $VM1] \
sl@0
   551
       [get_column_names $VM1]
sl@0
   552
} {SQLITE_ROW 1 13 {x counter}}
sl@0
   553
do_test capi2-6.99 {
sl@0
   554
  sqlite3_finalize $VM1
sl@0
   555
} {SQLITE_OK}
sl@0
   556
catchsql {ROLLBACK}
sl@0
   557
sl@0
   558
do_test capi2-7.1 {
sl@0
   559
  stepsql $DB {
sl@0
   560
    SELECT * FROM t1
sl@0
   561
  }
sl@0
   562
} {0 1 2 3}
sl@0
   563
do_test capi2-7.2 {
sl@0
   564
  stepsql $DB {
sl@0
   565
    PRAGMA count_changes=on
sl@0
   566
  }
sl@0
   567
} {0}
sl@0
   568
do_test capi2-7.3 {
sl@0
   569
  stepsql $DB {
sl@0
   570
    UPDATE t1 SET a=a+10;
sl@0
   571
  }
sl@0
   572
} {0 1}
sl@0
   573
do_test capi2-7.4 {
sl@0
   574
  stepsql $DB {
sl@0
   575
    INSERT INTO t1 SELECT a+1,b+1,c+1 FROM t1;
sl@0
   576
  }
sl@0
   577
} {0 1}
sl@0
   578
do_test capi2-7.4b {sqlite3_changes $DB} {1}
sl@0
   579
do_test capi2-7.5 {
sl@0
   580
  stepsql $DB {
sl@0
   581
    UPDATE t1 SET a=a+10;
sl@0
   582
  }
sl@0
   583
} {0 2}
sl@0
   584
do_test capi2-7.5b {sqlite3_changes $DB} {2}
sl@0
   585
do_test capi2-7.6 {
sl@0
   586
  stepsql $DB {
sl@0
   587
    SELECT * FROM t1;
sl@0
   588
  }
sl@0
   589
} {0 21 2 3 22 3 4}
sl@0
   590
do_test capi2-7.7 {
sl@0
   591
  stepsql $DB {
sl@0
   592
    INSERT INTO t1 SELECT a+2,b+2,c+2 FROM t1;
sl@0
   593
  }
sl@0
   594
} {0 2}
sl@0
   595
do_test capi2-7.8 {
sl@0
   596
  sqlite3_changes $DB
sl@0
   597
} {2}
sl@0
   598
do_test capi2-7.9 {
sl@0
   599
  stepsql $DB {
sl@0
   600
    SELECT * FROM t1;
sl@0
   601
  }
sl@0
   602
} {0 21 2 3 22 3 4 23 4 5 24 5 6}
sl@0
   603
do_test capi2-7.10 {
sl@0
   604
  stepsql $DB {
sl@0
   605
    UPDATE t1 SET a=a-20;
sl@0
   606
    SELECT * FROM t1;
sl@0
   607
  }
sl@0
   608
} {0 4 1 2 3 2 3 4 3 4 5 4 5 6}
sl@0
   609
sl@0
   610
# Update for version 3: A SELECT statement no longer resets the change
sl@0
   611
# counter (Test result changes from 0 to 4).
sl@0
   612
do_test capi2-7.11 {
sl@0
   613
  sqlite3_changes $DB
sl@0
   614
} {4}
sl@0
   615
do_test capi2-7.11a {
sl@0
   616
  execsql {SELECT count(*) FROM t1}
sl@0
   617
} {4}
sl@0
   618
sl@0
   619
ifcapable {explain} {
sl@0
   620
  do_test capi2-7.12 {
sl@0
   621
    set x [stepsql $DB {EXPLAIN SELECT * FROM t1}]
sl@0
   622
    lindex $x 0
sl@0
   623
  } {0}
sl@0
   624
}
sl@0
   625
sl@0
   626
# Ticket #261 - make sure we can finalize before the end of a query.
sl@0
   627
#
sl@0
   628
do_test capi2-8.1 {
sl@0
   629
  set VM1 [sqlite3_prepare $DB {SELECT * FROM t2} -1 TAIL]
sl@0
   630
  sqlite3_finalize $VM1
sl@0
   631
} {SQLITE_OK}
sl@0
   632
  
sl@0
   633
# Tickets #384 and #385 - make sure the TAIL argument to sqlite3_prepare
sl@0
   634
# and all of the return pointers in sqlite_step can be null.
sl@0
   635
#
sl@0
   636
do_test capi2-9.1 {
sl@0
   637
  set VM1 [sqlite3_prepare $DB {SELECT * FROM t2} -1 DUMMY]
sl@0
   638
  sqlite3_step $VM1
sl@0
   639
  sqlite3_finalize $VM1
sl@0
   640
} {SQLITE_OK}
sl@0
   641
sl@0
   642
# Test that passing a NULL pointer to sqlite3_finalize() or sqlite3_reset
sl@0
   643
# does not cause an error.
sl@0
   644
do_test capi2-10.1 {
sl@0
   645
  sqlite3_finalize 0
sl@0
   646
} {SQLITE_OK}
sl@0
   647
do_test capi2-10.2 {
sl@0
   648
  sqlite3_reset 0
sl@0
   649
} {SQLITE_OK}
sl@0
   650
sl@0
   651
#---------------------------------------------------------------------------
sl@0
   652
# The following tests - capi2-11.* - test the "column origin" APIs.
sl@0
   653
#
sl@0
   654
#   sqlite3_column_origin_name()
sl@0
   655
#   sqlite3_column_database_name()
sl@0
   656
#   sqlite3_column_table_name()
sl@0
   657
#
sl@0
   658
sl@0
   659
ifcapable columnmetadata {
sl@0
   660
sl@0
   661
# This proc uses the database handle $::DB to compile the SQL statement passed
sl@0
   662
# as a parameter. The return value of this procedure is a list with one
sl@0
   663
# element for each column returned by the compiled statement. Each element of
sl@0
   664
# this list is itself a list of length three, consisting of the origin
sl@0
   665
# database, table and column for the corresponding returned column.
sl@0
   666
proc check_origins {sql} {
sl@0
   667
  set ret [list]
sl@0
   668
  set ::STMT [sqlite3_prepare $::DB $sql -1 dummy]
sl@0
   669
  for {set i 0} {$i < [sqlite3_column_count $::STMT]} {incr i} {
sl@0
   670
    lappend ret [list                           \
sl@0
   671
      [sqlite3_column_database_name $::STMT $i] \
sl@0
   672
      [sqlite3_column_table_name $::STMT $i]    \
sl@0
   673
      [sqlite3_column_origin_name $::STMT $i]   \
sl@0
   674
    ]
sl@0
   675
  }
sl@0
   676
  sqlite3_finalize $::STMT
sl@0
   677
  return $ret
sl@0
   678
}
sl@0
   679
do_test capi2-11.1 {
sl@0
   680
  execsql {
sl@0
   681
    CREATE TABLE tab1(col1, col2);
sl@0
   682
  }
sl@0
   683
} {}
sl@0
   684
do_test capi2-11.2 {
sl@0
   685
  check_origins {SELECT col2, col1 FROM tab1}
sl@0
   686
} [list {main tab1 col2} {main tab1 col1}]
sl@0
   687
do_test capi2-11.3 {
sl@0
   688
  check_origins {SELECT col2 AS hello, col1 AS world FROM tab1}
sl@0
   689
} [list {main tab1 col2} {main tab1 col1}]
sl@0
   690
sl@0
   691
ifcapable subquery {
sl@0
   692
  do_test capi2-11.4 {
sl@0
   693
    check_origins {SELECT b, a FROM (SELECT col1 AS a, col2 AS b FROM tab1)}
sl@0
   694
  } [list {main tab1 col2} {main tab1 col1}]
sl@0
   695
  do_test capi2-11.5 {
sl@0
   696
    check_origins {SELECT (SELECT col2 FROM tab1), (SELECT col1 FROM tab1)}
sl@0
   697
  } [list {main tab1 col2} {main tab1 col1}]
sl@0
   698
  do_test capi2-11.6 {
sl@0
   699
    check_origins {SELECT (SELECT col2), (SELECT col1) FROM tab1}
sl@0
   700
  } [list {main tab1 col2} {main tab1 col1}]
sl@0
   701
  do_test capi2-11.7 {
sl@0
   702
    check_origins {SELECT * FROM tab1}
sl@0
   703
  } [list {main tab1 col1} {main tab1 col2}]
sl@0
   704
  do_test capi2-11.8 {
sl@0
   705
    check_origins {SELECT * FROM (SELECT * FROM tab1)}
sl@0
   706
  } [list {main tab1 col1} {main tab1 col2}]
sl@0
   707
}
sl@0
   708
sl@0
   709
ifcapable view&&subquery {
sl@0
   710
  do_test capi2-12.1 {
sl@0
   711
    execsql {
sl@0
   712
      CREATE VIEW view1 AS SELECT * FROM  tab1;
sl@0
   713
    }
sl@0
   714
  } {}
sl@0
   715
  do_test capi2-12.2 {
sl@0
   716
    check_origins {SELECT col2, col1 FROM view1}
sl@0
   717
  } [list {main tab1 col2} {main tab1 col1}]
sl@0
   718
  do_test capi2-12.3 {
sl@0
   719
    check_origins {SELECT col2 AS hello, col1 AS world FROM view1}
sl@0
   720
  } [list {main tab1 col2} {main tab1 col1}]
sl@0
   721
  do_test capi2-12.4 {
sl@0
   722
    check_origins {SELECT b, a FROM (SELECT col1 AS a, col2 AS b FROM view1)}
sl@0
   723
  } [list {main tab1 col2} {main tab1 col1}]
sl@0
   724
  do_test capi2-12.5 {
sl@0
   725
breakpoint
sl@0
   726
    check_origins {SELECT (SELECT col2 FROM view1), (SELECT col1 FROM view1)}
sl@0
   727
  } [list {main tab1 col2} {main tab1 col1}]
sl@0
   728
  do_test capi2-12.6 {
sl@0
   729
    check_origins {SELECT (SELECT col2), (SELECT col1) FROM view1}
sl@0
   730
  } [list {main tab1 col2} {main tab1 col1}]
sl@0
   731
  do_test capi2-12.7 {
sl@0
   732
    check_origins {SELECT * FROM view1}
sl@0
   733
  } [list {main tab1 col1} {main tab1 col2}]
sl@0
   734
  do_test capi2-12.8 {
sl@0
   735
    check_origins {select * from (select * from view1)}
sl@0
   736
  } [list {main tab1 col1} {main tab1 col2}]
sl@0
   737
  do_test capi2-12.9 {
sl@0
   738
    check_origins {select * from (select * from (select * from view1))}
sl@0
   739
  } [list {main tab1 col1} {main tab1 col2}]
sl@0
   740
  do_test capi2-12.10 {
sl@0
   741
    db close
sl@0
   742
    sqlite3 db test.db
sl@0
   743
    set ::DB [sqlite3_connection_pointer db]
sl@0
   744
    check_origins {select * from (select * from (select * from view1))}
sl@0
   745
  } [list {main tab1 col1} {main tab1 col2}]
sl@0
   746
  
sl@0
   747
  # This view will thwart the flattening optimization.
sl@0
   748
  do_test capi2-13.1 {
sl@0
   749
    execsql {
sl@0
   750
      CREATE VIEW view2 AS SELECT * FROM tab1 limit 10 offset 10;
sl@0
   751
    }
sl@0
   752
  } {}
sl@0
   753
  do_test capi2-13.2 {
sl@0
   754
    check_origins {SELECT col2, col1 FROM view2}
sl@0
   755
  } [list {main tab1 col2} {main tab1 col1}]
sl@0
   756
  do_test capi2-13.3 {
sl@0
   757
    check_origins {SELECT col2 AS hello, col1 AS world FROM view2}
sl@0
   758
  } [list {main tab1 col2} {main tab1 col1}]
sl@0
   759
  do_test capi2-13.4 {
sl@0
   760
    check_origins {SELECT b, a FROM (SELECT col1 AS a, col2 AS b FROM view2)}
sl@0
   761
  } [list {main tab1 col2} {main tab1 col1}]
sl@0
   762
  do_test capi2-13.5 {
sl@0
   763
    check_origins {SELECT (SELECT col2 FROM view2), (SELECT col1 FROM view2)}
sl@0
   764
  } [list {main tab1 col2} {main tab1 col1}]
sl@0
   765
  do_test capi2-13.6 {
sl@0
   766
    check_origins {SELECT (SELECT col2), (SELECT col1) FROM view2}
sl@0
   767
  } [list {main tab1 col2} {main tab1 col1}]
sl@0
   768
  do_test capi2-13.7 {
sl@0
   769
    check_origins {SELECT * FROM view2}
sl@0
   770
  } [list {main tab1 col1} {main tab1 col2}]
sl@0
   771
  do_test capi2-13.8 {
sl@0
   772
    check_origins {select * from (select * from view2)}
sl@0
   773
  } [list {main tab1 col1} {main tab1 col2}]
sl@0
   774
  do_test capi2-13.9 {
sl@0
   775
    check_origins {select * from (select * from (select * from view2))}
sl@0
   776
  } [list {main tab1 col1} {main tab1 col2}]
sl@0
   777
  do_test capi2-13.10 {
sl@0
   778
    db close
sl@0
   779
    sqlite3 db test.db
sl@0
   780
    set ::DB [sqlite3_connection_pointer db]
sl@0
   781
    check_origins {select * from (select * from (select * from view2))}
sl@0
   782
  } [list {main tab1 col1} {main tab1 col2}]
sl@0
   783
  do_test capi2-13.11 {
sl@0
   784
    check_origins {select * from (select * from tab1 limit 10 offset 10)}
sl@0
   785
  } [list {main tab1 col1} {main tab1 col2}]
sl@0
   786
}
sl@0
   787
sl@0
   788
sl@0
   789
} ;# ifcapable columnmetadata
sl@0
   790
sl@0
   791
db2 close
sl@0
   792
finish_test