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 SQLite library. The
12 # focus of this script testing the sqlite_bind API.
14 # $Id: bind.test,v 1.48 2009/07/22 07:27:57 danielk1977 Exp $
17 set testdir [file dirname $argv0]
18 source $testdir/tester.tcl
20 proc sqlite_step {stmt N VALS COLS} {
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]
30 for {set i 0} {$i < [sqlite3_data_count $stmt]} {incr i} {
31 lappend vals [sqlite3_column_text $stmt $i]
38 set DB [sqlite3_connection_pointer db]
39 execsql {CREATE TABLE t1(a,b,c);}
40 set VM [sqlite3_prepare $DB {INSERT INTO t1 VALUES(:1,?,:abc)} -1 TAIL]
44 sqlite3_bind_parameter_count $VM
47 sqlite3_bind_parameter_name $VM 1
50 sqlite3_bind_parameter_name $VM 2
53 sqlite3_bind_parameter_name $VM 3
56 sqlite_step $VM N VALUES COLNAMES
59 execsql {SELECT rowid, * FROM t1}
63 sqlite_bind $VM 1 {test value 1} normal
64 sqlite_step $VM N VALUES COLNAMES
67 execsql {SELECT rowid, * FROM t1}
68 } {1 {} {} {} 2 {test value 1} {} {}}
71 sqlite_bind $VM 3 {'test value 2'} normal
72 sqlite_step $VM N VALUES COLNAMES
75 execsql {SELECT rowid, * FROM t1}
76 } {1 {} {} {} 2 {test value 1} {} {} 3 {test value 1} {} {'test value 2'}}
79 set sqlite_static_bind_value 123
80 sqlite_bind $VM 1 {} static
81 sqlite_bind $VM 2 {abcdefg} normal
82 sqlite_bind $VM 3 {} null
83 execsql {DELETE FROM t1}
84 sqlite_step $VM N VALUES COLNAMES
85 execsql {SELECT rowid, * FROM t1}
89 sqlite_bind $VM 1 {456} normal
90 sqlite_step $VM N VALUES COLNAMES
91 execsql {SELECT rowid, * FROM t1}
92 } {1 123 abcdefg {} 2 456 abcdefg {}}
96 sqlite3_prepare db {INSERT INTO t1 VALUES($abc:123,?,:abc)} -1 TAIL
99 } {1 {(1) near ":123": syntax error}}
102 sqlite3_prepare db {INSERT INTO t1 VALUES(@abc:xyz,?,:abc)} -1 TAIL
105 } {1 {(1) near ":xyz": syntax error}}
111 # Prepare the statement in different ways depending on whether or not
112 # the $var processing is compiled into the library.
119 set VM [sqlite3_prepare $DB {INSERT INTO t1 VALUES($one,$::two,$x(-z-))}\
127 ifcapable {!tclvar} {
132 set VM [sqlite3_prepare $DB {INSERT INTO t1 VALUES(:one,:two,:_)} -1 TX]
141 sqlite3_bind_parameter_count $VM
144 sqlite3_bind_parameter_name $VM 1
147 sqlite3_bind_parameter_name $VM 2
150 sqlite3_bind_parameter_name $VM 3
153 sqlite3_bind_parameter_index $VM $v1
156 sqlite3_bind_parameter_index $VM $v2
159 sqlite3_bind_parameter_index $VM $v3
162 sqlite3_bind_parameter_index $VM {:hi}
167 sqlite3_bind_int $VM 1 123
168 sqlite3_bind_int $VM 2 456
169 sqlite3_bind_int $VM 3 789
170 sqlite_step $VM N VALUES COLNAMES
172 execsql {SELECT rowid, * FROM t1}
175 sqlite3_bind_int $VM 2 -2000000000
176 sqlite3_bind_int $VM 3 2000000000
177 sqlite_step $VM N VALUES COLNAMES
179 execsql {SELECT rowid, * FROM t1}
180 } {1 123 456 789 2 123 -2000000000 2000000000}
182 execsql {SELECT typeof(a), typeof(b), typeof(c) FROM t1}
183 } {integer integer integer integer integer integer}
192 sqlite3_bind_int64 $VM 1 32
193 sqlite3_bind_int64 $VM 2 -2000000000000
194 sqlite3_bind_int64 $VM 3 2000000000000
195 sqlite_step $VM N VALUES COLNAMES
197 execsql {SELECT rowid, * FROM t1}
198 } {1 32 -2000000000000 2000000000000}
200 execsql {SELECT typeof(a), typeof(b), typeof(c) FROM t1}
201 } {integer integer integer}
210 sqlite3_bind_double $VM 1 1234.1234
211 sqlite3_bind_double $VM 2 0.00001
212 sqlite3_bind_double $VM 3 123456789
213 sqlite_step $VM N VALUES COLNAMES
215 set x [execsql {SELECT rowid, * FROM t1}]
216 regsub {1e-005} $x {1e-05} y
218 } {1 1234.1234 1e-05 123456789.0}
220 execsql {SELECT typeof(a), typeof(b), typeof(c) FROM t1}
228 sqlite3_bind_double $VM 1 NaN
229 sqlite3_bind_double $VM 2 1e300
230 sqlite3_bind_double $VM 3 -1e-300
231 sqlite_step $VM N VALUES COLNAMES
233 set x [execsql {SELECT rowid, * FROM t1}]
234 regsub {1e-005} $x {1e-05} y
236 } {1 {} 1e+300 -1e-300}
238 execsql {SELECT typeof(a), typeof(b), typeof(c) FROM t1}
248 sqlite3_bind_null $VM 1
249 sqlite3_bind_null $VM 2
250 sqlite3_bind_null $VM 3
251 sqlite_step $VM N VALUES COLNAMES
253 execsql {SELECT rowid, * FROM t1}
256 execsql {SELECT typeof(a), typeof(b), typeof(c) FROM t1}
266 sqlite3_bind_text $VM 1 hellothere 5
267 sqlite3_bind_text $VM 2 ".." 1
268 sqlite3_bind_text $VM 3 world\000 -1
269 sqlite_step $VM N VALUES COLNAMES
271 execsql {SELECT rowid, * FROM t1}
274 execsql {SELECT typeof(a), typeof(b), typeof(c) FROM t1}
282 # Make sure zeros in a string work.
285 db eval {DELETE FROM t1}
286 sqlite3_bind_text $VM 1 hello\000there\000 12
287 sqlite3_bind_text $VM 2 hello\000there\000 11
288 sqlite3_bind_text $VM 3 hello\000there\000 -1
289 sqlite_step $VM N VALUES COLNAMES
291 execsql {SELECT * FROM t1}
292 } {hello hello hello}
293 set enc [db eval {PRAGMA encoding}]
294 if {$enc=="UTF-8" || $enc==""} {
296 execsql {SELECT hex(a), hex(b), hex(c) FROM t1}
297 } {68656C6C6F00746865726500 68656C6C6F007468657265 68656C6C6F}
298 } elseif {$enc=="UTF-16le"} {
300 execsql {SELECT hex(a), hex(b), hex(c) FROM t1}
301 } {680065006C006C006F000000740068006500720065000000 680065006C006C006F00000074006800650072006500 680065006C006C006F00}
302 } elseif {$enc=="UTF-16be"} {
304 execsql {SELECT hex(a), hex(b), hex(c) FROM t1}
305 } {00680065006C006C006F0000007400680065007200650000 00680065006C006C006F000000740068006500720065 00680065006C006C006F}
308 set "Unknown database encoding: $::enc"
312 execsql {SELECT typeof(a), typeof(b), typeof(c) FROM t1}
323 sqlite3_bind_text16 $VM 1 [encoding convertto unicode hellothere] 10
324 sqlite3_bind_text16 $VM 2 [encoding convertto unicode ""] 0
325 sqlite3_bind_text16 $VM 3 [encoding convertto unicode world] 10
326 sqlite_step $VM N VALUES COLNAMES
328 execsql {SELECT rowid, * FROM t1}
331 execsql {SELECT typeof(a), typeof(b), typeof(c) FROM t1}
334 db eval {DELETE FROM t1}
335 sqlite3_bind_text16 $VM 1 [encoding convertto unicode hi\000yall\000] 16
336 sqlite3_bind_text16 $VM 2 [encoding convertto unicode hi\000yall\000] 14
337 sqlite3_bind_text16 $VM 3 [encoding convertto unicode hi\000yall\000] -1
338 sqlite_step $VM N VALUES COLNAMES
340 execsql {SELECT * FROM t1}
344 execsql {SELECT hex(a), hex(b), hex(c) FROM t1}
345 } {68690079616C6C00 68690079616C6C 6869}
346 } elseif {$enc=="UTF-16le"} {
348 execsql {SELECT hex(a), hex(b), hex(c) FROM t1}
349 } {680069000000790061006C006C000000 680069000000790061006C006C00 68006900}
350 } elseif {$enc=="UTF-16be"} {
352 execsql {SELECT hex(a), hex(b), hex(c) FROM t1}
353 } {00680069000000790061006C006C0000 00680069000000790061006C006C 00680069}
356 execsql {SELECT typeof(a), typeof(b), typeof(c) FROM t1}
360 execsql {DELETE FROM t1;}
363 # Test that the 'out of range' error works.
365 catch { sqlite3_bind_null $VM 0 }
369 } {bind or column index out of range}
372 encoding convertfrom unicode [sqlite3_errmsg16 $DB]
373 } {bind or column index out of range}
376 sqlite3_bind_null $VM 1
380 catch { sqlite3_bind_null $VM 4 }
384 } {bind or column index out of range}
387 encoding convertfrom unicode [sqlite3_errmsg16 $DB]
388 } {bind or column index out of range}
392 catch { sqlite3_bind_blob $VM 0 "abc" 3 }
395 catch { sqlite3_bind_blob $VM 4 "abc" 3 }
398 catch { sqlite3_bind_text $VM 0 "abc" 3 }
402 catch { sqlite3_bind_text16 $VM 4 "abc" 2 }
406 catch { sqlite3_bind_int $VM 0 5 }
409 catch { sqlite3_bind_int $VM 4 5 }
412 catch { sqlite3_bind_double $VM 0 5.0 }
415 catch { sqlite3_bind_double $VM 4 6.0 }
422 set iMaxVar $SQLITE_MAX_VARIABLE_NUMBER
423 set zError "(1) variable number must be between ?1 and ?$iMaxVar"
426 CREATE TABLE t2(a,b,c,d,e,f);
429 sqlite3_prepare $DB {
430 INSERT INTO t2(a) VALUES(?0)
437 sqlite3_prepare $DB "INSERT INTO t2(a) VALUES(?[expr $iMaxVar+1])" -1 TAIL
443 sqlite3_prepare $DB "
444 INSERT INTO t2(a,b) VALUES(?1,?$iMaxVar)
447 sqlite3_bind_parameter_count $VM
449 catch {sqlite3_finalize $VM}
452 sqlite3_prepare $DB "
453 INSERT INTO t2(a,b) VALUES(?2,?[expr $iMaxVar - 1])
456 sqlite3_bind_parameter_count $VM
457 } [expr {$iMaxVar - 1}]
458 catch {sqlite3_finalize $VM}
461 sqlite3_prepare $DB "
462 INSERT INTO t2(a,b,c,d) VALUES(?1,?[expr $iMaxVar - 2],?,?)
465 sqlite3_bind_parameter_count $VM
468 sqlite3_bind_int $VM 1 1
469 sqlite3_bind_int $VM [expr $iMaxVar - 2] 999
470 sqlite3_bind_int $VM [expr $iMaxVar - 1] 1000
471 sqlite3_bind_int $VM $iMaxVar 1001
478 execsql {SELECT * FROM t2}
479 } {1 999 1000 1001 {} {}}
484 sqlite3_prepare $DB {
485 INSERT INTO t2(a,b,c,d,e,f) VALUES(:abc,$abc,:abc,$ab,$abc,:abc)
488 sqlite3_bind_parameter_count $VM
493 ifcapable {!tclvar} {
496 sqlite3_prepare $DB {
497 INSERT INTO t2(a,b,c,d,e,f) VALUES(:abc,:xyz,:abc,:xy,:xyz,:abc)
500 sqlite3_bind_parameter_count $VM
506 sqlite3_bind_parameter_index $VM :abc
509 sqlite3_bind_parameter_index $VM $v1
512 sqlite3_bind_parameter_index $VM $v2
515 sqlite3_bind_parameter_name $VM 1
518 sqlite3_bind_parameter_name $VM 2
521 sqlite3_bind_parameter_name $VM 3
523 do_test bind-10.7.1 {
524 sqlite3_bind_parameter_name 0 1 ;# Ignore if VM is NULL
526 do_test bind-10.7.2 {
527 sqlite3_bind_parameter_name $VM 0 ;# Ignore if index too small
529 do_test bind-10.7.3 {
530 sqlite3_bind_parameter_name $VM 4 ;# Ignore if index is too big
533 sqlite3_bind_int $VM 1 1
534 sqlite3_bind_int $VM 2 2
535 sqlite3_bind_int $VM 3 3
538 do_test bind-10.8.1 {
539 # Binding attempts after program start should fail
541 sqlite3_bind_int $VM 1 1
549 execsql {SELECT * FROM t2}
550 } {1 999 1000 1001 {} {} 1 2 1 3 2 1}
555 # catch {sqlite3_finalize $VM}
557 sqlite3_prepare $DB {
558 INSERT INTO t2(a,b,c,d,e,f) VALUES(:abc,?,?4,:pqr,:abc,?4)
561 sqlite3_bind_parameter_count $VM
563 do_test bind-10.11.1 {
564 sqlite3_bind_parameter_index 0 :xyz ;# ignore NULL VM arguments
567 sqlite3_bind_parameter_index $VM :xyz
570 sqlite3_bind_parameter_index $VM {}
573 sqlite3_bind_parameter_index $VM :pqr
576 sqlite3_bind_parameter_index $VM ?4
579 sqlite3_bind_parameter_name $VM 1
582 sqlite3_bind_parameter_name $VM 2
585 sqlite3_bind_parameter_name $VM 3
588 sqlite3_bind_parameter_name $VM 4
591 sqlite3_bind_parameter_name $VM 5
593 catch {sqlite3_finalize $VM}
595 # Make sure we catch an unterminated "(" in a Tcl-style variable name
599 catchsql {SELECT * FROM sqlite_master WHERE name=$abc(123 and sql NOT NULL;}
600 } {1 {unrecognized token: "$abc(123"}}
603 if {[execsql {pragma encoding}]=="UTF-8"} {
604 # Test the ability to bind text that contains embedded '\000' characters.
605 # Make sure we can recover the entire input string.
609 CREATE TABLE t3(x BLOB);
611 set VM [sqlite3_prepare $DB {INSERT INTO t3 VALUES(?)} -1 TAIL]
612 sqlite_bind $VM 1 not-used blob10
616 SELECT typeof(x), length(x), quote(x),
617 length(cast(x AS BLOB)), quote(cast(x AS BLOB)) FROM t3
619 } {text 3 'abc' 10 X'6162630078797A007071'}
621 sqlite3_create_function $DB
623 SELECT quote(cast(x_coalesce(x) AS blob)) FROM t3
625 } {X'6162630078797A007071'}
628 # Test the operation of sqlite3_clear_bindings
631 set VM [sqlite3_prepare $DB {SELECT ?,?,?} -1 TAIL]
633 list [sqlite3_column_type $VM 0] [sqlite3_column_type $VM 1] \
634 [sqlite3_column_type $VM 2]
638 sqlite3_bind_int $VM 1 1
639 sqlite3_bind_int $VM 2 2
640 sqlite3_bind_int $VM 3 3
642 list [sqlite3_column_type $VM 0] [sqlite3_column_type $VM 1] \
643 [sqlite3_column_type $VM 2]
644 } {INTEGER INTEGER INTEGER}
648 list [sqlite3_column_type $VM 0] [sqlite3_column_type $VM 1] \
649 [sqlite3_column_type $VM 2]
650 } {INTEGER INTEGER INTEGER}
653 sqlite3_clear_bindings $VM
655 list [sqlite3_column_type $VM 0] [sqlite3_column_type $VM 1] \
656 [sqlite3_column_type $VM 2]
660 #--------------------------------------------------------------------
661 # These tests attempt to reproduce bug #3463.
663 proc param_names {db zSql} {
665 set VM [sqlite3_prepare db $zSql -1 TAIL]
666 for {set ii 1} {$ii <= [sqlite3_bind_parameter_count $VM]} {incr ii} {
667 lappend ret [sqlite3_bind_parameter_name $VM $ii]
674 param_names db { SELECT @a, @b }
677 param_names db { SELECT NULL FROM (SELECT NULL) WHERE @a = @b }
680 param_names db { SELECT @a FROM (SELECT NULL) WHERE 1 = @b }
683 param_names db { SELECT @a, @b FROM (SELECT NULL) }
686 #--------------------------------------------------------------------------
687 # Tests of the OP_Variable opcode where P3>1
690 db eval {CREATE TABLE t4(a,b,c,d,e,f,g,h);}
691 set VM [sqlite3_prepare db {
692 INSERT INTO t4(a,b,c,d,f,g,h,e) VALUES(?,?,?,?,?,?,?,?)
694 sqlite3_bind_int $VM 1 1
695 sqlite3_bind_int $VM 2 2
696 sqlite3_bind_int $VM 3 3
697 sqlite3_bind_int $VM 4 4
698 sqlite3_bind_int $VM 5 5
699 sqlite3_bind_int $VM 6 6
700 sqlite3_bind_int $VM 7 7
701 sqlite3_bind_int $VM 8 8
704 db eval {SELECT * FROM t4}
707 db eval {DELETE FROM t4}
708 set VM [sqlite3_prepare db {
709 INSERT INTO t4(a,b,c,d,e,f,g,h) VALUES(?,?,?,?,?,?,?,?)
711 sqlite3_bind_int $VM 1 1
712 sqlite3_bind_int $VM 2 2
713 sqlite3_bind_int $VM 3 3
714 sqlite3_bind_int $VM 4 4
715 sqlite3_bind_int $VM 5 5
716 sqlite3_bind_int $VM 6 6
717 sqlite3_bind_int $VM 7 7
718 sqlite3_bind_int $VM 8 8
721 db eval {SELECT * FROM t4}
724 db eval {DELETE FROM t4}
725 set VM [sqlite3_prepare db {
726 INSERT INTO t4(h,g,f,e,d,c,b,a) VALUES(?,?,?,?,?,?,?,?)
728 sqlite3_bind_int $VM 1 1
729 sqlite3_bind_int $VM 2 2
730 sqlite3_bind_int $VM 3 3
731 sqlite3_bind_int $VM 4 4
732 sqlite3_bind_int $VM 5 5
733 sqlite3_bind_int $VM 6 6
734 sqlite3_bind_int $VM 7 7
735 sqlite3_bind_int $VM 8 8
738 db eval {SELECT * FROM t4}
741 db eval {DELETE FROM t4}
742 set VM [sqlite3_prepare db {
743 INSERT INTO t4(a,b,c,d,e,f,g,h) VALUES(?,?,?,?4,?,?6,?,?)
745 sqlite3_bind_int $VM 1 1
746 sqlite3_bind_int $VM 2 2
747 sqlite3_bind_int $VM 3 3
748 sqlite3_bind_int $VM 4 4
749 sqlite3_bind_int $VM 5 5
750 sqlite3_bind_int $VM 6 6
751 sqlite3_bind_int $VM 7 7
752 sqlite3_bind_int $VM 8 8
755 db eval {SELECT * FROM t4}