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