blob: e2c58e7f0ec6fd1d104f300399b1188c302ce986 [file] [log] [blame]
drh82a48512003-09-06 22:45:20 +00001# 2003 September 6
2#
3# The author disclaims copyright to this source code. In place of
4# a legal notice, here is a blessing:
5#
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.
9#
10#***********************************************************************
11# This file implements regression tests for SQLite library. The
12# focus of this script testing the sqlite_bind API.
13#
drh6bf89572004-11-03 16:27:01 +000014# $Id: bind.test,v 1.21 2004/11/03 16:27:02 drh Exp $
drh82a48512003-09-06 22:45:20 +000015#
16
17set testdir [file dirname $argv0]
18source $testdir/tester.tcl
19
danielk197717240fd2004-05-26 00:07:25 +000020proc sqlite_step {stmt N VALS COLS} {
21 upvar VALS vals
22 upvar COLS cols
23 set vals [list]
24 set cols [list]
25
26 set rc [sqlite3_step $stmt]
27 for {set i 0} {$i < [sqlite3_column_count $stmt]} {incr i} {
28 lappend cols [sqlite3_column_name $stmt $i]
29 }
30 for {set i 0} {$i < [sqlite3_data_count $stmt]} {incr i} {
drheb2e1762004-05-27 01:53:56 +000031 lappend vals [sqlite3_column_text $stmt $i]
danielk197717240fd2004-05-26 00:07:25 +000032 }
33
34 return $rc
35}
36
drh82a48512003-09-06 22:45:20 +000037do_test bind-1.1 {
38 db close
drhef4ac8f2004-06-19 00:16:31 +000039 set DB [sqlite3 db test.db]
drh2c6674c2004-08-25 04:07:01 +000040 execsql {CREATE TABLE t1(a,b,c);}
41 set VM [sqlite3_prepare $DB {INSERT INTO t1 VALUES(:1,?,:abc)} -1 TAIL]
drh82a48512003-09-06 22:45:20 +000042 set TAIL
43} {}
drh75f6a032004-07-15 14:15:00 +000044do_test bind-1.1.1 {
45 sqlite3_bind_parameter_count $VM
46} 3
drh895d7472004-08-20 16:02:39 +000047do_test bind-1.1.2 {
48 sqlite3_bind_parameter_name $VM 1
drh2c6674c2004-08-25 04:07:01 +000049} {:1}
drh895d7472004-08-20 16:02:39 +000050do_test bind-1.1.3 {
51 sqlite3_bind_parameter_name $VM 2
52} {}
53do_test bind-1.1.4 {
54 sqlite3_bind_parameter_name $VM 3
drh2c6674c2004-08-25 04:07:01 +000055} {:abc}
drh82a48512003-09-06 22:45:20 +000056do_test bind-1.2 {
57 sqlite_step $VM N VALUES COLNAMES
58} {SQLITE_DONE}
59do_test bind-1.3 {
60 execsql {SELECT rowid, * FROM t1}
61} {1 {} {} {}}
62do_test bind-1.4 {
danielk1977106bb232004-05-21 10:08:53 +000063 sqlite3_reset $VM
drh82a48512003-09-06 22:45:20 +000064 sqlite_bind $VM 1 {test value 1} normal
65 sqlite_step $VM N VALUES COLNAMES
66} SQLITE_DONE
67do_test bind-1.5 {
68 execsql {SELECT rowid, * FROM t1}
69} {1 {} {} {} 2 {test value 1} {} {}}
70do_test bind-1.6 {
danielk1977106bb232004-05-21 10:08:53 +000071 sqlite3_reset $VM
drh82a48512003-09-06 22:45:20 +000072 sqlite_bind $VM 3 {'test value 2'} normal
73 sqlite_step $VM N VALUES COLNAMES
74} SQLITE_DONE
75do_test bind-1.7 {
76 execsql {SELECT rowid, * FROM t1}
77} {1 {} {} {} 2 {test value 1} {} {} 3 {test value 1} {} {'test value 2'}}
78do_test bind-1.8 {
danielk1977106bb232004-05-21 10:08:53 +000079 sqlite3_reset $VM
drh82a48512003-09-06 22:45:20 +000080 set sqlite_static_bind_value 123
81 sqlite_bind $VM 1 {} static
82 sqlite_bind $VM 2 {abcdefg} normal
83 sqlite_bind $VM 3 {} null
84 execsql {DELETE FROM t1}
85 sqlite_step $VM N VALUES COLNAMES
86 execsql {SELECT rowid, * FROM t1}
87} {1 123 abcdefg {}}
88do_test bind-1.9 {
danielk1977106bb232004-05-21 10:08:53 +000089 sqlite3_reset $VM
drh82a48512003-09-06 22:45:20 +000090 sqlite_bind $VM 1 {456} normal
91 sqlite_step $VM N VALUES COLNAMES
92 execsql {SELECT rowid, * FROM t1}
93} {1 123 abcdefg {} 2 456 abcdefg {}}
94
drh82a48512003-09-06 22:45:20 +000095do_test bind-1.99 {
danielk1977106bb232004-05-21 10:08:53 +000096 sqlite3_finalize $VM
danielk19773cf86062004-05-26 10:11:05 +000097} SQLITE_OK
drh82a48512003-09-06 22:45:20 +000098
drh6bf89572004-11-03 16:27:01 +000099# Prepare the statement in different ways depending on whether or not
100# the $var processing is compiled into the library.
101#
102ifcapable {tclvar} {
103 do_test bind-2.1 {
104 execsql {
105 DELETE FROM t1;
106 }
107 set VM [sqlite3_prepare $DB {INSERT INTO t1 VALUES($one,$::two,${x})} -1 TX]
108 set TX
109 } {}
110 set v1 {$one}
111 set v2 {$::two}
112 set v3 {${x}}
113}
114ifcapable {!tclvar} {
115 do_test bind-2.1 {
116 execsql {
117 DELETE FROM t1;
118 }
119 set VM [sqlite3_prepare $DB {INSERT INTO t1 VALUES(:one,:two,:_)} -1 TX]
120 set TX
121 } {}
122 set v1 {:one}
123 set v2 {:two}
124 set v3 {:_}
125}
126
drh895d7472004-08-20 16:02:39 +0000127do_test bind-2.1.1 {
128 sqlite3_bind_parameter_count $VM
129} 3
130do_test bind-2.1.2 {
131 sqlite3_bind_parameter_name $VM 1
drh6bf89572004-11-03 16:27:01 +0000132} $v1
drh895d7472004-08-20 16:02:39 +0000133do_test bind-2.1.3 {
134 sqlite3_bind_parameter_name $VM 2
drh6bf89572004-11-03 16:27:01 +0000135} $v2
drh895d7472004-08-20 16:02:39 +0000136do_test bind-2.1.4 {
137 sqlite3_bind_parameter_name $VM 3
drh6bf89572004-11-03 16:27:01 +0000138} $v3
drhfa6bc002004-09-07 16:19:52 +0000139do_test bind-2.1.5 {
drh6bf89572004-11-03 16:27:01 +0000140 sqlite3_bind_parameter_index $VM $v1
drhfa6bc002004-09-07 16:19:52 +0000141} 1
142do_test bind-2.1.6 {
drh6bf89572004-11-03 16:27:01 +0000143 sqlite3_bind_parameter_index $VM $v2
drhfa6bc002004-09-07 16:19:52 +0000144} 2
145do_test bind-2.1.7 {
drh6bf89572004-11-03 16:27:01 +0000146 sqlite3_bind_parameter_index $VM $v3
drhfa6bc002004-09-07 16:19:52 +0000147} 3
148do_test bind-2.1.8 {
149 sqlite3_bind_parameter_index $VM {:hi}
150} 0
danielk197751e3d8e2004-05-20 01:12:34 +0000151
152# 32 bit Integers
153do_test bind-2.2 {
drh241db312004-06-22 12:46:53 +0000154 sqlite3_bind_int $VM 1 123
155 sqlite3_bind_int $VM 2 456
156 sqlite3_bind_int $VM 3 789
danielk197751e3d8e2004-05-20 01:12:34 +0000157 sqlite_step $VM N VALUES COLNAMES
danielk1977106bb232004-05-21 10:08:53 +0000158 sqlite3_reset $VM
danielk197751e3d8e2004-05-20 01:12:34 +0000159 execsql {SELECT rowid, * FROM t1}
160} {1 123 456 789}
161do_test bind-2.3 {
drh241db312004-06-22 12:46:53 +0000162 sqlite3_bind_int $VM 2 -2000000000
163 sqlite3_bind_int $VM 3 2000000000
danielk197751e3d8e2004-05-20 01:12:34 +0000164 sqlite_step $VM N VALUES COLNAMES
danielk1977106bb232004-05-21 10:08:53 +0000165 sqlite3_reset $VM
danielk197751e3d8e2004-05-20 01:12:34 +0000166 execsql {SELECT rowid, * FROM t1}
167} {1 123 456 789 2 123 -2000000000 2000000000}
168do_test bind-2.4 {
danielk197735bb9d02004-05-24 12:55:54 +0000169 execsql {SELECT typeof(a), typeof(b), typeof(c) FROM t1}
170} {integer integer integer integer integer integer}
danielk197751e3d8e2004-05-20 01:12:34 +0000171do_test bind-2.5 {
172 execsql {
173 DELETE FROM t1;
174 }
175} {}
176
177# 64 bit Integers
178do_test bind-3.1 {
179 sqlite3_bind_int64 $VM 1 32
180 sqlite3_bind_int64 $VM 2 -2000000000000
181 sqlite3_bind_int64 $VM 3 2000000000000
182 sqlite_step $VM N VALUES COLNAMES
danielk1977106bb232004-05-21 10:08:53 +0000183 sqlite3_reset $VM
danielk197751e3d8e2004-05-20 01:12:34 +0000184 execsql {SELECT rowid, * FROM t1}
185} {1 32 -2000000000000 2000000000000}
186do_test bind-3.2 {
danielk197735bb9d02004-05-24 12:55:54 +0000187 execsql {SELECT typeof(a), typeof(b), typeof(c) FROM t1}
188} {integer integer integer}
danielk197751e3d8e2004-05-20 01:12:34 +0000189do_test bind-3.3 {
190 execsql {
191 DELETE FROM t1;
192 }
193} {}
194
195# Doubles
196do_test bind-4.1 {
197 sqlite3_bind_double $VM 1 1234.1234
198 sqlite3_bind_double $VM 2 0.00001
199 sqlite3_bind_double $VM 3 123456789
200 sqlite_step $VM N VALUES COLNAMES
danielk1977106bb232004-05-21 10:08:53 +0000201 sqlite3_reset $VM
danielk197751e3d8e2004-05-20 01:12:34 +0000202 execsql {SELECT rowid, * FROM t1}
drh92febd92004-08-20 18:34:20 +0000203} {1 1234.1234 1e-05 123456789.0}
danielk197751e3d8e2004-05-20 01:12:34 +0000204do_test bind-4.2 {
danielk197735bb9d02004-05-24 12:55:54 +0000205 execsql {SELECT typeof(a), typeof(b), typeof(c) FROM t1}
206} {real real real}
danielk197751e3d8e2004-05-20 01:12:34 +0000207do_test bind-4.3 {
208 execsql {
209 DELETE FROM t1;
210 }
211} {}
212
213# NULL
214do_test bind-5.1 {
215 sqlite3_bind_null $VM 1
216 sqlite3_bind_null $VM 2
217 sqlite3_bind_null $VM 3
218 sqlite_step $VM N VALUES COLNAMES
danielk1977106bb232004-05-21 10:08:53 +0000219 sqlite3_reset $VM
danielk197751e3d8e2004-05-20 01:12:34 +0000220 execsql {SELECT rowid, * FROM t1}
221} {1 {} {} {}}
222do_test bind-5.2 {
danielk197735bb9d02004-05-24 12:55:54 +0000223 execsql {SELECT typeof(a), typeof(b), typeof(c) FROM t1}
224} {null null null}
danielk197751e3d8e2004-05-20 01:12:34 +0000225do_test bind-5.3 {
226 execsql {
227 DELETE FROM t1;
228 }
229} {}
230
231# UTF-8 text
232do_test bind-6.1 {
233 sqlite3_bind_text $VM 1 hellothere 5
danielk1977c572ef72004-05-27 09:28:41 +0000234 sqlite3_bind_text $VM 2 ".." 1
danielk197751e3d8e2004-05-20 01:12:34 +0000235 sqlite3_bind_text $VM 3 world -1
236 sqlite_step $VM N VALUES COLNAMES
danielk1977106bb232004-05-21 10:08:53 +0000237 sqlite3_reset $VM
danielk197751e3d8e2004-05-20 01:12:34 +0000238 execsql {SELECT rowid, * FROM t1}
239} {1 hello . world}
240do_test bind-6.2 {
danielk197735bb9d02004-05-24 12:55:54 +0000241 execsql {SELECT typeof(a), typeof(b), typeof(c) FROM t1}
242} {text text text}
danielk197751e3d8e2004-05-20 01:12:34 +0000243do_test bind-6.3 {
244 execsql {
245 DELETE FROM t1;
246 }
247} {}
248
249# UTF-16 text
250do_test bind-7.1 {
251 sqlite3_bind_text16 $VM 1 [encoding convertto unicode hellothere] 10
252 sqlite3_bind_text16 $VM 2 [encoding convertto unicode ""] 0
253 sqlite3_bind_text16 $VM 3 [encoding convertto unicode world] 10
254 sqlite_step $VM N VALUES COLNAMES
danielk1977106bb232004-05-21 10:08:53 +0000255 sqlite3_reset $VM
danielk197751e3d8e2004-05-20 01:12:34 +0000256 execsql {SELECT rowid, * FROM t1}
257} {1 hello {} world}
258do_test bind-7.2 {
danielk197735bb9d02004-05-24 12:55:54 +0000259 execsql {SELECT typeof(a), typeof(b), typeof(c) FROM t1}
260} {text text text}
danielk197751e3d8e2004-05-20 01:12:34 +0000261do_test bind-7.3 {
262 execsql {
263 DELETE FROM t1;
264 }
265} {}
266
danielk19776622cce2004-05-20 11:00:52 +0000267# Test that the 'out of range' error works.
268do_test bind-8.1 {
269 catch { sqlite3_bind_null $VM 0 }
270} {1}
271do_test bind-8.2 {
272 sqlite3_errmsg $DB
273} {bind index out of range}
274do_test bind-8.3 {
275 encoding convertfrom unicode [sqlite3_errmsg16 $DB]
276} {bind index out of range}
277do_test bind-8.4 {
278 sqlite3_bind_null $VM 1
279 sqlite3_errmsg $DB
280} {not an error}
281do_test bind-8.5 {
282 catch { sqlite3_bind_null $VM 4 }
283} {1}
284do_test bind-8.6 {
285 sqlite3_errmsg $DB
286} {bind index out of range}
287do_test bind-8.7 {
288 encoding convertfrom unicode [sqlite3_errmsg16 $DB]
289} {bind index out of range}
290
danielk1977f4618892004-06-28 13:09:11 +0000291do_test bind-8.8 {
292 catch { sqlite3_bind_blob $VM 0 "abc" 3 }
293} {1}
294do_test bind-8.9 {
295 catch { sqlite3_bind_blob $VM 4 "abc" 3 }
296} {1}
297do_test bind-8.10 {
298 catch { sqlite3_bind_text $VM 0 "abc" 3 }
299} {1}
300do_test bind-8.11 {
301 catch { sqlite3_bind_text16 $VM 4 "abc" 2 }
302} {1}
303do_test bind-8.12 {
304 catch { sqlite3_bind_int $VM 0 5 }
305} {1}
306do_test bind-8.13 {
307 catch { sqlite3_bind_int $VM 4 5 }
308} {1}
309do_test bind-8.14 {
310 catch { sqlite3_bind_double $VM 0 5.0 }
311} {1}
312do_test bind-8.15 {
313 catch { sqlite3_bind_double $VM 4 6.0 }
314} {1}
danielk19776622cce2004-05-20 11:00:52 +0000315
drhfa6bc002004-09-07 16:19:52 +0000316do_test bind-8.99 {
danielk1977106bb232004-05-21 10:08:53 +0000317 sqlite3_finalize $VM
danielk19773cf86062004-05-26 10:11:05 +0000318} SQLITE_OK
danielk197751e3d8e2004-05-20 01:12:34 +0000319
drhfa6bc002004-09-07 16:19:52 +0000320do_test bind-9.1 {
321 execsql {
322 CREATE TABLE t2(a,b,c,d,e,f);
323 }
324 set rc [catch {
325 sqlite3_prepare $DB {
326 INSERT INTO t2(a) VALUES(?0)
327 } -1 TAIL
328 } msg]
329 lappend rc $msg
330} {1 {(1) variable number must be between ?1 and ?999}}
331do_test bind-9.2 {
332 set rc [catch {
333 sqlite3_prepare $DB {
334 INSERT INTO t2(a) VALUES(?1000)
335 } -1 TAIL
336 } msg]
337 lappend rc $msg
338} {1 {(1) variable number must be between ?1 and ?999}}
339do_test bind-9.3 {
340 set VM [
341 sqlite3_prepare $DB {
342 INSERT INTO t2(a,b) VALUES(?1,?999)
343 } -1 TAIL
344 ]
345 sqlite3_bind_parameter_count $VM
346} {999}
347catch {sqlite3_finalize $VM}
348do_test bind-9.4 {
349 set VM [
350 sqlite3_prepare $DB {
351 INSERT INTO t2(a,b,c,d) VALUES(?1,?999,?,?)
352 } -1 TAIL
353 ]
354 sqlite3_bind_parameter_count $VM
355} {1001}
356do_test bind-9.5 {
357 sqlite3_bind_int $VM 1 1
358 sqlite3_bind_int $VM 999 999
359 sqlite3_bind_int $VM 1000 1000
360 sqlite3_bind_int $VM 1001 1001
361 sqlite3_step $VM
362} SQLITE_DONE
363do_test bind-9.6 {
364 sqlite3_finalize $VM
365} SQLITE_OK
366do_test bind-9.7 {
367 execsql {SELECT * FROM t2}
368} {1 999 1000 1001 {} {}}
danielk197751e3d8e2004-05-20 01:12:34 +0000369
drh6bf89572004-11-03 16:27:01 +0000370ifcapable {tclvar} {
371 do_test bind-10.1 {
372 catch {sqlite3_finalize $VM}
373 set VM [
374 sqlite3_prepare $DB {
375 INSERT INTO t2(a,b,c,d,e,f) VALUES(:abc,$abc,:abc,$ab,$abc,:abc)
376 } -1 TAIL
377 ]
378 sqlite3_bind_parameter_count $VM
379 } 3
380 set v1 {$abc}
381 set v2 {$ab}
382}
383ifcapable {!tclvar} {
384 do_test bind-10.1 {
385 catch {sqlite3_finalize $VM}
386 set VM [
387 sqlite3_prepare $DB {
388 INSERT INTO t2(a,b,c,d,e,f) VALUES(:abc,:xyz,:abc,:xy,:xyz,:abc)
389 } -1 TAIL
390 ]
391 sqlite3_bind_parameter_count $VM
392 } 3
393 set v1 {:xyz}
394 set v2 {:xy}
395}
drhfa6bc002004-09-07 16:19:52 +0000396do_test bind-10.2 {
397 sqlite3_bind_parameter_index $VM :abc
398} 1
399do_test bind-10.3 {
drh6bf89572004-11-03 16:27:01 +0000400 sqlite3_bind_parameter_index $VM $v1
drhfa6bc002004-09-07 16:19:52 +0000401} 2
402do_test bind-10.4 {
drh6bf89572004-11-03 16:27:01 +0000403 sqlite3_bind_parameter_index $VM $v2
drhfa6bc002004-09-07 16:19:52 +0000404} 3
405do_test bind-10.5 {
406 sqlite3_bind_parameter_name $VM 1
407} :abc
408do_test bind-10.6 {
409 sqlite3_bind_parameter_name $VM 2
drh6bf89572004-11-03 16:27:01 +0000410} $v1
drhfa6bc002004-09-07 16:19:52 +0000411do_test bind-10.7 {
412 sqlite3_bind_parameter_name $VM 3
drh6bf89572004-11-03 16:27:01 +0000413} $v2
drhfa6bc002004-09-07 16:19:52 +0000414do_test bind-10.8 {
415 sqlite3_bind_int $VM 1 1
416 sqlite3_bind_int $VM 2 2
417 sqlite3_bind_int $VM 3 3
418 sqlite3_step $VM
419} SQLITE_DONE
420do_test bind-10.9 {
421 sqlite3_finalize $VM
422} SQLITE_OK
423do_test bind-10.10 {
424 execsql {SELECT * FROM t2}
425} {1 999 1000 1001 {} {} 1 2 1 3 2 1}
drh82a48512003-09-06 22:45:20 +0000426
drh971a7c82004-09-24 12:48:12 +0000427# Ticket #918
428#
429do_test bind-10.11 {
430 catch {sqlite3_finalize $VM}
431 set VM [
432 sqlite3_prepare $DB {
433 INSERT INTO t2(a,b,c,d,e,f) VALUES(:abc,?,?4,:pqr,:abc,?4)
434 } -1 TAIL
435 ]
436 sqlite3_bind_parameter_count $VM
437} 5
438do_test bind-10.12 {
439 sqlite3_bind_parameter_index $VM :xyz
440} 0
441do_test bind-10.13 {
442 sqlite3_bind_parameter_index $VM {}
443} 0
444do_test bind-10.14 {
445 sqlite3_bind_parameter_index $VM :pqr
446} 5
447do_test bind-10.15 {
448 sqlite3_bind_parameter_index $VM ?4
449} 4
450do_test bind-10.16 {
451 sqlite3_bind_parameter_name $VM 1
452} :abc
453do_test bind-10.16 {
454 sqlite3_bind_parameter_name $VM 2
455} {}
456do_test bind-10.16 {
457 sqlite3_bind_parameter_name $VM 3
458} {}
459do_test bind-10.16 {
460 sqlite3_bind_parameter_name $VM 4
461} {?4}
462do_test bind-10.16 {
463 sqlite3_bind_parameter_name $VM 5
464} :pqr
465catch {sqlite3_finalize $VM}
466
drh82a48512003-09-06 22:45:20 +0000467finish_test