AryaWu/sqlite
0
1# 2014 October 302#3# The author disclaims copyright to this source code. In place of4# 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#12 13set testdir [file dirname $argv0]14source $testdir/tester.tcl15set testprefix e_blobwrite16 17ifcapable !incrblob {18 finish_test19 return20}21 22#--------------------------------------------------------------------------23# EVIDENCE-OF: R-62898-22698 This function is used to write data into an24# open BLOB handle from a caller-supplied buffer. N bytes of data are25# copied from the buffer Z into the open BLOB, starting at offset26# iOffset.27#28set dots [string repeat . 40]29do_execsql_test 1.0 {30 CREATE TABLE t1(a INTEGER PRIMARY KEY, t TEXT);31 INSERT INTO t1 VALUES(-1, $dots);32 INSERT INTO t1 VALUES(-2, $dots);33 INSERT INTO t1 VALUES(-3, $dots);34 INSERT INTO t1 VALUES(-4, $dots);35 INSERT INTO t1 VALUES(-5, $dots);36 INSERT INTO t1 VALUES(-6, $dots);37}38 39proc blob_write_test {tn id iOffset blob nData final} {40 sqlite3_blob_open db main t1 t $id 1 B41 42 # EVIDENCE-OF: R-45864-01884 On success, sqlite3_blob_write() returns43 # SQLITE_OK. Otherwise, an error code or an extended error code is44 # returned.45 #46 # This block tests the SQLITE_OK case in the requirement above (the47 # Tcl sqlite3_blob_write() wrapper uses an empty string in place of48 # "SQLITE_OK"). The error cases are tested by the "blob_write_error_test"49 # tests below.50 #51 set res [sqlite3_blob_write $B $iOffset $blob $nData]52 uplevel [list do_test $tn.1 [list set {} $res] {}]53 54 sqlite3_blob_close $B55 uplevel [list do_execsql_test $tn.3 "SELECT t FROM t1 WHERE a=$id" $final]56}57 58set blob "0123456789012345678901234567890123456789"59blob_write_test 1.1 -1 0 $blob 10 { 0123456789.............................. }60blob_write_test 1.2 -2 8 $blob 10 { ........0123456789...................... }61blob_write_test 1.3 -3 8 $blob 1 { ........0............................... }62blob_write_test 1.4 -4 18 $blob 22 { ..................0123456789012345678901 }63blob_write_test 1.5 -5 18 $blob 0 { ........................................ }64blob_write_test 1.6 -6 0 $blob 40 { 0123456789012345678901234567890123456789 }65 66 67proc blob_write_error_test {tn B iOffset blob nData errcode errmsg} {68 69 # In cases where the underlying sqlite3_blob_write() function returns70 # SQLITE_OK, the Tcl wrapper returns an empty string. If the underlying71 # function returns an error, the Tcl wrapper throws an exception with72 # the error code as the Tcl exception message.73 #74 if {$errcode=="SQLITE_OK"} {75 set ret ""76 set isError 077 } else {78 set ret $errcode79 set isError 180 }81 82 set cmd [list sqlite3_blob_write $B $iOffset $blob $nData]83 uplevel [list do_test $tn.1 [subst -nocommands {84 list [catch {$cmd} msg] [set msg] 85 }] [list $isError $ret]]86 87 # EVIDENCE-OF: R-34782-18311 Unless SQLITE_MISUSE is returned, this88 # function sets the database connection error code and message89 # accessible via sqlite3_errcode() and sqlite3_errmsg() and related90 # functions.91 #92 if {$errcode == "SQLITE_MISUSE"} { error "test proc misuse!" }93 uplevel [list do_test $tn.2 [list sqlite3_errcode db] $errcode]94 uplevel [list do_test $tn.3 [list sqlite3_errmsg db] $errmsg]95}96 97do_execsql_test 2.0 {98 CREATE TABLE t2(a TEXT, b INTEGER PRIMARY KEY);99 INSERT INTO t2 VALUES($dots, 43);100 INSERT INTO t2 VALUES($dots, 44);101 INSERT INTO t2 VALUES($dots, 45);102}103 104# EVIDENCE-OF: R-63341-57517 If the BLOB handle passed as the first105# argument was not opened for writing (the flags parameter to106# sqlite3_blob_open() was zero), this function returns SQLITE_READONLY.107#108sqlite3_blob_open db main t2 a 43 0 B109blob_write_error_test 2.1 $B 0 $blob 10 \110 SQLITE_READONLY {attempt to write a readonly database}111sqlite3_blob_close $B112 113# EVIDENCE-OF: R-29804-27366 If offset iOffset is less than N bytes from114# the end of the BLOB, SQLITE_ERROR is returned and no data is written.115#116sqlite3_blob_open db main t2 a 44 3 B117blob_write_error_test 2.2.1 $B 31 $blob 10 \118 SQLITE_ERROR {SQL logic error}119 120# Make a successful write to the blob handle. This shows that the121# sqlite3_errcode() and sqlite3_errmsg() values are set even if the122# blob_write() call succeeds (see requirement in the [blob_write_error_test]123# proc).124blob_write_error_test 2.2.1 $B 30 $blob 10 SQLITE_OK {not an error}125 126# EVIDENCE-OF: R-58570-38916 If N or iOffset are less than zero127# SQLITE_ERROR is returned and no data is written.128#129blob_write_error_test 2.2.2 $B 31 $blob -1 \130 SQLITE_ERROR {SQL logic error}131blob_write_error_test 2.2.3 $B 20 $blob 10 SQLITE_OK {not an error}132blob_write_error_test 2.2.4 $B -1 $blob 10 \133 SQLITE_ERROR {SQL logic error}134sqlite3_blob_close $B135 136# EVIDENCE-OF: R-20958-54138 An attempt to write to an expired BLOB137# handle fails with an error code of SQLITE_ABORT.138#139do_test 2.3 {140 sqlite3_blob_open db main t2 a 43 0 B141 execsql { DELETE FROM t2 WHERE b=43 }142} {}143blob_write_error_test 2.3.1 $B 5 $blob 5 \144 SQLITE_ABORT {query aborted}145do_test 2.3.2 {146 execsql { SELECT 1, 2, 3 }147 sqlite3_errcode db148} {SQLITE_OK}149blob_write_error_test 2.3.3 $B 5 $blob 5 \150 SQLITE_ABORT {query aborted}151sqlite3_blob_close $B152 153# EVIDENCE-OF: R-08382-59936 Writes to the BLOB that occurred before the154# BLOB handle expired are not rolled back by the expiration of the155# handle, though of course those changes might have been overwritten by156# the statement that expired the BLOB handle or by other independent157# statements.158#159# 3.1.*: not rolled back, 160# 3.2.*: overwritten.161#162do_execsql_test 3.0 {163 CREATE TABLE t3(i INTEGER PRIMARY KEY, j TEXT, k TEXT);164 INSERT INTO t3 VALUES(1, $dots, $dots);165 INSERT INTO t3 VALUES(2, $dots, $dots);166 SELECT * FROM t3 WHERE i=1;167} {168 1169 ........................................170 ........................................171}172sqlite3_blob_open db main t3 j 1 1 B173blob_write_error_test 3.1.1 $B 5 $blob 10 SQLITE_OK {not an error}174do_execsql_test 3.1.2 {175 UPDATE t3 SET k = 'xyz' WHERE i=1;176 SELECT * FROM t3 WHERE i=1;177} {178 1 .....0123456789......................... xyz179}180blob_write_error_test 3.1.3 $B 15 $blob 10 \181 SQLITE_ABORT {query aborted}182sqlite3_blob_close $B183do_execsql_test 3.1.4 {184 SELECT * FROM t3 WHERE i=1;185} {186 1 .....0123456789......................... xyz187}188 189sqlite3_blob_open db main t3 j 2 1 B190blob_write_error_test 3.2.1 $B 5 $blob 10 SQLITE_OK {not an error}191do_execsql_test 3.2.2 {192 UPDATE t3 SET j = 'xyz' WHERE i=2;193 SELECT * FROM t3 WHERE i=2;194} {195 2 xyz ........................................196}197blob_write_error_test 3.2.3 $B 15 $blob 10 \198 SQLITE_ABORT {query aborted}199sqlite3_blob_close $B200do_execsql_test 3.2.4 {201 SELECT * FROM t3 WHERE i=2;202} {203 2 xyz ........................................204}205 206 207 208finish_test209 