* Removed an outdated comment.
[dbsrgits/DBIx-Class.git] / t / 95sql_maker_quote.t
CommitLineData
3da841f1 1use strict;
2use warnings;
3
4use Test::More;
949172b0 5use DBIC::SqlMakerTest;
3da841f1 6
7
8BEGIN {
9 eval "use DBD::SQLite";
10 plan $@
11 ? ( skip_all => 'needs DBD::SQLite for testing' )
c80207cd 12 : ( tests => 12 );
3da841f1 13}
14
15use lib qw(t/lib);
16
17use_ok('DBICTest');
18
c216324a 19my $schema = DBICTest->init_schema();
3da841f1 20
c216324a 21my $sql_maker = $schema->storage->sql_maker;
3da841f1 22
23$sql_maker->quote_char('`');
24$sql_maker->name_sep('.');
25
9b459129 26my ($sql, @bind) = $sql_maker->select(
3da841f1 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
9b459129 55is_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);
3da841f1 60
c80207cd 61
9b459129 62($sql, @bind) = $sql_maker->select(
3da841f1 63 [
64 {
65 'me' => 'cd'
66 }
67 ],
68 [
69 'me.cdid',
70 'me.artist',
71 'me.title',
72 'me.year'
73 ],
74 undef,
c80207cd 75 'year DESC',
76 undef,
77 undef
78);
79
80is_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(
3da841f1 88 [
c80207cd 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'
3da841f1 103 ],
104 undef,
105 undef
106);
107
8682bb07 108is_same_sql_bind(
109 $sql, \@bind,
c80207cd 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)'
8682bb07 112);
113
89479564 114SKIP: {
89869c75 115 skip "SQL::Abstract < 1.49 does not support hashrefs in order_by", 2
116 if $SQL::Abstract::VERSION < 1.49;
8682bb07 117
89479564 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 );
3da841f1 135
89479564 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 );
3da841f1 141
c80207cd 142
89479564 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 );
c80207cd 169
89479564 170}
c80207cd 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'
3da841f1 184 ],
185 undef,
c80207cd 186 \'year DESC',
187 undef,
3da841f1 188 undef
189);
190
9b459129 191is_same_sql_bind(
192 $sql, \@bind,
193 q/SELECT `me`.`cdid`, `me`.`artist`, `me`.`title`, `me`.`year` FROM `cd` `me` ORDER BY year DESC/, [],
c80207cd 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
219is_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)'
3da841f1 223);
224
c80207cd 225
9b459129 226($sql, @bind) = $sql_maker->update(
3da841f1 227 'group',
228 {
229 'order' => '12',
230 'name' => 'Bill'
231 }
232);
233
9b459129 234is_same_sql_bind(
235 $sql, \@bind,
236 q/UPDATE `group` SET `name` = ?, `order` = ?/, [ ['name' => 'Bill'], ['order' => '12'] ],
237 'quoted table names for UPDATE'
238);
3da841f1 239
949172b0 240SKIP: {
7ec9e4cf 241 skip "select attr with star does not work in SQL::Abstract < 1.49", 1
949172b0 242 if $SQL::Abstract::VERSION < 1.49;
c80207cd 243
949172b0 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 );
c80207cd 258
949172b0 259 is_same_sql_bind(
260 $sql, \@bind,
261 q/SELECT `me`.* FROM `cd` `me`/, [],
262 'select attr with me.* is right'
263 );
264}
c80207cd 265
266
3da841f1 267$sql_maker->quote_char([qw/[ ]/]);
268
9b459129 269($sql, @bind) = $sql_maker->select(
3da841f1 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
9b459129 298is_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);
3da841f1 303
304
9b459129 305($sql, @bind) = $sql_maker->update(
3da841f1 306 'group',
307 {
308 'order' => '12',
309 'name' => 'Bill'
310 }
311);
312
9b459129 313is_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);