add handling for NULL when using -in
[dbsrgits/SQL-Abstract.git] / t / 05in_between.t
CommitLineData
cf02fc47 1#!/usr/bin/perl
2
3use strict;
4use warnings;
5use Test::More;
6use Test::Exception;
7use SQL::Abstract::Test import => ['is_same_sql_bind'];
8
9use Data::Dumper;
10use SQL::Abstract;
11
cf02fc47 12my @in_between_tests = (
13 {
14 where => { x => { -between => [1, 2] } },
15 stmt => 'WHERE (x BETWEEN ? AND ?)',
16 bind => [qw/1 2/],
17 test => '-between with two placeholders',
18 },
19 {
20 where => { x => { -between => [\"1", 2] } },
21 stmt => 'WHERE (x BETWEEN 1 AND ?)',
22 bind => [qw/2/],
23 test => '-between with one literal sql arg and one placeholder',
24 },
25 {
26 where => { x => { -between => [1, \"2"] } },
27 stmt => 'WHERE (x BETWEEN ? AND 2)',
28 bind => [qw/1/],
29 test => '-between with one placeholder and one literal sql arg',
30 },
31 {
32 where => { x => { -between => [\'current_date - 1', \'current_date - 0'] } },
33 stmt => 'WHERE (x BETWEEN current_date - 1 AND current_date - 0)',
34 bind => [],
35 test => '-between with two literal sql arguments',
36 },
37 {
4d8b3dc4 38 where => { x => { -between => [ \['current_date - ?', 1], \['current_date - ?', 0] ] } },
39 stmt => 'WHERE (x BETWEEN current_date - ? AND current_date - ?)',
40 bind => [1, 0],
41 test => '-between with two literal sql arguments with bind',
42 },
43 {
cf02fc47 44 where => { x => { -between => \['? AND ?', 1, 2] } },
45 stmt => 'WHERE (x BETWEEN ? AND ?)',
46 bind => [1,2],
47 test => '-between with literal sql with placeholders (\["? AND ?", scalar, scalar])',
48 },
49 {
50 where => { x => { -between => \["'something' AND ?", 2] } },
51 stmt => "WHERE (x BETWEEN 'something' AND ?)",
52 bind => [2],
53 test => '-between with literal sql with one literal arg and one placeholder (\["\'something\' AND ?", scalar])',
54 },
55 {
56 where => { x => { -between => \["? AND 'something'", 1] } },
57 stmt => "WHERE (x BETWEEN ? AND 'something')",
58 bind => [1],
59 test => '-between with literal sql with one placeholder and one literal arg (\["? AND \'something\'", scalar])',
60 },
61 {
4d8b3dc4 62 where => { x => { -between => \"'this' AND 'that'" } },
cf02fc47 63 stmt => "WHERE (x BETWEEN 'this' AND 'that')",
64 bind => [],
4d8b3dc4 65 test => '-between with literal sql with a literal (\"\'this\' AND \'that\'")',
cf02fc47 66 },
e41c3bdd 67 {
68 where => {
0336eddb 69 start0 => { -between => [ 1, { -upper => 2 } ] },
e41c3bdd 70 start1 => { -between => \["? AND ?", 1, 2] },
71 start2 => { -between => \"lower(x) AND upper(y)" },
72 start3 => { -between => [
73 \"lower(x)",
74 \["upper(?)", 'stuff' ],
75 ] },
76 },
77 stmt => "WHERE (
b3b79607 78 ( start0 BETWEEN ? AND UPPER ? )
e41c3bdd 79 AND ( start1 BETWEEN ? AND ? )
80 AND ( start2 BETWEEN lower(x) AND upper(y) )
81 AND ( start3 BETWEEN lower(x) AND upper(?) )
82 )",
83 bind => [1, 2, 1, 2, 'stuff'],
84 test => '-between POD test',
85 },
5e5cbf51 86 {
87 args => { bindtype => 'columns' },
88 where => {
89 start0 => { -between => [ 1, { -upper => 2 } ] },
90 start1 => { -between => \["? AND ?", [ start1 => 1], [start1 => 2] ] },
91 start2 => { -between => \"lower(x) AND upper(y)" },
92 start3 => { -between => [
93 \"lower(x)",
94 \["upper(?)", [ start3 => 'stuff'] ],
95 ] },
96 },
97 stmt => "WHERE (
98 ( start0 BETWEEN ? AND UPPER ? )
99 AND ( start1 BETWEEN ? AND ? )
100 AND ( start2 BETWEEN lower(x) AND upper(y) )
101 AND ( start3 BETWEEN lower(x) AND upper(?) )
102 )",
103 bind => [
104 [ start0 => 1 ],
105 [ start0 => 2 ],
106 [ start1 => 1 ],
107 [ start1 => 2 ],
108 [ start3 => 'stuff' ],
109 ],
110 test => '-between POD test',
111 },
4a1f01a3 112
113 {
114 parenthesis_significant => 1,
115 where => { x => { -in => [ 1 .. 3] } },
116 stmt => "WHERE ( x IN (?, ?, ?) )",
117 bind => [ 1 .. 3],
118 test => '-in with an array of scalars',
119 },
120 {
121 parenthesis_significant => 1,
e41c3bdd 122 where => { x => { -in => [] } },
123 stmt => "WHERE ( 0=1 )",
124 bind => [],
125 test => '-in with an empty array',
126 },
127 {
128 parenthesis_significant => 1,
4a1f01a3 129 where => { x => { -in => \'( 1,2,lower(y) )' } },
b9a4fdae 130 stmt => "WHERE ( x IN ( 1,2,lower(y) ) )",
4a1f01a3 131 bind => [],
132 test => '-in with a literal scalarref',
133 },
134 {
135 parenthesis_significant => 1,
136 where => { x => { -in => \['( ( ?,?,lower(y) ) )', 1, 2] } },
b9a4fdae 137 stmt => "WHERE ( x IN ( ?,?,lower(y) ) )", # note that outer parens are opened even though literal was requested (RIBASUSHI)
4a1f01a3 138 bind => [1, 2],
139 test => '-in with a literal arrayrefref',
140 },
e41c3bdd 141 {
142 parenthesis_significant => 1,
143 where => {
171a709f 144 status => { -in => \"(SELECT status_codes\nFROM states)" },
145 },
146 # failed to open outer parens on a multi-line query in 1.61 (semifor)
147 stmt => " WHERE ( status IN ( SELECT status_codes FROM states )) ",
148 bind => [],
149 test => '-in multi-line subquery test',
150 },
151 {
152 parenthesis_significant => 1,
153 where => {
e41c3bdd 154 customer => { -in => \[
155 'SELECT cust_id FROM cust WHERE balance > ?',
156 2000,
157 ]},
158 status => { -in => \'SELECT status_codes FROM states' },
159 },
160 stmt => "
161 WHERE ((
162 customer IN ( SELECT cust_id FROM cust WHERE balance > ? )
163 AND status IN ( SELECT status_codes FROM states )
164 ))
165 ",
166 bind => [2000],
167 test => '-in POD test',
168 },
0336eddb 169 {
170 where => { x => { -in => [ \['LOWER(?)', 'A' ], \'LOWER(b)', { -lower => 'c' } ] } },
171 stmt => " WHERE ( x IN ( LOWER(?), LOWER(b), LOWER ? ) )",
172 bind => [qw/A c/],
173 test => '-in with an array of function array refs with args',
174 },
279eb282 175 {
176 where => { x => { -in => [ 1, undef ] } },
177 stmt => " WHERE ( x IN ( ?, NULL ) )",
178 bind => [ 1 ],
179 test => '-in with undef as an element',
180 },
181 {
182 where => { x => { -in => [ 1, undef, 2, 3, undef ] } },
183 stmt => " WHERE ( x IN ( ?, NULL, ?, ?, NULL ) )",
184 bind => [ 1, 2, 3 ],
185 test => '-in with undef as an element',
186 },
cf02fc47 187);
188
4d8b3dc4 189plan tests => @in_between_tests*4;
cf02fc47 190
191for my $case (@in_between_tests) {
192 TODO: {
193 local $TODO = $case->{todo} if $case->{todo};
4a1f01a3 194 local $SQL::Abstract::Test::parenthesis_significant = $case->{parenthesis_significant};
cf02fc47 195
196 local $Data::Dumper::Terse = 1;
197
4d8b3dc4 198 lives_ok (sub {
199
200 my @w;
201 local $SIG{__WARN__} = sub { push @w, @_ };
202 my $sql = SQL::Abstract->new ($case->{args} || {});
203 lives_ok (sub {
204 my ($stmt, @bind) = $sql->where($case->{where});
205 is_same_sql_bind(
206 $stmt,
207 \@bind,
208 $case->{stmt},
209 $case->{bind},
210 )
211 || diag "Search term:\n" . Dumper $case->{where};
212 });
213 is (@w, 0, $case->{test} || 'No warnings within in-between tests')
214 || diag join "\n", 'Emitted warnings:', @w;
215 }, "$case->{test} doesn't die");
cf02fc47 216 }
217}