First public contribution.
     3 # Portions Copyright (c) 2007-2008 Nokia Corporation and/or its subsidiaries. All rights reserved.
 
     5 # The author disclaims copyright to this source code.  In place of
 
     6 # a legal notice, here is a blessing:
 
     8 #    May you do good and not evil.
 
     9 #    May you find forgiveness for yourself and forgive others.
 
    10 #    May you share freely, never taking more than you give.
 
    12 #***********************************************************************
 
    13 # This file implements regression tests for TCL interface to the
 
    16 # Actually, all tests are based on the TCL interface, so the main
 
    17 # interface is pretty well tested.  This file contains some addition
 
    18 # tests for fringe issues that the main test suite does not cover.
 
    20 # $Id: tclsqlite.test,v 1.69 2008/09/09 12:31:34 drh Exp $
 
    22 set testdir [file dirname $argv0]
 
    23 source $testdir/tester.tcl
 
    25 # Check the error messages generated by tclsqlite
 
    27 if {[sqlite3 -has-codec]} {
 
    28   set r "sqlite_orig HANDLE FILENAME ?-key CODEC-KEY?"
 
    30   set r "sqlite3 HANDLE FILENAME ?-vfs VFSNAME? ?-readonly BOOLEAN? ?-create BOOLEAN? ?-nomutex BOOLEAN? ?-fullmutex BOOLEAN?"
 
    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 {db bogus} msg]
 
    40 } {1 {bad option "bogus": must be authorizer, busy, cache, changes, close, collate, collation_needed, commit_hook, complete, copy, enable_load_extension, errorcode, eval, exists, function, incrblob, interrupt, last_insert_rowid, nullvalue, onecolumn, profile, progress, rekey, rollback_hook, timeout, total_changes, trace, transaction, update_hook, or version}}
 
    42   set v [catch {db cache bogus} msg]
 
    44 } {1 {bad option "bogus": must be flush or size}}
 
    46   set v [catch {db cache} msg]
 
    48 } {1 {wrong # args: should be "db cache option ?arg?"}}
 
    50   execsql {CREATE TABLE t1(a int, b int)}
 
    51   execsql {INSERT INTO t1 VALUES(10,20)}
 
    53     db eval {SELECT * FROM t1} data {
 
    54       error "The error message"
 
    58 } {1 {The error message}}
 
    61     db eval {SELECT * FROM t2} data {
 
    62       error "The error message"
 
    66 } {1 {no such table: t2}}
 
    69     db eval {SELECT * FROM t1} data {
 
    78     db eval {SELECT * FROM t1} data {
 
    85   set v [catch {db} msg]
 
    87 } {1 {wrong # args: should be "db SUBCOMMAND ..."}}
 
    88 if {[catch {db auth {}}]==0} {
 
    90     set v [catch {db authorizer 1 2 3} msg]
 
    92   } {1 {wrong # args: should be "db authorizer ?CALLBACK?"}}
 
    95   set v [catch {db busy 1 2 3} msg]
 
    97 } {1 {wrong # args: should be "db busy CALLBACK"}}
 
    99   set v [catch {db progress 1} msg]
 
   101 } {1 {wrong # args: should be "db progress N CALLBACK"}}
 
   103   set v [catch {db changes xyz} msg]
 
   105 } {1 {wrong # args: should be "db changes "}}
 
   107   set v [catch {db commit_hook a b c} msg]
 
   109 } {1 {wrong # args: should be "db commit_hook ?CALLBACK?"}}
 
   110 ifcapable {complete} {
 
   112     set v [catch {db complete} msg]
 
   114   } {1 {wrong # args: should be "db complete SQL"}}
 
   117   set v [catch {db eval} msg]
 
   119 } {1 {wrong # args: should be "db eval SQL ?ARRAY-NAME? ?SCRIPT?"}}
 
   121   set v [catch {db function} msg]
 
   123 } {1 {wrong # args: should be "db function NAME [-argcount N] SCRIPT"}}
 
   125   set v [catch {db last_insert_rowid xyz} msg]
 
   127 } {1 {wrong # args: should be "db last_insert_rowid "}}
 
   129   set v [catch {db rekey} msg]
 
   131 } {1 {wrong # args: should be "db rekey KEY"}}
 
   133   set v [catch {db timeout} msg]
 
   135 } {1 {wrong # args: should be "db timeout MILLISECONDS"}}
 
   137   set v [catch {db collate} msg]
 
   139 } {1 {wrong # args: should be "db collate NAME SCRIPT"}}
 
   141   set v [catch {db collation_needed} msg]
 
   143 } {1 {wrong # args: should be "db collation_needed SCRIPT"}}
 
   145   set v [catch {db total_changes xyz} msg]
 
   147 } {1 {wrong # args: should be "db total_changes "}}
 
   149   set v [catch {db copy} msg]
 
   151 } {1 {wrong # args: should be "db copy CONFLICT-ALGORITHM TABLE FILENAME ?SEPARATOR? ?NULLINDICATOR?"}}
 
   153   set v [catch {sqlite3 db2 test.db -vfs nosuchvfs} msg]
 
   155 } {1 {no such vfs: nosuchvfs}}
 
   157 catch {unset ::result}
 
   159   execsql "CREATE TABLE t\u0123x(a int, b\u1235 float)"
 
   161 ifcapable schema_pragmas {
 
   163     execsql "PRAGMA table_info(t\u0123x)"
 
   164   } "0 a int 0 {} 0 1 b\u1235 float 0 {} 0"
 
   167   execsql "INSERT INTO t\u0123x VALUES(1,2.3)"
 
   168   db eval "SELECT * FROM t\u0123x" result break
 
   173 # Test the onecolumn method
 
   177     INSERT INTO t1 SELECT a*2, b*2 FROM t1;
 
   178     INSERT INTO t1 SELECT a*2+1, b*2+1 FROM t1;
 
   179     INSERT INTO t1 SELECT a*2+3, b*2+3 FROM t1;
 
   181   set rc [catch {db onecolumn {SELECT * FROM t1 ORDER BY a}} msg]
 
   185   db onecolumn {SELECT * FROM t1 WHERE a<0}
 
   188   set rc [catch {db onecolumn} errmsg]
 
   190 } {1 {wrong # args: should be "db onecolumn SQL"}}
 
   192   set rc [catch {db onecolumn {SELECT bogus}} errmsg]
 
   194 } {1 {no such column: bogus}}
 
   198     set rc [catch {db one {SELECT * FROM t1 WHERE b>$b}} msg]
 
   203     set rc [catch {db one {SELECT * FROM t1 WHERE b>$b}} msg]
 
   208     set rc [catch {db one {
 
   209       INSERT INTO t1 VALUES(99,510);
 
   210       SELECT * FROM t1 WHERE b>$b
 
   215 ifcapable {!tclvar} {
 
   216    execsql {INSERT INTO t1 VALUES(99,510)}
 
   219 # Turn the busy handler on and off
 
   222   proc busy_callback {cnt} {
 
   225   db busy busy_callback
 
   234   # Parsing of TCL variable names within SQL into bound parameters.
 
   237     execsql {CREATE TABLE t3(a,b,c)}
 
   242       INSERT INTO t3 VALUES($::x(1),$::x(2),$::x(3));
 
   248       SELECT typeof(a), typeof(b), typeof(c) FROM t3
 
   253     set x [binary format h12 686900686f00]
 
   255       UPDATE t3 SET a=$::x;
 
   260     binary scan $a h12 adata
 
   265       SELECT typeof(a), typeof(b), typeof(c) FROM t3
 
   270 # Operation of "break" and "continue" within row scripts
 
   273   db eval {SELECT * FROM t1} {
 
   280   db eval {SELECT * FROM t1} {
 
   288   db eval {SELECT * FROM t1} {
 
   295   proc return_test {x} {
 
   296     db eval {SELECT * FROM t1} {
 
   297       if {$a==$x} {return $b}
 
   317 # modify and reset the NULL representation
 
   321   execsql {INSERT INTO t1 VALUES(30,NULL)}
 
   322   db eval {SELECT * FROM t1 WHERE b IS NULL}
 
   330   db eval {SELECT * FROM t1 WHERE b IS NULL}
 
   333 # Test the return type of user-defined functions
 
   336   db function ret_str {return "hi"}
 
   337   execsql {SELECT typeof(ret_str())}
 
   340   db function ret_dbl {return [expr {rand()*0.5}]}
 
   341   execsql {SELECT typeof(ret_dbl())}
 
   344   db function ret_int {return [expr {int(rand()*200)}]}
 
   345   execsql {SELECT typeof(ret_int())}
 
   348 # Recursive calls to the same user-defined function
 
   352     proc userfunc_r1 {n} {
 
   353       if {$n<=0} {return 0}
 
   354       set nm1 [expr {$n-1}]
 
   355       return [expr {[db eval {SELECT r1($nm1)}]+$n}]
 
   357     db function r1 userfunc_r1
 
   358     execsql {SELECT r1(10)}
 
   360   if {$::tcl_platform(platform)!="symbian"} {
 
   362       execsql {SELECT r1(100)}
 
   367 # Tests for the new transaction method
 
   373   db transaction deferred {}
 
   376   db transaction immediate {}
 
   379   db transaction exclusive {}
 
   382   set rc [catch {db transaction xyzzy {}} msg]
 
   384 } {1 {bad transaction type "xyzzy": must be deferred, exclusive, or immediate}}
 
   386   set rc [catch {db transaction {error test-error}} msg]
 
   391     db eval {CREATE TABLE t4(x)}
 
   393       db eval {INSERT INTO t4 VALUES(1)}
 
   396   db eval {SELECT * FROM t4}
 
   401       db eval {INSERT INTO t4 VALUES(2)}
 
   402       db eval {INSERT INTO t4 VALUES(3)}
 
   403       db eval {INSERT INTO t4 VALUES(4)}
 
   407   db eval {SELECT * FROM t4}
 
   411     db eval {INSERT INTO t4 VALUES(2)}
 
   414         db eval {INSERT INTO t4 VALUES(3)}
 
   415         db eval {INSERT INTO t4 VALUES(4)}
 
   420   db eval {SELECT * FROM t4}
 
   423   for {set i 0} {$i<1} {incr i} {
 
   425       db eval {INSERT INTO t4 VALUES(5)}
 
   429   db eval {SELECT * FROM t4}
 
   432   for {set i 0} {$i<10} {incr i} {
 
   434       db eval {INSERT INTO t4 VALUES(6)}
 
   438   db eval {SELECT * FROM t4}
 
   442     for {set i 0} {$i<10} {incr i} {
 
   444         db eval {INSERT INTO t4 VALUES(7)}
 
   451   db eval {SELECT * FROM t4}
 
   455   db exists {SELECT x,x*2,x+x FROM t4 WHERE x==4}
 
   458   db exists {SELECT 0 FROM t4 WHERE x==4}
 
   461   db exists {SELECT 1 FROM t4 WHERE x==8}
 
   465   unset -nocomplain a b c version
 
   466   set version [db version]
 
   467   scan $version "%d.%d.%d" a b c
 
   468   expr $a*1000000 + $b*1000 + $c
 
   469 } [sqlite3_libversion_number]
 
   472 # Check to see that when bindings of the form @aaa are used instead
 
   473 # of $aaa, that objects are treated as bytearray and are inserted
 
   478     db eval {CREATE TABLE t5(x BLOB)}
 
   480     db eval {INSERT INTO t5 VALUES($x)}
 
   481     db eval {SELECT typeof(x) FROM t5}
 
   484     binary scan $x H notUsed
 
   487       INSERT INTO t5 VALUES($x);
 
   488       SELECT typeof(x) FROM t5;
 
   494       INSERT INTO t5 VALUES(@x);
 
   495       SELECT typeof(x) FROM t5;
 
   502       INSERT INTO t5 VALUES(@y);
 
   503       SELECT hex(x), typeof(x) FROM t5