drh | 82a4851 | 2003-09-06 22:45:20 +0000 | [diff] [blame] | 1 | # 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 | # |
drh | bf8aa2a | 2005-12-02 02:44:05 +0000 | [diff] [blame^] | 14 | # $Id: bind.test,v 1.33 2005/12/02 02:44:06 drh Exp $ |
drh | 82a4851 | 2003-09-06 22:45:20 +0000 | [diff] [blame] | 15 | # |
| 16 | |
| 17 | set testdir [file dirname $argv0] |
| 18 | source $testdir/tester.tcl |
| 19 | |
danielk1977 | 17240fd | 2004-05-26 00:07:25 +0000 | [diff] [blame] | 20 | proc 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} { |
drh | eb2e176 | 2004-05-27 01:53:56 +0000 | [diff] [blame] | 31 | lappend vals [sqlite3_column_text $stmt $i] |
danielk1977 | 17240fd | 2004-05-26 00:07:25 +0000 | [diff] [blame] | 32 | } |
| 33 | |
| 34 | return $rc |
| 35 | } |
| 36 | |
drh | 82a4851 | 2003-09-06 22:45:20 +0000 | [diff] [blame] | 37 | do_test bind-1.1 { |
| 38 | db close |
drh | ef4ac8f | 2004-06-19 00:16:31 +0000 | [diff] [blame] | 39 | set DB [sqlite3 db test.db] |
drh | 2c6674c | 2004-08-25 04:07:01 +0000 | [diff] [blame] | 40 | execsql {CREATE TABLE t1(a,b,c);} |
| 41 | set VM [sqlite3_prepare $DB {INSERT INTO t1 VALUES(:1,?,:abc)} -1 TAIL] |
drh | 82a4851 | 2003-09-06 22:45:20 +0000 | [diff] [blame] | 42 | set TAIL |
| 43 | } {} |
drh | 75f6a03 | 2004-07-15 14:15:00 +0000 | [diff] [blame] | 44 | do_test bind-1.1.1 { |
| 45 | sqlite3_bind_parameter_count $VM |
| 46 | } 3 |
drh | 895d747 | 2004-08-20 16:02:39 +0000 | [diff] [blame] | 47 | do_test bind-1.1.2 { |
| 48 | sqlite3_bind_parameter_name $VM 1 |
drh | 2c6674c | 2004-08-25 04:07:01 +0000 | [diff] [blame] | 49 | } {:1} |
drh | 895d747 | 2004-08-20 16:02:39 +0000 | [diff] [blame] | 50 | do_test bind-1.1.3 { |
| 51 | sqlite3_bind_parameter_name $VM 2 |
| 52 | } {} |
| 53 | do_test bind-1.1.4 { |
| 54 | sqlite3_bind_parameter_name $VM 3 |
drh | 2c6674c | 2004-08-25 04:07:01 +0000 | [diff] [blame] | 55 | } {:abc} |
drh | 82a4851 | 2003-09-06 22:45:20 +0000 | [diff] [blame] | 56 | do_test bind-1.2 { |
| 57 | sqlite_step $VM N VALUES COLNAMES |
| 58 | } {SQLITE_DONE} |
| 59 | do_test bind-1.3 { |
| 60 | execsql {SELECT rowid, * FROM t1} |
| 61 | } {1 {} {} {}} |
| 62 | do_test bind-1.4 { |
danielk1977 | 106bb23 | 2004-05-21 10:08:53 +0000 | [diff] [blame] | 63 | sqlite3_reset $VM |
drh | 82a4851 | 2003-09-06 22:45:20 +0000 | [diff] [blame] | 64 | sqlite_bind $VM 1 {test value 1} normal |
| 65 | sqlite_step $VM N VALUES COLNAMES |
| 66 | } SQLITE_DONE |
| 67 | do_test bind-1.5 { |
| 68 | execsql {SELECT rowid, * FROM t1} |
| 69 | } {1 {} {} {} 2 {test value 1} {} {}} |
| 70 | do_test bind-1.6 { |
danielk1977 | 106bb23 | 2004-05-21 10:08:53 +0000 | [diff] [blame] | 71 | sqlite3_reset $VM |
drh | 82a4851 | 2003-09-06 22:45:20 +0000 | [diff] [blame] | 72 | sqlite_bind $VM 3 {'test value 2'} normal |
| 73 | sqlite_step $VM N VALUES COLNAMES |
| 74 | } SQLITE_DONE |
| 75 | do_test bind-1.7 { |
| 76 | execsql {SELECT rowid, * FROM t1} |
| 77 | } {1 {} {} {} 2 {test value 1} {} {} 3 {test value 1} {} {'test value 2'}} |
| 78 | do_test bind-1.8 { |
danielk1977 | 106bb23 | 2004-05-21 10:08:53 +0000 | [diff] [blame] | 79 | sqlite3_reset $VM |
drh | 82a4851 | 2003-09-06 22:45:20 +0000 | [diff] [blame] | 80 | 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 {}} |
| 88 | do_test bind-1.9 { |
danielk1977 | 106bb23 | 2004-05-21 10:08:53 +0000 | [diff] [blame] | 89 | sqlite3_reset $VM |
drh | 82a4851 | 2003-09-06 22:45:20 +0000 | [diff] [blame] | 90 | 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 | |
drh | 82a4851 | 2003-09-06 22:45:20 +0000 | [diff] [blame] | 95 | do_test bind-1.99 { |
danielk1977 | 106bb23 | 2004-05-21 10:08:53 +0000 | [diff] [blame] | 96 | sqlite3_finalize $VM |
danielk1977 | 3cf8606 | 2004-05-26 10:11:05 +0000 | [diff] [blame] | 97 | } SQLITE_OK |
drh | 82a4851 | 2003-09-06 22:45:20 +0000 | [diff] [blame] | 98 | |
drh | 6bf8957 | 2004-11-03 16:27:01 +0000 | [diff] [blame] | 99 | # Prepare the statement in different ways depending on whether or not |
| 100 | # the $var processing is compiled into the library. |
| 101 | # |
| 102 | ifcapable {tclvar} { |
| 103 | do_test bind-2.1 { |
| 104 | execsql { |
| 105 | DELETE FROM t1; |
| 106 | } |
drh | 288d37f | 2005-06-22 08:48:06 +0000 | [diff] [blame] | 107 | set VM [sqlite3_prepare $DB {INSERT INTO t1 VALUES($one,$::two,$x(-z-))}\ |
drh | 48e5aa2 | 2005-01-11 17:46:41 +0000 | [diff] [blame] | 108 | -1 TX] |
drh | 6bf8957 | 2004-11-03 16:27:01 +0000 | [diff] [blame] | 109 | set TX |
| 110 | } {} |
| 111 | set v1 {$one} |
| 112 | set v2 {$::two} |
drh | 288d37f | 2005-06-22 08:48:06 +0000 | [diff] [blame] | 113 | set v3 {$x(-z-)} |
drh | 6bf8957 | 2004-11-03 16:27:01 +0000 | [diff] [blame] | 114 | } |
| 115 | ifcapable {!tclvar} { |
| 116 | do_test bind-2.1 { |
| 117 | execsql { |
| 118 | DELETE FROM t1; |
| 119 | } |
| 120 | set VM [sqlite3_prepare $DB {INSERT INTO t1 VALUES(:one,:two,:_)} -1 TX] |
| 121 | set TX |
| 122 | } {} |
| 123 | set v1 {:one} |
| 124 | set v2 {:two} |
| 125 | set v3 {:_} |
| 126 | } |
| 127 | |
drh | 895d747 | 2004-08-20 16:02:39 +0000 | [diff] [blame] | 128 | do_test bind-2.1.1 { |
| 129 | sqlite3_bind_parameter_count $VM |
| 130 | } 3 |
| 131 | do_test bind-2.1.2 { |
| 132 | sqlite3_bind_parameter_name $VM 1 |
drh | 6bf8957 | 2004-11-03 16:27:01 +0000 | [diff] [blame] | 133 | } $v1 |
drh | 895d747 | 2004-08-20 16:02:39 +0000 | [diff] [blame] | 134 | do_test bind-2.1.3 { |
| 135 | sqlite3_bind_parameter_name $VM 2 |
drh | 6bf8957 | 2004-11-03 16:27:01 +0000 | [diff] [blame] | 136 | } $v2 |
drh | 895d747 | 2004-08-20 16:02:39 +0000 | [diff] [blame] | 137 | do_test bind-2.1.4 { |
| 138 | sqlite3_bind_parameter_name $VM 3 |
drh | 6bf8957 | 2004-11-03 16:27:01 +0000 | [diff] [blame] | 139 | } $v3 |
drh | fa6bc00 | 2004-09-07 16:19:52 +0000 | [diff] [blame] | 140 | do_test bind-2.1.5 { |
drh | 6bf8957 | 2004-11-03 16:27:01 +0000 | [diff] [blame] | 141 | sqlite3_bind_parameter_index $VM $v1 |
drh | fa6bc00 | 2004-09-07 16:19:52 +0000 | [diff] [blame] | 142 | } 1 |
| 143 | do_test bind-2.1.6 { |
drh | 6bf8957 | 2004-11-03 16:27:01 +0000 | [diff] [blame] | 144 | sqlite3_bind_parameter_index $VM $v2 |
drh | fa6bc00 | 2004-09-07 16:19:52 +0000 | [diff] [blame] | 145 | } 2 |
| 146 | do_test bind-2.1.7 { |
drh | 6bf8957 | 2004-11-03 16:27:01 +0000 | [diff] [blame] | 147 | sqlite3_bind_parameter_index $VM $v3 |
drh | fa6bc00 | 2004-09-07 16:19:52 +0000 | [diff] [blame] | 148 | } 3 |
| 149 | do_test bind-2.1.8 { |
| 150 | sqlite3_bind_parameter_index $VM {:hi} |
| 151 | } 0 |
danielk1977 | 51e3d8e | 2004-05-20 01:12:34 +0000 | [diff] [blame] | 152 | |
| 153 | # 32 bit Integers |
| 154 | do_test bind-2.2 { |
drh | 241db31 | 2004-06-22 12:46:53 +0000 | [diff] [blame] | 155 | sqlite3_bind_int $VM 1 123 |
| 156 | sqlite3_bind_int $VM 2 456 |
| 157 | sqlite3_bind_int $VM 3 789 |
danielk1977 | 51e3d8e | 2004-05-20 01:12:34 +0000 | [diff] [blame] | 158 | sqlite_step $VM N VALUES COLNAMES |
danielk1977 | 106bb23 | 2004-05-21 10:08:53 +0000 | [diff] [blame] | 159 | sqlite3_reset $VM |
danielk1977 | 51e3d8e | 2004-05-20 01:12:34 +0000 | [diff] [blame] | 160 | execsql {SELECT rowid, * FROM t1} |
| 161 | } {1 123 456 789} |
| 162 | do_test bind-2.3 { |
drh | 241db31 | 2004-06-22 12:46:53 +0000 | [diff] [blame] | 163 | sqlite3_bind_int $VM 2 -2000000000 |
| 164 | sqlite3_bind_int $VM 3 2000000000 |
danielk1977 | 51e3d8e | 2004-05-20 01:12:34 +0000 | [diff] [blame] | 165 | sqlite_step $VM N VALUES COLNAMES |
danielk1977 | 106bb23 | 2004-05-21 10:08:53 +0000 | [diff] [blame] | 166 | sqlite3_reset $VM |
danielk1977 | 51e3d8e | 2004-05-20 01:12:34 +0000 | [diff] [blame] | 167 | execsql {SELECT rowid, * FROM t1} |
| 168 | } {1 123 456 789 2 123 -2000000000 2000000000} |
| 169 | do_test bind-2.4 { |
danielk1977 | 35bb9d0 | 2004-05-24 12:55:54 +0000 | [diff] [blame] | 170 | execsql {SELECT typeof(a), typeof(b), typeof(c) FROM t1} |
| 171 | } {integer integer integer integer integer integer} |
danielk1977 | 51e3d8e | 2004-05-20 01:12:34 +0000 | [diff] [blame] | 172 | do_test bind-2.5 { |
| 173 | execsql { |
| 174 | DELETE FROM t1; |
| 175 | } |
| 176 | } {} |
| 177 | |
| 178 | # 64 bit Integers |
| 179 | do_test bind-3.1 { |
| 180 | sqlite3_bind_int64 $VM 1 32 |
| 181 | sqlite3_bind_int64 $VM 2 -2000000000000 |
| 182 | sqlite3_bind_int64 $VM 3 2000000000000 |
| 183 | sqlite_step $VM N VALUES COLNAMES |
danielk1977 | 106bb23 | 2004-05-21 10:08:53 +0000 | [diff] [blame] | 184 | sqlite3_reset $VM |
danielk1977 | 51e3d8e | 2004-05-20 01:12:34 +0000 | [diff] [blame] | 185 | execsql {SELECT rowid, * FROM t1} |
| 186 | } {1 32 -2000000000000 2000000000000} |
| 187 | do_test bind-3.2 { |
danielk1977 | 35bb9d0 | 2004-05-24 12:55:54 +0000 | [diff] [blame] | 188 | execsql {SELECT typeof(a), typeof(b), typeof(c) FROM t1} |
| 189 | } {integer integer integer} |
danielk1977 | 51e3d8e | 2004-05-20 01:12:34 +0000 | [diff] [blame] | 190 | do_test bind-3.3 { |
| 191 | execsql { |
| 192 | DELETE FROM t1; |
| 193 | } |
| 194 | } {} |
| 195 | |
| 196 | # Doubles |
| 197 | do_test bind-4.1 { |
| 198 | sqlite3_bind_double $VM 1 1234.1234 |
| 199 | sqlite3_bind_double $VM 2 0.00001 |
| 200 | sqlite3_bind_double $VM 3 123456789 |
| 201 | sqlite_step $VM N VALUES COLNAMES |
danielk1977 | 106bb23 | 2004-05-21 10:08:53 +0000 | [diff] [blame] | 202 | sqlite3_reset $VM |
drh | ab34b8f | 2005-03-20 23:18:57 +0000 | [diff] [blame] | 203 | set x [execsql {SELECT rowid, * FROM t1}] |
| 204 | regsub {1e-005} $x {1e-05} y |
| 205 | set y |
drh | 92febd9 | 2004-08-20 18:34:20 +0000 | [diff] [blame] | 206 | } {1 1234.1234 1e-05 123456789.0} |
danielk1977 | 51e3d8e | 2004-05-20 01:12:34 +0000 | [diff] [blame] | 207 | do_test bind-4.2 { |
danielk1977 | 35bb9d0 | 2004-05-24 12:55:54 +0000 | [diff] [blame] | 208 | execsql {SELECT typeof(a), typeof(b), typeof(c) FROM t1} |
| 209 | } {real real real} |
danielk1977 | 51e3d8e | 2004-05-20 01:12:34 +0000 | [diff] [blame] | 210 | do_test bind-4.3 { |
| 211 | execsql { |
| 212 | DELETE FROM t1; |
| 213 | } |
| 214 | } {} |
| 215 | |
| 216 | # NULL |
| 217 | do_test bind-5.1 { |
| 218 | sqlite3_bind_null $VM 1 |
| 219 | sqlite3_bind_null $VM 2 |
| 220 | sqlite3_bind_null $VM 3 |
| 221 | sqlite_step $VM N VALUES COLNAMES |
danielk1977 | 106bb23 | 2004-05-21 10:08:53 +0000 | [diff] [blame] | 222 | sqlite3_reset $VM |
danielk1977 | 51e3d8e | 2004-05-20 01:12:34 +0000 | [diff] [blame] | 223 | execsql {SELECT rowid, * FROM t1} |
| 224 | } {1 {} {} {}} |
| 225 | do_test bind-5.2 { |
danielk1977 | 35bb9d0 | 2004-05-24 12:55:54 +0000 | [diff] [blame] | 226 | execsql {SELECT typeof(a), typeof(b), typeof(c) FROM t1} |
| 227 | } {null null null} |
danielk1977 | 51e3d8e | 2004-05-20 01:12:34 +0000 | [diff] [blame] | 228 | do_test bind-5.3 { |
| 229 | execsql { |
| 230 | DELETE FROM t1; |
| 231 | } |
| 232 | } {} |
| 233 | |
| 234 | # UTF-8 text |
| 235 | do_test bind-6.1 { |
| 236 | sqlite3_bind_text $VM 1 hellothere 5 |
danielk1977 | c572ef7 | 2004-05-27 09:28:41 +0000 | [diff] [blame] | 237 | sqlite3_bind_text $VM 2 ".." 1 |
danielk1977 | 51e3d8e | 2004-05-20 01:12:34 +0000 | [diff] [blame] | 238 | sqlite3_bind_text $VM 3 world -1 |
| 239 | sqlite_step $VM N VALUES COLNAMES |
danielk1977 | 106bb23 | 2004-05-21 10:08:53 +0000 | [diff] [blame] | 240 | sqlite3_reset $VM |
danielk1977 | 51e3d8e | 2004-05-20 01:12:34 +0000 | [diff] [blame] | 241 | execsql {SELECT rowid, * FROM t1} |
| 242 | } {1 hello . world} |
| 243 | do_test bind-6.2 { |
danielk1977 | 35bb9d0 | 2004-05-24 12:55:54 +0000 | [diff] [blame] | 244 | execsql {SELECT typeof(a), typeof(b), typeof(c) FROM t1} |
| 245 | } {text text text} |
danielk1977 | 51e3d8e | 2004-05-20 01:12:34 +0000 | [diff] [blame] | 246 | do_test bind-6.3 { |
| 247 | execsql { |
| 248 | DELETE FROM t1; |
| 249 | } |
| 250 | } {} |
| 251 | |
| 252 | # UTF-16 text |
drh | 6c62608 | 2004-11-14 21:56:29 +0000 | [diff] [blame] | 253 | ifcapable {utf16} { |
| 254 | do_test bind-7.1 { |
| 255 | sqlite3_bind_text16 $VM 1 [encoding convertto unicode hellothere] 10 |
| 256 | sqlite3_bind_text16 $VM 2 [encoding convertto unicode ""] 0 |
| 257 | sqlite3_bind_text16 $VM 3 [encoding convertto unicode world] 10 |
| 258 | sqlite_step $VM N VALUES COLNAMES |
| 259 | sqlite3_reset $VM |
| 260 | execsql {SELECT rowid, * FROM t1} |
| 261 | } {1 hello {} world} |
| 262 | do_test bind-7.2 { |
| 263 | execsql {SELECT typeof(a), typeof(b), typeof(c) FROM t1} |
| 264 | } {text text text} |
| 265 | } |
danielk1977 | 51e3d8e | 2004-05-20 01:12:34 +0000 | [diff] [blame] | 266 | do_test bind-7.3 { |
| 267 | execsql { |
| 268 | DELETE FROM t1; |
| 269 | } |
| 270 | } {} |
| 271 | |
danielk1977 | 6622cce | 2004-05-20 11:00:52 +0000 | [diff] [blame] | 272 | # Test that the 'out of range' error works. |
| 273 | do_test bind-8.1 { |
| 274 | catch { sqlite3_bind_null $VM 0 } |
| 275 | } {1} |
| 276 | do_test bind-8.2 { |
| 277 | sqlite3_errmsg $DB |
drh | b08153d | 2004-11-20 20:18:55 +0000 | [diff] [blame] | 278 | } {bind or column index out of range} |
drh | 6c62608 | 2004-11-14 21:56:29 +0000 | [diff] [blame] | 279 | ifcapable {utf16} { |
| 280 | do_test bind-8.3 { |
| 281 | encoding convertfrom unicode [sqlite3_errmsg16 $DB] |
drh | b08153d | 2004-11-20 20:18:55 +0000 | [diff] [blame] | 282 | } {bind or column index out of range} |
drh | 6c62608 | 2004-11-14 21:56:29 +0000 | [diff] [blame] | 283 | } |
danielk1977 | 6622cce | 2004-05-20 11:00:52 +0000 | [diff] [blame] | 284 | do_test bind-8.4 { |
| 285 | sqlite3_bind_null $VM 1 |
| 286 | sqlite3_errmsg $DB |
| 287 | } {not an error} |
| 288 | do_test bind-8.5 { |
| 289 | catch { sqlite3_bind_null $VM 4 } |
| 290 | } {1} |
| 291 | do_test bind-8.6 { |
| 292 | sqlite3_errmsg $DB |
drh | b08153d | 2004-11-20 20:18:55 +0000 | [diff] [blame] | 293 | } {bind or column index out of range} |
drh | 6c62608 | 2004-11-14 21:56:29 +0000 | [diff] [blame] | 294 | ifcapable {utf16} { |
| 295 | do_test bind-8.7 { |
| 296 | encoding convertfrom unicode [sqlite3_errmsg16 $DB] |
drh | b08153d | 2004-11-20 20:18:55 +0000 | [diff] [blame] | 297 | } {bind or column index out of range} |
drh | 6c62608 | 2004-11-14 21:56:29 +0000 | [diff] [blame] | 298 | } |
danielk1977 | 6622cce | 2004-05-20 11:00:52 +0000 | [diff] [blame] | 299 | |
danielk1977 | f461889 | 2004-06-28 13:09:11 +0000 | [diff] [blame] | 300 | do_test bind-8.8 { |
| 301 | catch { sqlite3_bind_blob $VM 0 "abc" 3 } |
| 302 | } {1} |
| 303 | do_test bind-8.9 { |
| 304 | catch { sqlite3_bind_blob $VM 4 "abc" 3 } |
| 305 | } {1} |
| 306 | do_test bind-8.10 { |
| 307 | catch { sqlite3_bind_text $VM 0 "abc" 3 } |
| 308 | } {1} |
drh | 6c62608 | 2004-11-14 21:56:29 +0000 | [diff] [blame] | 309 | ifcapable {utf16} { |
| 310 | do_test bind-8.11 { |
| 311 | catch { sqlite3_bind_text16 $VM 4 "abc" 2 } |
| 312 | } {1} |
| 313 | } |
danielk1977 | f461889 | 2004-06-28 13:09:11 +0000 | [diff] [blame] | 314 | do_test bind-8.12 { |
| 315 | catch { sqlite3_bind_int $VM 0 5 } |
| 316 | } {1} |
| 317 | do_test bind-8.13 { |
| 318 | catch { sqlite3_bind_int $VM 4 5 } |
| 319 | } {1} |
| 320 | do_test bind-8.14 { |
| 321 | catch { sqlite3_bind_double $VM 0 5.0 } |
| 322 | } {1} |
| 323 | do_test bind-8.15 { |
| 324 | catch { sqlite3_bind_double $VM 4 6.0 } |
| 325 | } {1} |
danielk1977 | 6622cce | 2004-05-20 11:00:52 +0000 | [diff] [blame] | 326 | |
drh | fa6bc00 | 2004-09-07 16:19:52 +0000 | [diff] [blame] | 327 | do_test bind-8.99 { |
danielk1977 | 106bb23 | 2004-05-21 10:08:53 +0000 | [diff] [blame] | 328 | sqlite3_finalize $VM |
danielk1977 | 3cf8606 | 2004-05-26 10:11:05 +0000 | [diff] [blame] | 329 | } SQLITE_OK |
danielk1977 | 51e3d8e | 2004-05-20 01:12:34 +0000 | [diff] [blame] | 330 | |
drh | fa6bc00 | 2004-09-07 16:19:52 +0000 | [diff] [blame] | 331 | do_test bind-9.1 { |
| 332 | execsql { |
| 333 | CREATE TABLE t2(a,b,c,d,e,f); |
| 334 | } |
| 335 | set rc [catch { |
| 336 | sqlite3_prepare $DB { |
| 337 | INSERT INTO t2(a) VALUES(?0) |
| 338 | } -1 TAIL |
| 339 | } msg] |
| 340 | lappend rc $msg |
| 341 | } {1 {(1) variable number must be between ?1 and ?999}} |
| 342 | do_test bind-9.2 { |
| 343 | set rc [catch { |
| 344 | sqlite3_prepare $DB { |
| 345 | INSERT INTO t2(a) VALUES(?1000) |
| 346 | } -1 TAIL |
| 347 | } msg] |
| 348 | lappend rc $msg |
| 349 | } {1 {(1) variable number must be between ?1 and ?999}} |
| 350 | do_test bind-9.3 { |
| 351 | set VM [ |
| 352 | sqlite3_prepare $DB { |
| 353 | INSERT INTO t2(a,b) VALUES(?1,?999) |
| 354 | } -1 TAIL |
| 355 | ] |
| 356 | sqlite3_bind_parameter_count $VM |
| 357 | } {999} |
| 358 | catch {sqlite3_finalize $VM} |
| 359 | do_test bind-9.4 { |
| 360 | set VM [ |
| 361 | sqlite3_prepare $DB { |
| 362 | INSERT INTO t2(a,b,c,d) VALUES(?1,?999,?,?) |
| 363 | } -1 TAIL |
| 364 | ] |
| 365 | sqlite3_bind_parameter_count $VM |
| 366 | } {1001} |
| 367 | do_test bind-9.5 { |
| 368 | sqlite3_bind_int $VM 1 1 |
| 369 | sqlite3_bind_int $VM 999 999 |
| 370 | sqlite3_bind_int $VM 1000 1000 |
| 371 | sqlite3_bind_int $VM 1001 1001 |
| 372 | sqlite3_step $VM |
| 373 | } SQLITE_DONE |
| 374 | do_test bind-9.6 { |
| 375 | sqlite3_finalize $VM |
| 376 | } SQLITE_OK |
| 377 | do_test bind-9.7 { |
| 378 | execsql {SELECT * FROM t2} |
| 379 | } {1 999 1000 1001 {} {}} |
danielk1977 | 51e3d8e | 2004-05-20 01:12:34 +0000 | [diff] [blame] | 380 | |
drh | 6bf8957 | 2004-11-03 16:27:01 +0000 | [diff] [blame] | 381 | ifcapable {tclvar} { |
| 382 | do_test bind-10.1 { |
drh | 6bf8957 | 2004-11-03 16:27:01 +0000 | [diff] [blame] | 383 | set VM [ |
| 384 | sqlite3_prepare $DB { |
| 385 | INSERT INTO t2(a,b,c,d,e,f) VALUES(:abc,$abc,:abc,$ab,$abc,:abc) |
| 386 | } -1 TAIL |
| 387 | ] |
| 388 | sqlite3_bind_parameter_count $VM |
| 389 | } 3 |
| 390 | set v1 {$abc} |
| 391 | set v2 {$ab} |
| 392 | } |
| 393 | ifcapable {!tclvar} { |
| 394 | do_test bind-10.1 { |
drh | 6bf8957 | 2004-11-03 16:27:01 +0000 | [diff] [blame] | 395 | set VM [ |
| 396 | sqlite3_prepare $DB { |
| 397 | INSERT INTO t2(a,b,c,d,e,f) VALUES(:abc,:xyz,:abc,:xy,:xyz,:abc) |
| 398 | } -1 TAIL |
| 399 | ] |
| 400 | sqlite3_bind_parameter_count $VM |
| 401 | } 3 |
| 402 | set v1 {:xyz} |
| 403 | set v2 {:xy} |
| 404 | } |
drh | fa6bc00 | 2004-09-07 16:19:52 +0000 | [diff] [blame] | 405 | do_test bind-10.2 { |
| 406 | sqlite3_bind_parameter_index $VM :abc |
| 407 | } 1 |
| 408 | do_test bind-10.3 { |
drh | 6bf8957 | 2004-11-03 16:27:01 +0000 | [diff] [blame] | 409 | sqlite3_bind_parameter_index $VM $v1 |
drh | fa6bc00 | 2004-09-07 16:19:52 +0000 | [diff] [blame] | 410 | } 2 |
| 411 | do_test bind-10.4 { |
drh | 6bf8957 | 2004-11-03 16:27:01 +0000 | [diff] [blame] | 412 | sqlite3_bind_parameter_index $VM $v2 |
drh | fa6bc00 | 2004-09-07 16:19:52 +0000 | [diff] [blame] | 413 | } 3 |
| 414 | do_test bind-10.5 { |
| 415 | sqlite3_bind_parameter_name $VM 1 |
| 416 | } :abc |
| 417 | do_test bind-10.6 { |
| 418 | sqlite3_bind_parameter_name $VM 2 |
drh | 6bf8957 | 2004-11-03 16:27:01 +0000 | [diff] [blame] | 419 | } $v1 |
drh | fa6bc00 | 2004-09-07 16:19:52 +0000 | [diff] [blame] | 420 | do_test bind-10.7 { |
| 421 | sqlite3_bind_parameter_name $VM 3 |
drh | 6bf8957 | 2004-11-03 16:27:01 +0000 | [diff] [blame] | 422 | } $v2 |
drh | c5cdca6 | 2005-01-11 16:54:14 +0000 | [diff] [blame] | 423 | do_test bind-10.7.1 { |
| 424 | sqlite3_bind_parameter_name 0 1 ;# Ignore if VM is NULL |
| 425 | } {} |
| 426 | do_test bind-10.7.2 { |
| 427 | sqlite3_bind_parameter_name $VM 0 ;# Ignore if index too small |
| 428 | } {} |
| 429 | do_test bind-10.7.3 { |
| 430 | sqlite3_bind_parameter_name $VM 4 ;# Ignore if index is too big |
| 431 | } {} |
drh | fa6bc00 | 2004-09-07 16:19:52 +0000 | [diff] [blame] | 432 | do_test bind-10.8 { |
| 433 | sqlite3_bind_int $VM 1 1 |
| 434 | sqlite3_bind_int $VM 2 2 |
| 435 | sqlite3_bind_int $VM 3 3 |
| 436 | sqlite3_step $VM |
| 437 | } SQLITE_DONE |
drh | c5cdca6 | 2005-01-11 16:54:14 +0000 | [diff] [blame] | 438 | do_test bind-10.8.1 { |
| 439 | # Binding attempts after program start should fail |
| 440 | set rc [catch { |
| 441 | sqlite3_bind_int $VM 1 1 |
| 442 | } msg] |
| 443 | lappend rc $msg |
| 444 | } {1 {}} |
drh | fa6bc00 | 2004-09-07 16:19:52 +0000 | [diff] [blame] | 445 | do_test bind-10.9 { |
| 446 | sqlite3_finalize $VM |
| 447 | } SQLITE_OK |
| 448 | do_test bind-10.10 { |
| 449 | execsql {SELECT * FROM t2} |
| 450 | } {1 999 1000 1001 {} {} 1 2 1 3 2 1} |
drh | 82a4851 | 2003-09-06 22:45:20 +0000 | [diff] [blame] | 451 | |
drh | 971a7c8 | 2004-09-24 12:48:12 +0000 | [diff] [blame] | 452 | # Ticket #918 |
| 453 | # |
| 454 | do_test bind-10.11 { |
| 455 | catch {sqlite3_finalize $VM} |
| 456 | set VM [ |
| 457 | sqlite3_prepare $DB { |
| 458 | INSERT INTO t2(a,b,c,d,e,f) VALUES(:abc,?,?4,:pqr,:abc,?4) |
| 459 | } -1 TAIL |
| 460 | ] |
| 461 | sqlite3_bind_parameter_count $VM |
| 462 | } 5 |
drh | c5cdca6 | 2005-01-11 16:54:14 +0000 | [diff] [blame] | 463 | do_test bind-10.11.1 { |
| 464 | sqlite3_bind_parameter_index 0 :xyz ;# ignore NULL VM arguments |
| 465 | } 0 |
drh | 971a7c8 | 2004-09-24 12:48:12 +0000 | [diff] [blame] | 466 | do_test bind-10.12 { |
| 467 | sqlite3_bind_parameter_index $VM :xyz |
| 468 | } 0 |
| 469 | do_test bind-10.13 { |
| 470 | sqlite3_bind_parameter_index $VM {} |
| 471 | } 0 |
| 472 | do_test bind-10.14 { |
| 473 | sqlite3_bind_parameter_index $VM :pqr |
| 474 | } 5 |
| 475 | do_test bind-10.15 { |
| 476 | sqlite3_bind_parameter_index $VM ?4 |
| 477 | } 4 |
| 478 | do_test bind-10.16 { |
| 479 | sqlite3_bind_parameter_name $VM 1 |
| 480 | } :abc |
danielk1977 | b3bce66 | 2005-01-29 08:32:43 +0000 | [diff] [blame] | 481 | do_test bind-10.17 { |
drh | 971a7c8 | 2004-09-24 12:48:12 +0000 | [diff] [blame] | 482 | sqlite3_bind_parameter_name $VM 2 |
| 483 | } {} |
danielk1977 | b3bce66 | 2005-01-29 08:32:43 +0000 | [diff] [blame] | 484 | do_test bind-10.18 { |
drh | 971a7c8 | 2004-09-24 12:48:12 +0000 | [diff] [blame] | 485 | sqlite3_bind_parameter_name $VM 3 |
| 486 | } {} |
danielk1977 | b3bce66 | 2005-01-29 08:32:43 +0000 | [diff] [blame] | 487 | do_test bind-10.19 { |
drh | 971a7c8 | 2004-09-24 12:48:12 +0000 | [diff] [blame] | 488 | sqlite3_bind_parameter_name $VM 4 |
| 489 | } {?4} |
danielk1977 | b3bce66 | 2005-01-29 08:32:43 +0000 | [diff] [blame] | 490 | do_test bind-10.20 { |
drh | 971a7c8 | 2004-09-24 12:48:12 +0000 | [diff] [blame] | 491 | sqlite3_bind_parameter_name $VM 5 |
| 492 | } :pqr |
| 493 | catch {sqlite3_finalize $VM} |
| 494 | |
drh | 48e5aa2 | 2005-01-11 17:46:41 +0000 | [diff] [blame] | 495 | # Make sure we catch an unterminated "(" in a Tcl-style variable name |
| 496 | # |
danielk1977 | 4489f9b | 2005-01-20 02:17:01 +0000 | [diff] [blame] | 497 | ifcapable tclvar { |
| 498 | do_test bind-11.1 { |
| 499 | catchsql {SELECT * FROM sqlite_master WHERE name=$abc(123 and sql NOT NULL;} |
| 500 | } {1 {unrecognized token: "$abc(123"}} |
| 501 | } |
drh | 48e5aa2 | 2005-01-11 17:46:41 +0000 | [diff] [blame] | 502 | |
drh | bf8aa2a | 2005-12-02 02:44:05 +0000 | [diff] [blame^] | 503 | # Test the ability to bind text that contains embedded '\000' characters. |
| 504 | # Make sure we can recover the enter input string. |
| 505 | # |
| 506 | do_test bind-12.1 { |
| 507 | execsql { |
| 508 | CREATE TABLE t3(x BLOB); |
| 509 | } |
| 510 | set VM [sqlite3_prepare $DB {INSERT INTO t3 VALUES(?)} -1 TAIL] |
| 511 | sqlite_bind $VM 1 not-used blob10 |
| 512 | sqlite3_step $VM |
| 513 | sqlite3_finalize $VM |
| 514 | execsql { |
| 515 | SELECT typeof(x), length(x), quote(x), |
| 516 | length(cast(x AS BLOB)), quote(cast(x AS BLOB)) FROM t3 |
| 517 | } |
| 518 | } {text 3 'abc' 10 X'6162630078797A007071'} |
| 519 | |
| 520 | |
drh | 82a4851 | 2003-09-06 22:45:20 +0000 | [diff] [blame] | 521 | finish_test |