Commit | Line | Data |
f5aab26e |
1 | use strict; |
2 | use warnings; |
3 | use Test::More; |
952f9e2d |
4 | use Test::Exception; |
f5aab26e |
5 | |
6 | use SQL::Abstract; |
7 | |
5aad8cf3 |
8 | use SQL::Abstract::Test import => ['is_same_sql_bind']; |
10e6c946 |
9 | my @cases = |
f5aab26e |
10 | ( |
11 | { |
12 | given => \'colA DESC', |
13 | expects => ' ORDER BY colA DESC', |
14 | expects_quoted => ' ORDER BY colA DESC', |
15 | }, |
16 | { |
17 | given => 'colA', |
18 | expects => ' ORDER BY colA', |
19 | expects_quoted => ' ORDER BY `colA`', |
20 | }, |
71519cf6 |
21 | { # it may look odd, but this is the desired behaviour (mst) |
22 | given => 'colA DESC', |
23 | expects => ' ORDER BY colA DESC', |
24 | expects_quoted => ' ORDER BY `colA DESC`', |
25 | }, |
f5aab26e |
26 | { |
27 | given => [qw/colA colB/], |
28 | expects => ' ORDER BY colA, colB', |
29 | expects_quoted => ' ORDER BY `colA`, `colB`', |
30 | }, |
71519cf6 |
31 | { # it may look odd, but this is the desired behaviour (mst) |
32 | given => ['colA ASC', 'colB DESC'], |
33 | expects => ' ORDER BY colA ASC, colB DESC', |
34 | expects_quoted => ' ORDER BY `colA ASC`, `colB DESC`', |
35 | }, |
f5aab26e |
36 | { |
37 | given => {-asc => 'colA'}, |
38 | expects => ' ORDER BY colA ASC', |
39 | expects_quoted => ' ORDER BY `colA` ASC', |
40 | }, |
41 | { |
42 | given => {-desc => 'colB'}, |
43 | expects => ' ORDER BY colB DESC', |
44 | expects_quoted => ' ORDER BY `colB` DESC', |
45 | }, |
46 | { |
47 | given => [{-asc => 'colA'}, {-desc => 'colB'}], |
48 | expects => ' ORDER BY colA ASC, colB DESC', |
49 | expects_quoted => ' ORDER BY `colA` ASC, `colB` DESC', |
50 | }, |
1cfa1db3 |
51 | { |
52 | given => ['colA', {-desc => 'colB'}], |
53 | expects => ' ORDER BY colA, colB DESC', |
54 | expects_quoted => ' ORDER BY `colA`, `colB` DESC', |
55 | }, |
b6475fb1 |
56 | { |
57 | given => undef, |
58 | expects => '', |
59 | expects_quoted => '', |
60 | }, |
d0a4a580 |
61 | { |
62 | given => [ {} ], |
63 | expects => '', |
64 | expects_quoted => '', |
65 | }, |
994edb77 |
66 | |
67 | { |
68 | given => [{-desc => [ qw/colA colB/ ] }], |
69 | expects => ' ORDER BY colA DESC, colB DESC', |
70 | expects_quoted => ' ORDER BY `colA` DESC, `colB` DESC', |
71 | }, |
72 | { |
73 | given => [{-desc => [ qw/colA colB/ ] }, {-asc => 'colC'}], |
74 | expects => ' ORDER BY colA DESC, colB DESC, colC ASC', |
75 | expects_quoted => ' ORDER BY `colA` DESC, `colB` DESC, `colC` ASC', |
76 | }, |
77 | { |
78 | given => [{-desc => [ qw/colA colB/ ] }, {-asc => [ qw/colC colD/ ] }], |
79 | expects => ' ORDER BY colA DESC, colB DESC, colC ASC, colD ASC', |
80 | expects_quoted => ' ORDER BY `colA` DESC, `colB` DESC, `colC` ASC, `colD` ASC', |
81 | }, |
82 | { |
83 | given => [{-desc => [ qw/colA colB/ ] }, {-desc => 'colC' }], |
84 | expects => ' ORDER BY colA DESC, colB DESC, colC DESC', |
85 | expects_quoted => ' ORDER BY `colA` DESC, `colB` DESC, `colC` DESC', |
86 | }, |
b8b747f7 |
87 | { |
952f9e2d |
88 | given => [{ -asc => 'colA' }, { -desc => [qw/colB/] }, { -asc => [qw/colC colD/] }], |
89 | expects => ' ORDER BY colA ASC, colB DESC, colC ASC, colD ASC', |
90 | expects_quoted => ' ORDER BY `colA` ASC, `colB` DESC, `colC` ASC, `colD` ASC', |
b8b747f7 |
91 | }, |
293149c5 |
92 | { |
93 | given => { -desc => \['colA LIKE ?', 'test'] }, |
94 | expects => ' ORDER BY colA LIKE ? DESC', |
95 | expects_quoted => ' ORDER BY colA LIKE ? DESC', |
f267b646 |
96 | bind => ['test'], |
97 | }, |
98 | { |
16c7f71c |
99 | given => \['colA LIKE ? DESC', 'test'], |
100 | expects => ' ORDER BY colA LIKE ? DESC', |
101 | expects_quoted => ' ORDER BY colA LIKE ? DESC', |
102 | bind => ['test'], |
103 | }, |
104 | { |
f267b646 |
105 | given => [ { -asc => \['colA'] }, { -desc => \['colB LIKE ?', 'test'] }, { -asc => \['colC LIKE ?', 'tost'] }], |
106 | expects => ' ORDER BY colA ASC, colB LIKE ? DESC, colC LIKE ? ASC', |
107 | expects_quoted => ' ORDER BY colA ASC, colB LIKE ? DESC, colC LIKE ? ASC', |
108 | bind => [qw/test tost/], |
293149c5 |
109 | }, |
f5aab26e |
110 | ); |
111 | |
6dcf723c |
112 | my $sql = SQL::Abstract->new; |
113 | my $sqlq = SQL::Abstract->new({quote_char => '`'}); |
114 | |
ca4f826a |
115 | for my $case (@cases) { |
f267b646 |
116 | my ($stat, @bind); |
117 | |
26fe4d30 |
118 | ($stat, @bind) = $sql->where(undef, $case->{given}); |
f267b646 |
119 | is_same_sql_bind ( |
120 | $stat, |
121 | \@bind, |
122 | $case->{expects}, |
123 | $case->{bind} || [], |
124 | ); |
125 | |
26fe4d30 |
126 | ($stat, @bind) = $sqlq->where(undef, $case->{given}); |
f267b646 |
127 | is_same_sql_bind ( |
128 | $stat, |
129 | \@bind, |
130 | $case->{expects_quoted}, |
131 | $case->{bind} || [], |
132 | ); |
f5aab26e |
133 | } |
952f9e2d |
134 | |
135 | throws_ok ( |
136 | sub { $sql->_order_by({-desc => 'colA', -asc => 'colB' }) }, |
5e436130 |
137 | qr/hash passed .+ must have exactly one key/, |
952f9e2d |
138 | 'Undeterministic order exception', |
139 | ); |
140 | |
141 | throws_ok ( |
9d4775b6 |
142 | sub { $sql->_order_by([ {-desc => 'colA', -asc => 'colB' } ]) }, |
143 | qr/hash passed .+ must have exactly one key/, |
144 | 'Undeterministic order exception', |
145 | ); |
146 | |
147 | throws_ok ( |
952f9e2d |
148 | sub { $sql->_order_by({-desc => [ qw/colA colB/ ], -asc => [ qw/colC colD/ ] }) }, |
5e436130 |
149 | qr/hash passed .+ must have exactly one key/, |
952f9e2d |
150 | 'Undeterministic order exception', |
151 | ); |
10e6c946 |
152 | |
153 | done_testing; |