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 forcedelete test.db test.db-journal
126 execsql "PRAGMA mmap_size = 0"
127 execsql "PRAGMA auto_vacuum = $AutoVacuumMode"
129 do_test incrblob-2.$AutoVacuumMode.1 {
130 set ::str [string repeat abcdefghij 2900]
133 CREATE TABLE blobs(k PRIMARY KEY, v BLOB, i INTEGER);
135 INSERT INTO blobs VALUES('one', $::str || randstr(500,500), 45);
138 expr [file size test.db]/1024
139 } [expr 31 + $AutoVacuumMode]
141 ifcapable autovacuum {
142 do_test incrblob-2.$AutoVacuumMode.2 {
149 do_test incrblob-2.$AutoVacuumMode.3 {
150 # Open and close the db to make sure the page cache is empty.
153 execsql "PRAGMA mmap_size = 0"
155 # Read the last 20 bytes of the blob via a blob handle.
156 set ::blob [db incrblob blobs v 1]
158 set ::fragment [read $::blob]
161 # If the database is not in auto-vacuum mode, the whole of
162 # the overflow-chain must be scanned. In auto-vacuum mode,
163 # sqlite uses the ptrmap pages to avoid reading the other pages.
166 } [expr $AutoVacuumMode ? 4 : 30]
168 do_test incrblob-2.$AutoVacuumMode.4 {
169 string range [db one {SELECT v FROM blobs}] end-19 end
172 do_test incrblob-2.$AutoVacuumMode.5 {
173 # Open and close the db to make sure the page cache is empty.
176 execsql "PRAGMA mmap_size = 0"
178 # Write the second-to-last 20 bytes of the blob via a blob handle.
180 set ::blob [db incrblob blobs v 1]
182 puts -nonewline $::blob "1234567890abcdefghij"
185 # If the database is not in auto-vacuum mode, the whole of
186 # the overflow-chain must be scanned. In auto-vacuum mode,
187 # sqlite uses the ptrmap pages to avoid reading the other pages.
190 } [expr $AutoVacuumMode ? 4 : 30]
192 # Pages 1 (the write-counter) and 32 (the blob data) were written.
193 do_test incrblob-2.$AutoVacuumMode.6 {
198 do_test incrblob-2.$AutoVacuumMode.7 {
199 string range [db one {SELECT v FROM blobs}] end-39 end-20
200 } "1234567890abcdefghij"
202 do_test incrblob-2.$AutoVacuumMode.8 {
203 # Open and close the db to make sure the page cache is empty.
206 execsql { PRAGMA mmap_size = 0 }
208 execsql { SELECT i FROM blobs }
211 do_test incrblob-2.$AutoVacuumMode.9 {
213 } [expr $AutoVacuumMode ? 4 : 30]
215 sqlite3_soft_heap_limit $cmdlinearg(soft-heap-limit)
217 #------------------------------------------------------------------------
220 # Test the outcome of trying to write to a read-only blob handle.
222 do_test incrblob-3.1 {
223 set ::blob [db incrblob -readonly blobs v 1]
226 } "1234567890abcdefghij"
227 do_test incrblob-3.2 {
230 puts -nonewline $::blob "helloworld"
234 } "1 {channel \"$::blob\" wasn't opened for writing}"
236 do_test incrblob-3.3 {
237 set ::blob [db incrblob -readonly blobs v 1]
240 } "1234567890abcdefghij"
241 do_test incrblob-3.4 {
243 sqlite3_blob_write $::blob 20 "qwertyuioplkjhgfds"
246 } {1 SQLITE_READONLY}
247 catch {close $::blob}
249 #------------------------------------------------------------------------
252 # Try a couple of error conditions:
254 # 4.1 - Attempt to open a row that does not exist.
255 # 4.2 - Attempt to open a column that does not exist.
256 # 4.3 - Attempt to open a table that does not exist.
257 # 4.4 - Attempt to open a database that does not exist.
259 # 4.5 - Attempt to open an integer
260 # 4.6 - Attempt to open a real value
261 # 4.7 - Attempt to open an SQL null
263 # 4.8 - Attempt to open an indexed column for writing
264 # 4.9 - Attempt to open an indexed column for reading (this works)
266 # 4.11 - Attempt to open a column of a view.
267 # 4.12 - Attempt to open a column of a virtual table.
269 do_test incrblob-4.1 {
271 set ::blob [db incrblob blobs v 2]
274 } {1 {no such rowid: 2}}
275 do_test incrblob-4.2 {
277 set ::blob [db incrblob blobs blue 1]
280 } {1 {no such column: "blue"}}
281 do_test incrblob-4.3 {
283 set ::blob [db incrblob nosuchtable blue 1]
286 } {1 {no such table: main.nosuchtable}}
287 do_test incrblob-4.4 {
289 set ::blob [db incrblob nosuchdb blobs v 1]
292 } {1 {no such table: nosuchdb.blobs}}
294 do_test incrblob-4.5 {
296 set ::blob [db incrblob blobs i 1]
299 } {1 {cannot open value of type integer}}
300 do_test incrblob-4.6 {
302 INSERT INTO blobs(k, v, i) VALUES(123, 567.765, NULL);
305 set ::blob [db incrblob blobs v 2]
308 } {1 {cannot open value of type real}}
309 do_test incrblob-4.7 {
311 set ::blob [db incrblob blobs i 2]
314 } {1 {cannot open value of type null}}
316 do_test incrblob-4.8.1 {
318 INSERT INTO blobs(k, v, i) VALUES(X'010203040506070809', 'hello', 'world');
321 set ::blob [db incrblob blobs k 3]
324 } {1 {cannot open indexed column for writing}}
325 do_test incrblob-4.8.2 {
327 CREATE TABLE t3(a INTEGER PRIMARY KEY, b);
328 INSERT INTO t3 VALUES(1, 2);
331 set ::blob [db incrblob -readonly t3 a 1]
334 } {1 {cannot open value of type null}}
335 do_test incrblob-4.8.3 {
337 set ::blob [db incrblob -readonly t3 rowid 1]
340 } {1 {no such column: "rowid"}}
342 do_test incrblob-4.9.1 {
344 set ::blob [db incrblob -readonly blobs k 3]
347 do_test incrblob-4.9.2 {
348 binary scan [read $::blob] c* c
351 } {1 2 3 4 5 6 7 8 9}
353 do_test incrblob-4.10 {
354 set ::blob [db incrblob -readonly blobs k 3]
355 set rc [catch { sqlite3_blob_read $::blob 10 100 } msg]
358 do_test incrblob-4.10.2 {
363 do_test incrblob-4.11 {
364 execsql { CREATE VIEW blobs_view AS SELECT k, v, i FROM blobs }
365 set rc [catch { db incrblob blobs_view v 3 } msg]
367 } {1 {cannot open view: blobs_view}}
370 register_echo_module [sqlite3_connection_pointer db]
371 do_test incrblob-4.12 {
372 execsql { CREATE VIRTUAL TABLE blobs_echo USING echo(blobs) }
373 set rc [catch { db incrblob blobs_echo v 3 } msg]
375 } {1 {cannot open virtual table: blobs_echo}}
379 #------------------------------------------------------------------------
382 # Test that opening a blob in an attached database works.
385 do_test incrblob-5.1 {
386 forcedelete test2.db test2.db-journal
387 set ::size [expr [file size [info script]]]
389 ATTACH 'test2.db' AS aux;
390 CREATE TABLE aux.files(name, text);
391 INSERT INTO aux.files VALUES('this one', zeroblob($::size));
393 set fd [db incrblob aux files text 1]
394 fconfigure $fd -translation binary
395 set fd2 [open [info script]]
396 fconfigure $fd2 -translation binary
397 puts -nonewline $fd [read $fd2]
400 set ::text [db one {select text from aux.files}]
401 string length $::text
402 } [file size [info script]]
403 do_test incrblob-5.2 {
404 set fd2 [open [info script]]
405 fconfigure $fd2 -translation binary
406 set ::data [read $fd2]
413 unset -nocomplain ::data
414 unset -nocomplain ::text
416 #------------------------------------------------------------------------
419 # Test that opening a blob for write-access is impossible if
420 # another connection has the database RESERVED lock.
422 # Then test that blob writes that take place inside of a
423 # transaction are not visible to external connections until
424 # after the transaction is commited and the blob channel
427 # This test does not work with the "memsubsys1" configuration.
428 # Permutation memsubsys1 configures a very small static allocation
429 # for use as page-cache memory. This causes SQLite to upgrade
430 # to an exclusive lock when writing earlier than usual, which
431 # makes some of these tests fail.
433 sqlite3_soft_heap_limit 0
434 if {[permutation] != "memsubsys1"} {
435 do_test incrblob-6.1 {
439 INSERT INTO blobs(k, v, i) VALUES('a', 'different', 'connection');
442 do_test incrblob-6.2 {
444 SELECT rowid FROM blobs ORDER BY rowid
447 do_test incrblob-6.3 {
449 db incrblob blobs v 1
452 } {1 {database is locked}}
453 do_test incrblob-6.4 {
455 db incrblob blobs v 3
458 } {1 {database is locked}}
459 do_test incrblob-6.5 {
460 set ::blob [db incrblob -readonly blobs v 3]
463 do_test incrblob-6.6 {
467 do_test incrblob-6.7 {
468 set ::blob [db2 incrblob blobs i 4]
471 do_test incrblob-6.8 {
474 do_test incrblob-6.9 {
476 puts -nonewline $::blob "invocation"
480 # At this point commit should be illegal (because
481 # there is an open blob channel).
483 do_test incrblob-6.11 {
487 } {1 {cannot commit transaction - SQL statements in progress}}
489 do_test incrblob-6.12 {
491 SELECT * FROM blobs WHERE rowid = 4;
494 do_test incrblob-6.13 {
497 do_test incrblob-6.14 {
502 do_test incrblob-6.15 {
504 SELECT * FROM blobs WHERE rowid = 4;
506 } {a different invocation}
509 sqlite3_soft_heap_limit $cmdlinearg(soft-heap-limit)
511 #-----------------------------------------------------------------------
512 # The following tests verify the behavior of the incremental IO
513 # APIs in the following cases:
515 # 7.1 A row that containing an open blob is modified.
517 # 7.2 A CREATE TABLE requires that an overflow page that is part
518 # of an open blob is moved.
520 # 7.3 An INCREMENTAL VACUUM moves an overflow page that is part
523 # In the first case above, correct behavior is for all subsequent
524 # read/write operations on the blob-handle to return SQLITE_ABORT.
525 # More accurately, blob-handles are invalidated whenever the table
526 # they belong to is written to.
528 # The second two cases have no external effect. They are testing
529 # that the internal cache of overflow page numbers is correctly
532 do_test incrblob-7.1.0 {
536 CREATE TABLE t1 (a, b, c, d BLOB);
537 INSERT INTO t1(a, b, c, d) VALUES(1, 2, 3, 4);
542 foreach {tn arg} {1 "" 2 -readonly} {
545 UPDATE t1 SET d = zeroblob(10000);
548 do_test incrblob-7.1.$tn.1 {
549 set ::b [eval db incrblob $arg t1 d 1]
550 binary scan [sqlite3_blob_read $::b 5000 5] c* c
553 do_test incrblob-7.1.$tn.2 {
555 UPDATE t1 SET d = 15;
558 do_test incrblob-7.1.$tn.3 {
559 set rc [catch { sqlite3_blob_read $::b 5000 5 } msg]
562 do_test incrblob-7.1.$tn.4 {
567 do_test incrblob-7.1.$tn.5 {
568 set rc [catch { close $::b } msg]
571 do_test incrblob-7.1.$tn.6 {
579 set fd [open [info script]]
580 fconfigure $fd -translation binary
581 set ::data [read $fd 14000]
585 forcedelete test.db test.db-journal
588 do_test incrblob-7.2.1 {
590 PRAGMA auto_vacuum = "incremental";
591 CREATE TABLE t1(a INTEGER PRIMARY KEY, b); -- root@page3
592 INSERT INTO t1 VALUES(123, $::data);
594 set ::b [db incrblob -readonly t1 b 123]
595 fconfigure $::b -translation binary
598 do_test incrblob-7.2.2 {
600 CREATE TABLE t2(a INTEGER PRIMARY KEY, b); -- root@page4
605 do_test incrblob-7.2.3 {
608 SELECT rootpage FROM sqlite_master;
612 set ::otherdata "[string range $::data 0 1000][string range $::data 1001 end]"
613 do_test incrblob-7.3.1 {
615 INSERT INTO t2 VALUES(456, $::otherdata);
617 set ::b [db incrblob -readonly t2 b 456]
618 fconfigure $::b -translation binary
621 do_test incrblob-7.3.2 {
622 expr [file size test.db]/1024
624 do_test incrblob-7.3.3 {
626 DELETE FROM t1 WHERE a = 123;
627 PRAGMA INCREMENTAL_VACUUM(0);
633 # Attempt to write on a read-only blob. Make sure the error code
634 # gets set. Ticket #2464.
636 do_test incrblob-7.4 {
637 set rc [catch {sqlite3_blob_write $::b 10 HELLO} msg]
639 } {1 SQLITE_READONLY}
640 do_test incrblob-7.5 {
643 do_test incrblob-7.6 {
645 } {attempt to write a readonly database}
647 # Test that if either the "offset" or "amount" arguments to
648 # sqlite3_blob_write() are less than zero, SQLITE_ERROR is returned.
650 do_test incrblob-8.1 {
651 execsql { INSERT INTO t1 VALUES(314159, 'sqlite') }
652 set ::b [db incrblob t1 b 314159]
653 fconfigure $::b -translation binary
654 set rc [catch {sqlite3_blob_write $::b 10 HELLO -1} msg]
657 do_test incrblob-8.2 {
660 do_test incrblob-8.3 {
661 set rc [catch {sqlite3_blob_write $::b -1 HELLO 5} msg]
664 do_test incrblob-8.4 {
667 do_test incrblob-8.5 {
668 execsql {SELECT b FROM t1 WHERE a = 314159}
670 do_test incrblob-8.6 {
671 set rc [catch {sqlite3_blob_write $::b 0 etilqs 6} msg]
674 do_test incrblob-8.7 {
675 execsql {SELECT b FROM t1 WHERE a = 314159}
678 # The following test case exposes an instance in the blob code where
679 # an error message was set using a call similar to sqlite3_mprintf(zErr),
680 # where zErr is an arbitrary string. This is no good if the string contains
681 # characters that can be mistaken for printf() formatting directives.
683 do_test incrblob-9.1 {
684 list [catch { db incrblob t1 "A tricky column name %s%s" 1 } msg] $msg
685 } {1 {no such column: "A tricky column name %s%s"}}