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 | 75f6a03 | 2004-07-15 14:15:00 +0000 | [diff] [blame^] | 14 | # $Id: bind.test,v 1.15 2004/07/15 14:15:02 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 | 82a4851 | 2003-09-06 22:45:20 +0000 | [diff] [blame] | 40 | execsql {CREATE TABLE t1(a,b,c)} |
danielk1977 | 4ad1713 | 2004-05-21 01:47:26 +0000 | [diff] [blame] | 41 | set VM [sqlite3_prepare $DB {INSERT INTO t1 VALUES(?,?,?)} -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 | 82a4851 | 2003-09-06 22:45:20 +0000 | [diff] [blame] | 47 | do_test bind-1.2 { |
| 48 | sqlite_step $VM N VALUES COLNAMES |
| 49 | } {SQLITE_DONE} |
| 50 | do_test bind-1.3 { |
| 51 | execsql {SELECT rowid, * FROM t1} |
| 52 | } {1 {} {} {}} |
| 53 | do_test bind-1.4 { |
danielk1977 | 106bb23 | 2004-05-21 10:08:53 +0000 | [diff] [blame] | 54 | sqlite3_reset $VM |
drh | 82a4851 | 2003-09-06 22:45:20 +0000 | [diff] [blame] | 55 | sqlite_bind $VM 1 {test value 1} normal |
| 56 | sqlite_step $VM N VALUES COLNAMES |
| 57 | } SQLITE_DONE |
| 58 | do_test bind-1.5 { |
| 59 | execsql {SELECT rowid, * FROM t1} |
| 60 | } {1 {} {} {} 2 {test value 1} {} {}} |
| 61 | do_test bind-1.6 { |
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 3 {'test value 2'} normal |
| 64 | sqlite_step $VM N VALUES COLNAMES |
| 65 | } SQLITE_DONE |
| 66 | do_test bind-1.7 { |
| 67 | execsql {SELECT rowid, * FROM t1} |
| 68 | } {1 {} {} {} 2 {test value 1} {} {} 3 {test value 1} {} {'test value 2'}} |
| 69 | do_test bind-1.8 { |
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 | 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 {}} |
| 79 | do_test bind-1.9 { |
danielk1977 | 106bb23 | 2004-05-21 10:08:53 +0000 | [diff] [blame] | 80 | sqlite3_reset $VM |
drh | 82a4851 | 2003-09-06 22:45:20 +0000 | [diff] [blame] | 81 | 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 | |
drh | 82a4851 | 2003-09-06 22:45:20 +0000 | [diff] [blame] | 86 | do_test bind-1.99 { |
danielk1977 | 106bb23 | 2004-05-21 10:08:53 +0000 | [diff] [blame] | 87 | sqlite3_finalize $VM |
danielk1977 | 3cf8606 | 2004-05-26 10:11:05 +0000 | [diff] [blame] | 88 | } SQLITE_OK |
drh | 82a4851 | 2003-09-06 22:45:20 +0000 | [diff] [blame] | 89 | |
danielk1977 | 51e3d8e | 2004-05-20 01:12:34 +0000 | [diff] [blame] | 90 | do_test bind-2.1 { |
| 91 | execsql { |
| 92 | DELETE FROM t1; |
| 93 | } |
danielk1977 | 2f2322f | 2004-05-21 02:11:40 +0000 | [diff] [blame] | 94 | set VM [sqlite3_prepare $DB {INSERT INTO t1 VALUES(?,?,?)} -1 TAIL] |
danielk1977 | 51e3d8e | 2004-05-20 01:12:34 +0000 | [diff] [blame] | 95 | set TAIL |
| 96 | } {} |
| 97 | |
| 98 | # 32 bit Integers |
| 99 | do_test bind-2.2 { |
drh | 241db31 | 2004-06-22 12:46:53 +0000 | [diff] [blame] | 100 | sqlite3_bind_int $VM 1 123 |
| 101 | sqlite3_bind_int $VM 2 456 |
| 102 | sqlite3_bind_int $VM 3 789 |
danielk1977 | 51e3d8e | 2004-05-20 01:12:34 +0000 | [diff] [blame] | 103 | sqlite_step $VM N VALUES COLNAMES |
danielk1977 | 106bb23 | 2004-05-21 10:08:53 +0000 | [diff] [blame] | 104 | sqlite3_reset $VM |
danielk1977 | 51e3d8e | 2004-05-20 01:12:34 +0000 | [diff] [blame] | 105 | execsql {SELECT rowid, * FROM t1} |
| 106 | } {1 123 456 789} |
| 107 | do_test bind-2.3 { |
drh | 241db31 | 2004-06-22 12:46:53 +0000 | [diff] [blame] | 108 | sqlite3_bind_int $VM 2 -2000000000 |
| 109 | sqlite3_bind_int $VM 3 2000000000 |
danielk1977 | 51e3d8e | 2004-05-20 01:12:34 +0000 | [diff] [blame] | 110 | sqlite_step $VM N VALUES COLNAMES |
danielk1977 | 106bb23 | 2004-05-21 10:08:53 +0000 | [diff] [blame] | 111 | sqlite3_reset $VM |
danielk1977 | 51e3d8e | 2004-05-20 01:12:34 +0000 | [diff] [blame] | 112 | execsql {SELECT rowid, * FROM t1} |
| 113 | } {1 123 456 789 2 123 -2000000000 2000000000} |
| 114 | do_test bind-2.4 { |
danielk1977 | 35bb9d0 | 2004-05-24 12:55:54 +0000 | [diff] [blame] | 115 | execsql {SELECT typeof(a), typeof(b), typeof(c) FROM t1} |
| 116 | } {integer integer integer integer integer integer} |
danielk1977 | 51e3d8e | 2004-05-20 01:12:34 +0000 | [diff] [blame] | 117 | do_test bind-2.5 { |
| 118 | execsql { |
| 119 | DELETE FROM t1; |
| 120 | } |
| 121 | } {} |
| 122 | |
| 123 | # 64 bit Integers |
| 124 | do_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 |
danielk1977 | 106bb23 | 2004-05-21 10:08:53 +0000 | [diff] [blame] | 129 | sqlite3_reset $VM |
danielk1977 | 51e3d8e | 2004-05-20 01:12:34 +0000 | [diff] [blame] | 130 | execsql {SELECT rowid, * FROM t1} |
| 131 | } {1 32 -2000000000000 2000000000000} |
| 132 | do_test bind-3.2 { |
danielk1977 | 35bb9d0 | 2004-05-24 12:55:54 +0000 | [diff] [blame] | 133 | execsql {SELECT typeof(a), typeof(b), typeof(c) FROM t1} |
| 134 | } {integer integer integer} |
danielk1977 | 51e3d8e | 2004-05-20 01:12:34 +0000 | [diff] [blame] | 135 | do_test bind-3.3 { |
| 136 | execsql { |
| 137 | DELETE FROM t1; |
| 138 | } |
| 139 | } {} |
| 140 | |
| 141 | # Doubles |
| 142 | do_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 |
danielk1977 | 106bb23 | 2004-05-21 10:08:53 +0000 | [diff] [blame] | 147 | sqlite3_reset $VM |
danielk1977 | 51e3d8e | 2004-05-20 01:12:34 +0000 | [diff] [blame] | 148 | execsql {SELECT rowid, * FROM t1} |
| 149 | } {1 1234.1234 1e-05 123456789} |
| 150 | do_test bind-4.2 { |
danielk1977 | 35bb9d0 | 2004-05-24 12:55:54 +0000 | [diff] [blame] | 151 | execsql {SELECT typeof(a), typeof(b), typeof(c) FROM t1} |
| 152 | } {real real real} |
danielk1977 | 51e3d8e | 2004-05-20 01:12:34 +0000 | [diff] [blame] | 153 | do_test bind-4.3 { |
| 154 | execsql { |
| 155 | DELETE FROM t1; |
| 156 | } |
| 157 | } {} |
| 158 | |
| 159 | # NULL |
| 160 | do_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 |
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 {} {} {}} |
| 168 | do_test bind-5.2 { |
danielk1977 | 35bb9d0 | 2004-05-24 12:55:54 +0000 | [diff] [blame] | 169 | execsql {SELECT typeof(a), typeof(b), typeof(c) FROM t1} |
| 170 | } {null null null} |
danielk1977 | 51e3d8e | 2004-05-20 01:12:34 +0000 | [diff] [blame] | 171 | do_test bind-5.3 { |
| 172 | execsql { |
| 173 | DELETE FROM t1; |
| 174 | } |
| 175 | } {} |
| 176 | |
| 177 | # UTF-8 text |
| 178 | do_test bind-6.1 { |
| 179 | sqlite3_bind_text $VM 1 hellothere 5 |
danielk1977 | c572ef7 | 2004-05-27 09:28:41 +0000 | [diff] [blame] | 180 | sqlite3_bind_text $VM 2 ".." 1 |
danielk1977 | 51e3d8e | 2004-05-20 01:12:34 +0000 | [diff] [blame] | 181 | sqlite3_bind_text $VM 3 world -1 |
| 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 hello . world} |
| 186 | do_test bind-6.2 { |
danielk1977 | 35bb9d0 | 2004-05-24 12:55:54 +0000 | [diff] [blame] | 187 | execsql {SELECT typeof(a), typeof(b), typeof(c) FROM t1} |
| 188 | } {text text text} |
danielk1977 | 51e3d8e | 2004-05-20 01:12:34 +0000 | [diff] [blame] | 189 | do_test bind-6.3 { |
| 190 | execsql { |
| 191 | DELETE FROM t1; |
| 192 | } |
| 193 | } {} |
| 194 | |
| 195 | # UTF-16 text |
| 196 | do_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 |
danielk1977 | 106bb23 | 2004-05-21 10:08:53 +0000 | [diff] [blame] | 201 | sqlite3_reset $VM |
danielk1977 | 51e3d8e | 2004-05-20 01:12:34 +0000 | [diff] [blame] | 202 | execsql {SELECT rowid, * FROM t1} |
| 203 | } {1 hello {} world} |
| 204 | do_test bind-7.2 { |
danielk1977 | 35bb9d0 | 2004-05-24 12:55:54 +0000 | [diff] [blame] | 205 | execsql {SELECT typeof(a), typeof(b), typeof(c) FROM t1} |
| 206 | } {text text text} |
danielk1977 | 51e3d8e | 2004-05-20 01:12:34 +0000 | [diff] [blame] | 207 | do_test bind-7.3 { |
| 208 | execsql { |
| 209 | DELETE FROM t1; |
| 210 | } |
| 211 | } {} |
| 212 | |
danielk1977 | 6622cce | 2004-05-20 11:00:52 +0000 | [diff] [blame] | 213 | # Test that the 'out of range' error works. |
| 214 | do_test bind-8.1 { |
| 215 | catch { sqlite3_bind_null $VM 0 } |
| 216 | } {1} |
| 217 | do_test bind-8.2 { |
| 218 | sqlite3_errmsg $DB |
| 219 | } {bind index out of range} |
| 220 | do_test bind-8.3 { |
| 221 | encoding convertfrom unicode [sqlite3_errmsg16 $DB] |
| 222 | } {bind index out of range} |
| 223 | do_test bind-8.4 { |
| 224 | sqlite3_bind_null $VM 1 |
| 225 | sqlite3_errmsg $DB |
| 226 | } {not an error} |
| 227 | do_test bind-8.5 { |
| 228 | catch { sqlite3_bind_null $VM 4 } |
| 229 | } {1} |
| 230 | do_test bind-8.6 { |
| 231 | sqlite3_errmsg $DB |
| 232 | } {bind index out of range} |
| 233 | do_test bind-8.7 { |
| 234 | encoding convertfrom unicode [sqlite3_errmsg16 $DB] |
| 235 | } {bind index out of range} |
| 236 | |
danielk1977 | f461889 | 2004-06-28 13:09:11 +0000 | [diff] [blame] | 237 | do_test bind-8.8 { |
| 238 | catch { sqlite3_bind_blob $VM 0 "abc" 3 } |
| 239 | } {1} |
| 240 | do_test bind-8.9 { |
| 241 | catch { sqlite3_bind_blob $VM 4 "abc" 3 } |
| 242 | } {1} |
| 243 | do_test bind-8.10 { |
| 244 | catch { sqlite3_bind_text $VM 0 "abc" 3 } |
| 245 | } {1} |
| 246 | do_test bind-8.11 { |
| 247 | catch { sqlite3_bind_text16 $VM 4 "abc" 2 } |
| 248 | } {1} |
| 249 | do_test bind-8.12 { |
| 250 | catch { sqlite3_bind_int $VM 0 5 } |
| 251 | } {1} |
| 252 | do_test bind-8.13 { |
| 253 | catch { sqlite3_bind_int $VM 4 5 } |
| 254 | } {1} |
| 255 | do_test bind-8.14 { |
| 256 | catch { sqlite3_bind_double $VM 0 5.0 } |
| 257 | } {1} |
| 258 | do_test bind-8.15 { |
| 259 | catch { sqlite3_bind_double $VM 4 6.0 } |
| 260 | } {1} |
danielk1977 | 6622cce | 2004-05-20 11:00:52 +0000 | [diff] [blame] | 261 | |
| 262 | do_test bind-9.99 { |
danielk1977 | 106bb23 | 2004-05-21 10:08:53 +0000 | [diff] [blame] | 263 | sqlite3_finalize $VM |
danielk1977 | 3cf8606 | 2004-05-26 10:11:05 +0000 | [diff] [blame] | 264 | } SQLITE_OK |
danielk1977 | 51e3d8e | 2004-05-20 01:12:34 +0000 | [diff] [blame] | 265 | |
| 266 | |
drh | 82a4851 | 2003-09-06 22:45:20 +0000 | [diff] [blame] | 267 | |
| 268 | finish_test |