os/persistentdata/persistentstorage/sqlite3api/TEST/TclScript/blob.test
author sl@SLION-WIN7.fritz.box
Fri, 15 Jun 2012 03:10:57 +0200
changeset 0 bde4ae8d615e
permissions -rw-r--r--
First public contribution.
sl@0
     1
# 2001 September 15
sl@0
     2
#
sl@0
     3
# The author disclaims copyright to this source code.  In place of
sl@0
     4
# a legal notice, here is a blessing:
sl@0
     5
#
sl@0
     6
#    May you do good and not evil.
sl@0
     7
#    May you find forgiveness for yourself and forgive others.
sl@0
     8
#    May you share freely, never taking more than you give.
sl@0
     9
#
sl@0
    10
#***********************************************************************
sl@0
    11
# This file implements regression tests for SQLite library.
sl@0
    12
#
sl@0
    13
# $Id: blob.test,v 1.7 2008/04/04 15:12:22 drh Exp $
sl@0
    14
sl@0
    15
set testdir [file dirname $argv0]
sl@0
    16
source $testdir/tester.tcl
sl@0
    17
sl@0
    18
ifcapable {!bloblit} {
sl@0
    19
  finish_test
sl@0
    20
  return
sl@0
    21
}
sl@0
    22
sl@0
    23
proc bin_to_hex {blob} {
sl@0
    24
  set bytes {}
sl@0
    25
  binary scan $blob \c* bytes
sl@0
    26
  set bytes2 [list]
sl@0
    27
  foreach b $bytes {lappend bytes2 [format %02X [expr $b & 0xFF]]}
sl@0
    28
  join $bytes2 {}
sl@0
    29
}
sl@0
    30
sl@0
    31
# Simplest possible case. Specify a blob literal
sl@0
    32
do_test blob-1.0 {
sl@0
    33
  set blob [execsql {SELECT X'01020304';}]
sl@0
    34
  bin_to_hex [lindex $blob 0]
sl@0
    35
} {01020304}
sl@0
    36
do_test blob-1.1 {
sl@0
    37
  set blob [execsql {SELECT x'ABCDEF';}]
sl@0
    38
  bin_to_hex [lindex $blob 0]
sl@0
    39
} {ABCDEF}
sl@0
    40
do_test blob-1.2 {
sl@0
    41
  set blob [execsql {SELECT x'';}]
sl@0
    42
  bin_to_hex [lindex $blob 0]
sl@0
    43
} {}
sl@0
    44
do_test blob-1.3 {
sl@0
    45
  set blob [execsql {SELECT x'abcdEF12';}]
sl@0
    46
  bin_to_hex [lindex $blob 0]
sl@0
    47
} {ABCDEF12}
sl@0
    48
do_test blob-1.3.2 {
sl@0
    49
  set blob [execsql {SELECT x'0123456789abcdefABCDEF';}]
sl@0
    50
  bin_to_hex [lindex $blob 0]
sl@0
    51
} {0123456789ABCDEFABCDEF}
sl@0
    52
sl@0
    53
# Try some syntax errors in blob literals.
sl@0
    54
do_test blob-1.4 {
sl@0
    55
  catchsql {SELECT X'01020k304', 100}
sl@0
    56
} {1 {unrecognized token: "X'01020k304'"}}
sl@0
    57
do_test blob-1.5 {
sl@0
    58
  catchsql {SELECT X'01020, 100}
sl@0
    59
} {1 {unrecognized token: "X'01020, 100"}}
sl@0
    60
do_test blob-1.6 {
sl@0
    61
  catchsql {SELECT X'01020 100'}
sl@0
    62
} {1 {unrecognized token: "X'01020 100'"}}
sl@0
    63
do_test blob-1.7 {
sl@0
    64
  catchsql {SELECT X'01001'}
sl@0
    65
} {1 {unrecognized token: "X'01001'"}}
sl@0
    66
do_test blob-1.8 {
sl@0
    67
  catchsql {SELECT x'012/45'}
sl@0
    68
} {1 {unrecognized token: "x'012/45'"}}
sl@0
    69
do_test blob-1.9 {
sl@0
    70
  catchsql {SELECT x'012:45'}
sl@0
    71
} {1 {unrecognized token: "x'012:45'"}}
sl@0
    72
do_test blob-1.10 {
sl@0
    73
  catchsql {SELECT x'012@45'}
sl@0
    74
} {1 {unrecognized token: "x'012@45'"}}
sl@0
    75
do_test blob-1.11 {
sl@0
    76
  catchsql {SELECT x'012G45'}
sl@0
    77
} {1 {unrecognized token: "x'012G45'"}}
sl@0
    78
do_test blob-1.12 {
sl@0
    79
  catchsql {SELECT x'012`45'}
sl@0
    80
} {1 {unrecognized token: "x'012`45'"}}
sl@0
    81
do_test blob-1.13 {
sl@0
    82
  catchsql {SELECT x'012g45'}
sl@0
    83
} {1 {unrecognized token: "x'012g45'"}}
sl@0
    84
sl@0
    85
sl@0
    86
# Insert a blob into a table and retrieve it.
sl@0
    87
do_test blob-2.0 {
sl@0
    88
  execsql {
sl@0
    89
    CREATE TABLE t1(a BLOB, b BLOB);
sl@0
    90
    INSERT INTO t1 VALUES(X'123456', x'7890ab');
sl@0
    91
    INSERT INTO t1 VALUES(X'CDEF12', x'345678');
sl@0
    92
  }
sl@0
    93
  set blobs [execsql {SELECT * FROM t1}]
sl@0
    94
  set blobs2 [list]
sl@0
    95
  foreach b $blobs {lappend blobs2 [bin_to_hex $b]}
sl@0
    96
  set blobs2
sl@0
    97
} {123456 7890AB CDEF12 345678}
sl@0
    98
sl@0
    99
# An index on a blob column
sl@0
   100
do_test blob-2.1 {
sl@0
   101
  execsql {
sl@0
   102
    CREATE INDEX i1 ON t1(a);
sl@0
   103
  }
sl@0
   104
  set blobs [execsql {SELECT * FROM t1}]
sl@0
   105
  set blobs2 [list]
sl@0
   106
  foreach b $blobs {lappend blobs2 [bin_to_hex $b]}
sl@0
   107
  set blobs2
sl@0
   108
} {123456 7890AB CDEF12 345678}
sl@0
   109
do_test blob-2.2 {
sl@0
   110
  set blobs [execsql {SELECT * FROM t1 where a = X'123456'}]
sl@0
   111
  set blobs2 [list]
sl@0
   112
  foreach b $blobs {lappend blobs2 [bin_to_hex $b]}
sl@0
   113
  set blobs2
sl@0
   114
} {123456 7890AB}
sl@0
   115
do_test blob-2.3 {
sl@0
   116
  set blobs [execsql {SELECT * FROM t1 where a = X'CDEF12'}]
sl@0
   117
  set blobs2 [list]
sl@0
   118
  foreach b $blobs {lappend blobs2 [bin_to_hex $b]}
sl@0
   119
  set blobs2
sl@0
   120
} {CDEF12 345678}
sl@0
   121
do_test blob-2.4 {
sl@0
   122
  set blobs [execsql {SELECT * FROM t1 where a = X'CD12'}]
sl@0
   123
  set blobs2 [list]
sl@0
   124
  foreach b $blobs {lappend blobs2 [bin_to_hex $b]}
sl@0
   125
  set blobs2
sl@0
   126
} {}
sl@0
   127
sl@0
   128
# Try to bind a blob value to a prepared statement.
sl@0
   129
do_test blob-3.0 {
sl@0
   130
  sqlite3 db2 test.db
sl@0
   131
  set DB [sqlite3_connection_pointer db2]
sl@0
   132
  set STMT [sqlite3_prepare $DB "DELETE FROM t1 WHERE a = ?" -1 DUMMY]
sl@0
   133
  sqlite3_bind_blob $STMT 1 "\x12\x34\x56" 3
sl@0
   134
  sqlite3_step $STMT
sl@0
   135
} {SQLITE_DONE}
sl@0
   136
do_test blob-3.1 {
sl@0
   137
  sqlite3_finalize $STMT
sl@0
   138
  db2 close
sl@0
   139
} {}
sl@0
   140
do_test blob-2.3 {
sl@0
   141
  set blobs [execsql {SELECT * FROM t1}]
sl@0
   142
  set blobs2 [list]
sl@0
   143
  foreach b $blobs {lappend blobs2 [bin_to_hex $b]}
sl@0
   144
  set blobs2
sl@0
   145
} {CDEF12 345678}
sl@0
   146
sl@0
   147
finish_test