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