e90befeb8e38a09b3d2a1dc1eeb6774d44380423
[dbsrgits/DBIx-Class.git] / t / sqlmaker / core_quoted.t
1 use strict;
2 use warnings;
3
4 use Test::More;
5
6 use lib qw(t/lib);
7 use DBICTest ':DiffSQL';
8
9 my $schema = DBICTest->init_schema();
10
11 my $sql_maker = $schema->storage->sql_maker;
12
13 $sql_maker->quote_char('`');
14 $sql_maker->name_sep('.');
15
16 my ($sql, @bind) = $sql_maker->select(
17           [
18             {
19               'me' => 'cd'
20             },
21             [
22               {
23                 'artist' => 'artist',
24                 '-join_type' => ''
25               },
26               {
27                 'artist.artistid' => 'me.artist'
28               }
29             ],
30             [
31               {
32                 'tracks' => 'tracks',
33                 '-join_type' => 'left'
34               },
35               {
36                 'tracks.cd' => 'me.cdid'
37               }
38             ],
39           ],
40           [
41             'me.cdid',
42             { count => 'tracks.cd' },
43             { min => 'me.year', -as => 'minyear' },
44           ],
45           {
46             'artist.name' => 'Caterwauler McCrae',
47             'me.year' => 2001
48           },
49           {},
50           undef,
51           undef
52 );
53
54 is_same_sql_bind(
55   $sql, \@bind,
56   q/
57     SELECT `me`.`cdid`, COUNT( `tracks`.`cd` ), MIN( `me`.`year` ) AS `minyear`
58       FROM `cd` `me`
59       JOIN `artist` `artist` ON ( `artist`.`artistid` = `me`.`artist` )
60       LEFT JOIN `tracks` `tracks` ON ( `tracks`.`cd` = `me`.`cdid` )
61     WHERE ( `artist`.`name` = ? AND `me`.`year` = ? )
62   /,
63   [ ['artist.name' => 'Caterwauler McCrae'], ['me.year' => 2001] ],
64   'got correct SQL and bind parameters for complex select query with quoting'
65 );
66
67
68 ($sql, @bind) = $sql_maker->select(
69           [
70             {
71               'me' => 'cd'
72             }
73           ],
74           [
75             'me.cdid',
76             'me.artist',
77             'me.title',
78             'me.year'
79           ],
80           undef,
81           { order_by => 'year DESC' },
82           undef,
83           undef
84 );
85
86 is_same_sql_bind(
87   $sql, \@bind,
88   q/SELECT `me`.`cdid`, `me`.`artist`, `me`.`title`, `me`.`year` FROM `cd` `me` ORDER BY `year DESC`/, [],
89   'scalar ORDER BY okay (single value)'
90 );
91
92
93 ($sql, @bind) = $sql_maker->select(
94           [
95             {
96               'me' => 'cd'
97             }
98           ],
99           [
100             'me.cdid',
101             'me.artist',
102             'me.title',
103             'me.year'
104           ],
105           undef,
106           { order_by => [
107             'year DESC',
108             'title ASC'
109           ]},
110           undef,
111           undef
112 );
113
114 is_same_sql_bind(
115   $sql, \@bind,
116   q/SELECT `me`.`cdid`, `me`.`artist`, `me`.`title`, `me`.`year` FROM `cd` `me` ORDER BY `year DESC`, `title ASC`/, [],
117   'scalar ORDER BY okay (multiple values)'
118 );
119
120 {
121   ($sql, @bind) = $sql_maker->select(
122             [
123               {
124                 'me' => 'cd'
125               }
126             ],
127             [
128               'me.cdid',
129               'me.artist',
130               'me.title',
131               'me.year'
132             ],
133             undef,
134             { order_by => { -desc => 'year' } },
135             undef,
136             undef
137   );
138
139   is_same_sql_bind(
140     $sql, \@bind,
141     q/SELECT `me`.`cdid`, `me`.`artist`, `me`.`title`, `me`.`year` FROM `cd` `me` ORDER BY `year` DESC/, [],
142     'hashref ORDER BY okay (single value)'
143   );
144
145
146   ($sql, @bind) = $sql_maker->select(
147             [
148               {
149                 'me' => 'cd'
150               }
151             ],
152             [
153               'me.cdid',
154               'me.artist',
155               'me.title',
156               'me.year'
157             ],
158             undef,
159             { order_by => [
160               { -desc => 'year' },
161               { -asc => 'title' },
162             ]},
163             undef,
164             undef
165   );
166
167   is_same_sql_bind(
168     $sql, \@bind,
169     q/SELECT `me`.`cdid`, `me`.`artist`, `me`.`title`, `me`.`year` FROM `cd` `me` ORDER BY `year` DESC, `title` ASC/, [],
170     'hashref ORDER BY okay (multiple values)'
171   );
172
173 }
174
175
176 ($sql, @bind) = $sql_maker->select(
177           [
178             {
179               'me' => 'cd'
180             }
181           ],
182           [
183             'me.cdid',
184             'me.artist',
185             'me.title',
186             'me.year'
187           ],
188           undef,
189           { order_by => \'year DESC' },
190           undef,
191           undef
192 );
193
194 is_same_sql_bind(
195   $sql, \@bind,
196   q/SELECT `me`.`cdid`, `me`.`artist`, `me`.`title`, `me`.`year` FROM `cd` `me` ORDER BY year DESC/, [],
197   'did not quote ORDER BY with scalarref (single value)'
198 );
199
200
201 ($sql, @bind) = $sql_maker->select(
202           [
203             {
204               'me' => 'cd'
205             }
206           ],
207           [
208             'me.cdid',
209             'me.artist',
210             'me.title',
211             'me.year'
212           ],
213           undef,
214           { order_by => [
215             \'year DESC',
216             \'title ASC'
217           ]},
218           undef,
219           undef
220 );
221
222 is_same_sql_bind(
223   $sql, \@bind,
224   q/SELECT `me`.`cdid`, `me`.`artist`, `me`.`title`, `me`.`year` FROM `cd` `me` ORDER BY year DESC, title ASC/, [],
225   'did not quote ORDER BY with scalarref (multiple values)'
226 );
227
228
229 ($sql, @bind) = $sql_maker->select(
230   [ { me => 'cd' }                  ],
231   [qw/ me.cdid me.artist me.title  /],
232   { cdid => \['rlike ?', [cdid => 'X'] ]       },
233   { group_by => 'title', having => \['count(me.artist) > ?', [ cnt => 2] ] },
234 );
235
236 is_same_sql_bind(
237   $sql, \@bind,
238   q/SELECT `me`.`cdid`, `me`.`artist`, `me`.`title` FROM `cd` `me` WHERE ( `cdid` rlike ? ) GROUP BY `title` HAVING count(me.artist) > ?/,
239   [ [ cdid => 'X'], ['cnt' => '2'] ],
240   'Quoting works with where/having arrayrefsrefs',
241 );
242
243
244 ($sql, @bind) = $sql_maker->select(
245   [ { me => 'cd' }                  ],
246   [qw/ me.cdid me.artist me.title  /],
247   { cdid => \'rlike X'              },
248   { group_by => 'title', having => \'count(me.artist) > 2' },
249 );
250
251 is_same_sql_bind(
252   $sql, \@bind,
253   q/SELECT `me`.`cdid`, `me`.`artist`, `me`.`title` FROM `cd` `me` WHERE ( `cdid` rlike X ) GROUP BY `title` HAVING count(me.artist) > 2/,
254   [],
255   'Quoting works with where/having scalarrefs',
256 );
257
258
259 ($sql, @bind) = $sql_maker->update(
260           'group',
261           {
262             'order' => '12',
263             'name' => 'Bill'
264           }
265 );
266
267 is_same_sql_bind(
268   $sql, \@bind,
269   q/UPDATE `group` SET `name` = ?, `order` = ?/, [ ['name' => 'Bill'], ['order' => '12'] ],
270   'quoted table names for UPDATE'
271 );
272
273 {
274   ($sql, @bind) = $sql_maker->select(
275         [
276           {
277             'me' => 'cd'
278           }
279         ],
280         [
281           'me.*'
282         ],
283         undef,
284         undef,
285         undef,
286         undef,
287   );
288
289   is_same_sql_bind(
290     $sql, \@bind,
291     q/SELECT `me`.* FROM `cd` `me`/, [],
292     'select attr with me.* is right'
293   );
294 }
295
296
297 $sql_maker->quote_char([qw/[ ]/]);
298
299 ($sql, @bind) = $sql_maker->select(
300           [
301             {
302               'me' => 'cd'
303             },
304             [
305               {
306                 'artist' => 'artist',
307                 '-join_type' => ''
308               },
309               {
310                 'artist.artistid' => 'me.artist'
311               }
312             ]
313           ],
314           [
315             {
316               max => 'rank',
317               -as => 'max_rank',
318             },
319             'rank',
320             {
321               'count' => '*',
322               -as => 'cnt',
323             }
324           ],
325           {
326             'artist.name' => 'Caterwauler McCrae',
327             'me.year' => 2001
328           },
329           undef,
330           undef,
331           undef,
332 );
333
334 is_same_sql_bind(
335   $sql, \@bind,
336   q/SELECT MAX ( [rank] ) AS [max_rank], [rank], COUNT( * ) AS [cnt] FROM [cd] [me]  JOIN [artist] [artist] ON ( [artist].[artistid] = [me].[artist] ) WHERE ( [artist].[name] = ? AND [me].[year] = ? )/, [ ['artist.name' => 'Caterwauler McCrae'], ['me.year' => 2001] ],
337   'got correct SQL and bind parameters for count query with bracket quoting'
338 );
339
340
341 ($sql, @bind) = $sql_maker->update(
342           'group',
343           {
344             'order' => '12',
345             'name' => 'Bill'
346           }
347 );
348
349 is_same_sql_bind(
350   $sql, \@bind,
351   q/UPDATE [group] SET [name] = ?, [order] = ?/, [ ['name' => 'Bill'], ['order' => '12'] ],
352   'bracket quoted table names for UPDATE'
353 );
354
355 done_testing;