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_blobopen
20 ATTACH 'test.db2' AS aux;
22 CREATE TABLE main.t1(a INTEGER PRIMARY KEY, b TEXT, c BLOB);
23 CREATE TEMP TABLE t1(a INTEGER PRIMARY KEY, b TEXT, c BLOB);
24 CREATE TABLE aux.t1(a INTEGER PRIMARY KEY, b TEXT, c BLOB);
26 CREATE TABLE main.x1(a INTEGER PRIMARY KEY, b TEXT, c BLOB);
27 CREATE TEMP TABLE x2(a INTEGER PRIMARY KEY, b TEXT, c BLOB);
28 CREATE TABLE aux.x3(a INTEGER PRIMARY KEY, b TEXT, c BLOB);
30 INSERT INTO main.t1 VALUES(1, 'main one', X'0101');
31 INSERT INTO main.t1 VALUES(2, 'main two', X'0102');
32 INSERT INTO main.t1 VALUES(3, 'main three', X'0103');
33 INSERT INTO main.t1 VALUES(4, 'main four', X'0104');
34 INSERT INTO main.t1 VALUES(5, 'main five', X'0105');
36 INSERT INTO main.x1 VALUES(1, 'x main one', X'000101');
37 INSERT INTO main.x1 VALUES(2, 'x main two', X'000102');
38 INSERT INTO main.x1 VALUES(3, 'x main three', X'000103');
39 INSERT INTO main.x1 VALUES(4, 'x main four', X'000104');
40 INSERT INTO main.x1 VALUES(5, 'x main five', X'000105');
42 INSERT INTO temp.t1 VALUES(1, 'temp one', X'0201');
43 INSERT INTO temp.t1 VALUES(2, 'temp two', X'0202');
44 INSERT INTO temp.t1 VALUES(3, 'temp three', X'0203');
45 INSERT INTO temp.t1 VALUES(4, 'temp four', X'0204');
46 INSERT INTO temp.t1 VALUES(5, 'temp five', X'0205');
48 INSERT INTO temp.x2 VALUES(1, 'x temp one', X'000201');
49 INSERT INTO temp.x2 VALUES(2, 'x temp two', X'000202');
50 INSERT INTO temp.x2 VALUES(3, 'x temp three', X'000203');
51 INSERT INTO temp.x2 VALUES(4, 'x temp four', X'000204');
52 INSERT INTO temp.x2 VALUES(5, 'x temp five', X'000205');
54 INSERT INTO aux.t1 VALUES(1, 'aux one', X'0301');
55 INSERT INTO aux.t1 VALUES(2, 'aux two', X'0302');
56 INSERT INTO aux.t1 VALUES(3, 'aux three', X'0303');
57 INSERT INTO aux.t1 VALUES(4, 'aux four', X'0304');
58 INSERT INTO aux.t1 VALUES(5, 'aux five', X'0305');
60 INSERT INTO aux.x3 VALUES(1, 'x aux one', X'000301');
61 INSERT INTO aux.x3 VALUES(2, 'x aux two', X'000302');
62 INSERT INTO aux.x3 VALUES(3, 'x aux three', X'000303');
63 INSERT INTO aux.x3 VALUES(4, 'x aux four', X'000304');
64 INSERT INTO aux.x3 VALUES(5, 'x aux five', X'000305');
67 #-------------------------------------------------------------------------
68 # EVIDENCE-OF: R-37639-55938 This interfaces opens a handle to the BLOB
69 # located in row iRow, column zColumn, table zTable in database zDb; in
70 # other words, the same BLOB that would be selected by: SELECT zColumn
71 # FROM zDb.zTable WHERE rowid = iRow;
73 proc read_blob {zDb zTab zCol iRow} {
74 sqlite3_blob_open db $zDb $zTab $zCol $iRow 0 B
75 set nByte [sqlite3_blob_bytes $B]
76 set data [sqlite3_blob_read $B 0 $nByte]
81 do_test 1.1.1 { read_blob main t1 b 1 } "main one"
82 do_test 1.1.2 { read_blob main t1 c 1 } "\01\01"
83 do_test 1.1.3 { read_blob temp t1 b 1 } "temp one"
84 do_test 1.1.4 { read_blob temp t1 c 1 } "\02\01"
85 do_test 1.1.6 { read_blob aux t1 b 1 } "aux one"
86 do_test 1.1.7 { read_blob aux t1 c 1 } "\03\01"
88 do_test 1.2.1 { read_blob main t1 b 4 } "main four"
89 do_test 1.2.2 { read_blob main t1 c 4 } "\01\04"
90 do_test 1.2.3 { read_blob temp t1 b 4 } "temp four"
91 do_test 1.2.4 { read_blob temp t1 c 4 } "\02\04"
92 do_test 1.2.6 { read_blob aux t1 b 4 } "aux four"
93 do_test 1.2.7 { read_blob aux t1 c 4 } "\03\04"
95 do_test 1.3.1 { read_blob main x1 b 2 } "x main two"
96 do_test 1.3.2 { read_blob main x1 c 2 } "\00\01\02"
97 do_test 1.3.3 { read_blob temp x2 b 2 } "x temp two"
98 do_test 1.3.4 { read_blob temp x2 c 2 } "\00\02\02"
99 do_test 1.3.6 { read_blob aux x3 b 2 } "x aux two"
100 do_test 1.3.7 { read_blob aux x3 c 2 } "\00\03\02"
102 #-------------------------------------------------------------------------
103 # EVIDENCE-OF: R-27234-05761 Parameter zDb is not the filename that
104 # contains the database, but rather the symbolic name of the database.
105 # For attached databases, this is the name that appears after the AS
106 # keyword in the ATTACH statement. For the main database file, the
107 # database name is "main". For TEMP tables, the database name is "temp".
109 # The test cases immediately above demonstrate that the database name
110 # for the main db, for TEMP tables and for those in attached databases
111 # is correct. The following tests check that filenames cannot be
115 list [catch { sqlite3_blob_open db "test.db" t1 b 1 0 B } msg] $msg
118 list [catch { sqlite3_blob_open db "test.db2" t1 b 1 0 B } msg] $msg
121 #-------------------------------------------------------------------------
122 # EVIDENCE-OF: R-50854-53979 If the flags parameter is non-zero, then
123 # the BLOB is opened for read and write access.
125 # EVIDENCE-OF: R-03922-41160 If the flags parameter is zero, the BLOB is
126 # opened for read-only access.
128 foreach {tn iRow flags} {
136 sqlite3_blob_open db main x1 c $iRow $flags B
137 set n [sqlite3_blob_bytes $B]
138 sqlite3_blob_read $B 0 $n
139 } [binary format ccc 0 1 $iRow]
142 # Blob was opened for read-only access - writing returns an error.
144 list [catch { sqlite3_blob_write $B 0 xxx 3 } msg] $msg
145 } {1 SQLITE_READONLY}
147 do_execsql_test 3.$tn.3 {
148 SELECT c FROM x1 WHERE a=$iRow;
149 } [binary format ccc 0 1 $iRow]
151 # Blob was opened for read/write access - writing succeeds
153 list [catch { sqlite3_blob_write $B 0 xxx 3 } msg] $msg
156 do_execsql_test 3.$tn.5 {
157 SELECT c FROM x1 WHERE a=$iRow;
161 sqlite3_blob_close $B
164 #-------------------------------------------------------------------------
167 do_execsql_test 4.0 {
168 CREATE TABLE t1(x, y);
169 INSERT INTO t1 VALUES('abcd', 152);
170 INSERT INTO t1 VALUES(NULL, X'00010203');
171 INSERT INTO t1 VALUES('', 154.2);
173 CREATE TABLE t2(x PRIMARY KEY, y) WITHOUT ROWID;
174 INSERT INTO t2 VALUES(1, 'blob');
176 CREATE TABLE t3(a PRIMARY KEY, b, c, d, e, f, UNIQUE(e, f));
177 INSERT INTO t3 VALUES('aaaa', 'bbbb', 'cccc', 'dddd', 'eeee', 'ffff');
178 CREATE INDEX t3b ON t3(b);
180 CREATE TABLE p1(x PRIMARY KEY);
181 INSERT INTO p1 VALUES('abc');
183 CREATE TABLE c1(a INTEGER PRIMARY KEY, b REFERENCES p1);
184 INSERT INTO c1 VALUES(45, 'abc');
187 proc test_blob_open {tn zDb zTab zCol iRow flags errcode errmsg} {
191 if {$errcode=="SQLITE_OK"} {
194 set expected "1 $errcode"
198 catch { sqlite3_blob_open db $zDb $zTab $zCol $iRow $flags B } msg
200 do_test 4.$tn.1 { set ::res } $expected
202 # EVIDENCE-OF: R-08940-21305 Unless it returns SQLITE_MISUSE, this
203 # function sets the database connection error code and message
204 # accessible via sqlite3_errcode() and sqlite3_errmsg() and related
207 # This proc (test_blob_open) is used below to test various error and
208 # non-error conditions. But never SQLITE_MISUSE conditions. So these
209 # test cases are considered as partly verifying the requirement above.
210 # See below for a test of the SQLITE_MISUSE case.
219 # EVIDENCE-OF: R-31086-35521 On success, SQLITE_OK is returned and the
220 # new BLOB handle is stored in *ppBlob. Otherwise an error code is
221 # returned and, unless the error code is SQLITE_MISUSE, *ppBlob is set
226 } [expr {$errcode != "SQLITE_OK"}]
228 # EVIDENCE-OF: R-63421-15521 This means that, provided the API is not
229 # misused, it is always safe to call sqlite3_blob_close() on *ppBlob
230 # after this function it returns.
232 sqlite3_blob_close $B
236 # EVIDENCE-OF: R-31204-44780 Database zDb does not exist
237 test_blob_open 1 nosuchdb t1 x 1 0 SQLITE_ERROR "no such table: nosuchdb.t1"
239 # EVIDENCE-OF: R-28676-08005 Table zTable does not exist within database zDb
240 test_blob_open 2 main tt1 x 1 0 SQLITE_ERROR "no such table: main.tt1"
242 # EVIDENCE-OF: R-40134-30296 Table zTable is a WITHOUT ROWID table
243 test_blob_open 3 main t2 y 1 0 SQLITE_ERROR \
244 "cannot open table without rowid: t2"
246 # EVIDENCE-OF: R-56376-21261 Column zColumn does not exist
247 test_blob_open 4 main t1 z 2 0 SQLITE_ERROR "no such column: \"z\""
249 # EVIDENCE-OF: R-28258-23166 Row iRow is not present in the table
250 test_blob_open 5 main t1 y 6 0 SQLITE_ERROR "no such rowid: 6"
252 # EVIDENCE-OF: R-11683-62380 The specified column of row iRow contains a
253 # value that is not a TEXT or BLOB value
254 test_blob_open 6 main t1 x 2 0 SQLITE_ERROR "cannot open value of type null"
255 test_blob_open 7 main t1 y 1 0 SQLITE_ERROR "cannot open value of type integer"
256 test_blob_open 8 main t1 y 3 0 SQLITE_ERROR "cannot open value of type real"
258 # EVIDENCE-OF: R-34146-30782 Column zColumn is part of an index, PRIMARY
259 # KEY or UNIQUE constraint and the blob is being opened for read/write
262 # Test cases 8.1.* show that such columns can be opened for read-access.
263 # Tests 8.2.* show that read-write access is different. Columns "c" and "c"
264 # are not part of an index, PK or UNIQUE constraint, so they work in both
267 test_blob_open 8.1.1 main t3 a 1 0 SQLITE_OK "not an error"
268 test_blob_open 8.1.2 main t3 b 1 0 SQLITE_OK "not an error"
269 test_blob_open 8.1.3 main t3 c 1 0 SQLITE_OK "not an error"
270 test_blob_open 8.1.4 main t3 d 1 0 SQLITE_OK "not an error"
271 test_blob_open 8.1.5 main t3 e 1 0 SQLITE_OK "not an error"
272 test_blob_open 8.1.6 main t3 f 1 0 SQLITE_OK "not an error"
274 set cannot "cannot open indexed column for writing"
275 test_blob_open 8.2.1 main t3 a 1 8 SQLITE_ERROR $cannot
276 test_blob_open 8.2.2 main t3 b 1 8 SQLITE_ERROR $cannot
277 test_blob_open 8.2.3 main t3 c 1 8 SQLITE_OK "not an error"
278 test_blob_open 8.2.4 main t3 d 1 8 SQLITE_OK "not an error"
279 test_blob_open 8.2.5 main t3 e 1 8 SQLITE_ERROR $cannot
280 test_blob_open 8.2.6 main t3 f 1 8 SQLITE_ERROR $cannot
282 # EVIDENCE-OF: R-50117-55204 Foreign key constraints are enabled, column
283 # zColumn is part of a child key definition and the blob is being opened
284 # for read/write access
286 # 9.1: FK disabled, read-only access.
287 # 9.2: FK disabled, read-only access.
288 # 9.3: FK enabled, read/write access.
289 # 9.4: FK enabled, read/write access.
291 test_blob_open 9.1 main c1 b 45 0 SQLITE_OK "not an error"
292 test_blob_open 9.2 main c1 b 45 1 SQLITE_OK "not an error"
293 execsql { PRAGMA foreign_keys = ON }
294 test_blob_open 9.3 main c1 b 45 0 SQLITE_OK "not an error"
295 test_blob_open 9.4 main c1 b 45 1 SQLITE_ERROR \
296 "cannot open foreign key column for writing"
298 #-------------------------------------------------------------------------
299 # EVIDENCE-OF: R-08940-21305 Unless it returns SQLITE_MISUSE, this
300 # function sets the database connection error code and message
301 # accessible via sqlite3_errcode() and sqlite3_errmsg() and related
304 # This requirement is partially verified by the many uses of test
305 # command [test_blob_open] above. All that is left is to verify the
306 # SQLITE_MISUSE case.
308 # SQLITE_MISUSE is only returned if SQLITE_ENABLE_API_ARMOR is defined
309 # during compilation.
311 ifcapable api_armor {
312 sqlite3_blob_open db main t1 x 1 0 B
315 list [catch {sqlite3_blob_open $B main t1 x 1 0 B2} msg] $msg
318 list [sqlite3_errcode db] [sqlite3_errmsg db]
319 } {SQLITE_OK {not an error}}
320 sqlite3_blob_close $B
323 list [catch {sqlite3_blob_open db main {} x 1 0 B} msg] $msg
326 list [sqlite3_errcode db] [sqlite3_errmsg db]
327 } {SQLITE_OK {not an error}}
330 #-------------------------------------------------------------------------
331 # EVIDENCE-OF: R-50542-62589 If the row that a BLOB handle points to is
332 # modified by an UPDATE, DELETE, or by ON CONFLICT side-effects then the
333 # BLOB handle is marked as "expired". This is true if any column of the
334 # row is changed, even a column other than the one the BLOB handle is
337 # EVIDENCE-OF: R-48367-20048 Calls to sqlite3_blob_read() and
338 # sqlite3_blob_write() for an expired BLOB handle fail with a return
339 # code of SQLITE_ABORT.
341 # 11.2: read-only handle, DELETE.
342 # 11.3: read-only handle, UPDATE.
343 # 11.4: read-only handle, REPLACE.
344 # 11.5: read/write handle, DELETE.
345 # 11.6: read/write handle, UPDATE.
346 # 11.7: read/write handle, REPLACE.
348 do_execsql_test 11.1 {
349 CREATE TABLE b1(a INTEGER PRIMARY KEY, b, c UNIQUE);
350 INSERT INTO b1 VALUES(1, '1234567890', 1);
351 INSERT INTO b1 VALUES(2, '1234567890', 2);
352 INSERT INTO b1 VALUES(3, '1234567890', 3);
353 INSERT INTO b1 VALUES(4, '1234567890', 4);
354 INSERT INTO b1 VALUES(5, '1234567890', 5);
355 INSERT INTO b1 VALUES(6, '1234567890', 6);
357 CREATE TABLE b2(a INTEGER PRIMARY KEY, b, c UNIQUE);
358 INSERT INTO b2 VALUES(1, '1234567890', 1);
359 INSERT INTO b2 VALUES(2, '1234567890', 2);
360 INSERT INTO b2 VALUES(3, '1234567890', 3);
361 INSERT INTO b2 VALUES(4, '1234567890', 4);
362 INSERT INTO b2 VALUES(5, '1234567890', 5);
363 INSERT INTO b2 VALUES(6, '1234567890', 6);
367 sqlite3_blob_open db main b1 b 2 0 B
368 sqlite3_blob_read $B 0 10
371 # Deleting a different row does not invalidate the blob handle.
372 execsql { DELETE FROM b1 WHERE a = 1 }
373 sqlite3_blob_read $B 0 10
376 execsql { DELETE FROM b1 WHERE a = 2 }
377 list [catch { sqlite3_blob_read $B 0 10 } msg] $msg
380 sqlite3_blob_close $B
384 sqlite3_blob_open db main b1 b 3 0 B
385 sqlite3_blob_read $B 0 10
388 # Updating a different row
389 execsql { UPDATE b1 SET c = 42 WHERE a=4 }
390 sqlite3_blob_read $B 0 10
393 execsql { UPDATE b1 SET c = 43 WHERE a=3 }
394 list [catch { sqlite3_blob_read $B 0 10 } msg] $msg
397 sqlite3_blob_close $B
401 sqlite3_blob_open db main b1 b 6 0 B
402 sqlite3_blob_read $B 0 10
405 # Replace a different row
406 execsql { INSERT OR REPLACE INTO b1 VALUES(10, 'abcdefghij', 5) }
407 sqlite3_blob_read $B 0 10
410 execsql { INSERT OR REPLACE INTO b1 VALUES(11, 'abcdefghij', 6) }
411 list [catch { sqlite3_blob_read $B 0 10 } msg] $msg
414 sqlite3_blob_close $B
418 sqlite3_blob_open db main b2 b 2 1 B
419 sqlite3_blob_write $B 0 "abcdefghij"
422 # Deleting a different row does not invalidate the blob handle.
423 execsql { DELETE FROM b2 WHERE a = 1 }
424 sqlite3_blob_write $B 0 "ABCDEFGHIJ"
427 execsql { DELETE FROM b2 WHERE a = 2 }
428 list [catch { sqlite3_blob_write $B 0 "0987654321" } msg] $msg
431 sqlite3_blob_close $B
435 sqlite3_blob_open db main b2 b 3 1 B
436 sqlite3_blob_write $B 0 "abcdefghij"
439 # Updating a different row
440 execsql { UPDATE b2 SET c = 42 WHERE a=4 }
441 sqlite3_blob_write $B 0 "ABCDEFGHIJ"
444 execsql { UPDATE b2 SET c = 43 WHERE a=3 }
445 list [catch { sqlite3_blob_write $B 0 "0987654321" } msg] $msg
448 sqlite3_blob_close $B
452 sqlite3_blob_open db main b2 b 6 1 B
453 sqlite3_blob_write $B 0 "abcdefghij"
456 # Replace a different row
457 execsql { INSERT OR REPLACE INTO b2 VALUES(10, 'abcdefghij', 5) }
458 sqlite3_blob_write $B 0 "ABCDEFGHIJ"
461 execsql { INSERT OR REPLACE INTO b2 VALUES(11, 'abcdefghij', 6) }
462 list [catch { sqlite3_blob_write $B 0 "0987654321" } msg] $msg
465 sqlite3_blob_close $B
468 #-------------------------------------------------------------------------
469 # EVIDENCE-OF: R-45408-40694 Changes written into a BLOB prior to the
470 # BLOB expiring are not rolled back by the expiration of the BLOB. Such
471 # changes will eventually commit if the transaction continues to
474 do_execsql_test 12.1 {
475 CREATE TABLE b3(x INTEGER PRIMARY KEY, y TEXT, z INTEGER);
476 INSERT INTO b3 VALUES(22, '..........', NULL);
479 sqlite3_blob_open db main b3 y 22 1 B
480 sqlite3_blob_write $B 0 "xxxxx" 5
482 do_execsql_test 12.3 {
483 UPDATE b3 SET z = 'not null';
486 list [catch {sqlite3_blob_write $B 5 "xxxxx" 5} msg] $msg
488 do_execsql_test 12.5 {
490 } {22 xxxxx..... {not null}}
492 sqlite3_blob_close $B
494 do_execsql_test 12.6 {
496 } {22 xxxxx..... {not null}}
498 #-------------------------------------------------------------------------
499 # EVIDENCE-OF: R-58813-55036 The sqlite3_bind_zeroblob() and
500 # sqlite3_result_zeroblob() interfaces and the built-in zeroblob SQL
501 # function may be used to create a zero-filled blob to read or write
502 # using the incremental-blob interface.
504 do_execsql_test 13.1 {
505 CREATE TABLE c2(i INTEGER PRIMARY KEY, j);
506 INSERT INTO c2 VALUES(10, zeroblob(24));
510 set stmt [sqlite3_prepare_v2 db "INSERT INTO c2 VALUES(11, ?)" -1]
511 sqlite3_bind_zeroblob $stmt 1 45
513 sqlite3_finalize $stmt
516 # The blobs can be read:
519 sqlite3_blob_open db main c2 j 10 1 B
520 sqlite3_blob_open db main c2 j 11 1 B2
521 list [sqlite3_blob_bytes $B] [sqlite3_blob_bytes $B2]
524 sqlite3_blob_read $B 0 24
525 } [string repeat [binary format c 0] 24]
527 sqlite3_blob_read $B2 0 45
528 } [string repeat [binary format c 0] 45]
533 sqlite3_blob_write $B 0 [string repeat [binary format c 1] 24]
536 sqlite3_blob_write $B2 0 [string repeat [binary format c 1] 45]
539 sqlite3_blob_close $B
540 sqlite3_blob_close $B2
541 execsql { SELECT j FROM c2 }
543 [string repeat [binary format c 1] 24] \
544 [string repeat [binary format c 1] 45] \