Fix some should_quote_data_type problems
[dbsrgits/DBIx-Class.git] / t / 95sql_maker_quote.t
CommitLineData
3da841f1 1use strict;
2use warnings;
3
4use Test::More;
5
c61a0748 6use lib qw(t/lib);
7use DBIC::SqlMakerTest;
3da841f1 8
9BEGIN {
10 eval "use DBD::SQLite";
11 plan $@
12 ? ( skip_all => 'needs DBD::SQLite for testing' )
c80207cd 13 : ( tests => 12 );
3da841f1 14}
15
3da841f1 16use_ok('DBICTest');
17
c216324a 18my $schema = DBICTest->init_schema();
3da841f1 19
c216324a 20my $sql_maker = $schema->storage->sql_maker;
3da841f1 21
22$sql_maker->quote_char('`');
23$sql_maker->name_sep('.');
24
9b459129 25my ($sql, @bind) = $sql_maker->select(
3da841f1 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
9b459129 54is_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);
3da841f1 59
c80207cd 60
9b459129 61($sql, @bind) = $sql_maker->select(
3da841f1 62 [
63 {
64 'me' => 'cd'
65 }
66 ],
67 [
68 'me.cdid',
69 'me.artist',
70 'me.title',
71 'me.year'
72 ],
73 undef,
c80207cd 74 'year DESC',
75 undef,
76 undef
77);
78
79is_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(
3da841f1 87 [
c80207cd 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'
3da841f1 102 ],
103 undef,
104 undef
105);
106
8682bb07 107is_same_sql_bind(
108 $sql, \@bind,
c80207cd 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)'
8682bb07 111);
112
89479564 113SKIP: {
89869c75 114 skip "SQL::Abstract < 1.49 does not support hashrefs in order_by", 2
115 if $SQL::Abstract::VERSION < 1.49;
8682bb07 116
89479564 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 );
3da841f1 134
89479564 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 );
3da841f1 140
c80207cd 141
89479564 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 );
c80207cd 168
89479564 169}
c80207cd 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'
3da841f1 183 ],
184 undef,
c80207cd 185 \'year DESC',
186 undef,
3da841f1 187 undef
188);
189
9b459129 190is_same_sql_bind(
191 $sql, \@bind,
192 q/SELECT `me`.`cdid`, `me`.`artist`, `me`.`title`, `me`.`year` FROM `cd` `me` ORDER BY year DESC/, [],
c80207cd 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
218is_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)'
3da841f1 222);
223
c80207cd 224
9b459129 225($sql, @bind) = $sql_maker->update(
3da841f1 226 'group',
227 {
228 'order' => '12',
229 'name' => 'Bill'
230 }
231);
232
9b459129 233is_same_sql_bind(
234 $sql, \@bind,
235 q/UPDATE `group` SET `name` = ?, `order` = ?/, [ ['name' => 'Bill'], ['order' => '12'] ],
236 'quoted table names for UPDATE'
237);
3da841f1 238
949172b0 239SKIP: {
7ec9e4cf 240 skip "select attr with star does not work in SQL::Abstract < 1.49", 1
949172b0 241 if $SQL::Abstract::VERSION < 1.49;
c80207cd 242
949172b0 243 ($sql, @bind) = $sql_maker->select(
244 [
245 {
246 'me' => 'cd'
247 }
248 ],
249 [
250 'me.*'
251 ],
252 undef,
253 [],
254 undef,
255 undef
256 );
c80207cd 257
949172b0 258 is_same_sql_bind(
259 $sql, \@bind,
260 q/SELECT `me`.* FROM `cd` `me`/, [],
261 'select attr with me.* is right'
262 );
263}
c80207cd 264
265
3da841f1 266$sql_maker->quote_char([qw/[ ]/]);
267
9b459129 268($sql, @bind) = $sql_maker->select(
3da841f1 269 [
270 {
271 'me' => 'cd'
272 },
273 [
274 {
275 'artist' => 'artist',
276 '-join_type' => ''
277 },
278 {
279 'artist.artistid' => 'me.artist'
280 }
281 ]
282 ],
283 [
284 {
285 'count' => '*'
286 }
287 ],
288 {
289 'artist.name' => 'Caterwauler McCrae',
290 'me.year' => 2001
291 },
292 [],
293 undef,
294 undef
295);
296
9b459129 297is_same_sql_bind(
298 $sql, \@bind,
299 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] ],
300 'got correct SQL and bind parameters for count query with bracket quoting'
301);
3da841f1 302
303
9b459129 304($sql, @bind) = $sql_maker->update(
3da841f1 305 'group',
306 {
307 'order' => '12',
308 'name' => 'Bill'
309 }
310);
311
9b459129 312is_same_sql_bind(
313 $sql, \@bind,
314 q/UPDATE [group] SET [name] = ?, [order] = ?/, [ ['name' => 'Bill'], ['order' => '12'] ],
315 'bracket quoted table names for UPDATE'
316);