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