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
16 set testprefix incrblob3
24 sqlite3_db_config_lookaside db 0 0 0
26 do_execsql_test incrblob3-1.1 {
27 CREATE TABLE blobs(k INTEGER PRIMARY KEY, v BLOB);
28 INSERT INTO blobs VALUES(1, zeroblob(100));
29 INSERT INTO blobs VALUES(2, zeroblob(100));
32 # Test the sqlite3_blob_reopen()/read()/write() functions.
34 do_test incrblob3-1.2 {
35 set ::blob [db incrblob blobs v 1]
36 puts $::blob "hello world"
39 do_test incrblob3-1.3 {
40 sqlite3_blob_reopen $::blob 2
41 puts $::blob "world hello"
44 do_test incrblob3-1.4 {
45 sqlite3_blob_reopen $::blob 1
49 do_test incrblob3-1.5 {
50 sqlite3_blob_reopen $::blob 2
54 do_test incrblob3-1.6 { close $::blob } {}
56 # Test some error conditions.
58 # incrblob3-2.1: Attempting to reopen a row that does not exist.
59 # incrblob3-2.2: Attempting to reopen a row that does not contain a blob
62 do_test incrblob3-2.1.1 {
63 set ::blob [db incrblob blobs v 1]
64 list [catch {sqlite3_blob_reopen $::blob 3} msg] $msg
66 do_test incrblob3-2.1.2 {
67 list [sqlite3_errcode db] [sqlite3_errmsg db]
68 } {SQLITE_ERROR {no such rowid: 3}}
69 do_test incrblob3-2.1.3 {
70 list [catch {sqlite3_blob_reopen $::blob 1} msg] $msg
72 do_test incrblob3-2.1.4 { close $::blob } {}
74 do_execsql_test incrblob3-2.2.1 {
75 INSERT INTO blobs VALUES(3, 42);
76 INSERT INTO blobs VALUES(4, 54.4);
77 INSERT INTO blobs VALUES(5, NULL);
79 foreach {tn rowid type} {
84 do_test incrblob3-2.2.$tn.1 {
85 set ::blob [db incrblob blobs v 1]
86 list [catch {sqlite3_blob_reopen $::blob $rowid} msg] $msg
88 do_test incrblob3-2.2.$tn.2 {
89 list [sqlite3_errcode db] [sqlite3_errmsg db]
90 } "SQLITE_ERROR {cannot open value of type $type}"
92 do_test incrblob3-2.2.$tn.3 {
93 list [catch {sqlite3_blob_reopen $::blob 1} msg] $msg
95 do_test incrblob3-2.2.$tn.4 {
96 list [catch {sqlite3_blob_read $::blob 0 10} msg] $msg
98 do_test incrblob3-2.2.$tn.5 {
99 list [catch {sqlite3_blob_write $::blob 0 "abcd"} msg] $msg
101 do_test incrblob3-2.2.$tn.6 {
102 sqlite3_blob_bytes $::blob
105 do_test incrblob3-2.2.$tn.7 { close $::blob } {}
108 # Test that passing NULL to sqlite3_blob_XXX() APIs returns SQLITE_MISUSE.
110 # incrblob3-3.1: sqlite3_blob_reopen()
111 # incrblob3-3.2: sqlite3_blob_read()
112 # incrblob3-3.3: sqlite3_blob_write()
113 # incrblob3-3.4: sqlite3_blob_bytes()
115 do_test incrblob3-3.1 {
116 list [catch {sqlite3_blob_reopen {} 3} msg] $msg
119 do_test incrblob3-3.2 {
120 list [catch {sqlite3_blob_read {} 0 10} msg] $msg
123 do_test incrblob3-3.3 {
124 list [catch {sqlite3_blob_write {} 0 "abcd"} msg] $msg
127 do_test incrblob3-3.4 { sqlite3_blob_bytes {} } {0}
129 do_test incrblob3-3.5 { sqlite3_blob_close {} } {}
131 # Test out-of-range reading and writing
133 do_test incrblob3-4.1 {
134 set ::blob [db incrblob blobs v 1]
135 sqlite3_blob_bytes $::blob
137 do_test incrblob3-4.2 {
138 list [catch { sqlite3_blob_read $::blob -1 10 } msg] $msg
140 do_test incrblob3-4.3 {
141 list [catch { sqlite3_blob_read $::blob 0 -10 } msg] $msg
143 do_test incrblob3-4.4 {
144 list [catch { sqlite3_blob_read $::blob 95 10 } msg] $msg
146 do_test incrblob3-4.5 {
147 list [catch { sqlite3_blob_write $::blob -1 "abcdefghij" 10 } msg] $msg
149 do_test incrblob3-4.6 {
150 list [catch { sqlite3_blob_write $::blob 0 "abcdefghij" -10 } msg] $msg
152 do_test incrblob3-4.7 {
153 list [catch { sqlite3_blob_write $::blob 95 "abcdefghij" } msg] $msg
156 do_test incrblob3-4.8 { close $::blob } {}
158 # Test that modifying the row a blob handle points to aborts the blob.
160 do_test incrblob3-5.1 {
161 set ::blob [db incrblob blobs v 1]
162 sqlite3_blob_bytes $::blob
164 do_test incrblob3-5.2 {
165 execsql { UPDATE blobs SET v = '123456789012345678901234567890' WHERE k = 1 }
166 list [catch { sqlite3_blob_read $::blob 0 10 } msg] $msg
169 # Test various errors that can occur in sqlite3_blob_open():
171 # 1. Trying to open a virtual table column.
172 # 2. Trying to open a view column.
173 # 3. Trying to open a column that does not exist.
174 # 4. Trying to open a read/write handle on an indexed column.
175 # 5. Trying to open a read/write handle on the child key of an FK constraint.
178 do_test incrblob3-6.1 {
180 CREATE VIRTUAL TABLE ft USING fts3;
181 INSERT INTO ft VALUES('rules to open a column to which');
184 list [catch { db incrblob ft content 1 } msg] $msg
185 } {1 {cannot open virtual table: ft}}
188 do_test incrblob3-6.2 {
189 execsql { CREATE VIEW v1 AS SELECT * FROM blobs }
190 list [catch { db incrblob v1 content 1 } msg] $msg
191 } {1 {cannot open view: v1}}
194 do_test incrblob3-6.3 {
195 list [catch { db incrblob blobs content 1 } msg] $msg
196 } {1 {no such column: "content"}}
198 do_test incrblob3-6.4.1 {
200 CREATE TABLE t1(a, b);
201 CREATE INDEX i1 ON t1(b);
202 INSERT INTO t1 VALUES(zeroblob(100), zeroblob(100));
204 list [catch { db incrblob t1 b 1 } msg] $msg
205 } {1 {cannot open indexed column for writing}}
206 do_test incrblob3-6.4.2 {
207 set ::blob [db incrblob t1 a 1]
210 do_test incrblob3-6.4.3 {
211 set ::blob [db incrblob -readonly t1 b 1]
215 do_test incrblob3-6.5.1 {
217 CREATE TABLE p1(a PRIMARY KEY);
218 CREATE TABLE c1(a, b REFERENCES p1);
219 PRAGMA foreign_keys = 1;
220 INSERT INTO p1 VALUES(zeroblob(100));
221 INSERT INTO c1 VALUES(zeroblob(100), zeroblob(100));
223 list [catch { db incrblob c1 b 1 } msg] $msg
224 } {1 {cannot open foreign key column for writing}}
226 do_test incrblob3-6.5.2 {
227 set ::blob [db incrblob c1 a 1]
230 do_test incrblob3-6.5.3 {
231 set ::blob [db incrblob -readonly c1 b 1]
234 do_test incrblob3-6.5.4 {
235 execsql { PRAGMA foreign_keys = 0 }
236 set ::blob [db incrblob c1 b 1]
241 # Test that sqlite3_blob_open() handles transient and persistent schema
244 do_test incrblob3-7.1 {
246 sqlite3_db_config_lookaside db2 0 0 0
247 execsql { CREATE TABLE t2(x) } db2
248 set ::blob [db incrblob blobs v 1]
253 testvfs tvfs -default 1
255 tvfs script access_method
257 proc access_method {args} {
258 set schemacookie [hexio_get_int [hexio_read test.db 40 4]]
260 hexio_write test.db 40 [hexio_render_int32 $schemacookie]
262 set dbversion [hexio_get_int [hexio_read test.db 24 4]]
264 hexio_write test.db 24 [hexio_render_int32 $dbversion]
269 do_test incrblob3-7.2 {
271 sqlite3_db_config_lookaside db 0 0 0
272 list [catch {db incrblob blobs v 1} msg] $msg
273 } {1 {database schema has changed}}
277 #-------------------------------------------------------------------------
281 do_execsql_test 8.1 {
282 CREATE TABLE t1(a INTEGER PRIMARY KEY, b);
283 ATTACH 'test.db2' AS aux;
284 CREATE TABLE aux.t1(a INTEGER PRIMARY KEY, b);
286 INSERT INTO t1 VALUES(4, 'hello');
287 INSERT INTO aux.t1 VALUES(4, 'world');
291 set ::blob [db incrblob -readonly main t1 b 4]
297 set ::blob [db incrblob -readonly aux t1 b 4]
303 set ::blob [db incrblob -readonly t1 b 4]
309 list [catch { db incrblob -readonly nosuchdb t1 b 4 } msg] $msg
310 } {1 {no such table: nosuchdb.t1}}