Bumped revision number.
[dbsrgits/SQL-Abstract.git] / t / 02where.t
CommitLineData
41751122 1#!/usr/bin/perl
32eab2da 2
3use strict;
41751122 4use warnings;
5use Test::More;
8a68b5be 6use Test::Exception;
5aad8cf3 7use SQL::Abstract::Test import => ['is_same_sql_bind'];
fffe6900 8
32eab2da 9use SQL::Abstract;
10
11# Make sure to test the examples, since having them break is somewhat
12# embarrassing. :-(
13
5ebfe5f2 14my $not_stringifiable = bless {}, 'SQLA::NotStringifiable';
ba566dc3 15
32eab2da 16my @handle_tests = (
17 {
18 where => {
19 requestor => 'inna',
20 worker => ['nwiger', 'rcwe', 'sfz'],
21 status => { '!=', 'completed' }
22 },
23 order => [],
24 stmt => " WHERE ( requestor = ? AND status != ? AND ( ( worker = ? ) OR"
25 . " ( worker = ? ) OR ( worker = ? ) ) )",
26 bind => [qw/inna completed nwiger rcwe sfz/],
27 },
28
29 {
30 where => {
31 user => 'nwiger',
32 status => 'completed'
33 },
34 order => [qw/ticket/],
35 stmt => " WHERE ( status = ? AND user = ? ) ORDER BY ticket",
36 bind => [qw/completed nwiger/],
37 },
38
39 {
40 where => {
41 user => 'nwiger',
42 status => { '!=', 'completed' }
43 },
44 order => [qw/ticket/],
45 stmt => " WHERE ( status != ? AND user = ? ) ORDER BY ticket",
46 bind => [qw/completed nwiger/],
47 },
48
49 {
50 where => {
51 status => 'completed',
52 reportid => { 'in', [567, 2335, 2] }
53 },
54 order => [],
55 stmt => " WHERE ( reportid IN ( ?, ?, ? ) AND status = ? )",
56 bind => [qw/567 2335 2 completed/],
57 },
58
59 {
60 where => {
61 status => 'completed',
62 reportid => { 'not in', [567, 2335, 2] }
63 },
64 order => [],
65 stmt => " WHERE ( reportid NOT IN ( ?, ?, ? ) AND status = ? )",
66 bind => [qw/567 2335 2 completed/],
67 },
68
69 {
70 where => {
71 status => 'completed',
72 completion_date => { 'between', ['2002-10-01', '2003-02-06'] },
73 },
74 order => \'ticket, requestor',
96449e8e 75#LDNOTE: modified parentheses
76# stmt => " WHERE ( completion_date BETWEEN ? AND ? AND status = ? ) ORDER BY ticket, requestor",
77 stmt => " WHERE ( ( completion_date BETWEEN ? AND ? ) AND status = ? ) ORDER BY ticket, requestor",
32eab2da 78 bind => [qw/2002-10-01 2003-02-06 completed/],
79 },
80
81 {
82 where => [
83 {
84 user => 'nwiger',
85 status => { 'in', ['pending', 'dispatched'] },
86 },
87 {
88 user => 'robot',
89 status => 'unassigned',
90 },
91 ],
92 order => [],
93 stmt => " WHERE ( ( status IN ( ?, ? ) AND user = ? ) OR ( status = ? AND user = ? ) )",
94 bind => [qw/pending dispatched nwiger unassigned robot/],
95 },
96
97 {
98 where => {
99 priority => [ {'>', 3}, {'<', 1} ],
100 requestor => \'is not null',
101 },
102 order => 'priority',
103 stmt => " WHERE ( ( ( priority > ? ) OR ( priority < ? ) ) AND requestor is not null ) ORDER BY priority",
104 bind => [qw/3 1/],
105 },
106
107 {
108 where => {
109 priority => [ {'>', 3}, {'<', 1} ],
110 requestor => { '!=', undef },
111 },
112 order => [qw/a b c d e f g/],
113 stmt => " WHERE ( ( ( priority > ? ) OR ( priority < ? ) ) AND requestor IS NOT NULL )"
114 . " ORDER BY a, b, c, d, e, f, g",
115 bind => [qw/3 1/],
116 },
117
118 {
119 where => {
120 priority => { 'between', [1, 3] },
121 requestor => { 'like', undef },
122 },
123 order => \'requestor, ticket',
96449e8e 124#LDNOTE: modified parentheses
125# stmt => " WHERE ( priority BETWEEN ? AND ? AND requestor IS NULL ) ORDER BY requestor, ticket",
126 stmt => " WHERE ( ( priority BETWEEN ? AND ? ) AND requestor IS NULL ) ORDER BY requestor, ticket",
32eab2da 127 bind => [qw/1 3/],
128 },
129
130
131 {
132 where => {
133 id => 1,
134 num => {
135 '<=' => 20,
136 '>' => 10,
137 },
138 },
96449e8e 139# LDNOTE : modified test below, just parentheses differ
140# stmt => " WHERE ( id = ? AND num <= ? AND num > ? )",
141 stmt => " WHERE ( id = ? AND ( num <= ? AND num > ? ) )",
32eab2da 142 bind => [qw/1 20 10/],
143 },
144
145 {
146 where => { foo => {-not_like => [7,8,9]},
147 fum => {'like' => [qw/a b/]},
148 nix => {'between' => [100,200] },
149 nox => {'not between' => [150,160] },
150 wix => {'in' => [qw/zz yy/]},
151 wux => {'not_in' => [qw/30 40/]}
152 },
96449e8e 153# LDNOTE: modified parentheses for BETWEEN (trivial).
154# Also modified the logic of "not_like" (severe, same reasons as #14 in 00where.t)
155# stmt => " WHERE ( ( ( foo NOT LIKE ? ) OR ( foo NOT LIKE ? ) OR ( foo NOT LIKE ? ) ) AND ( ( fum LIKE ? ) OR ( fum LIKE ? ) ) AND nix BETWEEN ? AND ? AND nox NOT BETWEEN ? AND ? AND wix IN ( ?, ? ) AND wux NOT IN ( ?, ? ) )",
156 stmt => " WHERE ( ( foo NOT LIKE ? AND foo NOT LIKE ? AND foo NOT LIKE ? ) AND ( ( fum LIKE ? ) OR ( fum LIKE ? ) ) AND ( nix BETWEEN ? AND ? ) AND ( nox NOT BETWEEN ? AND ? ) AND wix IN ( ?, ? ) AND wux NOT IN ( ?, ? ) )",
32eab2da 157 bind => [7,8,9,'a','b',100,200,150,160,'zz','yy','30','40'],
158 },
159
8a68b5be 160 {
161 where => {
162 id => [],
163 bar => {'!=' => []},
164 },
165 stmt => " WHERE ( 1=1 AND 0=1 )",
166 bind => [],
167 },
168
96449e8e 169
170 {
171 where => {
172 foo => \["IN (?, ?)", 22, 33],
173 bar => [-and => \["> ?", 44], \["< ?", 55] ],
174 },
175 stmt => " WHERE ( (bar > ? AND bar < ?) AND foo IN (?, ?) )",
176 bind => [44, 55, 22, 33],
177 },
178
4b7b6026 179 {
180 where => { -and => [{}, { 'me.id' => '1'}] },
181 stmt => " WHERE ( ( me.id = ? ) )",
182 bind => [ 1 ],
183 },
184
fffe6900 185 {
ba566dc3 186 where => { foo => $not_stringifiable, },
187 stmt => " WHERE ( foo = ? )",
188 bind => [ $not_stringifiable ],
189 },
190
32eab2da 191);
192
6dcf723c 193
194plan tests => scalar(@handle_tests) + 1;
195
8a68b5be 196for my $case (@handle_tests) {
32eab2da 197 my $sql = SQL::Abstract->new;
8a68b5be 198 my($stmt, @bind) = $sql->where($case->{where}, $case->{order});
96449e8e 199 is_same_sql_bind($stmt, \@bind, $case->{stmt}, $case->{bind})
32eab2da 200}
201
8a68b5be 202dies_ok {
203 my $sql = SQL::Abstract->new;
204 $sql->where({ foo => { '>=' => [] }},);
fffe6900 205};