github.com/jdgcs/sqlite3@v1.12.1-0.20210908114423-bc5f96e4dd51/testdata/tcl/e_blobwrite.test (about)

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