3 # The author disclaims copyright to this source code. In place of
4 # a legal notice, here is a blessing:
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.
10 #***********************************************************************
11 # This file implements regression tests for SQLite library.
13 # $Id: blob.test,v 1.8 2009/04/28 18:00:27 drh Exp $
15 set testdir [file dirname $argv0]
16 source $testdir/tester.tcl
18 ifcapable {!bloblit} {
23 proc bin_to_hex {blob} {
25 binary scan $blob \c* bytes
27 foreach b $bytes {lappend bytes2 [format %02X [expr $b & 0xFF]]}
31 # Simplest possible case. Specify a blob literal
33 set blob [execsql {SELECT X'01020304';}]
34 bin_to_hex [lindex $blob 0]
37 set blob [execsql {SELECT x'ABCDEF';}]
38 bin_to_hex [lindex $blob 0]
41 set blob [execsql {SELECT x'';}]
42 bin_to_hex [lindex $blob 0]
45 set blob [execsql {SELECT x'abcdEF12';}]
46 bin_to_hex [lindex $blob 0]
49 set blob [execsql {SELECT x'0123456789abcdefABCDEF';}]
50 bin_to_hex [lindex $blob 0]
51 } {0123456789ABCDEFABCDEF}
53 # Try some syntax errors in blob literals.
55 catchsql {SELECT X'01020k304', 100}
56 } {1 {unrecognized token: "X'01020k304'"}}
58 catchsql {SELECT X'01020, 100}
59 } {1 {unrecognized token: "X'01020, 100"}}
61 catchsql {SELECT X'01020 100'}
62 } {1 {unrecognized token: "X'01020 100'"}}
64 catchsql {SELECT X'01001'}
65 } {1 {unrecognized token: "X'01001'"}}
67 catchsql {SELECT x'012/45'}
68 } {1 {unrecognized token: "x'012/45'"}}
70 catchsql {SELECT x'012:45'}
71 } {1 {unrecognized token: "x'012:45'"}}
73 catchsql {SELECT x'012@45'}
74 } {1 {unrecognized token: "x'012@45'"}}
76 catchsql {SELECT x'012G45'}
77 } {1 {unrecognized token: "x'012G45'"}}
79 catchsql {SELECT x'012`45'}
80 } {1 {unrecognized token: "x'012`45'"}}
82 catchsql {SELECT x'012g45'}
83 } {1 {unrecognized token: "x'012g45'"}}
86 # Insert a blob into a table and retrieve it.
89 CREATE TABLE t1(a BLOB, b BLOB);
90 INSERT INTO t1 VALUES(X'123456', x'7890ab');
91 INSERT INTO t1 VALUES(X'CDEF12', x'345678');
93 set blobs [execsql {SELECT * FROM t1}]
95 foreach b $blobs {lappend blobs2 [bin_to_hex $b]}
97 } {123456 7890AB CDEF12 345678}
99 # An index on a blob column
102 CREATE INDEX i1 ON t1(a);
104 set blobs [execsql {SELECT * FROM t1}]
106 foreach b $blobs {lappend blobs2 [bin_to_hex $b]}
108 } {123456 7890AB CDEF12 345678}
110 set blobs [execsql {SELECT * FROM t1 where a = X'123456'}]
112 foreach b $blobs {lappend blobs2 [bin_to_hex $b]}
116 set blobs [execsql {SELECT * FROM t1 where a = X'CDEF12'}]
118 foreach b $blobs {lappend blobs2 [bin_to_hex $b]}
122 set blobs [execsql {SELECT * FROM t1 where a = X'CD12'}]
124 foreach b $blobs {lappend blobs2 [bin_to_hex $b]}
128 # Try to bind a blob value to a prepared statement.
131 set DB [sqlite3_connection_pointer db2]
132 set STMT [sqlite3_prepare $DB "DELETE FROM t1 WHERE a = ?" -1 DUMMY]
133 sqlite3_bind_blob $STMT 1 "\x12\x34\x56" 3
137 sqlite3_finalize $STMT
141 set blobs [execsql {SELECT * FROM t1}]
143 foreach b $blobs {lappend blobs2 [bin_to_hex $b]}