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_blobopen
000016  
000017  forcedelete test.db2
000018  
000019  do_execsql_test 1.0 {
000020    ATTACH 'test.db2' AS aux;
000021  
000022    CREATE TABLE main.t1(a INTEGER PRIMARY KEY, b TEXT, c BLOB);
000023    CREATE TEMP TABLE t1(a INTEGER PRIMARY KEY, b TEXT, c BLOB);
000024    CREATE TABLE aux.t1(a INTEGER PRIMARY KEY, b TEXT, c BLOB);
000025  
000026    CREATE TABLE main.x1(a INTEGER PRIMARY KEY, b TEXT, c BLOB);
000027    CREATE TEMP TABLE x2(a INTEGER PRIMARY KEY, b TEXT, c BLOB);
000028    CREATE TABLE aux.x3(a INTEGER PRIMARY KEY, b TEXT, c BLOB);
000029  
000030    INSERT INTO main.t1 VALUES(1, 'main one', X'0101');
000031    INSERT INTO main.t1 VALUES(2, 'main two', X'0102');
000032    INSERT INTO main.t1 VALUES(3, 'main three', X'0103');
000033    INSERT INTO main.t1 VALUES(4, 'main four', X'0104');
000034    INSERT INTO main.t1 VALUES(5, 'main five', X'0105');
000035  
000036    INSERT INTO main.x1 VALUES(1, 'x main one', X'000101');
000037    INSERT INTO main.x1 VALUES(2, 'x main two', X'000102');
000038    INSERT INTO main.x1 VALUES(3, 'x main three', X'000103');
000039    INSERT INTO main.x1 VALUES(4, 'x main four', X'000104');
000040    INSERT INTO main.x1 VALUES(5, 'x main five', X'000105');
000041  
000042    INSERT INTO temp.t1 VALUES(1, 'temp one', X'0201');
000043    INSERT INTO temp.t1 VALUES(2, 'temp two', X'0202');
000044    INSERT INTO temp.t1 VALUES(3, 'temp three', X'0203');
000045    INSERT INTO temp.t1 VALUES(4, 'temp four', X'0204');
000046    INSERT INTO temp.t1 VALUES(5, 'temp five', X'0205');
000047  
000048    INSERT INTO temp.x2 VALUES(1, 'x temp one', X'000201');
000049    INSERT INTO temp.x2 VALUES(2, 'x temp two', X'000202');
000050    INSERT INTO temp.x2 VALUES(3, 'x temp three', X'000203');
000051    INSERT INTO temp.x2 VALUES(4, 'x temp four', X'000204');
000052    INSERT INTO temp.x2 VALUES(5, 'x temp five', X'000205');
000053  
000054    INSERT INTO aux.t1 VALUES(1, 'aux one', X'0301');
000055    INSERT INTO aux.t1 VALUES(2, 'aux two', X'0302');
000056    INSERT INTO aux.t1 VALUES(3, 'aux three', X'0303');
000057    INSERT INTO aux.t1 VALUES(4, 'aux four', X'0304');
000058    INSERT INTO aux.t1 VALUES(5, 'aux five', X'0305');
000059  
000060    INSERT INTO aux.x3 VALUES(1, 'x aux one', X'000301');
000061    INSERT INTO aux.x3 VALUES(2, 'x aux two', X'000302');
000062    INSERT INTO aux.x3 VALUES(3, 'x aux three', X'000303');
000063    INSERT INTO aux.x3 VALUES(4, 'x aux four', X'000304');
000064    INSERT INTO aux.x3 VALUES(5, 'x aux five', X'000305');
000065  }
000066  
000067  #-------------------------------------------------------------------------
000068  # EVIDENCE-OF: R-37639-55938 This interfaces opens a handle to the BLOB
000069  # located in row iRow, column zColumn, table zTable in database zDb; in
000070  # other words, the same BLOB that would be selected by: SELECT zColumn
000071  # FROM zDb.zTable WHERE rowid = iRow;
000072  #
000073  proc read_blob {zDb zTab zCol iRow} {
000074    sqlite3_blob_open db $zDb $zTab $zCol $iRow 0 B
000075    set nByte [sqlite3_blob_bytes $B]
000076    set data [sqlite3_blob_read $B 0 $nByte]
000077    sqlite3_blob_close $B
000078    return $data
000079  }
000080  
000081  do_test 1.1.1 { read_blob main t1 b 1 } "main one"
000082  do_test 1.1.2 { read_blob main t1 c 1 } "\01\01"
000083  do_test 1.1.3 { read_blob temp t1 b 1 } "temp one"
000084  do_test 1.1.4 { read_blob temp t1 c 1 } "\02\01"
000085  do_test 1.1.6 { read_blob aux  t1 b 1 } "aux one"
000086  do_test 1.1.7 { read_blob aux  t1 c 1 } "\03\01"
000087  
000088  do_test 1.2.1 { read_blob main t1 b 4 } "main four"
000089  do_test 1.2.2 { read_blob main t1 c 4 } "\01\04"
000090  do_test 1.2.3 { read_blob temp t1 b 4 } "temp four"
000091  do_test 1.2.4 { read_blob temp t1 c 4 } "\02\04"
000092  do_test 1.2.6 { read_blob aux  t1 b 4 } "aux four"
000093  do_test 1.2.7 { read_blob aux  t1 c 4 } "\03\04"
000094  
000095  do_test 1.3.1 { read_blob main x1 b 2 } "x main two"
000096  do_test 1.3.2 { read_blob main x1 c 2 } "\00\01\02"
000097  do_test 1.3.3 { read_blob temp x2 b 2 } "x temp two"
000098  do_test 1.3.4 { read_blob temp x2 c 2 } "\00\02\02"
000099  do_test 1.3.6 { read_blob aux  x3 b 2 } "x aux two"
000100  do_test 1.3.7 { read_blob aux  x3 c 2 } "\00\03\02"
000101  
000102  #-------------------------------------------------------------------------
000103  # EVIDENCE-OF: R-27234-05761 Parameter zDb is not the filename that
000104  # contains the database, but rather the symbolic name of the database.
000105  # For attached databases, this is the name that appears after the AS
000106  # keyword in the ATTACH statement. For the main database file, the
000107  # database name is "main". For TEMP tables, the database name is "temp".
000108  #
000109  #   The test cases immediately above demonstrate that the database name
000110  #   for the main db, for TEMP tables and for those in attached databases
000111  #   is correct. The following tests check that filenames cannot be
000112  #   used as well.
000113  #
000114  do_test 2.1 {
000115    list [catch { sqlite3_blob_open db "test.db" t1 b 1 0 B } msg] $msg
000116  } {1 SQLITE_ERROR}
000117  do_test 2.2 {
000118    list [catch { sqlite3_blob_open db "test.db2" t1 b 1 0 B } msg] $msg
000119  } {1 SQLITE_ERROR}
000120  
000121  #-------------------------------------------------------------------------
000122  # EVIDENCE-OF: R-50854-53979 If the flags parameter is non-zero, then
000123  # the BLOB is opened for read and write access.
000124  #
000125  # EVIDENCE-OF: R-03922-41160 If the flags parameter is zero, the BLOB is
000126  # opened for read-only access.
000127  #
000128  foreach {tn iRow flags} {
000129    1 1   0
000130    2 2   1
000131    3 3  -1
000132    4 4   2147483647
000133    5 5  -2147483648
000134  } {
000135    do_test 3.$tn.1 {
000136      sqlite3_blob_open db main x1 c $iRow $flags B
000137      set n [sqlite3_blob_bytes $B]
000138      sqlite3_blob_read $B 0 $n
000139    } [binary format ccc 0 1 $iRow]
000140  
000141    if {$flags==0} {
000142      # Blob was opened for read-only access - writing returns an error.
000143      do_test 3.$tn.2 {
000144        list [catch { sqlite3_blob_write $B 0 xxx 3 } msg] $msg
000145      } {1 SQLITE_READONLY}
000146  
000147      do_execsql_test 3.$tn.3 {
000148        SELECT c FROM x1 WHERE a=$iRow;
000149      } [binary format ccc 0 1 $iRow]
000150    } else {
000151      # Blob was opened for read/write access - writing succeeds
000152      do_test 3.$tn.4 {
000153        list [catch { sqlite3_blob_write $B 0 xxx 3 } msg] $msg
000154      } {0 {}}
000155  
000156      do_execsql_test 3.$tn.5 {
000157        SELECT c FROM x1 WHERE a=$iRow;
000158      } {xxx}
000159    }
000160  
000161    sqlite3_blob_close $B
000162  }
000163  
000164  #-------------------------------------------------------------------------
000165  #
000166  reset_db
000167  do_execsql_test 4.0 {
000168    CREATE TABLE t1(x, y);
000169    INSERT INTO t1 VALUES('abcd', 152);
000170    INSERT INTO t1 VALUES(NULL, X'00010203');
000171    INSERT INTO t1 VALUES('', 154.2);
000172  
000173    CREATE TABLE t2(x PRIMARY KEY, y) WITHOUT ROWID;
000174    INSERT INTO t2 VALUES(1, 'blob');
000175  
000176    CREATE TABLE t3(a PRIMARY KEY, b, c, d, e, f, UNIQUE(e, f));
000177    INSERT INTO t3 VALUES('aaaa', 'bbbb', 'cccc', 'dddd', 'eeee', 'ffff');
000178    CREATE INDEX t3b ON t3(b);
000179  
000180    CREATE TABLE p1(x PRIMARY KEY);
000181    INSERT INTO p1 VALUES('abc');
000182  
000183    CREATE TABLE c1(a INTEGER PRIMARY KEY, b REFERENCES p1);
000184    INSERT INTO c1 VALUES(45, 'abc');
000185  }
000186  
000187  proc test_blob_open {tn zDb zTab zCol iRow flags    errcode errmsg} {
000188    global B
000189    set B "0x1234"
000190  
000191    if {$errcode=="SQLITE_OK"} {
000192      set expected "0 {}"
000193    } else {
000194      set expected "1 $errcode"
000195    }
000196  
000197    set ::res [list [
000198      catch { sqlite3_blob_open db $zDb $zTab $zCol $iRow $flags B } msg
000199    ] $msg]
000200    do_test 4.$tn.1 { set ::res } $expected
000201  
000202    # EVIDENCE-OF: R-08940-21305 Unless it returns SQLITE_MISUSE, this
000203    # function sets the database connection error code and message
000204    # accessible via sqlite3_errcode() and sqlite3_errmsg() and related
000205    # functions.
000206    #
000207    #   This proc (test_blob_open) is used below to test various error and
000208    #   non-error conditions. But never SQLITE_MISUSE conditions. So these
000209    #   test cases are considered as partly verifying the requirement above.
000210    #   See below for a test of the SQLITE_MISUSE case.
000211    #
000212    do_test 4.$tn.2 {
000213      sqlite3_errcode db
000214    } $errcode
000215    do_test 4.$tn.3 {
000216      sqlite3_errmsg db
000217    } $errmsg
000218  
000219    # EVIDENCE-OF: R-31086-35521 On success, SQLITE_OK is returned and the
000220    # new BLOB handle is stored in *ppBlob. Otherwise an error code is
000221    # returned and, unless the error code is SQLITE_MISUSE, *ppBlob is set
000222    # to NULL.
000223    #
000224    do_test 4.$tn.4 {
000225      expr {$B == "0"}
000226    } [expr {$errcode != "SQLITE_OK"}]
000227  
000228    # EVIDENCE-OF: R-63421-15521 This means that, provided the API is not
000229    # misused, it is always safe to call sqlite3_blob_close() on *ppBlob
000230    # after this function it returns.
000231    do_test 4.$tn.5 {
000232      sqlite3_blob_close $B
000233    } {}
000234  }
000235  
000236  # EVIDENCE-OF: R-31204-44780 Database zDb does not exist
000237  test_blob_open 1 nosuchdb t1 x 1 0 SQLITE_ERROR "no such table: nosuchdb.t1"
000238  
000239  # EVIDENCE-OF: R-28676-08005 Table zTable does not exist within database zDb
000240  test_blob_open 2 main tt1 x 1 0    SQLITE_ERROR "no such table: main.tt1"
000241  
000242  # EVIDENCE-OF: R-40134-30296 Table zTable is a WITHOUT ROWID table
000243  test_blob_open 3 main t2 y 1 0     SQLITE_ERROR \
000244      "cannot open table without rowid: t2"
000245  
000246  # EVIDENCE-OF: R-56376-21261 Column zColumn does not exist
000247  test_blob_open 4 main t1 z 2 0     SQLITE_ERROR "no such column: \"z\""
000248  
000249  # EVIDENCE-OF: R-28258-23166 Row iRow is not present in the table
000250  test_blob_open 5 main t1 y 6 0     SQLITE_ERROR "no such rowid: 6"
000251  
000252  # EVIDENCE-OF: R-11683-62380 The specified column of row iRow contains a
000253  # value that is not a TEXT or BLOB value
000254  test_blob_open 6 main t1 x 2 0 SQLITE_ERROR "cannot open value of type null"
000255  test_blob_open 7 main t1 y 1 0 SQLITE_ERROR "cannot open value of type integer"
000256  test_blob_open 8 main t1 y 3 0 SQLITE_ERROR "cannot open value of type real"
000257  
000258  # EVIDENCE-OF: R-34146-30782 Column zColumn is part of an index, PRIMARY
000259  # KEY or UNIQUE constraint and the blob is being opened for read/write
000260  # access
000261  #
000262  # Test cases 8.1.* show that such columns can be opened for read-access. 
000263  # Tests 8.2.* show that read-write access is different. Columns "c" and "c"
000264  # are not part of an index, PK or UNIQUE constraint, so they work in both
000265  # cases.
000266  #
000267  test_blob_open 8.1.1 main t3 a 1 0 SQLITE_OK "not an error"
000268  test_blob_open 8.1.2 main t3 b 1 0 SQLITE_OK "not an error"
000269  test_blob_open 8.1.3 main t3 c 1 0 SQLITE_OK "not an error"
000270  test_blob_open 8.1.4 main t3 d 1 0 SQLITE_OK "not an error"
000271  test_blob_open 8.1.5 main t3 e 1 0 SQLITE_OK "not an error"
000272  test_blob_open 8.1.6 main t3 f 1 0 SQLITE_OK "not an error"
000273  
000274  set cannot "cannot open indexed column for writing"
000275  test_blob_open 8.2.1 main t3 a 1 8 SQLITE_ERROR $cannot
000276  test_blob_open 8.2.2 main t3 b 1 8 SQLITE_ERROR $cannot
000277  test_blob_open 8.2.3 main t3 c 1 8 SQLITE_OK "not an error"
000278  test_blob_open 8.2.4 main t3 d 1 8 SQLITE_OK "not an error"
000279  test_blob_open 8.2.5 main t3 e 1 8 SQLITE_ERROR $cannot
000280  test_blob_open 8.2.6 main t3 f 1 8 SQLITE_ERROR $cannot
000281  
000282  # EVIDENCE-OF: R-50117-55204 Foreign key constraints are enabled, column
000283  # zColumn is part of a child key definition and the blob is being opened
000284  # for read/write access
000285  #
000286  #   9.1: FK disabled, read-only access.
000287  #   9.2: FK disabled, read-only access.
000288  #   9.3: FK enabled, read/write access.
000289  #   9.4: FK enabled, read/write access.
000290  #
000291  test_blob_open 9.1 main c1 b 45 0 SQLITE_OK "not an error"
000292  test_blob_open 9.2 main c1 b 45 1 SQLITE_OK "not an error"
000293  execsql { PRAGMA foreign_keys = ON }
000294  test_blob_open 9.3 main c1 b 45 0 SQLITE_OK "not an error"
000295  test_blob_open 9.4 main c1 b 45 1 SQLITE_ERROR \
000296          "cannot open foreign key column for writing"
000297  
000298  #-------------------------------------------------------------------------
000299  # EVIDENCE-OF: R-08940-21305 Unless it returns SQLITE_MISUSE, this
000300  # function sets the database connection error code and message
000301  # accessible via sqlite3_errcode() and sqlite3_errmsg() and related
000302  # functions.
000303  #
000304  #   This requirement is partially verified by the many uses of test
000305  #   command [test_blob_open] above. All that is left is to verify the
000306  #   SQLITE_MISUSE case.
000307  #
000308  #   SQLITE_MISUSE is only returned if SQLITE_ENABLE_API_ARMOR is defined
000309  #   during compilation.
000310  #
000311  ifcapable api_armor {
000312    sqlite3_blob_open db main t1 x 1 0 B
000313  
000314    do_test 10.1.1 {
000315      list [catch {sqlite3_blob_open $B main t1 x 1 0 B2} msg] $msg
000316    } {1 SQLITE_MISUSE}
000317    do_test 10.1.2 {
000318      list [sqlite3_errcode db] [sqlite3_errmsg db]
000319    } {SQLITE_OK {not an error}}
000320    sqlite3_blob_close $B
000321  
000322    do_test 10.2.1 {
000323      list [catch {sqlite3_blob_open db main {} x 1 0 B} msg] $msg
000324    } {1 SQLITE_MISUSE}
000325    do_test 10.2.2 {
000326      list [sqlite3_errcode db] [sqlite3_errmsg db]
000327    } {SQLITE_OK {not an error}}
000328  }
000329  
000330  #-------------------------------------------------------------------------
000331  # EVIDENCE-OF: R-50542-62589 If the row that a BLOB handle points to is
000332  # modified by an UPDATE, DELETE, or by ON CONFLICT side-effects then the
000333  # BLOB handle is marked as "expired". This is true if any column of the
000334  # row is changed, even a column other than the one the BLOB handle is
000335  # open on.
000336  #
000337  # EVIDENCE-OF: R-48367-20048 Calls to sqlite3_blob_read() and
000338  # sqlite3_blob_write() for an expired BLOB handle fail with a return
000339  # code of SQLITE_ABORT.
000340  #
000341  #   11.2: read-only handle, DELETE.
000342  #   11.3: read-only handle, UPDATE.
000343  #   11.4: read-only handle, REPLACE.
000344  #   11.5: read/write handle, DELETE.
000345  #   11.6: read/write handle, UPDATE.
000346  #   11.7: read/write handle, REPLACE.
000347  #
000348  do_execsql_test 11.1 {
000349    CREATE TABLE b1(a INTEGER PRIMARY KEY, b, c UNIQUE);
000350    INSERT INTO b1 VALUES(1, '1234567890', 1);
000351    INSERT INTO b1 VALUES(2, '1234567890', 2);
000352    INSERT INTO b1 VALUES(3, '1234567890', 3);
000353    INSERT INTO b1 VALUES(4, '1234567890', 4);
000354    INSERT INTO b1 VALUES(5, '1234567890', 5);
000355    INSERT INTO b1 VALUES(6, '1234567890', 6);
000356  
000357    CREATE TABLE b2(a INTEGER PRIMARY KEY, b, c UNIQUE);
000358    INSERT INTO b2 VALUES(1, '1234567890', 1);
000359    INSERT INTO b2 VALUES(2, '1234567890', 2);
000360    INSERT INTO b2 VALUES(3, '1234567890', 3);
000361    INSERT INTO b2 VALUES(4, '1234567890', 4);
000362    INSERT INTO b2 VALUES(5, '1234567890', 5);
000363    INSERT INTO b2 VALUES(6, '1234567890', 6);
000364  }
000365  
000366  do_test 11.2.1 {
000367    sqlite3_blob_open db main b1 b 2 0 B
000368    sqlite3_blob_read $B 0 10
000369  } {1234567890}
000370  do_test 11.2.2 {
000371    # Deleting a different row does not invalidate the blob handle.
000372    execsql { DELETE FROM b1 WHERE a = 1 }
000373    sqlite3_blob_read $B 0 10
000374  } {1234567890}
000375  do_test 11.2.3 {
000376    execsql { DELETE FROM b1 WHERE a = 2 }
000377    list [catch { sqlite3_blob_read $B 0 10 } msg] $msg
000378  } {1 SQLITE_ABORT}
000379  do_test 11.2.4 {
000380    sqlite3_blob_close $B
000381  } {}
000382  
000383  do_test 11.3.1 {
000384    sqlite3_blob_open db main b1 b 3 0 B
000385    sqlite3_blob_read $B 0 10
000386  } {1234567890}
000387  do_test 11.3.2 {
000388    # Updating a different row
000389    execsql { UPDATE b1 SET c = 42 WHERE a=4 }
000390    sqlite3_blob_read $B 0 10
000391  } {1234567890}
000392  do_test 11.3.3 {
000393    execsql { UPDATE b1 SET c = 43 WHERE a=3 }
000394    list [catch { sqlite3_blob_read $B 0 10 } msg] $msg
000395  } {1 SQLITE_ABORT}
000396  do_test 11.3.4 {
000397    sqlite3_blob_close $B
000398  } {}
000399  
000400  do_test 11.4.1 {
000401    sqlite3_blob_open db main b1 b 6 0 B
000402    sqlite3_blob_read $B 0 10
000403  } {1234567890}
000404  do_test 11.4.2 {
000405    # Replace a different row
000406    execsql { INSERT OR REPLACE INTO b1 VALUES(10, 'abcdefghij', 5) }
000407    sqlite3_blob_read $B 0 10
000408  } {1234567890}
000409  do_test 11.4.3 {
000410    execsql { INSERT OR REPLACE INTO b1 VALUES(11, 'abcdefghij', 6) }
000411    list [catch { sqlite3_blob_read $B 0 10 } msg] $msg
000412  } {1 SQLITE_ABORT}
000413  do_test 11.4.4 {
000414    sqlite3_blob_close $B
000415  } {}
000416  
000417  do_test 11.4.1 {
000418    sqlite3_blob_open db main b2 b 2 1 B
000419    sqlite3_blob_write $B 0 "abcdefghij"
000420  } {}
000421  do_test 11.4.2 {
000422    # Deleting a different row does not invalidate the blob handle.
000423    execsql { DELETE FROM b2 WHERE a = 1 }
000424    sqlite3_blob_write $B 0 "ABCDEFGHIJ"
000425  } {}
000426  do_test 11.4.3 {
000427    execsql { DELETE FROM b2 WHERE a = 2 }
000428    list [catch { sqlite3_blob_write $B 0 "0987654321" } msg] $msg
000429  } {1 SQLITE_ABORT}
000430  do_test 11.4.4 {
000431    sqlite3_blob_close $B
000432  } {}
000433  
000434  do_test 11.5.1 {
000435    sqlite3_blob_open db main b2 b 3 1 B
000436    sqlite3_blob_write $B 0 "abcdefghij"
000437  } {}
000438  do_test 11.5.2 {
000439    # Updating a different row
000440    execsql { UPDATE b2 SET c = 42 WHERE a=4 }
000441    sqlite3_blob_write $B 0 "ABCDEFGHIJ"
000442  } {}
000443  do_test 11.5.3 {
000444    execsql { UPDATE b2 SET c = 43 WHERE a=3 }
000445    list [catch { sqlite3_blob_write $B 0 "0987654321" } msg] $msg
000446  } {1 SQLITE_ABORT}
000447  do_test 11.5.4 {
000448    sqlite3_blob_close $B
000449  } {}
000450  
000451  do_test 11.6.1 {
000452    sqlite3_blob_open db main b2 b 6 1 B
000453    sqlite3_blob_write $B 0 "abcdefghij"
000454  } {}
000455  do_test 11.6.2 {
000456    # Replace a different row
000457    execsql { INSERT OR REPLACE INTO b2 VALUES(10, 'abcdefghij', 5) }
000458    sqlite3_blob_write $B 0 "ABCDEFGHIJ"
000459  } {}
000460  do_test 11.6.3 {
000461    execsql { INSERT OR REPLACE INTO b2 VALUES(11, 'abcdefghij', 6) }
000462    list [catch { sqlite3_blob_write $B 0 "0987654321" } msg] $msg
000463  } {1 SQLITE_ABORT}
000464  do_test 11.6.4 {
000465    sqlite3_blob_close $B
000466  } {}
000467  
000468  #-------------------------------------------------------------------------
000469  # EVIDENCE-OF: R-45408-40694 Changes written into a BLOB prior to the
000470  # BLOB expiring are not rolled back by the expiration of the BLOB. Such
000471  # changes will eventually commit if the transaction continues to
000472  # completion.
000473  #
000474  do_execsql_test 12.1 {
000475    CREATE TABLE b3(x INTEGER PRIMARY KEY, y TEXT, z INTEGER);
000476    INSERT INTO b3 VALUES(22, '..........', NULL);
000477  }
000478  do_test 12.2 {
000479    sqlite3_blob_open db main b3 y 22 1 B
000480    sqlite3_blob_write $B 0 "xxxxx" 5
000481  } {}
000482  do_execsql_test 12.3 {
000483    UPDATE b3 SET z = 'not null';
000484  }
000485  do_test 12.4 {
000486    list [catch {sqlite3_blob_write $B 5 "xxxxx" 5} msg] $msg
000487  } {1 SQLITE_ABORT}
000488  do_execsql_test 12.5 {
000489    SELECT * FROM b3;
000490  } {22 xxxxx..... {not null}}
000491  do_test 12.5 {
000492    sqlite3_blob_close $B
000493  } {}
000494  do_execsql_test 12.6 {
000495    SELECT * FROM b3;
000496  } {22 xxxxx..... {not null}}
000497  
000498  #-------------------------------------------------------------------------
000499  # EVIDENCE-OF: R-58813-55036 The sqlite3_bind_zeroblob() and
000500  # sqlite3_result_zeroblob() interfaces and the built-in zeroblob SQL
000501  # function may be used to create a zero-filled blob to read or write
000502  # using the incremental-blob interface.
000503  #
000504  do_execsql_test 13.1 {
000505    CREATE TABLE c2(i INTEGER PRIMARY KEY, j);
000506    INSERT INTO c2 VALUES(10, zeroblob(24));
000507  }
000508  
000509  do_test 13.2 {
000510    set stmt [sqlite3_prepare_v2 db "INSERT INTO c2 VALUES(11, ?)" -1]
000511    sqlite3_bind_zeroblob $stmt 1 45
000512    sqlite3_step $stmt
000513    sqlite3_finalize $stmt
000514  } {SQLITE_OK}
000515  
000516  # The blobs can be read:
000517  #
000518  do_test 13.3.1 {
000519    sqlite3_blob_open db main c2 j 10 1 B
000520    sqlite3_blob_open db main c2 j 11 1 B2
000521    list [sqlite3_blob_bytes $B] [sqlite3_blob_bytes $B2]
000522  } {24 45}
000523  do_test 13.3.2 {
000524    sqlite3_blob_read $B 0 24
000525  } [string repeat [binary format c 0] 24]
000526  do_test 13.3.3 {
000527    sqlite3_blob_read $B2 0 45
000528  } [string repeat [binary format c 0] 45]
000529  
000530  # And also written:
000531  #
000532  do_test 13.4.1 {
000533    sqlite3_blob_write $B 0 [string repeat [binary format c 1] 24]
000534  } {}
000535  do_test 13.4.2 {
000536    sqlite3_blob_write $B2 0 [string repeat [binary format c 1] 45]
000537  } {}
000538  do_test 13.5 {
000539    sqlite3_blob_close $B
000540    sqlite3_blob_close $B2
000541    execsql { SELECT j FROM c2 }
000542  } [list \
000543      [string repeat [binary format c 1] 24] \
000544      [string repeat [binary format c 1] 45] \
000545  ]
000546  
000547  
000548  finish_test