blob: bff7001ed5005bb7102caf35c5ed9f18f036f8cd [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#
drhb08153d2004-11-20 20:18:55 +000014# $Id: bind.test,v 1.23 2004/11/20 20:18:55 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
drh6c626082004-11-14 21:56:29 +0000250ifcapable {utf16} {
251 do_test bind-7.1 {
252 sqlite3_bind_text16 $VM 1 [encoding convertto unicode hellothere] 10
253 sqlite3_bind_text16 $VM 2 [encoding convertto unicode ""] 0
254 sqlite3_bind_text16 $VM 3 [encoding convertto unicode world] 10
255 sqlite_step $VM N VALUES COLNAMES
256 sqlite3_reset $VM
257 execsql {SELECT rowid, * FROM t1}
258 } {1 hello {} world}
259 do_test bind-7.2 {
260 execsql {SELECT typeof(a), typeof(b), typeof(c) FROM t1}
261 } {text text text}
262}
danielk197751e3d8e2004-05-20 01:12:34 +0000263do_test bind-7.3 {
264 execsql {
265 DELETE FROM t1;
266 }
267} {}
268
danielk19776622cce2004-05-20 11:00:52 +0000269# Test that the 'out of range' error works.
270do_test bind-8.1 {
271 catch { sqlite3_bind_null $VM 0 }
272} {1}
273do_test bind-8.2 {
274 sqlite3_errmsg $DB
drhb08153d2004-11-20 20:18:55 +0000275} {bind or column index out of range}
drh6c626082004-11-14 21:56:29 +0000276ifcapable {utf16} {
277 do_test bind-8.3 {
278 encoding convertfrom unicode [sqlite3_errmsg16 $DB]
drhb08153d2004-11-20 20:18:55 +0000279 } {bind or column index out of range}
drh6c626082004-11-14 21:56:29 +0000280}
danielk19776622cce2004-05-20 11:00:52 +0000281do_test bind-8.4 {
282 sqlite3_bind_null $VM 1
283 sqlite3_errmsg $DB
284} {not an error}
285do_test bind-8.5 {
286 catch { sqlite3_bind_null $VM 4 }
287} {1}
288do_test bind-8.6 {
289 sqlite3_errmsg $DB
drhb08153d2004-11-20 20:18:55 +0000290} {bind or column index out of range}
drh6c626082004-11-14 21:56:29 +0000291ifcapable {utf16} {
292 do_test bind-8.7 {
293 encoding convertfrom unicode [sqlite3_errmsg16 $DB]
drhb08153d2004-11-20 20:18:55 +0000294 } {bind or column index out of range}
drh6c626082004-11-14 21:56:29 +0000295}
danielk19776622cce2004-05-20 11:00:52 +0000296
danielk1977f4618892004-06-28 13:09:11 +0000297do_test bind-8.8 {
298 catch { sqlite3_bind_blob $VM 0 "abc" 3 }
299} {1}
300do_test bind-8.9 {
301 catch { sqlite3_bind_blob $VM 4 "abc" 3 }
302} {1}
303do_test bind-8.10 {
304 catch { sqlite3_bind_text $VM 0 "abc" 3 }
305} {1}
drh6c626082004-11-14 21:56:29 +0000306ifcapable {utf16} {
307 do_test bind-8.11 {
308 catch { sqlite3_bind_text16 $VM 4 "abc" 2 }
309 } {1}
310}
danielk1977f4618892004-06-28 13:09:11 +0000311do_test bind-8.12 {
312 catch { sqlite3_bind_int $VM 0 5 }
313} {1}
314do_test bind-8.13 {
315 catch { sqlite3_bind_int $VM 4 5 }
316} {1}
317do_test bind-8.14 {
318 catch { sqlite3_bind_double $VM 0 5.0 }
319} {1}
320do_test bind-8.15 {
321 catch { sqlite3_bind_double $VM 4 6.0 }
322} {1}
danielk19776622cce2004-05-20 11:00:52 +0000323
drhfa6bc002004-09-07 16:19:52 +0000324do_test bind-8.99 {
danielk1977106bb232004-05-21 10:08:53 +0000325 sqlite3_finalize $VM
danielk19773cf86062004-05-26 10:11:05 +0000326} SQLITE_OK
danielk197751e3d8e2004-05-20 01:12:34 +0000327
drhfa6bc002004-09-07 16:19:52 +0000328do_test bind-9.1 {
329 execsql {
330 CREATE TABLE t2(a,b,c,d,e,f);
331 }
332 set rc [catch {
333 sqlite3_prepare $DB {
334 INSERT INTO t2(a) VALUES(?0)
335 } -1 TAIL
336 } msg]
337 lappend rc $msg
338} {1 {(1) variable number must be between ?1 and ?999}}
339do_test bind-9.2 {
340 set rc [catch {
341 sqlite3_prepare $DB {
342 INSERT INTO t2(a) VALUES(?1000)
343 } -1 TAIL
344 } msg]
345 lappend rc $msg
346} {1 {(1) variable number must be between ?1 and ?999}}
347do_test bind-9.3 {
348 set VM [
349 sqlite3_prepare $DB {
350 INSERT INTO t2(a,b) VALUES(?1,?999)
351 } -1 TAIL
352 ]
353 sqlite3_bind_parameter_count $VM
354} {999}
355catch {sqlite3_finalize $VM}
356do_test bind-9.4 {
357 set VM [
358 sqlite3_prepare $DB {
359 INSERT INTO t2(a,b,c,d) VALUES(?1,?999,?,?)
360 } -1 TAIL
361 ]
362 sqlite3_bind_parameter_count $VM
363} {1001}
364do_test bind-9.5 {
365 sqlite3_bind_int $VM 1 1
366 sqlite3_bind_int $VM 999 999
367 sqlite3_bind_int $VM 1000 1000
368 sqlite3_bind_int $VM 1001 1001
369 sqlite3_step $VM
370} SQLITE_DONE
371do_test bind-9.6 {
372 sqlite3_finalize $VM
373} SQLITE_OK
374do_test bind-9.7 {
375 execsql {SELECT * FROM t2}
376} {1 999 1000 1001 {} {}}
danielk197751e3d8e2004-05-20 01:12:34 +0000377
drh6bf89572004-11-03 16:27:01 +0000378ifcapable {tclvar} {
379 do_test bind-10.1 {
380 catch {sqlite3_finalize $VM}
381 set VM [
382 sqlite3_prepare $DB {
383 INSERT INTO t2(a,b,c,d,e,f) VALUES(:abc,$abc,:abc,$ab,$abc,:abc)
384 } -1 TAIL
385 ]
386 sqlite3_bind_parameter_count $VM
387 } 3
388 set v1 {$abc}
389 set v2 {$ab}
390}
391ifcapable {!tclvar} {
392 do_test bind-10.1 {
393 catch {sqlite3_finalize $VM}
394 set VM [
395 sqlite3_prepare $DB {
396 INSERT INTO t2(a,b,c,d,e,f) VALUES(:abc,:xyz,:abc,:xy,:xyz,:abc)
397 } -1 TAIL
398 ]
399 sqlite3_bind_parameter_count $VM
400 } 3
401 set v1 {:xyz}
402 set v2 {:xy}
403}
drhfa6bc002004-09-07 16:19:52 +0000404do_test bind-10.2 {
405 sqlite3_bind_parameter_index $VM :abc
406} 1
407do_test bind-10.3 {
drh6bf89572004-11-03 16:27:01 +0000408 sqlite3_bind_parameter_index $VM $v1
drhfa6bc002004-09-07 16:19:52 +0000409} 2
410do_test bind-10.4 {
drh6bf89572004-11-03 16:27:01 +0000411 sqlite3_bind_parameter_index $VM $v2
drhfa6bc002004-09-07 16:19:52 +0000412} 3
413do_test bind-10.5 {
414 sqlite3_bind_parameter_name $VM 1
415} :abc
416do_test bind-10.6 {
417 sqlite3_bind_parameter_name $VM 2
drh6bf89572004-11-03 16:27:01 +0000418} $v1
drhfa6bc002004-09-07 16:19:52 +0000419do_test bind-10.7 {
420 sqlite3_bind_parameter_name $VM 3
drh6bf89572004-11-03 16:27:01 +0000421} $v2
drhfa6bc002004-09-07 16:19:52 +0000422do_test bind-10.8 {
423 sqlite3_bind_int $VM 1 1
424 sqlite3_bind_int $VM 2 2
425 sqlite3_bind_int $VM 3 3
426 sqlite3_step $VM
427} SQLITE_DONE
428do_test bind-10.9 {
429 sqlite3_finalize $VM
430} SQLITE_OK
431do_test bind-10.10 {
432 execsql {SELECT * FROM t2}
433} {1 999 1000 1001 {} {} 1 2 1 3 2 1}
drh82a48512003-09-06 22:45:20 +0000434
drh971a7c82004-09-24 12:48:12 +0000435# Ticket #918
436#
437do_test bind-10.11 {
438 catch {sqlite3_finalize $VM}
439 set VM [
440 sqlite3_prepare $DB {
441 INSERT INTO t2(a,b,c,d,e,f) VALUES(:abc,?,?4,:pqr,:abc,?4)
442 } -1 TAIL
443 ]
444 sqlite3_bind_parameter_count $VM
445} 5
446do_test bind-10.12 {
447 sqlite3_bind_parameter_index $VM :xyz
448} 0
449do_test bind-10.13 {
450 sqlite3_bind_parameter_index $VM {}
451} 0
452do_test bind-10.14 {
453 sqlite3_bind_parameter_index $VM :pqr
454} 5
455do_test bind-10.15 {
456 sqlite3_bind_parameter_index $VM ?4
457} 4
458do_test bind-10.16 {
459 sqlite3_bind_parameter_name $VM 1
460} :abc
461do_test bind-10.16 {
462 sqlite3_bind_parameter_name $VM 2
463} {}
464do_test bind-10.16 {
465 sqlite3_bind_parameter_name $VM 3
466} {}
467do_test bind-10.16 {
468 sqlite3_bind_parameter_name $VM 4
469} {?4}
470do_test bind-10.16 {
471 sqlite3_bind_parameter_name $VM 5
472} :pqr
473catch {sqlite3_finalize $VM}
474
drh82a48512003-09-06 22:45:20 +0000475finish_test