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 #***********************************************************************
13 set testdir [file dirname $argv0]
14 source $testdir/tester.tcl
15 set testprefix e_blobwrite
17 #--------------------------------------------------------------------------
18 # EVIDENCE-OF: R-62898-22698 This function is used to write data into an
19 # open BLOB handle from a caller-supplied buffer. N bytes of data are
20 # copied from the buffer Z into the open BLOB, starting at offset
23 set dots [string repeat . 40]
25 CREATE TABLE t1(a INTEGER PRIMARY KEY, t TEXT);
26 INSERT INTO t1 VALUES(-1, $dots);
27 INSERT INTO t1 VALUES(-2, $dots);
28 INSERT INTO t1 VALUES(-3, $dots);
29 INSERT INTO t1 VALUES(-4, $dots);
30 INSERT INTO t1 VALUES(-5, $dots);
31 INSERT INTO t1 VALUES(-6, $dots);
34 proc blob_write_test {tn id iOffset blob nData final} {
35 sqlite3_blob_open db main t1 t $id 1 B
37 # EVIDENCE-OF: R-45864-01884 On success, sqlite3_blob_write() returns
38 # SQLITE_OK. Otherwise, an error code or an extended error code is
41 # This block tests the SQLITE_OK case in the requirement above (the
42 # Tcl sqlite3_blob_write() wrapper uses an empty string in place of
43 # "SQLITE_OK"). The error cases are tested by the "blob_write_error_test"
46 set res [sqlite3_blob_write $B $iOffset $blob $nData]
47 uplevel [list do_test $tn.1 [list set {} $res] {}]
50 uplevel [list do_execsql_test $tn.3 "SELECT t FROM t1 WHERE a=$id" $final]
53 set blob "0123456789012345678901234567890123456789"
54 blob_write_test 1.1 -1 0 $blob 10 { 0123456789.............................. }
55 blob_write_test 1.2 -2 8 $blob 10 { ........0123456789...................... }
56 blob_write_test 1.3 -3 8 $blob 1 { ........0............................... }
57 blob_write_test 1.4 -4 18 $blob 22 { ..................0123456789012345678901 }
58 blob_write_test 1.5 -5 18 $blob 0 { ........................................ }
59 blob_write_test 1.6 -6 0 $blob 40 { 0123456789012345678901234567890123456789 }
62 proc blob_write_error_test {tn B iOffset blob nData errcode errmsg} {
64 # In cases where the underlying sqlite3_blob_write() function returns
65 # SQLITE_OK, the Tcl wrapper returns an empty string. If the underlying
66 # function returns an error, the Tcl wrapper throws an exception with
67 # the error code as the Tcl exception message.
69 if {$errcode=="SQLITE_OK"} {
77 set cmd [list sqlite3_blob_write $B $iOffset $blob $nData]
78 uplevel [list do_test $tn.1 [subst -nocommands {
79 list [catch {$cmd} msg] [set msg]
80 }] [list $isError $ret]]
82 # EVIDENCE-OF: R-34782-18311 Unless SQLITE_MISUSE is returned, this
83 # function sets the database connection error code and message
84 # accessible via sqlite3_errcode() and sqlite3_errmsg() and related
87 if {$errcode == "SQLITE_MISUSE"} { error "test proc misuse!" }
88 uplevel [list do_test $tn.2 [list sqlite3_errcode db] $errcode]
89 uplevel [list do_test $tn.3 [list sqlite3_errmsg db] $errmsg]
93 CREATE TABLE t2(a TEXT, b INTEGER PRIMARY KEY);
94 INSERT INTO t2 VALUES($dots, 43);
95 INSERT INTO t2 VALUES($dots, 44);
96 INSERT INTO t2 VALUES($dots, 45);
99 # EVIDENCE-OF: R-63341-57517 If the BLOB handle passed as the first
100 # argument was not opened for writing (the flags parameter to
101 # sqlite3_blob_open() was zero), this function returns SQLITE_READONLY.
103 sqlite3_blob_open db main t2 a 43 0 B
104 blob_write_error_test 2.1 $B 0 $blob 10 \
105 SQLITE_READONLY {attempt to write a readonly database}
106 sqlite3_blob_close $B
108 # EVIDENCE-OF: R-29804-27366 If offset iOffset is less than N bytes from
109 # the end of the BLOB, SQLITE_ERROR is returned and no data is written.
111 sqlite3_blob_open db main t2 a 44 3 B
112 blob_write_error_test 2.2.1 $B 31 $blob 10 \
113 SQLITE_ERROR {SQL logic error}
115 # Make a successful write to the blob handle. This shows that the
116 # sqlite3_errcode() and sqlite3_errmsg() values are set even if the
117 # blob_write() call succeeds (see requirement in the [blob_write_error_test]
119 blob_write_error_test 2.2.1 $B 30 $blob 10 SQLITE_OK {not an error}
121 # EVIDENCE-OF: R-58570-38916 If N or iOffset are less than zero
122 # SQLITE_ERROR is returned and no data is written.
124 blob_write_error_test 2.2.2 $B 31 $blob -1 \
125 SQLITE_ERROR {SQL logic error}
126 blob_write_error_test 2.2.3 $B 20 $blob 10 SQLITE_OK {not an error}
127 blob_write_error_test 2.2.4 $B -1 $blob 10 \
128 SQLITE_ERROR {SQL logic error}
129 sqlite3_blob_close $B
131 # EVIDENCE-OF: R-20958-54138 An attempt to write to an expired BLOB
132 # handle fails with an error code of SQLITE_ABORT.
135 sqlite3_blob_open db main t2 a 43 0 B
136 execsql { DELETE FROM t2 WHERE b=43 }
138 blob_write_error_test 2.3.1 $B 5 $blob 5 \
139 SQLITE_ABORT {query aborted}
141 execsql { SELECT 1, 2, 3 }
144 blob_write_error_test 2.3.3 $B 5 $blob 5 \
145 SQLITE_ABORT {query aborted}
146 sqlite3_blob_close $B
148 # EVIDENCE-OF: R-08382-59936 Writes to the BLOB that occurred before the
149 # BLOB handle expired are not rolled back by the expiration of the
150 # handle, though of course those changes might have been overwritten by
151 # the statement that expired the BLOB handle or by other independent
154 # 3.1.*: not rolled back,
155 # 3.2.*: overwritten.
157 do_execsql_test 3.0 {
158 CREATE TABLE t3(i INTEGER PRIMARY KEY, j TEXT, k TEXT);
159 INSERT INTO t3 VALUES(1, $dots, $dots);
160 INSERT INTO t3 VALUES(2, $dots, $dots);
161 SELECT * FROM t3 WHERE i=1;
164 ........................................
165 ........................................
167 sqlite3_blob_open db main t3 j 1 1 B
168 blob_write_error_test 3.1.1 $B 5 $blob 10 SQLITE_OK {not an error}
169 do_execsql_test 3.1.2 {
170 UPDATE t3 SET k = 'xyz' WHERE i=1;
171 SELECT * FROM t3 WHERE i=1;
173 1 .....0123456789......................... xyz
175 blob_write_error_test 3.1.3 $B 15 $blob 10 \
176 SQLITE_ABORT {query aborted}
177 sqlite3_blob_close $B
178 do_execsql_test 3.1.4 {
179 SELECT * FROM t3 WHERE i=1;
181 1 .....0123456789......................... xyz
184 sqlite3_blob_open db main t3 j 2 1 B
185 blob_write_error_test 3.2.1 $B 5 $blob 10 SQLITE_OK {not an error}
186 do_execsql_test 3.2.2 {
187 UPDATE t3 SET j = 'xyz' WHERE i=2;
188 SELECT * FROM t3 WHERE i=2;
190 2 xyz ........................................
192 blob_write_error_test 3.2.3 $B 15 $blob 10 \
193 SQLITE_ABORT {query aborted}
194 sqlite3_blob_close $B
195 do_execsql_test 3.2.4 {
196 SELECT * FROM t3 WHERE i=2;
198 2 xyz ........................................