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