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 #***********************************************************************
14 set testdir [file dirname $argv0]
15 source $testdir/tester.tcl
18 sqlite3_db_config_lookaside db 0 0 0
20 do_execsql_test incrblob3-1.1 {
21 CREATE TABLE blobs(k INTEGER PRIMARY KEY, v BLOB);
22 INSERT INTO blobs VALUES(1, zeroblob(100));
23 INSERT INTO blobs VALUES(2, zeroblob(100));
26 # Test the sqlite3_blob_reopen()/read()/write() functions.
28 do_test incrblob3-1.2 {
29 set ::blob [db incrblob blobs v 1]
30 puts $::blob "hello world"
33 do_test incrblob3-1.3 {
34 sqlite3_blob_reopen $::blob 2
35 puts $::blob "world hello"
38 do_test incrblob3-1.4 {
39 sqlite3_blob_reopen $::blob 1
43 do_test incrblob3-1.5 {
44 sqlite3_blob_reopen $::blob 2
48 do_test incrblob3-1.6 { close $::blob } {}
50 # Test some error conditions.
52 # incrblob3-2.1: Attempting to reopen a row that does not exist.
53 # incrblob3-2.2: Attempting to reopen a row that does not contain a blob
56 do_test incrblob3-2.1.1 {
57 set ::blob [db incrblob blobs v 1]
58 list [catch {sqlite3_blob_reopen $::blob 3} msg] $msg
60 do_test incrblob3-2.1.2 {
61 list [sqlite3_errcode db] [sqlite3_errmsg db]
62 } {SQLITE_ERROR {no such rowid: 3}}
63 do_test incrblob3-2.1.3 {
64 list [catch {sqlite3_blob_reopen $::blob 1} msg] $msg
66 do_test incrblob3-2.1.4 { close $::blob } {}
68 do_execsql_test incrblob3-2.2.1 {
69 INSERT INTO blobs VALUES(3, 42);
70 INSERT INTO blobs VALUES(4, 54.4);
71 INSERT INTO blobs VALUES(5, NULL);
73 foreach {tn rowid type} {
78 do_test incrblob3-2.2.$tn.1 {
79 set ::blob [db incrblob blobs v 1]
80 list [catch {sqlite3_blob_reopen $::blob $rowid} msg] $msg
82 do_test incrblob3-2.2.$tn.2 {
83 list [sqlite3_errcode db] [sqlite3_errmsg db]
84 } "SQLITE_ERROR {cannot open value of type $type}"
86 do_test incrblob3-2.2.$tn.3 {
87 list [catch {sqlite3_blob_reopen $::blob 1} msg] $msg
89 do_test incrblob3-2.2.$tn.4 {
90 list [catch {sqlite3_blob_read $::blob 0 10} msg] $msg
92 do_test incrblob3-2.2.$tn.5 {
93 list [catch {sqlite3_blob_write $::blob 0 "abcd"} msg] $msg
95 do_test incrblob3-2.2.$tn.6 {
96 sqlite3_blob_bytes $::blob
99 do_test incrblob3-2.2.$tn.7 { close $::blob } {}
102 # Test that passing NULL to sqlite3_blob_XXX() APIs returns SQLITE_MISUSE.
104 # incrblob3-3.1: sqlite3_blob_reopen()
105 # incrblob3-3.2: sqlite3_blob_read()
106 # incrblob3-3.3: sqlite3_blob_write()
107 # incrblob3-3.4: sqlite3_blob_bytes()
109 do_test incrblob3-3.1 {
110 list [catch {sqlite3_blob_reopen {} 3} msg] $msg
113 do_test incrblob3-3.2 {
114 list [catch {sqlite3_blob_read {} 0 10} msg] $msg
117 do_test incrblob3-3.3 {
118 list [catch {sqlite3_blob_write {} 0 "abcd"} msg] $msg
121 do_test incrblob3-3.4 { sqlite3_blob_bytes {} } {0}
123 do_test incrblob3-3.5 { sqlite3_blob_close {} } {}
125 # Test out-of-range reading and writing
127 do_test incrblob3-4.1 {
128 set ::blob [db incrblob blobs v 1]
129 sqlite3_blob_bytes $::blob
131 do_test incrblob3-4.2 {
132 list [catch { sqlite3_blob_read $::blob -1 10 } msg] $msg
134 do_test incrblob3-4.3 {
135 list [catch { sqlite3_blob_read $::blob 0 -10 } msg] $msg
137 do_test incrblob3-4.4 {
138 list [catch { sqlite3_blob_read $::blob 95 10 } msg] $msg
140 do_test incrblob3-4.5 {
141 list [catch { sqlite3_blob_write $::blob -1 "abcdefghij" 10 } msg] $msg
143 do_test incrblob3-4.6 {
144 list [catch { sqlite3_blob_write $::blob 0 "abcdefghij" -10 } msg] $msg
146 do_test incrblob3-4.7 {
147 list [catch { sqlite3_blob_write $::blob 95 "abcdefghij" } msg] $msg
150 do_test incrblob3-4.8 { close $::blob } {}
152 # Test that modifying the row a blob handle points to aborts the blob.
154 do_test incrblob3-5.1 {
155 set ::blob [db incrblob blobs v 1]
156 sqlite3_blob_bytes $::blob
158 do_test incrblob3-5.2 {
159 execsql { UPDATE blobs SET v = '123456789012345678901234567890' WHERE k = 1 }
160 list [catch { sqlite3_blob_read $::blob 0 10 } msg] $msg
163 # Test various errors that can occur in sqlite3_blob_open():
165 # 1. Trying to open a virtual table column.
166 # 2. Trying to open a view column.
167 # 3. Trying to open a column that does not exist.
168 # 4. Trying to open a read/write handle on an indexed column.
169 # 5. Trying to open a read/write handle on the child key of an FK constraint.
172 do_test incrblob3-6.1 {
174 CREATE VIRTUAL TABLE ft USING fts3;
175 INSERT INTO ft VALUES('rules to open a column to which');
178 list [catch { db incrblob ft content 1 } msg] $msg
179 } {1 {cannot open virtual table: ft}}
182 do_test incrblob3-6.2 {
183 execsql { CREATE VIEW v1 AS SELECT * FROM blobs }
184 list [catch { db incrblob v1 content 1 } msg] $msg
185 } {1 {cannot open view: v1}}
188 do_test incrblob3-6.3 {
189 list [catch { db incrblob blobs content 1 } msg] $msg
190 } {1 {no such column: "content"}}
192 do_test incrblob3-6.4.1 {
194 CREATE TABLE t1(a, b);
195 CREATE INDEX i1 ON t1(b);
196 INSERT INTO t1 VALUES(zeroblob(100), zeroblob(100));
198 list [catch { db incrblob t1 b 1 } msg] $msg
199 } {1 {cannot open indexed column for writing}}
200 do_test incrblob3-6.4.2 {
201 set ::blob [db incrblob t1 a 1]
204 do_test incrblob3-6.4.3 {
205 set ::blob [db incrblob -readonly t1 b 1]
209 do_test incrblob3-6.5.1 {
211 CREATE TABLE p1(a PRIMARY KEY);
212 CREATE TABLE c1(a, b REFERENCES p1);
213 PRAGMA foreign_keys = 1;
214 INSERT INTO p1 VALUES(zeroblob(100));
215 INSERT INTO c1 VALUES(zeroblob(100), zeroblob(100));
217 list [catch { db incrblob c1 b 1 } msg] $msg
218 } {1 {cannot open foreign key column for writing}}
220 do_test incrblob3-6.5.2 {
221 set ::blob [db incrblob c1 a 1]
224 do_test incrblob3-6.5.3 {
225 set ::blob [db incrblob -readonly c1 b 1]
228 do_test incrblob3-6.5.4 {
229 execsql { PRAGMA foreign_keys = 0 }
230 set ::blob [db incrblob c1 b 1]
235 # Test that sqlite3_blob_open() handles transient and persistent schema
238 do_test incrblob3-7.1 {
240 sqlite3_db_config_lookaside db2 0 0 0
241 execsql { CREATE TABLE t2(x) } db2
242 set ::blob [db incrblob blobs v 1]
247 testvfs tvfs -default 1
249 tvfs script access_method
251 proc access_method {args} {
252 set schemacookie [hexio_get_int [hexio_read test.db 40 4]]
254 hexio_write test.db 40 [hexio_render_int32 $schemacookie]
256 set dbversion [hexio_get_int [hexio_read test.db 24 4]]
258 hexio_write test.db 24 [hexio_render_int32 $dbversion]
263 do_test incrblob3-7.2 {
265 sqlite3_db_config_lookaside db 0 0 0
266 list [catch {db incrblob blobs v 1} msg] $msg
267 } {1 {database schema has changed}}