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