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
25 ATTACH 'test.db2' AS aux;
27 CREATE TABLE main.t1(a INTEGER PRIMARY KEY, b TEXT, c BLOB);
28 CREATE TEMP TABLE t1(a INTEGER PRIMARY KEY, b TEXT, c BLOB);
29 CREATE TABLE aux.t1(a INTEGER PRIMARY KEY, b TEXT, c BLOB);
31 CREATE TABLE main.x1(a INTEGER PRIMARY KEY, b TEXT, c BLOB);
32 CREATE TEMP TABLE x2(a INTEGER PRIMARY KEY, b TEXT, c BLOB);
33 CREATE TABLE aux.x3(a INTEGER PRIMARY KEY, b TEXT, c BLOB);
35 INSERT INTO main.t1 VALUES(1, 'main one', X'0101');
36 INSERT INTO main.t1 VALUES(2, 'main two', X'0102');
37 INSERT INTO main.t1 VALUES(3, 'main three', X'0103');
38 INSERT INTO main.t1 VALUES(4, 'main four', X'0104');
39 INSERT INTO main.t1 VALUES(5, 'main five', X'0105');
41 INSERT INTO main.x1 VALUES(1, 'x main one', X'000101');
42 INSERT INTO main.x1 VALUES(2, 'x main two', X'000102');
43 INSERT INTO main.x1 VALUES(3, 'x main three', X'000103');
44 INSERT INTO main.x1 VALUES(4, 'x main four', X'000104');
45 INSERT INTO main.x1 VALUES(5, 'x main five', X'000105');
47 INSERT INTO temp.t1 VALUES(1, 'temp one', X'0201');
48 INSERT INTO temp.t1 VALUES(2, 'temp two', X'0202');
49 INSERT INTO temp.t1 VALUES(3, 'temp three', X'0203');
50 INSERT INTO temp.t1 VALUES(4, 'temp four', X'0204');
51 INSERT INTO temp.t1 VALUES(5, 'temp five', X'0205');
53 INSERT INTO temp.x2 VALUES(1, 'x temp one', X'000201');
54 INSERT INTO temp.x2 VALUES(2, 'x temp two', X'000202');
55 INSERT INTO temp.x2 VALUES(3, 'x temp three', X'000203');
56 INSERT INTO temp.x2 VALUES(4, 'x temp four', X'000204');
57 INSERT INTO temp.x2 VALUES(5, 'x temp five', X'000205');
59 INSERT INTO aux.t1 VALUES(1, 'aux one', X'0301');
60 INSERT INTO aux.t1 VALUES(2, 'aux two', X'0302');
61 INSERT INTO aux.t1 VALUES(3, 'aux three', X'0303');
62 INSERT INTO aux.t1 VALUES(4, 'aux four', X'0304');
63 INSERT INTO aux.t1 VALUES(5, 'aux five', X'0305');
65 INSERT INTO aux.x3 VALUES(1, 'x aux one', X'000301');
66 INSERT INTO aux.x3 VALUES(2, 'x aux two', X'000302');
67 INSERT INTO aux.x3 VALUES(3, 'x aux three', X'000303');
68 INSERT INTO aux.x3 VALUES(4, 'x aux four', X'000304');
69 INSERT INTO aux.x3 VALUES(5, 'x aux five', X'000305');
72 #-------------------------------------------------------------------------
73 # EVIDENCE-OF: R-37639-55938 This interfaces opens a handle to the BLOB
74 # located in row iRow, column zColumn, table zTable in database zDb; in
75 # other words, the same BLOB that would be selected by: SELECT zColumn
76 # FROM zDb.zTable WHERE rowid = iRow;
78 proc read_blob {zDb zTab zCol iRow} {
79 sqlite3_blob_open db $zDb $zTab $zCol $iRow 0 B
80 set nByte [sqlite3_blob_bytes $B]
81 set data [sqlite3_blob_read $B 0 $nByte]
86 do_test 1.1.1 { read_blob main t1 b 1 } "main one"
87 do_test 1.1.2 { read_blob main t1 c 1 } "\01\01"
88 do_test 1.1.3 { read_blob temp t1 b 1 } "temp one"
89 do_test 1.1.4 { read_blob temp t1 c 1 } "\02\01"
90 do_test 1.1.6 { read_blob aux t1 b 1 } "aux one"
91 do_test 1.1.7 { read_blob aux t1 c 1 } "\03\01"
93 do_test 1.2.1 { read_blob main t1 b 4 } "main four"
94 do_test 1.2.2 { read_blob main t1 c 4 } "\01\04"
95 do_test 1.2.3 { read_blob temp t1 b 4 } "temp four"
96 do_test 1.2.4 { read_blob temp t1 c 4 } "\02\04"
97 do_test 1.2.6 { read_blob aux t1 b 4 } "aux four"
98 do_test 1.2.7 { read_blob aux t1 c 4 } "\03\04"
100 do_test 1.3.1 { read_blob main x1 b 2 } "x main two"
101 do_test 1.3.2 { read_blob main x1 c 2 } "\00\01\02"
102 do_test 1.3.3 { read_blob temp x2 b 2 } "x temp two"
103 do_test 1.3.4 { read_blob temp x2 c 2 } "\00\02\02"
104 do_test 1.3.6 { read_blob aux x3 b 2 } "x aux two"
105 do_test 1.3.7 { read_blob aux x3 c 2 } "\00\03\02"
107 #-------------------------------------------------------------------------
108 # EVIDENCE-OF: R-27234-05761 Parameter zDb is not the filename that
109 # contains the database, but rather the symbolic name of the database.
110 # For attached databases, this is the name that appears after the AS
111 # keyword in the ATTACH statement. For the main database file, the
112 # database name is "main". For TEMP tables, the database name is "temp".
114 # The test cases immediately above demonstrate that the database name
115 # for the main db, for TEMP tables and for those in attached databases
116 # is correct. The following tests check that filenames cannot be
120 list [catch { sqlite3_blob_open db "test.db" t1 b 1 0 B } msg] $msg
123 list [catch { sqlite3_blob_open db "test.db2" t1 b 1 0 B } msg] $msg
126 #-------------------------------------------------------------------------
127 # EVIDENCE-OF: R-50854-53979 If the flags parameter is non-zero, then
128 # the BLOB is opened for read and write access.
130 # EVIDENCE-OF: R-03922-41160 If the flags parameter is zero, the BLOB is
131 # opened for read-only access.
133 foreach {tn iRow flags} {
141 sqlite3_blob_open db main x1 c $iRow $flags B
142 set n [sqlite3_blob_bytes $B]
143 sqlite3_blob_read $B 0 $n
144 } [binary format ccc 0 1 $iRow]
147 # Blob was opened for read-only access - writing returns an error.
149 list [catch { sqlite3_blob_write $B 0 xxx 3 } msg] $msg
150 } {1 SQLITE_READONLY}
152 do_execsql_test 3.$tn.3 {
153 SELECT c FROM x1 WHERE a=$iRow;
154 } [binary format ccc 0 1 $iRow]
156 # Blob was opened for read/write access - writing succeeds
158 list [catch { sqlite3_blob_write $B 0 xxx 3 } msg] $msg
161 do_execsql_test 3.$tn.5 {
162 SELECT c FROM x1 WHERE a=$iRow;
166 sqlite3_blob_close $B
169 #-------------------------------------------------------------------------
172 do_execsql_test 4.0 {
173 CREATE TABLE t1(x, y);
174 INSERT INTO t1 VALUES('abcd', 152);
175 INSERT INTO t1 VALUES(NULL, X'00010203');
176 INSERT INTO t1 VALUES('', 154.2);
178 CREATE TABLE t2(x PRIMARY KEY, y) WITHOUT ROWID;
179 INSERT INTO t2 VALUES(1, 'blob');
181 CREATE TABLE t3(a PRIMARY KEY, b, c, d, e, f, UNIQUE(e, f));
182 INSERT INTO t3 VALUES('aaaa', 'bbbb', 'cccc', 'dddd', 'eeee', 'ffff');
183 CREATE INDEX t3b ON t3(b);
185 CREATE TABLE p1(x PRIMARY KEY);
186 INSERT INTO p1 VALUES('abc');
188 CREATE TABLE c1(a INTEGER PRIMARY KEY, b REFERENCES p1);
189 INSERT INTO c1 VALUES(45, 'abc');
192 proc test_blob_open {tn zDb zTab zCol iRow flags errcode errmsg} {
196 if {$errcode=="SQLITE_OK"} {
199 set expected "1 $errcode"
203 catch { sqlite3_blob_open db $zDb $zTab $zCol $iRow $flags B } msg
205 do_test 4.$tn.1 { set ::res } $expected
207 # EVIDENCE-OF: R-08940-21305 Unless it returns SQLITE_MISUSE, this
208 # function sets the database connection error code and message
209 # accessible via sqlite3_errcode() and sqlite3_errmsg() and related
212 # This proc (test_blob_open) is used below to test various error and
213 # non-error conditions. But never SQLITE_MISUSE conditions. So these
214 # test cases are considered as partly verifying the requirement above.
215 # See below for a test of the SQLITE_MISUSE case.
224 # EVIDENCE-OF: R-31086-35521 On success, SQLITE_OK is returned and the
225 # new BLOB handle is stored in *ppBlob. Otherwise an error code is
226 # returned and, unless the error code is SQLITE_MISUSE, *ppBlob is set
231 } [expr {$errcode != "SQLITE_OK"}]
233 # EVIDENCE-OF: R-63421-15521 This means that, provided the API is not
234 # misused, it is always safe to call sqlite3_blob_close() on *ppBlob
235 # after this function it returns.
237 sqlite3_blob_close $B
241 # EVIDENCE-OF: R-31204-44780 Database zDb does not exist
242 test_blob_open 1 nosuchdb t1 x 1 0 SQLITE_ERROR "no such table: nosuchdb.t1"
244 # EVIDENCE-OF: R-28676-08005 Table zTable does not exist within database zDb
245 test_blob_open 2 main tt1 x 1 0 SQLITE_ERROR "no such table: main.tt1"
247 # EVIDENCE-OF: R-40134-30296 Table zTable is a WITHOUT ROWID table
248 test_blob_open 3 main t2 y 1 0 SQLITE_ERROR \
249 "cannot open table without rowid: t2"
251 # EVIDENCE-OF: R-56376-21261 Column zColumn does not exist
252 test_blob_open 4 main t1 z 2 0 SQLITE_ERROR "no such column: \"z\""
254 # EVIDENCE-OF: R-28258-23166 Row iRow is not present in the table
255 test_blob_open 5 main t1 y 6 0 SQLITE_ERROR "no such rowid: 6"
257 # EVIDENCE-OF: R-11683-62380 The specified column of row iRow contains a
258 # value that is not a TEXT or BLOB value
259 test_blob_open 6 main t1 x 2 0 SQLITE_ERROR "cannot open value of type null"
260 test_blob_open 7 main t1 y 1 0 SQLITE_ERROR "cannot open value of type integer"
261 test_blob_open 8 main t1 y 3 0 SQLITE_ERROR "cannot open value of type real"
263 # EVIDENCE-OF: R-34146-30782 Column zColumn is part of an index, PRIMARY
264 # KEY or UNIQUE constraint and the blob is being opened for read/write
267 # Test cases 8.1.* show that such columns can be opened for read-access.
268 # Tests 8.2.* show that read-write access is different. Columns "c" and "c"
269 # are not part of an index, PK or UNIQUE constraint, so they work in both
272 test_blob_open 8.1.1 main t3 a 1 0 SQLITE_OK "not an error"
273 test_blob_open 8.1.2 main t3 b 1 0 SQLITE_OK "not an error"
274 test_blob_open 8.1.3 main t3 c 1 0 SQLITE_OK "not an error"
275 test_blob_open 8.1.4 main t3 d 1 0 SQLITE_OK "not an error"
276 test_blob_open 8.1.5 main t3 e 1 0 SQLITE_OK "not an error"
277 test_blob_open 8.1.6 main t3 f 1 0 SQLITE_OK "not an error"
279 set cannot "cannot open indexed column for writing"
280 test_blob_open 8.2.1 main t3 a 1 8 SQLITE_ERROR $cannot
281 test_blob_open 8.2.2 main t3 b 1 8 SQLITE_ERROR $cannot
282 test_blob_open 8.2.3 main t3 c 1 8 SQLITE_OK "not an error"
283 test_blob_open 8.2.4 main t3 d 1 8 SQLITE_OK "not an error"
284 test_blob_open 8.2.5 main t3 e 1 8 SQLITE_ERROR $cannot
285 test_blob_open 8.2.6 main t3 f 1 8 SQLITE_ERROR $cannot
287 # EVIDENCE-OF: R-50117-55204 Foreign key constraints are enabled, column
288 # zColumn is part of a child key definition and the blob is being opened
289 # for read/write access
291 # 9.1: FK disabled, read-only access.
292 # 9.2: FK disabled, read-only access.
293 # 9.3: FK enabled, read/write access.
294 # 9.4: FK enabled, read/write access.
296 test_blob_open 9.1 main c1 b 45 0 SQLITE_OK "not an error"
297 test_blob_open 9.2 main c1 b 45 1 SQLITE_OK "not an error"
298 execsql { PRAGMA foreign_keys = ON }
299 test_blob_open 9.3 main c1 b 45 0 SQLITE_OK "not an error"
300 test_blob_open 9.4 main c1 b 45 1 SQLITE_ERROR \
301 "cannot open foreign key column for writing"
303 #-------------------------------------------------------------------------
304 # EVIDENCE-OF: R-08940-21305 Unless it returns SQLITE_MISUSE, this
305 # function sets the database connection error code and message
306 # accessible via sqlite3_errcode() and sqlite3_errmsg() and related
309 # This requirement is partially verified by the many uses of test
310 # command [test_blob_open] above. All that is left is to verify the
311 # SQLITE_MISUSE case.
313 # SQLITE_MISUSE is only returned if SQLITE_ENABLE_API_ARMOR is defined
314 # during compilation.
316 ifcapable api_armor {
317 sqlite3_blob_open db main t1 x 1 0 B
320 list [catch {sqlite3_blob_open $B main t1 x 1 0 B2} msg] $msg
323 list [sqlite3_errcode db] [sqlite3_errmsg db]
324 } {SQLITE_OK {not an error}}
325 sqlite3_blob_close $B
328 list [catch {sqlite3_blob_open db main {} x 1 0 B} msg] $msg
331 list [sqlite3_errcode db] [sqlite3_errmsg db]
332 } {SQLITE_OK {not an error}}
335 #-------------------------------------------------------------------------
336 # EVIDENCE-OF: R-50542-62589 If the row that a BLOB handle points to is
337 # modified by an UPDATE, DELETE, or by ON CONFLICT side-effects then the
338 # BLOB handle is marked as "expired". This is true if any column of the
339 # row is changed, even a column other than the one the BLOB handle is
342 # EVIDENCE-OF: R-48367-20048 Calls to sqlite3_blob_read() and
343 # sqlite3_blob_write() for an expired BLOB handle fail with a return
344 # code of SQLITE_ABORT.
346 # 11.2: read-only handle, DELETE.
347 # 11.3: read-only handle, UPDATE.
348 # 11.4: read-only handle, REPLACE.
349 # 11.5: read/write handle, DELETE.
350 # 11.6: read/write handle, UPDATE.
351 # 11.7: read/write handle, REPLACE.
353 do_execsql_test 11.1 {
354 CREATE TABLE b1(a INTEGER PRIMARY KEY, b, c UNIQUE);
355 INSERT INTO b1 VALUES(1, '1234567890', 1);
356 INSERT INTO b1 VALUES(2, '1234567890', 2);
357 INSERT INTO b1 VALUES(3, '1234567890', 3);
358 INSERT INTO b1 VALUES(4, '1234567890', 4);
359 INSERT INTO b1 VALUES(5, '1234567890', 5);
360 INSERT INTO b1 VALUES(6, '1234567890', 6);
362 CREATE TABLE b2(a INTEGER PRIMARY KEY, b, c UNIQUE);
363 INSERT INTO b2 VALUES(1, '1234567890', 1);
364 INSERT INTO b2 VALUES(2, '1234567890', 2);
365 INSERT INTO b2 VALUES(3, '1234567890', 3);
366 INSERT INTO b2 VALUES(4, '1234567890', 4);
367 INSERT INTO b2 VALUES(5, '1234567890', 5);
368 INSERT INTO b2 VALUES(6, '1234567890', 6);
372 sqlite3_blob_open db main b1 b 2 0 B
373 sqlite3_blob_read $B 0 10
376 # Deleting a different row does not invalidate the blob handle.
377 execsql { DELETE FROM b1 WHERE a = 1 }
378 sqlite3_blob_read $B 0 10
381 execsql { DELETE FROM b1 WHERE a = 2 }
382 list [catch { sqlite3_blob_read $B 0 10 } msg] $msg
385 sqlite3_blob_close $B
389 sqlite3_blob_open db main b1 b 3 0 B
390 sqlite3_blob_read $B 0 10
393 # Updating a different row
394 execsql { UPDATE b1 SET c = 42 WHERE a=4 }
395 sqlite3_blob_read $B 0 10
398 execsql { UPDATE b1 SET c = 43 WHERE a=3 }
399 list [catch { sqlite3_blob_read $B 0 10 } msg] $msg
402 sqlite3_blob_close $B
406 sqlite3_blob_open db main b1 b 6 0 B
407 sqlite3_blob_read $B 0 10
410 # Replace a different row
411 execsql { INSERT OR REPLACE INTO b1 VALUES(10, 'abcdefghij', 5) }
412 sqlite3_blob_read $B 0 10
415 execsql { INSERT OR REPLACE INTO b1 VALUES(11, 'abcdefghij', 6) }
416 list [catch { sqlite3_blob_read $B 0 10 } msg] $msg
419 sqlite3_blob_close $B
423 sqlite3_blob_open db main b2 b 2 1 B
424 sqlite3_blob_write $B 0 "abcdefghij"
427 # Deleting a different row does not invalidate the blob handle.
428 execsql { DELETE FROM b2 WHERE a = 1 }
429 sqlite3_blob_write $B 0 "ABCDEFGHIJ"
432 execsql { DELETE FROM b2 WHERE a = 2 }
433 list [catch { sqlite3_blob_write $B 0 "0987654321" } msg] $msg
436 sqlite3_blob_close $B
440 sqlite3_blob_open db main b2 b 3 1 B
441 sqlite3_blob_write $B 0 "abcdefghij"
444 # Updating a different row
445 execsql { UPDATE b2 SET c = 42 WHERE a=4 }
446 sqlite3_blob_write $B 0 "ABCDEFGHIJ"
449 execsql { UPDATE b2 SET c = 43 WHERE a=3 }
450 list [catch { sqlite3_blob_write $B 0 "0987654321" } msg] $msg
453 sqlite3_blob_close $B
457 sqlite3_blob_open db main b2 b 6 1 B
458 sqlite3_blob_write $B 0 "abcdefghij"
461 # Replace a different row
462 execsql { INSERT OR REPLACE INTO b2 VALUES(10, 'abcdefghij', 5) }
463 sqlite3_blob_write $B 0 "ABCDEFGHIJ"
466 execsql { INSERT OR REPLACE INTO b2 VALUES(11, 'abcdefghij', 6) }
467 list [catch { sqlite3_blob_write $B 0 "0987654321" } msg] $msg
470 sqlite3_blob_close $B
473 #-------------------------------------------------------------------------
474 # EVIDENCE-OF: R-45408-40694 Changes written into a BLOB prior to the
475 # BLOB expiring are not rolled back by the expiration of the BLOB. Such
476 # changes will eventually commit if the transaction continues to
479 do_execsql_test 12.1 {
480 CREATE TABLE b3(x INTEGER PRIMARY KEY, y TEXT, z INTEGER);
481 INSERT INTO b3 VALUES(22, '..........', NULL);
484 sqlite3_blob_open db main b3 y 22 1 B
485 sqlite3_blob_write $B 0 "xxxxx" 5
487 do_execsql_test 12.3 {
488 UPDATE b3 SET z = 'not null';
491 list [catch {sqlite3_blob_write $B 5 "xxxxx" 5} msg] $msg
493 do_execsql_test 12.5 {
495 } {22 xxxxx..... {not null}}
497 sqlite3_blob_close $B
499 do_execsql_test 12.6 {
501 } {22 xxxxx..... {not null}}
503 #-------------------------------------------------------------------------
504 # EVIDENCE-OF: R-58813-55036 The sqlite3_bind_zeroblob() and
505 # sqlite3_result_zeroblob() interfaces and the built-in zeroblob SQL
506 # function may be used to create a zero-filled blob to read or write
507 # using the incremental-blob interface.
509 do_execsql_test 13.1 {
510 CREATE TABLE c2(i INTEGER PRIMARY KEY, j);
511 INSERT INTO c2 VALUES(10, zeroblob(24));
515 set stmt [sqlite3_prepare_v2 db "INSERT INTO c2 VALUES(11, ?)" -1]
516 sqlite3_bind_zeroblob $stmt 1 45
518 sqlite3_finalize $stmt
521 # The blobs can be read:
524 sqlite3_blob_open db main c2 j 10 1 B
525 sqlite3_blob_open db main c2 j 11 1 B2
526 list [sqlite3_blob_bytes $B] [sqlite3_blob_bytes $B2]
529 sqlite3_blob_read $B 0 24
530 } [string repeat [binary format c 0] 24]
532 sqlite3_blob_read $B2 0 45
533 } [string repeat [binary format c 0] 45]
538 sqlite3_blob_write $B 0 [string repeat [binary format c 1] 24]
541 sqlite3_blob_write $B2 0 [string repeat [binary format c 1] 45]
544 sqlite3_blob_close $B
545 sqlite3_blob_close $B2
546 execsql { SELECT j FROM c2 }
548 [string repeat [binary format c 1] 24] \
549 [string repeat [binary format c 1] 45] \