blob: 3ddb208db979625de104305c65529346bf2111c8 [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#
danielk1977f4618892004-06-28 13:09:11 +000014# $Id: bind.test,v 1.14 2004/06/28 13:09:11 danielk1977 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} {}
44do_test bind-1.2 {
45 sqlite_step $VM N VALUES COLNAMES
46} {SQLITE_DONE}
47do_test bind-1.3 {
48 execsql {SELECT rowid, * FROM t1}
49} {1 {} {} {}}
50do_test bind-1.4 {
danielk1977106bb232004-05-21 10:08:53 +000051 sqlite3_reset $VM
drh82a48512003-09-06 22:45:20 +000052 sqlite_bind $VM 1 {test value 1} normal
53 sqlite_step $VM N VALUES COLNAMES
54} SQLITE_DONE
55do_test bind-1.5 {
56 execsql {SELECT rowid, * FROM t1}
57} {1 {} {} {} 2 {test value 1} {} {}}
58do_test bind-1.6 {
danielk1977106bb232004-05-21 10:08:53 +000059 sqlite3_reset $VM
drh82a48512003-09-06 22:45:20 +000060 sqlite_bind $VM 3 {'test value 2'} normal
61 sqlite_step $VM N VALUES COLNAMES
62} SQLITE_DONE
63do_test bind-1.7 {
64 execsql {SELECT rowid, * FROM t1}
65} {1 {} {} {} 2 {test value 1} {} {} 3 {test value 1} {} {'test value 2'}}
66do_test bind-1.8 {
danielk1977106bb232004-05-21 10:08:53 +000067 sqlite3_reset $VM
drh82a48512003-09-06 22:45:20 +000068 set sqlite_static_bind_value 123
69 sqlite_bind $VM 1 {} static
70 sqlite_bind $VM 2 {abcdefg} normal
71 sqlite_bind $VM 3 {} null
72 execsql {DELETE FROM t1}
73 sqlite_step $VM N VALUES COLNAMES
74 execsql {SELECT rowid, * FROM t1}
75} {1 123 abcdefg {}}
76do_test bind-1.9 {
danielk1977106bb232004-05-21 10:08:53 +000077 sqlite3_reset $VM
drh82a48512003-09-06 22:45:20 +000078 sqlite_bind $VM 1 {456} normal
79 sqlite_step $VM N VALUES COLNAMES
80 execsql {SELECT rowid, * FROM t1}
81} {1 123 abcdefg {} 2 456 abcdefg {}}
82
drh82a48512003-09-06 22:45:20 +000083do_test bind-1.99 {
danielk1977106bb232004-05-21 10:08:53 +000084 sqlite3_finalize $VM
danielk19773cf86062004-05-26 10:11:05 +000085} SQLITE_OK
drh82a48512003-09-06 22:45:20 +000086
danielk197751e3d8e2004-05-20 01:12:34 +000087do_test bind-2.1 {
88 execsql {
89 DELETE FROM t1;
90 }
danielk19772f2322f2004-05-21 02:11:40 +000091 set VM [sqlite3_prepare $DB {INSERT INTO t1 VALUES(?,?,?)} -1 TAIL]
danielk197751e3d8e2004-05-20 01:12:34 +000092 set TAIL
93} {}
94
95# 32 bit Integers
96do_test bind-2.2 {
drh241db312004-06-22 12:46:53 +000097 sqlite3_bind_int $VM 1 123
98 sqlite3_bind_int $VM 2 456
99 sqlite3_bind_int $VM 3 789
danielk197751e3d8e2004-05-20 01:12:34 +0000100 sqlite_step $VM N VALUES COLNAMES
danielk1977106bb232004-05-21 10:08:53 +0000101 sqlite3_reset $VM
danielk197751e3d8e2004-05-20 01:12:34 +0000102 execsql {SELECT rowid, * FROM t1}
103} {1 123 456 789}
104do_test bind-2.3 {
drh241db312004-06-22 12:46:53 +0000105 sqlite3_bind_int $VM 2 -2000000000
106 sqlite3_bind_int $VM 3 2000000000
danielk197751e3d8e2004-05-20 01:12:34 +0000107 sqlite_step $VM N VALUES COLNAMES
danielk1977106bb232004-05-21 10:08:53 +0000108 sqlite3_reset $VM
danielk197751e3d8e2004-05-20 01:12:34 +0000109 execsql {SELECT rowid, * FROM t1}
110} {1 123 456 789 2 123 -2000000000 2000000000}
111do_test bind-2.4 {
danielk197735bb9d02004-05-24 12:55:54 +0000112 execsql {SELECT typeof(a), typeof(b), typeof(c) FROM t1}
113} {integer integer integer integer integer integer}
danielk197751e3d8e2004-05-20 01:12:34 +0000114do_test bind-2.5 {
115 execsql {
116 DELETE FROM t1;
117 }
118} {}
119
120# 64 bit Integers
121do_test bind-3.1 {
122 sqlite3_bind_int64 $VM 1 32
123 sqlite3_bind_int64 $VM 2 -2000000000000
124 sqlite3_bind_int64 $VM 3 2000000000000
125 sqlite_step $VM N VALUES COLNAMES
danielk1977106bb232004-05-21 10:08:53 +0000126 sqlite3_reset $VM
danielk197751e3d8e2004-05-20 01:12:34 +0000127 execsql {SELECT rowid, * FROM t1}
128} {1 32 -2000000000000 2000000000000}
129do_test bind-3.2 {
danielk197735bb9d02004-05-24 12:55:54 +0000130 execsql {SELECT typeof(a), typeof(b), typeof(c) FROM t1}
131} {integer integer integer}
danielk197751e3d8e2004-05-20 01:12:34 +0000132do_test bind-3.3 {
133 execsql {
134 DELETE FROM t1;
135 }
136} {}
137
138# Doubles
139do_test bind-4.1 {
140 sqlite3_bind_double $VM 1 1234.1234
141 sqlite3_bind_double $VM 2 0.00001
142 sqlite3_bind_double $VM 3 123456789
143 sqlite_step $VM N VALUES COLNAMES
danielk1977106bb232004-05-21 10:08:53 +0000144 sqlite3_reset $VM
danielk197751e3d8e2004-05-20 01:12:34 +0000145 execsql {SELECT rowid, * FROM t1}
146} {1 1234.1234 1e-05 123456789}
147do_test bind-4.2 {
danielk197735bb9d02004-05-24 12:55:54 +0000148 execsql {SELECT typeof(a), typeof(b), typeof(c) FROM t1}
149} {real real real}
danielk197751e3d8e2004-05-20 01:12:34 +0000150do_test bind-4.3 {
151 execsql {
152 DELETE FROM t1;
153 }
154} {}
155
156# NULL
157do_test bind-5.1 {
158 sqlite3_bind_null $VM 1
159 sqlite3_bind_null $VM 2
160 sqlite3_bind_null $VM 3
161 sqlite_step $VM N VALUES COLNAMES
danielk1977106bb232004-05-21 10:08:53 +0000162 sqlite3_reset $VM
danielk197751e3d8e2004-05-20 01:12:34 +0000163 execsql {SELECT rowid, * FROM t1}
164} {1 {} {} {}}
165do_test bind-5.2 {
danielk197735bb9d02004-05-24 12:55:54 +0000166 execsql {SELECT typeof(a), typeof(b), typeof(c) FROM t1}
167} {null null null}
danielk197751e3d8e2004-05-20 01:12:34 +0000168do_test bind-5.3 {
169 execsql {
170 DELETE FROM t1;
171 }
172} {}
173
174# UTF-8 text
175do_test bind-6.1 {
176 sqlite3_bind_text $VM 1 hellothere 5
danielk1977c572ef72004-05-27 09:28:41 +0000177 sqlite3_bind_text $VM 2 ".." 1
danielk197751e3d8e2004-05-20 01:12:34 +0000178 sqlite3_bind_text $VM 3 world -1
179 sqlite_step $VM N VALUES COLNAMES
danielk1977106bb232004-05-21 10:08:53 +0000180 sqlite3_reset $VM
danielk197751e3d8e2004-05-20 01:12:34 +0000181 execsql {SELECT rowid, * FROM t1}
182} {1 hello . world}
183do_test bind-6.2 {
danielk197735bb9d02004-05-24 12:55:54 +0000184 execsql {SELECT typeof(a), typeof(b), typeof(c) FROM t1}
185} {text text text}
danielk197751e3d8e2004-05-20 01:12:34 +0000186do_test bind-6.3 {
187 execsql {
188 DELETE FROM t1;
189 }
190} {}
191
192# UTF-16 text
193do_test bind-7.1 {
194 sqlite3_bind_text16 $VM 1 [encoding convertto unicode hellothere] 10
195 sqlite3_bind_text16 $VM 2 [encoding convertto unicode ""] 0
196 sqlite3_bind_text16 $VM 3 [encoding convertto unicode world] 10
197 sqlite_step $VM N VALUES COLNAMES
danielk1977106bb232004-05-21 10:08:53 +0000198 sqlite3_reset $VM
danielk197751e3d8e2004-05-20 01:12:34 +0000199 execsql {SELECT rowid, * FROM t1}
200} {1 hello {} world}
201do_test bind-7.2 {
danielk197735bb9d02004-05-24 12:55:54 +0000202 execsql {SELECT typeof(a), typeof(b), typeof(c) FROM t1}
203} {text text text}
danielk197751e3d8e2004-05-20 01:12:34 +0000204do_test bind-7.3 {
205 execsql {
206 DELETE FROM t1;
207 }
208} {}
209
danielk19776622cce2004-05-20 11:00:52 +0000210# Test that the 'out of range' error works.
211do_test bind-8.1 {
212 catch { sqlite3_bind_null $VM 0 }
213} {1}
214do_test bind-8.2 {
215 sqlite3_errmsg $DB
216} {bind index out of range}
217do_test bind-8.3 {
218 encoding convertfrom unicode [sqlite3_errmsg16 $DB]
219} {bind index out of range}
220do_test bind-8.4 {
221 sqlite3_bind_null $VM 1
222 sqlite3_errmsg $DB
223} {not an error}
224do_test bind-8.5 {
225 catch { sqlite3_bind_null $VM 4 }
226} {1}
227do_test bind-8.6 {
228 sqlite3_errmsg $DB
229} {bind index out of range}
230do_test bind-8.7 {
231 encoding convertfrom unicode [sqlite3_errmsg16 $DB]
232} {bind index out of range}
233
danielk1977f4618892004-06-28 13:09:11 +0000234do_test bind-8.8 {
235 catch { sqlite3_bind_blob $VM 0 "abc" 3 }
236} {1}
237do_test bind-8.9 {
238 catch { sqlite3_bind_blob $VM 4 "abc" 3 }
239} {1}
240do_test bind-8.10 {
241 catch { sqlite3_bind_text $VM 0 "abc" 3 }
242} {1}
243do_test bind-8.11 {
244 catch { sqlite3_bind_text16 $VM 4 "abc" 2 }
245} {1}
246do_test bind-8.12 {
247 catch { sqlite3_bind_int $VM 0 5 }
248} {1}
249do_test bind-8.13 {
250 catch { sqlite3_bind_int $VM 4 5 }
251} {1}
252do_test bind-8.14 {
253 catch { sqlite3_bind_double $VM 0 5.0 }
254} {1}
255do_test bind-8.15 {
256 catch { sqlite3_bind_double $VM 4 6.0 }
257} {1}
danielk19776622cce2004-05-20 11:00:52 +0000258
259do_test bind-9.99 {
danielk1977106bb232004-05-21 10:08:53 +0000260 sqlite3_finalize $VM
danielk19773cf86062004-05-26 10:11:05 +0000261} SQLITE_OK
danielk197751e3d8e2004-05-20 01:12:34 +0000262
263
drh82a48512003-09-06 22:45:20 +0000264
265finish_test