c8d3ce19b4c97c91dc7a9f1631a7aff1dd10a6fc
[dbsrgits/SQL-Abstract.git] / t / 04modifiers.t
1 #!/usr/bin/perl
2
3 use strict;
4 use warnings;
5 use Test::More;
6 use Test::Exception;
7 use SQL::Abstract::Test import => ['is_same_sql_bind'];
8
9 use Data::Dumper;
10 use SQL::Abstract;
11 use Storable 'dclone';
12
13 #### WARNING ####
14 #
15 # -nest has been undocumented on purpose, but is still supported for the
16 # foreseable future. Do not rip out the -nest tests before speaking to
17 # someone on the DBIC mailing list or in irc.perl.org#dbix-class
18 #
19 #################
20
21 =begin
22 Test -and -or and -nest modifiers, assuming the following:
23
24   * Modifiers are respected in both hashrefs and arrayrefs (with the obvious
25     limitation of one modifier type per hahsref)
26   * When in condition context i.e. where => { -or { a = 1 } }, each modifier
27     affects only the immediate element following it.
28   * When in column multi-condition context i.e. 
29     where => { x => { '!=', [-and, [qw/1 2 3/]] } }, a modifier affects the
30     OUTER ARRAYREF if and only if it is the first element of said ARRAYREF
31
32 =cut
33
34 # no warnings (the -or/-and => { } warning is silly, there is nothing wrong with such usage)
35 my $and_or_args = {
36   and => { stmt => 'WHERE a = ? AND b = ?', bind => [qw/1 2/] },
37   or => { stmt => 'WHERE a = ? OR b = ?', bind => [qw/1 2/] },
38   or_and => { stmt => 'WHERE ( foo = ? OR bar = ? ) AND baz = ? ', bind => [qw/1 2 3/] },
39   or_or => { stmt => 'WHERE foo = ? OR bar = ? OR baz = ?', bind => [qw/1 2 3/] },
40   and_or => { stmt => 'WHERE ( foo = ? AND bar = ? ) OR baz = ?', bind => [qw/1 2 3/] },
41 };
42
43 my @and_or_tests = (
44   # basic tests
45   {
46     where => { -and => [a => 1, b => 2] },
47     %{$and_or_args->{and}},
48   },
49   {
50     where => [ -and => [a => 1, b => 2] ],
51     %{$and_or_args->{and}},
52   },
53   {
54     where => { -or => [a => 1, b => 2] },
55     %{$and_or_args->{or}},
56   },
57   {
58     where => [ -or => [a => 1, b => 2] ],
59     %{$and_or_args->{or}},
60   },
61
62   {
63     where => { -and => {a => 1, b => 2} },
64     %{$and_or_args->{and}},
65   },
66   {
67     where => [ -and => {a => 1, b => 2} ],
68     %{$and_or_args->{and}},
69   },
70   {
71     where => { -or => {a => 1, b => 2} },
72     %{$and_or_args->{or}},
73   },
74   {
75     where => [ -or => {a => 1, b => 2} ],
76     %{$and_or_args->{or}},
77   },
78
79   # test modifiers within hashrefs 
80   {
81     where => { -or => [
82       [ foo => 1, bar => 2 ],
83       baz => 3,
84     ]},
85     %{$and_or_args->{or_or}},
86   },
87   {
88     where => { -and => [
89       [ foo => 1, bar => 2 ],
90       baz => 3,
91     ]},
92     %{$and_or_args->{or_and}},
93   },
94
95   # test modifiers within arrayrefs 
96   {
97     where => [ -or => [
98       [ foo => 1, bar => 2 ],
99       baz => 3,
100     ]],
101     %{$and_or_args->{or_or}},
102   },
103   {
104     where => [ -and => [
105       [ foo => 1, bar => 2 ],
106       baz => 3,
107     ]],
108     %{$and_or_args->{or_and}},
109   },
110
111   # test ambiguous modifiers within hashrefs (op extends to to immediate RHS only)
112   {
113     where => { -and => [ -or =>
114       [ foo => 1, bar => 2 ],
115       baz => 3,
116     ]},
117     %{$and_or_args->{or_and}},
118   },
119   {
120     where => { -or => [ -and =>
121       [ foo => 1, bar => 2 ],
122       baz => 3,
123     ]},
124     %{$and_or_args->{and_or}},
125   },
126
127   # test ambiguous modifiers within arrayrefs (op extends to to immediate RHS only)
128   {
129     where => [ -and => [ -or =>
130       [ foo => 1, bar => 2 ],
131       baz => 3,
132     ]],
133     %{$and_or_args->{or_and}},
134   },
135   {
136     where => [ -or => [ -and =>
137       [ foo => 1, bar => 2 ],
138       baz => 3
139     ]],
140     %{$and_or_args->{and_or}},
141   },
142
143   # test column multi-cond in arrayref (useless example)
144   {
145     where => { x => [ -and => (1 .. 3) ] },
146     stmt => 'WHERE x = ? AND x = ? AND x = ?',
147     bind => [1..3],
148   },
149   # test column multi-cond in arrayref (more useful)
150   {
151     where => { x => [ -and => {'!=' => 1}, {'!=' => 2}, {'!=' => 3} ] },
152     stmt => 'WHERE x != ? AND x != ? AND x != ?',
153     bind => [1..3],
154   },
155   # test column multi-cond in arrayref (even more useful)
156   {
157     where => { x => { '!=' => [ -and => (1 .. 3) ] } },
158     stmt => 'WHERE x != ? AND x != ? AND x != ?',
159     bind => [1..3],
160   },
161
162   # the -or should affect only the inner hashref, as we are not in an outer arrayref
163   {
164     where => { x => {
165       -or => { '!=', 1, '>=', 2 }, -like => 'x%'
166     }},
167     stmt => 'WHERE x LIKE ? AND ( x != ? OR x >= ? )',
168     bind => [qw/x% 1 2/],
169   },
170
171   # the -and should affect the OUTER arrayref, while the internal structures remain intact
172   {
173     where => { x => [ 
174       -and => [ 1, 2 ], { -like => 'x%' } 
175     ]},
176     stmt => 'WHERE (x = ? OR x = ?) AND x LIKE ?',
177     bind => [qw/1 2 x%/],
178   },
179
180   {
181     where => { -and => [a => 1, b => 2], x => 9, -or => { c => 3, d => 4 } },
182     stmt => 'WHERE a = ? AND b = ? AND ( c = ? OR d = ? ) AND x = ?',
183     bind => [qw/1 2 3 4 9/],
184   },
185
186   {
187     where => { -and => [a => 1, b => 2, k => [11, 12] ], x => 9, -or => { c => 3, d => 4, l => { '=' => [21, 22] } } },
188     stmt => 'WHERE a = ? AND b = ? AND (k = ? OR k = ?) AND (c = ? OR d = ? OR (l = ? OR l = ?) ) AND x = ?',
189     bind => [qw/1 2 11 12 3 4 21 22 9/],
190   },
191
192   {
193     where => { -or => [a => 1, b => 2, k => [11, 12] ], x => 9, -and => { c => 3, d => 4, l => { '=' => [21, 22] } } },
194     stmt => 'WHERE c = ? AND d = ? AND ( l = ? OR l = ?) AND (a = ? OR b = ? OR k = ? OR k = ?) AND x = ?',
195     bind => [qw/3 4 21 22 1 2 11 12 9/],
196   },
197
198   {
199     where => [ -or => [a => 1, b => 2], -or => { c => 3, d => 4}, e => 5, -and => [ f => 6, g => 7], [ h => 8, i => 9, -and => [ k => 10, l => 11] ], { m => 12, n => 13 }],
200     stmt => 'WHERE a = ? OR b = ? OR c = ? OR d = ? OR e = ? OR ( f = ? AND g = ?) OR h = ? OR i = ? OR ( k = ? AND l = ? ) OR (m = ? AND n = ?)',
201     bind => [1 .. 13],
202   },
203
204   {
205     # explicit OR logic in arrays should leave everything intact
206     args => { logic => 'or' },
207     where => { -and => [a => 1, b => 2, k => [11, 12] ], x => 9, -or => { c => 3, d => 4, l => { '=' => [21, 22] } }  },
208     stmt => 'WHERE a = ? AND b = ? AND (k = ? OR k = ?) AND ( c = ? OR d = ? OR l = ? OR l = ? ) AND x = ? ',
209     bind => [qw/1 2 11 12 3 4 21 22 9/],
210   },
211
212   {
213     # flip logic in arrays except where excplicitly requested otherwise
214     args => { logic => 'and' },
215     where => [ -or => [a => 1, b => 2], -or => { c => 3, d => 4}, e => 5, -and => [ f => 6, g => 7], [ h => 8, i => 9, -and => [ k => 10, l => 11] ], { m => 12, n => 13 }],
216     stmt => 'WHERE (a = ? OR b = ?) AND (c = ? OR d = ?) AND e = ? AND f = ? AND g = ? AND h = ? AND i = ? AND k = ? AND l = ? AND m = ? AND n = ?',
217     bind => [1 .. 13],
218   },
219
220   # 1st -and is in column mode, thus flips the entire array, whereas the 
221   # 2nd one is just a condition modifier
222   {
223     where => [
224       col => [ -and => {'<' => 123}, {'>' => 456 }, {'!=' => 789} ],
225       -and => [
226         col2 => [ -or => { -like => 'crap' }, { -like => 'crop' } ],
227         col3 => [ -and => { -like => 'chap' }, { -like => 'chop' } ],
228       ],
229     ],
230     stmt => 'WHERE
231       (col < ? AND col > ? AND col != ?)
232         OR
233       (
234         ( col2 LIKE ? OR col2 LIKE ? )
235           AND
236         ( col3 LIKE ? AND col3 LIKE ? )
237       )
238     ',
239     bind => [qw/123 456 789 crap crop chap chop/],
240   },
241
242   ##########
243   # some corner cases by ldami (some produce useless SQL, just for clarification on 1.5 direction)
244   #
245
246   {
247     where => { foo => [
248       -and => [ { -like => 'foo%'}, {'>' => 'moo'} ],
249       { -like => '%bar', '<' => 'baz'},
250       [ {-like => '%alpha'}, {-like => '%beta'} ],
251       [ {'!=' => 'toto', '=' => 'koko'} ],
252     ] },
253     stmt => 'WHERE (foo LIKE ? OR foo > ?) AND (foo LIKE ? AND foo < ?) AND (foo LIKE ? OR foo LIKE ?) AND (foo != ? AND foo = ?)',
254     bind => [qw/foo% moo %bar baz %alpha %beta toto koko/],
255   },
256
257   {
258     where => [
259       -and => [a => 1, b => 2],
260       -or => [c => 3, d => 4],
261       e => [-and => {-like => 'foo%'}, {-like => '%bar'} ],
262     ],
263     stmt => 'WHERE (a = ? AND b = ?) OR c = ? OR d = ? OR (e LIKE ? AND e LIKE ?)',
264     bind => [qw/1 2 3 4 foo% %bar/],
265   },
266
267   # -or has nothing to flip
268   {
269     where => [-and => [{foo => 1}, {bar => 2}, -or => {baz => 3}] ],
270     stmt => 'WHERE foo = ? AND bar = ? AND baz = ?',
271     bind => [1 .. 3],
272   },
273   {
274     where => [-and => [{foo => 1}, {bar => 2}, -or => {baz => 3, woz => 4} ] ],
275     stmt => 'WHERE foo = ? AND bar = ? AND (baz = ? OR woz = ?)',
276     bind => [1 .. 4],
277   },
278
279   # -and has only 1 following element, thus all still ORed
280   {
281     where => { col => [ -and => [{'<' => 123}, {'>' => 456 }, {'!=' => 789}] ] },
282     stmt => 'WHERE col < ? OR col > ? OR col != ?',
283     bind => [qw/123 456 789/],
284   },
285
286   # flipping array logic affects both column value and condition arrays
287   {
288     args => { logic => 'and' },
289     where => [ col => [ {'<' => 123}, {'>' => 456 }, {'!=' => 789} ], col2 => 0 ],
290     stmt => 'WHERE col < ? AND col > ? AND col != ? AND col2 = ?',
291     bind => [qw/123 456 789 0/],
292   },
293
294   # flipping array logic with explicit -and works
295   {
296     args => { logic => 'and' },
297     where => [ col => [ -and => {'<' => 123}, {'>' => 456 }, {'!=' => 789} ], col2 => 0 ],
298     stmt => 'WHERE col < ? AND col > ? AND col != ? AND col2 = ?',
299     bind => [qw/123 456 789 0/],
300   },
301   # flipping array logic with explicit -or flipping it back
302   {
303     args => { logic => 'and' },
304     where => [ col => [ -or => {'<' => 123}, {'>' => 456 }, {'!=' => 789} ], col2 => 0 ],
305     stmt => 'WHERE (col < ? OR col > ? OR col != ?) AND col2 = ?',
306     bind => [qw/123 456 789 0/],
307   },
308 );
309
310 # modN and mod_N were a bad design decision - they go away in SQLA2, warn now
311 my @numbered_mods = (
312   {
313     backcompat => {
314       -and => [a => 10, b => 11],
315       -and2 => [ c => 20, d => 21 ],
316       -nest => [ x => 1 ],
317       -nest2 => [ y => 2 ],
318       -or => { m => 7, n => 8 },
319       -or2 => { m => 17, n => 18 },
320     },
321     correct => { -and => [
322       -and => [a => 10, b => 11],
323       -and => [ c => 20, d => 21 ],
324       -nest => [ x => 1 ],
325       -nest => [ y => 2 ],
326       -or => { m => 7, n => 8 },
327       -or => { m => 17, n => 18 },
328     ] },
329   },
330   {
331     backcompat => {
332       -and2 => [a => 10, b => 11],
333       -and_3 => [ c => 20, d => 21 ],
334       -nest2 => [ x => 1 ],
335       -nest_3 => [ y => 2 ],
336       -or2 => { m => 7, n => 8 },
337       -or_3 => { m => 17, n => 18 },
338     },
339     correct => [ -and => [
340       -and => [a => 10, b => 11],
341       -and => [ c => 20, d => 21 ],
342       -nest => [ x => 1 ],
343       -nest => [ y => 2 ],
344       -or => { m => 7, n => 8 },
345       -or => { m => 17, n => 18 },
346     ] ],
347   },
348 );
349
350 my @nest_tests = (
351  {
352    where => {a => 1, -nest => [b => 2, c => 3]},
353    stmt  => 'WHERE ( ( (b = ? OR c = ?) AND a = ? ) )',
354    bind  => [qw/2 3 1/],
355  },
356  {
357    where => {a => 1, -nest => {b => 2, c => 3}},
358    stmt  => 'WHERE ( ( (b = ? AND c = ?) AND a = ? ) )',
359    bind  => [qw/2 3 1/],
360  },
361  {
362    where => {a => 1, -or => {-nest => {b => 2, c => 3}}},
363    stmt  => 'WHERE ( ( (b = ? AND c = ?) AND a = ? ) )',
364    bind  => [qw/2 3 1/],
365  },
366  {
367    where => {a => 1, -or => {-nest => [b => 2, c => 3]}},
368    stmt  => 'WHERE ( ( (b = ? OR c = ?) AND a = ? ) )',
369    bind  => [qw/2 3 1/],
370  },
371  {
372    where => {a => 1, -nest => {-or => {b => 2, c => 3}}},
373    stmt  => 'WHERE ( ( (b = ? OR c = ?) AND a = ? ) )',
374    bind  => [qw/2 3 1/],
375  },
376  {
377    where => [a => 1, -nest => {b => 2, c => 3}, -nest => [d => 4, e => 5]],
378    stmt  => 'WHERE ( ( a = ? OR ( b = ? AND c = ? ) OR ( d = ? OR e = ? ) ) )',
379    bind  => [qw/1 2 3 4 5/],
380  },
381 );
382
383 for my $case (@and_or_tests) {
384   TODO: {
385     local $TODO = $case->{todo} if $case->{todo};
386
387     local $Data::Dumper::Terse = 1;
388
389     my @w;
390     local $SIG{__WARN__} = sub { push @w, @_ };
391
392     my $sql = SQL::Abstract->new ($case->{args} || {});
393
394     my $where_copy = dclone($case->{where});
395
396     lives_ok (sub { 
397       my ($stmt, @bind) = $sql->where($case->{where});
398       is_same_sql_bind(
399         $stmt,
400         \@bind,
401         $case->{stmt},
402         $case->{bind},
403       )
404         || diag "Search term:\n" . Dumper $case->{where};
405     });
406     is (@w, 0, 'No warnings within and-or tests')
407       || diag join "\n", 'Emitted warnings:', @w;
408
409     is_deeply ($case->{where}, $where_copy, 'Where conditions unchanged');
410   }
411 }
412
413 for my $case (@nest_tests) {
414   TODO: {
415     local $TODO = $case->{todo} if $case->{todo};
416
417     local $SQL::Abstract::Test::parenthesis_significant = 1;
418     local $Data::Dumper::Terse = 1;
419
420     my $sql = SQL::Abstract->new ($case->{args} || {});
421     lives_ok (sub {
422       my ($stmt, @bind) = $sql->where($case->{where});
423       is_same_sql_bind(
424         $stmt,
425         \@bind,
426         $case->{stmt},
427         $case->{bind},
428       )
429         || diag "Search term:\n" . Dumper $case->{where};
430     });
431   }
432 }
433
434
435
436 my $w_str = "\QUse of [and|or|nest]_N modifiers is deprecated and will be removed in SQLA v2.0\E";
437 for my $case (@numbered_mods) {
438   TODO: {
439     local $TODO = $case->{todo} if $case->{todo};
440
441     local $Data::Dumper::Terse = 1;
442
443     my @w;
444     local $SIG{__WARN__} = sub { push @w, @_ };
445     my $sql = SQL::Abstract->new ($case->{args} || {});
446     lives_ok (sub {
447       my ($old_s, @old_b) = $sql->where($case->{backcompat});
448       my ($new_s, @new_b) = $sql->where($case->{correct});
449       is_same_sql_bind(
450         $old_s, \@old_b,
451         $new_s, \@new_b,
452         'Backcompat and the correct(tm) syntax result in identical statements',
453       ) || diag "Search terms:\n" . Dumper {
454           backcompat => $case->{backcompat},
455           correct => $case->{correct},
456         };
457     });
458
459     ok (@w, 'Warnings were emitted about a mod_N construct');
460
461     my @non_match;
462     for (@w) {
463       push @non_match, $_ if ($_ !~ /$w_str/);
464     }
465
466     is (@non_match, 0, 'All warnings match the deprecation message')
467       || diag join "\n", 'Rogue warnings:', @non_match;
468   }
469 }
470
471 done_testing;