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 #***********************************************************************
12 # $Id: incrblob.test,v 1.24 2009/06/19 22:23:42 drh Exp $
15 set testdir [file dirname $argv0]
16 source $testdir/tester.tcl
18 ifcapable {!autovacuum || !pragma || !incrblob} {
23 do_test incrblob-1.1 {
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');
31 do_test incrblob-1.2.1 {
32 set ::blob [db incrblob blobs v 1]
33 string match incrblob_* $::blob
35 unset -nocomplain data
36 do_test incrblob-1.2.2 {
37 binary scan [read $::blob] c* data
39 } {1 2 3 4 5 6 7 8 9 10}
40 do_test incrblob-1.2.3 {
42 puts -nonewline $::blob "1234567890"
45 do_test incrblob-1.2.4 {
47 binary scan [read $::blob] c* data
49 } {49 50 51 52 53 54 55 56 57 48}
50 do_test incrblob-1.2.5 {
53 do_test incrblob-1.2.6 {
55 SELECT v FROM blobs WHERE rowid = 1;
59 #--------------------------------------------------------------------
60 # Test cases incrblob-1.3.X check that it is possible to read and write
61 # regions of a blob that lie on overflow pages.
63 do_test incrblob-1.3.1 {
64 set ::str "[string repeat . 10000]"
66 INSERT INTO blobs(rowid, k, v) VALUES(3, 'three', $::str);
70 do_test incrblob-1.3.2 {
71 set ::blob [db incrblob blobs v 3]
75 do_test incrblob-1.3.3 {
77 puts -nonewline $::blob 1234567890
79 do_test incrblob-1.3.4 {
83 do_test incrblob-1.3.10 {
87 #------------------------------------------------------------------------
90 # Test that the following operations use ptrmap pages to reduce
93 # * Reading near the end of a blob,
94 # * Writing near the end of a blob, and
95 # * SELECT a column value that is located on an overflow page.
98 set bt [btree_from_db $db]
100 array set stats [btree_pager_stats $bt]
105 set bt [btree_from_db $db]
107 array set stats [btree_pager_stats $bt]
112 sqlite3_soft_heap_limit 0
114 foreach AutoVacuumMode [list 0 1] {
116 if {$AutoVacuumMode>0} {
117 ifcapable !autovacuum {
123 file delete -force test.db test.db-journal
126 execsql "PRAGMA auto_vacuum = $AutoVacuumMode"
128 do_test incrblob-2.$AutoVacuumMode.1 {
129 set ::str [string repeat abcdefghij 2900]
132 CREATE TABLE blobs(k PRIMARY KEY, v BLOB, i INTEGER);
134 INSERT INTO blobs VALUES('one', $::str || randstr(500,500), 45);
137 expr [file size test.db]/1024
138 } [expr 31 + $AutoVacuumMode]
140 ifcapable autovacuum {
141 do_test incrblob-2.$AutoVacuumMode.2 {
148 do_test incrblob-2.$AutoVacuumMode.3 {
149 # Open and close the db to make sure the page cache is empty.
153 # Read the last 20 bytes of the blob via a blob handle.
154 set ::blob [db incrblob blobs v 1]
156 set ::fragment [read $::blob]
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.
164 } [expr $AutoVacuumMode ? 4 : 30]
166 do_test incrblob-2.$AutoVacuumMode.4 {
167 string range [db one {SELECT v FROM blobs}] end-19 end
170 do_test incrblob-2.$AutoVacuumMode.5 {
171 # Open and close the db to make sure the page cache is empty.
175 # Write the second-to-last 20 bytes of the blob via a blob handle.
177 set ::blob [db incrblob blobs v 1]
179 puts -nonewline $::blob "1234567890abcdefghij"
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.
187 } [expr $AutoVacuumMode ? 4 : 30]
189 # Pages 1 (the write-counter) and 32 (the blob data) were written.
190 do_test incrblob-2.$AutoVacuumMode.6 {
195 do_test incrblob-2.$AutoVacuumMode.7 {
196 string range [db one {SELECT v FROM blobs}] end-39 end-20
197 } "1234567890abcdefghij"
199 do_test incrblob-2.$AutoVacuumMode.8 {
200 # Open and close the db to make sure the page cache is empty.
204 execsql { SELECT i FROM blobs }
207 do_test incrblob-2.$AutoVacuumMode.9 {
209 } [expr $AutoVacuumMode ? 4 : 30]
211 sqlite3_soft_heap_limit $cmdlinearg(soft-heap-limit)
213 #------------------------------------------------------------------------
216 # Test the outcome of trying to write to a read-only blob handle.
218 do_test incrblob-3.1 {
219 set ::blob [db incrblob -readonly blobs v 1]
222 } "1234567890abcdefghij"
223 do_test incrblob-3.2 {
226 puts -nonewline $::blob "helloworld"
230 } "1 {channel \"$::blob\" wasn't opened for writing}"
232 do_test incrblob-3.3 {
233 set ::blob [db incrblob -readonly blobs v 1]
236 } "1234567890abcdefghij"
237 do_test incrblob-3.4 {
239 sqlite3_blob_write $::blob 20 "qwertyuioplkjhgfds"
242 } {1 SQLITE_READONLY}
243 catch {close $::blob}
245 #------------------------------------------------------------------------
248 # Try a couple of error conditions:
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.
255 # 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
259 # 4.8 - Attempt to open an indexed column for writing
260 # 4.9 - Attempt to open an indexed column for reading (this works)
262 # 4.11 - Attempt to open a column of a view.
263 # 4.12 - Attempt to open a column of a virtual table.
265 do_test incrblob-4.1 {
267 set ::blob [db incrblob blobs v 2]
270 } {1 {no such rowid: 2}}
271 do_test incrblob-4.2 {
273 set ::blob [db incrblob blobs blue 1]
276 } {1 {no such column: "blue"}}
277 do_test incrblob-4.3 {
279 set ::blob [db incrblob nosuchtable blue 1]
282 } {1 {no such table: main.nosuchtable}}
283 do_test incrblob-4.4 {
285 set ::blob [db incrblob nosuchdb blobs v 1]
288 } {1 {no such table: nosuchdb.blobs}}
290 do_test incrblob-4.5 {
292 set ::blob [db incrblob blobs i 1]
295 } {1 {cannot open value of type integer}}
296 do_test incrblob-4.6 {
298 INSERT INTO blobs(k, v, i) VALUES(123, 567.765, NULL);
301 set ::blob [db incrblob blobs v 2]
304 } {1 {cannot open value of type real}}
305 do_test incrblob-4.7 {
307 set ::blob [db incrblob blobs i 2]
310 } {1 {cannot open value of type null}}
312 do_test incrblob-4.8.1 {
314 INSERT INTO blobs(k, v, i) VALUES(X'010203040506070809', 'hello', 'world');
317 set ::blob [db incrblob blobs k 3]
320 } {1 {cannot open indexed column for writing}}
321 do_test incrblob-4.8.2 {
323 CREATE TABLE t3(a INTEGER PRIMARY KEY, b);
324 INSERT INTO t3 VALUES(1, 2);
327 set ::blob [db incrblob -readonly t3 a 1]
330 } {1 {cannot open value of type null}}
331 do_test incrblob-4.8.3 {
333 set ::blob [db incrblob -readonly t3 rowid 1]
336 } {1 {no such column: "rowid"}}
338 do_test incrblob-4.9.1 {
340 set ::blob [db incrblob -readonly blobs k 3]
343 do_test incrblob-4.9.2 {
344 binary scan [read $::blob] c* c
347 } {1 2 3 4 5 6 7 8 9}
349 do_test incrblob-4.10 {
350 set ::blob [db incrblob -readonly blobs k 3]
351 set rc [catch { sqlite3_blob_read $::blob 10 100 } msg]
354 do_test incrblob-4.10.2 {
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]
363 } {1 {cannot open view: blobs_view}}
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]
371 } {1 {cannot open virtual table: blobs_echo}}
375 #------------------------------------------------------------------------
378 # Test that opening a blob in an attached database works.
381 do_test incrblob-5.1 {
382 file delete -force test2.db test2.db-journal
383 set ::size [expr [file size [info script]]]
385 ATTACH 'test2.db' AS aux;
386 CREATE TABLE aux.files(name, text);
387 INSERT INTO aux.files VALUES('this one', zeroblob($::size));
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]
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]
409 unset -nocomplain ::data
410 unset -nocomplain ::text
412 #------------------------------------------------------------------------
415 # Test that opening a blob for write-access is impossible if
416 # another connection has the database RESERVED lock.
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
423 # This test does not work with the "memsubsys1" configuration.
424 # Permutation memsubsys1 configures a very small static allocation
425 # for use as page-cache memory. This causes SQLite to upgrade
426 # to an exclusive lock when writing earlier than usual, which
427 # makes some of these tests fail.
429 sqlite3_soft_heap_limit 0
430 if {[permutation] != "memsubsys1"} {
431 do_test incrblob-6.1 {
435 INSERT INTO blobs(k, v, i) VALUES('a', 'different', 'connection');
438 do_test incrblob-6.2 {
440 SELECT rowid FROM blobs
443 do_test incrblob-6.3 {
445 db incrblob blobs v 1
448 } {1 {database is locked}}
449 do_test incrblob-6.4 {
451 db incrblob blobs v 3
454 } {1 {database is locked}}
455 do_test incrblob-6.5 {
456 set ::blob [db incrblob -readonly blobs v 3]
459 do_test incrblob-6.6 {
463 do_test incrblob-6.7 {
464 set ::blob [db2 incrblob blobs i 4]
467 do_test incrblob-6.8 {
470 do_test incrblob-6.9 {
472 puts -nonewline $::blob "invocation"
476 # At this point rollback should be illegal (because
477 # there is an open blob channel). But commit is also illegal because
478 # the open blob is read-write.
480 do_test incrblob-6.10 {
484 } {1 {cannot rollback transaction - SQL statements in progress}}
485 do_test incrblob-6.11 {
489 } {1 {cannot commit transaction - SQL statements in progress}}
491 do_test incrblob-6.12 {
493 SELECT * FROM blobs WHERE rowid = 4;
496 do_test incrblob-6.13 {
499 do_test incrblob-6.14 {
504 do_test incrblob-6.15 {
506 SELECT * FROM blobs WHERE rowid = 4;
508 } {a different invocation}
511 sqlite3_soft_heap_limit $cmdlinearg(soft-heap-limit)
513 #-----------------------------------------------------------------------
514 # The following tests verify the behaviour of the incremental IO
515 # APIs in the following cases:
517 # 7.1 A row that containing an open blob is modified.
519 # 7.2 A CREATE TABLE requires that an overflow page that is part
520 # of an open blob is moved.
522 # 7.3 An INCREMENTAL VACUUM moves an overflow page that is part
525 # In the first case above, correct behaviour is for all subsequent
526 # read/write operations on the blob-handle to return SQLITE_ABORT.
527 # More accurately, blob-handles are invalidated whenever the table
528 # they belong to is written to.
530 # The second two cases have no external effect. They are testing
531 # that the internal cache of overflow page numbers is correctly
534 do_test incrblob-7.1.0 {
538 CREATE TABLE t1 (a, b, c, d BLOB);
539 INSERT INTO t1(a, b, c, d) VALUES(1, 2, 3, 4);
544 foreach {tn arg} {1 "" 2 -readonly} {
547 UPDATE t1 SET d = zeroblob(10000);
550 do_test incrblob-7.1.$tn.1 {
551 set ::b [eval db incrblob $arg t1 d 1]
552 binary scan [sqlite3_blob_read $::b 5000 5] c* c
555 do_test incrblob-7.1.$tn.2 {
557 UPDATE t1 SET d = 15;
560 do_test incrblob-7.1.$tn.3 {
561 set rc [catch { sqlite3_blob_read $::b 5000 5 } msg]
564 do_test incrblob-7.1.$tn.4 {
569 do_test incrblob-7.1.$tn.5 {
570 set rc [catch { close $::b } msg]
573 do_test incrblob-7.1.$tn.6 {
581 set fd [open [info script]]
582 fconfigure $fd -translation binary
583 set ::data [read $fd 14000]
587 file delete -force test.db test.db-journal
590 do_test incrblob-7.2.1 {
592 PRAGMA auto_vacuum = "incremental";
593 CREATE TABLE t1(a INTEGER PRIMARY KEY, b); -- root@page3
594 INSERT INTO t1 VALUES(123, $::data);
596 set ::b [db incrblob -readonly t1 b 123]
597 fconfigure $::b -translation binary
600 do_test incrblob-7.2.2 {
602 CREATE TABLE t2(a INTEGER PRIMARY KEY, b); -- root@page4
607 do_test incrblob-7.2.3 {
610 SELECT rootpage FROM sqlite_master;
614 set ::otherdata "[string range $::data 0 1000][string range $::data 1001 end]"
615 do_test incrblob-7.3.1 {
617 INSERT INTO t2 VALUES(456, $::otherdata);
619 set ::b [db incrblob -readonly t2 b 456]
620 fconfigure $::b -translation binary
623 do_test incrblob-7.3.2 {
624 expr [file size test.db]/1024
626 do_test incrblob-7.3.3 {
628 DELETE FROM t1 WHERE a = 123;
629 PRAGMA INCREMENTAL_VACUUM(0);
635 # Attempt to write on a read-only blob. Make sure the error code
636 # gets set. Ticket #2464.
638 do_test incrblob-7.4 {
639 set rc [catch {sqlite3_blob_write $::b 10 HELLO} msg]
641 } {1 SQLITE_READONLY}
642 do_test incrblob-7.5 {
645 do_test incrblob-7.6 {
647 } {attempt to write a readonly database}
649 # Test that if either the "offset" or "amount" arguments to
650 # sqlite3_blob_write() are less than zero, SQLITE_ERROR is returned.
652 do_test incrblob-8.1 {
653 execsql { INSERT INTO t1 VALUES(314159, 'sqlite') }
654 set ::b [db incrblob t1 b 314159]
655 fconfigure $::b -translation binary
656 set rc [catch {sqlite3_blob_write $::b 10 HELLO -1} msg]
659 do_test incrblob-8.2 {
662 do_test incrblob-8.3 {
663 set rc [catch {sqlite3_blob_write $::b -1 HELLO 5} msg]
666 do_test incrblob-8.4 {
669 do_test incrblob-8.5 {
670 execsql {SELECT b FROM t1 WHERE a = 314159}
672 do_test incrblob-8.6 {
673 set rc [catch {sqlite3_blob_write $::b 0 etilqs 6} msg]
676 do_test incrblob-8.7 {
677 execsql {SELECT b FROM t1 WHERE a = 314159}
680 # The following test case exposes an instance in the blob code where
681 # an error message was set using a call similar to sqlite3_mprintf(zErr),
682 # where zErr is an arbitrary string. This is no good if the string contains
683 # characters that can be mistaken for printf() formatting directives.
685 do_test incrblob-9.1 {
686 list [catch { db incrblob t1 "A tricky column name %s%s" 1 } msg] $msg
687 } {1 {no such column: "A tricky column name %s%s"}}