blob: ff88ba706dcd5b2dca5b2742ebf63355bca2bb26 [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#
danielk1977f1819242007-05-03 18:14:10 +000012# $Id: incrblob.test,v 1.6 2007/05/03 18:14:10 danielk1977 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
18do_test incrblob-1.1 {
19 execsql {
20 CREATE TABLE blobs(k PRIMARY KEY, v BLOB);
21 INSERT INTO blobs VALUES('one', X'0102030405060708090A');
22 INSERT INTO blobs VALUES('two', X'0A090807060504030201');
23 }
24} {}
25
26do_test incrblob-1.2.1 {
27 set ::blob [db incrblob blobs v 1]
28} {incrblob_1}
29do_test incrblob-1.2.2 {
30 binary scan [read $::blob] c* data
31 set data
32} {1 2 3 4 5 6 7 8 9 10}
33do_test incrblob-1.2.3 {
34 seek $::blob 0
35 puts -nonewline $::blob "1234567890"
36 flush $::blob
37} {}
38do_test incrblob-1.2.4 {
39 seek $::blob 0
40 binary scan [read $::blob] c* data
41 set data
42} {49 50 51 52 53 54 55 56 57 48}
43do_test incrblob-1.2.5 {
44 close $::blob
45} {}
46do_test incrblob-1.2.6 {
47 execsql {
48 SELECT v FROM blobs WHERE rowid = 1;
49 }
50} {1234567890}
51
danielk1977d04417962007-05-02 13:16:30 +000052#--------------------------------------------------------------------
danielk197720713f32007-05-03 11:43:33 +000053# Test cases incrblob-1.3.X check that it is possible to read and write
danielk1977d04417962007-05-02 13:16:30 +000054# regions of a blob that lie on overflow pages.
danielk197720713f32007-05-03 11:43:33 +000055#
56do_test incrblob-1.3.1 {
danielk1977d04417962007-05-02 13:16:30 +000057 set ::str "[string repeat . 10000]"
58 execsql {
59 INSERT INTO blobs(rowid, k, v) VALUES(3, 'three', $::str);
60 }
61} {}
danielk1977b4e9af92007-05-01 17:49:49 +000062
danielk197720713f32007-05-03 11:43:33 +000063do_test incrblob-1.3.2 {
danielk1977d04417962007-05-02 13:16:30 +000064 set ::blob [db incrblob blobs v 3]
65 seek $::blob 8500
66 read $::blob 10
67} {..........}
danielk197720713f32007-05-03 11:43:33 +000068do_test incrblob-1.3.3 {
danielk1977d04417962007-05-02 13:16:30 +000069 seek $::blob 8500
70 puts -nonewline $::blob 1234567890
71} {}
danielk197720713f32007-05-03 11:43:33 +000072do_test incrblob-1.3.4 {
danielk1977d04417962007-05-02 13:16:30 +000073 seek $::blob 8496
74 read $::blob 10
75} {....123456}
danielk197720713f32007-05-03 11:43:33 +000076do_test incrblob-1.3.10 {
danielk1977d04417962007-05-02 13:16:30 +000077 close $::blob
78} {}
79
danielk1977b4e9af92007-05-01 17:49:49 +000080
danielk197720713f32007-05-03 11:43:33 +000081#------------------------------------------------------------------------
danielk19778cbadb02007-05-03 16:31:26 +000082# incrblob-2.*:
83#
84# Test that the following operations use ptrmap pages to reduce
85# unnecessary reads:
danielk197744e6c8d2007-05-03 13:11:32 +000086#
87# * Reading near the end of a blob,
danielk19778cbadb02007-05-03 16:31:26 +000088# * Writing near the end of a blob, and
89# * SELECT a column value that is located on an overflow page.
danielk197720713f32007-05-03 11:43:33 +000090#
91proc nRead {db} {
92 set bt [btree_from_db $db]
93 array set stats [btree_pager_stats $bt]
94 return $stats(read)
95}
danielk19778cbadb02007-05-03 16:31:26 +000096proc nWrite {db} {
97 set bt [btree_from_db $db]
98 array set stats [btree_pager_stats $bt]
99 return $stats(write)
100}
danielk197720713f32007-05-03 11:43:33 +0000101
102foreach AutoVacuumMode [list 0 1] {
103
104 db close
105 file delete -force test.db test.db-journal
106
107 sqlite3 db test.db
108 execsql "PRAGMA auto_vacuum = $AutoVacuumMode"
109
110 do_test incrblob-2.$AutoVacuumMode.1 {
111 set ::str [string repeat abcdefghij 2900]
112 execsql {
113 BEGIN;
danielk19778cbadb02007-05-03 16:31:26 +0000114 CREATE TABLE blobs(k PRIMARY KEY, v BLOB, i INTEGER);
danielk197720713f32007-05-03 11:43:33 +0000115 DELETE FROM blobs;
danielk19778cbadb02007-05-03 16:31:26 +0000116 INSERT INTO blobs VALUES('one', $::str || randstr(500,500), 45);
danielk197720713f32007-05-03 11:43:33 +0000117 COMMIT;
118 }
119 expr [file size test.db]/1024
120 } [expr 31 + $AutoVacuumMode]
121
122 do_test incrblob-2.$AutoVacuumMode.2 {
123 execsql {
124 PRAGMA auto_vacuum;
125 }
126 } $AutoVacuumMode
127
128 do_test incrblob-2.$AutoVacuumMode.3 {
129 # Open and close the db to make sure the page cache is empty.
130 db close
131 sqlite3 db test.db
132
133 # Read the last 20 bytes of the blob via a blob handle.
134 set ::blob [db incrblob blobs v 1]
135 seek $::blob -20 end
136 set ::fragment [read $::blob]
137 close $::blob
138
139 # If the database is not in auto-vacuum mode, the whole of
140 # the overflow-chain must be scanned. In auto-vacuum mode,
141 # sqlite uses the ptrmap pages to avoid reading the other pages.
142 #
143 nRead db
144 } [expr $AutoVacuumMode ? 4 : 30]
145
danielk19778cbadb02007-05-03 16:31:26 +0000146 do_test incrblob-2.$AutoVacuumMode.4 {
danielk197720713f32007-05-03 11:43:33 +0000147 string range [db one {SELECT v FROM blobs}] end-19 end
148 } $::fragment
danielk19778cbadb02007-05-03 16:31:26 +0000149
150 do_test incrblob-2.$AutoVacuumMode.5 {
151 # Open and close the db to make sure the page cache is empty.
152 db close
153 sqlite3 db test.db
154
155 # Write the second-to-last 20 bytes of the blob via a blob handle.
156 #
157 set ::blob [db incrblob blobs v 1]
158 seek $::blob -40 end
159 puts -nonewline $::blob "1234567890abcdefghij"
160 flush $::blob
161
162 # If the database is not in auto-vacuum mode, the whole of
163 # the overflow-chain must be scanned. In auto-vacuum mode,
164 # sqlite uses the ptrmap pages to avoid reading the other pages.
165 #
166 nRead db
167 } [expr $AutoVacuumMode ? 4 : 30]
168
169 # Pages 1 (the write-counter) and 32 (the blob data) were written.
170 do_test incrblob-2.$AutoVacuumMode.6 {
171 close $::blob
172 nWrite db
173 } 2
174
175 do_test incrblob-2.$AutoVacuumMode.7 {
176 string range [db one {SELECT v FROM blobs}] end-39 end-20
177 } "1234567890abcdefghij"
178
179 do_test incrblob-2.$AutoVacuumMode.8 {
180 # Open and close the db to make sure the page cache is empty.
181 db close
182 sqlite3 db test.db
183
184 execsql { SELECT i FROM blobs }
185 } {45}
186
187 do_test incrblob-2.$AutoVacuumMode.9 {
188 nRead db
189 } [expr $AutoVacuumMode ? 4 : 30]
danielk197720713f32007-05-03 11:43:33 +0000190}
191
danielk19778cbadb02007-05-03 16:31:26 +0000192#------------------------------------------------------------------------
193# incrblob-3.*:
194#
195# Test the outcome of trying to write to a read-only blob handle.
196#
197# TODO: The following test only tests the tcl interface, not the
198# underlying sqlite3 interface. Need to find some other method
199# to call sqlite3_blob_write() on a readonly handle...
200#
201do_test incrblob-3.1 {
202 set ::blob [db incrblob -readonly blobs v 1]
203 seek $::blob -40 end
204 read $::blob 20
205} "1234567890abcdefghij"
206do_test incrblob-3.2 {
207 seek $::blob 0
208 set rc [catch {
209 puts -nonewline $::blob "helloworld"
210 } msg]
danielk1977f1819242007-05-03 18:14:10 +0000211 close $::blob
danielk19778cbadb02007-05-03 16:31:26 +0000212 list $rc $msg
213} "1 {channel \"$::blob\" wasn't opened for writing}"
214
215#------------------------------------------------------------------------
216# incrblob-4.*:
217#
218# Try a couple of error conditions:
219#
220# 4.1 - Attempt to open a row that does not exist.
221# 4.2 - Attempt to open a column that does not exist.
222# 4.3 - Attempt to open a table that does not exist.
223# 4.4 - Attempt to open a database that does not exist.
224#
danielk1977f1819242007-05-03 18:14:10 +0000225# 4.5 - Attempt to open an integer
226# 4.6 - Attempt to open a real value
227# 4.7 - Attempt to open an SQL null
228#
229# 4.8 - Attempt to open an indexed column for writing
230# 4.9 - Attempt to open an indexed column for reading (this works)
231#
danielk19778cbadb02007-05-03 16:31:26 +0000232do_test incrblob-4.1 {
233 set rc [catch {
234 set ::blob [db incrblob blobs v 2]
235 } msg ]
236 list $rc $msg
237} {1 {no such rowid: 2}}
danielk19778cbadb02007-05-03 16:31:26 +0000238do_test incrblob-4.2 {
239 set rc [catch {
240 set ::blob [db incrblob blobs blue 1]
241 } msg ]
242 list $rc $msg
243} {1 {no such column: "blue"}}
danielk19778cbadb02007-05-03 16:31:26 +0000244do_test incrblob-4.3 {
245 set rc [catch {
246 set ::blob [db incrblob nosuchtable blue 1]
danielk1977f1819242007-05-03 18:14:10 +0000247 } msg ]
danielk19778cbadb02007-05-03 16:31:26 +0000248 list $rc $msg
249} {1 {no such table: main.nosuchtable}}
danielk19778cbadb02007-05-03 16:31:26 +0000250do_test incrblob-4.4 {
251 set rc [catch {
252 set ::blob [db incrblob nosuchdb blobs v 1]
253 } msg ]
254 list $rc $msg
255} {1 {no such table: nosuchdb.blobs}}
256
danielk1977f1819242007-05-03 18:14:10 +0000257do_test incrblob-4.5 {
258 set rc [catch {
259 set ::blob [db incrblob blobs i 1]
260 } msg ]
261 list $rc $msg
262} {1 {cannot open value of type integer}}
263do_test incrblob-4.6 {
264 execsql {
265 INSERT INTO blobs(k, v, i) VALUES(123, 567.765, NULL);
266 }
267 set rc [catch {
268 set ::blob [db incrblob blobs v 2]
269 } msg ]
270 list $rc $msg
271} {1 {cannot open value of type real}}
272do_test incrblob-4.7 {
273 set rc [catch {
274 set ::blob [db incrblob blobs i 2]
275 } msg ]
276 list $rc $msg
277} {1 {cannot open value of type null}}
danielk19778cbadb02007-05-03 16:31:26 +0000278
danielk1977f1819242007-05-03 18:14:10 +0000279do_test incrblob-4.8 {
280 execsql {
281 INSERT INTO blobs(k, v, i) VALUES(X'010203040506070809', 'hello', 'world');
282 }
283 set rc [catch {
284 set ::blob [db incrblob blobs k 3]
285 } msg ]
286 list $rc $msg
287} {1 {cannot open indexed column for writing}}
288
289do_test incrblob-4.9.1 {
290 set rc [catch {
291 set ::blob [db incrblob -readonly blobs k 3]
292 } msg]
293} {0}
294do_test incrblob-4.9.2 {
295 binary scan [read $::blob] c* c
296 close $::blob
297 set c
298} {1 2 3 4 5 6 7 8 9}
299
300#------------------------------------------------------------------------
301# incrblob-5.*:
302#
303# Test that opening a blob in an attached database works.
304#
305do_test incrblob-5.1 {
306 file delete -force test2.db test2.db-journal
307 set ::size [expr [file size [info script]]]
308 execsql {
309 ATTACH 'test2.db' AS aux;
310 CREATE TABLE aux.files(name, text);
311 INSERT INTO aux.files VALUES('this one', zeroblob($::size));
312 }
313 set fd [db incrblob aux files text 1]
314 set fd2 [open [info script]]
315 puts -nonewline $fd [read $fd2]
316 close $fd
317 close $fd2
318 set ::text [db one {select text from aux.files}]
319 string length $::text
320} [file size [info script]]
321do_test incrblob-5.2 {
322 set fd2 [open [info script]]
323 set ::data [read $fd2]
324 close $fd2
325 set ::data
326} $::text
327
328# free memory
329unset ::data
330unset ::text
331
332#------------------------------------------------------------------------
333# incrblob-6.*:
334#
335# Test that opening a blob for write-access is impossible if
336# another connection has the database RESERVED lock.
337#
338# Then test that blob writes that take place inside of a
339# transaction are not visible to external connections until
340# after the transaction is commited and the blob channel
341# closed.
342#
343do_test incrblob-6.1 {
344 sqlite3 db2 test.db
345 execsql {
346 BEGIN;
347 INSERT INTO blobs(k, v, i) VALUES('a', 'different', 'connection');
348 } db2
349} {}
350do_test incrblob-6.2 {
351 execsql {
352 SELECT rowid FROM blobs
353 }
354} {1 2 3}
355do_test incrblob-6.3 {
356 set rc [catch {
357 db incrblob blobs v 1
358 } msg]
359 list $rc $msg
360} {1 {database is locked}}
361do_test incrblob-6.4 {
362 set rc [catch {
363 db incrblob blobs v 3
364 } msg]
365 list $rc $msg
366} {1 {database is locked}}
367do_test incrblob-6.5 {
368 set ::blob [db incrblob -readonly blobs v 3]
369 read $::blob
370} {hello}
371do_test incrblob-6.6 {
372 close $::blob
373} {}
374
375do_test incrblob-6.7 {
376 set ::blob [db2 incrblob blobs i 4]
377 gets $::blob
378} {connection}
379do_test incrblob-6.8 {
380 tell $::blob
381} {10}
382breakpoint
383do_test incrblob-6.9 {
384 seek $::blob 0
385 puts -nonewline $::blob "invocation"
386 flush $::blob
387} {}
388
389# At this point rollback or commit should be illegal (because
390# there is an open blob channel).
391do_test incrblob-6.10 {
392 catchsql {
393 ROLLBACK;
394 } db2
395} {1 {cannot rollback transaction - SQL statements in progress}}
396do_test incrblob-6.11 {
397 catchsql {
398 COMMIT;
399 } db2
400} {1 {cannot commit transaction - SQL statements in progress}}
401
402do_test incrblob-6.12 {
403 execsql {
404 SELECT * FROM blobs WHERE rowid = 4;
405 }
406} {}
407do_test incrblob-6.13 {
408 close $::blob
409 execsql {
410 COMMIT;
411 } db2
412} {}
413do_test incrblob-6.14 {
414 execsql {
415 SELECT * FROM blobs WHERE rowid = 4;
416 }
417} {a different invocation}
418db2 close
419
420finish_test