blob: d5ea33f67c28aad1735d2141ee250b51015ad1b6 [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#
drh2c6674c2004-08-25 04:07:01 +000014# $Id: bind.test,v 1.18 2004/08/25 04:07:03 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
danielk197751e3d8e2004-05-20 01:12:34 +000099do_test bind-2.1 {
100 execsql {
101 DELETE FROM t1;
102 }
drh895d7472004-08-20 16:02:39 +0000103 set VM [sqlite3_prepare $DB {INSERT INTO t1 VALUES($one,$::two,${x})} -1 TAIL]
danielk197751e3d8e2004-05-20 01:12:34 +0000104 set TAIL
105} {}
drh895d7472004-08-20 16:02:39 +0000106do_test bind-2.1.1 {
107 sqlite3_bind_parameter_count $VM
108} 3
109do_test bind-2.1.2 {
110 sqlite3_bind_parameter_name $VM 1
111} {$one}
112do_test bind-2.1.3 {
113 sqlite3_bind_parameter_name $VM 2
114} {$::two}
115do_test bind-2.1.4 {
116 sqlite3_bind_parameter_name $VM 3
117} {${x}}
danielk197751e3d8e2004-05-20 01:12:34 +0000118
119# 32 bit Integers
120do_test bind-2.2 {
drh241db312004-06-22 12:46:53 +0000121 sqlite3_bind_int $VM 1 123
122 sqlite3_bind_int $VM 2 456
123 sqlite3_bind_int $VM 3 789
danielk197751e3d8e2004-05-20 01:12:34 +0000124 sqlite_step $VM N VALUES COLNAMES
danielk1977106bb232004-05-21 10:08:53 +0000125 sqlite3_reset $VM
danielk197751e3d8e2004-05-20 01:12:34 +0000126 execsql {SELECT rowid, * FROM t1}
127} {1 123 456 789}
128do_test bind-2.3 {
drh241db312004-06-22 12:46:53 +0000129 sqlite3_bind_int $VM 2 -2000000000
130 sqlite3_bind_int $VM 3 2000000000
danielk197751e3d8e2004-05-20 01:12:34 +0000131 sqlite_step $VM N VALUES COLNAMES
danielk1977106bb232004-05-21 10:08:53 +0000132 sqlite3_reset $VM
danielk197751e3d8e2004-05-20 01:12:34 +0000133 execsql {SELECT rowid, * FROM t1}
134} {1 123 456 789 2 123 -2000000000 2000000000}
135do_test bind-2.4 {
danielk197735bb9d02004-05-24 12:55:54 +0000136 execsql {SELECT typeof(a), typeof(b), typeof(c) FROM t1}
137} {integer integer integer integer integer integer}
danielk197751e3d8e2004-05-20 01:12:34 +0000138do_test bind-2.5 {
139 execsql {
140 DELETE FROM t1;
141 }
142} {}
143
144# 64 bit Integers
145do_test bind-3.1 {
146 sqlite3_bind_int64 $VM 1 32
147 sqlite3_bind_int64 $VM 2 -2000000000000
148 sqlite3_bind_int64 $VM 3 2000000000000
149 sqlite_step $VM N VALUES COLNAMES
danielk1977106bb232004-05-21 10:08:53 +0000150 sqlite3_reset $VM
danielk197751e3d8e2004-05-20 01:12:34 +0000151 execsql {SELECT rowid, * FROM t1}
152} {1 32 -2000000000000 2000000000000}
153do_test bind-3.2 {
danielk197735bb9d02004-05-24 12:55:54 +0000154 execsql {SELECT typeof(a), typeof(b), typeof(c) FROM t1}
155} {integer integer integer}
danielk197751e3d8e2004-05-20 01:12:34 +0000156do_test bind-3.3 {
157 execsql {
158 DELETE FROM t1;
159 }
160} {}
161
162# Doubles
163do_test bind-4.1 {
164 sqlite3_bind_double $VM 1 1234.1234
165 sqlite3_bind_double $VM 2 0.00001
166 sqlite3_bind_double $VM 3 123456789
167 sqlite_step $VM N VALUES COLNAMES
danielk1977106bb232004-05-21 10:08:53 +0000168 sqlite3_reset $VM
danielk197751e3d8e2004-05-20 01:12:34 +0000169 execsql {SELECT rowid, * FROM t1}
drh92febd92004-08-20 18:34:20 +0000170} {1 1234.1234 1e-05 123456789.0}
danielk197751e3d8e2004-05-20 01:12:34 +0000171do_test bind-4.2 {
danielk197735bb9d02004-05-24 12:55:54 +0000172 execsql {SELECT typeof(a), typeof(b), typeof(c) FROM t1}
173} {real real real}
danielk197751e3d8e2004-05-20 01:12:34 +0000174do_test bind-4.3 {
175 execsql {
176 DELETE FROM t1;
177 }
178} {}
179
180# NULL
181do_test bind-5.1 {
182 sqlite3_bind_null $VM 1
183 sqlite3_bind_null $VM 2
184 sqlite3_bind_null $VM 3
185 sqlite_step $VM N VALUES COLNAMES
danielk1977106bb232004-05-21 10:08:53 +0000186 sqlite3_reset $VM
danielk197751e3d8e2004-05-20 01:12:34 +0000187 execsql {SELECT rowid, * FROM t1}
188} {1 {} {} {}}
189do_test bind-5.2 {
danielk197735bb9d02004-05-24 12:55:54 +0000190 execsql {SELECT typeof(a), typeof(b), typeof(c) FROM t1}
191} {null null null}
danielk197751e3d8e2004-05-20 01:12:34 +0000192do_test bind-5.3 {
193 execsql {
194 DELETE FROM t1;
195 }
196} {}
197
198# UTF-8 text
199do_test bind-6.1 {
200 sqlite3_bind_text $VM 1 hellothere 5
danielk1977c572ef72004-05-27 09:28:41 +0000201 sqlite3_bind_text $VM 2 ".." 1
danielk197751e3d8e2004-05-20 01:12:34 +0000202 sqlite3_bind_text $VM 3 world -1
203 sqlite_step $VM N VALUES COLNAMES
danielk1977106bb232004-05-21 10:08:53 +0000204 sqlite3_reset $VM
danielk197751e3d8e2004-05-20 01:12:34 +0000205 execsql {SELECT rowid, * FROM t1}
206} {1 hello . world}
207do_test bind-6.2 {
danielk197735bb9d02004-05-24 12:55:54 +0000208 execsql {SELECT typeof(a), typeof(b), typeof(c) FROM t1}
209} {text text text}
danielk197751e3d8e2004-05-20 01:12:34 +0000210do_test bind-6.3 {
211 execsql {
212 DELETE FROM t1;
213 }
214} {}
215
216# UTF-16 text
217do_test bind-7.1 {
218 sqlite3_bind_text16 $VM 1 [encoding convertto unicode hellothere] 10
219 sqlite3_bind_text16 $VM 2 [encoding convertto unicode ""] 0
220 sqlite3_bind_text16 $VM 3 [encoding convertto unicode world] 10
221 sqlite_step $VM N VALUES COLNAMES
danielk1977106bb232004-05-21 10:08:53 +0000222 sqlite3_reset $VM
danielk197751e3d8e2004-05-20 01:12:34 +0000223 execsql {SELECT rowid, * FROM t1}
224} {1 hello {} world}
225do_test bind-7.2 {
danielk197735bb9d02004-05-24 12:55:54 +0000226 execsql {SELECT typeof(a), typeof(b), typeof(c) FROM t1}
227} {text text text}
danielk197751e3d8e2004-05-20 01:12:34 +0000228do_test bind-7.3 {
229 execsql {
230 DELETE FROM t1;
231 }
232} {}
233
danielk19776622cce2004-05-20 11:00:52 +0000234# Test that the 'out of range' error works.
235do_test bind-8.1 {
236 catch { sqlite3_bind_null $VM 0 }
237} {1}
238do_test bind-8.2 {
239 sqlite3_errmsg $DB
240} {bind index out of range}
241do_test bind-8.3 {
242 encoding convertfrom unicode [sqlite3_errmsg16 $DB]
243} {bind index out of range}
244do_test bind-8.4 {
245 sqlite3_bind_null $VM 1
246 sqlite3_errmsg $DB
247} {not an error}
248do_test bind-8.5 {
249 catch { sqlite3_bind_null $VM 4 }
250} {1}
251do_test bind-8.6 {
252 sqlite3_errmsg $DB
253} {bind index out of range}
254do_test bind-8.7 {
255 encoding convertfrom unicode [sqlite3_errmsg16 $DB]
256} {bind index out of range}
257
danielk1977f4618892004-06-28 13:09:11 +0000258do_test bind-8.8 {
259 catch { sqlite3_bind_blob $VM 0 "abc" 3 }
260} {1}
261do_test bind-8.9 {
262 catch { sqlite3_bind_blob $VM 4 "abc" 3 }
263} {1}
264do_test bind-8.10 {
265 catch { sqlite3_bind_text $VM 0 "abc" 3 }
266} {1}
267do_test bind-8.11 {
268 catch { sqlite3_bind_text16 $VM 4 "abc" 2 }
269} {1}
270do_test bind-8.12 {
271 catch { sqlite3_bind_int $VM 0 5 }
272} {1}
273do_test bind-8.13 {
274 catch { sqlite3_bind_int $VM 4 5 }
275} {1}
276do_test bind-8.14 {
277 catch { sqlite3_bind_double $VM 0 5.0 }
278} {1}
279do_test bind-8.15 {
280 catch { sqlite3_bind_double $VM 4 6.0 }
281} {1}
danielk19776622cce2004-05-20 11:00:52 +0000282
283do_test bind-9.99 {
danielk1977106bb232004-05-21 10:08:53 +0000284 sqlite3_finalize $VM
danielk19773cf86062004-05-26 10:11:05 +0000285} SQLITE_OK
danielk197751e3d8e2004-05-20 01:12:34 +0000286
287
drh82a48512003-09-06 22:45:20 +0000288
289finish_test