We already depend on latest SQLA - remove all references to >= 1.50 - it will only...
[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
20ea616f 113{
89479564 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 );
3da841f1 131
89479564 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 );
3da841f1 137
c80207cd 138
89479564 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 );
c80207cd 165
89479564 166}
c80207cd 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'
3da841f1 180 ],
181 undef,
c80207cd 182 \'year DESC',
183 undef,
3da841f1 184 undef
185);
186
9b459129 187is_same_sql_bind(
188 $sql, \@bind,
189 q/SELECT `me`.`cdid`, `me`.`artist`, `me`.`title`, `me`.`year` FROM `cd` `me` ORDER BY year DESC/, [],
c80207cd 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
215is_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)'
3da841f1 219);
220
c80207cd 221
9b459129 222($sql, @bind) = $sql_maker->update(
3da841f1 223 'group',
224 {
225 'order' => '12',
226 'name' => 'Bill'
227 }
228);
229
9b459129 230is_same_sql_bind(
231 $sql, \@bind,
232 q/UPDATE `group` SET `name` = ?, `order` = ?/, [ ['name' => 'Bill'], ['order' => '12'] ],
233 'quoted table names for UPDATE'
234);
3da841f1 235
20ea616f 236{
949172b0 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 );
c80207cd 251
949172b0 252 is_same_sql_bind(
253 $sql, \@bind,
254 q/SELECT `me`.* FROM `cd` `me`/, [],
255 'select attr with me.* is right'
256 );
257}
c80207cd 258
259
3da841f1 260$sql_maker->quote_char([qw/[ ]/]);
261
9b459129 262($sql, @bind) = $sql_maker->select(
3da841f1 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
9b459129 291is_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);
3da841f1 296
297
9b459129 298($sql, @bind) = $sql_maker->update(
3da841f1 299 'group',
300 {
301 'order' => '12',
302 'name' => 'Bill'
303 }
304);
305
9b459129 306is_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);