drh | b19a2bc | 2001-09-16 00:13:26 +0000 | [diff] [blame] | 1 | # 2001 September 15 |
drh | 960e8c6 | 2001-04-03 16:53:21 +0000 | [diff] [blame] | 2 | # |
drh | b19a2bc | 2001-09-16 00:13:26 +0000 | [diff] [blame] | 3 | # The author disclaims copyright to this source code. In place of |
| 4 | # a legal notice, here is a blessing: |
drh | 960e8c6 | 2001-04-03 16:53:21 +0000 | [diff] [blame] | 5 | # |
drh | b19a2bc | 2001-09-16 00:13:26 +0000 | [diff] [blame] | 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. |
drh | 960e8c6 | 2001-04-03 16:53:21 +0000 | [diff] [blame] | 9 | # |
| 10 | #*********************************************************************** |
| 11 | # This file implements regression tests for TCL interface to the |
| 12 | # SQLite library. |
| 13 | # |
| 14 | # Actually, all tests are based on the TCL interface, so the main |
| 15 | # interface is pretty well tested. This file contains some addition |
| 16 | # tests for fringe issues that the main test suite does not cover. |
| 17 | # |
danielk1977 | cd38d52 | 2009-01-02 17:33:46 +0000 | [diff] [blame^] | 18 | # $Id: tclsqlite.test,v 1.71 2009/01/02 17:33:46 danielk1977 Exp $ |
drh | 960e8c6 | 2001-04-03 16:53:21 +0000 | [diff] [blame] | 19 | |
| 20 | set testdir [file dirname $argv0] |
| 21 | source $testdir/tester.tcl |
| 22 | |
| 23 | # Check the error messages generated by tclsqlite |
| 24 | # |
drh | ef4ac8f | 2004-06-19 00:16:31 +0000 | [diff] [blame] | 25 | if {[sqlite3 -has-codec]} { |
drh | 9eb9e26 | 2004-02-11 02:18:05 +0000 | [diff] [blame] | 26 | set r "sqlite_orig HANDLE FILENAME ?-key CODEC-KEY?" |
drh | 22fbcb8 | 2004-02-01 01:22:50 +0000 | [diff] [blame] | 27 | } else { |
drh | 039963a | 2008-09-03 00:43:15 +0000 | [diff] [blame] | 28 | set r "sqlite3 HANDLE FILENAME ?-vfs VFSNAME? ?-readonly BOOLEAN? ?-create BOOLEAN? ?-nomutex BOOLEAN? ?-fullmutex BOOLEAN?" |
drh | 22fbcb8 | 2004-02-01 01:22:50 +0000 | [diff] [blame] | 29 | } |
drh | 960e8c6 | 2001-04-03 16:53:21 +0000 | [diff] [blame] | 30 | do_test tcl-1.1 { |
drh | ef4ac8f | 2004-06-19 00:16:31 +0000 | [diff] [blame] | 31 | set v [catch {sqlite3 bogus} msg] |
drh | df81a25 | 2008-08-25 19:09:01 +0000 | [diff] [blame] | 32 | regsub {really_sqlite3} $msg {sqlite3} msg |
drh | 960e8c6 | 2001-04-03 16:53:21 +0000 | [diff] [blame] | 33 | lappend v $msg |
drh | 22fbcb8 | 2004-02-01 01:22:50 +0000 | [diff] [blame] | 34 | } [list 1 "wrong # args: should be \"$r\""] |
drh | 960e8c6 | 2001-04-03 16:53:21 +0000 | [diff] [blame] | 35 | do_test tcl-1.2 { |
| 36 | set v [catch {db bogus} msg] |
| 37 | lappend v $msg |
drh | 073e7b1 | 2008-10-09 14:45:25 +0000 | [diff] [blame] | 38 | } {1 {bad option "bogus": must be authorizer, busy, cache, changes, close, collate, collation_needed, commit_hook, complete, copy, enable_load_extension, errorcode, eval, exists, function, incrblob, interrupt, last_insert_rowid, nullvalue, onecolumn, profile, progress, rekey, rollback_hook, status, timeout, total_changes, trace, transaction, update_hook, or version}} |
danielk1977 | 191fadc | 2007-10-23 08:17:48 +0000 | [diff] [blame] | 39 | do_test tcl-1.2.1 { |
| 40 | set v [catch {db cache bogus} msg] |
| 41 | lappend v $msg |
| 42 | } {1 {bad option "bogus": must be flush or size}} |
| 43 | do_test tcl-1.2.2 { |
| 44 | set v [catch {db cache} msg] |
| 45 | lappend v $msg |
| 46 | } {1 {wrong # args: should be "db cache option ?arg?"}} |
drh | 960e8c6 | 2001-04-03 16:53:21 +0000 | [diff] [blame] | 47 | do_test tcl-1.3 { |
| 48 | execsql {CREATE TABLE t1(a int, b int)} |
| 49 | execsql {INSERT INTO t1 VALUES(10,20)} |
| 50 | set v [catch { |
| 51 | db eval {SELECT * FROM t1} data { |
| 52 | error "The error message" |
| 53 | } |
| 54 | } msg] |
| 55 | lappend v $msg |
| 56 | } {1 {The error message}} |
| 57 | do_test tcl-1.4 { |
| 58 | set v [catch { |
| 59 | db eval {SELECT * FROM t2} data { |
| 60 | error "The error message" |
| 61 | } |
| 62 | } msg] |
| 63 | lappend v $msg |
| 64 | } {1 {no such table: t2}} |
| 65 | do_test tcl-1.5 { |
| 66 | set v [catch { |
| 67 | db eval {SELECT * FROM t1} data { |
| 68 | break |
| 69 | } |
| 70 | } msg] |
| 71 | lappend v $msg |
| 72 | } {0 {}} |
drh | d65e530 | 2008-04-28 13:02:57 +0000 | [diff] [blame] | 73 | catch {expr x*} msg |
drh | 960e8c6 | 2001-04-03 16:53:21 +0000 | [diff] [blame] | 74 | do_test tcl-1.6 { |
| 75 | set v [catch { |
| 76 | db eval {SELECT * FROM t1} data { |
| 77 | expr x* |
| 78 | } |
| 79 | } msg] |
| 80 | lappend v $msg |
drh | d65e530 | 2008-04-28 13:02:57 +0000 | [diff] [blame] | 81 | } [list 1 $msg] |
drh | 0f14e2e | 2004-06-29 12:39:08 +0000 | [diff] [blame] | 82 | do_test tcl-1.7 { |
| 83 | set v [catch {db} msg] |
| 84 | lappend v $msg |
| 85 | } {1 {wrong # args: should be "db SUBCOMMAND ..."}} |
drh | 1211de3 | 2004-07-26 12:24:22 +0000 | [diff] [blame] | 86 | if {[catch {db auth {}}]==0} { |
| 87 | do_test tcl-1.8 { |
| 88 | set v [catch {db authorizer 1 2 3} msg] |
| 89 | lappend v $msg |
| 90 | } {1 {wrong # args: should be "db authorizer ?CALLBACK?"}} |
| 91 | } |
drh | 0f14e2e | 2004-06-29 12:39:08 +0000 | [diff] [blame] | 92 | do_test tcl-1.9 { |
| 93 | set v [catch {db busy 1 2 3} msg] |
| 94 | lappend v $msg |
| 95 | } {1 {wrong # args: should be "db busy CALLBACK"}} |
| 96 | do_test tcl-1.10 { |
| 97 | set v [catch {db progress 1} msg] |
| 98 | lappend v $msg |
| 99 | } {1 {wrong # args: should be "db progress N CALLBACK"}} |
| 100 | do_test tcl-1.11 { |
| 101 | set v [catch {db changes xyz} msg] |
| 102 | lappend v $msg |
| 103 | } {1 {wrong # args: should be "db changes "}} |
| 104 | do_test tcl-1.12 { |
| 105 | set v [catch {db commit_hook a b c} msg] |
| 106 | lappend v $msg |
| 107 | } {1 {wrong # args: should be "db commit_hook ?CALLBACK?"}} |
drh | ccae602 | 2005-02-26 17:31:26 +0000 | [diff] [blame] | 108 | ifcapable {complete} { |
| 109 | do_test tcl-1.13 { |
| 110 | set v [catch {db complete} msg] |
| 111 | lappend v $msg |
| 112 | } {1 {wrong # args: should be "db complete SQL"}} |
| 113 | } |
drh | 0f14e2e | 2004-06-29 12:39:08 +0000 | [diff] [blame] | 114 | do_test tcl-1.14 { |
| 115 | set v [catch {db eval} msg] |
| 116 | lappend v $msg |
drh | 895d747 | 2004-08-20 16:02:39 +0000 | [diff] [blame] | 117 | } {1 {wrong # args: should be "db eval SQL ?ARRAY-NAME? ?SCRIPT?"}} |
drh | 0f14e2e | 2004-06-29 12:39:08 +0000 | [diff] [blame] | 118 | do_test tcl-1.15 { |
| 119 | set v [catch {db function} msg] |
| 120 | lappend v $msg |
drh | e3602be | 2008-09-09 12:31:33 +0000 | [diff] [blame] | 121 | } {1 {wrong # args: should be "db function NAME [-argcount N] SCRIPT"}} |
danielk1977 | 7ddad96 | 2005-12-12 06:53:03 +0000 | [diff] [blame] | 122 | do_test tcl-1.16 { |
drh | 0f14e2e | 2004-06-29 12:39:08 +0000 | [diff] [blame] | 123 | set v [catch {db last_insert_rowid xyz} msg] |
| 124 | lappend v $msg |
| 125 | } {1 {wrong # args: should be "db last_insert_rowid "}} |
danielk1977 | 7ddad96 | 2005-12-12 06:53:03 +0000 | [diff] [blame] | 126 | do_test tcl-1.17 { |
drh | 0f14e2e | 2004-06-29 12:39:08 +0000 | [diff] [blame] | 127 | set v [catch {db rekey} msg] |
| 128 | lappend v $msg |
| 129 | } {1 {wrong # args: should be "db rekey KEY"}} |
danielk1977 | 7ddad96 | 2005-12-12 06:53:03 +0000 | [diff] [blame] | 130 | do_test tcl-1.18 { |
drh | 0f14e2e | 2004-06-29 12:39:08 +0000 | [diff] [blame] | 131 | set v [catch {db timeout} msg] |
| 132 | lappend v $msg |
| 133 | } {1 {wrong # args: should be "db timeout MILLISECONDS"}} |
danielk1977 | 7ddad96 | 2005-12-12 06:53:03 +0000 | [diff] [blame] | 134 | do_test tcl-1.19 { |
drh | 0f14e2e | 2004-06-29 12:39:08 +0000 | [diff] [blame] | 135 | set v [catch {db collate} msg] |
| 136 | lappend v $msg |
| 137 | } {1 {wrong # args: should be "db collate NAME SCRIPT"}} |
danielk1977 | 7ddad96 | 2005-12-12 06:53:03 +0000 | [diff] [blame] | 138 | do_test tcl-1.20 { |
drh | 0f14e2e | 2004-06-29 12:39:08 +0000 | [diff] [blame] | 139 | set v [catch {db collation_needed} msg] |
| 140 | lappend v $msg |
| 141 | } {1 {wrong # args: should be "db collation_needed SCRIPT"}} |
danielk1977 | 7ddad96 | 2005-12-12 06:53:03 +0000 | [diff] [blame] | 142 | do_test tcl-1.21 { |
drh | 0f14e2e | 2004-06-29 12:39:08 +0000 | [diff] [blame] | 143 | set v [catch {db total_changes xyz} msg] |
| 144 | lappend v $msg |
| 145 | } {1 {wrong # args: should be "db total_changes "}} |
tpoindex | 1067fe1 | 2004-12-17 15:41:11 +0000 | [diff] [blame] | 146 | do_test tcl-1.20 { |
| 147 | set v [catch {db copy} msg] |
| 148 | lappend v $msg |
| 149 | } {1 {wrong # args: should be "db copy CONFLICT-ALGORITHM TABLE FILENAME ?SEPARATOR? ?NULLINDICATOR?"}} |
danielk1977 | 95c8a54 | 2007-09-01 06:51:27 +0000 | [diff] [blame] | 150 | do_test tcl-1.21 { |
| 151 | set v [catch {sqlite3 db2 test.db -vfs nosuchvfs} msg] |
| 152 | lappend v $msg |
| 153 | } {1 {no such vfs: nosuchvfs}} |
drh | 960e8c6 | 2001-04-03 16:53:21 +0000 | [diff] [blame] | 154 | |
drh | 3570ad9 | 2007-08-31 14:31:44 +0000 | [diff] [blame] | 155 | catch {unset ::result} |
| 156 | do_test tcl-2.1 { |
| 157 | execsql "CREATE TABLE t\u0123x(a int, b\u1235 float)" |
| 158 | } {} |
| 159 | ifcapable schema_pragmas { |
| 160 | do_test tcl-2.2 { |
| 161 | execsql "PRAGMA table_info(t\u0123x)" |
| 162 | } "0 a int 0 {} 0 1 b\u1235 float 0 {} 0" |
drh | 6d4abfb | 2001-10-22 02:58:08 +0000 | [diff] [blame] | 163 | } |
drh | 3570ad9 | 2007-08-31 14:31:44 +0000 | [diff] [blame] | 164 | do_test tcl-2.3 { |
| 165 | execsql "INSERT INTO t\u0123x VALUES(1,2.3)" |
| 166 | db eval "SELECT * FROM t\u0123x" result break |
| 167 | set result(*) |
| 168 | } "a b\u1235" |
drh | 6d4abfb | 2001-10-22 02:58:08 +0000 | [diff] [blame] | 169 | |
drh | 6d4abfb | 2001-10-22 02:58:08 +0000 | [diff] [blame] | 170 | |
drh | 5d9d757 | 2003-08-19 14:31:01 +0000 | [diff] [blame] | 171 | # Test the onecolumn method |
| 172 | # |
| 173 | do_test tcl-3.1 { |
| 174 | execsql { |
| 175 | INSERT INTO t1 SELECT a*2, b*2 FROM t1; |
| 176 | INSERT INTO t1 SELECT a*2+1, b*2+1 FROM t1; |
| 177 | INSERT INTO t1 SELECT a*2+3, b*2+3 FROM t1; |
| 178 | } |
drh | 22fbcb8 | 2004-02-01 01:22:50 +0000 | [diff] [blame] | 179 | set rc [catch {db onecolumn {SELECT * FROM t1 ORDER BY a}} msg] |
| 180 | lappend rc $msg |
| 181 | } {0 10} |
drh | 5d9d757 | 2003-08-19 14:31:01 +0000 | [diff] [blame] | 182 | do_test tcl-3.2 { |
| 183 | db onecolumn {SELECT * FROM t1 WHERE a<0} |
| 184 | } {} |
| 185 | do_test tcl-3.3 { |
| 186 | set rc [catch {db onecolumn} errmsg] |
| 187 | lappend rc $errmsg |
| 188 | } {1 {wrong # args: should be "db onecolumn SQL"}} |
drh | 0f14e2e | 2004-06-29 12:39:08 +0000 | [diff] [blame] | 189 | do_test tcl-3.4 { |
| 190 | set rc [catch {db onecolumn {SELECT bogus}} errmsg] |
| 191 | lappend rc $errmsg |
| 192 | } {1 {no such column: bogus}} |
drh | 6bf8957 | 2004-11-03 16:27:01 +0000 | [diff] [blame] | 193 | ifcapable {tclvar} { |
| 194 | do_test tcl-3.5 { |
| 195 | set b 50 |
| 196 | set rc [catch {db one {SELECT * FROM t1 WHERE b>$b}} msg] |
| 197 | lappend rc $msg |
| 198 | } {0 41} |
| 199 | do_test tcl-3.6 { |
| 200 | set b 500 |
| 201 | set rc [catch {db one {SELECT * FROM t1 WHERE b>$b}} msg] |
| 202 | lappend rc $msg |
| 203 | } {0 {}} |
| 204 | do_test tcl-3.7 { |
| 205 | set b 500 |
| 206 | set rc [catch {db one { |
| 207 | INSERT INTO t1 VALUES(99,510); |
| 208 | SELECT * FROM t1 WHERE b>$b |
| 209 | }} msg] |
| 210 | lappend rc $msg |
| 211 | } {0 99} |
| 212 | } |
| 213 | ifcapable {!tclvar} { |
| 214 | execsql {INSERT INTO t1 VALUES(99,510)} |
| 215 | } |
drh | 5d9d757 | 2003-08-19 14:31:01 +0000 | [diff] [blame] | 216 | |
drh | 0f14e2e | 2004-06-29 12:39:08 +0000 | [diff] [blame] | 217 | # Turn the busy handler on and off |
| 218 | # |
| 219 | do_test tcl-4.1 { |
| 220 | proc busy_callback {cnt} { |
| 221 | break |
| 222 | } |
| 223 | db busy busy_callback |
| 224 | db busy |
| 225 | } {busy_callback} |
| 226 | do_test tcl-4.2 { |
| 227 | db busy {} |
| 228 | db busy |
| 229 | } {} |
drh | 6d4abfb | 2001-10-22 02:58:08 +0000 | [diff] [blame] | 230 | |
drh | 6bf8957 | 2004-11-03 16:27:01 +0000 | [diff] [blame] | 231 | ifcapable {tclvar} { |
| 232 | # Parsing of TCL variable names within SQL into bound parameters. |
| 233 | # |
| 234 | do_test tcl-5.1 { |
| 235 | execsql {CREATE TABLE t3(a,b,c)} |
| 236 | catch {unset x} |
danielk1977 | 7a1d17f | 2008-08-29 15:54:56 +0000 | [diff] [blame] | 237 | set x(1) A |
| 238 | set x(2) B |
drh | 6bf8957 | 2004-11-03 16:27:01 +0000 | [diff] [blame] | 239 | execsql { |
| 240 | INSERT INTO t3 VALUES($::x(1),$::x(2),$::x(3)); |
| 241 | SELECT * FROM t3 |
| 242 | } |
danielk1977 | 7a1d17f | 2008-08-29 15:54:56 +0000 | [diff] [blame] | 243 | } {A B {}} |
drh | 6bf8957 | 2004-11-03 16:27:01 +0000 | [diff] [blame] | 244 | do_test tcl-5.2 { |
| 245 | execsql { |
| 246 | SELECT typeof(a), typeof(b), typeof(c) FROM t3 |
| 247 | } |
| 248 | } {text text null} |
| 249 | do_test tcl-5.3 { |
| 250 | catch {unset x} |
| 251 | set x [binary format h12 686900686f00] |
| 252 | execsql { |
| 253 | UPDATE t3 SET a=$::x; |
| 254 | } |
| 255 | db eval { |
| 256 | SELECT a FROM t3 |
| 257 | } break |
| 258 | binary scan $a h12 adata |
| 259 | set adata |
| 260 | } {686900686f00} |
| 261 | do_test tcl-5.4 { |
| 262 | execsql { |
| 263 | SELECT typeof(a), typeof(b), typeof(c) FROM t3 |
| 264 | } |
| 265 | } {blob text null} |
| 266 | } |
drh | 92febd9 | 2004-08-20 18:34:20 +0000 | [diff] [blame] | 267 | |
drh | fd241b0 | 2004-09-13 13:46:01 +0000 | [diff] [blame] | 268 | # Operation of "break" and "continue" within row scripts |
| 269 | # |
| 270 | do_test tcl-6.1 { |
| 271 | db eval {SELECT * FROM t1} { |
| 272 | break |
| 273 | } |
| 274 | lappend a $b |
| 275 | } {10 20} |
| 276 | do_test tcl-6.2 { |
| 277 | set cnt 0 |
| 278 | db eval {SELECT * FROM t1} { |
| 279 | if {$a>40} continue |
| 280 | incr cnt |
| 281 | } |
| 282 | set cnt |
| 283 | } {4} |
| 284 | do_test tcl-6.3 { |
| 285 | set cnt 0 |
| 286 | db eval {SELECT * FROM t1} { |
| 287 | if {$a<40} continue |
| 288 | incr cnt |
| 289 | } |
| 290 | set cnt |
| 291 | } {5} |
| 292 | do_test tcl-6.4 { |
| 293 | proc return_test {x} { |
| 294 | db eval {SELECT * FROM t1} { |
| 295 | if {$a==$x} {return $b} |
| 296 | } |
| 297 | } |
| 298 | return_test 10 |
| 299 | } 20 |
| 300 | do_test tcl-6.5 { |
| 301 | return_test 20 |
| 302 | } 40 |
| 303 | do_test tcl-6.6 { |
| 304 | return_test 99 |
| 305 | } 510 |
| 306 | do_test tcl-6.7 { |
| 307 | return_test 0 |
| 308 | } {} |
| 309 | |
danielk1977 | 4397de5 | 2005-01-12 12:44:03 +0000 | [diff] [blame] | 310 | do_test tcl-7.1 { |
| 311 | db version |
| 312 | expr 0 |
| 313 | } {0} |
| 314 | |
danielk1977 | 55c45f2 | 2005-04-03 23:54:43 +0000 | [diff] [blame] | 315 | # modify and reset the NULL representation |
| 316 | # |
| 317 | do_test tcl-8.1 { |
| 318 | db nullvalue NaN |
| 319 | execsql {INSERT INTO t1 VALUES(30,NULL)} |
| 320 | db eval {SELECT * FROM t1 WHERE b IS NULL} |
| 321 | } {30 NaN} |
| 322 | do_test tcl-8.2 { |
| 323 | db nullvalue NULL |
| 324 | db nullvalue |
| 325 | } {NULL} |
| 326 | do_test tcl-8.3 { |
| 327 | db nullvalue {} |
| 328 | db eval {SELECT * FROM t1 WHERE b IS NULL} |
| 329 | } {30 {}} |
| 330 | |
drh | c7f269d | 2005-05-05 10:30:29 +0000 | [diff] [blame] | 331 | # Test the return type of user-defined functions |
| 332 | # |
| 333 | do_test tcl-9.1 { |
| 334 | db function ret_str {return "hi"} |
| 335 | execsql {SELECT typeof(ret_str())} |
| 336 | } {text} |
| 337 | do_test tcl-9.2 { |
drh | 9645d8d | 2006-09-01 15:49:05 +0000 | [diff] [blame] | 338 | db function ret_dbl {return [expr {rand()*0.5}]} |
drh | c7f269d | 2005-05-05 10:30:29 +0000 | [diff] [blame] | 339 | execsql {SELECT typeof(ret_dbl())} |
| 340 | } {real} |
| 341 | do_test tcl-9.3 { |
drh | 9645d8d | 2006-09-01 15:49:05 +0000 | [diff] [blame] | 342 | db function ret_int {return [expr {int(rand()*200)}]} |
drh | c7f269d | 2005-05-05 10:30:29 +0000 | [diff] [blame] | 343 | execsql {SELECT typeof(ret_int())} |
| 344 | } {integer} |
| 345 | |
drh | d1e4733 | 2005-06-26 17:55:33 +0000 | [diff] [blame] | 346 | # Recursive calls to the same user-defined function |
| 347 | # |
danielk1977 | 3bdca9c | 2006-01-17 09:35:01 +0000 | [diff] [blame] | 348 | ifcapable tclvar { |
| 349 | do_test tcl-9.10 { |
| 350 | proc userfunc_r1 {n} { |
| 351 | if {$n<=0} {return 0} |
| 352 | set nm1 [expr {$n-1}] |
| 353 | return [expr {[db eval {SELECT r1($nm1)}]+$n}] |
| 354 | } |
| 355 | db function r1 userfunc_r1 |
| 356 | execsql {SELECT r1(10)} |
| 357 | } {55} |
| 358 | do_test tcl-9.11 { |
| 359 | execsql {SELECT r1(100)} |
| 360 | } {5050} |
| 361 | } |
drh | d1e4733 | 2005-06-26 17:55:33 +0000 | [diff] [blame] | 362 | |
drh | b5555e7 | 2005-08-02 17:15:14 +0000 | [diff] [blame] | 363 | # Tests for the new transaction method |
| 364 | # |
| 365 | do_test tcl-10.1 { |
| 366 | db transaction {} |
| 367 | } {} |
| 368 | do_test tcl-10.2 { |
| 369 | db transaction deferred {} |
| 370 | } {} |
| 371 | do_test tcl-10.3 { |
| 372 | db transaction immediate {} |
| 373 | } {} |
| 374 | do_test tcl-10.4 { |
| 375 | db transaction exclusive {} |
| 376 | } {} |
| 377 | do_test tcl-10.5 { |
| 378 | set rc [catch {db transaction xyzzy {}} msg] |
| 379 | lappend rc $msg |
| 380 | } {1 {bad transaction type "xyzzy": must be deferred, exclusive, or immediate}} |
| 381 | do_test tcl-10.6 { |
| 382 | set rc [catch {db transaction {error test-error}} msg] |
| 383 | lappend rc $msg |
| 384 | } {1 test-error} |
| 385 | do_test tcl-10.7 { |
| 386 | db transaction { |
| 387 | db eval {CREATE TABLE t4(x)} |
| 388 | db transaction { |
| 389 | db eval {INSERT INTO t4 VALUES(1)} |
| 390 | } |
| 391 | } |
| 392 | db eval {SELECT * FROM t4} |
| 393 | } 1 |
| 394 | do_test tcl-10.8 { |
| 395 | catch { |
| 396 | db transaction { |
| 397 | db eval {INSERT INTO t4 VALUES(2)} |
| 398 | db eval {INSERT INTO t4 VALUES(3)} |
| 399 | db eval {INSERT INTO t4 VALUES(4)} |
| 400 | error test-error |
| 401 | } |
| 402 | } |
| 403 | db eval {SELECT * FROM t4} |
| 404 | } 1 |
| 405 | do_test tcl-10.9 { |
| 406 | db transaction { |
| 407 | db eval {INSERT INTO t4 VALUES(2)} |
| 408 | catch { |
| 409 | db transaction { |
| 410 | db eval {INSERT INTO t4 VALUES(3)} |
| 411 | db eval {INSERT INTO t4 VALUES(4)} |
| 412 | error test-error |
| 413 | } |
| 414 | } |
| 415 | } |
| 416 | db eval {SELECT * FROM t4} |
danielk1977 | cd38d52 | 2009-01-02 17:33:46 +0000 | [diff] [blame^] | 417 | } {1 2} |
drh | b5555e7 | 2005-08-02 17:15:14 +0000 | [diff] [blame] | 418 | do_test tcl-10.10 { |
| 419 | for {set i 0} {$i<1} {incr i} { |
| 420 | db transaction { |
| 421 | db eval {INSERT INTO t4 VALUES(5)} |
| 422 | continue |
| 423 | } |
danielk1977 | cd38d52 | 2009-01-02 17:33:46 +0000 | [diff] [blame^] | 424 | error "This line should not be run" |
drh | b5555e7 | 2005-08-02 17:15:14 +0000 | [diff] [blame] | 425 | } |
| 426 | db eval {SELECT * FROM t4} |
danielk1977 | cd38d52 | 2009-01-02 17:33:46 +0000 | [diff] [blame^] | 427 | } {1 2 5} |
drh | b5555e7 | 2005-08-02 17:15:14 +0000 | [diff] [blame] | 428 | do_test tcl-10.11 { |
| 429 | for {set i 0} {$i<10} {incr i} { |
| 430 | db transaction { |
| 431 | db eval {INSERT INTO t4 VALUES(6)} |
| 432 | break |
| 433 | } |
| 434 | } |
| 435 | db eval {SELECT * FROM t4} |
danielk1977 | cd38d52 | 2009-01-02 17:33:46 +0000 | [diff] [blame^] | 436 | } {1 2 5 6} |
drh | b5555e7 | 2005-08-02 17:15:14 +0000 | [diff] [blame] | 437 | do_test tcl-10.12 { |
| 438 | set rc [catch { |
| 439 | for {set i 0} {$i<10} {incr i} { |
| 440 | db transaction { |
| 441 | db eval {INSERT INTO t4 VALUES(7)} |
| 442 | return |
| 443 | } |
| 444 | } |
| 445 | }] |
| 446 | } {2} |
| 447 | do_test tcl-10.13 { |
| 448 | db eval {SELECT * FROM t4} |
danielk1977 | cd38d52 | 2009-01-02 17:33:46 +0000 | [diff] [blame^] | 449 | } {1 2 5 6 7} |
| 450 | |
| 451 | # Now test that [db transaction] commands may be nested with |
| 452 | # the expected results. |
| 453 | # |
| 454 | do_test tcl-10.14 { |
| 455 | db transaction { |
| 456 | db eval { |
| 457 | DELETE FROM t4; |
| 458 | INSERT INTO t4 VALUES('one'); |
| 459 | } |
| 460 | |
| 461 | catch { |
| 462 | db transaction { |
| 463 | db eval { INSERT INTO t4 VALUES('two') } |
| 464 | db transaction { |
| 465 | db eval { INSERT INTO t4 VALUES('three') } |
| 466 | error "throw an error!" |
| 467 | } |
| 468 | } |
| 469 | } |
| 470 | } |
| 471 | |
| 472 | db eval {SELECT * FROM t4} |
| 473 | } {one} |
| 474 | do_test tcl-10.15 { |
| 475 | # Make sure a transaction has not been left open. |
| 476 | db eval {BEGIN ; COMMIT} |
| 477 | } {} |
| 478 | do_test tcl-10.16 { |
| 479 | db transaction { |
| 480 | db eval { INSERT INTO t4 VALUES('two'); } |
| 481 | db transaction { |
| 482 | db eval { INSERT INTO t4 VALUES('three') } |
| 483 | db transaction { |
| 484 | db eval { INSERT INTO t4 VALUES('four') } |
| 485 | } |
| 486 | } |
| 487 | } |
| 488 | db eval {SELECT * FROM t4} |
| 489 | } {one two three four} |
| 490 | do_test tcl-10.17 { |
| 491 | catch { |
| 492 | db transaction { |
| 493 | db eval { INSERT INTO t4 VALUES('A'); } |
| 494 | db transaction { |
| 495 | db eval { INSERT INTO t4 VALUES('B') } |
| 496 | db transaction { |
| 497 | db eval { INSERT INTO t4 VALUES('C') } |
| 498 | error "throw an error!" |
| 499 | } |
| 500 | } |
| 501 | } |
| 502 | } |
| 503 | db eval {SELECT * FROM t4} |
| 504 | } {one two three four} |
| 505 | do_test tcl-10.18 { |
| 506 | # Make sure a transaction has not been left open. |
| 507 | db eval {BEGIN ; COMMIT} |
| 508 | } {} |
| 509 | |
| 510 | # Mess up a [db transaction] command by locking the database using a |
| 511 | # second connection when it tries to commit. Make sure the transaction |
| 512 | # is not still open after the "database is locked" exception is thrown. |
| 513 | # |
| 514 | do_test tcl-10.18 { |
| 515 | sqlite3 db2 test.db |
| 516 | db2 eval { |
| 517 | BEGIN; |
| 518 | SELECT * FROM sqlite_master; |
| 519 | } |
| 520 | |
| 521 | set rc [catch { |
| 522 | db transaction { |
| 523 | db eval {INSERT INTO t4 VALUES('five')} |
| 524 | } |
| 525 | } msg] |
| 526 | list $rc $msg |
| 527 | } {1 {database is locked}} |
| 528 | do_test tcl-10.19 { |
| 529 | db eval {BEGIN ; COMMIT} |
| 530 | } {} |
| 531 | |
| 532 | # Thwart a [db transaction] command by locking the database using a |
| 533 | # second connection with "BEGIN EXCLUSIVE". Make sure no transaction is |
| 534 | # open after the "database is locked" exception is thrown. |
| 535 | # |
| 536 | do_test tcl-10.20 { |
| 537 | db2 eval { |
| 538 | COMMIT; |
| 539 | BEGIN EXCLUSIVE; |
| 540 | } |
| 541 | set rc [catch { |
| 542 | db transaction { |
| 543 | db eval {INSERT INTO t4 VALUES('five')} |
| 544 | } |
| 545 | } msg] |
| 546 | list $rc $msg |
| 547 | } {1 {database is locked}} |
| 548 | do_test tcl-10.21 { |
| 549 | db2 close |
| 550 | db eval {BEGIN ; COMMIT} |
| 551 | } {} |
| 552 | do_test tcl-10.22 { |
| 553 | sqlite3 db2 test.db |
| 554 | db transaction exclusive { |
| 555 | catch { db2 eval {SELECT * FROM sqlite_master} } msg |
| 556 | set msg "db2: $msg" |
| 557 | } |
| 558 | set msg |
| 559 | } {db2: database is locked} |
| 560 | db2 close |
drh | c7f269d | 2005-05-05 10:30:29 +0000 | [diff] [blame] | 561 | |
drh | 97f2ebc | 2005-12-10 21:19:04 +0000 | [diff] [blame] | 562 | do_test tcl-11.1 { |
danielk1977 | cd38d52 | 2009-01-02 17:33:46 +0000 | [diff] [blame^] | 563 | db eval {INSERT INTO t4 VALUES(6)} |
| 564 | db exists {SELECT x,x*2,x+x FROM t4 WHERE x==6} |
drh | 97f2ebc | 2005-12-10 21:19:04 +0000 | [diff] [blame] | 565 | } {1} |
| 566 | do_test tcl-11.2 { |
danielk1977 | cd38d52 | 2009-01-02 17:33:46 +0000 | [diff] [blame^] | 567 | db exists {SELECT 0 FROM t4 WHERE x==6} |
drh | 97f2ebc | 2005-12-10 21:19:04 +0000 | [diff] [blame] | 568 | } {1} |
| 569 | do_test tcl-11.3 { |
| 570 | db exists {SELECT 1 FROM t4 WHERE x==8} |
| 571 | } {0} |
| 572 | |
danielk1977 | 161fb79 | 2006-01-24 10:58:21 +0000 | [diff] [blame] | 573 | do_test tcl-12.1 { |
| 574 | unset -nocomplain a b c version |
| 575 | set version [db version] |
| 576 | scan $version "%d.%d.%d" a b c |
| 577 | expr $a*1000000 + $b*1000 + $c |
| 578 | } [sqlite3_libversion_number] |
| 579 | |
drh | 4f5e80f | 2007-06-19 17:15:46 +0000 | [diff] [blame] | 580 | |
| 581 | # Check to see that when bindings of the form @aaa are used instead |
drh | 1c74781 | 2007-06-19 23:01:41 +0000 | [diff] [blame] | 582 | # of $aaa, that objects are treated as bytearray and are inserted |
| 583 | # as BLOBs. |
drh | 4f5e80f | 2007-06-19 17:15:46 +0000 | [diff] [blame] | 584 | # |
danielk1977 | 4152e67 | 2007-09-12 17:01:45 +0000 | [diff] [blame] | 585 | ifcapable tclvar { |
| 586 | do_test tcl-13.1 { |
| 587 | db eval {CREATE TABLE t5(x BLOB)} |
| 588 | set x abc123 |
| 589 | db eval {INSERT INTO t5 VALUES($x)} |
| 590 | db eval {SELECT typeof(x) FROM t5} |
| 591 | } {text} |
| 592 | do_test tcl-13.2 { |
| 593 | binary scan $x H notUsed |
| 594 | db eval { |
| 595 | DELETE FROM t5; |
| 596 | INSERT INTO t5 VALUES($x); |
| 597 | SELECT typeof(x) FROM t5; |
| 598 | } |
| 599 | } {text} |
| 600 | do_test tcl-13.3 { |
| 601 | db eval { |
| 602 | DELETE FROM t5; |
| 603 | INSERT INTO t5 VALUES(@x); |
| 604 | SELECT typeof(x) FROM t5; |
| 605 | } |
| 606 | } {blob} |
| 607 | do_test tcl-13.4 { |
| 608 | set y 1234 |
| 609 | db eval { |
| 610 | DELETE FROM t5; |
| 611 | INSERT INTO t5 VALUES(@y); |
| 612 | SELECT hex(x), typeof(x) FROM t5 |
| 613 | } |
| 614 | } {31323334 blob} |
| 615 | } |
drh | 1c74781 | 2007-06-19 23:01:41 +0000 | [diff] [blame] | 616 | |
drh | 4f5e80f | 2007-06-19 17:15:46 +0000 | [diff] [blame] | 617 | |
drh | 960e8c6 | 2001-04-03 16:53:21 +0000 | [diff] [blame] | 618 | finish_test |