7 use SQL::Abstract::Test import => ['is_same_sql_bind'];
12 # Make sure to test the examples, since having them break is somewhat
15 my $not_stringifiable = bless {}, 'SQLA::NotStringifiable';
21 worker => ['nwiger', 'rcwe', 'sfz'],
22 status => { '!=', 'completed' }
25 stmt => " WHERE ( requestor = ? AND status != ? AND ( ( worker = ? ) OR"
26 . " ( worker = ? ) OR ( worker = ? ) ) )",
27 bind => [qw/inna completed nwiger rcwe sfz/],
32 status => 'completed',
35 stmt => " WHERE ( status = ? OR user = ? )",
36 bind => [qw/completed nwiger/],
44 order => [qw/ticket/],
45 stmt => " WHERE ( status = ? AND user = ? ) ORDER BY ticket",
46 bind => [qw/completed nwiger/],
52 status => { '!=', 'completed' }
54 order => [qw/ticket/],
55 stmt => " WHERE ( status != ? AND user = ? ) ORDER BY ticket",
56 bind => [qw/completed nwiger/],
61 status => 'completed',
62 reportid => { 'in', [567, 2335, 2] }
65 stmt => " WHERE ( reportid IN ( ?, ?, ? ) AND status = ? )",
66 bind => [qw/567 2335 2 completed/],
71 status => 'completed',
72 reportid => { 'not in', [567, 2335, 2] }
75 stmt => " WHERE ( reportid NOT IN ( ?, ?, ? ) AND status = ? )",
76 bind => [qw/567 2335 2 completed/],
81 status => 'completed',
82 completion_date => { 'between', ['2002-10-01', '2003-02-06'] },
84 order => \'ticket, requestor',
85 #LDNOTE: modified parentheses
88 stmt => "WHERE ( ( completion_date BETWEEN ? AND ? ) AND status = ? ) ORDER BY ticket, requestor",
89 bind => [qw/2002-10-01 2003-02-06 completed/],
96 status => { 'in', ['pending', 'dispatched'] },
100 status => 'unassigned',
104 stmt => " WHERE ( ( status IN ( ?, ? ) AND user = ? ) OR ( status = ? AND user = ? ) )",
105 bind => [qw/pending dispatched nwiger unassigned robot/],
110 priority => [ {'>', 3}, {'<', 1} ],
111 requestor => \'is not null',
114 stmt => " WHERE ( ( ( priority > ? ) OR ( priority < ? ) ) AND requestor is not null ) ORDER BY priority",
120 priority => [ {'>', 3}, {'<', 1} ],
121 requestor => { '!=', undef },
123 order => [qw/a b c d e f g/],
124 stmt => " WHERE ( ( ( priority > ? ) OR ( priority < ? ) ) AND requestor IS NOT NULL )"
125 . " ORDER BY a, b, c, d, e, f, g",
131 priority => { 'between', [1, 3] },
132 requestor => { 'like', undef },
134 order => \'requestor, ticket',
135 #LDNOTE: modified parentheses
138 stmt => " WHERE ( ( priority BETWEEN ? AND ? ) AND requestor IS NULL ) ORDER BY requestor, ticket",
151 # LDNOTE : modified test below, just parentheses differ
154 stmt => " WHERE ( id = ? AND ( num <= ? AND num > ? ) )",
155 bind => [qw/1 20 10/],
159 # LDNOTE 23.03.09 : modified test below, just parentheses differ
160 where => { foo => {-not_like => [7,8,9]},
161 fum => {'like' => [qw/a b/]},
162 nix => {'between' => [100,200] },
163 nox => {'not between' => [150,160] },
164 wix => {'in' => [qw/zz yy/]},
165 wux => {'not_in' => [qw/30 40/]}
167 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 ( ?, ? ) )",
168 bind => [7,8,9,'a','b',100,200,150,160,'zz','yy','30','40'],
176 stmt => " WHERE ( 1=1 AND 0=1 )",
183 foo => \["IN (?, ?)", 22, 33],
184 bar => [-and => \["> ?", 44], \["< ?", 55] ],
186 stmt => " WHERE ( (bar > ? AND bar < ?) AND foo IN (?, ?) )",
187 bind => [44, 55, 22, 33],
190 where => { -and => [{}, { 'me.id' => '1'}] },
191 stmt => " WHERE ( ( me.id = ? ) )",
196 where => { foo => $not_stringifiable, },
197 stmt => " WHERE ( foo = ? )",
198 bind => [ $not_stringifiable ],
202 where => \[ 'foo = ?','bar' ],
203 stmt => " WHERE (foo = ?)",
208 where => [ \[ 'foo = ?','bar' ] ],
209 stmt => " WHERE (foo = ?)",
214 plan tests => ( @handle_tests * 2 ) + 1;
216 for my $case (@handle_tests) {
217 local $Data::Dumper::Terse = 1;
218 my $sql = SQL::Abstract->new;
221 ($stmt, @bind) = $sql->where($case->{where}, $case->{order});
222 is_same_sql_bind($stmt, \@bind, $case->{stmt}, $case->{bind})
223 || diag "Search term:\n" . Dumper $case->{where};
228 my $sql = SQL::Abstract->new;
229 $sql->where({ foo => { '>=' => [] }},);