xref: /sqlite-3.40.0/test/e_blobopen.test (revision a32536b4)
1b391b944Sdan# 2014 October 30
2b391b944Sdan#
3b391b944Sdan# The author disclaims copyright to this source code.  In place of
4b391b944Sdan# a legal notice, here is a blessing:
5b391b944Sdan#
6b391b944Sdan#    May you do good and not evil.
7b391b944Sdan#    May you find forgiveness for yourself and forgive others.
8b391b944Sdan#    May you share freely, never taking more than you give.
9b391b944Sdan#
10b391b944Sdan#***********************************************************************
11b391b944Sdan#
12b391b944Sdan
13b391b944Sdanset testdir [file dirname $argv0]
14b391b944Sdansource $testdir/tester.tcl
15b391b944Sdanset testprefix e_blobopen
16b391b944Sdan
17*a32536b4Sdanifcapable !incrblob {
18*a32536b4Sdan  finish_test
19*a32536b4Sdan  return
20*a32536b4Sdan}
21*a32536b4Sdan
22b391b944Sdanforcedelete test.db2
23b391b944Sdan
24b391b944Sdando_execsql_test 1.0 {
25b391b944Sdan  ATTACH 'test.db2' AS aux;
26b391b944Sdan
27b391b944Sdan  CREATE TABLE main.t1(a INTEGER PRIMARY KEY, b TEXT, c BLOB);
28b391b944Sdan  CREATE TEMP TABLE t1(a INTEGER PRIMARY KEY, b TEXT, c BLOB);
29b391b944Sdan  CREATE TABLE aux.t1(a INTEGER PRIMARY KEY, b TEXT, c BLOB);
30b391b944Sdan
31b391b944Sdan  CREATE TABLE main.x1(a INTEGER PRIMARY KEY, b TEXT, c BLOB);
32b391b944Sdan  CREATE TEMP TABLE x2(a INTEGER PRIMARY KEY, b TEXT, c BLOB);
33b391b944Sdan  CREATE TABLE aux.x3(a INTEGER PRIMARY KEY, b TEXT, c BLOB);
34b391b944Sdan
35b391b944Sdan  INSERT INTO main.t1 VALUES(1, 'main one', X'0101');
36b391b944Sdan  INSERT INTO main.t1 VALUES(2, 'main two', X'0102');
37b391b944Sdan  INSERT INTO main.t1 VALUES(3, 'main three', X'0103');
38b391b944Sdan  INSERT INTO main.t1 VALUES(4, 'main four', X'0104');
39b391b944Sdan  INSERT INTO main.t1 VALUES(5, 'main five', X'0105');
40b391b944Sdan
41b391b944Sdan  INSERT INTO main.x1 VALUES(1, 'x main one', X'000101');
42b391b944Sdan  INSERT INTO main.x1 VALUES(2, 'x main two', X'000102');
43b391b944Sdan  INSERT INTO main.x1 VALUES(3, 'x main three', X'000103');
44b391b944Sdan  INSERT INTO main.x1 VALUES(4, 'x main four', X'000104');
45b391b944Sdan  INSERT INTO main.x1 VALUES(5, 'x main five', X'000105');
46b391b944Sdan
47b391b944Sdan  INSERT INTO temp.t1 VALUES(1, 'temp one', X'0201');
48b391b944Sdan  INSERT INTO temp.t1 VALUES(2, 'temp two', X'0202');
49b391b944Sdan  INSERT INTO temp.t1 VALUES(3, 'temp three', X'0203');
50b391b944Sdan  INSERT INTO temp.t1 VALUES(4, 'temp four', X'0204');
51b391b944Sdan  INSERT INTO temp.t1 VALUES(5, 'temp five', X'0205');
52b391b944Sdan
53b391b944Sdan  INSERT INTO temp.x2 VALUES(1, 'x temp one', X'000201');
54b391b944Sdan  INSERT INTO temp.x2 VALUES(2, 'x temp two', X'000202');
55b391b944Sdan  INSERT INTO temp.x2 VALUES(3, 'x temp three', X'000203');
56b391b944Sdan  INSERT INTO temp.x2 VALUES(4, 'x temp four', X'000204');
57b391b944Sdan  INSERT INTO temp.x2 VALUES(5, 'x temp five', X'000205');
58b391b944Sdan
59b391b944Sdan  INSERT INTO aux.t1 VALUES(1, 'aux one', X'0301');
60b391b944Sdan  INSERT INTO aux.t1 VALUES(2, 'aux two', X'0302');
61b391b944Sdan  INSERT INTO aux.t1 VALUES(3, 'aux three', X'0303');
62b391b944Sdan  INSERT INTO aux.t1 VALUES(4, 'aux four', X'0304');
63b391b944Sdan  INSERT INTO aux.t1 VALUES(5, 'aux five', X'0305');
64b391b944Sdan
65b391b944Sdan  INSERT INTO aux.x3 VALUES(1, 'x aux one', X'000301');
66b391b944Sdan  INSERT INTO aux.x3 VALUES(2, 'x aux two', X'000302');
67b391b944Sdan  INSERT INTO aux.x3 VALUES(3, 'x aux three', X'000303');
68b391b944Sdan  INSERT INTO aux.x3 VALUES(4, 'x aux four', X'000304');
69b391b944Sdan  INSERT INTO aux.x3 VALUES(5, 'x aux five', X'000305');
70b391b944Sdan}
71b391b944Sdan
72b391b944Sdan#-------------------------------------------------------------------------
73b391b944Sdan# EVIDENCE-OF: R-37639-55938 This interfaces opens a handle to the BLOB
74b391b944Sdan# located in row iRow, column zColumn, table zTable in database zDb; in
75b391b944Sdan# other words, the same BLOB that would be selected by: SELECT zColumn
76b391b944Sdan# FROM zDb.zTable WHERE rowid = iRow;
77b391b944Sdan#
78b391b944Sdanproc read_blob {zDb zTab zCol iRow} {
79b391b944Sdan  sqlite3_blob_open db $zDb $zTab $zCol $iRow 0 B
80b391b944Sdan  set nByte [sqlite3_blob_bytes $B]
81b391b944Sdan  set data [sqlite3_blob_read $B 0 $nByte]
82b391b944Sdan  sqlite3_blob_close $B
83b391b944Sdan  return $data
84b391b944Sdan}
85b391b944Sdan
86b391b944Sdando_test 1.1.1 { read_blob main t1 b 1 } "main one"
87b391b944Sdando_test 1.1.2 { read_blob main t1 c 1 } "\01\01"
88b391b944Sdando_test 1.1.3 { read_blob temp t1 b 1 } "temp one"
89b391b944Sdando_test 1.1.4 { read_blob temp t1 c 1 } "\02\01"
90b391b944Sdando_test 1.1.6 { read_blob aux  t1 b 1 } "aux one"
91b391b944Sdando_test 1.1.7 { read_blob aux  t1 c 1 } "\03\01"
92b391b944Sdan
93b391b944Sdando_test 1.2.1 { read_blob main t1 b 4 } "main four"
94b391b944Sdando_test 1.2.2 { read_blob main t1 c 4 } "\01\04"
95b391b944Sdando_test 1.2.3 { read_blob temp t1 b 4 } "temp four"
96b391b944Sdando_test 1.2.4 { read_blob temp t1 c 4 } "\02\04"
97b391b944Sdando_test 1.2.6 { read_blob aux  t1 b 4 } "aux four"
98b391b944Sdando_test 1.2.7 { read_blob aux  t1 c 4 } "\03\04"
99b391b944Sdan
100b391b944Sdando_test 1.3.1 { read_blob main x1 b 2 } "x main two"
101b391b944Sdando_test 1.3.2 { read_blob main x1 c 2 } "\00\01\02"
102b391b944Sdando_test 1.3.3 { read_blob temp x2 b 2 } "x temp two"
103b391b944Sdando_test 1.3.4 { read_blob temp x2 c 2 } "\00\02\02"
104b391b944Sdando_test 1.3.6 { read_blob aux  x3 b 2 } "x aux two"
105b391b944Sdando_test 1.3.7 { read_blob aux  x3 c 2 } "\00\03\02"
106b391b944Sdan
107b391b944Sdan#-------------------------------------------------------------------------
108b391b944Sdan# EVIDENCE-OF: R-27234-05761 Parameter zDb is not the filename that
109b391b944Sdan# contains the database, but rather the symbolic name of the database.
110b391b944Sdan# For attached databases, this is the name that appears after the AS
111b391b944Sdan# keyword in the ATTACH statement. For the main database file, the
112b391b944Sdan# database name is "main". For TEMP tables, the database name is "temp".
113b391b944Sdan#
114b391b944Sdan#   The test cases immediately above demonstrate that the database name
115b391b944Sdan#   for the main db, for TEMP tables and for those in attached databases
116b391b944Sdan#   is correct. The following tests check that filenames cannot be
117b391b944Sdan#   used as well.
118b391b944Sdan#
119b391b944Sdando_test 2.1 {
120b391b944Sdan  list [catch { sqlite3_blob_open db "test.db" t1 b 1 0 B } msg] $msg
121b391b944Sdan} {1 SQLITE_ERROR}
122b391b944Sdando_test 2.2 {
123b391b944Sdan  list [catch { sqlite3_blob_open db "test.db2" t1 b 1 0 B } msg] $msg
124b391b944Sdan} {1 SQLITE_ERROR}
125b391b944Sdan
126b391b944Sdan#-------------------------------------------------------------------------
127b391b944Sdan# EVIDENCE-OF: R-50854-53979 If the flags parameter is non-zero, then
128b391b944Sdan# the BLOB is opened for read and write access.
129b391b944Sdan#
130b391b944Sdan# EVIDENCE-OF: R-03922-41160 If the flags parameter is zero, the BLOB is
131b391b944Sdan# opened for read-only access.
132b391b944Sdan#
133b391b944Sdanforeach {tn iRow flags} {
134b391b944Sdan  1 1   0
135b391b944Sdan  2 2   1
136b391b944Sdan  3 3  -1
137b391b944Sdan  4 4   2147483647
138b391b944Sdan  5 5  -2147483648
139b391b944Sdan} {
140b391b944Sdan  do_test 3.$tn.1 {
141b391b944Sdan    sqlite3_blob_open db main x1 c $iRow $flags B
142b391b944Sdan    set n [sqlite3_blob_bytes $B]
143b391b944Sdan    sqlite3_blob_read $B 0 $n
144b391b944Sdan  } [binary format ccc 0 1 $iRow]
145b391b944Sdan
146b391b944Sdan  if {$flags==0} {
147b391b944Sdan    # Blob was opened for read-only access - writing returns an error.
148b391b944Sdan    do_test 3.$tn.2 {
149b391b944Sdan      list [catch { sqlite3_blob_write $B 0 xxx 3 } msg] $msg
150b391b944Sdan    } {1 SQLITE_READONLY}
151b391b944Sdan
152b391b944Sdan    do_execsql_test 3.$tn.3 {
153b391b944Sdan      SELECT c FROM x1 WHERE a=$iRow;
154b391b944Sdan    } [binary format ccc 0 1 $iRow]
155b391b944Sdan  } else {
156b391b944Sdan    # Blob was opened for read/write access - writing succeeds
157b391b944Sdan    do_test 3.$tn.4 {
158b391b944Sdan      list [catch { sqlite3_blob_write $B 0 xxx 3 } msg] $msg
159b391b944Sdan    } {0 {}}
160b391b944Sdan
161b391b944Sdan    do_execsql_test 3.$tn.5 {
162b391b944Sdan      SELECT c FROM x1 WHERE a=$iRow;
163b391b944Sdan    } {xxx}
164b391b944Sdan  }
165b391b944Sdan
166b391b944Sdan  sqlite3_blob_close $B
167b391b944Sdan}
168b391b944Sdan
169b391b944Sdan#-------------------------------------------------------------------------
170b391b944Sdan#
171b391b944Sdanreset_db
172b391b944Sdando_execsql_test 4.0 {
173b391b944Sdan  CREATE TABLE t1(x, y);
174b391b944Sdan  INSERT INTO t1 VALUES('abcd', 152);
175b391b944Sdan  INSERT INTO t1 VALUES(NULL, X'00010203');
176b391b944Sdan  INSERT INTO t1 VALUES('', 154.2);
177b391b944Sdan
178b391b944Sdan  CREATE TABLE t2(x PRIMARY KEY, y) WITHOUT ROWID;
179b391b944Sdan  INSERT INTO t2 VALUES(1, 'blob');
180b391b944Sdan
181b391b944Sdan  CREATE TABLE t3(a PRIMARY KEY, b, c, d, e, f, UNIQUE(e, f));
182b391b944Sdan  INSERT INTO t3 VALUES('aaaa', 'bbbb', 'cccc', 'dddd', 'eeee', 'ffff');
183b391b944Sdan  CREATE INDEX t3b ON t3(b);
184b391b944Sdan
185b391b944Sdan  CREATE TABLE p1(x PRIMARY KEY);
186b391b944Sdan  INSERT INTO p1 VALUES('abc');
187b391b944Sdan
188b391b944Sdan  CREATE TABLE c1(a INTEGER PRIMARY KEY, b REFERENCES p1);
189b391b944Sdan  INSERT INTO c1 VALUES(45, 'abc');
190b391b944Sdan}
191b391b944Sdan
192b391b944Sdanproc test_blob_open {tn zDb zTab zCol iRow flags    errcode errmsg} {
193b391b944Sdan  global B
194b391b944Sdan  set B "0x1234"
195b391b944Sdan
196b391b944Sdan  if {$errcode=="SQLITE_OK"} {
197b391b944Sdan    set expected "0 {}"
198b391b944Sdan  } else {
199b391b944Sdan    set expected "1 $errcode"
200b391b944Sdan  }
201b391b944Sdan
202b391b944Sdan  set ::res [list [
203b391b944Sdan    catch { sqlite3_blob_open db $zDb $zTab $zCol $iRow $flags B } msg
204b391b944Sdan  ] $msg]
205b391b944Sdan  do_test 4.$tn.1 { set ::res } $expected
206b391b944Sdan
207b391b944Sdan  # EVIDENCE-OF: R-08940-21305 Unless it returns SQLITE_MISUSE, this
208b391b944Sdan  # function sets the database connection error code and message
209b391b944Sdan  # accessible via sqlite3_errcode() and sqlite3_errmsg() and related
210b391b944Sdan  # functions.
211b391b944Sdan  #
212b391b944Sdan  #   This proc (test_blob_open) is used below to test various error and
213b391b944Sdan  #   non-error conditions. But never SQLITE_MISUSE conditions. So these
214b391b944Sdan  #   test cases are considered as partly verifying the requirement above.
215b391b944Sdan  #   See below for a test of the SQLITE_MISUSE case.
216b391b944Sdan  #
217b391b944Sdan  do_test 4.$tn.2 {
218b391b944Sdan    sqlite3_errcode db
219b391b944Sdan  } $errcode
220b391b944Sdan  do_test 4.$tn.3 {
221b391b944Sdan    sqlite3_errmsg db
222b391b944Sdan  } $errmsg
223b391b944Sdan
224b391b944Sdan  # EVIDENCE-OF: R-31086-35521 On success, SQLITE_OK is returned and the
225b391b944Sdan  # new BLOB handle is stored in *ppBlob. Otherwise an error code is
226b391b944Sdan  # returned and, unless the error code is SQLITE_MISUSE, *ppBlob is set
227b391b944Sdan  # to NULL.
228b391b944Sdan  #
229b391b944Sdan  do_test 4.$tn.4 {
230b391b944Sdan    expr {$B == "0"}
231b391b944Sdan  } [expr {$errcode != "SQLITE_OK"}]
232b391b944Sdan
233b391b944Sdan  # EVIDENCE-OF: R-63421-15521 This means that, provided the API is not
234b391b944Sdan  # misused, it is always safe to call sqlite3_blob_close() on *ppBlob
235b391b944Sdan  # after this function it returns.
236b391b944Sdan  do_test 4.$tn.5 {
237b391b944Sdan    sqlite3_blob_close $B
238b391b944Sdan  } {}
239b391b944Sdan}
240b391b944Sdan
241b391b944Sdan# EVIDENCE-OF: R-31204-44780 Database zDb does not exist
242b391b944Sdantest_blob_open 1 nosuchdb t1 x 1 0 SQLITE_ERROR "no such table: nosuchdb.t1"
243b391b944Sdan
244b391b944Sdan# EVIDENCE-OF: R-28676-08005 Table zTable does not exist within database zDb
245b391b944Sdantest_blob_open 2 main tt1 x 1 0    SQLITE_ERROR "no such table: main.tt1"
246b391b944Sdan
247b391b944Sdan# EVIDENCE-OF: R-40134-30296 Table zTable is a WITHOUT ROWID table
248b391b944Sdantest_blob_open 3 main t2 y 1 0     SQLITE_ERROR \
249b391b944Sdan    "cannot open table without rowid: t2"
250b391b944Sdan
251b391b944Sdan# EVIDENCE-OF: R-56376-21261 Column zColumn does not exist
252b391b944Sdantest_blob_open 4 main t1 z 2 0     SQLITE_ERROR "no such column: \"z\""
253b391b944Sdan
254b391b944Sdan# EVIDENCE-OF: R-28258-23166 Row iRow is not present in the table
255b391b944Sdantest_blob_open 5 main t1 y 6 0     SQLITE_ERROR "no such rowid: 6"
256b391b944Sdan
257b391b944Sdan# EVIDENCE-OF: R-11683-62380 The specified column of row iRow contains a
258b391b944Sdan# value that is not a TEXT or BLOB value
259b391b944Sdantest_blob_open 6 main t1 x 2 0 SQLITE_ERROR "cannot open value of type null"
260b391b944Sdantest_blob_open 7 main t1 y 1 0 SQLITE_ERROR "cannot open value of type integer"
261b391b944Sdantest_blob_open 8 main t1 y 3 0 SQLITE_ERROR "cannot open value of type real"
262b391b944Sdan
263b391b944Sdan# EVIDENCE-OF: R-34146-30782 Column zColumn is part of an index, PRIMARY
264b391b944Sdan# KEY or UNIQUE constraint and the blob is being opened for read/write
265b391b944Sdan# access
266b391b944Sdan#
267b391b944Sdan# Test cases 8.1.* show that such columns can be opened for read-access.
268b391b944Sdan# Tests 8.2.* show that read-write access is different. Columns "c" and "c"
269b391b944Sdan# are not part of an index, PK or UNIQUE constraint, so they work in both
270b391b944Sdan# cases.
271b391b944Sdan#
272b391b944Sdantest_blob_open 8.1.1 main t3 a 1 0 SQLITE_OK "not an error"
273b391b944Sdantest_blob_open 8.1.2 main t3 b 1 0 SQLITE_OK "not an error"
274b391b944Sdantest_blob_open 8.1.3 main t3 c 1 0 SQLITE_OK "not an error"
275b391b944Sdantest_blob_open 8.1.4 main t3 d 1 0 SQLITE_OK "not an error"
276b391b944Sdantest_blob_open 8.1.5 main t3 e 1 0 SQLITE_OK "not an error"
277b391b944Sdantest_blob_open 8.1.6 main t3 f 1 0 SQLITE_OK "not an error"
278b391b944Sdan
279b391b944Sdanset cannot "cannot open indexed column for writing"
280b391b944Sdantest_blob_open 8.2.1 main t3 a 1 8 SQLITE_ERROR $cannot
281b391b944Sdantest_blob_open 8.2.2 main t3 b 1 8 SQLITE_ERROR $cannot
282b391b944Sdantest_blob_open 8.2.3 main t3 c 1 8 SQLITE_OK "not an error"
283b391b944Sdantest_blob_open 8.2.4 main t3 d 1 8 SQLITE_OK "not an error"
284b391b944Sdantest_blob_open 8.2.5 main t3 e 1 8 SQLITE_ERROR $cannot
285b391b944Sdantest_blob_open 8.2.6 main t3 f 1 8 SQLITE_ERROR $cannot
286b391b944Sdan
287b391b944Sdan# EVIDENCE-OF: R-50117-55204 Foreign key constraints are enabled, column
288b391b944Sdan# zColumn is part of a child key definition and the blob is being opened
289b391b944Sdan# for read/write access
290b391b944Sdan#
291b391b944Sdan#   9.1: FK disabled, read-only access.
292b391b944Sdan#   9.2: FK disabled, read-only access.
293b391b944Sdan#   9.3: FK enabled, read/write access.
294b391b944Sdan#   9.4: FK enabled, read/write access.
295b391b944Sdan#
296b391b944Sdantest_blob_open 9.1 main c1 b 45 0 SQLITE_OK "not an error"
297b391b944Sdantest_blob_open 9.2 main c1 b 45 1 SQLITE_OK "not an error"
298b391b944Sdanexecsql { PRAGMA foreign_keys = ON }
299b391b944Sdantest_blob_open 9.3 main c1 b 45 0 SQLITE_OK "not an error"
300b391b944Sdantest_blob_open 9.4 main c1 b 45 1 SQLITE_ERROR \
301b391b944Sdan        "cannot open foreign key column for writing"
302b391b944Sdan
303b391b944Sdan#-------------------------------------------------------------------------
304b391b944Sdan# EVIDENCE-OF: R-08940-21305 Unless it returns SQLITE_MISUSE, this
305b391b944Sdan# function sets the database connection error code and message
306b391b944Sdan# accessible via sqlite3_errcode() and sqlite3_errmsg() and related
307b391b944Sdan# functions.
308b391b944Sdan#
309b391b944Sdan#   This requirement is partially verified by the many uses of test
310b391b944Sdan#   command [test_blob_open] above. All that is left is to verify the
311b391b944Sdan#   SQLITE_MISUSE case.
312b391b944Sdan#
313b391b944Sdan#   SQLITE_MISUSE is only returned if SQLITE_ENABLE_API_ARMOR is defined
314b391b944Sdan#   during compilation.
315b391b944Sdan#
316b391b944Sdanifcapable api_armor {
317b391b944Sdan  sqlite3_blob_open db main t1 x 1 0 B
318b391b944Sdan
319b391b944Sdan  do_test 10.1.1 {
320b391b944Sdan    list [catch {sqlite3_blob_open $B main t1 x 1 0 B2} msg] $msg
321b391b944Sdan  } {1 SQLITE_MISUSE}
322b391b944Sdan  do_test 10.1.2 {
323b391b944Sdan    list [sqlite3_errcode db] [sqlite3_errmsg db]
324b391b944Sdan  } {SQLITE_OK {not an error}}
325b391b944Sdan  sqlite3_blob_close $B
326b391b944Sdan
327b391b944Sdan  do_test 10.2.1 {
328b391b944Sdan    list [catch {sqlite3_blob_open db main {} x 1 0 B} msg] $msg
329b391b944Sdan  } {1 SQLITE_MISUSE}
330b391b944Sdan  do_test 10.2.2 {
331b391b944Sdan    list [sqlite3_errcode db] [sqlite3_errmsg db]
332b391b944Sdan  } {SQLITE_OK {not an error}}
333b391b944Sdan}
334b391b944Sdan
335b391b944Sdan#-------------------------------------------------------------------------
336b391b944Sdan# EVIDENCE-OF: R-50542-62589 If the row that a BLOB handle points to is
337b391b944Sdan# modified by an UPDATE, DELETE, or by ON CONFLICT side-effects then the
338b391b944Sdan# BLOB handle is marked as "expired". This is true if any column of the
339b391b944Sdan# row is changed, even a column other than the one the BLOB handle is
340b391b944Sdan# open on.
341b391b944Sdan#
342b391b944Sdan# EVIDENCE-OF: R-48367-20048 Calls to sqlite3_blob_read() and
343b391b944Sdan# sqlite3_blob_write() for an expired BLOB handle fail with a return
344b391b944Sdan# code of SQLITE_ABORT.
345b391b944Sdan#
346b391b944Sdan#   11.2: read-only handle, DELETE.
347b391b944Sdan#   11.3: read-only handle, UPDATE.
348b391b944Sdan#   11.4: read-only handle, REPLACE.
349b391b944Sdan#   11.5: read/write handle, DELETE.
350b391b944Sdan#   11.6: read/write handle, UPDATE.
351b391b944Sdan#   11.7: read/write handle, REPLACE.
352b391b944Sdan#
353b391b944Sdando_execsql_test 11.1 {
354b391b944Sdan  CREATE TABLE b1(a INTEGER PRIMARY KEY, b, c UNIQUE);
355b391b944Sdan  INSERT INTO b1 VALUES(1, '1234567890', 1);
356b391b944Sdan  INSERT INTO b1 VALUES(2, '1234567890', 2);
357b391b944Sdan  INSERT INTO b1 VALUES(3, '1234567890', 3);
358b391b944Sdan  INSERT INTO b1 VALUES(4, '1234567890', 4);
359b391b944Sdan  INSERT INTO b1 VALUES(5, '1234567890', 5);
360b391b944Sdan  INSERT INTO b1 VALUES(6, '1234567890', 6);
361b391b944Sdan
362b391b944Sdan  CREATE TABLE b2(a INTEGER PRIMARY KEY, b, c UNIQUE);
363b391b944Sdan  INSERT INTO b2 VALUES(1, '1234567890', 1);
364b391b944Sdan  INSERT INTO b2 VALUES(2, '1234567890', 2);
365b391b944Sdan  INSERT INTO b2 VALUES(3, '1234567890', 3);
366b391b944Sdan  INSERT INTO b2 VALUES(4, '1234567890', 4);
367b391b944Sdan  INSERT INTO b2 VALUES(5, '1234567890', 5);
368b391b944Sdan  INSERT INTO b2 VALUES(6, '1234567890', 6);
369b391b944Sdan}
370b391b944Sdan
371b391b944Sdando_test 11.2.1 {
372b391b944Sdan  sqlite3_blob_open db main b1 b 2 0 B
373b391b944Sdan  sqlite3_blob_read $B 0 10
374b391b944Sdan} {1234567890}
375b391b944Sdando_test 11.2.2 {
376b391b944Sdan  # Deleting a different row does not invalidate the blob handle.
377b391b944Sdan  execsql { DELETE FROM b1 WHERE a = 1 }
378b391b944Sdan  sqlite3_blob_read $B 0 10
379b391b944Sdan} {1234567890}
380b391b944Sdando_test 11.2.3 {
381b391b944Sdan  execsql { DELETE FROM b1 WHERE a = 2 }
382b391b944Sdan  list [catch { sqlite3_blob_read $B 0 10 } msg] $msg
383b391b944Sdan} {1 SQLITE_ABORT}
384b391b944Sdando_test 11.2.4 {
385b391b944Sdan  sqlite3_blob_close $B
386b391b944Sdan} {}
387b391b944Sdan
388b391b944Sdando_test 11.3.1 {
389b391b944Sdan  sqlite3_blob_open db main b1 b 3 0 B
390b391b944Sdan  sqlite3_blob_read $B 0 10
391b391b944Sdan} {1234567890}
392b391b944Sdando_test 11.3.2 {
393b391b944Sdan  # Updating a different row
394b391b944Sdan  execsql { UPDATE b1 SET c = 42 WHERE a=4 }
395b391b944Sdan  sqlite3_blob_read $B 0 10
396b391b944Sdan} {1234567890}
397b391b944Sdando_test 11.3.3 {
398b391b944Sdan  execsql { UPDATE b1 SET c = 43 WHERE a=3 }
399b391b944Sdan  list [catch { sqlite3_blob_read $B 0 10 } msg] $msg
400b391b944Sdan} {1 SQLITE_ABORT}
401b391b944Sdando_test 11.3.4 {
402b391b944Sdan  sqlite3_blob_close $B
403b391b944Sdan} {}
404b391b944Sdan
405b391b944Sdando_test 11.4.1 {
406b391b944Sdan  sqlite3_blob_open db main b1 b 6 0 B
407b391b944Sdan  sqlite3_blob_read $B 0 10
408b391b944Sdan} {1234567890}
409b391b944Sdando_test 11.4.2 {
410b391b944Sdan  # Replace a different row
411b391b944Sdan  execsql { INSERT OR REPLACE INTO b1 VALUES(10, 'abcdefghij', 5) }
412b391b944Sdan  sqlite3_blob_read $B 0 10
413b391b944Sdan} {1234567890}
414b391b944Sdando_test 11.4.3 {
415b391b944Sdan  execsql { INSERT OR REPLACE INTO b1 VALUES(11, 'abcdefghij', 6) }
416b391b944Sdan  list [catch { sqlite3_blob_read $B 0 10 } msg] $msg
417b391b944Sdan} {1 SQLITE_ABORT}
418b391b944Sdando_test 11.4.4 {
419b391b944Sdan  sqlite3_blob_close $B
420b391b944Sdan} {}
421b391b944Sdan
422b391b944Sdando_test 11.4.1 {
423b391b944Sdan  sqlite3_blob_open db main b2 b 2 1 B
424b391b944Sdan  sqlite3_blob_write $B 0 "abcdefghij"
425b391b944Sdan} {}
426b391b944Sdando_test 11.4.2 {
427b391b944Sdan  # Deleting a different row does not invalidate the blob handle.
428b391b944Sdan  execsql { DELETE FROM b2 WHERE a = 1 }
429b391b944Sdan  sqlite3_blob_write $B 0 "ABCDEFGHIJ"
430b391b944Sdan} {}
431b391b944Sdando_test 11.4.3 {
432b391b944Sdan  execsql { DELETE FROM b2 WHERE a = 2 }
433b391b944Sdan  list [catch { sqlite3_blob_write $B 0 "0987654321" } msg] $msg
434b391b944Sdan} {1 SQLITE_ABORT}
435b391b944Sdando_test 11.4.4 {
436b391b944Sdan  sqlite3_blob_close $B
437b391b944Sdan} {}
438b391b944Sdan
439b391b944Sdando_test 11.5.1 {
440b391b944Sdan  sqlite3_blob_open db main b2 b 3 1 B
441b391b944Sdan  sqlite3_blob_write $B 0 "abcdefghij"
442b391b944Sdan} {}
443b391b944Sdando_test 11.5.2 {
444b391b944Sdan  # Updating a different row
445b391b944Sdan  execsql { UPDATE b2 SET c = 42 WHERE a=4 }
446b391b944Sdan  sqlite3_blob_write $B 0 "ABCDEFGHIJ"
447b391b944Sdan} {}
448b391b944Sdando_test 11.5.3 {
449b391b944Sdan  execsql { UPDATE b2 SET c = 43 WHERE a=3 }
450b391b944Sdan  list [catch { sqlite3_blob_write $B 0 "0987654321" } msg] $msg
451b391b944Sdan} {1 SQLITE_ABORT}
452b391b944Sdando_test 11.5.4 {
453b391b944Sdan  sqlite3_blob_close $B
454b391b944Sdan} {}
455b391b944Sdan
456b391b944Sdando_test 11.6.1 {
457b391b944Sdan  sqlite3_blob_open db main b2 b 6 1 B
458b391b944Sdan  sqlite3_blob_write $B 0 "abcdefghij"
459b391b944Sdan} {}
460b391b944Sdando_test 11.6.2 {
461b391b944Sdan  # Replace a different row
462b391b944Sdan  execsql { INSERT OR REPLACE INTO b2 VALUES(10, 'abcdefghij', 5) }
463b391b944Sdan  sqlite3_blob_write $B 0 "ABCDEFGHIJ"
464b391b944Sdan} {}
465b391b944Sdando_test 11.6.3 {
466b391b944Sdan  execsql { INSERT OR REPLACE INTO b2 VALUES(11, 'abcdefghij', 6) }
467b391b944Sdan  list [catch { sqlite3_blob_write $B 0 "0987654321" } msg] $msg
468b391b944Sdan} {1 SQLITE_ABORT}
469b391b944Sdando_test 11.6.4 {
470b391b944Sdan  sqlite3_blob_close $B
471b391b944Sdan} {}
472b391b944Sdan
473b391b944Sdan#-------------------------------------------------------------------------
474b391b944Sdan# EVIDENCE-OF: R-45408-40694 Changes written into a BLOB prior to the
475b391b944Sdan# BLOB expiring are not rolled back by the expiration of the BLOB. Such
476b391b944Sdan# changes will eventually commit if the transaction continues to
477b391b944Sdan# completion.
478b391b944Sdan#
479b391b944Sdando_execsql_test 12.1 {
480b391b944Sdan  CREATE TABLE b3(x INTEGER PRIMARY KEY, y TEXT, z INTEGER);
481b391b944Sdan  INSERT INTO b3 VALUES(22, '..........', NULL);
482b391b944Sdan}
483b391b944Sdando_test 12.2 {
484b391b944Sdan  sqlite3_blob_open db main b3 y 22 1 B
485b391b944Sdan  sqlite3_blob_write $B 0 "xxxxx" 5
486b391b944Sdan} {}
487b391b944Sdando_execsql_test 12.3 {
488b391b944Sdan  UPDATE b3 SET z = 'not null';
489b391b944Sdan}
490b391b944Sdando_test 12.4 {
491b391b944Sdan  list [catch {sqlite3_blob_write $B 5 "xxxxx" 5} msg] $msg
492b391b944Sdan} {1 SQLITE_ABORT}
493b391b944Sdando_execsql_test 12.5 {
494b391b944Sdan  SELECT * FROM b3;
495b391b944Sdan} {22 xxxxx..... {not null}}
496b391b944Sdando_test 12.5 {
497b391b944Sdan  sqlite3_blob_close $B
498b391b944Sdan} {}
499b391b944Sdando_execsql_test 12.6 {
500b391b944Sdan  SELECT * FROM b3;
501b391b944Sdan} {22 xxxxx..... {not null}}
502b391b944Sdan
503b391b944Sdan#-------------------------------------------------------------------------
504b391b944Sdan# EVIDENCE-OF: R-58813-55036 The sqlite3_bind_zeroblob() and
505b391b944Sdan# sqlite3_result_zeroblob() interfaces and the built-in zeroblob SQL
506b391b944Sdan# function may be used to create a zero-filled blob to read or write
507b391b944Sdan# using the incremental-blob interface.
508b391b944Sdan#
509b391b944Sdando_execsql_test 13.1 {
510b391b944Sdan  CREATE TABLE c2(i INTEGER PRIMARY KEY, j);
511b391b944Sdan  INSERT INTO c2 VALUES(10, zeroblob(24));
512b391b944Sdan}
513b391b944Sdan
514b391b944Sdando_test 13.2 {
515b391b944Sdan  set stmt [sqlite3_prepare_v2 db "INSERT INTO c2 VALUES(11, ?)" -1]
516b391b944Sdan  sqlite3_bind_zeroblob $stmt 1 45
517b391b944Sdan  sqlite3_step $stmt
518b391b944Sdan  sqlite3_finalize $stmt
519b391b944Sdan} {SQLITE_OK}
520b391b944Sdan
521b391b944Sdan# The blobs can be read:
522b391b944Sdan#
523b391b944Sdando_test 13.3.1 {
524b391b944Sdan  sqlite3_blob_open db main c2 j 10 1 B
525b391b944Sdan  sqlite3_blob_open db main c2 j 11 1 B2
526b391b944Sdan  list [sqlite3_blob_bytes $B] [sqlite3_blob_bytes $B2]
527b391b944Sdan} {24 45}
528b391b944Sdando_test 13.3.2 {
529b391b944Sdan  sqlite3_blob_read $B 0 24
530b391b944Sdan} [string repeat [binary format c 0] 24]
531b391b944Sdando_test 13.3.3 {
532b391b944Sdan  sqlite3_blob_read $B2 0 45
533b391b944Sdan} [string repeat [binary format c 0] 45]
534b391b944Sdan
535b391b944Sdan# And also written:
536b391b944Sdan#
537b391b944Sdando_test 13.4.1 {
538b391b944Sdan  sqlite3_blob_write $B 0 [string repeat [binary format c 1] 24]
539b391b944Sdan} {}
540b391b944Sdando_test 13.4.2 {
541b391b944Sdan  sqlite3_blob_write $B2 0 [string repeat [binary format c 1] 45]
542b391b944Sdan} {}
543b391b944Sdando_test 13.5 {
544b391b944Sdan  sqlite3_blob_close $B
545b391b944Sdan  sqlite3_blob_close $B2
546b391b944Sdan  execsql { SELECT j FROM c2 }
547b391b944Sdan} [list \
548b391b944Sdan    [string repeat [binary format c 1] 24] \
549b391b944Sdan    [string repeat [binary format c 1] 45] \
550b391b944Sdan]
551b391b944Sdan
552b391b944Sdan
553b391b944Sdanfinish_test
554