000001 # 2014 October 30
000002 #
000003 # The author disclaims copyright to this source code. In place of
000004 # a legal notice, here is a blessing:
000005 #
000006 # May you do good and not evil.
000007 # May you find forgiveness for yourself and forgive others.
000008 # May you share freely, never taking more than you give.
000009 #
000010 #***********************************************************************
000011 #
000012
000013 set testdir [file dirname $argv0]
000014 source $testdir/tester.tcl
000015 set testprefix e_blobwrite
000016
000017 ifcapable !incrblob {
000018 finish_test
000019 return
000020 }
000021
000022 #--------------------------------------------------------------------------
000023 # EVIDENCE-OF: R-62898-22698 This function is used to write data into an
000024 # open BLOB handle from a caller-supplied buffer. N bytes of data are
000025 # copied from the buffer Z into the open BLOB, starting at offset
000026 # iOffset.
000027 #
000028 set dots [string repeat . 40]
000029 do_execsql_test 1.0 {
000030 CREATE TABLE t1(a INTEGER PRIMARY KEY, t TEXT);
000031 INSERT INTO t1 VALUES(-1, $dots);
000032 INSERT INTO t1 VALUES(-2, $dots);
000033 INSERT INTO t1 VALUES(-3, $dots);
000034 INSERT INTO t1 VALUES(-4, $dots);
000035 INSERT INTO t1 VALUES(-5, $dots);
000036 INSERT INTO t1 VALUES(-6, $dots);
000037 }
000038
000039 proc blob_write_test {tn id iOffset blob nData final} {
000040 sqlite3_blob_open db main t1 t $id 1 B
000041
000042 # EVIDENCE-OF: R-45864-01884 On success, sqlite3_blob_write() returns
000043 # SQLITE_OK. Otherwise, an error code or an extended error code is
000044 # returned.
000045 #
000046 # This block tests the SQLITE_OK case in the requirement above (the
000047 # Tcl sqlite3_blob_write() wrapper uses an empty string in place of
000048 # "SQLITE_OK"). The error cases are tested by the "blob_write_error_test"
000049 # tests below.
000050 #
000051 set res [sqlite3_blob_write $B $iOffset $blob $nData]
000052 uplevel [list do_test $tn.1 [list set {} $res] {}]
000053
000054 sqlite3_blob_close $B
000055 uplevel [list do_execsql_test $tn.3 "SELECT t FROM t1 WHERE a=$id" $final]
000056 }
000057
000058 set blob "0123456789012345678901234567890123456789"
000059 blob_write_test 1.1 -1 0 $blob 10 { 0123456789.............................. }
000060 blob_write_test 1.2 -2 8 $blob 10 { ........0123456789...................... }
000061 blob_write_test 1.3 -3 8 $blob 1 { ........0............................... }
000062 blob_write_test 1.4 -4 18 $blob 22 { ..................0123456789012345678901 }
000063 blob_write_test 1.5 -5 18 $blob 0 { ........................................ }
000064 blob_write_test 1.6 -6 0 $blob 40 { 0123456789012345678901234567890123456789 }
000065
000066
000067 proc blob_write_error_test {tn B iOffset blob nData errcode errmsg} {
000068
000069 # In cases where the underlying sqlite3_blob_write() function returns
000070 # SQLITE_OK, the Tcl wrapper returns an empty string. If the underlying
000071 # function returns an error, the Tcl wrapper throws an exception with
000072 # the error code as the Tcl exception message.
000073 #
000074 if {$errcode=="SQLITE_OK"} {
000075 set ret ""
000076 set isError 0
000077 } else {
000078 set ret $errcode
000079 set isError 1
000080 }
000081
000082 set cmd [list sqlite3_blob_write $B $iOffset $blob $nData]
000083 uplevel [list do_test $tn.1 [subst -nocommands {
000084 list [catch {$cmd} msg] [set msg]
000085 }] [list $isError $ret]]
000086
000087 # EVIDENCE-OF: R-34782-18311 Unless SQLITE_MISUSE is returned, this
000088 # function sets the database connection error code and message
000089 # accessible via sqlite3_errcode() and sqlite3_errmsg() and related
000090 # functions.
000091 #
000092 if {$errcode == "SQLITE_MISUSE"} { error "test proc misuse!" }
000093 uplevel [list do_test $tn.2 [list sqlite3_errcode db] $errcode]
000094 uplevel [list do_test $tn.3 [list sqlite3_errmsg db] $errmsg]
000095 }
000096
000097 do_execsql_test 2.0 {
000098 CREATE TABLE t2(a TEXT, b INTEGER PRIMARY KEY);
000099 INSERT INTO t2 VALUES($dots, 43);
000100 INSERT INTO t2 VALUES($dots, 44);
000101 INSERT INTO t2 VALUES($dots, 45);
000102 }
000103
000104 # EVIDENCE-OF: R-63341-57517 If the BLOB handle passed as the first
000105 # argument was not opened for writing (the flags parameter to
000106 # sqlite3_blob_open() was zero), this function returns SQLITE_READONLY.
000107 #
000108 sqlite3_blob_open db main t2 a 43 0 B
000109 blob_write_error_test 2.1 $B 0 $blob 10 \
000110 SQLITE_READONLY {attempt to write a readonly database}
000111 sqlite3_blob_close $B
000112
000113 # EVIDENCE-OF: R-29804-27366 If offset iOffset is less than N bytes from
000114 # the end of the BLOB, SQLITE_ERROR is returned and no data is written.
000115 #
000116 sqlite3_blob_open db main t2 a 44 3 B
000117 blob_write_error_test 2.2.1 $B 31 $blob 10 \
000118 SQLITE_ERROR {SQL logic error}
000119
000120 # Make a successful write to the blob handle. This shows that the
000121 # sqlite3_errcode() and sqlite3_errmsg() values are set even if the
000122 # blob_write() call succeeds (see requirement in the [blob_write_error_test]
000123 # proc).
000124 blob_write_error_test 2.2.1 $B 30 $blob 10 SQLITE_OK {not an error}
000125
000126 # EVIDENCE-OF: R-58570-38916 If N or iOffset are less than zero
000127 # SQLITE_ERROR is returned and no data is written.
000128 #
000129 blob_write_error_test 2.2.2 $B 31 $blob -1 \
000130 SQLITE_ERROR {SQL logic error}
000131 blob_write_error_test 2.2.3 $B 20 $blob 10 SQLITE_OK {not an error}
000132 blob_write_error_test 2.2.4 $B -1 $blob 10 \
000133 SQLITE_ERROR {SQL logic error}
000134 sqlite3_blob_close $B
000135
000136 # EVIDENCE-OF: R-20958-54138 An attempt to write to an expired BLOB
000137 # handle fails with an error code of SQLITE_ABORT.
000138 #
000139 do_test 2.3 {
000140 sqlite3_blob_open db main t2 a 43 0 B
000141 execsql { DELETE FROM t2 WHERE b=43 }
000142 } {}
000143 blob_write_error_test 2.3.1 $B 5 $blob 5 \
000144 SQLITE_ABORT {query aborted}
000145 do_test 2.3.2 {
000146 execsql { SELECT 1, 2, 3 }
000147 sqlite3_errcode db
000148 } {SQLITE_OK}
000149 blob_write_error_test 2.3.3 $B 5 $blob 5 \
000150 SQLITE_ABORT {query aborted}
000151 sqlite3_blob_close $B
000152
000153 # EVIDENCE-OF: R-08382-59936 Writes to the BLOB that occurred before the
000154 # BLOB handle expired are not rolled back by the expiration of the
000155 # handle, though of course those changes might have been overwritten by
000156 # the statement that expired the BLOB handle or by other independent
000157 # statements.
000158 #
000159 # 3.1.*: not rolled back,
000160 # 3.2.*: overwritten.
000161 #
000162 do_execsql_test 3.0 {
000163 CREATE TABLE t3(i INTEGER PRIMARY KEY, j TEXT, k TEXT);
000164 INSERT INTO t3 VALUES(1, $dots, $dots);
000165 INSERT INTO t3 VALUES(2, $dots, $dots);
000166 SELECT * FROM t3 WHERE i=1;
000167 } {
000168 1
000169 ........................................
000170 ........................................
000171 }
000172 sqlite3_blob_open db main t3 j 1 1 B
000173 blob_write_error_test 3.1.1 $B 5 $blob 10 SQLITE_OK {not an error}
000174 do_execsql_test 3.1.2 {
000175 UPDATE t3 SET k = 'xyz' WHERE i=1;
000176 SELECT * FROM t3 WHERE i=1;
000177 } {
000178 1 .....0123456789......................... xyz
000179 }
000180 blob_write_error_test 3.1.3 $B 15 $blob 10 \
000181 SQLITE_ABORT {query aborted}
000182 sqlite3_blob_close $B
000183 do_execsql_test 3.1.4 {
000184 SELECT * FROM t3 WHERE i=1;
000185 } {
000186 1 .....0123456789......................... xyz
000187 }
000188
000189 sqlite3_blob_open db main t3 j 2 1 B
000190 blob_write_error_test 3.2.1 $B 5 $blob 10 SQLITE_OK {not an error}
000191 do_execsql_test 3.2.2 {
000192 UPDATE t3 SET j = 'xyz' WHERE i=2;
000193 SELECT * FROM t3 WHERE i=2;
000194 } {
000195 2 xyz ........................................
000196 }
000197 blob_write_error_test 3.2.3 $B 15 $blob 10 \
000198 SQLITE_ABORT {query aborted}
000199 sqlite3_blob_close $B
000200 do_execsql_test 3.2.4 {
000201 SELECT * FROM t3 WHERE i=2;
000202 } {
000203 2 xyz ........................................
000204 }
000205
000206
000207
000208 finish_test