2 #pragma ident "%Z%%M% %I% %E% SMI"
6 # The author disclaims copyright to this source code. In place of
7 # a legal notice, here is a blessing:
9 # May you do good and not evil.
10 # May you find forgiveness for yourself and forgive others.
11 # May you share freely, never taking more than you give.
13 #***********************************************************************
14 # This file implements regression tests for SQLite library. The
15 # focus of this script testing the callback-free C/C++ API.
17 # $Id: capi2.test,v 1.10 2003/08/05 13:13:38 drh Exp $
20 set testdir [file dirname $argv0]
21 source $testdir/tester.tcl
23 # Check basic functionality
27 set DB [sqlite db test.db]
28 execsql {CREATE TABLE t1(a,b,c)}
29 set VM [sqlite_compile $DB {SELECT name, rowid FROM sqlite_master} TAIL]
33 sqlite_step $VM N VALUES COLNAMES
43 } {name rowid text INTEGER}
48 sqlite_step $VM N VALUES COLNAMES
51 list $N $VALUES $COLNAMES
52 } {2 {} {name rowid text INTEGER}}
57 sqlite_step $VM N VALUES COLNAMES
60 list $N $VALUES $COLNAMES
66 # Check to make sure that the "tail" of a multi-statement SQL script
67 # is returned by sqlite_compile.
71 SELECT name, rowid FROM sqlite_master;
72 SELECT name, rowid FROM sqlite_temp_master;
73 -- A comment at the end
75 set VM [sqlite_compile $DB $SQL SQL]
78 SELECT name, rowid FROM sqlite_temp_master;
79 -- A comment at the end
82 set r [sqlite_step $VM n val colname]
83 lappend r $n $val $colname
84 } {SQLITE_ROW 2 {t1 1} {name rowid text INTEGER}}
86 set r [sqlite_step $VM n val colname]
87 lappend r $n $val $colname
88 } {SQLITE_DONE 2 {} {name rowid text INTEGER}}
93 set VM [sqlite_compile $DB $SQL SQL]
96 -- A comment at the end
99 set r [sqlite_step $VM n val colname]
100 lappend r $n $val $colname
101 } {SQLITE_DONE 2 {} {name rowid text INTEGER}}
106 set VM [sqlite_compile $DB $SQL SQL]
110 # Check the error handling.
114 sqlite_compile $DB {select bogus from sqlite_master} TAIL
116 lappend rc $msg $TAIL
117 } {1 {(1) no such column: bogus} {}}
120 sqlite_compile $DB {select bogus from } TAIL
122 lappend rc $msg $TAIL
123 } {1 {(1) near " ": syntax error} {}}
126 sqlite_compile $DB {;;;;select bogus from sqlite_master} TAIL
128 lappend rc $msg $TAIL
129 } {1 {(1) no such column: bogus} {}}
132 sqlite_compile $DB {select bogus from sqlite_master;x;} TAIL
134 lappend rc $msg $TAIL
135 } {1 {(1) no such column: bogus} {x;}}
138 sqlite_compile $DB {select bogus from sqlite_master;;;x;} TAIL
140 lappend rc $msg $TAIL
141 } {1 {(1) no such column: bogus} {;;x;}}
144 sqlite_compile $DB {select 5/0} TAIL
152 list [sqlite_step $VM N VALUE COLNAME] [set N] [set VALUE] [set COLNAME]
153 } {SQLITE_ROW 1 {{}} {5/0 NUMERIC}}
158 execsql {CREATE UNIQUE INDEX i1 ON t1(a)}
159 set VM [sqlite_compile $DB {INSERT INTO t1 VALUES(1,2,3)} TAIL]
162 do_test capi2-3.9b {db changes} {0}
167 list [sqlite_step $VM N VALUE COLNAME] [set N] [set VALUE] [set COLNAME]
168 } {SQLITE_DONE 0 {} {}}
169 do_test capi2-3.10b {db changes} {1}
173 do_test capi2-3.11b {db changes} {1}
175 list [catch {sqlite_finalize $VM} msg] [set msg]
176 } {1 {(21) library routine called out of sequence}}
178 set VM [sqlite_compile $DB {INSERT INTO t1 VALUES(1,3,4)} TAIL]
179 list [sqlite_step $VM N VALUE COLNAME] [set N] [set VALUE] [set COLNAME]
180 } {SQLITE_ERROR 0 {} {}}
181 do_test capi2-3.13b {db changes} {0}
183 list [catch {sqlite_finalize $VM} msg] [set msg]
184 } {1 {(19) column a is not unique}}
186 set VM [sqlite_compile $DB {CREATE TABLE t2(a NOT NULL, b)} TAIL]
190 list [sqlite_step $VM N VALUE COLNAME] [set N] [set VALUE] [set COLNAME]
191 } {SQLITE_DONE 0 {} {}}
193 list [catch {sqlite_finalize $VM} msg] [set msg]
196 set VM [sqlite_compile $DB {INSERT INTO t2 VALUES(NULL,2)} TAIL]
197 list [sqlite_step $VM N VALUE COLNAME] [set N] [set VALUE] [set COLNAME]
198 } {SQLITE_ERROR 0 {} {}}
200 list [catch {sqlite_finalize $VM} msg] [set msg]
201 } {1 {(19) t2.a may not be NULL}}
203 # Two or more virtual machines exists at the same time.
206 set VM1 [sqlite_compile $DB {INSERT INTO t2 VALUES(1,2)} TAIL]
210 set VM2 [sqlite_compile $DB {INSERT INTO t2 VALUES(2,3)} TAIL]
214 set VM3 [sqlite_compile $DB {INSERT INTO t2 VALUES(3,4)} TAIL]
218 list [sqlite_step $VM2 N VALUE COLNAME] [set N] [set VALUE] [set COLNAME]
219 } {SQLITE_DONE 0 {} {}}
221 execsql {SELECT * FROM t2 ORDER BY a}
224 list [catch {sqlite_finalize $VM2} msg] [set msg]
227 list [sqlite_step $VM3 N VALUE COLNAME] [set N] [set VALUE] [set COLNAME]
228 } {SQLITE_DONE 0 {} {}}
230 execsql {SELECT * FROM t2 ORDER BY a}
233 list [catch {sqlite_finalize $VM3} msg] [set msg]
236 list [sqlite_step $VM1 N VALUE COLNAME] [set N] [set VALUE] [set COLNAME]
237 } {SQLITE_DONE 0 {} {}}
239 execsql {SELECT * FROM t2 ORDER BY a}
242 list [catch {sqlite_finalize $VM1} msg] [set msg]
245 # Interleaved SELECTs
248 set VM1 [sqlite_compile $DB {SELECT * FROM t2} TAIL]
249 set VM2 [sqlite_compile $DB {SELECT * FROM t2} TAIL]
250 set VM3 [sqlite_compile $DB {SELECT * FROM t2} TAIL]
251 list [sqlite_step $VM1 N VALUE COLNAME] [set N] [set VALUE] [set COLNAME]
252 } {SQLITE_ROW 2 {2 3} {a b {} {}}}
254 list [sqlite_step $VM2 N VALUE COLNAME] [set N] [set VALUE] [set COLNAME]
255 } {SQLITE_ROW 2 {2 3} {a b {} {}}}
257 list [sqlite_step $VM1 N VALUE COLNAME] [set N] [set VALUE] [set COLNAME]
258 } {SQLITE_ROW 2 {3 4} {a b {} {}}}
260 list [sqlite_step $VM3 N VALUE COLNAME] [set N] [set VALUE] [set COLNAME]
261 } {SQLITE_ROW 2 {2 3} {a b {} {}}}
263 list [sqlite_step $VM3 N VALUE COLNAME] [set N] [set VALUE] [set COLNAME]
264 } {SQLITE_ROW 2 {3 4} {a b {} {}}}
266 list [sqlite_step $VM3 N VALUE COLNAME] [set N] [set VALUE] [set COLNAME]
267 } {SQLITE_ROW 2 {1 2} {a b {} {}}}
269 list [sqlite_step $VM3 N VALUE COLNAME] [set N] [set VALUE] [set COLNAME]
270 } {SQLITE_DONE 2 {} {a b {} {}}}
272 list [catch {sqlite_finalize $VM3} msg] [set msg]
275 list [sqlite_step $VM1 N VALUE COLNAME] [set N] [set VALUE] [set COLNAME]
276 } {SQLITE_ROW 2 {1 2} {a b {} {}}}
278 list [catch {sqlite_finalize $VM1} msg] [set msg]
281 list [sqlite_step $VM2 N VALUE COLNAME] [set N] [set VALUE] [set COLNAME]
282 } {SQLITE_ROW 2 {3 4} {a b {} {}}}
284 list [sqlite_step $VM2 N VALUE COLNAME] [set N] [set VALUE] [set COLNAME]
285 } {SQLITE_ROW 2 {1 2} {a b {} {}}}
287 list [catch {sqlite_finalize $VM2} msg] [set msg]
290 # Check for proper SQLITE_BUSY returns.
295 CREATE TABLE t3(x counter);
296 INSERT INTO t3 VALUES(1);
297 INSERT INTO t3 VALUES(2);
298 INSERT INTO t3 SELECT x+2 FROM t3;
299 INSERT INTO t3 SELECT x+4 FROM t3;
300 INSERT INTO t3 SELECT x+8 FROM t3;
303 set VM1 [sqlite_compile $DB {SELECT * FROM t3} TAIL]
308 list [sqlite_step $VM1 N VALUE COLNAME] [set N] [set VALUE] [set COLNAME]
309 } {SQLITE_BUSY 0 {} {}}
314 list [sqlite_step $VM1 N VALUE COLNAME] [set N] [set VALUE] [set COLNAME]
315 } {SQLITE_ROW 1 1 {x counter}}
318 } {1 {database is locked}}
320 list [sqlite_step $VM1 N VALUE COLNAME] [set N] [set VALUE] [set COLNAME]
321 } {SQLITE_ROW 1 2 {x counter}}
323 execsql {SELECT * FROM t2} db2
326 list [sqlite_step $VM1 N VALUE COLNAME] [set N] [set VALUE] [set COLNAME]
327 } {SQLITE_ROW 1 3 {x counter}}
329 execsql {SELECT * FROM t2}
332 list [sqlite_step $VM1 N VALUE COLNAME] [set N] [set VALUE] [set COLNAME]
333 } {SQLITE_ROW 1 4 {x counter}}
338 list [sqlite_step $VM1 N VALUE COLNAME] [set N] [set VALUE] [set COLNAME]
339 } {SQLITE_ROW 1 5 {x counter}}
340 # execsql {pragma vdbe_trace=on}
342 catchsql {UPDATE t3 SET x=x+1}
343 } {1 {database table is locked}}
345 list [sqlite_step $VM1 N VALUE COLNAME] [set N] [set VALUE] [set COLNAME]
346 } {SQLITE_ROW 1 6 {x counter}}
347 # puts [list [catch {sqlite_finalize $VM1} msg] [set msg]]; exit
349 execsql {SELECT * FROM t1}
352 list [sqlite_step $VM1 N VALUE COLNAME] [set N] [set VALUE] [set COLNAME]
353 } {SQLITE_ROW 1 7 {x counter}}
355 catchsql {UPDATE t1 SET b=b+1}
358 list [sqlite_step $VM1 N VALUE COLNAME] [set N] [set VALUE] [set COLNAME]
359 } {SQLITE_ROW 1 8 {x counter}}
361 execsql {SELECT * FROM t1}
364 list [sqlite_step $VM1 N VALUE COLNAME] [set N] [set VALUE] [set COLNAME]
365 } {SQLITE_ROW 1 9 {x counter}}
367 execsql {ROLLBACK; SELECT * FROM t1}
370 list [sqlite_step $VM1 N VALUE COLNAME] [set N] [set VALUE] [set COLNAME]
371 } {SQLITE_ROW 1 10 {x counter}}
373 execsql {BEGIN TRANSACTION ON CONFLICT ROLLBACK;}
376 list [sqlite_step $VM1 N VALUE COLNAME] [set N] [set VALUE] [set COLNAME]
377 } {SQLITE_ROW 1 11 {x counter}}
380 INSERT INTO t1 VALUES(2,3,4);
385 list [sqlite_step $VM1 N VALUE COLNAME] [set N] [set VALUE] [set COLNAME]
386 } {SQLITE_ROW 1 12 {x counter}}
389 INSERT INTO t1 VALUES(2,4,5);
392 } {1 {column a is not unique}}
394 list [sqlite_step $VM1 N VALUE COLNAME] [set N] [set VALUE] [set COLNAME]
395 } {SQLITE_ROW 1 13 {x counter}}
397 list [catch {sqlite_finalize $VM1} msg] [set msg]
408 PRAGMA count_changes=on
413 UPDATE t1 SET a=a+10;
418 INSERT INTO t1 SELECT a+1,b+1,c+1 FROM t1;
421 do_test capi2-7.4b {db changes} {1}
424 UPDATE t1 SET a=a+10;
427 do_test capi2-7.5b {db changes} {2}
435 INSERT INTO t1 SELECT a+2,b+2,c+2 FROM t1;
445 } {0 21 2 3 22 3 4 23 4 5 24 5 6}
448 UPDATE t1 SET a=a-20;
451 } {0 4 1 2 3 2 3 4 3 4 5 4 5 6}
456 set x [stepsql $DB {EXPLAIN SELECT * FROM t1}]
460 # Ticket #261 - make sure we can finalize before the end of a query.
463 set VM1 [sqlite_compile $DB {SELECT * FROM t2} TAIL]
467 # Tickets #384 and #385 - make sure the TAIL argument to sqlite_compile
468 # and all of the return pointers in sqlite_step can be null.
471 set VM1 [sqlite_compile $DB {SELECT * FROM t2}]