4fa987bfef4159d091e3734ac599e0c0ceb0fd86
[dbsrgits/DBIx-Class.git] / t / 95sql_maker_quote.t
1 use strict;
2 use warnings;
3
4 use Test::More;
5
6 use lib qw(t/lib);
7 use DBIC::SqlMakerTest;
8
9 BEGIN {
10     eval "use DBD::SQLite";
11     plan $@
12         ? ( skip_all => 'needs DBD::SQLite for testing' )
13         : ( tests => 12 );
14 }
15
16 use_ok('DBICTest');
17
18 my $schema = DBICTest->init_schema();
19
20 my $sql_maker = $schema->storage->sql_maker;
21
22 $sql_maker->quote_char('`');
23 $sql_maker->name_sep('.');
24
25 my ($sql, @bind) = $sql_maker->select(
26           [
27             {
28               'me' => 'cd'
29             },
30             [
31               {
32                 'artist' => 'artist',
33                 '-join_type' => ''
34               },
35               {
36                 'artist.artistid' => 'me.artist'
37               }
38             ]
39           ],
40           [
41             {
42               'count' => '*'
43             }
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/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] ],
57   'got correct SQL and bind parameters for count query with quoting'
58 );
59
60
61 ($sql, @bind) = $sql_maker->select(
62           [
63             {
64               'me' => 'cd'
65             }
66           ],
67           [
68             'me.cdid',
69             'me.artist',
70             'me.title',
71             'me.year'
72           ],
73           undef,
74           'year DESC',
75           undef,
76           undef
77 );
78
79 is_same_sql_bind(
80   $sql, \@bind,
81   q/SELECT `me`.`cdid`, `me`.`artist`, `me`.`title`, `me`.`year` FROM `cd` `me` ORDER BY `year DESC`/, [],
82   'scalar ORDER BY okay (single value)'
83 );
84
85
86 ($sql, @bind) = $sql_maker->select(
87           [
88             {
89               'me' => 'cd'
90             }
91           ],
92           [
93             'me.cdid',
94             'me.artist',
95             'me.title',
96             'me.year'
97           ],
98           undef,
99           [
100             'year DESC',
101             'title ASC'
102           ],
103           undef,
104           undef
105 );
106
107 is_same_sql_bind(
108   $sql, \@bind,
109   q/SELECT `me`.`cdid`, `me`.`artist`, `me`.`title`, `me`.`year` FROM `cd` `me` ORDER BY `year DESC`, `title ASC`/, [],
110   'scalar ORDER BY okay (multiple values)'
111 );
112
113 SKIP: {
114   skip "SQL::Abstract < 1.49 does not support hashrefs in order_by", 2
115     if $SQL::Abstract::VERSION < 1.49;
116
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   );
134
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   );
140
141
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   );
168
169 }
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'
183           ],
184           undef,
185           \'year DESC',
186           undef,
187           undef
188 );
189
190 is_same_sql_bind(
191   $sql, \@bind,
192   q/SELECT `me`.`cdid`, `me`.`artist`, `me`.`title`, `me`.`year` FROM `cd` `me` ORDER BY year DESC/, [],
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
218 is_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)'
222 );
223
224
225 ($sql, @bind) = $sql_maker->update(
226           'group',
227           {
228             'order' => '12',
229             'name' => 'Bill'
230           }
231 );
232
233 is_same_sql_bind(
234   $sql, \@bind,
235   q/UPDATE `group` SET `name` = ?, `order` = ?/, [ ['name' => 'Bill'], ['order' => '12'] ],
236   'quoted table names for UPDATE'
237 );
238
239 SKIP: {
240   skip "select attr with star does not work in SQL::Abstract < 1.49", 1
241     if $SQL::Abstract::VERSION < 1.49;
242
243   ($sql, @bind) = $sql_maker->select(
244         [
245           {
246             'me' => 'cd'
247           }
248         ],
249         [
250           'me.*'
251         ],
252         undef,
253         [],
254         undef,
255         undef    
256   );
257
258   is_same_sql_bind(
259     $sql, \@bind,
260     q/SELECT `me`.* FROM `cd` `me`/, [],
261     'select attr with me.* is right'
262   );
263 }
264
265
266 $sql_maker->quote_char([qw/[ ]/]);
267
268 ($sql, @bind) = $sql_maker->select(
269           [
270             {
271               'me' => 'cd'
272             },
273             [
274               {
275                 'artist' => 'artist',
276                 '-join_type' => ''
277               },
278               {
279                 'artist.artistid' => 'me.artist'
280               }
281             ]
282           ],
283           [
284             {
285               'count' => '*'
286             }
287           ],
288           {
289             'artist.name' => 'Caterwauler McCrae',
290             'me.year' => 2001
291           },
292           [],
293           undef,
294           undef
295 );
296
297 is_same_sql_bind(
298   $sql, \@bind,
299   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] ],
300   'got correct SQL and bind parameters for count query with bracket quoting'
301 );
302
303
304 ($sql, @bind) = $sql_maker->update(
305           'group',
306           {
307             'order' => '12',
308             'name' => 'Bill'
309           }
310 );
311
312 is_same_sql_bind(
313   $sql, \@bind,
314   q/UPDATE [group] SET [name] = ?, [order] = ?/, [ ['name' => 'Bill'], ['order' => '12'] ],
315   'bracket quoted table names for UPDATE'
316 );