* Made some more tests SKIP if SQLA version < 1.50.
[dbsrgits/DBIx-Class.git] / t / 95sql_maker_quote.t
CommitLineData
3da841f1 1use strict;
2use warnings;
3
4use Test::More;
9b459129 5use SQL::Abstract::Test import => ['is_same_sql_bind'];
3da841f1 6
7
8BEGIN {
9 eval "use DBD::SQLite";
10 plan $@
11 ? ( skip_all => 'needs DBD::SQLite for testing' )
c80207cd 12 : ( tests => 12 );
3da841f1 13}
14
15use lib qw(t/lib);
16
17use_ok('DBICTest');
18
c216324a 19my $schema = DBICTest->init_schema();
3da841f1 20
c216324a 21my $sql_maker = $schema->storage->sql_maker;
3da841f1 22
23$sql_maker->quote_char('`');
24$sql_maker->name_sep('.');
25
9b459129 26my ($sql, @bind) = $sql_maker->select(
3da841f1 27 [
28 {
29 'me' => 'cd'
30 },
31 [
32 {
33 'artist' => 'artist',
34 '-join_type' => ''
35 },
36 {
37 'artist.artistid' => 'me.artist'
38 }
39 ]
40 ],
41 [
42 {
43 'count' => '*'
44 }
45 ],
46 {
47 'artist.name' => 'Caterwauler McCrae',
48 'me.year' => 2001
49 },
50 [],
51 undef,
52 undef
53);
54
9b459129 55is_same_sql_bind(
56 $sql, \@bind,
57 q/SELECT COUNT( * ) FROM `cd` `me` JOIN `artist` `artist` ON ( `artist`.`artistid` = `me`.`artist` ) WHERE ( `artist`.`name` = ? AND `me`.`year` = ? )/, [ ['artist.name' => 'Caterwauler McCrae'], ['me.year' => 2001] ],
58 'got correct SQL and bind parameters for count query with quoting'
59);
3da841f1 60
c80207cd 61
9b459129 62($sql, @bind) = $sql_maker->select(
3da841f1 63 [
64 {
65 'me' => 'cd'
66 }
67 ],
68 [
69 'me.cdid',
70 'me.artist',
71 'me.title',
72 'me.year'
73 ],
74 undef,
c80207cd 75 'year DESC',
76 undef,
77 undef
78);
79
80is_same_sql_bind(
81 $sql, \@bind,
82 q/SELECT `me`.`cdid`, `me`.`artist`, `me`.`title`, `me`.`year` FROM `cd` `me` ORDER BY `year DESC`/, [],
83 'scalar ORDER BY okay (single value)'
84);
85
86
87($sql, @bind) = $sql_maker->select(
3da841f1 88 [
c80207cd 89 {
90 'me' => 'cd'
91 }
92 ],
93 [
94 'me.cdid',
95 'me.artist',
96 'me.title',
97 'me.year'
98 ],
99 undef,
100 [
101 'year DESC',
102 'title ASC'
3da841f1 103 ],
104 undef,
105 undef
106);
107
8682bb07 108is_same_sql_bind(
109 $sql, \@bind,
c80207cd 110 q/SELECT `me`.`cdid`, `me`.`artist`, `me`.`title`, `me`.`year` FROM `cd` `me` ORDER BY `year DESC`, `title ASC`/, [],
111 'scalar ORDER BY okay (multiple values)'
8682bb07 112);
113
89479564 114SKIP: {
115 skip "SQL::Abstract < 1.50 does not support hashrefs in order_by", 2 if $SQL::Abstract::VERSION < 1.50;
8682bb07 116
89479564 117 ($sql, @bind) = $sql_maker->select(
118 [
119 {
120 'me' => 'cd'
121 }
122 ],
123 [
124 'me.cdid',
125 'me.artist',
126 'me.title',
127 'me.year'
128 ],
129 undef,
130 { -desc => 'year' },
131 undef,
132 undef
133 );
3da841f1 134
89479564 135 is_same_sql_bind(
136 $sql, \@bind,
137 q/SELECT `me`.`cdid`, `me`.`artist`, `me`.`title`, `me`.`year` FROM `cd` `me` ORDER BY `year` DESC/, [],
138 'hashref ORDER BY okay (single value)'
139 );
3da841f1 140
c80207cd 141
89479564 142 ($sql, @bind) = $sql_maker->select(
143 [
144 {
145 'me' => 'cd'
146 }
147 ],
148 [
149 'me.cdid',
150 'me.artist',
151 'me.title',
152 'me.year'
153 ],
154 undef,
155 [
156 { -desc => 'year' },
157 { -asc => 'title' }
158 ],
159 undef,
160 undef
161 );
162
163 is_same_sql_bind(
164 $sql, \@bind,
165 q/SELECT `me`.`cdid`, `me`.`artist`, `me`.`title`, `me`.`year` FROM `cd` `me` ORDER BY `year` DESC, `title` ASC/, [],
166 'hashref ORDER BY okay (multiple values)'
167 );
c80207cd 168
89479564 169}
c80207cd 170
171
172($sql, @bind) = $sql_maker->select(
173 [
174 {
175 'me' => 'cd'
176 }
177 ],
178 [
179 'me.cdid',
180 'me.artist',
181 'me.title',
182 'me.year'
3da841f1 183 ],
184 undef,
c80207cd 185 \'year DESC',
186 undef,
3da841f1 187 undef
188);
189
9b459129 190is_same_sql_bind(
191 $sql, \@bind,
192 q/SELECT `me`.`cdid`, `me`.`artist`, `me`.`title`, `me`.`year` FROM `cd` `me` ORDER BY year DESC/, [],
c80207cd 193 'did not quote ORDER BY with scalarref (single value)'
194);
195
196
197($sql, @bind) = $sql_maker->select(
198 [
199 {
200 'me' => 'cd'
201 }
202 ],
203 [
204 'me.cdid',
205 'me.artist',
206 'me.title',
207 'me.year'
208 ],
209 undef,
210 [
211 \'year DESC',
212 \'title ASC'
213 ],
214 undef,
215 undef
216);
217
218is_same_sql_bind(
219 $sql, \@bind,
220 q/SELECT `me`.`cdid`, `me`.`artist`, `me`.`title`, `me`.`year` FROM `cd` `me` ORDER BY year DESC, title ASC/, [],
221 'did not quote ORDER BY with scalarref (multiple values)'
3da841f1 222);
223
c80207cd 224
9b459129 225($sql, @bind) = $sql_maker->update(
3da841f1 226 'group',
227 {
228 'order' => '12',
229 'name' => 'Bill'
230 }
231);
232
9b459129 233is_same_sql_bind(
234 $sql, \@bind,
235 q/UPDATE `group` SET `name` = ?, `order` = ?/, [ ['name' => 'Bill'], ['order' => '12'] ],
236 'quoted table names for UPDATE'
237);
3da841f1 238
c80207cd 239
240($sql, @bind) = $sql_maker->select(
241 [
242 {
243 'me' => 'cd'
244 }
245 ],
246 [
247 'me.*'
248 ],
249 undef,
250 [],
251 undef,
252 undef
253);
254
255is_same_sql_bind(
256 $sql, \@bind,
257 q/SELECT `me`.* FROM `cd` `me`/, [],
258 'select attr with me.* is right'
259);
260
261
3da841f1 262$sql_maker->quote_char([qw/[ ]/]);
263
9b459129 264($sql, @bind) = $sql_maker->select(
3da841f1 265 [
266 {
267 'me' => 'cd'
268 },
269 [
270 {
271 'artist' => 'artist',
272 '-join_type' => ''
273 },
274 {
275 'artist.artistid' => 'me.artist'
276 }
277 ]
278 ],
279 [
280 {
281 'count' => '*'
282 }
283 ],
284 {
285 'artist.name' => 'Caterwauler McCrae',
286 'me.year' => 2001
287 },
288 [],
289 undef,
290 undef
291);
292
9b459129 293is_same_sql_bind(
294 $sql, \@bind,
295 q/SELECT COUNT( * ) FROM [cd] [me] JOIN [artist] [artist] ON ( [artist].[artistid] = [me].[artist] ) WHERE ( [artist].[name] = ? AND [me].[year] = ? )/, [ ['artist.name' => 'Caterwauler McCrae'], ['me.year' => 2001] ],
296 'got correct SQL and bind parameters for count query with bracket quoting'
297);
3da841f1 298
299
9b459129 300($sql, @bind) = $sql_maker->update(
3da841f1 301 'group',
302 {
303 'order' => '12',
304 'name' => 'Bill'
305 }
306);
307
9b459129 308is_same_sql_bind(
309 $sql, \@bind,
310 q/UPDATE [group] SET [name] = ?, [order] = ?/, [ ['name' => 'Bill'], ['order' => '12'] ],
311 'bracket quoted table names for UPDATE'
312);