xref: /sqlite-3.40.0/test/bind.test (revision dddca286)
182a4851aSdrh# 2003 September 6
282a4851aSdrh#
382a4851aSdrh# The author disclaims copyright to this source code.  In place of
482a4851aSdrh# a legal notice, here is a blessing:
582a4851aSdrh#
682a4851aSdrh#    May you do good and not evil.
782a4851aSdrh#    May you find forgiveness for yourself and forgive others.
882a4851aSdrh#    May you share freely, never taking more than you give.
982a4851aSdrh#
1082a4851aSdrh#***********************************************************************
1182a4851aSdrh# This file implements regression tests for SQLite library.  The
1282a4851aSdrh# focus of this script testing the sqlite_bind API.
1382a4851aSdrh#
14*dddca286Sdrh# $Id: bind.test,v 1.36 2006/01/03 00:33:50 drh Exp $
1582a4851aSdrh#
1682a4851aSdrh
1782a4851aSdrhset testdir [file dirname $argv0]
1882a4851aSdrhsource $testdir/tester.tcl
1982a4851aSdrh
2017240fd9Sdanielk1977proc sqlite_step {stmt N VALS COLS} {
2117240fd9Sdanielk1977  upvar VALS vals
2217240fd9Sdanielk1977  upvar COLS cols
2317240fd9Sdanielk1977  set vals [list]
2417240fd9Sdanielk1977  set cols [list]
2517240fd9Sdanielk1977
2617240fd9Sdanielk1977  set rc [sqlite3_step $stmt]
2717240fd9Sdanielk1977  for {set i 0} {$i < [sqlite3_column_count $stmt]} {incr i} {
2817240fd9Sdanielk1977    lappend cols [sqlite3_column_name $stmt $i]
2917240fd9Sdanielk1977  }
3017240fd9Sdanielk1977  for {set i 0} {$i < [sqlite3_data_count $stmt]} {incr i} {
31eb2e176aSdrh    lappend vals [sqlite3_column_text $stmt $i]
3217240fd9Sdanielk1977  }
3317240fd9Sdanielk1977
3417240fd9Sdanielk1977  return $rc
3517240fd9Sdanielk1977}
3617240fd9Sdanielk1977
3782a4851aSdrhdo_test bind-1.1 {
38*dddca286Sdrh  set DB [sqlite3_connection_pointer db]
392c6674cfSdrh  execsql {CREATE TABLE t1(a,b,c);}
402c6674cfSdrh  set VM [sqlite3_prepare $DB {INSERT INTO t1 VALUES(:1,?,:abc)} -1 TAIL]
4182a4851aSdrh  set TAIL
4282a4851aSdrh} {}
4375f6a032Sdrhdo_test bind-1.1.1 {
4475f6a032Sdrh  sqlite3_bind_parameter_count $VM
4575f6a032Sdrh} 3
46895d7472Sdrhdo_test bind-1.1.2 {
47895d7472Sdrh  sqlite3_bind_parameter_name $VM 1
482c6674cfSdrh} {:1}
49895d7472Sdrhdo_test bind-1.1.3 {
50895d7472Sdrh  sqlite3_bind_parameter_name $VM 2
51895d7472Sdrh} {}
52895d7472Sdrhdo_test bind-1.1.4 {
53895d7472Sdrh  sqlite3_bind_parameter_name $VM 3
542c6674cfSdrh} {:abc}
5582a4851aSdrhdo_test bind-1.2 {
5682a4851aSdrh  sqlite_step $VM N VALUES COLNAMES
5782a4851aSdrh} {SQLITE_DONE}
5882a4851aSdrhdo_test bind-1.3 {
5982a4851aSdrh  execsql {SELECT rowid, * FROM t1}
6082a4851aSdrh} {1 {} {} {}}
6182a4851aSdrhdo_test bind-1.4 {
62106bb236Sdanielk1977  sqlite3_reset $VM
6382a4851aSdrh  sqlite_bind $VM 1 {test value 1} normal
6482a4851aSdrh  sqlite_step $VM N VALUES COLNAMES
6582a4851aSdrh} SQLITE_DONE
6682a4851aSdrhdo_test bind-1.5 {
6782a4851aSdrh  execsql {SELECT rowid, * FROM t1}
6882a4851aSdrh} {1 {} {} {} 2 {test value 1} {} {}}
6982a4851aSdrhdo_test bind-1.6 {
70106bb236Sdanielk1977  sqlite3_reset $VM
7182a4851aSdrh  sqlite_bind $VM 3 {'test value 2'} normal
7282a4851aSdrh  sqlite_step $VM N VALUES COLNAMES
7382a4851aSdrh} SQLITE_DONE
7482a4851aSdrhdo_test bind-1.7 {
7582a4851aSdrh  execsql {SELECT rowid, * FROM t1}
7682a4851aSdrh} {1 {} {} {} 2 {test value 1} {} {} 3 {test value 1} {} {'test value 2'}}
7782a4851aSdrhdo_test bind-1.8 {
78106bb236Sdanielk1977  sqlite3_reset $VM
7982a4851aSdrh  set sqlite_static_bind_value 123
8082a4851aSdrh  sqlite_bind $VM 1 {} static
8182a4851aSdrh  sqlite_bind $VM 2 {abcdefg} normal
8282a4851aSdrh  sqlite_bind $VM 3 {} null
8382a4851aSdrh  execsql {DELETE FROM t1}
8482a4851aSdrh  sqlite_step $VM N VALUES COLNAMES
8582a4851aSdrh  execsql {SELECT rowid, * FROM t1}
8682a4851aSdrh} {1 123 abcdefg {}}
8782a4851aSdrhdo_test bind-1.9 {
88106bb236Sdanielk1977  sqlite3_reset $VM
8982a4851aSdrh  sqlite_bind $VM 1 {456} normal
9082a4851aSdrh  sqlite_step $VM N VALUES COLNAMES
9182a4851aSdrh  execsql {SELECT rowid, * FROM t1}
9282a4851aSdrh} {1 123 abcdefg {} 2 456 abcdefg {}}
9382a4851aSdrh
9482a4851aSdrhdo_test bind-1.99 {
95106bb236Sdanielk1977  sqlite3_finalize $VM
963cf86063Sdanielk1977} SQLITE_OK
9782a4851aSdrh
986bf89570Sdrh# Prepare the statement in different ways depending on whether or not
996bf89570Sdrh# the $var processing is compiled into the library.
1006bf89570Sdrh#
1016bf89570Sdrhifcapable {tclvar} {
10251e3d8e2Sdanielk1977  do_test bind-2.1 {
10351e3d8e2Sdanielk1977    execsql {
10451e3d8e2Sdanielk1977      DELETE FROM t1;
10551e3d8e2Sdanielk1977    }
106288d37f1Sdrh    set VM [sqlite3_prepare $DB {INSERT INTO t1 VALUES($one,$::two,$x(-z-))}\
10748e5aa27Sdrh            -1 TX]
1086bf89570Sdrh    set TX
10951e3d8e2Sdanielk1977  } {}
1106bf89570Sdrh  set v1 {$one}
1116bf89570Sdrh  set v2 {$::two}
112288d37f1Sdrh  set v3 {$x(-z-)}
1136bf89570Sdrh}
1146bf89570Sdrhifcapable {!tclvar} {
1156bf89570Sdrh  do_test bind-2.1 {
1166bf89570Sdrh    execsql {
1176bf89570Sdrh      DELETE FROM t1;
1186bf89570Sdrh    }
1196bf89570Sdrh    set VM [sqlite3_prepare $DB {INSERT INTO t1 VALUES(:one,:two,:_)} -1 TX]
1206bf89570Sdrh    set TX
1216bf89570Sdrh  } {}
1226bf89570Sdrh  set v1 {:one}
1236bf89570Sdrh  set v2 {:two}
1246bf89570Sdrh  set v3 {:_}
1256bf89570Sdrh}
1266bf89570Sdrh
127895d7472Sdrhdo_test bind-2.1.1 {
128895d7472Sdrh  sqlite3_bind_parameter_count $VM
129895d7472Sdrh} 3
130895d7472Sdrhdo_test bind-2.1.2 {
131895d7472Sdrh  sqlite3_bind_parameter_name $VM 1
1326bf89570Sdrh} $v1
133895d7472Sdrhdo_test bind-2.1.3 {
134895d7472Sdrh  sqlite3_bind_parameter_name $VM 2
1356bf89570Sdrh} $v2
136895d7472Sdrhdo_test bind-2.1.4 {
137895d7472Sdrh  sqlite3_bind_parameter_name $VM 3
1386bf89570Sdrh} $v3
139fa6bc000Sdrhdo_test bind-2.1.5 {
1406bf89570Sdrh  sqlite3_bind_parameter_index $VM $v1
141fa6bc000Sdrh} 1
142fa6bc000Sdrhdo_test bind-2.1.6 {
1436bf89570Sdrh  sqlite3_bind_parameter_index $VM $v2
144fa6bc000Sdrh} 2
145fa6bc000Sdrhdo_test bind-2.1.7 {
1466bf89570Sdrh  sqlite3_bind_parameter_index $VM $v3
147fa6bc000Sdrh} 3
148fa6bc000Sdrhdo_test bind-2.1.8 {
149fa6bc000Sdrh  sqlite3_bind_parameter_index $VM {:hi}
150fa6bc000Sdrh} 0
15151e3d8e2Sdanielk1977
15251e3d8e2Sdanielk1977# 32 bit Integers
15351e3d8e2Sdanielk1977do_test bind-2.2 {
154241db313Sdrh  sqlite3_bind_int $VM 1 123
155241db313Sdrh  sqlite3_bind_int $VM 2 456
156241db313Sdrh  sqlite3_bind_int $VM 3 789
15751e3d8e2Sdanielk1977  sqlite_step $VM N VALUES COLNAMES
158106bb236Sdanielk1977  sqlite3_reset $VM
15951e3d8e2Sdanielk1977  execsql {SELECT rowid, * FROM t1}
16051e3d8e2Sdanielk1977} {1 123 456 789}
16151e3d8e2Sdanielk1977do_test bind-2.3 {
162241db313Sdrh  sqlite3_bind_int $VM 2 -2000000000
163241db313Sdrh  sqlite3_bind_int $VM 3 2000000000
16451e3d8e2Sdanielk1977  sqlite_step $VM N VALUES COLNAMES
165106bb236Sdanielk1977  sqlite3_reset $VM
16651e3d8e2Sdanielk1977  execsql {SELECT rowid, * FROM t1}
16751e3d8e2Sdanielk1977} {1 123 456 789 2 123 -2000000000 2000000000}
16851e3d8e2Sdanielk1977do_test bind-2.4 {
16935bb9d02Sdanielk1977  execsql {SELECT typeof(a), typeof(b), typeof(c) FROM t1}
17035bb9d02Sdanielk1977} {integer integer integer integer integer integer}
17151e3d8e2Sdanielk1977do_test bind-2.5 {
17251e3d8e2Sdanielk1977  execsql {
17351e3d8e2Sdanielk1977    DELETE FROM t1;
17451e3d8e2Sdanielk1977  }
17551e3d8e2Sdanielk1977} {}
17651e3d8e2Sdanielk1977
17751e3d8e2Sdanielk1977# 64 bit Integers
17851e3d8e2Sdanielk1977do_test bind-3.1 {
17951e3d8e2Sdanielk1977  sqlite3_bind_int64 $VM 1 32
18051e3d8e2Sdanielk1977  sqlite3_bind_int64 $VM 2 -2000000000000
18151e3d8e2Sdanielk1977  sqlite3_bind_int64 $VM 3 2000000000000
18251e3d8e2Sdanielk1977  sqlite_step $VM N VALUES COLNAMES
183106bb236Sdanielk1977  sqlite3_reset $VM
18451e3d8e2Sdanielk1977  execsql {SELECT rowid, * FROM t1}
18551e3d8e2Sdanielk1977} {1 32 -2000000000000 2000000000000}
18651e3d8e2Sdanielk1977do_test bind-3.2 {
18735bb9d02Sdanielk1977  execsql {SELECT typeof(a), typeof(b), typeof(c) FROM t1}
18835bb9d02Sdanielk1977} {integer integer integer}
18951e3d8e2Sdanielk1977do_test bind-3.3 {
19051e3d8e2Sdanielk1977  execsql {
19151e3d8e2Sdanielk1977    DELETE FROM t1;
19251e3d8e2Sdanielk1977  }
19351e3d8e2Sdanielk1977} {}
19451e3d8e2Sdanielk1977
19551e3d8e2Sdanielk1977# Doubles
19651e3d8e2Sdanielk1977do_test bind-4.1 {
19751e3d8e2Sdanielk1977  sqlite3_bind_double $VM 1 1234.1234
19851e3d8e2Sdanielk1977  sqlite3_bind_double $VM 2 0.00001
19951e3d8e2Sdanielk1977  sqlite3_bind_double $VM 3 123456789
20051e3d8e2Sdanielk1977  sqlite_step $VM N VALUES COLNAMES
201106bb236Sdanielk1977  sqlite3_reset $VM
202ab34b8f6Sdrh  set x [execsql {SELECT rowid, * FROM t1}]
203ab34b8f6Sdrh  regsub {1e-005} $x {1e-05} y
204ab34b8f6Sdrh  set y
20592febd92Sdrh} {1 1234.1234 1e-05 123456789.0}
20651e3d8e2Sdanielk1977do_test bind-4.2 {
20735bb9d02Sdanielk1977  execsql {SELECT typeof(a), typeof(b), typeof(c) FROM t1}
20835bb9d02Sdanielk1977} {real real real}
20951e3d8e2Sdanielk1977do_test bind-4.3 {
21051e3d8e2Sdanielk1977  execsql {
21151e3d8e2Sdanielk1977    DELETE FROM t1;
21251e3d8e2Sdanielk1977  }
21351e3d8e2Sdanielk1977} {}
21451e3d8e2Sdanielk1977
21551e3d8e2Sdanielk1977# NULL
21651e3d8e2Sdanielk1977do_test bind-5.1 {
21751e3d8e2Sdanielk1977  sqlite3_bind_null $VM 1
21851e3d8e2Sdanielk1977  sqlite3_bind_null $VM 2
21951e3d8e2Sdanielk1977  sqlite3_bind_null $VM 3
22051e3d8e2Sdanielk1977  sqlite_step $VM N VALUES COLNAMES
221106bb236Sdanielk1977  sqlite3_reset $VM
22251e3d8e2Sdanielk1977  execsql {SELECT rowid, * FROM t1}
22351e3d8e2Sdanielk1977} {1 {} {} {}}
22451e3d8e2Sdanielk1977do_test bind-5.2 {
22535bb9d02Sdanielk1977  execsql {SELECT typeof(a), typeof(b), typeof(c) FROM t1}
22635bb9d02Sdanielk1977} {null null null}
22751e3d8e2Sdanielk1977do_test bind-5.3 {
22851e3d8e2Sdanielk1977  execsql {
22951e3d8e2Sdanielk1977    DELETE FROM t1;
23051e3d8e2Sdanielk1977  }
23151e3d8e2Sdanielk1977} {}
23251e3d8e2Sdanielk1977
23351e3d8e2Sdanielk1977# UTF-8 text
23451e3d8e2Sdanielk1977do_test bind-6.1 {
23551e3d8e2Sdanielk1977  sqlite3_bind_text $VM 1 hellothere 5
236c572ef7fSdanielk1977  sqlite3_bind_text $VM 2 ".." 1
23751e3d8e2Sdanielk1977  sqlite3_bind_text $VM 3 world -1
23851e3d8e2Sdanielk1977  sqlite_step $VM N VALUES COLNAMES
239106bb236Sdanielk1977  sqlite3_reset $VM
24051e3d8e2Sdanielk1977  execsql {SELECT rowid, * FROM t1}
24151e3d8e2Sdanielk1977} {1 hello . world}
24251e3d8e2Sdanielk1977do_test bind-6.2 {
24335bb9d02Sdanielk1977  execsql {SELECT typeof(a), typeof(b), typeof(c) FROM t1}
24435bb9d02Sdanielk1977} {text text text}
24551e3d8e2Sdanielk1977do_test bind-6.3 {
24651e3d8e2Sdanielk1977  execsql {
24751e3d8e2Sdanielk1977    DELETE FROM t1;
24851e3d8e2Sdanielk1977  }
24951e3d8e2Sdanielk1977} {}
25051e3d8e2Sdanielk1977
25151e3d8e2Sdanielk1977# UTF-16 text
2526c62608fSdrhifcapable {utf16} {
25351e3d8e2Sdanielk1977  do_test bind-7.1 {
25451e3d8e2Sdanielk1977    sqlite3_bind_text16 $VM 1 [encoding convertto unicode hellothere] 10
25551e3d8e2Sdanielk1977    sqlite3_bind_text16 $VM 2 [encoding convertto unicode ""] 0
25651e3d8e2Sdanielk1977    sqlite3_bind_text16 $VM 3 [encoding convertto unicode world] 10
25751e3d8e2Sdanielk1977    sqlite_step $VM N VALUES COLNAMES
258106bb236Sdanielk1977    sqlite3_reset $VM
25951e3d8e2Sdanielk1977    execsql {SELECT rowid, * FROM t1}
26051e3d8e2Sdanielk1977  } {1 hello {} world}
26151e3d8e2Sdanielk1977  do_test bind-7.2 {
26235bb9d02Sdanielk1977    execsql {SELECT typeof(a), typeof(b), typeof(c) FROM t1}
26335bb9d02Sdanielk1977  } {text text text}
2646c62608fSdrh}
26551e3d8e2Sdanielk1977do_test bind-7.3 {
26651e3d8e2Sdanielk1977  execsql {
26751e3d8e2Sdanielk1977    DELETE FROM t1;
26851e3d8e2Sdanielk1977  }
26951e3d8e2Sdanielk1977} {}
27051e3d8e2Sdanielk1977
2716622cce3Sdanielk1977# Test that the 'out of range' error works.
2726622cce3Sdanielk1977do_test bind-8.1 {
2736622cce3Sdanielk1977  catch { sqlite3_bind_null $VM 0 }
2746622cce3Sdanielk1977} {1}
2756622cce3Sdanielk1977do_test bind-8.2 {
2766622cce3Sdanielk1977  sqlite3_errmsg $DB
277b08153d0Sdrh} {bind or column index out of range}
2786c62608fSdrhifcapable {utf16} {
2796622cce3Sdanielk1977  do_test bind-8.3 {
2806622cce3Sdanielk1977    encoding convertfrom unicode [sqlite3_errmsg16 $DB]
281b08153d0Sdrh  } {bind or column index out of range}
2826c62608fSdrh}
2836622cce3Sdanielk1977do_test bind-8.4 {
2846622cce3Sdanielk1977  sqlite3_bind_null $VM 1
2856622cce3Sdanielk1977  sqlite3_errmsg $DB
2866622cce3Sdanielk1977} {not an error}
2876622cce3Sdanielk1977do_test bind-8.5 {
2886622cce3Sdanielk1977  catch { sqlite3_bind_null $VM 4 }
2896622cce3Sdanielk1977} {1}
2906622cce3Sdanielk1977do_test bind-8.6 {
2916622cce3Sdanielk1977  sqlite3_errmsg $DB
292b08153d0Sdrh} {bind or column index out of range}
2936c62608fSdrhifcapable {utf16} {
2946622cce3Sdanielk1977  do_test bind-8.7 {
2956622cce3Sdanielk1977    encoding convertfrom unicode [sqlite3_errmsg16 $DB]
296b08153d0Sdrh  } {bind or column index out of range}
2976c62608fSdrh}
2986622cce3Sdanielk1977
299f4618891Sdanielk1977do_test bind-8.8 {
300f4618891Sdanielk1977  catch { sqlite3_bind_blob $VM 0 "abc" 3 }
301f4618891Sdanielk1977} {1}
302f4618891Sdanielk1977do_test bind-8.9 {
303f4618891Sdanielk1977  catch { sqlite3_bind_blob $VM 4 "abc" 3 }
304f4618891Sdanielk1977} {1}
305f4618891Sdanielk1977do_test bind-8.10 {
306f4618891Sdanielk1977  catch { sqlite3_bind_text $VM 0 "abc" 3 }
307f4618891Sdanielk1977} {1}
3086c62608fSdrhifcapable {utf16} {
309f4618891Sdanielk1977  do_test bind-8.11 {
310f4618891Sdanielk1977    catch { sqlite3_bind_text16 $VM 4 "abc" 2 }
311f4618891Sdanielk1977  } {1}
3126c62608fSdrh}
313f4618891Sdanielk1977do_test bind-8.12 {
314f4618891Sdanielk1977  catch { sqlite3_bind_int $VM 0 5 }
315f4618891Sdanielk1977} {1}
316f4618891Sdanielk1977do_test bind-8.13 {
317f4618891Sdanielk1977  catch { sqlite3_bind_int $VM 4 5 }
318f4618891Sdanielk1977} {1}
319f4618891Sdanielk1977do_test bind-8.14 {
320f4618891Sdanielk1977  catch { sqlite3_bind_double $VM 0 5.0 }
321f4618891Sdanielk1977} {1}
322f4618891Sdanielk1977do_test bind-8.15 {
323f4618891Sdanielk1977  catch { sqlite3_bind_double $VM 4 6.0 }
324f4618891Sdanielk1977} {1}
3256622cce3Sdanielk1977
326fa6bc000Sdrhdo_test bind-8.99 {
327106bb236Sdanielk1977  sqlite3_finalize $VM
3283cf86063Sdanielk1977} SQLITE_OK
32951e3d8e2Sdanielk1977
330fa6bc000Sdrhdo_test bind-9.1 {
331fa6bc000Sdrh  execsql {
332fa6bc000Sdrh    CREATE TABLE t2(a,b,c,d,e,f);
333fa6bc000Sdrh  }
334fa6bc000Sdrh  set rc [catch {
335fa6bc000Sdrh    sqlite3_prepare $DB {
336fa6bc000Sdrh      INSERT INTO t2(a) VALUES(?0)
337fa6bc000Sdrh    } -1 TAIL
338fa6bc000Sdrh  } msg]
339fa6bc000Sdrh  lappend rc $msg
340fa6bc000Sdrh} {1 {(1) variable number must be between ?1 and ?999}}
341fa6bc000Sdrhdo_test bind-9.2 {
342fa6bc000Sdrh  set rc [catch {
343fa6bc000Sdrh    sqlite3_prepare $DB {
344fa6bc000Sdrh      INSERT INTO t2(a) VALUES(?1000)
345fa6bc000Sdrh    } -1 TAIL
346fa6bc000Sdrh  } msg]
347fa6bc000Sdrh  lappend rc $msg
348fa6bc000Sdrh} {1 {(1) variable number must be between ?1 and ?999}}
349fa6bc000Sdrhdo_test bind-9.3 {
350fa6bc000Sdrh  set VM [
351fa6bc000Sdrh    sqlite3_prepare $DB {
352fa6bc000Sdrh      INSERT INTO t2(a,b) VALUES(?1,?999)
353fa6bc000Sdrh    } -1 TAIL
354fa6bc000Sdrh  ]
355fa6bc000Sdrh  sqlite3_bind_parameter_count $VM
356fa6bc000Sdrh} {999}
357fa6bc000Sdrhcatch {sqlite3_finalize $VM}
358fa6bc000Sdrhdo_test bind-9.4 {
359fa6bc000Sdrh  set VM [
360fa6bc000Sdrh    sqlite3_prepare $DB {
361fa6bc000Sdrh      INSERT INTO t2(a,b,c,d) VALUES(?1,?999,?,?)
362fa6bc000Sdrh    } -1 TAIL
363fa6bc000Sdrh  ]
364fa6bc000Sdrh  sqlite3_bind_parameter_count $VM
365fa6bc000Sdrh} {1001}
366fa6bc000Sdrhdo_test bind-9.5 {
367fa6bc000Sdrh  sqlite3_bind_int $VM 1 1
368fa6bc000Sdrh  sqlite3_bind_int $VM 999 999
369fa6bc000Sdrh  sqlite3_bind_int $VM 1000 1000
370fa6bc000Sdrh  sqlite3_bind_int $VM 1001 1001
371fa6bc000Sdrh  sqlite3_step $VM
372fa6bc000Sdrh} SQLITE_DONE
373fa6bc000Sdrhdo_test bind-9.6 {
374fa6bc000Sdrh  sqlite3_finalize $VM
375fa6bc000Sdrh} SQLITE_OK
376fa6bc000Sdrhdo_test bind-9.7 {
377fa6bc000Sdrh  execsql {SELECT * FROM t2}
378fa6bc000Sdrh} {1 999 1000 1001 {} {}}
37951e3d8e2Sdanielk1977
3806bf89570Sdrhifcapable {tclvar} {
381fa6bc000Sdrh  do_test bind-10.1 {
382fa6bc000Sdrh    set VM [
383fa6bc000Sdrh      sqlite3_prepare $DB {
384fa6bc000Sdrh        INSERT INTO t2(a,b,c,d,e,f) VALUES(:abc,$abc,:abc,$ab,$abc,:abc)
385fa6bc000Sdrh      } -1 TAIL
386fa6bc000Sdrh    ]
387fa6bc000Sdrh    sqlite3_bind_parameter_count $VM
388fa6bc000Sdrh  } 3
3896bf89570Sdrh  set v1 {$abc}
3906bf89570Sdrh  set v2 {$ab}
3916bf89570Sdrh}
3926bf89570Sdrhifcapable {!tclvar} {
3936bf89570Sdrh  do_test bind-10.1 {
3946bf89570Sdrh    set VM [
3956bf89570Sdrh      sqlite3_prepare $DB {
3966bf89570Sdrh        INSERT INTO t2(a,b,c,d,e,f) VALUES(:abc,:xyz,:abc,:xy,:xyz,:abc)
3976bf89570Sdrh      } -1 TAIL
3986bf89570Sdrh    ]
3996bf89570Sdrh    sqlite3_bind_parameter_count $VM
4006bf89570Sdrh  } 3
4016bf89570Sdrh  set v1 {:xyz}
4026bf89570Sdrh  set v2 {:xy}
4036bf89570Sdrh}
404fa6bc000Sdrhdo_test bind-10.2 {
405fa6bc000Sdrh  sqlite3_bind_parameter_index $VM :abc
406fa6bc000Sdrh} 1
407fa6bc000Sdrhdo_test bind-10.3 {
4086bf89570Sdrh  sqlite3_bind_parameter_index $VM $v1
409fa6bc000Sdrh} 2
410fa6bc000Sdrhdo_test bind-10.4 {
4116bf89570Sdrh  sqlite3_bind_parameter_index $VM $v2
412fa6bc000Sdrh} 3
413fa6bc000Sdrhdo_test bind-10.5 {
414fa6bc000Sdrh  sqlite3_bind_parameter_name $VM 1
415fa6bc000Sdrh} :abc
416fa6bc000Sdrhdo_test bind-10.6 {
417fa6bc000Sdrh  sqlite3_bind_parameter_name $VM 2
4186bf89570Sdrh} $v1
419fa6bc000Sdrhdo_test bind-10.7 {
420fa6bc000Sdrh  sqlite3_bind_parameter_name $VM 3
4216bf89570Sdrh} $v2
422c5cdca61Sdrhdo_test bind-10.7.1 {
423c5cdca61Sdrh  sqlite3_bind_parameter_name 0 1   ;# Ignore if VM is NULL
424c5cdca61Sdrh} {}
425c5cdca61Sdrhdo_test bind-10.7.2 {
426c5cdca61Sdrh  sqlite3_bind_parameter_name $VM 0 ;# Ignore if index too small
427c5cdca61Sdrh} {}
428c5cdca61Sdrhdo_test bind-10.7.3 {
429c5cdca61Sdrh  sqlite3_bind_parameter_name $VM 4 ;# Ignore if index is too big
430c5cdca61Sdrh} {}
431fa6bc000Sdrhdo_test bind-10.8 {
432fa6bc000Sdrh  sqlite3_bind_int $VM 1 1
433fa6bc000Sdrh  sqlite3_bind_int $VM 2 2
434fa6bc000Sdrh  sqlite3_bind_int $VM 3 3
435fa6bc000Sdrh  sqlite3_step $VM
436fa6bc000Sdrh} SQLITE_DONE
437c5cdca61Sdrhdo_test bind-10.8.1 {
438c5cdca61Sdrh  # Binding attempts after program start should fail
439c5cdca61Sdrh  set rc [catch {
440c5cdca61Sdrh    sqlite3_bind_int $VM 1 1
441c5cdca61Sdrh  } msg]
442c5cdca61Sdrh  lappend rc $msg
443c5cdca61Sdrh} {1 {}}
444fa6bc000Sdrhdo_test bind-10.9 {
445fa6bc000Sdrh  sqlite3_finalize $VM
446fa6bc000Sdrh} SQLITE_OK
447fa6bc000Sdrhdo_test bind-10.10 {
448fa6bc000Sdrh  execsql {SELECT * FROM t2}
449fa6bc000Sdrh} {1 999 1000 1001 {} {} 1 2 1 3 2 1}
45082a4851aSdrh
451971a7c87Sdrh# Ticket #918
452971a7c87Sdrh#
453971a7c87Sdrhdo_test bind-10.11 {
454971a7c87Sdrh  catch {sqlite3_finalize $VM}
455971a7c87Sdrh  set VM [
456971a7c87Sdrh    sqlite3_prepare $DB {
457971a7c87Sdrh      INSERT INTO t2(a,b,c,d,e,f) VALUES(:abc,?,?4,:pqr,:abc,?4)
458971a7c87Sdrh    } -1 TAIL
459971a7c87Sdrh  ]
460971a7c87Sdrh  sqlite3_bind_parameter_count $VM
461971a7c87Sdrh} 5
462c5cdca61Sdrhdo_test bind-10.11.1 {
463c5cdca61Sdrh  sqlite3_bind_parameter_index 0 :xyz  ;# ignore NULL VM arguments
464c5cdca61Sdrh} 0
465971a7c87Sdrhdo_test bind-10.12 {
466971a7c87Sdrh  sqlite3_bind_parameter_index $VM :xyz
467971a7c87Sdrh} 0
468971a7c87Sdrhdo_test bind-10.13 {
469971a7c87Sdrh  sqlite3_bind_parameter_index $VM {}
470971a7c87Sdrh} 0
471971a7c87Sdrhdo_test bind-10.14 {
472971a7c87Sdrh  sqlite3_bind_parameter_index $VM :pqr
473971a7c87Sdrh} 5
474971a7c87Sdrhdo_test bind-10.15 {
475971a7c87Sdrh  sqlite3_bind_parameter_index $VM ?4
476971a7c87Sdrh} 4
477971a7c87Sdrhdo_test bind-10.16 {
478971a7c87Sdrh  sqlite3_bind_parameter_name $VM 1
479971a7c87Sdrh} :abc
480b3bce662Sdanielk1977do_test bind-10.17 {
481971a7c87Sdrh  sqlite3_bind_parameter_name $VM 2
482971a7c87Sdrh} {}
483b3bce662Sdanielk1977do_test bind-10.18 {
484971a7c87Sdrh  sqlite3_bind_parameter_name $VM 3
485971a7c87Sdrh} {}
486b3bce662Sdanielk1977do_test bind-10.19 {
487971a7c87Sdrh  sqlite3_bind_parameter_name $VM 4
488971a7c87Sdrh} {?4}
489b3bce662Sdanielk1977do_test bind-10.20 {
490971a7c87Sdrh  sqlite3_bind_parameter_name $VM 5
491971a7c87Sdrh} :pqr
492971a7c87Sdrhcatch {sqlite3_finalize $VM}
493971a7c87Sdrh
49448e5aa27Sdrh# Make sure we catch an unterminated "(" in a Tcl-style variable name
49548e5aa27Sdrh#
4964489f9bdSdanielk1977ifcapable tclvar {
49748e5aa27Sdrh  do_test bind-11.1 {
49848e5aa27Sdrh    catchsql {SELECT * FROM sqlite_master WHERE name=$abc(123 and sql NOT NULL;}
49948e5aa27Sdrh  } {1 {unrecognized token: "$abc(123"}}
5004489f9bdSdanielk1977}
50148e5aa27Sdrh
502e57c06fdSdrhif {[execsql {pragma encoding}]=="UTF-8"} {
503bf8aa2a6Sdrh  # Test the ability to bind text that contains embedded '\000' characters.
504bf8aa2a6Sdrh  # Make sure we can recover the enter input string.
505bf8aa2a6Sdrh  #
506bf8aa2a6Sdrh  do_test bind-12.1 {
507bf8aa2a6Sdrh    execsql {
508bf8aa2a6Sdrh      CREATE TABLE t3(x BLOB);
509bf8aa2a6Sdrh    }
510bf8aa2a6Sdrh    set VM [sqlite3_prepare $DB {INSERT INTO t3 VALUES(?)} -1 TAIL]
511bf8aa2a6Sdrh    sqlite_bind  $VM 1 not-used blob10
512bf8aa2a6Sdrh    sqlite3_step $VM
513bf8aa2a6Sdrh    sqlite3_finalize $VM
514bf8aa2a6Sdrh    execsql {
515bf8aa2a6Sdrh      SELECT typeof(x), length(x), quote(x),
516bf8aa2a6Sdrh             length(cast(x AS BLOB)), quote(cast(x AS BLOB)) FROM t3
517bf8aa2a6Sdrh    }
518bf8aa2a6Sdrh  } {text 3 'abc' 10 X'6162630078797A007071'}
5195c434b72Sdrh  do_test bind-12.2 {
5205c434b72Sdrh    sqlite3_create_function $DB
5215c434b72Sdrh    execsql {
5225c434b72Sdrh      SELECT quote(cast(x_coalesce(x) AS blob)) FROM t3
5235c434b72Sdrh    }
5245c434b72Sdrh  } {X'6162630078797A007071'}
525e57c06fdSdrh}
526bf8aa2a6Sdrh
52782a4851aSdrhfinish_test
528