blob: 5b548b92133e1f12936b4ea6f371f13221608097 [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#
drh75f6a032004-07-15 14:15:00 +000014# $Id: bind.test,v 1.15 2004/07/15 14:15: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]
drh82a48512003-09-06 22:45:20 +000040 execsql {CREATE TABLE t1(a,b,c)}
danielk19774ad17132004-05-21 01:47:26 +000041 set VM [sqlite3_prepare $DB {INSERT INTO t1 VALUES(?,?,?)} -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
drh82a48512003-09-06 22:45:20 +000047do_test bind-1.2 {
48 sqlite_step $VM N VALUES COLNAMES
49} {SQLITE_DONE}
50do_test bind-1.3 {
51 execsql {SELECT rowid, * FROM t1}
52} {1 {} {} {}}
53do_test bind-1.4 {
danielk1977106bb232004-05-21 10:08:53 +000054 sqlite3_reset $VM
drh82a48512003-09-06 22:45:20 +000055 sqlite_bind $VM 1 {test value 1} normal
56 sqlite_step $VM N VALUES COLNAMES
57} SQLITE_DONE
58do_test bind-1.5 {
59 execsql {SELECT rowid, * FROM t1}
60} {1 {} {} {} 2 {test value 1} {} {}}
61do_test bind-1.6 {
danielk1977106bb232004-05-21 10:08:53 +000062 sqlite3_reset $VM
drh82a48512003-09-06 22:45:20 +000063 sqlite_bind $VM 3 {'test value 2'} normal
64 sqlite_step $VM N VALUES COLNAMES
65} SQLITE_DONE
66do_test bind-1.7 {
67 execsql {SELECT rowid, * FROM t1}
68} {1 {} {} {} 2 {test value 1} {} {} 3 {test value 1} {} {'test value 2'}}
69do_test bind-1.8 {
danielk1977106bb232004-05-21 10:08:53 +000070 sqlite3_reset $VM
drh82a48512003-09-06 22:45:20 +000071 set sqlite_static_bind_value 123
72 sqlite_bind $VM 1 {} static
73 sqlite_bind $VM 2 {abcdefg} normal
74 sqlite_bind $VM 3 {} null
75 execsql {DELETE FROM t1}
76 sqlite_step $VM N VALUES COLNAMES
77 execsql {SELECT rowid, * FROM t1}
78} {1 123 abcdefg {}}
79do_test bind-1.9 {
danielk1977106bb232004-05-21 10:08:53 +000080 sqlite3_reset $VM
drh82a48512003-09-06 22:45:20 +000081 sqlite_bind $VM 1 {456} normal
82 sqlite_step $VM N VALUES COLNAMES
83 execsql {SELECT rowid, * FROM t1}
84} {1 123 abcdefg {} 2 456 abcdefg {}}
85
drh82a48512003-09-06 22:45:20 +000086do_test bind-1.99 {
danielk1977106bb232004-05-21 10:08:53 +000087 sqlite3_finalize $VM
danielk19773cf86062004-05-26 10:11:05 +000088} SQLITE_OK
drh82a48512003-09-06 22:45:20 +000089
danielk197751e3d8e2004-05-20 01:12:34 +000090do_test bind-2.1 {
91 execsql {
92 DELETE FROM t1;
93 }
danielk19772f2322f2004-05-21 02:11:40 +000094 set VM [sqlite3_prepare $DB {INSERT INTO t1 VALUES(?,?,?)} -1 TAIL]
danielk197751e3d8e2004-05-20 01:12:34 +000095 set TAIL
96} {}
97
98# 32 bit Integers
99do_test bind-2.2 {
drh241db312004-06-22 12:46:53 +0000100 sqlite3_bind_int $VM 1 123
101 sqlite3_bind_int $VM 2 456
102 sqlite3_bind_int $VM 3 789
danielk197751e3d8e2004-05-20 01:12:34 +0000103 sqlite_step $VM N VALUES COLNAMES
danielk1977106bb232004-05-21 10:08:53 +0000104 sqlite3_reset $VM
danielk197751e3d8e2004-05-20 01:12:34 +0000105 execsql {SELECT rowid, * FROM t1}
106} {1 123 456 789}
107do_test bind-2.3 {
drh241db312004-06-22 12:46:53 +0000108 sqlite3_bind_int $VM 2 -2000000000
109 sqlite3_bind_int $VM 3 2000000000
danielk197751e3d8e2004-05-20 01:12:34 +0000110 sqlite_step $VM N VALUES COLNAMES
danielk1977106bb232004-05-21 10:08:53 +0000111 sqlite3_reset $VM
danielk197751e3d8e2004-05-20 01:12:34 +0000112 execsql {SELECT rowid, * FROM t1}
113} {1 123 456 789 2 123 -2000000000 2000000000}
114do_test bind-2.4 {
danielk197735bb9d02004-05-24 12:55:54 +0000115 execsql {SELECT typeof(a), typeof(b), typeof(c) FROM t1}
116} {integer integer integer integer integer integer}
danielk197751e3d8e2004-05-20 01:12:34 +0000117do_test bind-2.5 {
118 execsql {
119 DELETE FROM t1;
120 }
121} {}
122
123# 64 bit Integers
124do_test bind-3.1 {
125 sqlite3_bind_int64 $VM 1 32
126 sqlite3_bind_int64 $VM 2 -2000000000000
127 sqlite3_bind_int64 $VM 3 2000000000000
128 sqlite_step $VM N VALUES COLNAMES
danielk1977106bb232004-05-21 10:08:53 +0000129 sqlite3_reset $VM
danielk197751e3d8e2004-05-20 01:12:34 +0000130 execsql {SELECT rowid, * FROM t1}
131} {1 32 -2000000000000 2000000000000}
132do_test bind-3.2 {
danielk197735bb9d02004-05-24 12:55:54 +0000133 execsql {SELECT typeof(a), typeof(b), typeof(c) FROM t1}
134} {integer integer integer}
danielk197751e3d8e2004-05-20 01:12:34 +0000135do_test bind-3.3 {
136 execsql {
137 DELETE FROM t1;
138 }
139} {}
140
141# Doubles
142do_test bind-4.1 {
143 sqlite3_bind_double $VM 1 1234.1234
144 sqlite3_bind_double $VM 2 0.00001
145 sqlite3_bind_double $VM 3 123456789
146 sqlite_step $VM N VALUES COLNAMES
danielk1977106bb232004-05-21 10:08:53 +0000147 sqlite3_reset $VM
danielk197751e3d8e2004-05-20 01:12:34 +0000148 execsql {SELECT rowid, * FROM t1}
149} {1 1234.1234 1e-05 123456789}
150do_test bind-4.2 {
danielk197735bb9d02004-05-24 12:55:54 +0000151 execsql {SELECT typeof(a), typeof(b), typeof(c) FROM t1}
152} {real real real}
danielk197751e3d8e2004-05-20 01:12:34 +0000153do_test bind-4.3 {
154 execsql {
155 DELETE FROM t1;
156 }
157} {}
158
159# NULL
160do_test bind-5.1 {
161 sqlite3_bind_null $VM 1
162 sqlite3_bind_null $VM 2
163 sqlite3_bind_null $VM 3
164 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 {} {} {}}
168do_test bind-5.2 {
danielk197735bb9d02004-05-24 12:55:54 +0000169 execsql {SELECT typeof(a), typeof(b), typeof(c) FROM t1}
170} {null null null}
danielk197751e3d8e2004-05-20 01:12:34 +0000171do_test bind-5.3 {
172 execsql {
173 DELETE FROM t1;
174 }
175} {}
176
177# UTF-8 text
178do_test bind-6.1 {
179 sqlite3_bind_text $VM 1 hellothere 5
danielk1977c572ef72004-05-27 09:28:41 +0000180 sqlite3_bind_text $VM 2 ".." 1
danielk197751e3d8e2004-05-20 01:12:34 +0000181 sqlite3_bind_text $VM 3 world -1
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 hello . world}
186do_test bind-6.2 {
danielk197735bb9d02004-05-24 12:55:54 +0000187 execsql {SELECT typeof(a), typeof(b), typeof(c) FROM t1}
188} {text text text}
danielk197751e3d8e2004-05-20 01:12:34 +0000189do_test bind-6.3 {
190 execsql {
191 DELETE FROM t1;
192 }
193} {}
194
195# UTF-16 text
196do_test bind-7.1 {
197 sqlite3_bind_text16 $VM 1 [encoding convertto unicode hellothere] 10
198 sqlite3_bind_text16 $VM 2 [encoding convertto unicode ""] 0
199 sqlite3_bind_text16 $VM 3 [encoding convertto unicode world] 10
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}
203} {1 hello {} world}
204do_test bind-7.2 {
danielk197735bb9d02004-05-24 12:55:54 +0000205 execsql {SELECT typeof(a), typeof(b), typeof(c) FROM t1}
206} {text text text}
danielk197751e3d8e2004-05-20 01:12:34 +0000207do_test bind-7.3 {
208 execsql {
209 DELETE FROM t1;
210 }
211} {}
212
danielk19776622cce2004-05-20 11:00:52 +0000213# Test that the 'out of range' error works.
214do_test bind-8.1 {
215 catch { sqlite3_bind_null $VM 0 }
216} {1}
217do_test bind-8.2 {
218 sqlite3_errmsg $DB
219} {bind index out of range}
220do_test bind-8.3 {
221 encoding convertfrom unicode [sqlite3_errmsg16 $DB]
222} {bind index out of range}
223do_test bind-8.4 {
224 sqlite3_bind_null $VM 1
225 sqlite3_errmsg $DB
226} {not an error}
227do_test bind-8.5 {
228 catch { sqlite3_bind_null $VM 4 }
229} {1}
230do_test bind-8.6 {
231 sqlite3_errmsg $DB
232} {bind index out of range}
233do_test bind-8.7 {
234 encoding convertfrom unicode [sqlite3_errmsg16 $DB]
235} {bind index out of range}
236
danielk1977f4618892004-06-28 13:09:11 +0000237do_test bind-8.8 {
238 catch { sqlite3_bind_blob $VM 0 "abc" 3 }
239} {1}
240do_test bind-8.9 {
241 catch { sqlite3_bind_blob $VM 4 "abc" 3 }
242} {1}
243do_test bind-8.10 {
244 catch { sqlite3_bind_text $VM 0 "abc" 3 }
245} {1}
246do_test bind-8.11 {
247 catch { sqlite3_bind_text16 $VM 4 "abc" 2 }
248} {1}
249do_test bind-8.12 {
250 catch { sqlite3_bind_int $VM 0 5 }
251} {1}
252do_test bind-8.13 {
253 catch { sqlite3_bind_int $VM 4 5 }
254} {1}
255do_test bind-8.14 {
256 catch { sqlite3_bind_double $VM 0 5.0 }
257} {1}
258do_test bind-8.15 {
259 catch { sqlite3_bind_double $VM 4 6.0 }
260} {1}
danielk19776622cce2004-05-20 11:00:52 +0000261
262do_test bind-9.99 {
danielk1977106bb232004-05-21 10:08:53 +0000263 sqlite3_finalize $VM
danielk19773cf86062004-05-26 10:11:05 +0000264} SQLITE_OK
danielk197751e3d8e2004-05-20 01:12:34 +0000265
266
drh82a48512003-09-06 22:45:20 +0000267
268finish_test