7 use SQL::Abstract::Test import => ['is_same_sql_bind'];
12 my @in_between_tests = (
14 where => { x => { -between => [1, 2] } },
15 stmt => 'WHERE (x BETWEEN ? AND ?)',
17 test => '-between with two placeholders',
20 where => { x => { -between => [\"1", 2] } },
21 stmt => 'WHERE (x BETWEEN 1 AND ?)',
23 test => '-between with one literal sql arg and one placeholder',
26 where => { x => { -between => [1, \"2"] } },
27 stmt => 'WHERE (x BETWEEN ? AND 2)',
29 test => '-between with one placeholder and one literal sql arg',
32 where => { x => { -between => [\'current_date - 1', \'current_date - 0'] } },
33 stmt => 'WHERE (x BETWEEN current_date - 1 AND current_date - 0)',
35 test => '-between with two literal sql arguments',
38 where => { x => { -between => [ \['current_date - ?', 1], \['current_date - ?', 0] ] } },
39 stmt => 'WHERE (x BETWEEN current_date - ? AND current_date - ?)',
41 test => '-between with two literal sql arguments with bind',
44 where => { x => { -between => \['? AND ?', 1, 2] } },
45 stmt => 'WHERE (x BETWEEN ? AND ?)',
47 test => '-between with literal sql with placeholders (\["? AND ?", scalar, scalar])',
50 where => { x => { -between => \["'something' AND ?", 2] } },
51 stmt => "WHERE (x BETWEEN 'something' AND ?)",
53 test => '-between with literal sql with one literal arg and one placeholder (\["\'something\' AND ?", scalar])',
56 where => { x => { -between => \["? AND 'something'", 1] } },
57 stmt => "WHERE (x BETWEEN ? AND 'something')",
59 test => '-between with literal sql with one placeholder and one literal arg (\["? AND \'something\'", scalar])',
62 where => { x => { -between => \"'this' AND 'that'" } },
63 stmt => "WHERE (x BETWEEN 'this' AND 'that')",
65 test => '-between with literal sql with a literal (\"\'this\' AND \'that\'")',
69 start0 => { -between => [ 1, 2 ] },
70 start1 => { -between => \["? AND ?", 1, 2] },
71 start2 => { -between => \"lower(x) AND upper(y)" },
72 start3 => { -between => [
74 \["upper(?)", 'stuff' ],
78 ( start0 BETWEEN ? AND ? )
79 AND ( start1 BETWEEN ? AND ? )
80 AND ( start2 BETWEEN lower(x) AND upper(y) )
81 AND ( start3 BETWEEN lower(x) AND upper(?) )
83 bind => [1, 2, 1, 2, 'stuff'],
84 test => '-between POD test',
88 parenthesis_significant => 1,
89 where => { x => { -in => [ 1 .. 3] } },
90 stmt => "WHERE ( x IN (?, ?, ?) )",
92 test => '-in with an array of scalars',
95 parenthesis_significant => 1,
96 where => { x => { -in => [] } },
97 stmt => "WHERE ( 0=1 )",
99 test => '-in with an empty array',
102 parenthesis_significant => 1,
103 where => { x => { -in => \'( 1,2,lower(y) )' } },
104 stmt => "WHERE ( x IN ( 1,2,lower(y) ) )",
106 test => '-in with a literal scalarref',
109 parenthesis_significant => 1,
110 where => { x => { -in => \['( ( ?,?,lower(y) ) )', 1, 2] } },
111 stmt => "WHERE ( x IN ( ?,?,lower(y) ) )", # note that outer parens are opened even though literal was requested (RIBASUSHI)
113 test => '-in with a literal arrayrefref',
116 parenthesis_significant => 1,
118 customer => { -in => \[
119 'SELECT cust_id FROM cust WHERE balance > ?',
122 status => { -in => \'SELECT status_codes FROM states' },
126 customer IN ( SELECT cust_id FROM cust WHERE balance > ? )
127 AND status IN ( SELECT status_codes FROM states )
131 test => '-in POD test',
135 plan tests => @in_between_tests*4;
137 for my $case (@in_between_tests) {
139 local $TODO = $case->{todo} if $case->{todo};
140 local $SQL::Abstract::Test::parenthesis_significant = $case->{parenthesis_significant};
142 local $Data::Dumper::Terse = 1;
147 local $SIG{__WARN__} = sub { push @w, @_ };
148 my $sql = SQL::Abstract->new ($case->{args} || {});
150 my ($stmt, @bind) = $sql->where($case->{where});
157 || diag "Search term:\n" . Dumper $case->{where};
159 is (@w, 0, $case->{test} || 'No warnings within in-between tests')
160 || diag join "\n", 'Emitted warnings:', @w;
161 }, "$case->{test} doesn't die");