blob: 0865912956fc5064ce5a4171f80b52519f8366a3 [file] [log] [blame]
danielk1977b4e9af92007-05-01 17:49:49 +00001# 2007 May 1
2#
3# The author disclaims copyright to this source code. In place of
4# a legal notice, here is a blessing:
5#
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.
9#
10#***********************************************************************
11#
drh9eb8cbe2009-06-19 22:23:41 +000012# $Id: incrblob.test,v 1.24 2009/06/19 22:23:42 drh Exp $
danielk197720713f32007-05-03 11:43:33 +000013#
danielk1977b4e9af92007-05-01 17:49:49 +000014
15set testdir [file dirname $argv0]
16source $testdir/tester.tcl
17
danielk197732a0d8b2007-05-04 19:03:02 +000018ifcapable {!autovacuum || !pragma || !incrblob} {
19 finish_test
20 return
21}
22
danielk1977b4e9af92007-05-01 17:49:49 +000023do_test incrblob-1.1 {
24 execsql {
25 CREATE TABLE blobs(k PRIMARY KEY, v BLOB);
26 INSERT INTO blobs VALUES('one', X'0102030405060708090A');
27 INSERT INTO blobs VALUES('two', X'0A090807060504030201');
28 }
29} {}
30
31do_test incrblob-1.2.1 {
32 set ::blob [db incrblob blobs v 1]
danielk1977ca306512007-06-15 15:08:08 +000033 string match incrblob_* $::blob
34} {1}
drh0e0d73c2008-04-16 23:39:26 +000035unset -nocomplain data
danielk1977b4e9af92007-05-01 17:49:49 +000036do_test incrblob-1.2.2 {
37 binary scan [read $::blob] c* data
38 set data
39} {1 2 3 4 5 6 7 8 9 10}
40do_test incrblob-1.2.3 {
41 seek $::blob 0
42 puts -nonewline $::blob "1234567890"
43 flush $::blob
44} {}
45do_test incrblob-1.2.4 {
46 seek $::blob 0
47 binary scan [read $::blob] c* data
48 set data
49} {49 50 51 52 53 54 55 56 57 48}
50do_test incrblob-1.2.5 {
51 close $::blob
52} {}
53do_test incrblob-1.2.6 {
54 execsql {
55 SELECT v FROM blobs WHERE rowid = 1;
56 }
57} {1234567890}
58
danielk1977d04417962007-05-02 13:16:30 +000059#--------------------------------------------------------------------
danielk197720713f32007-05-03 11:43:33 +000060# Test cases incrblob-1.3.X check that it is possible to read and write
danielk1977d04417962007-05-02 13:16:30 +000061# regions of a blob that lie on overflow pages.
danielk197720713f32007-05-03 11:43:33 +000062#
63do_test incrblob-1.3.1 {
danielk1977d04417962007-05-02 13:16:30 +000064 set ::str "[string repeat . 10000]"
65 execsql {
66 INSERT INTO blobs(rowid, k, v) VALUES(3, 'three', $::str);
67 }
68} {}
danielk1977b4e9af92007-05-01 17:49:49 +000069
danielk197720713f32007-05-03 11:43:33 +000070do_test incrblob-1.3.2 {
danielk1977d04417962007-05-02 13:16:30 +000071 set ::blob [db incrblob blobs v 3]
72 seek $::blob 8500
73 read $::blob 10
74} {..........}
danielk197720713f32007-05-03 11:43:33 +000075do_test incrblob-1.3.3 {
danielk1977d04417962007-05-02 13:16:30 +000076 seek $::blob 8500
77 puts -nonewline $::blob 1234567890
78} {}
danielk197720713f32007-05-03 11:43:33 +000079do_test incrblob-1.3.4 {
danielk1977d04417962007-05-02 13:16:30 +000080 seek $::blob 8496
81 read $::blob 10
82} {....123456}
danielk197720713f32007-05-03 11:43:33 +000083do_test incrblob-1.3.10 {
danielk1977d04417962007-05-02 13:16:30 +000084 close $::blob
85} {}
86
danielk197720713f32007-05-03 11:43:33 +000087#------------------------------------------------------------------------
danielk19778cbadb02007-05-03 16:31:26 +000088# incrblob-2.*:
89#
90# Test that the following operations use ptrmap pages to reduce
91# unnecessary reads:
danielk197744e6c8d2007-05-03 13:11:32 +000092#
93# * Reading near the end of a blob,
danielk19778cbadb02007-05-03 16:31:26 +000094# * Writing near the end of a blob, and
95# * SELECT a column value that is located on an overflow page.
danielk197720713f32007-05-03 11:43:33 +000096#
97proc nRead {db} {
98 set bt [btree_from_db $db]
drh27641702007-08-22 02:56:42 +000099 db_enter $db
danielk197720713f32007-05-03 11:43:33 +0000100 array set stats [btree_pager_stats $bt]
drh27641702007-08-22 02:56:42 +0000101 db_leave $db
danielk197720713f32007-05-03 11:43:33 +0000102 return $stats(read)
103}
danielk19778cbadb02007-05-03 16:31:26 +0000104proc nWrite {db} {
105 set bt [btree_from_db $db]
drh27641702007-08-22 02:56:42 +0000106 db_enter $db
danielk19778cbadb02007-05-03 16:31:26 +0000107 array set stats [btree_pager_stats $bt]
drh27641702007-08-22 02:56:42 +0000108 db_leave $db
danielk19778cbadb02007-05-03 16:31:26 +0000109 return $stats(write)
110}
danielk197720713f32007-05-03 11:43:33 +0000111
drh3aefaba2007-08-12 20:07:58 +0000112sqlite3_soft_heap_limit 0
113
danielk197720713f32007-05-03 11:43:33 +0000114foreach AutoVacuumMode [list 0 1] {
115
drh847d3ab2007-05-04 14:36:22 +0000116 if {$AutoVacuumMode>0} {
117 ifcapable !autovacuum {
118 break
119 }
120 }
121
danielk197720713f32007-05-03 11:43:33 +0000122 db close
123 file delete -force test.db test.db-journal
124
125 sqlite3 db test.db
126 execsql "PRAGMA auto_vacuum = $AutoVacuumMode"
127
128 do_test incrblob-2.$AutoVacuumMode.1 {
129 set ::str [string repeat abcdefghij 2900]
130 execsql {
131 BEGIN;
danielk19778cbadb02007-05-03 16:31:26 +0000132 CREATE TABLE blobs(k PRIMARY KEY, v BLOB, i INTEGER);
danielk197720713f32007-05-03 11:43:33 +0000133 DELETE FROM blobs;
danielk19778cbadb02007-05-03 16:31:26 +0000134 INSERT INTO blobs VALUES('one', $::str || randstr(500,500), 45);
danielk197720713f32007-05-03 11:43:33 +0000135 COMMIT;
136 }
137 expr [file size test.db]/1024
138 } [expr 31 + $AutoVacuumMode]
139
drh847d3ab2007-05-04 14:36:22 +0000140 ifcapable autovacuum {
141 do_test incrblob-2.$AutoVacuumMode.2 {
142 execsql {
143 PRAGMA auto_vacuum;
144 }
145 } $AutoVacuumMode
146 }
danielk197720713f32007-05-03 11:43:33 +0000147
148 do_test incrblob-2.$AutoVacuumMode.3 {
149 # Open and close the db to make sure the page cache is empty.
150 db close
151 sqlite3 db test.db
152
153 # Read the last 20 bytes of the blob via a blob handle.
154 set ::blob [db incrblob blobs v 1]
155 seek $::blob -20 end
156 set ::fragment [read $::blob]
157 close $::blob
158
159 # If the database is not in auto-vacuum mode, the whole of
160 # the overflow-chain must be scanned. In auto-vacuum mode,
161 # sqlite uses the ptrmap pages to avoid reading the other pages.
162 #
163 nRead db
164 } [expr $AutoVacuumMode ? 4 : 30]
165
danielk19778cbadb02007-05-03 16:31:26 +0000166 do_test incrblob-2.$AutoVacuumMode.4 {
danielk197720713f32007-05-03 11:43:33 +0000167 string range [db one {SELECT v FROM blobs}] end-19 end
168 } $::fragment
danielk19778cbadb02007-05-03 16:31:26 +0000169
170 do_test incrblob-2.$AutoVacuumMode.5 {
171 # Open and close the db to make sure the page cache is empty.
172 db close
173 sqlite3 db test.db
174
175 # Write the second-to-last 20 bytes of the blob via a blob handle.
176 #
177 set ::blob [db incrblob blobs v 1]
178 seek $::blob -40 end
179 puts -nonewline $::blob "1234567890abcdefghij"
180 flush $::blob
181
182 # If the database is not in auto-vacuum mode, the whole of
183 # the overflow-chain must be scanned. In auto-vacuum mode,
184 # sqlite uses the ptrmap pages to avoid reading the other pages.
185 #
186 nRead db
187 } [expr $AutoVacuumMode ? 4 : 30]
188
189 # Pages 1 (the write-counter) and 32 (the blob data) were written.
190 do_test incrblob-2.$AutoVacuumMode.6 {
191 close $::blob
192 nWrite db
193 } 2
194
195 do_test incrblob-2.$AutoVacuumMode.7 {
196 string range [db one {SELECT v FROM blobs}] end-39 end-20
197 } "1234567890abcdefghij"
198
199 do_test incrblob-2.$AutoVacuumMode.8 {
200 # Open and close the db to make sure the page cache is empty.
201 db close
202 sqlite3 db test.db
203
204 execsql { SELECT i FROM blobs }
205 } {45}
206
207 do_test incrblob-2.$AutoVacuumMode.9 {
208 nRead db
209 } [expr $AutoVacuumMode ? 4 : 30]
danielk197720713f32007-05-03 11:43:33 +0000210}
danc1a60c52010-06-07 14:28:16 +0000211sqlite3_soft_heap_limit $cmdlinearg(soft-heap-limit)
danielk197720713f32007-05-03 11:43:33 +0000212
danielk19778cbadb02007-05-03 16:31:26 +0000213#------------------------------------------------------------------------
214# incrblob-3.*:
215#
216# Test the outcome of trying to write to a read-only blob handle.
217#
danielk19778cbadb02007-05-03 16:31:26 +0000218do_test incrblob-3.1 {
219 set ::blob [db incrblob -readonly blobs v 1]
220 seek $::blob -40 end
221 read $::blob 20
222} "1234567890abcdefghij"
223do_test incrblob-3.2 {
224 seek $::blob 0
225 set rc [catch {
226 puts -nonewline $::blob "helloworld"
227 } msg]
danielk1977f1819242007-05-03 18:14:10 +0000228 close $::blob
danielk19778cbadb02007-05-03 16:31:26 +0000229 list $rc $msg
230} "1 {channel \"$::blob\" wasn't opened for writing}"
231
danielk1977dcbb5d32007-05-04 18:36:44 +0000232do_test incrblob-3.3 {
233 set ::blob [db incrblob -readonly blobs v 1]
234 seek $::blob -40 end
235 read $::blob 20
236} "1234567890abcdefghij"
237do_test incrblob-3.4 {
238 set rc [catch {
239 sqlite3_blob_write $::blob 20 "qwertyuioplkjhgfds"
240 } msg]
241 list $rc $msg
242} {1 SQLITE_READONLY}
243catch {close $::blob}
244
danielk19778cbadb02007-05-03 16:31:26 +0000245#------------------------------------------------------------------------
246# incrblob-4.*:
247#
248# Try a couple of error conditions:
249#
250# 4.1 - Attempt to open a row that does not exist.
251# 4.2 - Attempt to open a column that does not exist.
252# 4.3 - Attempt to open a table that does not exist.
253# 4.4 - Attempt to open a database that does not exist.
254#
danielk1977f1819242007-05-03 18:14:10 +0000255# 4.5 - Attempt to open an integer
256# 4.6 - Attempt to open a real value
257# 4.7 - Attempt to open an SQL null
258#
259# 4.8 - Attempt to open an indexed column for writing
260# 4.9 - Attempt to open an indexed column for reading (this works)
261#
danielk197736961ed2008-04-24 09:49:55 +0000262# 4.11 - Attempt to open a column of a view.
263# 4.12 - Attempt to open a column of a virtual table.
264#
danielk19778cbadb02007-05-03 16:31:26 +0000265do_test incrblob-4.1 {
266 set rc [catch {
267 set ::blob [db incrblob blobs v 2]
268 } msg ]
269 list $rc $msg
270} {1 {no such rowid: 2}}
danielk19778cbadb02007-05-03 16:31:26 +0000271do_test incrblob-4.2 {
272 set rc [catch {
273 set ::blob [db incrblob blobs blue 1]
274 } msg ]
275 list $rc $msg
276} {1 {no such column: "blue"}}
danielk19778cbadb02007-05-03 16:31:26 +0000277do_test incrblob-4.3 {
278 set rc [catch {
279 set ::blob [db incrblob nosuchtable blue 1]
danielk1977f1819242007-05-03 18:14:10 +0000280 } msg ]
danielk19778cbadb02007-05-03 16:31:26 +0000281 list $rc $msg
282} {1 {no such table: main.nosuchtable}}
danielk19778cbadb02007-05-03 16:31:26 +0000283do_test incrblob-4.4 {
284 set rc [catch {
285 set ::blob [db incrblob nosuchdb blobs v 1]
286 } msg ]
287 list $rc $msg
288} {1 {no such table: nosuchdb.blobs}}
289
danielk1977f1819242007-05-03 18:14:10 +0000290do_test incrblob-4.5 {
291 set rc [catch {
292 set ::blob [db incrblob blobs i 1]
293 } msg ]
294 list $rc $msg
295} {1 {cannot open value of type integer}}
296do_test incrblob-4.6 {
297 execsql {
298 INSERT INTO blobs(k, v, i) VALUES(123, 567.765, NULL);
299 }
300 set rc [catch {
301 set ::blob [db incrblob blobs v 2]
302 } msg ]
303 list $rc $msg
304} {1 {cannot open value of type real}}
305do_test incrblob-4.7 {
306 set rc [catch {
307 set ::blob [db incrblob blobs i 2]
308 } msg ]
309 list $rc $msg
310} {1 {cannot open value of type null}}
danielk19778cbadb02007-05-03 16:31:26 +0000311
dan13167002009-10-02 06:35:06 +0000312do_test incrblob-4.8.1 {
danielk1977f1819242007-05-03 18:14:10 +0000313 execsql {
314 INSERT INTO blobs(k, v, i) VALUES(X'010203040506070809', 'hello', 'world');
315 }
316 set rc [catch {
317 set ::blob [db incrblob blobs k 3]
318 } msg ]
319 list $rc $msg
320} {1 {cannot open indexed column for writing}}
dan13167002009-10-02 06:35:06 +0000321do_test incrblob-4.8.2 {
322 execsql {
323 CREATE TABLE t3(a INTEGER PRIMARY KEY, b);
324 INSERT INTO t3 VALUES(1, 2);
325 }
326 set rc [catch {
327 set ::blob [db incrblob -readonly t3 a 1]
328 } msg ]
329 list $rc $msg
330} {1 {cannot open value of type null}}
331do_test incrblob-4.8.3 {
332 set rc [catch {
333 set ::blob [db incrblob -readonly t3 rowid 1]
334 } msg ]
335 list $rc $msg
336} {1 {no such column: "rowid"}}
danielk1977f1819242007-05-03 18:14:10 +0000337
338do_test incrblob-4.9.1 {
339 set rc [catch {
340 set ::blob [db incrblob -readonly blobs k 3]
341 } msg]
342} {0}
343do_test incrblob-4.9.2 {
344 binary scan [read $::blob] c* c
345 close $::blob
346 set c
347} {1 2 3 4 5 6 7 8 9}
348
danielk1977880c15b2007-09-01 18:24:55 +0000349do_test incrblob-4.10 {
350 set ::blob [db incrblob -readonly blobs k 3]
351 set rc [catch { sqlite3_blob_read $::blob 10 100 } msg]
352 list $rc $msg
353} {1 SQLITE_ERROR}
danielk197736961ed2008-04-24 09:49:55 +0000354do_test incrblob-4.10.2 {
danielk1977880c15b2007-09-01 18:24:55 +0000355 close $::blob
356} {}
357
danielk197736961ed2008-04-24 09:49:55 +0000358ifcapable view {
359 do_test incrblob-4.11 {
360 execsql { CREATE VIEW blobs_view AS SELECT k, v, i FROM blobs }
361 set rc [catch { db incrblob blobs_view v 3 } msg]
362 list $rc $msg
363 } {1 {cannot open view: blobs_view}}
364}
365ifcapable vtab {
366 register_echo_module [sqlite3_connection_pointer db]
367 do_test incrblob-4.12 {
368 execsql { CREATE VIRTUAL TABLE blobs_echo USING echo(blobs) }
369 set rc [catch { db incrblob blobs_echo v 3 } msg]
370 list $rc $msg
371 } {1 {cannot open virtual table: blobs_echo}}
372}
373
374
danielk1977f1819242007-05-03 18:14:10 +0000375#------------------------------------------------------------------------
376# incrblob-5.*:
377#
378# Test that opening a blob in an attached database works.
379#
danielk19775a8f9372007-10-09 08:29:32 +0000380ifcapable attach {
381 do_test incrblob-5.1 {
382 file delete -force test2.db test2.db-journal
383 set ::size [expr [file size [info script]]]
384 execsql {
385 ATTACH 'test2.db' AS aux;
386 CREATE TABLE aux.files(name, text);
387 INSERT INTO aux.files VALUES('this one', zeroblob($::size));
388 }
389 set fd [db incrblob aux files text 1]
390 fconfigure $fd -translation binary
391 set fd2 [open [info script]]
392 fconfigure $fd2 -translation binary
393 puts -nonewline $fd [read $fd2]
394 close $fd
395 close $fd2
396 set ::text [db one {select text from aux.files}]
397 string length $::text
398 } [file size [info script]]
399 do_test incrblob-5.2 {
400 set fd2 [open [info script]]
401 fconfigure $fd2 -translation binary
402 set ::data [read $fd2]
403 close $fd2
404 set ::data
405 } $::text
406}
danielk1977f1819242007-05-03 18:14:10 +0000407
408# free memory
danielk19775a8f9372007-10-09 08:29:32 +0000409unset -nocomplain ::data
410unset -nocomplain ::text
danielk1977f1819242007-05-03 18:14:10 +0000411
412#------------------------------------------------------------------------
413# incrblob-6.*:
414#
415# Test that opening a blob for write-access is impossible if
416# another connection has the database RESERVED lock.
417#
418# Then test that blob writes that take place inside of a
419# transaction are not visible to external connections until
420# after the transaction is commited and the blob channel
421# closed.
422#
drh3aefaba2007-08-12 20:07:58 +0000423sqlite3_soft_heap_limit 0
danielk1977f1819242007-05-03 18:14:10 +0000424do_test incrblob-6.1 {
425 sqlite3 db2 test.db
426 execsql {
427 BEGIN;
428 INSERT INTO blobs(k, v, i) VALUES('a', 'different', 'connection');
429 } db2
430} {}
431do_test incrblob-6.2 {
432 execsql {
433 SELECT rowid FROM blobs
434 }
435} {1 2 3}
436do_test incrblob-6.3 {
437 set rc [catch {
438 db incrblob blobs v 1
439 } msg]
440 list $rc $msg
441} {1 {database is locked}}
442do_test incrblob-6.4 {
443 set rc [catch {
444 db incrblob blobs v 3
445 } msg]
446 list $rc $msg
447} {1 {database is locked}}
448do_test incrblob-6.5 {
449 set ::blob [db incrblob -readonly blobs v 3]
450 read $::blob
451} {hello}
452do_test incrblob-6.6 {
453 close $::blob
454} {}
455
456do_test incrblob-6.7 {
457 set ::blob [db2 incrblob blobs i 4]
458 gets $::blob
459} {connection}
460do_test incrblob-6.8 {
461 tell $::blob
462} {10}
danielk1977f1819242007-05-03 18:14:10 +0000463do_test incrblob-6.9 {
464 seek $::blob 0
465 puts -nonewline $::blob "invocation"
466 flush $::blob
467} {}
468
drhad4a4b82008-11-05 16:37:34 +0000469# At this point rollback should be illegal (because
drh9eb8cbe2009-06-19 22:23:41 +0000470# there is an open blob channel). But commit is also illegal because
471# the open blob is read-write.
drhad4a4b82008-11-05 16:37:34 +0000472#
danielk1977f1819242007-05-03 18:14:10 +0000473do_test incrblob-6.10 {
474 catchsql {
475 ROLLBACK;
476 } db2
477} {1 {cannot rollback transaction - SQL statements in progress}}
478do_test incrblob-6.11 {
479 catchsql {
480 COMMIT;
481 } db2
drh9eb8cbe2009-06-19 22:23:41 +0000482} {1 {cannot commit transaction - SQL statements in progress}}
danielk1977f1819242007-05-03 18:14:10 +0000483
484do_test incrblob-6.12 {
485 execsql {
486 SELECT * FROM blobs WHERE rowid = 4;
487 }
488} {}
489do_test incrblob-6.13 {
490 close $::blob
danielk1977f1819242007-05-03 18:14:10 +0000491} {}
492do_test incrblob-6.14 {
drh9eb8cbe2009-06-19 22:23:41 +0000493 catchsql {
494 COMMIT;
495 } db2
496} {0 {}}
497do_test incrblob-6.15 {
danielk1977f1819242007-05-03 18:14:10 +0000498 execsql {
499 SELECT * FROM blobs WHERE rowid = 4;
500 }
501} {a different invocation}
502db2 close
danc1a60c52010-06-07 14:28:16 +0000503sqlite3_soft_heap_limit $cmdlinearg(soft-heap-limit)
danielk1977f1819242007-05-03 18:14:10 +0000504
danielk1977dcbb5d32007-05-04 18:36:44 +0000505#-----------------------------------------------------------------------
506# The following tests verify the behaviour of the incremental IO
507# APIs in the following cases:
508#
509# 7.1 A row that containing an open blob is modified.
510#
511# 7.2 A CREATE TABLE requires that an overflow page that is part
512# of an open blob is moved.
513#
514# 7.3 An INCREMENTAL VACUUM moves an overflow page that is part
515# of an open blob.
516#
517# In the first case above, correct behaviour is for all subsequent
518# read/write operations on the blob-handle to return SQLITE_ABORT.
519# More accurately, blob-handles are invalidated whenever the table
520# they belong to is written to.
521#
522# The second two cases have no external effect. They are testing
523# that the internal cache of overflow page numbers is correctly
524# invalidated.
525#
526do_test incrblob-7.1.0 {
527 execsql {
528 BEGIN;
529 DROP TABLE blobs;
530 CREATE TABLE t1 (a, b, c, d BLOB);
531 INSERT INTO t1(a, b, c, d) VALUES(1, 2, 3, 4);
532 COMMIT;
533 }
534} {}
535
536foreach {tn arg} {1 "" 2 -readonly} {
537
538 execsql {
539 UPDATE t1 SET d = zeroblob(10000);
540 }
541
542 do_test incrblob-7.1.$tn.1 {
543 set ::b [eval db incrblob $arg t1 d 1]
544 binary scan [sqlite3_blob_read $::b 5000 5] c* c
545 set c
546 } {0 0 0 0 0}
547 do_test incrblob-7.1.$tn.2 {
548 execsql {
549 UPDATE t1 SET d = 15;
550 }
551 } {}
552 do_test incrblob-7.1.$tn.3 {
553 set rc [catch { sqlite3_blob_read $::b 5000 5 } msg]
554 list $rc $msg
555 } {1 SQLITE_ABORT}
556 do_test incrblob-7.1.$tn.4 {
557 execsql {
558 SELECT d FROM t1;
559 }
560 } {15}
561 do_test incrblob-7.1.$tn.5 {
562 set rc [catch { close $::b } msg]
563 list $rc $msg
564 } {0 {}}
565 do_test incrblob-7.1.$tn.6 {
566 execsql {
567 SELECT d FROM t1;
568 }
569 } {15}
570
571}
572
573set fd [open [info script]]
drhdfdcf2c2007-09-03 16:45:35 +0000574fconfigure $fd -translation binary
danielk1977880c15b2007-09-01 18:24:55 +0000575set ::data [read $fd 14000]
danielk1977dcbb5d32007-05-04 18:36:44 +0000576close $fd
577
578db close
579file delete -force test.db test.db-journal
580sqlite3 db test.db
581
582do_test incrblob-7.2.1 {
583 execsql {
584 PRAGMA auto_vacuum = "incremental";
585 CREATE TABLE t1(a INTEGER PRIMARY KEY, b); -- root@page3
586 INSERT INTO t1 VALUES(123, $::data);
587 }
588 set ::b [db incrblob -readonly t1 b 123]
danielk1977540cf6a2008-09-11 11:27:59 +0000589 fconfigure $::b -translation binary
danielk1977dcbb5d32007-05-04 18:36:44 +0000590 read $::b
591} $::data
592do_test incrblob-7.2.2 {
593 execsql {
594 CREATE TABLE t2(a INTEGER PRIMARY KEY, b); -- root@page4
595 }
596 seek $::b 0
597 read $::b
598} $::data
599do_test incrblob-7.2.3 {
600 close $::b
601 execsql {
602 SELECT rootpage FROM sqlite_master;
603 }
604} {3 4}
605
606set ::otherdata "[string range $::data 0 1000][string range $::data 1001 end]"
607do_test incrblob-7.3.1 {
608 execsql {
609 INSERT INTO t2 VALUES(456, $::otherdata);
610 }
611 set ::b [db incrblob -readonly t2 b 456]
danielk1977540cf6a2008-09-11 11:27:59 +0000612 fconfigure $::b -translation binary
danielk1977dcbb5d32007-05-04 18:36:44 +0000613 read $::b
614} $::otherdata
615do_test incrblob-7.3.2 {
616 expr [file size test.db]/1024
617} 30
618do_test incrblob-7.3.3 {
619 execsql {
620 DELETE FROM t1 WHERE a = 123;
danielk1977a9808b32007-05-07 09:32:45 +0000621 PRAGMA INCREMENTAL_VACUUM(0);
danielk1977dcbb5d32007-05-04 18:36:44 +0000622 }
623 seek $::b 0
624 read $::b
625} $::otherdata
626
drh0e8003d2007-06-27 00:36:13 +0000627# Attempt to write on a read-only blob. Make sure the error code
628# gets set. Ticket #2464.
629#
630do_test incrblob-7.4 {
631 set rc [catch {sqlite3_blob_write $::b 10 HELLO} msg]
632 lappend rc $msg
633} {1 SQLITE_READONLY}
634do_test incrblob-7.5 {
635 sqlite3_errcode db
636} {SQLITE_READONLY}
637do_test incrblob-7.6 {
638 sqlite3_errmsg db
639} {attempt to write a readonly database}
danielk1977dcbb5d32007-05-04 18:36:44 +0000640
danielk1977a8b30182008-10-02 14:49:01 +0000641# Test that if either the "offset" or "amount" arguments to
642# sqlite3_blob_write() are less than zero, SQLITE_ERROR is returned.
643#
644do_test incrblob-8.1 {
645 execsql { INSERT INTO t1 VALUES(314159, 'sqlite') }
646 set ::b [db incrblob t1 b 314159]
647 fconfigure $::b -translation binary
648 set rc [catch {sqlite3_blob_write $::b 10 HELLO -1} msg]
649 lappend rc $msg
650} {1 SQLITE_ERROR}
651do_test incrblob-8.2 {
652 sqlite3_errcode db
653} {SQLITE_ERROR}
654do_test incrblob-8.3 {
655 set rc [catch {sqlite3_blob_write $::b -1 HELLO 5} msg]
656 lappend rc $msg
657} {1 SQLITE_ERROR}
658do_test incrblob-8.4 {
659 sqlite3_errcode db
660} {SQLITE_ERROR}
661do_test incrblob-8.5 {
662 execsql {SELECT b FROM t1 WHERE a = 314159}
663} {sqlite}
664do_test incrblob-8.6 {
665 set rc [catch {sqlite3_blob_write $::b 0 etilqs 6} msg]
666 lappend rc $msg
667} {0 {}}
668do_test incrblob-8.7 {
669 execsql {SELECT b FROM t1 WHERE a = 314159}
670} {etilqs}
671
672
drh0e8003d2007-06-27 00:36:13 +0000673finish_test