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 # Extra value added to size answers
131 if {$AutoVacuumMode} {incr ib2_extra}
132 if {[nonzero_reserved_bytes]} {incr ib2_extra}
134 do_test incrblob-2.$AutoVacuumMode.1 {
135 set ::str [string repeat abcdefghij 2900]
138 CREATE TABLE blobs(k PRIMARY KEY, v BLOB, i INTEGER);
140 INSERT INTO blobs VALUES('one', $::str || randstr(500,500), 45);
143 expr [file size test.db]/1024
144 } [expr 31 + $ib2_extra]
146 ifcapable autovacuum {
147 do_test incrblob-2.$AutoVacuumMode.2 {
154 do_test incrblob-2.$AutoVacuumMode.3 {
155 # Open and close the db to make sure the page cache is empty.
158 execsql "PRAGMA mmap_size = 0"
160 # Read the last 20 bytes of the blob via a blob handle.
161 set ::blob [db incrblob blobs v 1]
163 set ::fragment [read $::blob]
166 # If the database is not in auto-vacuum mode, the whole of
167 # the overflow-chain must be scanned. In auto-vacuum mode,
168 # sqlite uses the ptrmap pages to avoid reading the other pages.
171 } [expr $AutoVacuumMode ? 4 : 30+$ib2_extra]
173 do_test incrblob-2.$AutoVacuumMode.4 {
174 string range [db one {SELECT v FROM blobs}] end-19 end
177 do_test incrblob-2.$AutoVacuumMode.5 {
178 # Open and close the db to make sure the page cache is empty.
181 execsql "PRAGMA mmap_size = 0"
183 # Write the second-to-last 20 bytes of the blob via a blob handle.
185 set ::blob [db incrblob blobs v 1]
187 puts -nonewline $::blob "1234567890abcdefghij"
190 # If the database is not in auto-vacuum mode, the whole of
191 # the overflow-chain must be scanned. In auto-vacuum mode,
192 # sqlite uses the ptrmap pages to avoid reading the other pages.
195 } [expr $AutoVacuumMode ? 4 : 30 + $ib2_extra]
197 # Pages 1 (the write-counter) and 32 (the blob data) were written.
198 do_test incrblob-2.$AutoVacuumMode.6 {
203 do_test incrblob-2.$AutoVacuumMode.7 {
204 string range [db one {SELECT v FROM blobs}] end-39 end-20
205 } "1234567890abcdefghij"
207 do_test incrblob-2.$AutoVacuumMode.8 {
208 # Open and close the db to make sure the page cache is empty.
211 execsql { PRAGMA mmap_size = 0 }
213 execsql { SELECT i FROM blobs }
216 do_test incrblob-2.$AutoVacuumMode.9 {
218 } [expr $AutoVacuumMode ? 4 : 30 + $ib2_extra]
220 sqlite3_soft_heap_limit $cmdlinearg(soft-heap-limit)
222 #------------------------------------------------------------------------
225 # Test the outcome of trying to write to a read-only blob handle.
227 do_test incrblob-3.1 {
228 set ::blob [db incrblob -readonly blobs v 1]
231 } "1234567890abcdefghij"
232 do_test incrblob-3.2 {
235 puts -nonewline $::blob "helloworld"
239 } "1 {channel \"$::blob\" wasn't opened for writing}"
241 do_test incrblob-3.3 {
242 set ::blob [db incrblob -readonly blobs v 1]
245 } "1234567890abcdefghij"
246 do_test incrblob-3.4 {
248 sqlite3_blob_write $::blob 20 "qwertyuioplkjhgfds"
251 } {1 SQLITE_READONLY}
252 catch {close $::blob}
254 #------------------------------------------------------------------------
257 # Try a couple of error conditions:
259 # 4.1 - Attempt to open a row that does not exist.
260 # 4.2 - Attempt to open a column that does not exist.
261 # 4.3 - Attempt to open a table that does not exist.
262 # 4.4 - Attempt to open a database that does not exist.
264 # 4.5 - Attempt to open an integer
265 # 4.6 - Attempt to open a real value
266 # 4.7 - Attempt to open an SQL null
268 # 4.8 - Attempt to open an indexed column for writing
269 # 4.9 - Attempt to open an indexed column for reading (this works)
271 # 4.11 - Attempt to open a column of a view.
272 # 4.12 - Attempt to open a column of a virtual table.
274 do_test incrblob-4.1 {
276 set ::blob [db incrblob blobs v 2]
279 } {1 {no such rowid: 2}}
280 do_test incrblob-4.2 {
282 set ::blob [db incrblob blobs blue 1]
285 } {1 {no such column: "blue"}}
286 do_test incrblob-4.3 {
288 set ::blob [db incrblob nosuchtable blue 1]
291 } {1 {no such table: main.nosuchtable}}
292 do_test incrblob-4.4 {
294 set ::blob [db incrblob nosuchdb blobs v 1]
297 } {1 {no such table: nosuchdb.blobs}}
299 do_test incrblob-4.5 {
301 set ::blob [db incrblob blobs i 1]
304 } {1 {cannot open value of type integer}}
305 do_test incrblob-4.6 {
307 INSERT INTO blobs(k, v, i) VALUES(123, 567.765, NULL);
310 set ::blob [db incrblob blobs v 2]
313 } {1 {cannot open value of type real}}
314 do_test incrblob-4.7 {
316 set ::blob [db incrblob blobs i 2]
319 } {1 {cannot open value of type null}}
321 do_test incrblob-4.8.1 {
323 INSERT INTO blobs(k, v, i) VALUES(X'010203040506070809', 'hello', 'world');
326 set ::blob [db incrblob blobs k 3]
329 } {1 {cannot open indexed column for writing}}
330 do_test incrblob-4.8.2 {
332 CREATE TABLE t3(a INTEGER PRIMARY KEY, b);
333 INSERT INTO t3 VALUES(1, 2);
336 set ::blob [db incrblob -readonly t3 a 1]
339 } {1 {cannot open value of type null}}
340 do_test incrblob-4.8.3 {
342 set ::blob [db incrblob -readonly t3 rowid 1]
345 } {1 {no such column: "rowid"}}
347 do_test incrblob-4.9.1 {
349 set ::blob [db incrblob -readonly blobs k 3]
352 do_test incrblob-4.9.2 {
353 binary scan [read $::blob] c* c
356 } {1 2 3 4 5 6 7 8 9}
358 do_test incrblob-4.10 {
359 set ::blob [db incrblob -readonly blobs k 3]
360 set rc [catch { sqlite3_blob_read $::blob 10 100 } msg]
363 do_test incrblob-4.10.2 {
368 do_test incrblob-4.11 {
369 execsql { CREATE VIEW blobs_view AS SELECT k, v, i FROM blobs }
370 set rc [catch { db incrblob blobs_view v 3 } msg]
372 } {1 {cannot open view: blobs_view}}
375 register_echo_module [sqlite3_connection_pointer db]
376 do_test incrblob-4.12 {
377 execsql { CREATE VIRTUAL TABLE blobs_echo USING echo(blobs) }
378 set rc [catch { db incrblob blobs_echo v 3 } msg]
380 } {1 {cannot open virtual table: blobs_echo}}
384 #------------------------------------------------------------------------
387 # Test that opening a blob in an attached database works.
390 do_test incrblob-5.1 {
391 forcedelete test2.db test2.db-journal
392 set ::size [expr [file size $::cmdlinearg(INFO_SCRIPT)]]
394 ATTACH 'test2.db' AS aux;
395 CREATE TABLE aux.files(name, text);
396 INSERT INTO aux.files VALUES('this one', zeroblob($::size));
398 set fd [db incrblob aux files text 1]
399 fconfigure $fd -translation binary
400 set fd2 [open $::cmdlinearg(INFO_SCRIPT)]
401 fconfigure $fd2 -translation binary
402 puts -nonewline $fd [read $fd2]
405 set ::text [db one {select text from aux.files}]
406 string length $::text
407 } [file size $::cmdlinearg(INFO_SCRIPT)]
408 do_test incrblob-5.2 {
409 set fd2 [open $::cmdlinearg(INFO_SCRIPT)]
410 fconfigure $fd2 -translation binary
411 set ::data [read $fd2]
418 unset -nocomplain ::data
419 unset -nocomplain ::text
421 #------------------------------------------------------------------------
424 # Test that opening a blob for write-access is impossible if
425 # another connection has the database RESERVED lock.
427 # Then test that blob writes that take place inside of a
428 # transaction are not visible to external connections until
429 # after the transaction is commited and the blob channel
432 # This test does not work with the "memsubsys1" configuration.
433 # Permutation memsubsys1 configures a very small static allocation
434 # for use as page-cache memory. This causes SQLite to upgrade
435 # to an exclusive lock when writing earlier than usual, which
436 # makes some of these tests fail.
438 sqlite3_soft_heap_limit 0
439 if {[permutation] != "memsubsys1"} {
440 do_test incrblob-6.1 {
444 INSERT INTO blobs(k, v, i) VALUES('a', 'different', 'connection');
447 do_test incrblob-6.2 {
449 SELECT rowid FROM blobs ORDER BY rowid
452 do_test incrblob-6.3 {
454 db incrblob blobs v 1
457 } {1 {database is locked}}
458 do_test incrblob-6.4 {
460 db incrblob blobs v 3
463 } {1 {database is locked}}
464 do_test incrblob-6.5 {
465 set ::blob [db incrblob -readonly blobs v 3]
468 do_test incrblob-6.6 {
472 do_test incrblob-6.7 {
473 set ::blob [db2 incrblob blobs i 4]
476 do_test incrblob-6.8 {
479 do_test incrblob-6.9 {
481 puts -nonewline $::blob "invocation"
485 # At this point commit should be illegal (because
486 # there is an open blob channel).
488 do_test incrblob-6.11 {
492 } {1 {cannot commit transaction - SQL statements in progress}}
494 do_test incrblob-6.12 {
496 SELECT * FROM blobs WHERE rowid = 4;
499 do_test incrblob-6.13 {
502 do_test incrblob-6.14 {
507 do_test incrblob-6.15 {
509 SELECT * FROM blobs WHERE rowid = 4;
511 } {a different invocation}
514 sqlite3_soft_heap_limit $cmdlinearg(soft-heap-limit)
516 #-----------------------------------------------------------------------
517 # The following tests verify the behavior of the incremental IO
518 # APIs in the following cases:
520 # 7.1 A row that containing an open blob is modified.
522 # 7.2 A CREATE TABLE requires that an overflow page that is part
523 # of an open blob is moved.
525 # 7.3 An INCREMENTAL VACUUM moves an overflow page that is part
528 # In the first case above, correct behavior is for all subsequent
529 # read/write operations on the blob-handle to return SQLITE_ABORT.
530 # More accurately, blob-handles are invalidated whenever the table
531 # they belong to is written to.
533 # The second two cases have no external effect. They are testing
534 # that the internal cache of overflow page numbers is correctly
537 do_test incrblob-7.1.0 {
541 CREATE TABLE t1 (a, b, c, d BLOB);
542 INSERT INTO t1(a, b, c, d) VALUES(1, 2, 3, 4);
547 foreach {tn arg} {1 "" 2 -readonly} {
550 UPDATE t1 SET d = zeroblob(10000);
553 do_test incrblob-7.1.$tn.1 {
554 set ::b [eval db incrblob $arg t1 d 1]
555 binary scan [sqlite3_blob_read $::b 5000 5] c* c
558 do_test incrblob-7.1.$tn.2 {
560 UPDATE t1 SET d = 15;
563 do_test incrblob-7.1.$tn.3 {
564 set rc [catch { sqlite3_blob_read $::b 5000 5 } msg]
567 do_test incrblob-7.1.$tn.4 {
572 do_test incrblob-7.1.$tn.5 {
573 set rc [catch { close $::b } msg]
576 do_test incrblob-7.1.$tn.6 {
584 set fd [open $::cmdlinearg(INFO_SCRIPT)]
585 fconfigure $fd -translation binary
586 set ::data [read $fd 14000]
590 forcedelete test.db test.db-journal
593 do_test incrblob-7.2.1 {
595 PRAGMA auto_vacuum = "incremental";
596 CREATE TABLE t1(a INTEGER PRIMARY KEY, b); -- root@page3
597 INSERT INTO t1 VALUES(123, $::data);
599 set ::b [db incrblob -readonly t1 b 123]
600 fconfigure $::b -translation binary
603 do_test incrblob-7.2.2 {
605 CREATE TABLE t2(a INTEGER PRIMARY KEY, b); -- root@page4
610 do_test incrblob-7.2.3 {
613 SELECT rootpage FROM sqlite_master;
617 set ::otherdata "[string range $::data 0 1000][string range $::data 1001 end]"
618 do_test incrblob-7.3.1 {
620 INSERT INTO t2 VALUES(456, $::otherdata);
622 set ::b [db incrblob -readonly t2 b 456]
623 fconfigure $::b -translation binary
626 do_test incrblob-7.3.2 {
627 expr [file size test.db]/1024
629 do_test incrblob-7.3.3 {
631 DELETE FROM t1 WHERE a = 123;
632 PRAGMA INCREMENTAL_VACUUM(0);
638 # Attempt to write on a read-only blob. Make sure the error code
639 # gets set. Ticket #2464.
641 do_test incrblob-7.4 {
642 set rc [catch {sqlite3_blob_write $::b 10 HELLO} msg]
644 } {1 SQLITE_READONLY}
645 do_test incrblob-7.5 {
648 do_test incrblob-7.6 {
650 } {attempt to write a readonly database}
652 # Test that if either the "offset" or "amount" arguments to
653 # sqlite3_blob_write() are less than zero, SQLITE_ERROR is returned.
655 do_test incrblob-8.1 {
656 execsql { INSERT INTO t1 VALUES(314159, 'sqlite') }
657 set ::b [db incrblob t1 b 314159]
658 fconfigure $::b -translation binary
659 set rc [catch {sqlite3_blob_write $::b 10 HELLO -1} msg]
662 do_test incrblob-8.2 {
665 do_test incrblob-8.3 {
666 set rc [catch {sqlite3_blob_write $::b -1 HELLO 5} msg]
669 do_test incrblob-8.4 {
672 do_test incrblob-8.5 {
673 execsql {SELECT b FROM t1 WHERE a = 314159}
675 do_test incrblob-8.6 {
676 set rc [catch {sqlite3_blob_write $::b 0 etilqs 6} msg]
679 do_test incrblob-8.7 {
680 execsql {SELECT b FROM t1 WHERE a = 314159}
683 # The following test case exposes an instance in the blob code where
684 # an error message was set using a call similar to sqlite3_mprintf(zErr),
685 # where zErr is an arbitrary string. This is no good if the string contains
686 # characters that can be mistaken for printf() formatting directives.
688 do_test incrblob-9.1 {
689 list [catch { db incrblob t1 "A tricky column name %s%s" 1 } msg] $msg
690 } {1 {no such column: "A tricky column name %s%s"}}