3 # The author disclaims copyright to this source code. In place of
4 # a legal notice, here is a blessing:
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.
10 #***********************************************************************
13 set testdir [file dirname $argv0]
14 source $testdir/tester.tcl
15 set testprefix e_blobclose
17 set dots [string repeat . 40]
19 CREATE TABLE x1(a INTEGER PRIMARY KEY, b DOTS);
20 INSERT INTO x1 VALUES(-1, $dots);
21 INSERT INTO x1 VALUES(-10, $dots);
22 INSERT INTO x1 VALUES(-100, $dots);
23 INSERT INTO x1 VALUES(-1000, $dots);
24 INSERT INTO x1 VALUES(-10000, $dots);
27 # EVIDENCE-OF: R-03145-46390 This function closes an open BLOB handle.
29 # It's not clear how to test that a blob handle really is closed.
30 # Attempting to use a closed blob handle will likely crash the process.
31 # Assume here that if the SHARED lock on the db file is released,
32 # the blob handle has been closed.
34 do_execsql_test 1.1 { PRAGMA lock_status } {main unlocked temp closed}
35 sqlite3_blob_open db main x1 b -1 0 B
36 do_execsql_test 1.2 { PRAGMA lock_status } {main shared temp closed}
38 do_execsql_test 1.3 { PRAGMA lock_status } {main unlocked temp closed}
41 # EVIDENCE-OF: R-34027-00617 If the blob handle being closed was opened
42 # for read-write access, and if the database is in auto-commit mode and
43 # there are no other open read-write blob handles or active write
44 # statements, the current transaction is committed.
46 # 2.1.*: Transaction is not committed if there are other open
47 # read-write blob handles.
49 # 2.2.*: Transaction is not committed if not in auto-commit mode.
51 # 2.3.*: Active write statements.
54 sqlite3_blob_open db main x1 b -100 1 B1
55 sqlite3_blob_open db main x1 b -1000 1 B2
56 sqlite3_blob_open db main x1 b -10000 1 B3
57 sqlite3_blob_open db main x1 b -10000 0 B4 ;# B4 is read-only!
58 execsql { PRAGMA lock_status }
59 } {main reserved temp closed}
61 sqlite3_blob_close $B1
62 execsql { PRAGMA lock_status }
63 } {main reserved temp closed}
65 sqlite3_blob_close $B2
66 execsql { PRAGMA lock_status }
67 } {main reserved temp closed}
69 sqlite3_blob_close $B3
70 execsql { PRAGMA lock_status }
71 } {main shared temp closed}
73 sqlite3_blob_close $B4
74 execsql { PRAGMA lock_status }
75 } {main unlocked temp closed}
78 sqlite3_blob_open db main x1 b -100 1 B1
79 execsql { PRAGMA lock_status }
80 } {main reserved temp closed}
83 sqlite3_blob_close $B1
84 execsql { PRAGMA lock_status }
85 } {main reserved temp closed}
88 execsql { PRAGMA lock_status }
89 } {main unlocked temp closed}
92 sqlite3_blob_close $::B
93 db eval { PRAGMA lock_status }
97 sqlite3_blob_open db main x1 b -100 1 B
98 execsql { PRAGMA lock_status }
99 } {main reserved temp closed}
101 execsql { INSERT INTO x1 VALUES(15, val()) }
102 execsql { PRAGMA lock_status }
103 } {main unlocked temp closed}
105 execsql { SELECT * FROM x1 WHERE a = 15 }
106 } {15 {main reserved temp closed}}
108 # A reader does not inhibit commit.
110 sqlite3_blob_open db main x1 b -100 1 B
111 execsql { PRAGMA lock_status }
112 } {main reserved temp closed}
114 execsql { SELECT a, val() FROM x1 LIMIT 1 }
115 } {-10000 {main shared temp closed}}
119 sqlite3_blob_open db main x1 b -10 1 B
121 INSERT INTO x1 VALUES(1, 'abc');
122 SELECT * FROM x1 WHERE a=1;
126 sqlite3_blob_write $B 0 "abcdefghij" 10
127 execsql { SELECT * FROM x1 WHERE a=-10 }
128 } {-10 abcdefghij..............................}
132 execsql { BEGIN ; SELECT * FROM x1 } db2
133 sqlite3_blob_close $B
136 # EVIDENCE-OF: R-41959-38737 Otherwise, if this function is passed a
137 # valid open blob handle, the values returned by the sqlite3_errcode()
138 # and sqlite3_errmsg() functions are set before returning.
141 list [sqlite3_errcode db] [sqlite3_errmsg db]
142 } {SQLITE_BUSY {database is locked}}
144 # EVIDENCE-OF: R-37801-37633 The BLOB handle is closed unconditionally.
145 # Even if this routine returns an error code, the handle is still
148 # Test that the lock has been released. Assume this means the handle
149 # is closed, even though blob_close() returned SQLITE_BUSY.
151 do_execsql_test 3.4 { PRAGMA lock_status } {main unlocked temp closed}
153 # EVIDENCE-OF: R-35111-05628 If an error occurs while committing the
154 # transaction, an error code is returned and the transaction rolled
157 # Row 1 is removed (it was inserted this transaction) and row -10
158 # is restored to its original state. Transaction has been rolled back.
160 do_execsql_test 3.5 {
161 SELECT * FROM x1 WHERE a IN (1, -10);
162 } {-10 ........................................}
164 # EVIDENCE-OF: R-25894-51060 Calling this routine with a null pointer
165 # (such as would be returned by a failed call to sqlite3_blob_open()) is
168 do_test 4.0 { sqlite3_blob_close 0 } {}