* Disabled stringification of arrayref bind values (used to pass values for PostgreS...
[dbsrgits/DBIx-Class.git] / t / 95sql_maker_quote.t
CommitLineData
3da841f1 1use strict;
2use warnings;
3
4use Test::More;
9b459129 5use SQL::Abstract::Test import => ['is_same_sql_bind'];
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
114
2924125d 115($sql, @bind) = $sql_maker->select(
c80207cd 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
2924125d 131);
3da841f1 132
2924125d 133is_same_sql_bind(
134 $sql, \@bind,
c80207cd 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)'
2924125d 137);
3da841f1 138
c80207cd 139
9b459129 140($sql, @bind) = $sql_maker->select(
3da841f1 141 [
142 {
143 'me' => 'cd'
144 }
145 ],
146 [
147 'me.cdid',
148 'me.artist',
149 'me.title',
150 'me.year'
151 ],
152 undef,
153 [
c80207cd 154 { -desc => 'year' },
155 { -asc => 'title' }
156 ],
157 undef,
158 undef
159);
160
161is_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'
3da841f1 179 ],
180 undef,
c80207cd 181 \'year DESC',
182 undef,
3da841f1 183 undef
184);
185
9b459129 186is_same_sql_bind(
187 $sql, \@bind,
188 q/SELECT `me`.`cdid`, `me`.`artist`, `me`.`title`, `me`.`year` FROM `cd` `me` ORDER BY year DESC/, [],
c80207cd 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
214is_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)'
3da841f1 218);
219
c80207cd 220
9b459129 221($sql, @bind) = $sql_maker->update(
3da841f1 222 'group',
223 {
224 'order' => '12',
225 'name' => 'Bill'
226 }
227);
228
9b459129 229is_same_sql_bind(
230 $sql, \@bind,
231 q/UPDATE `group` SET `name` = ?, `order` = ?/, [ ['name' => 'Bill'], ['order' => '12'] ],
232 'quoted table names for UPDATE'
233);
3da841f1 234
c80207cd 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
251is_same_sql_bind(
252 $sql, \@bind,
253 q/SELECT `me`.* FROM `cd` `me`/, [],
254 'select attr with me.* is right'
255);
256
257
3da841f1 258$sql_maker->quote_char([qw/[ ]/]);
259
9b459129 260($sql, @bind) = $sql_maker->select(
3da841f1 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
9b459129 289is_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);
3da841f1 294
295
9b459129 296($sql, @bind) = $sql_maker->update(
3da841f1 297 'group',
298 {
299 'order' => '12',
300 'name' => 'Bill'
301 }
302);
303
9b459129 304is_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);