xref: /sqlite-3.40.0/test/rowvalue5.test (revision 067b92ba)
1# 2016 July 29
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# This file implements regression tests for SQLite library.  The
12# focus of this file is syntax errors involving row-values and
13# virtual tables.
14#
15
16set testdir [file dirname $argv0]
17source $testdir/tester.tcl
18set ::testprefix rowvalue5
19
20ifcapable !vtab {
21  finish_test
22  return
23}
24
25proc vtab_command {method args} {
26  switch -- $method {
27    xConnect {
28      return "CREATE TABLE t1(a, b, c, d, expr)"
29    }
30
31    xBestIndex {
32      set COL(0) a
33      set COL(1) b
34      set COL(2) c
35      set COL(3) d
36      set COL(4) expr
37
38      set OP(eq) =
39      set OP(ne) !=
40      set OP(gt) >
41      set OP(le) <=
42      set OP(lt) <
43      set OP(ge) >=
44      set OP(match) MATCH
45      set OP(like) LIKE
46      set OP(glob) GLOB
47      set OP(regexp) REGEXP
48
49      set clist [lindex $args 0]
50      set ret [list]
51      set elist [list]
52      set i 0
53      foreach c $clist {
54        array set C $c
55        if {$C(usable)} {
56          lappend ret omit $i
57          lappend elist "$COL($C(column)) $OP($C(op)) %$i%"
58        }
59        incr i
60      }
61
62      lappend ret idxstr [join $elist " AND "]
63      #puts "xBestIndex: $ret"
64      return $ret
65    }
66
67    xFilter {
68      foreach {idxnum idxstr arglist} $args {}
69      set i 0
70      set ee $idxstr
71      foreach a $arglist {
72        if {[string is double $a]==0} {
73          set a "'[string map {' ''} $a]'"
74        }
75        set ee [string map [list "%$i%" $a] $ee]
76        incr i
77      }
78      set ee [string map [list "'" "''"] $ee]
79
80      set ret [list sql "SELECT 1, 'a', 'b', 'c', 'd', '$ee'"]
81      #puts "xFilter: $ret"
82      return $ret
83    }
84  }
85
86  return {}
87}
88
89register_tcl_module db
90do_execsql_test 1.0 {
91  CREATE VIRTUAL TABLE x1 USING tcl(vtab_command);
92} {}
93
94
95foreach {tn where res} {
96  1 "1"                              {{}}
97  2 "a=1"                            {{a = 1}}
98  3 "a=1 AND 4 = b"                  {{a = 1 AND b = 4}}
99  4 "c>'hello'"                      {{c > 'hello'}}
100  5 "c<='hel''lo'"                   {{c <= 'hel''lo'}}
101  6 "(a, b) = (SELECT 9, 10)"        {{a = 9 AND b = 10}}
102  7 "(+a, b) = (SELECT 'a', 'b')"    {{b = 'b'}}
103  8 "(a, +b) = (SELECT 'a', 'b')"    {{a = 'a'}}
104  11 "(+a, b) IN (SELECT 'a', 'b')"  {{b = 'b'}}
105  12 "(a, +b) IN (SELECT 'a', 'b')"  {{a = 'a'}}
106
107  13 "(a, b) < ('d', 'e')"           {{a <= 'd'}}
108  14 "(a, b) < ('a', 'c')"           {{a <= 'a'}}
109  15 "(a, b) <= ('a', 'b')"          {{a <= 'a'}}
110  16 "(a, b) < ('a', 'b')"           {}
111} {
112  do_execsql_test 1.$tn "SELECT expr FROM x1 WHERE $where" $res
113}
114
115finish_test
116