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 } {SQLITE_CONSTRAINT {column a is not unique}}
241 set VM [sqlite3_prepare $DB {CREATE TABLE t2(a NOT NULL, b)} -1 TAIL]
245 list [sqlite3_step $VM] \
246 [sqlite3_column_count $VM] \
247 [get_row_values $VM] \
248 [get_column_names $VM]
249 } {SQLITE_DONE 0 {} {}}
251 list [sqlite3_finalize $VM] [sqlite3_errmsg $DB]
252 } {SQLITE_OK {not an error}}
254 set VM [sqlite3_prepare $DB {INSERT INTO t2 VALUES(NULL,2)} -1 TAIL]
255 list [sqlite3_step $VM] \
256 [sqlite3_column_count $VM] \
257 [get_row_values $VM] \
258 [get_column_names $VM]
259 } {SQLITE_ERROR 0 {} {}}
261 list [sqlite3_finalize $VM] [sqlite3_errmsg $DB]
262 } {SQLITE_CONSTRAINT {t2.a may not be NULL}}
266 CREATE TABLE a1(message_id, name , UNIQUE(message_id, name) );
267 INSERT INTO a1 VALUES(1, 1);
271 set VM [sqlite3_prepare $DB {INSERT INTO a1 VALUES(1, 1)} -1 TAIL]
279 } {SQLITE_CONSTRAINT}
282 } {SQLITE_CONSTRAINT}
284 # Two or more virtual machines exists at the same time.
287 set VM1 [sqlite3_prepare $DB {INSERT INTO t2 VALUES(1,2)} -1 TAIL]
291 set VM2 [sqlite3_prepare $DB {INSERT INTO t2 VALUES(2,3)} -1 TAIL]
295 set VM3 [sqlite3_prepare $DB {INSERT INTO t2 VALUES(3,4)} -1 TAIL]
299 list [sqlite3_step $VM2] \
300 [sqlite3_column_count $VM2] \
301 [get_row_values $VM2] \
302 [get_column_names $VM2]
303 } {SQLITE_DONE 0 {} {}}
305 execsql {SELECT * FROM t2 ORDER BY a}
308 sqlite3_finalize $VM2
311 list [sqlite3_step $VM3] \
312 [sqlite3_column_count $VM3] \
313 [get_row_values $VM3] \
314 [get_column_names $VM3]
315 } {SQLITE_DONE 0 {} {}}
317 execsql {SELECT * FROM t2 ORDER BY a}
320 sqlite3_finalize $VM3
323 list [sqlite3_step $VM1] \
324 [sqlite3_column_count $VM1] \
325 [get_row_values $VM1] \
326 [get_column_names $VM1]
327 } {SQLITE_DONE 0 {} {}}
329 execsql {SELECT * FROM t2 ORDER BY a}
332 sqlite3_finalize $VM1
335 # Interleaved SELECTs
338 set VM1 [sqlite3_prepare $DB {SELECT * FROM t2} -1 TAIL]
339 set VM2 [sqlite3_prepare $DB {SELECT * FROM t2} -1 TAIL]
340 set VM3 [sqlite3_prepare $DB {SELECT * FROM t2} -1 TAIL]
341 list [sqlite3_step $VM1] \
342 [sqlite3_column_count $VM1] \
343 [get_row_values $VM1] \
344 [get_column_names $VM1]
345 } {SQLITE_ROW 2 {2 3} {a b {} {}}}
347 list [sqlite3_step $VM2] \
348 [sqlite3_column_count $VM2] \
349 [get_row_values $VM2] \
350 [get_column_names $VM2]
351 } {SQLITE_ROW 2 {2 3} {a b {} {}}}
353 list [sqlite3_step $VM1] \
354 [sqlite3_column_count $VM1] \
355 [get_row_values $VM1] \
356 [get_column_names $VM1]
357 } {SQLITE_ROW 2 {3 4} {a b {} {}}}
359 list [sqlite3_step $VM3] \
360 [sqlite3_column_count $VM3] \
361 [get_row_values $VM3] \
362 [get_column_names $VM3]
363 } {SQLITE_ROW 2 {2 3} {a b {} {}}}
365 list [sqlite3_step $VM3] \
366 [sqlite3_column_count $VM3] \
367 [get_row_values $VM3] \
368 [get_column_names $VM3]
369 } {SQLITE_ROW 2 {3 4} {a b {} {}}}
371 list [sqlite3_step $VM3] \
372 [sqlite3_column_count $VM3] \
373 [get_row_values $VM3] \
374 [get_column_names $VM3]
375 } {SQLITE_ROW 2 {1 2} {a b {} {}}}
377 list [sqlite3_step $VM3] \
378 [sqlite3_column_count $VM3] \
379 [get_row_values $VM3] \
380 [get_column_names $VM3]
381 } {SQLITE_DONE 2 {} {a b {} {}}}
383 sqlite3_finalize $VM3
386 list [sqlite3_step $VM1] \
387 [sqlite3_column_count $VM1] \
388 [get_row_values $VM1] \
389 [get_column_names $VM1]
390 } {SQLITE_ROW 2 {1 2} {a b {} {}}}
392 sqlite3_finalize $VM1
395 list [sqlite3_step $VM2] \
396 [sqlite3_column_count $VM2] \
397 [get_row_values $VM2] \
398 [get_column_names $VM2]
399 } {SQLITE_ROW 2 {3 4} {a b {} {}}}
401 list [sqlite3_step $VM2] \
402 [sqlite3_column_count $VM2] \
403 [get_row_values $VM2] \
404 [get_column_names $VM2]
405 } {SQLITE_ROW 2 {1 2} {a b {} {}}}
407 sqlite3_finalize $VM2
410 # Check for proper SQLITE_BUSY returns.
415 CREATE TABLE t3(x counter);
416 INSERT INTO t3 VALUES(1);
417 INSERT INTO t3 VALUES(2);
418 INSERT INTO t3 SELECT x+2 FROM t3;
419 INSERT INTO t3 SELECT x+4 FROM t3;
420 INSERT INTO t3 SELECT x+8 FROM t3;
423 set VM1 [sqlite3_prepare $DB {SELECT * FROM t3} -1 TAIL]
427 # Update for v3: BEGIN doesn't write-lock the database. It is quite
428 # difficult to get v3 to write-lock the database, which causes a few
429 # problems for test scripts.
431 # do_test capi2-6.2 {
432 # list [sqlite3_step $VM1] \
433 # [sqlite3_column_count $VM1] \
434 # [get_row_values $VM1] \
435 # [get_column_names $VM1]
436 # } {SQLITE_BUSY 0 {} {}}
441 list [sqlite3_step $VM1] \
442 [sqlite3_column_count $VM1] \
443 [get_row_values $VM1] \
444 [get_column_names $VM1]
445 } {SQLITE_ROW 1 1 {x counter}}
447 catchsql {INSERT INTO t3 VALUES(10);} db2
448 } {1 {database is locked}}
450 list [sqlite3_step $VM1] \
451 [sqlite3_column_count $VM1] \
452 [get_row_values $VM1] \
453 [get_column_names $VM1]
454 } {SQLITE_ROW 1 2 {x counter}}
456 execsql {SELECT * FROM t2} db2
459 list [sqlite3_step $VM1] \
460 [sqlite3_column_count $VM1] \
461 [get_row_values $VM1] \
462 [get_column_names $VM1]
463 } {SQLITE_ROW 1 3 {x counter}}
465 execsql {SELECT * FROM t2}
468 list [sqlite3_step $VM1] \
469 [sqlite3_column_count $VM1] \
470 [get_row_values $VM1] \
471 [get_column_names $VM1]
472 } {SQLITE_ROW 1 4 {x counter}}
477 list [sqlite3_step $VM1] \
478 [sqlite3_column_count $VM1] \
479 [get_row_values $VM1] \
480 [get_column_names $VM1]
481 } {SQLITE_ROW 1 5 {x counter}}
483 # A read no longer blocks a write in the same connection.
484 #do_test capi2-6.13 {
485 # catchsql {UPDATE t3 SET x=x+1}
486 #} {1 {database table is locked}}
489 list [sqlite3_step $VM1] \
490 [sqlite3_column_count $VM1] \
491 [get_row_values $VM1] \
492 [get_column_names $VM1]
493 } {SQLITE_ROW 1 6 {x counter}}
495 execsql {SELECT * FROM t1}
498 list [sqlite3_step $VM1] \
499 [sqlite3_column_count $VM1] \
500 [get_row_values $VM1] \
501 [get_column_names $VM1]
502 } {SQLITE_ROW 1 7 {x counter}}
504 catchsql {UPDATE t1 SET b=b+1}
507 list [sqlite3_step $VM1] \
508 [sqlite3_column_count $VM1] \
509 [get_row_values $VM1] \
510 [get_column_names $VM1]
511 } {SQLITE_ROW 1 8 {x counter}}
513 execsql {SELECT * FROM t1}
516 list [sqlite3_step $VM1] \
517 [sqlite3_column_count $VM1] \
518 [get_row_values $VM1] \
519 [get_column_names $VM1]
520 } {SQLITE_ROW 1 9 {x counter}}
521 #do_test capi2-6.21 {
522 # execsql {ROLLBACK; SELECT * FROM t1}
525 list [sqlite3_step $VM1] \
526 [sqlite3_column_count $VM1] \
527 [get_row_values $VM1] \
528 [get_column_names $VM1]
529 } {SQLITE_ROW 1 10 {x counter}}
530 #do_test capi2-6.23 {
531 # execsql {BEGIN TRANSACTION;}
534 list [sqlite3_step $VM1] \
535 [sqlite3_column_count $VM1] \
536 [get_row_values $VM1] \
537 [get_column_names $VM1]
538 } {SQLITE_ROW 1 11 {x counter}}
541 INSERT INTO t1 VALUES(2,3,4);
546 list [sqlite3_step $VM1] \
547 [sqlite3_column_count $VM1] \
548 [get_row_values $VM1] \
549 [get_column_names $VM1]
550 } {SQLITE_ROW 1 12 {x counter}}
553 INSERT INTO t1 VALUES(2,4,5);
556 } {1 {column a is not unique}}
558 list [sqlite3_step $VM1] \
559 [sqlite3_column_count $VM1] \
560 [get_row_values $VM1] \
561 [get_column_names $VM1]
562 } {SQLITE_ROW 1 13 {x counter}}
564 sqlite3_finalize $VM1
575 PRAGMA count_changes=on
580 UPDATE t1 SET a=a+10;
585 INSERT INTO t1 SELECT a+1,b+1,c+1 FROM t1;
588 do_test capi2-7.4b {sqlite3_changes $DB} {1}
591 UPDATE t1 SET a=a+10;
594 do_test capi2-7.5b {sqlite3_changes $DB} {2}
602 INSERT INTO t1 SELECT a+2,b+2,c+2 FROM t1;
612 } {0 21 2 3 22 3 4 23 4 5 24 5 6}
615 UPDATE t1 SET a=a-20;
618 } {0 4 1 2 3 2 3 4 3 4 5 4 5 6}
620 # Update for version 3: A SELECT statement no longer resets the change
621 # counter (Test result changes from 0 to 4).
625 do_test capi2-7.11a {
626 execsql {SELECT count(*) FROM t1}
629 ifcapable {explain} {
631 set x [stepsql $DB {EXPLAIN SELECT * FROM t1}]
636 # Ticket #261 - make sure we can finalize before the end of a query.
639 set VM1 [sqlite3_prepare $DB {SELECT * FROM t2} -1 TAIL]
640 sqlite3_finalize $VM1
643 # Tickets #384 and #385 - make sure the TAIL argument to sqlite3_prepare
644 # and all of the return pointers in sqlite_step can be null.
647 set VM1 [sqlite3_prepare $DB {SELECT * FROM t2} -1 DUMMY]
649 sqlite3_finalize $VM1
652 # Test that passing a NULL pointer to sqlite3_finalize() or sqlite3_reset
653 # does not cause an error.
661 #---------------------------------------------------------------------------
662 # The following tests - capi2-11.* - test the "column origin" APIs.
664 # sqlite3_column_origin_name()
665 # sqlite3_column_database_name()
666 # sqlite3_column_table_name()
669 ifcapable columnmetadata {
671 # This proc uses the database handle $::DB to compile the SQL statement passed
672 # as a parameter. The return value of this procedure is a list with one
673 # element for each column returned by the compiled statement. Each element of
674 # this list is itself a list of length three, consisting of the origin
675 # database, table and column for the corresponding returned column.
676 proc check_origins {sql} {
678 set ::STMT [sqlite3_prepare $::DB $sql -1 dummy]
679 for {set i 0} {$i < [sqlite3_column_count $::STMT]} {incr i} {
681 [sqlite3_column_database_name $::STMT $i] \
682 [sqlite3_column_table_name $::STMT $i] \
683 [sqlite3_column_origin_name $::STMT $i] \
686 sqlite3_finalize $::STMT
691 CREATE TABLE tab1(col1, col2);
695 check_origins {SELECT col2, col1 FROM tab1}
696 } [list {main tab1 col2} {main tab1 col1}]
698 check_origins {SELECT col2 AS hello, col1 AS world FROM tab1}
699 } [list {main tab1 col2} {main tab1 col1}]
703 check_origins {SELECT b, a FROM (SELECT col1 AS a, col2 AS b FROM tab1)}
704 } [list {main tab1 col2} {main tab1 col1}]
706 check_origins {SELECT (SELECT col2 FROM tab1), (SELECT col1 FROM tab1)}
707 } [list {main tab1 col2} {main tab1 col1}]
709 check_origins {SELECT (SELECT col2), (SELECT col1) FROM tab1}
710 } [list {main tab1 col2} {main tab1 col1}]
712 check_origins {SELECT * FROM tab1}
713 } [list {main tab1 col1} {main tab1 col2}]
715 check_origins {SELECT * FROM (SELECT * FROM tab1)}
716 } [list {main tab1 col1} {main tab1 col2}]
719 ifcapable view&&subquery {
722 CREATE VIEW view1 AS SELECT * FROM tab1;
726 check_origins {SELECT col2, col1 FROM view1}
727 } [list {main tab1 col2} {main tab1 col1}]
729 check_origins {SELECT col2 AS hello, col1 AS world FROM view1}
730 } [list {main tab1 col2} {main tab1 col1}]
732 check_origins {SELECT b, a FROM (SELECT col1 AS a, col2 AS b FROM view1)}
733 } [list {main tab1 col2} {main tab1 col1}]
735 check_origins {SELECT (SELECT col2 FROM view1), (SELECT col1 FROM view1)}
736 } [list {main tab1 col2} {main tab1 col1}]
738 check_origins {SELECT (SELECT col2), (SELECT col1) FROM view1}
739 } [list {main tab1 col2} {main tab1 col1}]
741 check_origins {SELECT * FROM view1}
742 } [list {main tab1 col1} {main tab1 col2}]
744 check_origins {select * from (select * from view1)}
745 } [list {main tab1 col1} {main tab1 col2}]
747 check_origins {select * from (select * from (select * from view1))}
748 } [list {main tab1 col1} {main tab1 col2}]
749 do_test capi2-12.10 {
752 set ::DB [sqlite3_connection_pointer db]
753 check_origins {select * from (select * from (select * from view1))}
754 } [list {main tab1 col1} {main tab1 col2}]
756 # This view will thwart the flattening optimization.
759 CREATE VIEW view2 AS SELECT * FROM tab1 limit 10 offset 10;
763 check_origins {SELECT col2, col1 FROM view2}
764 } [list {main tab1 col2} {main tab1 col1}]
766 check_origins {SELECT col2 AS hello, col1 AS world FROM view2}
767 } [list {main tab1 col2} {main tab1 col1}]
769 check_origins {SELECT b, a FROM (SELECT col1 AS a, col2 AS b FROM view2)}
770 } [list {main tab1 col2} {main tab1 col1}]
772 check_origins {SELECT (SELECT col2 FROM view2), (SELECT col1 FROM view2)}
773 } [list {main tab1 col2} {main tab1 col1}]
775 check_origins {SELECT (SELECT col2), (SELECT col1) FROM view2}
776 } [list {main tab1 col2} {main tab1 col1}]
778 check_origins {SELECT * FROM view2}
779 } [list {main tab1 col1} {main tab1 col2}]
781 check_origins {select * from (select * from view2)}
782 } [list {main tab1 col1} {main tab1 col2}]
784 check_origins {select * from (select * from (select * from view2))}
785 } [list {main tab1 col1} {main tab1 col2}]
786 do_test capi2-13.10 {
789 set ::DB [sqlite3_connection_pointer db]
790 check_origins {select * from (select * from (select * from view2))}
791 } [list {main tab1 col1} {main tab1 col2}]
792 do_test capi2-13.11 {
793 check_origins {select * from (select * from tab1 limit 10 offset 10)}
794 } [list {main tab1 col1} {main tab1 col2}]
798 } ;# ifcapable columnmetadata