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 | # |
danielk1977 | c572ef7 | 2004-05-27 09:28:41 +0000 | [diff] [blame^] | 14 | # $Id: bind.test,v 1.11 2004/05/27 09:28:44 danielk1977 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 |
| 39 | set DB [sqlite db test.db] |
| 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 | } {} |
| 44 | do_test bind-1.2 { |
| 45 | sqlite_step $VM N VALUES COLNAMES |
| 46 | } {SQLITE_DONE} |
| 47 | do_test bind-1.3 { |
| 48 | execsql {SELECT rowid, * FROM t1} |
| 49 | } {1 {} {} {}} |
| 50 | do_test bind-1.4 { |
danielk1977 | 106bb23 | 2004-05-21 10:08:53 +0000 | [diff] [blame] | 51 | sqlite3_reset $VM |
drh | 82a4851 | 2003-09-06 22:45:20 +0000 | [diff] [blame] | 52 | sqlite_bind $VM 1 {test value 1} normal |
| 53 | sqlite_step $VM N VALUES COLNAMES |
| 54 | } SQLITE_DONE |
| 55 | do_test bind-1.5 { |
| 56 | execsql {SELECT rowid, * FROM t1} |
| 57 | } {1 {} {} {} 2 {test value 1} {} {}} |
| 58 | do_test bind-1.6 { |
danielk1977 | 106bb23 | 2004-05-21 10:08:53 +0000 | [diff] [blame] | 59 | sqlite3_reset $VM |
drh | 82a4851 | 2003-09-06 22:45:20 +0000 | [diff] [blame] | 60 | sqlite_bind $VM 3 {'test value 2'} normal |
| 61 | sqlite_step $VM N VALUES COLNAMES |
| 62 | } SQLITE_DONE |
| 63 | do_test bind-1.7 { |
| 64 | execsql {SELECT rowid, * FROM t1} |
| 65 | } {1 {} {} {} 2 {test value 1} {} {} 3 {test value 1} {} {'test value 2'}} |
| 66 | do_test bind-1.8 { |
danielk1977 | 106bb23 | 2004-05-21 10:08:53 +0000 | [diff] [blame] | 67 | sqlite3_reset $VM |
drh | 82a4851 | 2003-09-06 22:45:20 +0000 | [diff] [blame] | 68 | 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 {}} |
| 76 | do_test bind-1.9 { |
danielk1977 | 106bb23 | 2004-05-21 10:08:53 +0000 | [diff] [blame] | 77 | sqlite3_reset $VM |
drh | 82a4851 | 2003-09-06 22:45:20 +0000 | [diff] [blame] | 78 | 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 | |
drh | 82a4851 | 2003-09-06 22:45:20 +0000 | [diff] [blame] | 83 | do_test bind-1.99 { |
danielk1977 | 106bb23 | 2004-05-21 10:08:53 +0000 | [diff] [blame] | 84 | sqlite3_finalize $VM |
danielk1977 | 3cf8606 | 2004-05-26 10:11:05 +0000 | [diff] [blame] | 85 | } SQLITE_OK |
drh | 82a4851 | 2003-09-06 22:45:20 +0000 | [diff] [blame] | 86 | |
danielk1977 | 51e3d8e | 2004-05-20 01:12:34 +0000 | [diff] [blame] | 87 | do_test bind-2.1 { |
| 88 | execsql { |
| 89 | DELETE FROM t1; |
| 90 | } |
danielk1977 | 2f2322f | 2004-05-21 02:11:40 +0000 | [diff] [blame] | 91 | set VM [sqlite3_prepare $DB {INSERT INTO t1 VALUES(?,?,?)} -1 TAIL] |
danielk1977 | 51e3d8e | 2004-05-20 01:12:34 +0000 | [diff] [blame] | 92 | set TAIL |
| 93 | } {} |
| 94 | |
| 95 | # 32 bit Integers |
| 96 | do_test bind-2.2 { |
| 97 | sqlite3_bind_int32 $VM 1 123 |
| 98 | sqlite3_bind_int32 $VM 2 456 |
| 99 | sqlite3_bind_int32 $VM 3 789 |
| 100 | sqlite_step $VM N VALUES COLNAMES |
danielk1977 | 106bb23 | 2004-05-21 10:08:53 +0000 | [diff] [blame] | 101 | sqlite3_reset $VM |
danielk1977 | 51e3d8e | 2004-05-20 01:12:34 +0000 | [diff] [blame] | 102 | execsql {SELECT rowid, * FROM t1} |
| 103 | } {1 123 456 789} |
| 104 | do_test bind-2.3 { |
| 105 | sqlite3_bind_int32 $VM 2 -2000000000 |
| 106 | sqlite3_bind_int32 $VM 3 2000000000 |
| 107 | sqlite_step $VM N VALUES COLNAMES |
danielk1977 | 106bb23 | 2004-05-21 10:08:53 +0000 | [diff] [blame] | 108 | sqlite3_reset $VM |
danielk1977 | 51e3d8e | 2004-05-20 01:12:34 +0000 | [diff] [blame] | 109 | execsql {SELECT rowid, * FROM t1} |
| 110 | } {1 123 456 789 2 123 -2000000000 2000000000} |
| 111 | do_test bind-2.4 { |
danielk1977 | 35bb9d0 | 2004-05-24 12:55:54 +0000 | [diff] [blame] | 112 | execsql {SELECT typeof(a), typeof(b), typeof(c) FROM t1} |
| 113 | } {integer integer integer integer integer integer} |
danielk1977 | 51e3d8e | 2004-05-20 01:12:34 +0000 | [diff] [blame] | 114 | do_test bind-2.5 { |
| 115 | execsql { |
| 116 | DELETE FROM t1; |
| 117 | } |
| 118 | } {} |
| 119 | |
| 120 | # 64 bit Integers |
| 121 | do_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 |
danielk1977 | 106bb23 | 2004-05-21 10:08:53 +0000 | [diff] [blame] | 126 | sqlite3_reset $VM |
danielk1977 | 51e3d8e | 2004-05-20 01:12:34 +0000 | [diff] [blame] | 127 | execsql {SELECT rowid, * FROM t1} |
| 128 | } {1 32 -2000000000000 2000000000000} |
| 129 | do_test bind-3.2 { |
danielk1977 | 35bb9d0 | 2004-05-24 12:55:54 +0000 | [diff] [blame] | 130 | execsql {SELECT typeof(a), typeof(b), typeof(c) FROM t1} |
| 131 | } {integer integer integer} |
danielk1977 | 51e3d8e | 2004-05-20 01:12:34 +0000 | [diff] [blame] | 132 | do_test bind-3.3 { |
| 133 | execsql { |
| 134 | DELETE FROM t1; |
| 135 | } |
| 136 | } {} |
| 137 | |
| 138 | # Doubles |
| 139 | do_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 |
danielk1977 | 106bb23 | 2004-05-21 10:08:53 +0000 | [diff] [blame] | 144 | sqlite3_reset $VM |
danielk1977 | 51e3d8e | 2004-05-20 01:12:34 +0000 | [diff] [blame] | 145 | execsql {SELECT rowid, * FROM t1} |
| 146 | } {1 1234.1234 1e-05 123456789} |
| 147 | do_test bind-4.2 { |
danielk1977 | 35bb9d0 | 2004-05-24 12:55:54 +0000 | [diff] [blame] | 148 | execsql {SELECT typeof(a), typeof(b), typeof(c) FROM t1} |
| 149 | } {real real real} |
danielk1977 | 51e3d8e | 2004-05-20 01:12:34 +0000 | [diff] [blame] | 150 | do_test bind-4.3 { |
| 151 | execsql { |
| 152 | DELETE FROM t1; |
| 153 | } |
| 154 | } {} |
| 155 | |
| 156 | # NULL |
| 157 | do_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 |
danielk1977 | 106bb23 | 2004-05-21 10:08:53 +0000 | [diff] [blame] | 162 | sqlite3_reset $VM |
danielk1977 | 51e3d8e | 2004-05-20 01:12:34 +0000 | [diff] [blame] | 163 | execsql {SELECT rowid, * FROM t1} |
| 164 | } {1 {} {} {}} |
| 165 | do_test bind-5.2 { |
danielk1977 | 35bb9d0 | 2004-05-24 12:55:54 +0000 | [diff] [blame] | 166 | execsql {SELECT typeof(a), typeof(b), typeof(c) FROM t1} |
| 167 | } {null null null} |
danielk1977 | 51e3d8e | 2004-05-20 01:12:34 +0000 | [diff] [blame] | 168 | do_test bind-5.3 { |
| 169 | execsql { |
| 170 | DELETE FROM t1; |
| 171 | } |
| 172 | } {} |
| 173 | |
| 174 | # UTF-8 text |
| 175 | do_test bind-6.1 { |
| 176 | sqlite3_bind_text $VM 1 hellothere 5 |
danielk1977 | c572ef7 | 2004-05-27 09:28:41 +0000 | [diff] [blame^] | 177 | sqlite3_bind_text $VM 2 ".." 1 |
danielk1977 | 51e3d8e | 2004-05-20 01:12:34 +0000 | [diff] [blame] | 178 | sqlite3_bind_text $VM 3 world -1 |
| 179 | sqlite_step $VM N VALUES COLNAMES |
danielk1977 | 106bb23 | 2004-05-21 10:08:53 +0000 | [diff] [blame] | 180 | sqlite3_reset $VM |
danielk1977 | 51e3d8e | 2004-05-20 01:12:34 +0000 | [diff] [blame] | 181 | execsql {SELECT rowid, * FROM t1} |
| 182 | } {1 hello . world} |
| 183 | do_test bind-6.2 { |
danielk1977 | 35bb9d0 | 2004-05-24 12:55:54 +0000 | [diff] [blame] | 184 | execsql {SELECT typeof(a), typeof(b), typeof(c) FROM t1} |
| 185 | } {text text text} |
danielk1977 | 51e3d8e | 2004-05-20 01:12:34 +0000 | [diff] [blame] | 186 | do_test bind-6.3 { |
| 187 | execsql { |
| 188 | DELETE FROM t1; |
| 189 | } |
| 190 | } {} |
| 191 | |
| 192 | # UTF-16 text |
| 193 | do_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 |
danielk1977 | 106bb23 | 2004-05-21 10:08:53 +0000 | [diff] [blame] | 198 | sqlite3_reset $VM |
danielk1977 | 51e3d8e | 2004-05-20 01:12:34 +0000 | [diff] [blame] | 199 | execsql {SELECT rowid, * FROM t1} |
| 200 | } {1 hello {} world} |
| 201 | do_test bind-7.2 { |
danielk1977 | 35bb9d0 | 2004-05-24 12:55:54 +0000 | [diff] [blame] | 202 | execsql {SELECT typeof(a), typeof(b), typeof(c) FROM t1} |
| 203 | } {text text text} |
danielk1977 | 51e3d8e | 2004-05-20 01:12:34 +0000 | [diff] [blame] | 204 | do_test bind-7.3 { |
| 205 | execsql { |
| 206 | DELETE FROM t1; |
| 207 | } |
| 208 | } {} |
| 209 | |
danielk1977 | 6622cce | 2004-05-20 11:00:52 +0000 | [diff] [blame] | 210 | # Test that the 'out of range' error works. |
| 211 | do_test bind-8.1 { |
| 212 | catch { sqlite3_bind_null $VM 0 } |
| 213 | } {1} |
| 214 | do_test bind-8.2 { |
| 215 | sqlite3_errmsg $DB |
| 216 | } {bind index out of range} |
| 217 | do_test bind-8.3 { |
| 218 | encoding convertfrom unicode [sqlite3_errmsg16 $DB] |
| 219 | } {bind index out of range} |
| 220 | do_test bind-8.4 { |
| 221 | sqlite3_bind_null $VM 1 |
| 222 | sqlite3_errmsg $DB |
| 223 | } {not an error} |
| 224 | do_test bind-8.5 { |
| 225 | catch { sqlite3_bind_null $VM 4 } |
| 226 | } {1} |
| 227 | do_test bind-8.6 { |
| 228 | sqlite3_errmsg $DB |
| 229 | } {bind index out of range} |
| 230 | do_test bind-8.7 { |
| 231 | encoding convertfrom unicode [sqlite3_errmsg16 $DB] |
| 232 | } {bind index out of range} |
| 233 | |
| 234 | |
| 235 | do_test bind-9.99 { |
danielk1977 | 106bb23 | 2004-05-21 10:08:53 +0000 | [diff] [blame] | 236 | sqlite3_finalize $VM |
danielk1977 | 3cf8606 | 2004-05-26 10:11:05 +0000 | [diff] [blame] | 237 | } SQLITE_OK |
danielk1977 | 51e3d8e | 2004-05-20 01:12:34 +0000 | [diff] [blame] | 238 | |
| 239 | |
drh | 82a4851 | 2003-09-06 22:45:20 +0000 | [diff] [blame] | 240 | |
| 241 | finish_test |