blob: 158a0087d93c380874cfa21f6cd42fcca72d4b0e [file] [log] [blame]
drhb19a2bc2001-09-16 00:13:26 +00001# 2001 September 15
drh960e8c62001-04-03 16:53:21 +00002#
drhb19a2bc2001-09-16 00:13:26 +00003# The author disclaims copyright to this source code. In place of
4# a legal notice, here is a blessing:
drh960e8c62001-04-03 16:53:21 +00005#
drhb19a2bc2001-09-16 00:13:26 +00006# 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.
drh960e8c62001-04-03 16:53:21 +00009#
10#***********************************************************************
11# This file implements regression tests for TCL interface to the
12# SQLite library.
13#
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.
17#
drh3570ad92007-08-31 14:31:44 +000018# $Id: tclsqlite.test,v 1.60 2007/08/31 14:31:45 drh Exp $
drh960e8c62001-04-03 16:53:21 +000019
20set testdir [file dirname $argv0]
21source $testdir/tester.tcl
22
23# Check the error messages generated by tclsqlite
24#
drhef4ac8f2004-06-19 00:16:31 +000025if {[sqlite3 -has-codec]} {
drh9eb9e262004-02-11 02:18:05 +000026 set r "sqlite_orig HANDLE FILENAME ?-key CODEC-KEY?"
drh22fbcb82004-02-01 01:22:50 +000027} else {
drh3570ad92007-08-31 14:31:44 +000028 set r "sqlite3 HANDLE FILENAME ?-vfs VFSNAME? ?-readonly BOOLEAN? ?-create BOOLEAN?"
drh22fbcb82004-02-01 01:22:50 +000029}
drh960e8c62001-04-03 16:53:21 +000030do_test tcl-1.1 {
drhef4ac8f2004-06-19 00:16:31 +000031 set v [catch {sqlite3 bogus} msg]
drh960e8c62001-04-03 16:53:21 +000032 lappend v $msg
drh22fbcb82004-02-01 01:22:50 +000033} [list 1 "wrong # args: should be \"$r\""]
drh960e8c62001-04-03 16:53:21 +000034do_test tcl-1.2 {
35 set v [catch {db bogus} msg]
36 lappend v $msg
danielk1977b4e9af92007-05-01 17:49:49 +000037} {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}}
drh960e8c62001-04-03 16:53:21 +000038do_test tcl-1.3 {
39 execsql {CREATE TABLE t1(a int, b int)}
40 execsql {INSERT INTO t1 VALUES(10,20)}
41 set v [catch {
42 db eval {SELECT * FROM t1} data {
43 error "The error message"
44 }
45 } msg]
46 lappend v $msg
47} {1 {The error message}}
48do_test tcl-1.4 {
49 set v [catch {
50 db eval {SELECT * FROM t2} data {
51 error "The error message"
52 }
53 } msg]
54 lappend v $msg
55} {1 {no such table: t2}}
56do_test tcl-1.5 {
57 set v [catch {
58 db eval {SELECT * FROM t1} data {
59 break
60 }
61 } msg]
62 lappend v $msg
63} {0 {}}
64do_test tcl-1.6 {
65 set v [catch {
66 db eval {SELECT * FROM t1} data {
67 expr x*
68 }
69 } msg]
drha297b5c2002-01-15 18:39:43 +000070 regsub {:.*$} $msg {} msg
drh960e8c62001-04-03 16:53:21 +000071 lappend v $msg
72} {1 {syntax error in expression "x*"}}
drh0f14e2e2004-06-29 12:39:08 +000073do_test tcl-1.7 {
74 set v [catch {db} msg]
75 lappend v $msg
76} {1 {wrong # args: should be "db SUBCOMMAND ..."}}
drh1211de32004-07-26 12:24:22 +000077if {[catch {db auth {}}]==0} {
78 do_test tcl-1.8 {
79 set v [catch {db authorizer 1 2 3} msg]
80 lappend v $msg
81 } {1 {wrong # args: should be "db authorizer ?CALLBACK?"}}
82}
drh0f14e2e2004-06-29 12:39:08 +000083do_test tcl-1.9 {
84 set v [catch {db busy 1 2 3} msg]
85 lappend v $msg
86} {1 {wrong # args: should be "db busy CALLBACK"}}
87do_test tcl-1.10 {
88 set v [catch {db progress 1} msg]
89 lappend v $msg
90} {1 {wrong # args: should be "db progress N CALLBACK"}}
91do_test tcl-1.11 {
92 set v [catch {db changes xyz} msg]
93 lappend v $msg
94} {1 {wrong # args: should be "db changes "}}
95do_test tcl-1.12 {
96 set v [catch {db commit_hook a b c} msg]
97 lappend v $msg
98} {1 {wrong # args: should be "db commit_hook ?CALLBACK?"}}
drhccae6022005-02-26 17:31:26 +000099ifcapable {complete} {
100 do_test tcl-1.13 {
101 set v [catch {db complete} msg]
102 lappend v $msg
103 } {1 {wrong # args: should be "db complete SQL"}}
104}
drh0f14e2e2004-06-29 12:39:08 +0000105do_test tcl-1.14 {
106 set v [catch {db eval} msg]
107 lappend v $msg
drh895d7472004-08-20 16:02:39 +0000108} {1 {wrong # args: should be "db eval SQL ?ARRAY-NAME? ?SCRIPT?"}}
drh0f14e2e2004-06-29 12:39:08 +0000109do_test tcl-1.15 {
110 set v [catch {db function} msg]
111 lappend v $msg
112} {1 {wrong # args: should be "db function NAME SCRIPT"}}
danielk19777ddad962005-12-12 06:53:03 +0000113do_test tcl-1.16 {
drh0f14e2e2004-06-29 12:39:08 +0000114 set v [catch {db last_insert_rowid xyz} msg]
115 lappend v $msg
116} {1 {wrong # args: should be "db last_insert_rowid "}}
danielk19777ddad962005-12-12 06:53:03 +0000117do_test tcl-1.17 {
drh0f14e2e2004-06-29 12:39:08 +0000118 set v [catch {db rekey} msg]
119 lappend v $msg
120} {1 {wrong # args: should be "db rekey KEY"}}
danielk19777ddad962005-12-12 06:53:03 +0000121do_test tcl-1.18 {
drh0f14e2e2004-06-29 12:39:08 +0000122 set v [catch {db timeout} msg]
123 lappend v $msg
124} {1 {wrong # args: should be "db timeout MILLISECONDS"}}
danielk19777ddad962005-12-12 06:53:03 +0000125do_test tcl-1.19 {
drh0f14e2e2004-06-29 12:39:08 +0000126 set v [catch {db collate} msg]
127 lappend v $msg
128} {1 {wrong # args: should be "db collate NAME SCRIPT"}}
danielk19777ddad962005-12-12 06:53:03 +0000129do_test tcl-1.20 {
drh0f14e2e2004-06-29 12:39:08 +0000130 set v [catch {db collation_needed} msg]
131 lappend v $msg
132} {1 {wrong # args: should be "db collation_needed SCRIPT"}}
danielk19777ddad962005-12-12 06:53:03 +0000133do_test tcl-1.21 {
drh0f14e2e2004-06-29 12:39:08 +0000134 set v [catch {db total_changes xyz} msg]
135 lappend v $msg
136} {1 {wrong # args: should be "db total_changes "}}
tpoindex1067fe12004-12-17 15:41:11 +0000137do_test tcl-1.20 {
138 set v [catch {db copy} msg]
139 lappend v $msg
140} {1 {wrong # args: should be "db copy CONFLICT-ALGORITHM TABLE FILENAME ?SEPARATOR? ?NULLINDICATOR?"}}
drh0f14e2e2004-06-29 12:39:08 +0000141
drh960e8c62001-04-03 16:53:21 +0000142
drh3570ad92007-08-31 14:31:44 +0000143catch {unset ::result}
144do_test tcl-2.1 {
145 execsql "CREATE TABLE t\u0123x(a int, b\u1235 float)"
146} {}
147ifcapable schema_pragmas {
148 do_test tcl-2.2 {
149 execsql "PRAGMA table_info(t\u0123x)"
150 } "0 a int 0 {} 0 1 b\u1235 float 0 {} 0"
drh6d4abfb2001-10-22 02:58:08 +0000151}
drh3570ad92007-08-31 14:31:44 +0000152do_test tcl-2.3 {
153 execsql "INSERT INTO t\u0123x VALUES(1,2.3)"
154 db eval "SELECT * FROM t\u0123x" result break
155 set result(*)
156} "a b\u1235"
drh6d4abfb2001-10-22 02:58:08 +0000157
drh6d4abfb2001-10-22 02:58:08 +0000158
drh5d9d7572003-08-19 14:31:01 +0000159# Test the onecolumn method
160#
161do_test tcl-3.1 {
162 execsql {
163 INSERT INTO t1 SELECT a*2, b*2 FROM t1;
164 INSERT INTO t1 SELECT a*2+1, b*2+1 FROM t1;
165 INSERT INTO t1 SELECT a*2+3, b*2+3 FROM t1;
166 }
drh22fbcb82004-02-01 01:22:50 +0000167 set rc [catch {db onecolumn {SELECT * FROM t1 ORDER BY a}} msg]
168 lappend rc $msg
169} {0 10}
drh5d9d7572003-08-19 14:31:01 +0000170do_test tcl-3.2 {
171 db onecolumn {SELECT * FROM t1 WHERE a<0}
172} {}
173do_test tcl-3.3 {
174 set rc [catch {db onecolumn} errmsg]
175 lappend rc $errmsg
176} {1 {wrong # args: should be "db onecolumn SQL"}}
drh0f14e2e2004-06-29 12:39:08 +0000177do_test tcl-3.4 {
178 set rc [catch {db onecolumn {SELECT bogus}} errmsg]
179 lappend rc $errmsg
180} {1 {no such column: bogus}}
drh6bf89572004-11-03 16:27:01 +0000181ifcapable {tclvar} {
182 do_test tcl-3.5 {
183 set b 50
184 set rc [catch {db one {SELECT * FROM t1 WHERE b>$b}} msg]
185 lappend rc $msg
186 } {0 41}
187 do_test tcl-3.6 {
188 set b 500
189 set rc [catch {db one {SELECT * FROM t1 WHERE b>$b}} msg]
190 lappend rc $msg
191 } {0 {}}
192 do_test tcl-3.7 {
193 set b 500
194 set rc [catch {db one {
195 INSERT INTO t1 VALUES(99,510);
196 SELECT * FROM t1 WHERE b>$b
197 }} msg]
198 lappend rc $msg
199 } {0 99}
200}
201ifcapable {!tclvar} {
202 execsql {INSERT INTO t1 VALUES(99,510)}
203}
drh5d9d7572003-08-19 14:31:01 +0000204
drh0f14e2e2004-06-29 12:39:08 +0000205# Turn the busy handler on and off
206#
207do_test tcl-4.1 {
208 proc busy_callback {cnt} {
209 break
210 }
211 db busy busy_callback
212 db busy
213} {busy_callback}
214do_test tcl-4.2 {
215 db busy {}
216 db busy
217} {}
drh6d4abfb2001-10-22 02:58:08 +0000218
drh6bf89572004-11-03 16:27:01 +0000219ifcapable {tclvar} {
220 # Parsing of TCL variable names within SQL into bound parameters.
221 #
222 do_test tcl-5.1 {
223 execsql {CREATE TABLE t3(a,b,c)}
224 catch {unset x}
225 set x(1) 5
226 set x(2) 7
227 execsql {
228 INSERT INTO t3 VALUES($::x(1),$::x(2),$::x(3));
229 SELECT * FROM t3
230 }
231 } {5 7 {}}
232 do_test tcl-5.2 {
233 execsql {
234 SELECT typeof(a), typeof(b), typeof(c) FROM t3
235 }
236 } {text text null}
237 do_test tcl-5.3 {
238 catch {unset x}
239 set x [binary format h12 686900686f00]
240 execsql {
241 UPDATE t3 SET a=$::x;
242 }
243 db eval {
244 SELECT a FROM t3
245 } break
246 binary scan $a h12 adata
247 set adata
248 } {686900686f00}
249 do_test tcl-5.4 {
250 execsql {
251 SELECT typeof(a), typeof(b), typeof(c) FROM t3
252 }
253 } {blob text null}
254}
drh92febd92004-08-20 18:34:20 +0000255
drhfd241b02004-09-13 13:46:01 +0000256# Operation of "break" and "continue" within row scripts
257#
258do_test tcl-6.1 {
259 db eval {SELECT * FROM t1} {
260 break
261 }
262 lappend a $b
263} {10 20}
264do_test tcl-6.2 {
265 set cnt 0
266 db eval {SELECT * FROM t1} {
267 if {$a>40} continue
268 incr cnt
269 }
270 set cnt
271} {4}
272do_test tcl-6.3 {
273 set cnt 0
274 db eval {SELECT * FROM t1} {
275 if {$a<40} continue
276 incr cnt
277 }
278 set cnt
279} {5}
280do_test tcl-6.4 {
281 proc return_test {x} {
282 db eval {SELECT * FROM t1} {
283 if {$a==$x} {return $b}
284 }
285 }
286 return_test 10
287} 20
288do_test tcl-6.5 {
289 return_test 20
290} 40
291do_test tcl-6.6 {
292 return_test 99
293} 510
294do_test tcl-6.7 {
295 return_test 0
296} {}
297
danielk19774397de52005-01-12 12:44:03 +0000298do_test tcl-7.1 {
299 db version
300 expr 0
301} {0}
302
danielk197755c45f22005-04-03 23:54:43 +0000303# modify and reset the NULL representation
304#
305do_test tcl-8.1 {
306 db nullvalue NaN
307 execsql {INSERT INTO t1 VALUES(30,NULL)}
308 db eval {SELECT * FROM t1 WHERE b IS NULL}
309} {30 NaN}
310do_test tcl-8.2 {
311 db nullvalue NULL
312 db nullvalue
313} {NULL}
314do_test tcl-8.3 {
315 db nullvalue {}
316 db eval {SELECT * FROM t1 WHERE b IS NULL}
317} {30 {}}
318
drhc7f269d2005-05-05 10:30:29 +0000319# Test the return type of user-defined functions
320#
321do_test tcl-9.1 {
322 db function ret_str {return "hi"}
323 execsql {SELECT typeof(ret_str())}
324} {text}
325do_test tcl-9.2 {
drh9645d8d2006-09-01 15:49:05 +0000326 db function ret_dbl {return [expr {rand()*0.5}]}
drhc7f269d2005-05-05 10:30:29 +0000327 execsql {SELECT typeof(ret_dbl())}
328} {real}
329do_test tcl-9.3 {
drh9645d8d2006-09-01 15:49:05 +0000330 db function ret_int {return [expr {int(rand()*200)}]}
drhc7f269d2005-05-05 10:30:29 +0000331 execsql {SELECT typeof(ret_int())}
332} {integer}
333
drhd1e47332005-06-26 17:55:33 +0000334# Recursive calls to the same user-defined function
335#
danielk19773bdca9c2006-01-17 09:35:01 +0000336ifcapable tclvar {
337 do_test tcl-9.10 {
338 proc userfunc_r1 {n} {
339 if {$n<=0} {return 0}
340 set nm1 [expr {$n-1}]
341 return [expr {[db eval {SELECT r1($nm1)}]+$n}]
342 }
343 db function r1 userfunc_r1
344 execsql {SELECT r1(10)}
345 } {55}
346 do_test tcl-9.11 {
347 execsql {SELECT r1(100)}
348 } {5050}
349}
drhd1e47332005-06-26 17:55:33 +0000350
drhb5555e72005-08-02 17:15:14 +0000351# Tests for the new transaction method
352#
353do_test tcl-10.1 {
354 db transaction {}
355} {}
356do_test tcl-10.2 {
357 db transaction deferred {}
358} {}
359do_test tcl-10.3 {
360 db transaction immediate {}
361} {}
362do_test tcl-10.4 {
363 db transaction exclusive {}
364} {}
365do_test tcl-10.5 {
366 set rc [catch {db transaction xyzzy {}} msg]
367 lappend rc $msg
368} {1 {bad transaction type "xyzzy": must be deferred, exclusive, or immediate}}
369do_test tcl-10.6 {
370 set rc [catch {db transaction {error test-error}} msg]
371 lappend rc $msg
372} {1 test-error}
373do_test tcl-10.7 {
374 db transaction {
375 db eval {CREATE TABLE t4(x)}
376 db transaction {
377 db eval {INSERT INTO t4 VALUES(1)}
378 }
379 }
380 db eval {SELECT * FROM t4}
381} 1
382do_test tcl-10.8 {
383 catch {
384 db transaction {
385 db eval {INSERT INTO t4 VALUES(2)}
386 db eval {INSERT INTO t4 VALUES(3)}
387 db eval {INSERT INTO t4 VALUES(4)}
388 error test-error
389 }
390 }
391 db eval {SELECT * FROM t4}
392} 1
393do_test tcl-10.9 {
394 db transaction {
395 db eval {INSERT INTO t4 VALUES(2)}
396 catch {
397 db transaction {
398 db eval {INSERT INTO t4 VALUES(3)}
399 db eval {INSERT INTO t4 VALUES(4)}
400 error test-error
401 }
402 }
403 }
404 db eval {SELECT * FROM t4}
405} {1 2 3 4}
406do_test tcl-10.10 {
407 for {set i 0} {$i<1} {incr i} {
408 db transaction {
409 db eval {INSERT INTO t4 VALUES(5)}
410 continue
411 }
412 }
413 db eval {SELECT * FROM t4}
414} {1 2 3 4 5}
415do_test tcl-10.11 {
416 for {set i 0} {$i<10} {incr i} {
417 db transaction {
418 db eval {INSERT INTO t4 VALUES(6)}
419 break
420 }
421 }
422 db eval {SELECT * FROM t4}
423} {1 2 3 4 5 6}
424do_test tcl-10.12 {
425 set rc [catch {
426 for {set i 0} {$i<10} {incr i} {
427 db transaction {
428 db eval {INSERT INTO t4 VALUES(7)}
429 return
430 }
431 }
432 }]
433} {2}
434do_test tcl-10.13 {
435 db eval {SELECT * FROM t4}
436} {1 2 3 4 5 6 7}
drhc7f269d2005-05-05 10:30:29 +0000437
drh97f2ebc2005-12-10 21:19:04 +0000438do_test tcl-11.1 {
439 db exists {SELECT x,x*2,x+x FROM t4 WHERE x==4}
440} {1}
441do_test tcl-11.2 {
442 db exists {SELECT 0 FROM t4 WHERE x==4}
443} {1}
444do_test tcl-11.3 {
445 db exists {SELECT 1 FROM t4 WHERE x==8}
446} {0}
447
danielk1977161fb792006-01-24 10:58:21 +0000448do_test tcl-12.1 {
449 unset -nocomplain a b c version
450 set version [db version]
451 scan $version "%d.%d.%d" a b c
452 expr $a*1000000 + $b*1000 + $c
453} [sqlite3_libversion_number]
454
drh4f5e80f2007-06-19 17:15:46 +0000455
456# Check to see that when bindings of the form @aaa are used instead
drh1c747812007-06-19 23:01:41 +0000457# of $aaa, that objects are treated as bytearray and are inserted
458# as BLOBs.
drh4f5e80f2007-06-19 17:15:46 +0000459#
460do_test tcl-13.1 {
461 db eval {CREATE TABLE t5(x BLOB)}
462 set x abc123
463 db eval {INSERT INTO t5 VALUES($x)}
464 db eval {SELECT typeof(x) FROM t5}
465} {text}
466do_test tcl-13.2 {
467 binary scan $x H notUsed
468 db eval {
469 DELETE FROM t5;
470 INSERT INTO t5 VALUES($x);
471 SELECT typeof(x) FROM t5;
472 }
473} {text}
474do_test tcl-13.3 {
drh4f5e80f2007-06-19 17:15:46 +0000475 db eval {
476 DELETE FROM t5;
477 INSERT INTO t5 VALUES(@x);
478 SELECT typeof(x) FROM t5;
479 }
480} {blob}
drh1c747812007-06-19 23:01:41 +0000481do_test tcl-13.4 {
482 set y 1234
483 db eval {
484 DELETE FROM t5;
485 INSERT INTO t5 VALUES(@y);
486 SELECT hex(x), typeof(x) FROM t5
487 }
488} {31323334 blob}
489
drh4f5e80f2007-06-19 17:15:46 +0000490
drh960e8c62001-04-03 16:53:21 +0000491finish_test