]> git.ipfire.org Git - thirdparty/sqlite.git/commitdiff
Add test file e_blobwrite.test, containing tests for the sqlite3_blob_write() interface.
authordan <dan@noemail.net>
Mon, 10 Nov 2014 17:53:03 +0000 (17:53 +0000)
committerdan <dan@noemail.net>
Mon, 10 Nov 2014 17:53:03 +0000 (17:53 +0000)
FossilOrigin-Name: 1df77e5f1bd82de4dc92fe28359c3e56ab3f9ed4

manifest
manifest.uuid
src/sqlite.h.in
src/vdbeblob.c
test/e_blobwrite.test [new file with mode: 0644]

index 7f7fe158b926f10d67c0f36b65f604cbd1466490..d14dec8c51ed32f182816f770bb50d4a4e3aaffe 100644 (file)
--- a/manifest
+++ b/manifest
@@ -1,5 +1,5 @@
-C Add\sthe\seval()\sSQL\sfunction\sextension\sin\sext/misc/eval.c.
-D 2014-11-10T16:49:56.620
+C Add\stest\sfile\se_blobwrite.test,\scontaining\stests\sfor\sthe\ssqlite3_blob_write()\sinterface.
+D 2014-11-10T17:53:03.345
 F Makefile.arm-wince-mingw32ce-gcc d6df77f1f48d690bd73162294bbba7f59507c72f
 F Makefile.in a226317fdf3f4c895fb3cfedc355b4d0868ce1fb
 F Makefile.linux-gcc 91d710bdc4998cb015f39edf3cb314ec4f4d7e23
@@ -230,7 +230,7 @@ F src/resolve.c 4965007d6497b6a4d7a6d98751cc39712885f952
 F src/rowset.c eccf6af6d620aaa4579bd3b72c1b6395d9e9fa1e
 F src/select.c 428165951748151e87a15295b7357221433e311b
 F src/shell.c bc28d5992109717c87804e2eb1a08a7c8cc7a2fd
-F src/sqlite.h.in 5abfd7d6c9bd2054105820de5ce12b1e491b1dc9
+F src/sqlite.h.in 5531c4c69ee0a351c2aa5ad9f3f0f2424a57a9f4
 F src/sqlite3.rc 992c9f5fb8285ae285d6be28240a7e8d3a7f2bad
 F src/sqlite3ext.h 17d487c3c91b0b8c584a32fbeb393f6f795eea7d
 F src/sqliteInt.h 71b0bf1a7fc55b5cb374f7579fd140e730a6e0f4
@@ -296,7 +296,7 @@ F src/vdbe.h 6fc69d9c5e146302c56e163cb4b31d1ee64a18c3
 F src/vdbeInt.h 9bb69ff2447c34b6ccc58b34ec35b615f86ead78
 F src/vdbeapi.c 07acb615d1e4170e71fc1b0d087f3c53a1ad8e83
 F src/vdbeaux.c 9b0a251b6dfab349dd6c6efb40062eb7386b26f5
-F src/vdbeblob.c 8b5442ff0954c44b45cbabbe2e94091a2e16fdef
+F src/vdbeblob.c 4af4bfb71f6df7778397b4a0ebc1879793276778
 F src/vdbemem.c 31d8eabb0cd78bfeab4e5124c7363c3e9e54db9f
 F src/vdbesort.c 87f3923483113d1c95d84640becb4e4946f27d9a
 F src/vdbetrace.c 7e4222955e07dd707a2f360c0eb73452be1cb010
@@ -454,6 +454,7 @@ F test/diskfull.test 106391384780753ea6896b7b4f005d10e9866b6e
 F test/distinct.test 086e70c765f172e8974e9f83b9ac5ca03c154e77
 F test/distinctagg.test 1a6ef9c87a58669438fc771450d7a72577417376
 F test/e_blobopen.test 234f960d90235a9b51ec3ca1e062e8541dd558d8
+F test/e_blobwrite.test 615b405f29feb2cfb5a1f03dab7933258294fa26
 F test/e_changes.test fd66105385153dbf21fdb35eb8ef6c3e1eade579
 F test/e_createtable.test c7e67b49e6cf92473c8fb30ab26143e9e2128cf7
 F test/e_delete.test d5186e2f5478b659f16a2c8b66c09892823e542a
@@ -1215,7 +1216,7 @@ F tool/vdbe_profile.tcl 67746953071a9f8f2f668b73fe899074e2c6d8c1
 F tool/warnings-clang.sh f6aa929dc20ef1f856af04a730772f59283631d4
 F tool/warnings.sh 0abfd78ceb09b7f7c27c688c8e3fe93268a13b32
 F tool/win/sqlite.vsix deb315d026cc8400325c5863eef847784a219a2f
-P 7f3819f6422badd344c1264b0cd2f2c7afe077df
-R a579eb1c0cce07efa12ad49b9c702f07
-U drh
-Z 4009c695bea2e084a946ee86095e7182
+P 27cf665b957f2c0ced403e3032099e80c295598f
+R 37c32da8fb4d2f1d3e3ebc03749cb035
+U dan
+Z 2886cb98deb64368f4cf93641ea335c8
index f90ad5421384cc52afe549df83114addf23ab063..719ea3ef1ba7d8227bcb6d0aced6002e2bc81e74 100644 (file)
@@ -1 +1 @@
-27cf665b957f2c0ced403e3032099e80c295598f
\ No newline at end of file
+1df77e5f1bd82de4dc92fe28359c3e56ab3f9ed4
\ No newline at end of file
index bed64bfae22a5d45bb17dd3717df10650cc30316..b5585defd6bade78e99b1c574127ffe5cf4ad2c3 100644 (file)
@@ -5821,21 +5821,27 @@ int sqlite3_blob_read(sqlite3_blob *, void *Z, int N, int iOffset);
 /*
 ** CAPI3REF: Write Data Into A BLOB Incrementally
 **
-** ^This function is used to write data into an open [BLOB handle] from a
-** caller-supplied buffer. ^N bytes of data are copied from the buffer Z
-** into the open BLOB, starting at offset iOffset.
+** ^(This function is used to write data into an open [BLOB handle] from a
+** caller-supplied buffer. N bytes of data are copied from the buffer Z
+** into the open BLOB, starting at offset iOffset.)^
+**
+** ^(On success, sqlite3_blob_write() returns SQLITE_OK.
+** Otherwise, an  [error code] or an [extended error code] is returned.)^
+** ^Unless SQLITE_MISUSE is returned, this function sets the 
+** [database connection] error code and message accessible via 
+** [sqlite3_errcode()] and [sqlite3_errmsg()] and related functions. 
 **
 ** ^If the [BLOB handle] passed as the first argument was not opened for
 ** writing (the flags parameter to [sqlite3_blob_open()] was zero),
 ** this function returns [SQLITE_READONLY].
 **
-** ^This function may only modify the contents of the BLOB; it is
+** This function may only modify the contents of the BLOB; it is
 ** not possible to increase the size of a BLOB using this API.
 ** ^If offset iOffset is less than N bytes from the end of the BLOB,
-** [SQLITE_ERROR] is returned and no data is written.  ^If N is
-** less than zero [SQLITE_ERROR] is returned and no data is written.
-** The size of the BLOB (and hence the maximum value of N+iOffset)
-** can be determined using the [sqlite3_blob_bytes()] interface.
+** [SQLITE_ERROR] is returned and no data is written. The size of the 
+** BLOB (and hence the maximum value of N+iOffset) can be determined 
+** using the [sqlite3_blob_bytes()] interface. ^If N or iOffset are less 
+** than zero [SQLITE_ERROR] is returned and no data is written.
 **
 ** ^An attempt to write to an expired [BLOB handle] fails with an
 ** error code of [SQLITE_ABORT].  ^Writes to the BLOB that occurred
@@ -5844,9 +5850,6 @@ int sqlite3_blob_read(sqlite3_blob *, void *Z, int N, int iOffset);
 ** have been overwritten by the statement that expired the BLOB handle
 ** or by other independent statements.
 **
-** ^(On success, sqlite3_blob_write() returns SQLITE_OK.
-** Otherwise, an  [error code] or an [extended error code] is returned.)^
-**
 ** This routine only works on a [BLOB handle] which has been created
 ** by a prior successful call to [sqlite3_blob_open()] and which has not
 ** been closed by [sqlite3_blob_close()].  Passing any other pointer in
index 0cf2b0652be1bb09279918c3b2b8871d9af0709c..cf1eb5905427edf0035d25a8d396b20fb9543f95 100644 (file)
@@ -376,7 +376,6 @@ static int blobReadWrite(
   if( n<0 || iOffset<0 || (iOffset+n)>p->nByte ){
     /* Request is out of range. Return a transient error. */
     rc = SQLITE_ERROR;
-    sqlite3Error(db, SQLITE_ERROR);
   }else if( v==0 ){
     /* If there is no statement handle, then the blob-handle has
     ** already been invalidated. Return SQLITE_ABORT in this case.
@@ -394,10 +393,10 @@ static int blobReadWrite(
       sqlite3VdbeFinalize(v);
       p->pStmt = 0;
     }else{
-      db->errCode = rc;
       v->rc = rc;
     }
   }
+  sqlite3Error(db, rc);
   rc = sqlite3ApiExit(db, rc);
   sqlite3_mutex_leave(db->mutex);
   return rc;
diff --git a/test/e_blobwrite.test b/test/e_blobwrite.test
new file mode 100644 (file)
index 0000000..a0d3333
--- /dev/null
@@ -0,0 +1,204 @@
+# 2014 October 30
+#
+# The author disclaims copyright to this source code.  In place of
+# a legal notice, here is a blessing:
+#
+#    May you do good and not evil.
+#    May you find forgiveness for yourself and forgive others.
+#    May you share freely, never taking more than you give.
+#
+#***********************************************************************
+#
+
+set testdir [file dirname $argv0]
+source $testdir/tester.tcl
+set testprefix e_blobwrite
+
+#--------------------------------------------------------------------------
+# EVIDENCE-OF: R-62898-22698 This function is used to write data into an
+# open BLOB handle from a caller-supplied buffer. N bytes of data are
+# copied from the buffer Z into the open BLOB, starting at offset
+# iOffset.
+#
+set dots [string repeat . 40]
+do_execsql_test 1.0 {
+  CREATE TABLE t1(a INTEGER PRIMARY KEY, t TEXT);
+  INSERT INTO t1 VALUES(-1, $dots);
+  INSERT INTO t1 VALUES(-2, $dots);
+  INSERT INTO t1 VALUES(-3, $dots);
+  INSERT INTO t1 VALUES(-4, $dots);
+  INSERT INTO t1 VALUES(-5, $dots);
+  INSERT INTO t1 VALUES(-6, $dots);
+}
+
+proc blob_write_test {tn id iOffset blob nData final} {
+  sqlite3_blob_open db main t1 t $id 1 B
+
+  # EVIDENCE-OF: R-45864-01884 On success, sqlite3_blob_write() returns
+  # SQLITE_OK. Otherwise, an error code or an extended error code is
+  # returned.
+  #
+  #   This block tests the SQLITE_OK case in the requirement above (the
+  #   Tcl sqlite3_blob_write() wrapper uses an empty string in place of
+  #   "SQLITE_OK"). The error cases are tested by the "blob_write_error_test"
+  #   tests below.
+  #
+  set res [sqlite3_blob_write $B $iOffset $blob $nData]
+  uplevel [list do_test $tn.1 [list set {} $res] {}]
+
+  sqlite3_blob_close $B
+  uplevel [list do_execsql_test $tn.3 "SELECT t FROM t1 WHERE a=$id" $final]
+}
+
+set blob "0123456789012345678901234567890123456789"
+blob_write_test 1.1 -1 0 $blob 10  { 0123456789.............................. }
+blob_write_test 1.2 -2 8 $blob 10  { ........0123456789...................... }
+blob_write_test 1.3 -3 8 $blob 1   { ........0............................... }
+blob_write_test 1.4 -4 18 $blob 22 { ..................0123456789012345678901 }
+blob_write_test 1.5 -5 18 $blob 0  { ........................................ }
+blob_write_test 1.6 -6 0 $blob 40  { 0123456789012345678901234567890123456789 }
+
+
+proc blob_write_error_test {tn B iOffset blob nData errcode errmsg} {
+
+  # In cases where the underlying sqlite3_blob_write() function returns
+  # SQLITE_OK, the Tcl wrapper returns an empty string. If the underlying
+  # function returns an error, the Tcl wrapper throws an exception with
+  # the error code as the Tcl exception message.
+  #
+  if {$errcode=="SQLITE_OK"} {
+    set ret ""
+    set isError 0
+  } else {
+    set ret $errcode
+    set isError 1
+  }
+
+  set cmd [list sqlite3_blob_write $B $iOffset $blob $nData]
+  uplevel [list do_test $tn.1 [subst -nocommands {
+    list [catch {$cmd} msg] [set msg]              
+  }] [list $isError $ret]]
+
+  # EVIDENCE-OF: R-34782-18311 Unless SQLITE_MISUSE is returned, this
+  # function sets the database connection error code and message
+  # accessible via sqlite3_errcode() and sqlite3_errmsg() and related
+  # functions.
+  #
+  if {$errcode == "SQLITE_MISUSE"} { error "test proc misuse!" }
+  uplevel [list do_test $tn.2 [list sqlite3_errcode db] $errcode]
+  uplevel [list do_test $tn.3 [list sqlite3_errmsg db] $errmsg]
+}
+
+do_execsql_test 2.0 {
+  CREATE TABLE t2(a TEXT, b INTEGER PRIMARY KEY);
+  INSERT INTO t2 VALUES($dots, 43);
+  INSERT INTO t2 VALUES($dots, 44);
+  INSERT INTO t2 VALUES($dots, 45);
+}
+
+# EVIDENCE-OF: R-63341-57517 If the BLOB handle passed as the first
+# argument was not opened for writing (the flags parameter to
+# sqlite3_blob_open() was zero), this function returns SQLITE_READONLY.
+#
+sqlite3_blob_open db main t2 a 43 0 B
+blob_write_error_test 2.1 $B 0 $blob 10   \
+    SQLITE_READONLY {attempt to write a readonly database}
+sqlite3_blob_close $B
+
+# EVIDENCE-OF: R-29804-27366 If offset iOffset is less than N bytes from
+# the end of the BLOB, SQLITE_ERROR is returned and no data is written.
+#
+sqlite3_blob_open db main t2 a 44 3 B
+blob_write_error_test 2.2.1 $B 31 $blob 10   \
+    SQLITE_ERROR {SQL logic error or missing database}
+
+# Make a successful write to the blob handle. This shows that the
+# sqlite3_errcode() and sqlite3_errmsg() values are set even if the
+# blob_write() call succeeds (see requirement in the [blob_write_error_test]
+# proc).
+blob_write_error_test 2.2.1 $B 30 $blob 10 SQLITE_OK {not an error}
+
+# EVIDENCE-OF: R-58570-38916 If N or iOffset are less than zero
+# SQLITE_ERROR is returned and no data is written.
+#
+blob_write_error_test 2.2.2 $B 31 $blob -1   \
+    SQLITE_ERROR {SQL logic error or missing database}
+blob_write_error_test 2.2.3 $B 20 $blob 10 SQLITE_OK {not an error}
+blob_write_error_test 2.2.4 $B -1 $blob 10   \
+    SQLITE_ERROR {SQL logic error or missing database}
+sqlite3_blob_close $B
+
+# EVIDENCE-OF: R-20958-54138 An attempt to write to an expired BLOB
+# handle fails with an error code of SQLITE_ABORT.
+#
+do_test 2.3 {
+  sqlite3_blob_open db main t2 a 43 0 B
+  execsql { DELETE FROM t2 WHERE b=43 }
+} {}
+blob_write_error_test 2.3.1 $B 5 $blob 5 \
+    SQLITE_ABORT {callback requested query abort}
+do_test 2.3.2 {
+  execsql { SELECT 1, 2, 3 }
+  sqlite3_errcode db
+} {SQLITE_OK}
+blob_write_error_test 2.3.3 $B 5 $blob 5 \
+    SQLITE_ABORT {callback requested query abort}
+sqlite3_blob_close $B
+
+# EVIDENCE-OF: R-08382-59936 Writes to the BLOB that occurred before the
+# BLOB handle expired are not rolled back by the expiration of the
+# handle, though of course those changes might have been overwritten by
+# the statement that expired the BLOB handle or by other independent
+# statements.
+#
+#   3.1.*: not rolled back, 
+#   3.2.*: overwritten.
+#
+do_execsql_test 3.0 {
+  CREATE TABLE t3(i INTEGER PRIMARY KEY, j TEXT, k TEXT);
+  INSERT INTO t3 VALUES(1, $dots, $dots);
+  INSERT INTO t3 VALUES(2, $dots, $dots);
+  SELECT * FROM t3 WHERE i=1;
+} {
+  1
+  ........................................
+  ........................................
+}
+sqlite3_blob_open db main t3 j 1 1 B
+blob_write_error_test 3.1.1 $B 5 $blob 10 SQLITE_OK {not an error}
+do_execsql_test 3.1.2 {
+  UPDATE t3 SET k = 'xyz' WHERE i=1;
+  SELECT * FROM t3 WHERE i=1;
+} {
+  1 .....0123456789......................... xyz
+}
+blob_write_error_test 3.1.3 $B 15 $blob 10 \
+    SQLITE_ABORT {callback requested query abort}
+sqlite3_blob_close $B
+do_execsql_test 3.1.4 {
+  SELECT * FROM t3 WHERE i=1;
+} {
+  1 .....0123456789......................... xyz
+}
+
+sqlite3_blob_open db main t3 j 2 1 B
+blob_write_error_test 3.2.1 $B 5 $blob 10 SQLITE_OK {not an error}
+do_execsql_test 3.2.2 {
+  UPDATE t3 SET j = 'xyz' WHERE i=2;
+  SELECT * FROM t3 WHERE i=2;
+} {
+  2 xyz ........................................
+}
+blob_write_error_test 3.2.3 $B 15 $blob 10 \
+    SQLITE_ABORT {callback requested query abort}
+sqlite3_blob_close $B
+do_execsql_test 3.2.4 {
+  SELECT * FROM t3 WHERE i=2;
+} {
+  2 xyz ........................................
+}
+
+
+
+finish_test
+