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