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 | 78ddb7c | 2008-04-16 16:11:49 +0000 | [diff] [blame^] | 14 | # $Id: bind.test,v 1.42 2008/04/16 16:11:49 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 { |
drh | dddca28 | 2006-01-03 00:33:50 +0000 | [diff] [blame] | 38 | set DB [sqlite3_connection_pointer db] |
drh | 2c6674c | 2004-08-25 04:07:01 +0000 | [diff] [blame] | 39 | execsql {CREATE TABLE t1(a,b,c);} |
| 40 | set VM [sqlite3_prepare $DB {INSERT INTO t1 VALUES(:1,?,:abc)} -1 TAIL] |
drh | 82a4851 | 2003-09-06 22:45:20 +0000 | [diff] [blame] | 41 | set TAIL |
| 42 | } {} |
drh | 75f6a03 | 2004-07-15 14:15:00 +0000 | [diff] [blame] | 43 | do_test bind-1.1.1 { |
| 44 | sqlite3_bind_parameter_count $VM |
| 45 | } 3 |
drh | 895d747 | 2004-08-20 16:02:39 +0000 | [diff] [blame] | 46 | do_test bind-1.1.2 { |
| 47 | sqlite3_bind_parameter_name $VM 1 |
drh | 2c6674c | 2004-08-25 04:07:01 +0000 | [diff] [blame] | 48 | } {:1} |
drh | 895d747 | 2004-08-20 16:02:39 +0000 | [diff] [blame] | 49 | do_test bind-1.1.3 { |
| 50 | sqlite3_bind_parameter_name $VM 2 |
| 51 | } {} |
| 52 | do_test bind-1.1.4 { |
| 53 | sqlite3_bind_parameter_name $VM 3 |
drh | 2c6674c | 2004-08-25 04:07:01 +0000 | [diff] [blame] | 54 | } {:abc} |
drh | 82a4851 | 2003-09-06 22:45:20 +0000 | [diff] [blame] | 55 | do_test bind-1.2 { |
| 56 | sqlite_step $VM N VALUES COLNAMES |
| 57 | } {SQLITE_DONE} |
| 58 | do_test bind-1.3 { |
| 59 | execsql {SELECT rowid, * FROM t1} |
| 60 | } {1 {} {} {}} |
| 61 | do_test bind-1.4 { |
danielk1977 | 106bb23 | 2004-05-21 10:08:53 +0000 | [diff] [blame] | 62 | sqlite3_reset $VM |
drh | 82a4851 | 2003-09-06 22:45:20 +0000 | [diff] [blame] | 63 | sqlite_bind $VM 1 {test value 1} normal |
| 64 | sqlite_step $VM N VALUES COLNAMES |
| 65 | } SQLITE_DONE |
| 66 | do_test bind-1.5 { |
| 67 | execsql {SELECT rowid, * FROM t1} |
| 68 | } {1 {} {} {} 2 {test value 1} {} {}} |
| 69 | do_test bind-1.6 { |
danielk1977 | 106bb23 | 2004-05-21 10:08:53 +0000 | [diff] [blame] | 70 | sqlite3_reset $VM |
drh | 82a4851 | 2003-09-06 22:45:20 +0000 | [diff] [blame] | 71 | sqlite_bind $VM 3 {'test value 2'} normal |
| 72 | sqlite_step $VM N VALUES COLNAMES |
| 73 | } SQLITE_DONE |
| 74 | do_test bind-1.7 { |
| 75 | execsql {SELECT rowid, * FROM t1} |
| 76 | } {1 {} {} {} 2 {test value 1} {} {} 3 {test value 1} {} {'test value 2'}} |
| 77 | do_test bind-1.8 { |
danielk1977 | 106bb23 | 2004-05-21 10:08:53 +0000 | [diff] [blame] | 78 | sqlite3_reset $VM |
drh | 82a4851 | 2003-09-06 22:45:20 +0000 | [diff] [blame] | 79 | set sqlite_static_bind_value 123 |
| 80 | sqlite_bind $VM 1 {} static |
| 81 | sqlite_bind $VM 2 {abcdefg} normal |
| 82 | sqlite_bind $VM 3 {} null |
| 83 | execsql {DELETE FROM t1} |
| 84 | sqlite_step $VM N VALUES COLNAMES |
| 85 | execsql {SELECT rowid, * FROM t1} |
| 86 | } {1 123 abcdefg {}} |
| 87 | do_test bind-1.9 { |
danielk1977 | 106bb23 | 2004-05-21 10:08:53 +0000 | [diff] [blame] | 88 | sqlite3_reset $VM |
drh | 82a4851 | 2003-09-06 22:45:20 +0000 | [diff] [blame] | 89 | sqlite_bind $VM 1 {456} normal |
| 90 | sqlite_step $VM N VALUES COLNAMES |
| 91 | execsql {SELECT rowid, * FROM t1} |
| 92 | } {1 123 abcdefg {} 2 456 abcdefg {}} |
| 93 | |
drh | 82a4851 | 2003-09-06 22:45:20 +0000 | [diff] [blame] | 94 | do_test bind-1.99 { |
danielk1977 | 106bb23 | 2004-05-21 10:08:53 +0000 | [diff] [blame] | 95 | sqlite3_finalize $VM |
danielk1977 | 3cf8606 | 2004-05-26 10:11:05 +0000 | [diff] [blame] | 96 | } SQLITE_OK |
drh | 82a4851 | 2003-09-06 22:45:20 +0000 | [diff] [blame] | 97 | |
drh | 6bf8957 | 2004-11-03 16:27:01 +0000 | [diff] [blame] | 98 | # Prepare the statement in different ways depending on whether or not |
| 99 | # the $var processing is compiled into the library. |
| 100 | # |
| 101 | ifcapable {tclvar} { |
| 102 | do_test bind-2.1 { |
| 103 | execsql { |
| 104 | DELETE FROM t1; |
| 105 | } |
drh | 288d37f | 2005-06-22 08:48:06 +0000 | [diff] [blame] | 106 | set VM [sqlite3_prepare $DB {INSERT INTO t1 VALUES($one,$::two,$x(-z-))}\ |
drh | 48e5aa2 | 2005-01-11 17:46:41 +0000 | [diff] [blame] | 107 | -1 TX] |
drh | 6bf8957 | 2004-11-03 16:27:01 +0000 | [diff] [blame] | 108 | set TX |
| 109 | } {} |
| 110 | set v1 {$one} |
| 111 | set v2 {$::two} |
drh | 288d37f | 2005-06-22 08:48:06 +0000 | [diff] [blame] | 112 | set v3 {$x(-z-)} |
drh | 6bf8957 | 2004-11-03 16:27:01 +0000 | [diff] [blame] | 113 | } |
| 114 | ifcapable {!tclvar} { |
| 115 | do_test bind-2.1 { |
| 116 | execsql { |
| 117 | DELETE FROM t1; |
| 118 | } |
| 119 | set VM [sqlite3_prepare $DB {INSERT INTO t1 VALUES(:one,:two,:_)} -1 TX] |
| 120 | set TX |
| 121 | } {} |
| 122 | set v1 {:one} |
| 123 | set v2 {:two} |
| 124 | set v3 {:_} |
| 125 | } |
| 126 | |
drh | 895d747 | 2004-08-20 16:02:39 +0000 | [diff] [blame] | 127 | do_test bind-2.1.1 { |
| 128 | sqlite3_bind_parameter_count $VM |
| 129 | } 3 |
| 130 | do_test bind-2.1.2 { |
| 131 | sqlite3_bind_parameter_name $VM 1 |
drh | 6bf8957 | 2004-11-03 16:27:01 +0000 | [diff] [blame] | 132 | } $v1 |
drh | 895d747 | 2004-08-20 16:02:39 +0000 | [diff] [blame] | 133 | do_test bind-2.1.3 { |
| 134 | sqlite3_bind_parameter_name $VM 2 |
drh | 6bf8957 | 2004-11-03 16:27:01 +0000 | [diff] [blame] | 135 | } $v2 |
drh | 895d747 | 2004-08-20 16:02:39 +0000 | [diff] [blame] | 136 | do_test bind-2.1.4 { |
| 137 | sqlite3_bind_parameter_name $VM 3 |
drh | 6bf8957 | 2004-11-03 16:27:01 +0000 | [diff] [blame] | 138 | } $v3 |
drh | fa6bc00 | 2004-09-07 16:19:52 +0000 | [diff] [blame] | 139 | do_test bind-2.1.5 { |
drh | 6bf8957 | 2004-11-03 16:27:01 +0000 | [diff] [blame] | 140 | sqlite3_bind_parameter_index $VM $v1 |
drh | fa6bc00 | 2004-09-07 16:19:52 +0000 | [diff] [blame] | 141 | } 1 |
| 142 | do_test bind-2.1.6 { |
drh | 6bf8957 | 2004-11-03 16:27:01 +0000 | [diff] [blame] | 143 | sqlite3_bind_parameter_index $VM $v2 |
drh | fa6bc00 | 2004-09-07 16:19:52 +0000 | [diff] [blame] | 144 | } 2 |
| 145 | do_test bind-2.1.7 { |
drh | 6bf8957 | 2004-11-03 16:27:01 +0000 | [diff] [blame] | 146 | sqlite3_bind_parameter_index $VM $v3 |
drh | fa6bc00 | 2004-09-07 16:19:52 +0000 | [diff] [blame] | 147 | } 3 |
| 148 | do_test bind-2.1.8 { |
| 149 | sqlite3_bind_parameter_index $VM {:hi} |
| 150 | } 0 |
danielk1977 | 51e3d8e | 2004-05-20 01:12:34 +0000 | [diff] [blame] | 151 | |
| 152 | # 32 bit Integers |
| 153 | do_test bind-2.2 { |
drh | 241db31 | 2004-06-22 12:46:53 +0000 | [diff] [blame] | 154 | sqlite3_bind_int $VM 1 123 |
| 155 | sqlite3_bind_int $VM 2 456 |
| 156 | sqlite3_bind_int $VM 3 789 |
danielk1977 | 51e3d8e | 2004-05-20 01:12:34 +0000 | [diff] [blame] | 157 | sqlite_step $VM N VALUES COLNAMES |
danielk1977 | 106bb23 | 2004-05-21 10:08:53 +0000 | [diff] [blame] | 158 | sqlite3_reset $VM |
danielk1977 | 51e3d8e | 2004-05-20 01:12:34 +0000 | [diff] [blame] | 159 | execsql {SELECT rowid, * FROM t1} |
| 160 | } {1 123 456 789} |
| 161 | do_test bind-2.3 { |
drh | 241db31 | 2004-06-22 12:46:53 +0000 | [diff] [blame] | 162 | sqlite3_bind_int $VM 2 -2000000000 |
| 163 | sqlite3_bind_int $VM 3 2000000000 |
danielk1977 | 51e3d8e | 2004-05-20 01:12:34 +0000 | [diff] [blame] | 164 | sqlite_step $VM N VALUES COLNAMES |
danielk1977 | 106bb23 | 2004-05-21 10:08:53 +0000 | [diff] [blame] | 165 | sqlite3_reset $VM |
danielk1977 | 51e3d8e | 2004-05-20 01:12:34 +0000 | [diff] [blame] | 166 | execsql {SELECT rowid, * FROM t1} |
| 167 | } {1 123 456 789 2 123 -2000000000 2000000000} |
| 168 | do_test bind-2.4 { |
danielk1977 | 35bb9d0 | 2004-05-24 12:55:54 +0000 | [diff] [blame] | 169 | execsql {SELECT typeof(a), typeof(b), typeof(c) FROM t1} |
| 170 | } {integer integer integer integer integer integer} |
danielk1977 | 51e3d8e | 2004-05-20 01:12:34 +0000 | [diff] [blame] | 171 | do_test bind-2.5 { |
| 172 | execsql { |
| 173 | DELETE FROM t1; |
| 174 | } |
| 175 | } {} |
| 176 | |
| 177 | # 64 bit Integers |
| 178 | do_test bind-3.1 { |
| 179 | sqlite3_bind_int64 $VM 1 32 |
| 180 | sqlite3_bind_int64 $VM 2 -2000000000000 |
| 181 | sqlite3_bind_int64 $VM 3 2000000000000 |
| 182 | sqlite_step $VM N VALUES COLNAMES |
danielk1977 | 106bb23 | 2004-05-21 10:08:53 +0000 | [diff] [blame] | 183 | sqlite3_reset $VM |
danielk1977 | 51e3d8e | 2004-05-20 01:12:34 +0000 | [diff] [blame] | 184 | execsql {SELECT rowid, * FROM t1} |
| 185 | } {1 32 -2000000000000 2000000000000} |
| 186 | do_test bind-3.2 { |
danielk1977 | 35bb9d0 | 2004-05-24 12:55:54 +0000 | [diff] [blame] | 187 | execsql {SELECT typeof(a), typeof(b), typeof(c) FROM t1} |
| 188 | } {integer integer integer} |
danielk1977 | 51e3d8e | 2004-05-20 01:12:34 +0000 | [diff] [blame] | 189 | do_test bind-3.3 { |
| 190 | execsql { |
| 191 | DELETE FROM t1; |
| 192 | } |
| 193 | } {} |
| 194 | |
| 195 | # Doubles |
| 196 | do_test bind-4.1 { |
| 197 | sqlite3_bind_double $VM 1 1234.1234 |
| 198 | sqlite3_bind_double $VM 2 0.00001 |
| 199 | sqlite3_bind_double $VM 3 123456789 |
| 200 | sqlite_step $VM N VALUES COLNAMES |
danielk1977 | 106bb23 | 2004-05-21 10:08:53 +0000 | [diff] [blame] | 201 | sqlite3_reset $VM |
drh | ab34b8f | 2005-03-20 23:18:57 +0000 | [diff] [blame] | 202 | set x [execsql {SELECT rowid, * FROM t1}] |
| 203 | regsub {1e-005} $x {1e-05} y |
| 204 | set y |
drh | 92febd9 | 2004-08-20 18:34:20 +0000 | [diff] [blame] | 205 | } {1 1234.1234 1e-05 123456789.0} |
danielk1977 | 51e3d8e | 2004-05-20 01:12:34 +0000 | [diff] [blame] | 206 | do_test bind-4.2 { |
danielk1977 | 35bb9d0 | 2004-05-24 12:55:54 +0000 | [diff] [blame] | 207 | execsql {SELECT typeof(a), typeof(b), typeof(c) FROM t1} |
| 208 | } {real real real} |
danielk1977 | 51e3d8e | 2004-05-20 01:12:34 +0000 | [diff] [blame] | 209 | do_test bind-4.3 { |
| 210 | execsql { |
| 211 | DELETE FROM t1; |
| 212 | } |
| 213 | } {} |
drh | 53c1402 | 2007-05-10 17:23:11 +0000 | [diff] [blame] | 214 | do_test bind-4.4 { |
| 215 | sqlite3_bind_double $VM 1 NaN |
| 216 | sqlite3_bind_double $VM 2 1e300 |
| 217 | sqlite3_bind_double $VM 3 -1e-300 |
| 218 | sqlite_step $VM N VALUES COLNAMES |
| 219 | sqlite3_reset $VM |
| 220 | set x [execsql {SELECT rowid, * FROM t1}] |
| 221 | regsub {1e-005} $x {1e-05} y |
| 222 | set y |
| 223 | } {1 {} 1e+300 -1e-300} |
| 224 | do_test bind-4.5 { |
| 225 | execsql {SELECT typeof(a), typeof(b), typeof(c) FROM t1} |
| 226 | } {null real real} |
| 227 | do_test bind-4.6 { |
| 228 | execsql { |
| 229 | DELETE FROM t1; |
| 230 | } |
| 231 | } {} |
danielk1977 | 51e3d8e | 2004-05-20 01:12:34 +0000 | [diff] [blame] | 232 | |
| 233 | # NULL |
| 234 | do_test bind-5.1 { |
| 235 | sqlite3_bind_null $VM 1 |
| 236 | sqlite3_bind_null $VM 2 |
| 237 | sqlite3_bind_null $VM 3 |
| 238 | sqlite_step $VM N VALUES COLNAMES |
danielk1977 | 106bb23 | 2004-05-21 10:08:53 +0000 | [diff] [blame] | 239 | sqlite3_reset $VM |
danielk1977 | 51e3d8e | 2004-05-20 01:12:34 +0000 | [diff] [blame] | 240 | execsql {SELECT rowid, * FROM t1} |
| 241 | } {1 {} {} {}} |
| 242 | do_test bind-5.2 { |
danielk1977 | 35bb9d0 | 2004-05-24 12:55:54 +0000 | [diff] [blame] | 243 | execsql {SELECT typeof(a), typeof(b), typeof(c) FROM t1} |
| 244 | } {null null null} |
danielk1977 | 51e3d8e | 2004-05-20 01:12:34 +0000 | [diff] [blame] | 245 | do_test bind-5.3 { |
| 246 | execsql { |
| 247 | DELETE FROM t1; |
| 248 | } |
| 249 | } {} |
| 250 | |
| 251 | # UTF-8 text |
| 252 | do_test bind-6.1 { |
| 253 | sqlite3_bind_text $VM 1 hellothere 5 |
danielk1977 | c572ef7 | 2004-05-27 09:28:41 +0000 | [diff] [blame] | 254 | sqlite3_bind_text $VM 2 ".." 1 |
drh | 10dfbbb | 2008-04-16 12:58:53 +0000 | [diff] [blame] | 255 | sqlite3_bind_text $VM 3 world\000 -1 |
danielk1977 | 51e3d8e | 2004-05-20 01:12:34 +0000 | [diff] [blame] | 256 | sqlite_step $VM N VALUES COLNAMES |
danielk1977 | 106bb23 | 2004-05-21 10:08:53 +0000 | [diff] [blame] | 257 | sqlite3_reset $VM |
danielk1977 | 51e3d8e | 2004-05-20 01:12:34 +0000 | [diff] [blame] | 258 | execsql {SELECT rowid, * FROM t1} |
| 259 | } {1 hello . world} |
| 260 | do_test bind-6.2 { |
danielk1977 | 35bb9d0 | 2004-05-24 12:55:54 +0000 | [diff] [blame] | 261 | execsql {SELECT typeof(a), typeof(b), typeof(c) FROM t1} |
| 262 | } {text text text} |
danielk1977 | 51e3d8e | 2004-05-20 01:12:34 +0000 | [diff] [blame] | 263 | do_test bind-6.3 { |
| 264 | execsql { |
| 265 | DELETE FROM t1; |
| 266 | } |
| 267 | } {} |
| 268 | |
drh | 10dfbbb | 2008-04-16 12:58:53 +0000 | [diff] [blame] | 269 | # Make sure zeros in a string work. |
| 270 | # |
| 271 | do_test bind-6.4 { |
| 272 | db eval {DELETE FROM t1} |
| 273 | sqlite3_bind_text $VM 1 hello\000there\000 12 |
| 274 | sqlite3_bind_text $VM 2 hello\000there\000 11 |
| 275 | sqlite3_bind_text $VM 3 hello\000there\000 -1 |
| 276 | sqlite_step $VM N VALUES COLNAMES |
| 277 | sqlite3_reset $VM |
drh | 10dfbbb | 2008-04-16 12:58:53 +0000 | [diff] [blame] | 278 | execsql {SELECT * FROM t1} |
| 279 | } {hello hello hello} |
drh | 78ddb7c | 2008-04-16 16:11:49 +0000 | [diff] [blame^] | 280 | set enc [db eval {PRAGMA encoding}] |
| 281 | if {$enc=="UTF-8"} { |
| 282 | do_test bind-6.5 { |
| 283 | execsql {SELECT hex(a), hex(b), hex(c) FROM t1} |
| 284 | } {68656C6C6F00746865726500 68656C6C6F007468657265 68656C6C6F} |
| 285 | } elseif {$enc=="UTF-16le"} { |
| 286 | do_test bind-6.5 { |
| 287 | execsql {SELECT hex(a), hex(b), hex(c) FROM t1} |
| 288 | } {680065006C006C006F000000740068006500720065000000 680065006C006C006F00000074006800650072006500 680065006C006C006F00} |
| 289 | } elseif {$enc=="UTF-16be"} { |
| 290 | do_test bind-6.5 { |
| 291 | execsql {SELECT hex(a), hex(b), hex(c) FROM t1} |
| 292 | } {00680065006C006C006F0000007400680065007200650000 00680065006C006C006F000000740068006500720065 00680065006C006C006F} |
| 293 | } else { |
| 294 | do_test bind-6.5 { |
| 295 | set "Unknown database encoding: $::enc" |
| 296 | } {} |
| 297 | } |
drh | 10dfbbb | 2008-04-16 12:58:53 +0000 | [diff] [blame] | 298 | do_test bind-6.6 { |
| 299 | execsql {SELECT typeof(a), typeof(b), typeof(c) FROM t1} |
| 300 | } {text text text} |
| 301 | do_test bind-6.7 { |
| 302 | execsql { |
| 303 | DELETE FROM t1; |
| 304 | } |
| 305 | } {} |
| 306 | |
danielk1977 | 51e3d8e | 2004-05-20 01:12:34 +0000 | [diff] [blame] | 307 | # UTF-16 text |
drh | 6c62608 | 2004-11-14 21:56:29 +0000 | [diff] [blame] | 308 | ifcapable {utf16} { |
| 309 | do_test bind-7.1 { |
| 310 | sqlite3_bind_text16 $VM 1 [encoding convertto unicode hellothere] 10 |
| 311 | sqlite3_bind_text16 $VM 2 [encoding convertto unicode ""] 0 |
| 312 | sqlite3_bind_text16 $VM 3 [encoding convertto unicode world] 10 |
| 313 | sqlite_step $VM N VALUES COLNAMES |
| 314 | sqlite3_reset $VM |
| 315 | execsql {SELECT rowid, * FROM t1} |
| 316 | } {1 hello {} world} |
| 317 | do_test bind-7.2 { |
| 318 | execsql {SELECT typeof(a), typeof(b), typeof(c) FROM t1} |
| 319 | } {text text text} |
drh | 10dfbbb | 2008-04-16 12:58:53 +0000 | [diff] [blame] | 320 | do_test bind-7.3 { |
| 321 | db eval {DELETE FROM t1} |
| 322 | sqlite3_bind_text16 $VM 1 [encoding convertto unicode hi\000yall\000] 16 |
| 323 | sqlite3_bind_text16 $VM 2 [encoding convertto unicode hi\000yall\000] 14 |
| 324 | sqlite3_bind_text16 $VM 3 [encoding convertto unicode hi\000yall\000] -1 |
| 325 | sqlite_step $VM N VALUES COLNAMES |
| 326 | sqlite3_reset $VM |
| 327 | execsql {SELECT * FROM t1} |
| 328 | } {hi hi hi} |
drh | 78ddb7c | 2008-04-16 16:11:49 +0000 | [diff] [blame^] | 329 | if {$enc=="UTF-8"} { |
| 330 | do_test bind-7.4 { |
| 331 | execsql {SELECT hex(a), hex(b), hex(c) FROM t1} |
| 332 | } {68690079616C6C00 68690079616C6C 6869} |
| 333 | } elseif {$enc=="UTF-16le"} { |
| 334 | do_test bind-7.4 { |
| 335 | execsql {SELECT hex(a), hex(b), hex(c) FROM t1} |
| 336 | } {680069000000790061006C006C000000 680069000000790061006C006C00 68006900} |
| 337 | } elseif {$enc=="UTF-16be"} { |
| 338 | do_test bind-7.4 { |
| 339 | execsql {SELECT hex(a), hex(b), hex(c) FROM t1} |
| 340 | } {00680069000000790061006C006C0000 00680069000000790061006C006C 00680069} |
| 341 | } |
drh | 10dfbbb | 2008-04-16 12:58:53 +0000 | [diff] [blame] | 342 | do_test bind-7.5 { |
| 343 | execsql {SELECT typeof(a), typeof(b), typeof(c) FROM t1} |
| 344 | } {text text text} |
drh | 6c62608 | 2004-11-14 21:56:29 +0000 | [diff] [blame] | 345 | } |
drh | 10dfbbb | 2008-04-16 12:58:53 +0000 | [diff] [blame] | 346 | do_test bind-7.99 { |
| 347 | execsql {DELETE FROM t1;} |
danielk1977 | 51e3d8e | 2004-05-20 01:12:34 +0000 | [diff] [blame] | 348 | } {} |
| 349 | |
danielk1977 | 6622cce | 2004-05-20 11:00:52 +0000 | [diff] [blame] | 350 | # Test that the 'out of range' error works. |
| 351 | do_test bind-8.1 { |
| 352 | catch { sqlite3_bind_null $VM 0 } |
| 353 | } {1} |
| 354 | do_test bind-8.2 { |
| 355 | sqlite3_errmsg $DB |
drh | b08153d | 2004-11-20 20:18:55 +0000 | [diff] [blame] | 356 | } {bind or column index out of range} |
drh | 6c62608 | 2004-11-14 21:56:29 +0000 | [diff] [blame] | 357 | ifcapable {utf16} { |
| 358 | do_test bind-8.3 { |
| 359 | encoding convertfrom unicode [sqlite3_errmsg16 $DB] |
drh | b08153d | 2004-11-20 20:18:55 +0000 | [diff] [blame] | 360 | } {bind or column index out of range} |
drh | 6c62608 | 2004-11-14 21:56:29 +0000 | [diff] [blame] | 361 | } |
danielk1977 | 6622cce | 2004-05-20 11:00:52 +0000 | [diff] [blame] | 362 | do_test bind-8.4 { |
| 363 | sqlite3_bind_null $VM 1 |
| 364 | sqlite3_errmsg $DB |
| 365 | } {not an error} |
| 366 | do_test bind-8.5 { |
| 367 | catch { sqlite3_bind_null $VM 4 } |
| 368 | } {1} |
| 369 | do_test bind-8.6 { |
| 370 | sqlite3_errmsg $DB |
drh | b08153d | 2004-11-20 20:18:55 +0000 | [diff] [blame] | 371 | } {bind or column index out of range} |
drh | 6c62608 | 2004-11-14 21:56:29 +0000 | [diff] [blame] | 372 | ifcapable {utf16} { |
| 373 | do_test bind-8.7 { |
| 374 | encoding convertfrom unicode [sqlite3_errmsg16 $DB] |
drh | b08153d | 2004-11-20 20:18:55 +0000 | [diff] [blame] | 375 | } {bind or column index out of range} |
drh | 6c62608 | 2004-11-14 21:56:29 +0000 | [diff] [blame] | 376 | } |
danielk1977 | 6622cce | 2004-05-20 11:00:52 +0000 | [diff] [blame] | 377 | |
danielk1977 | f461889 | 2004-06-28 13:09:11 +0000 | [diff] [blame] | 378 | do_test bind-8.8 { |
| 379 | catch { sqlite3_bind_blob $VM 0 "abc" 3 } |
| 380 | } {1} |
| 381 | do_test bind-8.9 { |
| 382 | catch { sqlite3_bind_blob $VM 4 "abc" 3 } |
| 383 | } {1} |
| 384 | do_test bind-8.10 { |
| 385 | catch { sqlite3_bind_text $VM 0 "abc" 3 } |
| 386 | } {1} |
drh | 6c62608 | 2004-11-14 21:56:29 +0000 | [diff] [blame] | 387 | ifcapable {utf16} { |
| 388 | do_test bind-8.11 { |
| 389 | catch { sqlite3_bind_text16 $VM 4 "abc" 2 } |
| 390 | } {1} |
| 391 | } |
danielk1977 | f461889 | 2004-06-28 13:09:11 +0000 | [diff] [blame] | 392 | do_test bind-8.12 { |
| 393 | catch { sqlite3_bind_int $VM 0 5 } |
| 394 | } {1} |
| 395 | do_test bind-8.13 { |
| 396 | catch { sqlite3_bind_int $VM 4 5 } |
| 397 | } {1} |
| 398 | do_test bind-8.14 { |
| 399 | catch { sqlite3_bind_double $VM 0 5.0 } |
| 400 | } {1} |
| 401 | do_test bind-8.15 { |
| 402 | catch { sqlite3_bind_double $VM 4 6.0 } |
| 403 | } {1} |
danielk1977 | 6622cce | 2004-05-20 11:00:52 +0000 | [diff] [blame] | 404 | |
drh | fa6bc00 | 2004-09-07 16:19:52 +0000 | [diff] [blame] | 405 | do_test bind-8.99 { |
danielk1977 | 106bb23 | 2004-05-21 10:08:53 +0000 | [diff] [blame] | 406 | sqlite3_finalize $VM |
danielk1977 | 3cf8606 | 2004-05-26 10:11:05 +0000 | [diff] [blame] | 407 | } SQLITE_OK |
danielk1977 | 51e3d8e | 2004-05-20 01:12:34 +0000 | [diff] [blame] | 408 | |
drh | fa6bc00 | 2004-09-07 16:19:52 +0000 | [diff] [blame] | 409 | do_test bind-9.1 { |
| 410 | execsql { |
| 411 | CREATE TABLE t2(a,b,c,d,e,f); |
| 412 | } |
| 413 | set rc [catch { |
| 414 | sqlite3_prepare $DB { |
| 415 | INSERT INTO t2(a) VALUES(?0) |
| 416 | } -1 TAIL |
| 417 | } msg] |
| 418 | lappend rc $msg |
| 419 | } {1 {(1) variable number must be between ?1 and ?999}} |
| 420 | do_test bind-9.2 { |
| 421 | set rc [catch { |
| 422 | sqlite3_prepare $DB { |
| 423 | INSERT INTO t2(a) VALUES(?1000) |
| 424 | } -1 TAIL |
| 425 | } msg] |
| 426 | lappend rc $msg |
| 427 | } {1 {(1) variable number must be between ?1 and ?999}} |
| 428 | do_test bind-9.3 { |
| 429 | set VM [ |
| 430 | sqlite3_prepare $DB { |
| 431 | INSERT INTO t2(a,b) VALUES(?1,?999) |
| 432 | } -1 TAIL |
| 433 | ] |
| 434 | sqlite3_bind_parameter_count $VM |
| 435 | } {999} |
| 436 | catch {sqlite3_finalize $VM} |
| 437 | do_test bind-9.4 { |
| 438 | set VM [ |
| 439 | sqlite3_prepare $DB { |
danielk1977 | 832b266 | 2007-05-09 11:37:22 +0000 | [diff] [blame] | 440 | INSERT INTO t2(a,b,c,d) VALUES(?1,?997,?,?) |
drh | fa6bc00 | 2004-09-07 16:19:52 +0000 | [diff] [blame] | 441 | } -1 TAIL |
| 442 | ] |
| 443 | sqlite3_bind_parameter_count $VM |
danielk1977 | 832b266 | 2007-05-09 11:37:22 +0000 | [diff] [blame] | 444 | } {999} |
drh | fa6bc00 | 2004-09-07 16:19:52 +0000 | [diff] [blame] | 445 | do_test bind-9.5 { |
| 446 | sqlite3_bind_int $VM 1 1 |
danielk1977 | 832b266 | 2007-05-09 11:37:22 +0000 | [diff] [blame] | 447 | sqlite3_bind_int $VM 997 999 |
| 448 | sqlite3_bind_int $VM 998 1000 |
| 449 | sqlite3_bind_int $VM 999 1001 |
drh | fa6bc00 | 2004-09-07 16:19:52 +0000 | [diff] [blame] | 450 | sqlite3_step $VM |
| 451 | } SQLITE_DONE |
| 452 | do_test bind-9.6 { |
| 453 | sqlite3_finalize $VM |
| 454 | } SQLITE_OK |
| 455 | do_test bind-9.7 { |
| 456 | execsql {SELECT * FROM t2} |
| 457 | } {1 999 1000 1001 {} {}} |
danielk1977 | 51e3d8e | 2004-05-20 01:12:34 +0000 | [diff] [blame] | 458 | |
drh | 6bf8957 | 2004-11-03 16:27:01 +0000 | [diff] [blame] | 459 | ifcapable {tclvar} { |
| 460 | do_test bind-10.1 { |
drh | 6bf8957 | 2004-11-03 16:27:01 +0000 | [diff] [blame] | 461 | set VM [ |
| 462 | sqlite3_prepare $DB { |
| 463 | INSERT INTO t2(a,b,c,d,e,f) VALUES(:abc,$abc,:abc,$ab,$abc,:abc) |
| 464 | } -1 TAIL |
| 465 | ] |
| 466 | sqlite3_bind_parameter_count $VM |
| 467 | } 3 |
| 468 | set v1 {$abc} |
| 469 | set v2 {$ab} |
| 470 | } |
| 471 | ifcapable {!tclvar} { |
| 472 | do_test bind-10.1 { |
drh | 6bf8957 | 2004-11-03 16:27:01 +0000 | [diff] [blame] | 473 | set VM [ |
| 474 | sqlite3_prepare $DB { |
| 475 | INSERT INTO t2(a,b,c,d,e,f) VALUES(:abc,:xyz,:abc,:xy,:xyz,:abc) |
| 476 | } -1 TAIL |
| 477 | ] |
| 478 | sqlite3_bind_parameter_count $VM |
| 479 | } 3 |
| 480 | set v1 {:xyz} |
| 481 | set v2 {:xy} |
| 482 | } |
drh | fa6bc00 | 2004-09-07 16:19:52 +0000 | [diff] [blame] | 483 | do_test bind-10.2 { |
| 484 | sqlite3_bind_parameter_index $VM :abc |
| 485 | } 1 |
| 486 | do_test bind-10.3 { |
drh | 6bf8957 | 2004-11-03 16:27:01 +0000 | [diff] [blame] | 487 | sqlite3_bind_parameter_index $VM $v1 |
drh | fa6bc00 | 2004-09-07 16:19:52 +0000 | [diff] [blame] | 488 | } 2 |
| 489 | do_test bind-10.4 { |
drh | 6bf8957 | 2004-11-03 16:27:01 +0000 | [diff] [blame] | 490 | sqlite3_bind_parameter_index $VM $v2 |
drh | fa6bc00 | 2004-09-07 16:19:52 +0000 | [diff] [blame] | 491 | } 3 |
| 492 | do_test bind-10.5 { |
| 493 | sqlite3_bind_parameter_name $VM 1 |
| 494 | } :abc |
| 495 | do_test bind-10.6 { |
| 496 | sqlite3_bind_parameter_name $VM 2 |
drh | 6bf8957 | 2004-11-03 16:27:01 +0000 | [diff] [blame] | 497 | } $v1 |
drh | fa6bc00 | 2004-09-07 16:19:52 +0000 | [diff] [blame] | 498 | do_test bind-10.7 { |
| 499 | sqlite3_bind_parameter_name $VM 3 |
drh | 6bf8957 | 2004-11-03 16:27:01 +0000 | [diff] [blame] | 500 | } $v2 |
drh | c5cdca6 | 2005-01-11 16:54:14 +0000 | [diff] [blame] | 501 | do_test bind-10.7.1 { |
| 502 | sqlite3_bind_parameter_name 0 1 ;# Ignore if VM is NULL |
| 503 | } {} |
| 504 | do_test bind-10.7.2 { |
| 505 | sqlite3_bind_parameter_name $VM 0 ;# Ignore if index too small |
| 506 | } {} |
| 507 | do_test bind-10.7.3 { |
| 508 | sqlite3_bind_parameter_name $VM 4 ;# Ignore if index is too big |
| 509 | } {} |
drh | fa6bc00 | 2004-09-07 16:19:52 +0000 | [diff] [blame] | 510 | do_test bind-10.8 { |
| 511 | sqlite3_bind_int $VM 1 1 |
| 512 | sqlite3_bind_int $VM 2 2 |
| 513 | sqlite3_bind_int $VM 3 3 |
| 514 | sqlite3_step $VM |
| 515 | } SQLITE_DONE |
drh | c5cdca6 | 2005-01-11 16:54:14 +0000 | [diff] [blame] | 516 | do_test bind-10.8.1 { |
| 517 | # Binding attempts after program start should fail |
| 518 | set rc [catch { |
| 519 | sqlite3_bind_int $VM 1 1 |
| 520 | } msg] |
| 521 | lappend rc $msg |
| 522 | } {1 {}} |
drh | fa6bc00 | 2004-09-07 16:19:52 +0000 | [diff] [blame] | 523 | do_test bind-10.9 { |
| 524 | sqlite3_finalize $VM |
| 525 | } SQLITE_OK |
| 526 | do_test bind-10.10 { |
| 527 | execsql {SELECT * FROM t2} |
| 528 | } {1 999 1000 1001 {} {} 1 2 1 3 2 1} |
drh | 82a4851 | 2003-09-06 22:45:20 +0000 | [diff] [blame] | 529 | |
drh | 971a7c8 | 2004-09-24 12:48:12 +0000 | [diff] [blame] | 530 | # Ticket #918 |
| 531 | # |
| 532 | do_test bind-10.11 { |
drh | a86a5b6 | 2006-01-23 18:42:21 +0000 | [diff] [blame] | 533 | # catch {sqlite3_finalize $VM} |
drh | 971a7c8 | 2004-09-24 12:48:12 +0000 | [diff] [blame] | 534 | set VM [ |
| 535 | sqlite3_prepare $DB { |
| 536 | INSERT INTO t2(a,b,c,d,e,f) VALUES(:abc,?,?4,:pqr,:abc,?4) |
| 537 | } -1 TAIL |
| 538 | ] |
| 539 | sqlite3_bind_parameter_count $VM |
| 540 | } 5 |
drh | c5cdca6 | 2005-01-11 16:54:14 +0000 | [diff] [blame] | 541 | do_test bind-10.11.1 { |
| 542 | sqlite3_bind_parameter_index 0 :xyz ;# ignore NULL VM arguments |
| 543 | } 0 |
drh | 971a7c8 | 2004-09-24 12:48:12 +0000 | [diff] [blame] | 544 | do_test bind-10.12 { |
| 545 | sqlite3_bind_parameter_index $VM :xyz |
| 546 | } 0 |
| 547 | do_test bind-10.13 { |
| 548 | sqlite3_bind_parameter_index $VM {} |
| 549 | } 0 |
| 550 | do_test bind-10.14 { |
| 551 | sqlite3_bind_parameter_index $VM :pqr |
| 552 | } 5 |
| 553 | do_test bind-10.15 { |
| 554 | sqlite3_bind_parameter_index $VM ?4 |
| 555 | } 4 |
| 556 | do_test bind-10.16 { |
| 557 | sqlite3_bind_parameter_name $VM 1 |
| 558 | } :abc |
danielk1977 | b3bce66 | 2005-01-29 08:32:43 +0000 | [diff] [blame] | 559 | do_test bind-10.17 { |
drh | 971a7c8 | 2004-09-24 12:48:12 +0000 | [diff] [blame] | 560 | sqlite3_bind_parameter_name $VM 2 |
| 561 | } {} |
danielk1977 | b3bce66 | 2005-01-29 08:32:43 +0000 | [diff] [blame] | 562 | do_test bind-10.18 { |
drh | 971a7c8 | 2004-09-24 12:48:12 +0000 | [diff] [blame] | 563 | sqlite3_bind_parameter_name $VM 3 |
| 564 | } {} |
danielk1977 | b3bce66 | 2005-01-29 08:32:43 +0000 | [diff] [blame] | 565 | do_test bind-10.19 { |
drh | 971a7c8 | 2004-09-24 12:48:12 +0000 | [diff] [blame] | 566 | sqlite3_bind_parameter_name $VM 4 |
| 567 | } {?4} |
danielk1977 | b3bce66 | 2005-01-29 08:32:43 +0000 | [diff] [blame] | 568 | do_test bind-10.20 { |
drh | 971a7c8 | 2004-09-24 12:48:12 +0000 | [diff] [blame] | 569 | sqlite3_bind_parameter_name $VM 5 |
| 570 | } :pqr |
| 571 | catch {sqlite3_finalize $VM} |
| 572 | |
drh | 48e5aa2 | 2005-01-11 17:46:41 +0000 | [diff] [blame] | 573 | # Make sure we catch an unterminated "(" in a Tcl-style variable name |
| 574 | # |
danielk1977 | 4489f9b | 2005-01-20 02:17:01 +0000 | [diff] [blame] | 575 | ifcapable tclvar { |
| 576 | do_test bind-11.1 { |
| 577 | catchsql {SELECT * FROM sqlite_master WHERE name=$abc(123 and sql NOT NULL;} |
| 578 | } {1 {unrecognized token: "$abc(123"}} |
| 579 | } |
drh | 48e5aa2 | 2005-01-11 17:46:41 +0000 | [diff] [blame] | 580 | |
drh | e57c06f | 2005-12-09 20:54:34 +0000 | [diff] [blame] | 581 | if {[execsql {pragma encoding}]=="UTF-8"} { |
| 582 | # Test the ability to bind text that contains embedded '\000' characters. |
drh | f9cb7f5 | 2006-06-27 20:06:44 +0000 | [diff] [blame] | 583 | # Make sure we can recover the entire input string. |
drh | e57c06f | 2005-12-09 20:54:34 +0000 | [diff] [blame] | 584 | # |
| 585 | do_test bind-12.1 { |
| 586 | execsql { |
| 587 | CREATE TABLE t3(x BLOB); |
| 588 | } |
| 589 | set VM [sqlite3_prepare $DB {INSERT INTO t3 VALUES(?)} -1 TAIL] |
| 590 | sqlite_bind $VM 1 not-used blob10 |
| 591 | sqlite3_step $VM |
| 592 | sqlite3_finalize $VM |
| 593 | execsql { |
| 594 | SELECT typeof(x), length(x), quote(x), |
| 595 | length(cast(x AS BLOB)), quote(cast(x AS BLOB)) FROM t3 |
| 596 | } |
| 597 | } {text 3 'abc' 10 X'6162630078797A007071'} |
| 598 | do_test bind-12.2 { |
| 599 | sqlite3_create_function $DB |
| 600 | execsql { |
| 601 | SELECT quote(cast(x_coalesce(x) AS blob)) FROM t3 |
| 602 | } |
| 603 | } {X'6162630078797A007071'} |
| 604 | } |
drh | bf8aa2a | 2005-12-02 02:44:05 +0000 | [diff] [blame] | 605 | |
drh | f9cb7f5 | 2006-06-27 20:06:44 +0000 | [diff] [blame] | 606 | # Test the operation of sqlite3_clear_bindings |
| 607 | # |
| 608 | do_test bind-13.1 { |
| 609 | set VM [sqlite3_prepare $DB {SELECT ?,?,?} -1 TAIL] |
| 610 | sqlite3_step $VM |
| 611 | list [sqlite3_column_type $VM 0] [sqlite3_column_type $VM 1] \ |
| 612 | [sqlite3_column_type $VM 2] |
| 613 | } {NULL NULL NULL} |
| 614 | do_test bind-13.2 { |
| 615 | sqlite3_reset $VM |
| 616 | sqlite3_bind_int $VM 1 1 |
| 617 | sqlite3_bind_int $VM 2 2 |
| 618 | sqlite3_bind_int $VM 3 3 |
| 619 | sqlite3_step $VM |
| 620 | list [sqlite3_column_type $VM 0] [sqlite3_column_type $VM 1] \ |
| 621 | [sqlite3_column_type $VM 2] |
| 622 | } {INTEGER INTEGER INTEGER} |
| 623 | do_test bind-13.3 { |
| 624 | sqlite3_reset $VM |
| 625 | sqlite3_step $VM |
| 626 | list [sqlite3_column_type $VM 0] [sqlite3_column_type $VM 1] \ |
| 627 | [sqlite3_column_type $VM 2] |
| 628 | } {INTEGER INTEGER INTEGER} |
| 629 | do_test bind-13.4 { |
| 630 | sqlite3_reset $VM |
| 631 | sqlite3_clear_bindings $VM |
| 632 | sqlite3_step $VM |
| 633 | list [sqlite3_column_type $VM 0] [sqlite3_column_type $VM 1] \ |
| 634 | [sqlite3_column_type $VM 2] |
| 635 | } {NULL NULL NULL} |
| 636 | sqlite3_finalize $VM |
| 637 | |
drh | 82a4851 | 2003-09-06 22:45:20 +0000 | [diff] [blame] | 638 | finish_test |