* Made some more tests SKIP if SQLA version < 1.50.
[dbsrgits/DBIx-Class.git] / t / 95sql_maker_quote.t
1 use strict;
2 use warnings;
3
4 use Test::More;
5 use SQL::Abstract::Test import => ['is_same_sql_bind'];
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.50 does not support hashrefs in order_by", 2 if $SQL::Abstract::VERSION < 1.50;
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
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
255 is_same_sql_bind(
256   $sql, \@bind,
257   q/SELECT `me`.* FROM `cd` `me`/, [],
258   'select attr with me.* is right'
259 );
260
261
262 $sql_maker->quote_char([qw/[ ]/]);
263
264 ($sql, @bind) = $sql_maker->select(
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
293 is_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 );
298
299
300 ($sql, @bind) = $sql_maker->update(
301           'group',
302           {
303             'order' => '12',
304             'name' => 'Bill'
305           }
306 );
307
308 is_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 );