AryaWu/sqlite
0
1# 2012 March 232#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.tcl15ifcapable {!incrblob} { finish_test ; return }16set testprefix incrblob417 18proc create_t1 {} {19 execsql {20 PRAGMA page_size = 1024;21 CREATE TABLE t1(k INTEGER PRIMARY KEY, v);22 }23}24 25proc populate_t1 {} {26 set data [list a b c d e f g h i j k l m n o p q r s t u v w x y z]27 foreach d $data {28 set blob [string repeat $d 900]29 execsql { INSERT INTO t1(v) VALUES($blob) }30 }31}32 33 34do_test 1.1 { 35 create_t136 populate_t1 37} {}38 39do_test 1.2 {40 set blob [db incrblob t1 v 5]41 read $blob 1042} {eeeeeeeeee}43 44do_test 1.3 {45 execsql { DELETE FROM t1 }46 populate_t147} {}48 49 50 51do_test 2.1 { 52 reset_db53 create_t154 populate_t1 55} {}56 57do_test 2.2 {58 set blob [db incrblob t1 v 10]59 read $blob 1060} {jjjjjjjjjj}61 62do_test 2.3 {63 set new [string repeat % 900]64 execsql { DELETE FROM t1 WHERE k=10 }65 execsql { DELETE FROM t1 WHERE k=9 }66 execsql { INSERT INTO t1(v) VALUES($new) }67} {}68 69 70 71do_test 3.1 {72 reset_db73 create_t174 populate_t1 75} {}76 77do_test 3.2 {78 set blob [db incrblob t1 v 20]79 read $blob 1080} {tttttttttt}81 82do_test 3.3 {83 set new [string repeat % 900]84 execsql { UPDATE t1 SET v = $new WHERE k = 20 }85 execsql { DELETE FROM t1 WHERE k=19 }86 execsql { INSERT INTO t1(v) VALUES($new) }87} {}88 89#-------------------------------------------------------------------------90# Test that it is not possible to DROP a table with an incremental blob91# cursor open on it.92#93do_execsql_test 4.1 {94 CREATE TABLE t2(a INTEGER PRIMARY KEY, b);95 INSERT INTO t2 VALUES(456, '0123456789');96}97do_test 4.2 {98 set blob [db incrblob -readonly t2 b 456]99 read $blob 5100} {01234}101do_catchsql_test 4.3 {102 DROP TABLE t2103} {1 {database table is locked}}104do_test 4.4 {105 sqlite3_extended_errcode db106} {SQLITE_LOCKED}107close $blob108 109#-------------------------------------------------------------------------110 111ifcapable preupdate {112 113reset_db114do_execsql_test 5.1 {115 CREATE TABLE t2(a INTEGER PRIMARY KEY, b);116 INSERT INTO t2 VALUES(1000, 'abcdefghijklmnopqrstuvwxyz');117 INSERT INTO t2 VALUES(2000, 'ABCDEFGHIJKLMNOPQRSTUVWXYZ');118 INSERT INTO t2 VALUES(3000, 'abcdefghijklmnopqrstuvwxyz');119}120 121do_test 5.2.1 {122 execsql BEGIN123 set blob [db incrblob t2 b 2000]124 seek $blob 0125 puts -nonewline $blob "hello "126 flush $blob127 execsql ROLLBACK128} {}129 130do_test 5.2.2 {131 puts -nonewline $blob "world"132 set rc [catch { flush $blob } msg]133 list $rc [regsub {input/output} $msg {I/O}]134} "1 {error flushing \"$blob\": I/O error}"135catch { close $blob }136 137set preupdate_count 0138proc preupdate {args} { incr ::preupdate_count ; return {} }139db preupdate hook preupdate140 141set preupdate_count 0142do_test 5.3.1 {143 execsql BEGIN144 set blob [db incrblob t2 b 1000]145 seek $blob 0146 puts -nonewline $blob "hello "147 flush $blob148 execsql ROLLBACK149} {}150 151do_test 5.3.2 {152 puts -nonewline $blob "world"153 set rc [catch { flush $blob } msg]154 list $rc [regsub {input/output} $msg {I/O}]155} "1 {error flushing \"$blob\": I/O error}"156catch { close $blob }157 158do_test 5.3.3 {159 set ::preupdate_count160} {1}161 162set preupdate_count 0163do_test 5.4.1 {164 execsql BEGIN165 set blob [db incrblob t2 b 1000]166 seek $blob 0167 puts -nonewline $blob "hello "168 flush $blob169 execsql { DELETE FROM t2 WHERE a=3000; }170} {}171 172do_test 5.4.2 {173 puts -nonewline $blob "world"174 list [catch { flush $blob } msg] $msg175} "0 {}"176catch { close $blob }177catchsql { ROLLBACK }178 179do_test 5.3.3 {180 set ::preupdate_count181} {3}182 183set preupdate_count 0184do_test 5.4.3 {185 execsql BEGIN186 set blob [db incrblob t2 b 2000]187 seek $blob 0188 puts -nonewline $blob "hello "189 flush $blob190 execsql { UPDATE t2 SET b='abcdefghijklmnopqrstuvwxyz' WHERE a=2000 }191} {}192 193do_test 5.4.4 {194 puts -nonewline $blob "world"195 set rc [catch { flush $blob } msg]196 list $rc [regsub {input/output} $msg {I/O}]197} "1 {error flushing \"$blob\": I/O error}"198catch { close $blob }199catchsql { ROLLBACK }200 201do_test 5.4.5 {202 set ::preupdate_count203} {2}204 205}206 207finish_test208 