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 TCL interface to the
14 # Actually, all tests are based on the TCL interface, so the main
15 # interface is pretty well tested. This file contains some addition
16 # tests for fringe issues that the main test suite does not cover.
18 # $Id: tclsqlite.test,v 1.73 2009/03/16 13:19:36 danielk1977 Exp $
22 set testdir [file dirname $argv0]
23 source $testdir/tester.tcl
26 # Check the error messages generated by tclsqlite
28 set r "sqlite_orig HANDLE ?FILENAME? ?-vfs VFSNAME? ?-readonly BOOLEAN? ?-create BOOLEAN? ?-nofollow BOOLEAN? ?-nomutex BOOLEAN? ?-fullmutex BOOLEAN? ?-uri BOOLEAN?"
29 if {[sqlite3 -has-codec]} {
30 append r " ?-key CODECKEY?"
33 set v [catch {sqlite3 -bogus} msg]
34 regsub {really_sqlite3} $msg {sqlite3} msg
36 } [list 1 "wrong # args: should be \"$r\""]
38 set v [catch {sqlite3} msg]
39 regsub {really_sqlite3} $msg {sqlite3} msg
41 } [list 1 "wrong # args: should be \"$r\""]
43 set v [catch {db bogus} msg]
45 } {1 {bad option "bogus": must be authorizer, backup, bind_fallback, busy, cache, changes, close, collate, collation_needed, commit_hook, complete, config, copy, deserialize, enable_load_extension, errorcode, erroroffset, eval, exists, function, incrblob, interrupt, last_insert_rowid, nullvalue, onecolumn, preupdate, profile, progress, rekey, restore, rollback_hook, serialize, status, timeout, total_changes, trace, trace_v2, transaction, unlock_notify, update_hook, version, or wal_hook}}
47 set v [catch {db cache bogus} msg]
49 } {1 {bad option "bogus": must be flush or size}}
51 set v [catch {db cache} msg]
53 } {1 {wrong # args: should be "db cache option ?arg?"}}
55 execsql {CREATE TABLE t1(a int, b int)}
56 execsql {INSERT INTO t1 VALUES(10,20)}
58 db eval {SELECT * FROM t1} data {
59 error "The error message"
63 } {1 {The error message}}
66 db eval {SELECT * FROM t2} data {
67 error "The error message"
71 } {1 {no such table: t2}}
74 db eval {SELECT * FROM t1} data {
83 db eval {SELECT * FROM t1} data {
90 set v [catch {db} msg]
92 } {1 {wrong # args: should be "db SUBCOMMAND ..."}}
93 if {[catch {db auth {}}]==0} {
95 set v [catch {db authorizer 1 2 3} msg]
97 } {1 {wrong # args: should be "db authorizer ?CALLBACK?"}}
100 set v [catch {db busy 1 2 3} msg]
102 } {1 {wrong # args: should be "db busy CALLBACK"}}
104 set v [catch {db progress 1} msg]
106 } {1 {wrong # args: should be "db progress N CALLBACK"}}
108 set v [catch {db changes xyz} msg]
110 } {1 {wrong # args: should be "db changes "}}
112 set v [catch {db commit_hook a b c} msg]
114 } {1 {wrong # args: should be "db commit_hook ?CALLBACK?"}}
115 ifcapable {complete} {
117 set v [catch {db complete} msg]
119 } {1 {wrong # args: should be "db complete SQL"}}
122 set v [catch {db eval} msg]
124 } {1 {wrong # args: should be "db eval ?OPTIONS? SQL ?ARRAY-NAME? ?SCRIPT?"}}
126 set v [catch {db function} msg]
128 } {1 {wrong # args: should be "db function NAME ?SWITCHES? SCRIPT"}}
130 set v [catch {db last_insert_rowid xyz} msg]
132 } {1 {wrong # args: should be "db last_insert_rowid "}}
134 set v [catch {db rekey} msg]
136 } {1 {wrong # args: should be "db rekey KEY"}}
138 set v [catch {db timeout} msg]
140 } {1 {wrong # args: should be "db timeout MILLISECONDS"}}
142 set v [catch {db collate} msg]
144 } {1 {wrong # args: should be "db collate NAME SCRIPT"}}
146 set v [catch {db collation_needed} msg]
148 } {1 {wrong # args: should be "db collation_needed SCRIPT"}}
150 set v [catch {db total_changes xyz} msg]
152 } {1 {wrong # args: should be "db total_changes "}}
154 set v [catch {db copy} msg]
156 } {1 {wrong # args: should be "db copy CONFLICT-ALGORITHM TABLE FILENAME ?SEPARATOR? ?NULLINDICATOR?"}}
158 set v [catch {sqlite3 db2 test.db -vfs nosuchvfs} msg]
160 } {1 {no such vfs: nosuchvfs}}
162 catch {unset ::result}
164 execsql "CREATE TABLE t\u0123x(a int, b\u1235 float)"
166 ifcapable schema_pragmas {
168 execsql "PRAGMA table_info(t\u0123x)"
169 } "0 a INT 0 {} 0 1 b\u1235 float 0 {} 0"
172 execsql "INSERT INTO t\u0123x VALUES(1,2.3)"
173 db eval "SELECT * FROM t\u0123x" result break
178 # Test the onecolumn method
182 INSERT INTO t1 SELECT a*2, b*2 FROM t1;
183 INSERT INTO t1 SELECT a*2+1, b*2+1 FROM t1;
184 INSERT INTO t1 SELECT a*2+3, b*2+3 FROM t1;
186 set rc [catch {db onecolumn {SELECT * FROM t1 ORDER BY a}} msg]
190 db onecolumn {SELECT * FROM t1 WHERE a<0}
193 set rc [catch {db onecolumn} errmsg]
195 } {1 {wrong # args: should be "db onecolumn SQL"}}
197 set rc [catch {db onecolumn {SELECT bogus}} errmsg]
199 } {1 {no such column: bogus}}
203 set rc [catch {db one {SELECT * FROM t1 WHERE b>$b}} msg]
208 set rc [catch {db one {SELECT * FROM t1 WHERE b>$b}} msg]
213 set rc [catch {db one {
214 INSERT INTO t1 VALUES(99,510);
215 SELECT * FROM t1 WHERE b>$b
220 ifcapable {!tclvar} {
221 execsql {INSERT INTO t1 VALUES(99,510)}
224 # Turn the busy handler on and off
227 proc busy_callback {cnt} {
230 db busy busy_callback
239 # Parsing of TCL variable names within SQL into bound parameters.
242 execsql {CREATE TABLE t3(a,b,c)}
247 INSERT INTO t3 VALUES($::x(1),$::x(2),$::x(3));
253 SELECT typeof(a), typeof(b), typeof(c) FROM t3
258 set x [binary format h12 686900686f00]
260 UPDATE t3 SET a=$::x;
265 binary scan $a h12 adata
270 SELECT typeof(a), typeof(b), typeof(c) FROM t3
275 # Operation of "break" and "continue" within row scripts
278 db eval {SELECT * FROM t1} {
285 db eval {SELECT * FROM t1} {
293 db eval {SELECT * FROM t1} {
300 proc return_test {x} {
301 db eval {SELECT * FROM t1} {
302 if {$a==$x} {return $b}
322 # modify and reset the NULL representation
326 execsql {INSERT INTO t1 VALUES(30,NULL)}
327 db eval {SELECT * FROM t1 WHERE b IS NULL}
329 proc concatFunc args {return [join $args {}]}
331 db function concat concatFunc
332 db eval {SELECT concat('a', b, 'z') FROM t1 WHERE b is NULL}
340 db eval {SELECT * FROM t1 WHERE b IS NULL}
343 db function concat concatFunc
344 db eval {SELECT concat('a', b, 'z') FROM t1 WHERE b is NULL}
347 # Test the return type of user-defined functions
350 db function ret_str {return "hi"}
351 execsql {SELECT typeof(ret_str())}
354 db function ret_dbl {return [expr {rand()*0.5}]}
355 execsql {SELECT typeof(ret_dbl())}
358 db function ret_int {return [expr {int(rand()*200)}]}
359 execsql {SELECT typeof(ret_int())}
362 # Recursive calls to the same user-defined function
366 proc userfunc_r1 {n} {
367 if {$n<=0} {return 0}
368 set nm1 [expr {$n-1}]
369 return [expr {[db eval {SELECT r1($nm1)}]+$n}]
371 db function r1 userfunc_r1
372 execsql {SELECT r1(10)}
374 # Fails under -fsanitize=address,undefined due to stack overflow
376 # execsql {SELECT r1(100)}
380 # Tests for the new transaction method
386 db transaction deferred {}
389 db transaction immediate {}
392 db transaction exclusive {}
395 set rc [catch {db transaction xyzzy {}} msg]
397 } {1 {bad transaction type "xyzzy": must be deferred, exclusive, or immediate}}
399 set rc [catch {db transaction {error test-error}} msg]
404 db eval {CREATE TABLE t4(x)}
406 db eval {INSERT INTO t4 VALUES(1)}
409 db eval {SELECT * FROM t4}
414 db eval {INSERT INTO t4 VALUES(2)}
415 db eval {INSERT INTO t4 VALUES(3)}
416 db eval {INSERT INTO t4 VALUES(4)}
420 db eval {SELECT * FROM t4}
424 db eval {INSERT INTO t4 VALUES(2)}
427 db eval {INSERT INTO t4 VALUES(3)}
428 db eval {INSERT INTO t4 VALUES(4)}
433 db eval {SELECT * FROM t4}
436 for {set i 0} {$i<1} {incr i} {
438 db eval {INSERT INTO t4 VALUES(5)}
441 error "This line should not be run"
443 db eval {SELECT * FROM t4}
446 for {set i 0} {$i<10} {incr i} {
448 db eval {INSERT INTO t4 VALUES(6)}
452 db eval {SELECT * FROM t4}
456 for {set i 0} {$i<10} {incr i} {
458 db eval {INSERT INTO t4 VALUES(7)}
465 db eval {SELECT * FROM t4}
468 # Now test that [db transaction] commands may be nested with
469 # the expected results.
475 INSERT INTO t4 VALUES('one');
480 db eval { INSERT INTO t4 VALUES('two') }
482 db eval { INSERT INTO t4 VALUES('three') }
483 error "throw an error!"
489 db eval {SELECT * FROM t4}
492 # Make sure a transaction has not been left open.
493 db eval {BEGIN ; COMMIT}
497 db eval { INSERT INTO t4 VALUES('two'); }
499 db eval { INSERT INTO t4 VALUES('three') }
501 db eval { INSERT INTO t4 VALUES('four') }
505 db eval {SELECT * FROM t4}
506 } {one two three four}
510 db eval { INSERT INTO t4 VALUES('A'); }
512 db eval { INSERT INTO t4 VALUES('B') }
514 db eval { INSERT INTO t4 VALUES('C') }
515 error "throw an error!"
520 db eval {SELECT * FROM t4}
521 } {one two three four}
523 # Make sure a transaction has not been left open.
524 db eval {BEGIN ; COMMIT}
527 # Mess up a [db transaction] command by locking the database using a
528 # second connection when it tries to commit. Make sure the transaction
529 # is not still open after the "database is locked" exception is thrown.
535 SELECT * FROM sqlite_master;
540 db eval {INSERT INTO t4 VALUES('five')}
544 } {1 {database is locked}}
546 db eval {BEGIN ; COMMIT}
549 # Thwart a [db transaction] command by locking the database using a
550 # second connection with "BEGIN EXCLUSIVE". Make sure no transaction is
551 # open after the "database is locked" exception is thrown.
560 db eval {INSERT INTO t4 VALUES('five')}
564 } {1 {database is locked}}
567 db eval {BEGIN ; COMMIT}
571 db transaction exclusive {
572 catch { db2 eval {SELECT * FROM sqlite_master} } msg
576 } {db2: database is locked}
580 db eval {INSERT INTO t4 VALUES(6)}
581 db exists {SELECT x,x*2,x+x FROM t4 WHERE x==6}
584 db exists {SELECT 0 FROM t4 WHERE x==6}
587 db exists {SELECT 1 FROM t4 WHERE x==8}
590 tcl_objproc db exists {SELECT 1 FROM t4 WHERE x==8}
594 unset -nocomplain a b c version
595 set version [db version]
596 scan $version "%d.%d.%d" a b c
597 expr $a*1000000 + $b*1000 + $c
598 } [sqlite3_libversion_number]
601 # Check to see that when bindings of the form @aaa are used instead
602 # of $aaa, that objects are treated as bytearray and are inserted
607 db eval {CREATE TABLE t5(x BLOB)}
609 db eval {INSERT INTO t5 VALUES($x)}
610 db eval {SELECT typeof(x) FROM t5}
613 binary scan $x H notUsed
616 INSERT INTO t5 VALUES($x);
617 SELECT typeof(x) FROM t5;
623 INSERT INTO t5 VALUES(@x);
624 SELECT typeof(x) FROM t5;
631 INSERT INTO t5 VALUES(@y);
632 SELECT hex(x), typeof(x) FROM t5
638 proc xCall {} { return "value" }
639 do_execsql_test tcl-14.1 {
641 INSERT INTO t6 VALUES(1);
644 db one {SELECT x FROM t6 WHERE xCall()!='value'}
647 # Verify that the "exists" and "onecolumn" methods work when
648 # a "profile" is registered.
652 proc noop-profile {args} {
656 db eval {CREATE TABLE t1(a); INSERT INTO t1 VALUES(1),(2),(3);}
657 db onecolumn {SELECT a FROM t1 WHERE a>2}
660 db exists {SELECT a FROM t1 WHERE a>2}
663 db exists {SELECT a FROM t1 WHERE a>3}
665 db profile noop-profile
667 db onecolumn {SELECT a FROM t1 WHERE a>2}
670 db exists {SELECT a FROM t1 WHERE a>2}
673 db exists {SELECT a FROM t1 WHERE a>3}
677 # 2017-06-26: The --withoutnulls flag to "db eval".
679 # In the "db eval --withoutnulls SQL ARRAY" form, NULL results cause the
680 # corresponding array entry to be unset. The default behavior (without
681 # the -withoutnulls flags) is for the corresponding array value to get
682 # the [db nullvalue] string.
687 do_execsql_test tcl-16.100 {
688 CREATE TABLE t1(a,b);
689 INSERT INTO t1 VALUES(1,2),(2,NULL),(3,'xyz');
694 db eval {SELECT * FROM t1} x {
695 lappend res $x(a) [array names x]
698 } {1 {a b *} 2 {a b *} 3 {a b *}}
701 db eval -unknown {SELECT * FROM t1} x {
702 lappend res $x(a) [array names x]
706 } {1 {unknown option: "-unknown"}}
710 db eval -withoutnulls {SELECT * FROM t1} x {
711 lappend res $x(a) [array names x]
714 } {1 {a b *} 2 {a *} 3 {a b *}}
716 #-------------------------------------------------------------------------
717 # Test the -type option to [db function].
720 proc add {a b} { return [expr $a + $b] }
721 proc ret {a} { return $a }
723 db function add_i -returntype integer add
724 db function add_r -ret real add
725 db function add_t -return text add
726 db function add_b -returntype blob add
727 db function add_a -returntype any add
729 db function ret_i -returntype int ret
730 db function ret_r -returntype real ret
731 db function ret_t -returntype text ret
732 db function ret_b -returntype blob ret
733 db function ret_a -r any ret
735 do_execsql_test 17.0 {
736 SELECT quote( add_i(2, 3) );
737 SELECT quote( add_r(2, 3) );
738 SELECT quote( add_t(2, 3) );
739 SELECT quote( add_b(2, 3) );
740 SELECT quote( add_a(2, 3) );
741 } {5 5.0 '5' X'35' 5}
743 do_execsql_test 17.1 {
744 SELECT quote( add_i(2.2, 3.3) );
745 SELECT quote( add_r(2.2, 3.3) );
746 SELECT quote( add_t(2.2, 3.3) );
747 SELECT quote( add_b(2.2, 3.3) );
748 SELECT quote( add_a(2.2, 3.3) );
749 } {5.5 5.5 '5.5' X'352E35' 5.5}
751 do_execsql_test 17.2 {
752 SELECT quote( ret_i(2.5) );
753 SELECT quote( ret_r(2.5) );
754 SELECT quote( ret_t(2.5) );
755 SELECT quote( ret_b(2.5) );
756 SELECT quote( ret_a(2.5) );
757 } {2.5 2.5 '2.5' X'322E35' 2.5}
759 do_execsql_test 17.3 {
760 SELECT quote( ret_i('2.5') );
761 SELECT quote( ret_r('2.5') );
762 SELECT quote( ret_t('2.5') );
763 SELECT quote( ret_b('2.5') );
764 SELECT quote( ret_a('2.5') );
765 } {2.5 2.5 '2.5' X'322E35' '2.5'}
767 do_execsql_test 17.4 {
768 SELECT quote( ret_i('abc') );
769 SELECT quote( ret_r('abc') );
770 SELECT quote( ret_t('abc') );
771 SELECT quote( ret_b('abc') );
772 SELECT quote( ret_a('abc') );
773 } {'abc' 'abc' 'abc' X'616263' 'abc'}
775 do_execsql_test 17.5 {
776 SELECT quote( ret_i(X'616263') );
777 SELECT quote( ret_r(X'616263') );
778 SELECT quote( ret_t(X'616263') );
779 SELECT quote( ret_b(X'616263') );
780 SELECT quote( ret_a(X'616263') );
781 } {'abc' 'abc' 'abc' X'616263' X'616263'}
784 list [catch { db function xyz -return object ret } msg] $msg
785 } {1 {bad type "object": must be integer, real, text, blob, or any}}
788 list [catch { db function xyz -return ret } msg] $msg
789 } {1 {option requires an argument: -return}}
792 list [catch { db function xyz -n object ret } msg] $msg
793 } {1 {bad option "-n": must be -argcount, -deterministic, -directonly, -innocuous, or -returntype}}
795 # 2019-02-28: The "bind_fallback" command.
798 unset -nocomplain bindings abc def ghi jkl mno e01 e02
799 set bindings(abc) [expr {1+2}]
800 set bindings(def) {hello}
801 set bindings(ghi) [expr {3.1415926*1.0}]
802 proc bind_callback {nm} {
804 set n2 [string range $nm 1 end]
805 if {[info exists bindings($n2)]} {
806 return $bindings($n2)
808 if {[string match e* $n2]} {
809 error "no such variable: $nm"
811 return -code return {}
813 db bind_fallback bind_callback
814 db eval {SELECT $abc, typeof($abc), $def, typeof($def), $ghi, typeof($ghi)}
815 } {3 integer hello text 3.1415926 real}
817 db eval {SELECT quote(@def), typeof(@def)}
818 } {X'68656C6C6F' blob}
819 do_execsql_test 18.120 {
822 do_catchsql_test 18.130 {
824 } {1 {no such variable: $e01}}
830 db eval {SELECT $abc, typeof($abc), $def, typeof($def), $ghi, typeof($ghi)}
831 } {{} null {} null {} null}
833 unset -nocomplain bindings
834 proc bind_callback {nm} {lappend ::bindings $nm}
835 db bind_fallback bind_callback
836 db eval {SELECT $abc, @def, $ghi(123), :mno}
838 } {{$abc} @def {$ghi(123)} :mno}
840 set rc [catch {db bind_fallback a b} msg]
842 } {1 {wrong # args: should be "db bind_fallback ?CALLBACK?"}}
844 db bind_fallback bind_fallback_does_not_exist
846 do_catchsql_test 19.911 {
847 SELECT $abc, typeof($abc), $def, typeof($def), $ghi, typeof($ghi);
848 } {1 {invalid command name "bind_fallback_does_not_exist"}}
851 #-------------------------------------------------------------------------
861 db eval {SELECT 1 UNION ALL SELECT 2 UNION ALL SELECT 3} { db close }
864 } {1 {invalid command name "db"}}
871 proc func1 {} { return 1 }
874 db func closedb closedb
880 SELECT closedb(),func1() UNION ALL SELECT 20,30 UNION ALL SELECT 30,40
884 } {0 {10 1 20 30 30 40}}
888 catch {db eval {SELECT 1 2 3;}} msg