We already depend on latest SQLA - remove all references to >= 1.50 - it will only...
[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 {
114   ($sql, @bind) = $sql_maker->select(
115             [
116               {
117                 'me' => 'cd'
118               }
119             ],
120             [
121               'me.cdid',
122               'me.artist',
123               'me.title',
124               'me.year'
125             ],
126             undef,
127             { -desc => 'year' },
128             undef,
129             undef
130   );
131
132   is_same_sql_bind(
133     $sql, \@bind,
134     q/SELECT `me`.`cdid`, `me`.`artist`, `me`.`title`, `me`.`year` FROM `cd` `me` ORDER BY `year` DESC/, [],
135     'hashref ORDER BY okay (single value)'
136   );
137
138
139   ($sql, @bind) = $sql_maker->select(
140             [
141               {
142                 'me' => 'cd'
143               }
144             ],
145             [
146               'me.cdid',
147               'me.artist',
148               'me.title',
149               'me.year'
150             ],
151             undef,
152             [
153               { -desc => 'year' },
154               { -asc => 'title' }
155             ],
156             undef,
157             undef
158   );
159
160   is_same_sql_bind(
161     $sql, \@bind,
162     q/SELECT `me`.`cdid`, `me`.`artist`, `me`.`title`, `me`.`year` FROM `cd` `me` ORDER BY `year` DESC, `title` ASC/, [],
163     'hashref ORDER BY okay (multiple values)'
164   );
165
166 }
167
168
169 ($sql, @bind) = $sql_maker->select(
170           [
171             {
172               'me' => 'cd'
173             }
174           ],
175           [
176             'me.cdid',
177             'me.artist',
178             'me.title',
179             'me.year'
180           ],
181           undef,
182           \'year DESC',
183           undef,
184           undef
185 );
186
187 is_same_sql_bind(
188   $sql, \@bind,
189   q/SELECT `me`.`cdid`, `me`.`artist`, `me`.`title`, `me`.`year` FROM `cd` `me` ORDER BY year DESC/, [],
190   'did not quote ORDER BY with scalarref (single value)'
191 );
192
193
194 ($sql, @bind) = $sql_maker->select(
195           [
196             {
197               'me' => 'cd'
198             }
199           ],
200           [
201             'me.cdid',
202             'me.artist',
203             'me.title',
204             'me.year'
205           ],
206           undef,
207           [
208             \'year DESC',
209             \'title ASC'
210           ],
211           undef,
212           undef
213 );
214
215 is_same_sql_bind(
216   $sql, \@bind,
217   q/SELECT `me`.`cdid`, `me`.`artist`, `me`.`title`, `me`.`year` FROM `cd` `me` ORDER BY year DESC, title ASC/, [],
218   'did not quote ORDER BY with scalarref (multiple values)'
219 );
220
221
222 ($sql, @bind) = $sql_maker->update(
223           'group',
224           {
225             'order' => '12',
226             'name' => 'Bill'
227           }
228 );
229
230 is_same_sql_bind(
231   $sql, \@bind,
232   q/UPDATE `group` SET `name` = ?, `order` = ?/, [ ['name' => 'Bill'], ['order' => '12'] ],
233   'quoted table names for UPDATE'
234 );
235
236 {
237   ($sql, @bind) = $sql_maker->select(
238         [
239           {
240             'me' => 'cd'
241           }
242         ],
243         [
244           'me.*'
245         ],
246         undef,
247         [],
248         undef,
249         undef    
250   );
251
252   is_same_sql_bind(
253     $sql, \@bind,
254     q/SELECT `me`.* FROM `cd` `me`/, [],
255     'select attr with me.* is right'
256   );
257 }
258
259
260 $sql_maker->quote_char([qw/[ ]/]);
261
262 ($sql, @bind) = $sql_maker->select(
263           [
264             {
265               'me' => 'cd'
266             },
267             [
268               {
269                 'artist' => 'artist',
270                 '-join_type' => ''
271               },
272               {
273                 'artist.artistid' => 'me.artist'
274               }
275             ]
276           ],
277           [
278             {
279               'count' => '*'
280             }
281           ],
282           {
283             'artist.name' => 'Caterwauler McCrae',
284             'me.year' => 2001
285           },
286           [],
287           undef,
288           undef
289 );
290
291 is_same_sql_bind(
292   $sql, \@bind,
293   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] ],
294   'got correct SQL and bind parameters for count query with bracket quoting'
295 );
296
297
298 ($sql, @bind) = $sql_maker->update(
299           'group',
300           {
301             'order' => '12',
302             'name' => 'Bill'
303           }
304 );
305
306 is_same_sql_bind(
307   $sql, \@bind,
308   q/UPDATE [group] SET [name] = ?, [order] = ?/, [ ['name' => 'Bill'], ['order' => '12'] ],
309   'bracket quoted table names for UPDATE'
310 );