3 # The author disclaims copyright to this source code. In place of
4 # a legal notice, here is a blessing:
6 # May you do good and not evil.
7 # May you find forgiveness for yourself and forgive others.
8 # May you share freely, never taking more than you give.
10 #***********************************************************************
11 # This file implements regression tests for SQLite library. The
12 # focus of this script testing the callback-free C/C++ API.
14 # $Id: capi2.test,v 1.37 2008/12/30 17:55:00 drh Exp $
17 set testdir [file dirname $argv0]
18 source $testdir/tester.tcl
20 # Return the text values from the current row pointed at by STMT as a list.
21 proc get_row_values {STMT} {
23 for {set i 0} {$i < [sqlite3_data_count $STMT]} {incr i} {
24 lappend VALUES [sqlite3_column_text $STMT $i]
29 # Return the column names followed by declaration types for the result set
30 # of the SQL statement STMT.
33 # CREATE TABLE abc(a text, b integer);
36 # The result is {a b text integer}
37 proc get_column_names {STMT} {
39 for {set i 0} {$i < [sqlite3_column_count $STMT]} {incr i} {
40 lappend VALUES [sqlite3_column_name $STMT $i]
42 for {set i 0} {$i < [sqlite3_column_count $STMT]} {incr i} {
43 lappend VALUES [sqlite3_column_decltype $STMT $i]
48 # Check basic functionality
51 set DB [sqlite3_connection_pointer db]
52 execsql {CREATE TABLE t1(a,b,c)}
53 set VM [sqlite3_prepare $DB {SELECT name, rowid FROM sqlite_master} -1 TAIL]
60 sqlite3_data_count $VM
67 } {name rowid text INTEGER}
72 list [sqlite3_column_count $VM] [get_row_values $VM] [get_column_names $VM]
73 } {2 {} {name rowid text INTEGER}}
75 # This used to be SQLITE_MISUSE. But now we automatically reset prepared
87 # Update: In v2, once SQLITE_MISUSE is returned the statement handle cannot
88 # be interrogated for more information. However in v3, since the column
89 # count, names and types are determined at compile time, these are still
90 # accessible after an SQLITE_MISUSE error.
93 list [sqlite3_column_count $VM] [get_row_values $VM] [get_column_names $VM]
94 } {2 {} {name rowid text INTEGER}}
96 sqlite3_data_count $VM
103 # Check to make sure that the "tail" of a multi-statement SQL script
104 # is returned by sqlite3_prepare.
108 SELECT name, rowid FROM sqlite_master;
109 SELECT name, rowid FROM sqlite_master WHERE 0;
110 -- A comment at the end
112 set VM [sqlite3_prepare $DB $SQL -1 SQL]
115 SELECT name, rowid FROM sqlite_master WHERE 0;
116 -- A comment at the end
119 set r [sqlite3_step $VM]
120 lappend r [sqlite3_column_count $VM] \
121 [get_row_values $VM] \
122 [get_column_names $VM]
123 } {SQLITE_ROW 2 {t1 1} {name rowid text INTEGER}}
125 set r [sqlite3_step $VM]
126 lappend r [sqlite3_column_count $VM] \
127 [get_row_values $VM] \
128 [get_column_names $VM]
129 } {SQLITE_DONE 2 {} {name rowid text INTEGER}}
134 set VM [sqlite3_prepare $DB $SQL -1 SQL]
137 -- A comment at the end
140 set r [sqlite3_step $VM]
141 lappend r [sqlite3_column_count $VM] \
142 [get_row_values $VM] \
143 [get_column_names $VM]
144 } {SQLITE_DONE 2 {} {name rowid text INTEGER}}
149 set VM [sqlite3_prepare $DB $SQL -1 SQL]
153 # Check the error handling.
157 sqlite3_prepare $DB {select bogus from sqlite_master} -1 TAIL
159 lappend rc $msg $TAIL
160 } {1 {(1) no such column: bogus} {}}
163 sqlite3_prepare $DB {select bogus from } -1 TAIL
165 lappend rc $msg $TAIL
166 } {1 {(1) near " ": syntax error} {}}
169 sqlite3_prepare $DB {;;;;select bogus from sqlite_master} -1 TAIL
171 lappend rc $msg $TAIL
172 } {1 {(1) no such column: bogus} {}}
175 sqlite3_prepare $DB {select bogus from sqlite_master;x;} -1 TAIL
177 lappend rc $msg $TAIL
178 } {1 {(1) no such column: bogus} {x;}}
181 sqlite3_prepare $DB {select bogus from sqlite_master;;;x;} -1 TAIL
183 lappend rc $msg $TAIL
184 } {1 {(1) no such column: bogus} {;;x;}}
187 sqlite3_prepare $DB {select 5/0} -1 TAIL
192 list [sqlite3_step $VM] \
193 [sqlite3_column_count $VM] \
194 [get_row_values $VM] \
195 [get_column_names $VM]
196 } {SQLITE_ROW 1 {{}} {5/0 {}}}
201 execsql {CREATE UNIQUE INDEX i1 ON t1(a)}
202 set VM [sqlite3_prepare $DB {INSERT INTO t1 VALUES(1,2,3)} -1 TAIL]
205 do_test capi2-3.9b {db changes} {0}
207 list [sqlite3_step $VM] \
208 [sqlite3_column_count $VM] \
209 [get_row_values $VM] \
210 [get_column_names $VM]
211 } {SQLITE_DONE 0 {} {}}
213 # Update for v3 - the change has not actually happened until the query is
214 # finalized. Is this going to cause trouble for anyone? Lee Nelson maybe?
215 # (Later:) The change now happens just before SQLITE_DONE is returned.
216 do_test capi2-3.10b {db changes} {1}
220 do_test capi2-3.11b {db changes} {1}
221 #do_test capi2-3.12-misuse {
222 # sqlite3_finalize $VM
225 set VM [sqlite3_prepare $DB {INSERT INTO t1 VALUES(1,3,4)} -1 TAIL]
226 list [sqlite3_step $VM] \
227 [sqlite3_column_count $VM] \
228 [get_row_values $VM] \
229 [get_column_names $VM]
230 } {SQLITE_ERROR 0 {} {}}
232 # Update for v3: Preparing a statement does not affect the change counter.
233 # (Test result changes from 0 to 1). (Later:) change counter updates occur
234 # when sqlite3_step returns, not at finalize time.
235 do_test capi2-3.13b {db changes} {0}
238 list [sqlite3_finalize $VM] [sqlite3_errmsg $DB] \
239 [sqlite3_extended_errcode $DB]
240 } {SQLITE_CONSTRAINT {UNIQUE constraint failed: t1.a} SQLITE_CONSTRAINT_UNIQUE}
242 set VM [sqlite3_prepare $DB {CREATE TABLE t2(a NOT NULL, b)} -1 TAIL]
246 list [sqlite3_step $VM] \
247 [sqlite3_column_count $VM] \
248 [get_row_values $VM] \
249 [get_column_names $VM]
250 } {SQLITE_DONE 0 {} {}}
252 list [sqlite3_finalize $VM] [sqlite3_errmsg $DB]
253 } {SQLITE_OK {not an error}}
255 set VM [sqlite3_prepare $DB {INSERT INTO t2 VALUES(NULL,2)} -1 TAIL]
256 list [sqlite3_step $VM] \
257 [sqlite3_column_count $VM] \
258 [get_row_values $VM] \
259 [get_column_names $VM]
260 } {SQLITE_ERROR 0 {} {}}
262 list [sqlite3_finalize $VM] [sqlite3_errmsg $DB] \
263 [sqlite3_extended_errcode $DB]
264 } {SQLITE_CONSTRAINT {NOT NULL constraint failed: t2.a} SQLITE_CONSTRAINT_NOTNULL}
268 CREATE TABLE a1(message_id, name , UNIQUE(message_id, name) );
269 INSERT INTO a1 VALUES(1, 1);
273 set VM [sqlite3_prepare $DB {INSERT INTO a1 VALUES(1, 1)} -1 TAIL]
281 } {SQLITE_CONSTRAINT}
283 list [sqlite3_errcode $DB] [sqlite3_extended_errcode $DB]
284 } {SQLITE_CONSTRAINT SQLITE_CONSTRAINT_UNIQUE}
286 # Two or more virtual machines exists at the same time.
289 set VM1 [sqlite3_prepare $DB {INSERT INTO t2 VALUES(1,2)} -1 TAIL]
293 set VM2 [sqlite3_prepare $DB {INSERT INTO t2 VALUES(2,3)} -1 TAIL]
297 set VM3 [sqlite3_prepare $DB {INSERT INTO t2 VALUES(3,4)} -1 TAIL]
301 list [sqlite3_step $VM2] \
302 [sqlite3_column_count $VM2] \
303 [get_row_values $VM2] \
304 [get_column_names $VM2]
305 } {SQLITE_DONE 0 {} {}}
307 execsql {SELECT * FROM t2 ORDER BY a}
310 sqlite3_finalize $VM2
313 list [sqlite3_step $VM3] \
314 [sqlite3_column_count $VM3] \
315 [get_row_values $VM3] \
316 [get_column_names $VM3]
317 } {SQLITE_DONE 0 {} {}}
319 execsql {SELECT * FROM t2 ORDER BY a}
322 sqlite3_finalize $VM3
325 list [sqlite3_step $VM1] \
326 [sqlite3_column_count $VM1] \
327 [get_row_values $VM1] \
328 [get_column_names $VM1]
329 } {SQLITE_DONE 0 {} {}}
331 execsql {SELECT * FROM t2 ORDER BY a}
334 sqlite3_finalize $VM1
337 # Interleaved SELECTs
340 set VM1 [sqlite3_prepare $DB {SELECT * FROM t2} -1 TAIL]
341 set VM2 [sqlite3_prepare $DB {SELECT * FROM t2} -1 TAIL]
342 set VM3 [sqlite3_prepare $DB {SELECT * FROM t2} -1 TAIL]
343 list [sqlite3_step $VM1] \
344 [sqlite3_column_count $VM1] \
345 [get_row_values $VM1] \
346 [get_column_names $VM1]
347 } {SQLITE_ROW 2 {2 3} {a b {} {}}}
349 list [sqlite3_step $VM2] \
350 [sqlite3_column_count $VM2] \
351 [get_row_values $VM2] \
352 [get_column_names $VM2]
353 } {SQLITE_ROW 2 {2 3} {a b {} {}}}
355 list [sqlite3_step $VM1] \
356 [sqlite3_column_count $VM1] \
357 [get_row_values $VM1] \
358 [get_column_names $VM1]
359 } {SQLITE_ROW 2 {3 4} {a b {} {}}}
361 list [sqlite3_step $VM3] \
362 [sqlite3_column_count $VM3] \
363 [get_row_values $VM3] \
364 [get_column_names $VM3]
365 } {SQLITE_ROW 2 {2 3} {a b {} {}}}
367 list [sqlite3_step $VM3] \
368 [sqlite3_column_count $VM3] \
369 [get_row_values $VM3] \
370 [get_column_names $VM3]
371 } {SQLITE_ROW 2 {3 4} {a b {} {}}}
373 list [sqlite3_step $VM3] \
374 [sqlite3_column_count $VM3] \
375 [get_row_values $VM3] \
376 [get_column_names $VM3]
377 } {SQLITE_ROW 2 {1 2} {a b {} {}}}
379 list [sqlite3_step $VM3] \
380 [sqlite3_column_count $VM3] \
381 [get_row_values $VM3] \
382 [get_column_names $VM3]
383 } {SQLITE_DONE 2 {} {a b {} {}}}
385 sqlite3_finalize $VM3
388 list [sqlite3_step $VM1] \
389 [sqlite3_column_count $VM1] \
390 [get_row_values $VM1] \
391 [get_column_names $VM1]
392 } {SQLITE_ROW 2 {1 2} {a b {} {}}}
394 sqlite3_finalize $VM1
397 list [sqlite3_step $VM2] \
398 [sqlite3_column_count $VM2] \
399 [get_row_values $VM2] \
400 [get_column_names $VM2]
401 } {SQLITE_ROW 2 {3 4} {a b {} {}}}
403 list [sqlite3_step $VM2] \
404 [sqlite3_column_count $VM2] \
405 [get_row_values $VM2] \
406 [get_column_names $VM2]
407 } {SQLITE_ROW 2 {1 2} {a b {} {}}}
409 sqlite3_finalize $VM2
412 # Check for proper SQLITE_BUSY returns.
417 CREATE TABLE t3(x counter);
418 INSERT INTO t3 VALUES(1);
419 INSERT INTO t3 VALUES(2);
420 INSERT INTO t3 SELECT x+2 FROM t3;
421 INSERT INTO t3 SELECT x+4 FROM t3;
422 INSERT INTO t3 SELECT x+8 FROM t3;
425 set VM1 [sqlite3_prepare $DB {SELECT * FROM t3} -1 TAIL]
429 # Update for v3: BEGIN doesn't write-lock the database. It is quite
430 # difficult to get v3 to write-lock the database, which causes a few
431 # problems for test scripts.
433 # do_test capi2-6.2 {
434 # list [sqlite3_step $VM1] \
435 # [sqlite3_column_count $VM1] \
436 # [get_row_values $VM1] \
437 # [get_column_names $VM1]
438 # } {SQLITE_BUSY 0 {} {}}
443 list [sqlite3_step $VM1] \
444 [sqlite3_column_count $VM1] \
445 [get_row_values $VM1] \
446 [get_column_names $VM1]
447 } {SQLITE_ROW 1 1 {x counter}}
449 catchsql {INSERT INTO t3 VALUES(10);} db2
450 } {1 {database is locked}}
452 list [sqlite3_step $VM1] \
453 [sqlite3_column_count $VM1] \
454 [get_row_values $VM1] \
455 [get_column_names $VM1]
456 } {SQLITE_ROW 1 2 {x counter}}
458 execsql {SELECT * FROM t2} db2
461 list [sqlite3_step $VM1] \
462 [sqlite3_column_count $VM1] \
463 [get_row_values $VM1] \
464 [get_column_names $VM1]
465 } {SQLITE_ROW 1 3 {x counter}}
467 execsql {SELECT * FROM t2}
470 list [sqlite3_step $VM1] \
471 [sqlite3_column_count $VM1] \
472 [get_row_values $VM1] \
473 [get_column_names $VM1]
474 } {SQLITE_ROW 1 4 {x counter}}
479 list [sqlite3_step $VM1] \
480 [sqlite3_column_count $VM1] \
481 [get_row_values $VM1] \
482 [get_column_names $VM1]
483 } {SQLITE_ROW 1 5 {x counter}}
485 # A read no longer blocks a write in the same connection.
486 #do_test capi2-6.13 {
487 # catchsql {UPDATE t3 SET x=x+1}
488 #} {1 {database table is locked}}
491 list [sqlite3_step $VM1] \
492 [sqlite3_column_count $VM1] \
493 [get_row_values $VM1] \
494 [get_column_names $VM1]
495 } {SQLITE_ROW 1 6 {x counter}}
497 execsql {SELECT * FROM t1}
500 list [sqlite3_step $VM1] \
501 [sqlite3_column_count $VM1] \
502 [get_row_values $VM1] \
503 [get_column_names $VM1]
504 } {SQLITE_ROW 1 7 {x counter}}
506 catchsql {UPDATE t1 SET b=b+1}
509 list [sqlite3_step $VM1] \
510 [sqlite3_column_count $VM1] \
511 [get_row_values $VM1] \
512 [get_column_names $VM1]
513 } {SQLITE_ROW 1 8 {x counter}}
515 execsql {SELECT * FROM t1}
518 list [sqlite3_step $VM1] \
519 [sqlite3_column_count $VM1] \
520 [get_row_values $VM1] \
521 [get_column_names $VM1]
522 } {SQLITE_ROW 1 9 {x counter}}
523 #do_test capi2-6.21 {
524 # execsql {ROLLBACK; SELECT * FROM t1}
527 list [sqlite3_step $VM1] \
528 [sqlite3_column_count $VM1] \
529 [get_row_values $VM1] \
530 [get_column_names $VM1]
531 } {SQLITE_ROW 1 10 {x counter}}
532 #do_test capi2-6.23 {
533 # execsql {BEGIN TRANSACTION;}
536 list [sqlite3_step $VM1] \
537 [sqlite3_column_count $VM1] \
538 [get_row_values $VM1] \
539 [get_column_names $VM1]
540 } {SQLITE_ROW 1 11 {x counter}}
543 INSERT INTO t1 VALUES(2,3,4);
548 list [sqlite3_step $VM1] \
549 [sqlite3_column_count $VM1] \
550 [get_row_values $VM1] \
551 [get_column_names $VM1]
552 } {SQLITE_ROW 1 12 {x counter}}
555 INSERT INTO t1 VALUES(2,4,5);
558 } {1 {UNIQUE constraint failed: t1.a}}
560 list [sqlite3_step $VM1] \
561 [sqlite3_column_count $VM1] \
562 [get_row_values $VM1] \
563 [get_column_names $VM1]
564 } {SQLITE_ROW 1 13 {x counter}}
566 sqlite3_finalize $VM1
577 PRAGMA count_changes=on
582 UPDATE t1 SET a=a+10;
587 INSERT INTO t1 SELECT a+1,b+1,c+1 FROM t1;
590 do_test capi2-7.4b {sqlite3_changes $DB} {1}
593 UPDATE t1 SET a=a+10;
596 do_test capi2-7.5b {sqlite3_changes $DB} {2}
604 INSERT INTO t1 SELECT a+2,b+2,c+2 FROM t1;
614 } {0 21 2 3 22 3 4 23 4 5 24 5 6}
617 UPDATE t1 SET a=a-20;
620 } {0 4 1 2 3 2 3 4 3 4 5 4 5 6}
622 # Update for version 3: A SELECT statement no longer resets the change
623 # counter (Test result changes from 0 to 4).
627 do_test capi2-7.11a {
628 execsql {SELECT count(*) FROM t1}
631 ifcapable {explain} {
633 set x [stepsql $DB {EXPLAIN SELECT * FROM t1}]
638 # Ticket #261 - make sure we can finalize before the end of a query.
641 set VM1 [sqlite3_prepare $DB {SELECT * FROM t2} -1 TAIL]
642 sqlite3_finalize $VM1
645 # Tickets #384 and #385 - make sure the TAIL argument to sqlite3_prepare
646 # and all of the return pointers in sqlite_step can be null.
649 set VM1 [sqlite3_prepare $DB {SELECT * FROM t2} -1 DUMMY]
651 sqlite3_finalize $VM1
654 # Test that passing a NULL pointer to sqlite3_finalize() or sqlite3_reset
655 # does not cause an error.
663 #---------------------------------------------------------------------------
664 # The following tests - capi2-11.* - test the "column origin" APIs.
666 # sqlite3_column_origin_name()
667 # sqlite3_column_database_name()
668 # sqlite3_column_table_name()
671 ifcapable columnmetadata {
673 # This proc uses the database handle $::DB to compile the SQL statement passed
674 # as a parameter. The return value of this procedure is a list with one
675 # element for each column returned by the compiled statement. Each element of
676 # this list is itself a list of length three, consisting of the origin
677 # database, table and column for the corresponding returned column.
678 proc check_origins {sql} {
680 set ::STMT [sqlite3_prepare $::DB $sql -1 dummy]
681 for {set i 0} {$i < [sqlite3_column_count $::STMT]} {incr i} {
683 [sqlite3_column_database_name $::STMT $i] \
684 [sqlite3_column_table_name $::STMT $i] \
685 [sqlite3_column_origin_name $::STMT $i] \
688 sqlite3_finalize $::STMT
693 CREATE TABLE tab1(col1, col2);
697 check_origins {SELECT col2, col1 FROM tab1}
698 } [list {main tab1 col2} {main tab1 col1}]
700 check_origins {SELECT col2 AS hello, col1 AS world FROM tab1}
701 } [list {main tab1 col2} {main tab1 col1}]
705 check_origins {SELECT b, a FROM (SELECT col1 AS a, col2 AS b FROM tab1)}
706 } [list {main tab1 col2} {main tab1 col1}]
708 check_origins {SELECT (SELECT col2 FROM tab1), (SELECT col1 FROM tab1)}
709 } [list {main tab1 col2} {main tab1 col1}]
711 check_origins {SELECT (SELECT col2), (SELECT col1) FROM tab1}
712 } [list {main tab1 col2} {main tab1 col1}]
714 check_origins {SELECT * FROM tab1}
715 } [list {main tab1 col1} {main tab1 col2}]
717 check_origins {SELECT * FROM (SELECT * FROM tab1)}
718 } [list {main tab1 col1} {main tab1 col2}]
721 ifcapable view&&subquery {
724 CREATE VIEW view1 AS SELECT * FROM tab1;
728 check_origins {SELECT col2, col1 FROM view1}
729 } [list {main tab1 col2} {main tab1 col1}]
731 check_origins {SELECT col2 AS hello, col1 AS world FROM view1}
732 } [list {main tab1 col2} {main tab1 col1}]
734 check_origins {SELECT b, a FROM (SELECT col1 AS a, col2 AS b FROM view1)}
735 } [list {main tab1 col2} {main tab1 col1}]
737 check_origins {SELECT (SELECT col2 FROM view1), (SELECT col1 FROM view1)}
738 } [list {main tab1 col2} {main tab1 col1}]
740 check_origins {SELECT (SELECT col2), (SELECT col1) FROM view1}
741 } [list {main tab1 col2} {main tab1 col1}]
743 check_origins {SELECT * FROM view1}
744 } [list {main tab1 col1} {main tab1 col2}]
746 check_origins {select * from (select * from view1)}
747 } [list {main tab1 col1} {main tab1 col2}]
749 check_origins {select * from (select * from (select * from view1))}
750 } [list {main tab1 col1} {main tab1 col2}]
751 do_test capi2-12.10 {
754 set ::DB [sqlite3_connection_pointer db]
755 check_origins {select * from (select * from (select * from view1))}
756 } [list {main tab1 col1} {main tab1 col2}]
758 # This view will thwart the flattening optimization.
761 CREATE VIEW view2 AS SELECT * FROM tab1 limit 10 offset 10;
765 check_origins {SELECT col2, col1 FROM view2}
766 } [list {main tab1 col2} {main tab1 col1}]
768 check_origins {SELECT col2 AS hello, col1 AS world FROM view2}
769 } [list {main tab1 col2} {main tab1 col1}]
771 check_origins {SELECT b, a FROM (SELECT col1 AS a, col2 AS b FROM view2)}
772 } [list {main tab1 col2} {main tab1 col1}]
774 check_origins {SELECT (SELECT col2 FROM view2), (SELECT col1 FROM view2)}
775 } [list {main tab1 col2} {main tab1 col1}]
777 check_origins {SELECT (SELECT col2), (SELECT col1) FROM view2}
778 } [list {main tab1 col2} {main tab1 col1}]
780 check_origins {SELECT * FROM view2}
781 } [list {main tab1 col1} {main tab1 col2}]
783 check_origins {select * from (select * from view2)}
784 } [list {main tab1 col1} {main tab1 col2}]
786 check_origins {select * from (select * from (select * from view2))}
787 } [list {main tab1 col1} {main tab1 col2}]
788 do_test capi2-13.10 {
791 set ::DB [sqlite3_connection_pointer db]
792 check_origins {select * from (select * from (select * from view2))}
793 } [list {main tab1 col1} {main tab1 col2}]
794 do_test capi2-13.11 {
795 check_origins {select * from (select * from tab1 limit 10 offset 10)}
796 } [list {main tab1 col1} {main tab1 col2}]
800 } ;# ifcapable columnmetadata