cleanup test
|
1 |
use Test::More; |
2 |
use strict; |
|
3 |
use warnings; |
|
4 |
use utf8; |
|
5 |
use Encode qw/encode_utf8 decode_utf8/; |
|
test cleanup
|
6 |
use FindBin; |
cleanup test
|
7 |
use lib "$FindBin::Bin/common"; |
cleanup test
|
8 | |
9 |
BEGIN { |
|
10 |
eval { require DBD::SQLite; 1 } |
|
11 |
or plan skip_all => 'DBD::SQLite required'; |
|
12 |
eval { DBD::SQLite->VERSION >= 1.25 } |
|
13 |
or plan skip_all => 'DBD::SQLite >= 1.25 required'; |
|
14 | ||
15 |
plan 'no_plan'; |
|
16 |
use_ok('DBIx::Custom'); |
|
17 |
} |
|
18 | ||
test cleanup
|
19 |
$SIG{__WARN__} = sub { warn $_[0] unless $_[0] =~ /DEPRECATED/}; |
cleanup test
|
20 |
sub test { print "# $_[0]\n" } |
21 | ||
test cleanup
|
22 |
use DBIx::Custom; |
test cleanup
|
23 |
{ |
24 |
package DBIx::Custom; |
|
25 |
has dsn => sub { 'dbi:SQLite:dbname=:memory:' } |
|
26 |
} |
|
test cleanup
|
27 |
use MyDBI1; |
28 |
{ |
|
29 |
package MyDBI4; |
|
30 | ||
31 |
use strict; |
|
32 |
use warnings; |
|
33 | ||
34 |
use base 'DBIx::Custom'; |
|
35 | ||
36 |
sub connect { |
|
37 |
my $self = shift->SUPER::connect(@_); |
|
38 |
|
|
39 |
$self->include_model( |
|
40 |
MyModel2 => [ |
|
41 |
'book', |
|
42 |
{class => 'Company', name => 'company'} |
|
43 |
] |
|
44 |
); |
|
45 |
} |
|
46 | ||
47 |
package MyModel2::Base1; |
|
48 | ||
49 |
use strict; |
|
50 |
use warnings; |
|
51 | ||
52 |
use base 'DBIx::Custom::Model'; |
|
53 | ||
54 |
package MyModel2::book; |
|
55 | ||
56 |
use strict; |
|
57 |
use warnings; |
|
58 | ||
59 |
use base 'MyModel2::Base1'; |
|
60 | ||
61 |
sub insert { |
|
62 |
my ($self, $param) = @_; |
|
63 |
|
|
64 |
return $self->SUPER::insert(param => $param); |
|
65 |
} |
|
66 | ||
67 |
sub list { shift->select; } |
|
68 | ||
69 |
package MyModel2::Company; |
|
70 | ||
71 |
use strict; |
|
72 |
use warnings; |
|
73 | ||
74 |
use base 'MyModel2::Base1'; |
|
75 | ||
76 |
sub insert { |
|
77 |
my ($self, $param) = @_; |
|
78 |
|
|
79 |
return $self->SUPER::insert(param => $param); |
|
80 |
} |
|
81 | ||
82 |
sub list { shift->select; } |
|
83 |
} |
|
84 |
{ |
|
85 |
package MyDBI5; |
|
86 | ||
87 |
use strict; |
|
88 |
use warnings; |
|
89 | ||
90 |
use base 'DBIx::Custom'; |
|
91 | ||
92 |
sub connect { |
|
93 |
my $self = shift->SUPER::connect(@_); |
|
94 |
|
|
95 |
$self->include_model('MyModel4'); |
|
96 |
} |
|
97 |
} |
|
98 |
{ |
|
99 |
package MyDBI6; |
|
100 |
|
|
101 |
use base 'DBIx::Custom'; |
|
102 |
|
|
103 |
sub connect { |
|
104 |
my $self = shift->SUPER::connect(@_); |
|
105 |
|
|
106 |
$self->include_model('MyModel5'); |
|
107 |
|
|
108 |
return $self; |
|
109 |
} |
|
110 |
} |
|
111 |
{ |
|
112 |
package MyDBI7; |
|
113 |
|
|
114 |
use base 'DBIx::Custom'; |
|
115 |
|
|
116 |
sub connect { |
|
117 |
my $self = shift->SUPER::connect(@_); |
|
118 |
|
|
119 |
$self->include_model('MyModel6'); |
|
120 |
|
|
121 |
|
|
122 |
return $self; |
|
123 |
} |
|
124 |
} |
|
125 |
{ |
|
126 |
package MyDBI8; |
|
127 |
|
|
128 |
use base 'DBIx::Custom'; |
|
129 |
|
|
130 |
sub connect { |
|
131 |
my $self = shift->SUPER::connect(@_); |
|
132 |
|
|
133 |
$self->include_model('MyModel7'); |
|
134 |
|
|
135 |
return $self; |
|
136 |
} |
|
137 |
} |
|
138 | ||
139 |
{ |
|
140 |
package MyDBI9; |
|
141 |
|
|
142 |
use base 'DBIx::Custom'; |
|
143 |
|
|
144 |
sub connect { |
|
145 |
my $self = shift->SUPER::connect(@_); |
|
146 |
|
|
147 |
$self->include_model('MyModel8')->setup_model; |
|
148 |
|
|
149 |
return $self; |
|
150 |
} |
|
151 |
} |
|
test cleanup
|
152 | |
cleanup test
|
153 |
# Constant |
cleanup test
|
154 |
my $create_table1 = 'create table table1 (key1 char(255), key2 char(255));'; |
155 |
my $create_table1_2 = 'create table table1 (key1 char(255), key2 char(255), key3 char(255), key4 char(255), key5 char(255));'; |
|
test cleanup
|
156 |
my $create_table2 = 'create table table2 (key1 char(255), key3 char(255));'; |
cleanup test
|
157 |
my $create_table2_2 = "create table table2 (key1, key2, key3)"; |
158 |
my $create_table3 = "create table table3 (key1, key2, key3)"; |
|
test cleanup
|
159 |
my $create_table_reserved = 'create table "table" ("select", "update")'; |
160 | ||
test cleanup
|
161 |
my $q = '"'; |
162 |
my $p = '"'; |
|
cleanup test
|
163 | |
cleanup test
|
164 |
# Variables |
cleanup test
|
165 |
my $builder; |
166 |
my $datas; |
|
cleanup test
|
167 |
my $dbi; |
168 |
my $sth; |
|
169 |
my $source; |
|
170 |
my @sources; |
|
cleanup test
|
171 |
my $select_source; |
172 |
my $insert_source; |
|
173 |
my $update_source; |
|
cleanup test
|
174 |
my $param; |
175 |
my $params; |
|
176 |
my $sql; |
|
177 |
my $result; |
|
178 |
my $row; |
|
179 |
my @rows; |
|
180 |
my $rows; |
|
181 |
my $query; |
|
182 |
my @queries; |
|
183 |
my $select_query; |
|
184 |
my $insert_query; |
|
185 |
my $update_query; |
|
186 |
my $ret_val; |
|
187 |
my $infos; |
|
188 |
my $model; |
|
189 |
my $model2; |
|
190 |
my $where; |
|
191 |
my $update_param; |
|
192 |
my $insert_param; |
|
cleanup test
|
193 |
my $join; |
cleanup test
|
194 |
my $binary; |
cleanup test
|
195 | |
196 |
# Prepare table |
|
test cleanup
|
197 |
$dbi = DBIx::Custom->connect; |
cleanup test
|
198 | |
199 | ||
200 | ||
201 |
test 'model insert id and primary_key option'; |
|
test cleanup
|
202 |
$dbi = MyDBI6->connect; |
cleanup test
|
203 |
eval { $dbi->execute('drop table table1') }; |
cleanup test
|
204 |
$dbi->execute($create_table1_2); |
cleanup test
|
205 |
$dbi->model('table1')->insert( |
206 |
id => [1, 2], |
|
207 |
param => {key3 => 3} |
|
208 |
); |
|
209 |
$result = $dbi->model('table1')->select; |
|
210 |
$row = $result->one; |
|
211 |
is($row->{key1}, 1); |
|
212 |
is($row->{key2}, 2); |
|
213 |
is($row->{key3}, 3); |
|
214 | ||
test cleanup
|
215 |
$dbi = MyDBI6->connect; |
cleanup test
|
216 |
eval { $dbi->execute('drop table table1') }; |
cleanup test
|
217 |
$dbi->execute($create_table1_2); |
cleanup test
|
218 |
$dbi->model('table1')->insert( |
219 |
{key3 => 3}, |
|
220 |
id => [1, 2] |
|
221 |
); |
|
222 |
$result = $dbi->model('table1')->select; |
|
223 |
$row = $result->one; |
|
224 |
is($row->{key1}, 1); |
|
225 |
is($row->{key2}, 2); |
|
226 |
is($row->{key3}, 3); |
|
227 | ||
228 |
test 'update and id option'; |
|
test cleanup
|
229 |
$dbi = DBIx::Custom->connect; |
cleanup test
|
230 |
eval { $dbi->execute('drop table table1') }; |
cleanup test
|
231 |
$dbi->execute($create_table1_2); |
cleanup test
|
232 |
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3}); |
233 |
$dbi->update( |
|
234 |
table => 'table1', |
|
235 |
primary_key => ['key1', 'key2'], |
|
236 |
id => [1, 2], |
|
237 |
param => {key3 => 4} |
|
238 |
); |
|
239 |
is($dbi->select(table => 'table1')->one->{key1}, 1); |
|
240 |
is($dbi->select(table => 'table1')->one->{key2}, 2); |
|
241 |
is($dbi->select(table => 'table1')->one->{key3}, 4); |
|
242 | ||
243 |
$dbi->delete_all(table => 'table1'); |
|
244 |
$dbi->insert(table => 'table1', param => {key1 => 0, key2 => 2, key3 => 3}); |
|
245 |
$dbi->update( |
|
246 |
table => 'table1', |
|
247 |
primary_key => 'key1', |
|
248 |
id => 0, |
|
249 |
param => {key3 => 4} |
|
250 |
); |
|
251 |
is($dbi->select(table => 'table1')->one->{key1}, 0); |
|
252 |
is($dbi->select(table => 'table1')->one->{key2}, 2); |
|
253 |
is($dbi->select(table => 'table1')->one->{key3}, 4); |
|
254 | ||
test cleanup
|
255 |
$dbi = DBIx::Custom->connect; |
cleanup test
|
256 |
eval { $dbi->execute('drop table table1') }; |
cleanup test
|
257 |
$dbi->execute($create_table1_2); |
cleanup test
|
258 |
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3}); |
259 |
$dbi->update( |
|
260 |
{key3 => 4}, |
|
261 |
table => 'table1', |
|
262 |
primary_key => ['key1', 'key2'], |
|
263 |
id => [1, 2] |
|
264 |
); |
|
265 |
is($dbi->select(table => 'table1')->one->{key1}, 1); |
|
266 |
is($dbi->select(table => 'table1')->one->{key2}, 2); |
|
267 |
is($dbi->select(table => 'table1')->one->{key3}, 4); |
|
268 | ||
269 | ||
270 |
test 'model update and id option'; |
|
test cleanup
|
271 |
$dbi = MyDBI6->connect; |
cleanup test
|
272 |
eval { $dbi->execute('drop table table1') }; |
cleanup test
|
273 |
$dbi->execute($create_table1_2); |
cleanup test
|
274 |
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3}); |
275 |
$dbi->model('table1')->update( |
|
276 |
id => [1, 2], |
|
277 |
param => {key3 => 4} |
|
278 |
); |
|
279 |
$result = $dbi->model('table1')->select; |
|
280 |
$row = $result->one; |
|
281 |
is($row->{key1}, 1); |
|
282 |
is($row->{key2}, 2); |
|
283 |
is($row->{key3}, 4); |
|
284 | ||
285 | ||
286 |
test 'delete and id option'; |
|
test cleanup
|
287 |
$dbi = DBIx::Custom->connect; |
cleanup test
|
288 |
eval { $dbi->execute('drop table table1') }; |
cleanup test
|
289 |
$dbi->execute($create_table1_2); |
cleanup test
|
290 |
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3}); |
291 |
$dbi->delete( |
|
292 |
table => 'table1', |
|
293 |
primary_key => ['key1', 'key2'], |
|
294 |
id => [1, 2], |
|
295 |
); |
|
296 |
is_deeply($dbi->select(table => 'table1')->all, []); |
|
297 | ||
298 |
$dbi->insert(table => 'table1', param => {key1 => 0, key2 => 2, key3 => 3}); |
|
299 |
$dbi->delete( |
|
300 |
table => 'table1', |
|
301 |
primary_key => 'key1', |
|
302 |
id => 0, |
|
303 |
); |
|
304 |
is_deeply($dbi->select(table => 'table1')->all, []); |
|
305 | ||
306 | ||
307 |
test 'model delete and id option'; |
|
test cleanup
|
308 |
$dbi = MyDBI6->connect; |
cleanup test
|
309 |
eval { $dbi->execute('drop table table1') }; |
310 |
eval { $dbi->execute('drop table table2') }; |
|
311 |
eval { $dbi->execute('drop table table3') }; |
|
cleanup test
|
312 |
$dbi->execute($create_table1_2); |
cleanup test
|
313 |
$dbi->execute($create_table2_2); |
314 |
$dbi->execute($create_table3); |
|
cleanup test
|
315 |
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3}); |
316 |
$dbi->model('table1')->delete(id => [1, 2]); |
|
317 |
is_deeply($dbi->select(table => 'table1')->all, []); |
|
318 |
$dbi->insert(table => 'table2', param => {key1 => 1, key2 => 2, key3 => 3}); |
|
319 |
$dbi->model('table1_1')->delete(id => [1, 2]); |
|
320 |
is_deeply($dbi->select(table => 'table1')->all, []); |
|
321 |
$dbi->insert(table => 'table3', param => {key1 => 1, key2 => 2, key3 => 3}); |
|
322 |
$dbi->model('table1_3')->delete(id => [1, 2]); |
|
323 |
is_deeply($dbi->select(table => 'table1')->all, []); |
|
324 | ||
325 | ||
326 |
test 'select and id option'; |
|
test cleanup
|
327 |
$dbi = DBIx::Custom->connect; |
cleanup test
|
328 |
eval { $dbi->execute('drop table table1') }; |
cleanup test
|
329 |
$dbi->execute($create_table1_2); |
cleanup test
|
330 |
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3}); |
331 |
$result = $dbi->select( |
|
332 |
table => 'table1', |
|
333 |
primary_key => ['key1', 'key2'], |
|
334 |
id => [1, 2] |
|
335 |
); |
|
336 |
$row = $result->one; |
|
337 |
is($row->{key1}, 1); |
|
338 |
is($row->{key2}, 2); |
|
339 |
is($row->{key3}, 3); |
|
340 | ||
341 |
$dbi->delete_all(table => 'table1'); |
|
342 |
$dbi->insert(table => 'table1', param => {key1 => 0, key2 => 2, key3 => 3}); |
|
343 |
$result = $dbi->select( |
|
344 |
table => 'table1', |
|
345 |
primary_key => 'key1', |
|
346 |
id => 0, |
|
347 |
); |
|
348 |
$row = $result->one; |
|
349 |
is($row->{key1}, 0); |
|
350 |
is($row->{key2}, 2); |
|
351 |
is($row->{key3}, 3); |
|
352 | ||
353 |
$dbi->delete_all(table => 'table1'); |
|
354 |
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3}); |
|
355 |
$result = $dbi->select( |
|
356 |
table => 'table1', |
|
357 |
primary_key => ['key1', 'key2'], |
|
358 |
id => [1, 2] |
|
359 |
); |
|
360 |
$row = $result->one; |
|
361 |
is($row->{key1}, 1); |
|
362 |
is($row->{key2}, 2); |
|
363 |
is($row->{key3}, 3); |
|
364 | ||
365 | ||
366 |
test 'model select_at'; |
|
test cleanup
|
367 |
$dbi = MyDBI6->connect; |
cleanup test
|
368 |
eval { $dbi->execute('drop table table1') }; |
cleanup test
|
369 |
$dbi->execute($create_table1_2); |
cleanup test
|
370 |
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3}); |
371 |
$result = $dbi->model('table1')->select(id => [1, 2]); |
|
372 |
$row = $result->one; |
|
373 |
is($row->{key1}, 1); |
|
374 |
is($row->{key2}, 2); |
|
375 |
is($row->{key3}, 3); |
|
376 | ||
377 |
test 'column separator is default .'; |
|
test cleanup
|
378 |
$dbi = MyDBI7->connect; |
cleanup test
|
379 |
eval { $dbi->execute('drop table table1') }; |
380 |
eval { $dbi->execute('drop table table2') }; |
|
cleanup test
|
381 |
$dbi->execute($create_table1); |
test cleanup
|
382 |
$dbi->execute($create_table2); |
cleanup test
|
383 |
$dbi->setup_model; |
384 |
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2}); |
|
385 |
$dbi->insert(table => 'table2', param => {key1 => 1, key3 => 3}); |
|
386 |
$model = $dbi->model('table1'); |
|
387 |
$result = $model->select( |
|
388 |
column => [$model->column('table2')], |
|
389 |
where => {'table1.key1' => 1} |
|
390 |
); |
|
391 |
is_deeply($result->one, |
|
392 |
{'table2.key1' => 1, 'table2.key3' => 3}); |
|
393 | ||
394 |
$result = $model->select( |
|
395 |
column => [$model->column('table2' => [qw/key1 key3/])], |
|
396 |
where => {'table1.key1' => 1} |
|
397 |
); |
|
398 |
is_deeply($result->one, |
|
399 |
{'table2.key1' => 1, 'table2.key3' => 3}); |
|
400 | ||
401 | ||
cleanup test
|
402 | |
403 |
test 'separator'; |
|
test cleanup
|
404 |
$dbi = DBIx::Custom->connect; |
cleanup test
|
405 |
eval { $dbi->execute('drop table table1') }; |
406 |
eval { $dbi->execute('drop table table2') }; |
|
407 |
$dbi->execute($create_table1); |
|
test cleanup
|
408 |
$dbi->execute($create_table2); |
cleanup test
|
409 | |
410 |
$dbi->create_model( |
|
411 |
table => 'table1', |
|
412 |
join => [ |
|
413 |
'left outer join table2 on table1.key1 = table2.key1' |
|
414 |
], |
|
415 |
primary_key => ['key1'], |
|
cleanup test
|
416 |
); |
cleanup test
|
417 |
$model2 = $dbi->create_model( |
418 |
table => 'table2', |
|
419 |
); |
|
420 |
$dbi->setup_model; |
|
421 |
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2}); |
|
422 |
$dbi->insert(table => 'table2', param => {key1 => 1, key3 => 3}); |
|
423 |
$model = $dbi->model('table1'); |
|
424 |
$result = $model->select( |
|
425 |
column => [ |
|
426 |
$model->mycolumn, |
|
427 |
{table2 => [qw/key1 key3/]} |
|
428 |
], |
|
429 |
where => {'table1.key1' => 1} |
|
430 |
); |
|
431 |
is_deeply($result->one, |
|
432 |
{key1 => 1, key2 => 2, 'table2.key1' => 1, 'table2.key3' => 3}); |
|
433 |
is_deeply($model2->select->one, {key1 => 1, key3 => 3}); |
|
cleanup test
|
434 | |
cleanup test
|
435 |
$dbi->separator('__'); |
436 |
$model = $dbi->model('table1'); |
|
437 |
$result = $model->select( |
|
438 |
column => [ |
|
439 |
$model->mycolumn, |
|
440 |
{table2 => [qw/key1 key3/]} |
|
441 |
], |
|
442 |
where => {'table1.key1' => 1} |
|
443 |
); |
|
444 |
is_deeply($result->one, |
|
445 |
{key1 => 1, key2 => 2, 'table2__key1' => 1, 'table2__key3' => 3}); |
|
446 |
is_deeply($model2->select->one, {key1 => 1, key3 => 3}); |
|
cleanup test
|
447 | |
cleanup test
|
448 |
$dbi->separator('-'); |
449 |
$model = $dbi->model('table1'); |
|
450 |
$result = $model->select( |
|
451 |
column => [ |
|
452 |
$model->mycolumn, |
|
453 |
{table2 => [qw/key1 key3/]} |
|
454 |
], |
|
455 |
where => {'table1.key1' => 1} |
|
456 |
); |
|
457 |
is_deeply($result->one, |
|
458 |
{key1 => 1, key2 => 2, 'table2-key1' => 1, 'table2-key3' => 3}); |
|
459 |
is_deeply($model2->select->one, {key1 => 1, key3 => 3}); |
|
cleanup test
|
460 | |
cleanup test
|
461 | |
462 |
test 'filter_off'; |
|
test cleanup
|
463 |
$dbi = DBIx::Custom->connect; |
cleanup test
|
464 |
eval { $dbi->execute('drop table table1') }; |
465 |
eval { $dbi->execute('drop table table2') }; |
|
466 |
$dbi->execute($create_table1); |
|
test cleanup
|
467 |
$dbi->execute($create_table2); |
cleanup test
|
468 | |
469 |
$dbi->create_model( |
|
470 |
table => 'table1', |
|
471 |
join => [ |
|
472 |
'left outer join table2 on table1.key1 = table2.key1' |
|
473 |
], |
|
474 |
primary_key => ['key1'], |
|
cleanup test
|
475 |
); |
cleanup test
|
476 |
$dbi->setup_model; |
477 |
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2}); |
|
478 |
$model = $dbi->model('table1'); |
|
479 |
$result = $model->select(column => 'key1'); |
|
480 |
$result->filter(key1 => sub { $_[0] * 2 }); |
|
481 |
is_deeply($result->one, {key1 => 2}); |
|
cleanup test
|
482 | |
cleanup test
|
483 |
test 'available_datetype'; |
test cleanup
|
484 |
$dbi = DBIx::Custom->connect; |
cleanup test
|
485 |
ok($dbi->can('available_datatype')); |
486 | ||
cleanup test
|
487 | |
cleanup test
|
488 |
test 'select prefix option'; |
test cleanup
|
489 |
$dbi = DBIx::Custom->connect; |
cleanup test
|
490 |
eval { $dbi->execute('drop table table1') }; |
491 |
$dbi->execute($create_table1); |
|
492 |
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2}); |
|
493 |
$rows = $dbi->select(prefix => 'key1,', column => 'key2', table => 'table1')->all; |
|
494 |
is_deeply($rows, [{key1 => 1, key2 => 2}], "table"); |
|
495 | ||
496 |
test 'map_param'; |
|
497 |
$dbi = DBIx::Custom->connect; |
|
498 |
$param = $dbi->map_param( |
|
499 |
{id => 1, author => 'Ken', price => 1900}, |
|
500 |
id => 'book.id', |
|
501 |
author => ['book.author', sub { '%' . $_[0] . '%' }], |
|
502 |
price => ['book.price', {if => sub { $_[0] eq 1900 }}] |
|
cleanup test
|
503 |
); |
cleanup test
|
504 |
is_deeply($param, {'book.id' => 1, 'book.author' => '%Ken%', |
505 |
'book.price' => 1900}); |
|
506 | ||
507 |
$param = $dbi->map_param( |
|
508 |
{id => 0, author => 0, price => 0}, |
|
509 |
id => 'book.id', |
|
510 |
author => ['book.author', sub { '%' . $_[0] . '%' }], |
|
511 |
price => ['book.price', sub { '%' . $_[0] . '%' }, |
|
512 |
{if => sub { $_[0] eq 0 }}] |
|
cleanup test
|
513 |
); |
cleanup test
|
514 |
is_deeply($param, {'book.id' => 0, 'book.author' => '%0%', 'book.price' => '%0%'}); |
cleanup test
|
515 | |
cleanup test
|
516 |
$param = $dbi->map_param( |
517 |
{id => '', author => '', price => ''}, |
|
518 |
id => 'book.id', |
|
519 |
author => ['book.author', sub { '%' . $_[0] . '%' }], |
|
520 |
price => ['book.price', sub { '%' . $_[0] . '%' }, |
|
521 |
{if => sub { $_[0] eq 1 }}] |
|
cleanup test
|
522 |
); |
cleanup test
|
523 |
is_deeply($param, {}); |
524 | ||
525 |
$param = $dbi->map_param( |
|
526 |
{id => undef, author => undef, price => undef}, |
|
527 |
id => 'book.id', |
|
528 |
price => ['book.price', {if => 'exists'}] |
|
cleanup test
|
529 |
); |
cleanup test
|
530 |
is_deeply($param, {'book.price' => undef}); |
531 | ||
532 |
$param = $dbi->map_param( |
|
533 |
{price => 'a'}, |
|
534 |
id => ['book.id', {if => 'exists'}], |
|
535 |
price => ['book.price', sub { '%' . $_[0] }, {if => 'exists'}] |
|
536 |
); |
|
537 |
is_deeply($param, {'book.price' => '%a'}); |
|
cleanup test
|
538 | |
cleanup test
|
539 | |
540 |
test 'table_alias'; |
|
test cleanup
|
541 |
$dbi = DBIx::Custom->connect; |
cleanup test
|
542 |
eval { $dbi->execute('drop table table1') }; |
543 |
$dbi->execute("create table table1 (key1 Date, key2 datetime)"); |
|
cleanup test
|
544 |
$dbi->type_rule( |
545 |
into1 => { |
|
cleanup test
|
546 |
date => sub { uc $_[0] } |
cleanup test
|
547 |
} |
548 |
); |
|
cleanup test
|
549 |
$dbi->execute("insert into table1 (key1) values (:table2.key1)", {'table2.key1' => 'a'}, |
550 |
table_alias => {table2 => 'table1'}); |
|
cleanup test
|
551 |
$result = $dbi->select(table => 'table1'); |
cleanup test
|
552 |
is($result->one->{key1}, 'A'); |
cleanup test
|
553 | |
554 | ||
cleanup test
|
555 |
test 'order'; |
test cleanup
|
556 |
$dbi = DBIx::Custom->connect; |
cleanup test
|
557 |
eval { $dbi->execute('drop table table1') }; |
558 |
$dbi->execute("create table table1 (key1, key2)"); |
|
559 |
$dbi->insert({key1 => 1, key2 => 1}, table => 'table1'); |
|
560 |
$dbi->insert({key1 => 1, key2 => 3}, table => 'table1'); |
|
561 |
$dbi->insert({key1 => 2, key2 => 2}, table => 'table1'); |
|
562 |
$dbi->insert({key1 => 2, key2 => 4}, table => 'table1'); |
|
563 |
my $order = $dbi->order; |
|
564 |
$order->prepend('key1', 'key2 desc'); |
|
565 |
$result = $dbi->select(table => 'table1', append => "$order"); |
|
566 |
is_deeply($result->all, [{key1 => 1, key2 => 3}, {key1 => 1, key2 => 1}, |
|
567 |
{key1 => 2, key2 => 4}, {key1 => 2, key2 => 2}]); |
|
568 |
$order->prepend('key1 desc'); |
|
569 |
$result = $dbi->select(table => 'table1', append => "$order"); |
|
570 |
is_deeply($result->all, [{key1 => 2, key2 => 4}, {key1 => 2, key2 => 2}, |
|
571 |
{key1 => 1, key2 => 3}, {key1 => 1, key2 => 1}]); |
|
cleanup test
|
572 | |
cleanup test
|
573 |
$order = $dbi->order; |
574 |
$order->prepend(['table1-key1'], [qw/table1-key2 desc/]); |
|
575 |
$result = $dbi->select(table => 'table1', |
|
576 |
column => [[key1 => 'table1-key1'], [key2 => 'table1-key2']], |
|
577 |
append => "$order"); |
|
578 |
is_deeply($result->all, [{'table1-key1' => 1, 'table1-key2' => 3}, |
|
579 |
{'table1-key1' => 1, 'table1-key2' => 1}, |
|
580 |
{'table1-key1' => 2, 'table1-key2' => 4}, |
|
581 |
{'table1-key1' => 2, 'table1-key2' => 2}]); |
|
cleanup test
|
582 | |
cleanup test
|
583 |
test 'tag_parse'; |
test cleanup
|
584 |
$dbi = DBIx::Custom->connect; |
cleanup test
|
585 |
$dbi->tag_parse(0); |
586 |
eval { $dbi->execute('drop table table1') }; |
|
587 |
$dbi->execute("create table table1 (key1, key2)"); |
|
588 |
$dbi->insert({key1 => 1, key2 => 1}, table => 'table1'); |
|
589 |
eval {$dbi->execute("select * from table1 where {= key1}", {key1 => 1})}; |
|
590 |
ok($@); |
|
cleanup test
|
591 | |
cleanup test
|
592 |
test 'last_sql'; |
test cleanup
|
593 |
$dbi = DBIx::Custom->connect; |
cleanup test
|
594 |
eval { $dbi->execute('drop table table1') }; |
595 |
$dbi->execute("create table table1 (key1, key2)"); |
|
596 |
$dbi->execute('select * from table1'); |
|
597 |
is($dbi->last_sql, 'select * from table1;'); |
|
cleanup test
|
598 | |
cleanup test
|
599 |
eval{$dbi->execute("aaa")}; |
600 |
is($dbi->last_sql, 'aaa;'); |
|
cleanup test
|
601 | |
cleanup test
|
602 |
test 'DBIx::Custom header'; |
test cleanup
|
603 |
$dbi = DBIx::Custom->connect; |
cleanup test
|
604 |
eval { $dbi->execute('drop table table1') }; |
605 |
$dbi->execute("create table table1 (key1, key2)"); |
|
606 |
$result = $dbi->execute('select key1 as h1, key2 as h2 from table1'); |
|
607 |
is_deeply($result->header, [qw/h1 h2/]); |
|
cleanup test
|
608 | |
cleanup test
|
609 |
test 'Named placeholder :name(operater) syntax'; |
610 |
$dbi->execute('drop table table1'); |
|
611 |
$dbi->execute($create_table1_2); |
|
612 |
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5}); |
|
613 |
$dbi->insert(table => 'table1', param => {key1 => 6, key2 => 7, key3 => 8, key4 => 9, key5 => 10}); |
|
cleanup test
|
614 | |
cleanup test
|
615 |
$source = "select * from table1 where :key1{=} and :key2{=}"; |
616 |
$result = $dbi->execute($source, param => {key1 => 1, key2 => 2}); |
|
617 |
$rows = $result->all; |
|
618 |
is_deeply($rows, [{key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5}]); |
|
cleanup test
|
619 | |
cleanup test
|
620 |
$source = "select * from table1 where :key1{ = } and :key2{=}"; |
621 |
$result = $dbi->execute($source, param => {key1 => 1, key2 => 2}); |
|
622 |
$rows = $result->all; |
|
623 |
is_deeply($rows, [{key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5}]); |
|
cleanup test
|
624 | |
cleanup test
|
625 |
$source = "select * from table1 where :key1{<} and :key2{=}"; |
626 |
$result = $dbi->execute($source, param => {key1 => 5, key2 => 2}); |
|
627 |
$rows = $result->all; |
|
628 |
is_deeply($rows, [{key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5}]); |
|
cleanup test
|
629 | |
cleanup test
|
630 |
$source = "select * from table1 where :table1.key1{=} and :table1.key2{=}"; |
631 |
$result = $dbi->execute( |
|
632 |
$source, |
|
633 |
param => {'table1.key1' => 1, 'table1.key2' => 1}, |
|
634 |
filter => {'table1.key2' => sub { $_[0] * 2 }} |
|
cleanup test
|
635 |
); |
cleanup test
|
636 |
$rows = $result->all; |
637 |
is_deeply($rows, [{key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5}]); |
|
cleanup test
|
638 | |
cleanup test
|
639 |
test 'high perfomance way'; |
640 |
$dbi->execute('drop table table1'); |
|
641 |
$dbi->execute("create table table1 (ab, bc, ik, hi, ui, pq, dc);"); |
|
642 |
$rows = [ |
|
643 |
{ab => 1, bc => 2, ik => 3, hi => 4, ui => 5, pq => 6, dc => 7}, |
|
644 |
{ab => 1, bc => 2, ik => 3, hi => 4, ui => 5, pq => 6, dc => 8}, |
|
645 |
]; |
|
646 |
{ |
|
647 |
my $query; |
|
648 |
foreach my $row (@$rows) { |
|
649 |
$query ||= $dbi->insert($row, table => 'table1', query => 1); |
|
650 |
$dbi->execute($query, $row, filter => {ab => sub { $_[0] * 2 }}); |
|
cleanup test
|
651 |
} |
cleanup test
|
652 |
is_deeply($dbi->select(table => 'table1')->all, |
653 |
[ |
|
654 |
{ab => 2, bc => 2, ik => 3, hi => 4, ui => 5, pq => 6, dc => 7}, |
|
655 |
{ab => 2, bc => 2, ik => 3, hi => 4, ui => 5, pq => 6, dc => 8}, |
|
656 |
] |
|
657 |
); |
|
658 |
} |
|
659 | ||
660 |
$dbi->execute('drop table table1'); |
|
661 |
$dbi->execute("create table table1 (ab, bc, ik, hi, ui, pq, dc);"); |
|
662 |
$rows = [ |
|
663 |
{ab => 1, bc => 2, ik => 3, hi => 4, ui => 5, pq => 6, dc => 7}, |
|
664 |
{ab => 1, bc => 2, ik => 3, hi => 4, ui => 5, pq => 6, dc => 8}, |
|
665 |
]; |
|
666 |
{ |
|
667 |
my $query; |
|
668 |
my $sth; |
|
669 |
foreach my $row (@$rows) { |
|
670 |
$query ||= $dbi->insert($row, table => 'table1', query => 1); |
|
671 |
$sth ||= $query->sth; |
|
672 |
$sth->execute(map { $row->{$_} } sort keys %$row); |
|
cleanup test
|
673 |
} |
cleanup test
|
674 |
is_deeply($dbi->select(table => 'table1')->all, |
675 |
[ |
|
676 |
{ab => 1, bc => 2, ik => 3, hi => 4, ui => 5, pq => 6, dc => 7}, |
|
677 |
{ab => 1, bc => 2, ik => 3, hi => 4, ui => 5, pq => 6, dc => 8}, |
|
678 |
] |
|
679 |
); |
|
680 |
} |
|
cleanup test
|
681 | |
cleanup test
|
682 |
test 'result'; |
test cleanup
|
683 |
$dbi = DBIx::Custom->connect; |
cleanup test
|
684 |
eval { $dbi->execute('drop table table1') }; |
685 |
$dbi->execute($create_table1); |
|
686 |
$dbi->insert({key1 => 1, key2 => 2}, table => 'table1'); |
|
687 |
$dbi->insert({key1 => 3, key2 => 4}, table => 'table1'); |
|
688 | ||
cleanup test
|
689 |
$result = $dbi->select(table => 'table1'); |
cleanup test
|
690 |
@rows = (); |
691 |
while (my $row = $result->fetch) { |
|
692 |
push @rows, [@$row]; |
|
693 |
} |
|
694 |
is_deeply(\@rows, [[1, 2], [3, 4]]); |
|
cleanup test
|
695 | |
696 |
$result = $dbi->select(table => 'table1'); |
|
cleanup test
|
697 |
@rows = (); |
698 |
while (my $row = $result->fetch_hash) { |
|
699 |
push @rows, {%$row}; |
|
700 |
} |
|
701 |
is_deeply(\@rows, [{key1 => 1, key2 => 2}, {key1 => 3, key2 => 4}]); |
|
cleanup test
|
702 | |
703 |
$result = $dbi->select(table => 'table1'); |
|
cleanup test
|
704 |
$row = $result->fetch_first; |
705 |
is_deeply($row, [1, 2], "row"); |
|
706 |
$row = $result->fetch; |
|
707 |
ok(!$row, "finished"); |
|
708 | ||
cleanup test
|
709 |
$result = $dbi->select(table => 'table1'); |
cleanup test
|
710 |
$row = $result->fetch_hash_first; |
711 |
is_deeply($row, {key1 => 1, key2 => 2}, "row"); |
|
712 |
$row = $result->fetch_hash; |
|
713 |
ok(!$row, "finished"); |
|
714 | ||
715 |
$dbi->execute('create table table2 (key1, key2);'); |
|
716 |
$result = $dbi->select(table => 'table2'); |
|
717 |
$row = $result->fetch_hash_first; |
|
718 |
ok(!$row, "no row fetch"); |
|
cleanup test
|
719 | |
test cleanup
|
720 |
$dbi = DBIx::Custom->connect; |
cleanup test
|
721 |
eval { $dbi->execute('drop table table1') }; |
722 |
$dbi->execute($create_table1); |
|
723 |
$dbi->insert({key1 => 1, key2 => 2}, table => 'table1'); |
|
724 |
$dbi->insert({key1 => 3, key2 => 4}, table => 'table1'); |
|
725 |
$dbi->insert({key1 => 5, key2 => 6}, table => 'table1'); |
|
726 |
$dbi->insert({key1 => 7, key2 => 8}, table => 'table1'); |
|
727 |
$dbi->insert({key1 => 9, key2 => 10}, table => 'table1'); |
|
cleanup test
|
728 |
$result = $dbi->select(table => 'table1'); |
cleanup test
|
729 |
$rows = $result->fetch_multi(2); |
730 |
is_deeply($rows, [[1, 2], |
|
731 |
[3, 4]], "fetch_multi first"); |
|
732 |
$rows = $result->fetch_multi(2); |
|
733 |
is_deeply($rows, [[5, 6], |
|
734 |
[7, 8]], "fetch_multi secound"); |
|
735 |
$rows = $result->fetch_multi(2); |
|
736 |
is_deeply($rows, [[9, 10]], "fetch_multi third"); |
|
737 |
$rows = $result->fetch_multi(2); |
|
738 |
ok(!$rows); |
|
739 | ||
cleanup test
|
740 |
$result = $dbi->select(table => 'table1'); |
cleanup test
|
741 |
eval {$result->fetch_multi}; |
742 |
like($@, qr/Row count must be specified/, "Not specified row count"); |
|
cleanup test
|
743 | |
744 |
$result = $dbi->select(table => 'table1'); |
|
cleanup test
|
745 |
$rows = $result->fetch_hash_multi(2); |
746 |
is_deeply($rows, [{key1 => 1, key2 => 2}, |
|
747 |
{key1 => 3, key2 => 4}], "fetch_multi first"); |
|
748 |
$rows = $result->fetch_hash_multi(2); |
|
749 |
is_deeply($rows, [{key1 => 5, key2 => 6}, |
|
750 |
{key1 => 7, key2 => 8}], "fetch_multi secound"); |
|
751 |
$rows = $result->fetch_hash_multi(2); |
|
752 |
is_deeply($rows, [{key1 => 9, key2 => 10}], "fetch_multi third"); |
|
753 |
$rows = $result->fetch_hash_multi(2); |
|
754 |
ok(!$rows); |
|
755 | ||
cleanup test
|
756 |
$result = $dbi->select(table => 'table1'); |
cleanup test
|
757 |
eval {$result->fetch_hash_multi}; |
758 |
like($@, qr/Row count must be specified/, "Not specified row count"); |
|
cleanup test
|
759 | |
test cleanup
|
760 |
$dbi = DBIx::Custom->connect; |
cleanup test
|
761 |
eval { $dbi->execute('drop table table1') }; |
cleanup test
|
762 |
$dbi->execute($create_table1); |
cleanup test
|
763 |
$dbi->insert({key1 => 1, key2 => 2}, table => 'table1'); |
764 |
$dbi->insert({key1 => 3, key2 => 4}, table => 'table1'); |
|
cleanup test
|
765 | |
cleanup test
|
766 |
test 'fetch_all'; |
767 |
$result = $dbi->select(table => 'table1'); |
|
768 |
$rows = $result->fetch_all; |
|
769 |
is_deeply($rows, [[1, 2], [3, 4]]); |
|
cleanup test
|
770 | |
cleanup test
|
771 |
$result = $dbi->select(table => 'table1'); |
772 |
$rows = $result->fetch_hash_all; |
|
773 |
is_deeply($rows, [{key1 => 1, key2 => 2}, {key1 => 3, key2 => 4}]); |
|
cleanup test
|
774 | |
cleanup test
|
775 |
$result = $dbi->select(table => 'table1'); |
776 |
$result->dbi->filters({three_times => sub { $_[0] * 3}}); |
|
777 |
$result->filter({key1 => 'three_times'}); |
|
cleanup test
|
778 | |
cleanup test
|
779 |
$rows = $result->fetch_all; |
780 |
is_deeply($rows, [[3, 2], [9, 4]], "array"); |
|
cleanup test
|
781 | |
cleanup test
|
782 |
$result = $dbi->select(table => 'table1'); |
783 |
$result->dbi->filters({three_times => sub { $_[0] * 3}}); |
|
784 |
$result->filter({key1 => 'three_times'}); |
|
785 |
$rows = $result->fetch_hash_all; |
|
786 |
is_deeply($rows, [{key1 => 3, key2 => 2}, {key1 => 9, key2 => 4}], "hash"); |
|
787 | ||
788 |
test "query_builder"; |
|
789 |
$datas = [ |
|
790 |
# Basic tests |
|
791 |
{ name => 'placeholder basic', |
|
792 |
source => "a {? k1} b {= k2} {<> k3} {> k4} {< k5} {>= k6} {<= k7} {like k8}", , |
|
793 |
sql_expected => "a ? b k2 = ? k3 <> ? k4 > ? k5 < ? k6 >= ? k7 <= ? k8 like ?;", |
|
794 |
columns_expected => [qw/k1 k2 k3 k4 k5 k6 k7 k8/] |
|
795 |
}, |
|
796 |
{ |
|
797 |
name => 'placeholder in', |
|
798 |
source => "{in k1 3};", |
|
799 |
sql_expected => "k1 in (?, ?, ?);", |
|
800 |
columns_expected => [qw/k1 k1 k1/] |
|
801 |
}, |
|
802 |
|
|
803 |
# Table name |
|
804 |
{ |
|
805 |
name => 'placeholder with table name', |
|
806 |
source => "{= a.k1} {= a.k2}", |
|
807 |
sql_expected => "a.k1 = ? a.k2 = ?;", |
|
808 |
columns_expected => [qw/a.k1 a.k2/] |
|
809 |
}, |
|
810 |
{ |
|
811 |
name => 'placeholder in with table name', |
|
812 |
source => "{in a.k1 2} {in b.k2 2}", |
|
813 |
sql_expected => "a.k1 in (?, ?) b.k2 in (?, ?);", |
|
814 |
columns_expected => [qw/a.k1 a.k1 b.k2 b.k2/] |
|
815 |
}, |
|
816 |
{ |
|
817 |
name => 'not contain tag', |
|
818 |
source => "aaa", |
|
819 |
sql_expected => "aaa;", |
|
820 |
columns_expected => [], |
|
821 |
} |
|
822 |
]; |
|
823 | ||
824 |
for (my $i = 0; $i < @$datas; $i++) { |
|
825 |
my $data = $datas->[$i]; |
|
826 |
my $builder = DBIx::Custom->new->query_builder; |
|
827 |
my $query = $builder->build_query($data->{source}); |
|
828 |
is($query->{sql}, $data->{sql_expected}, "$data->{name} : sql"); |
|
829 |
is_deeply($query->columns, $data->{columns_expected}, "$data->{name} : columns"); |
|
830 |
} |
|
831 | ||
832 |
$builder = DBIx::Custom->new->query_builder; |
|
833 |
$ret_val = $builder->register_tag( |
|
834 |
p => sub { |
|
835 |
my @args = @_; |
|
836 |
|
|
837 |
my $expand = "? $args[0] $args[1]"; |
|
838 |
my $columns = [2]; |
|
839 |
return [$expand, $columns]; |
|
840 |
} |
|
cleanup test
|
841 |
); |
842 | ||
cleanup test
|
843 |
$query = $builder->build_query("{p a b}"); |
844 |
is($query->{sql}, "? a b;", "register_tag sql"); |
|
845 |
is_deeply($query->{columns}, [2], "register_tag columns"); |
|
846 |
isa_ok($ret_val, 'DBIx::Custom::QueryBuilder'); |
|
cleanup test
|
847 | |
cleanup test
|
848 |
$builder = DBIx::Custom->new->query_builder; |
cleanup test
|
849 | |
cleanup test
|
850 |
eval{$builder->build_query('{? }')}; |
851 |
like($@, qr/\QColumn name must be specified in tag "{? }"/, "? not arguments"); |
|
cleanup test
|
852 | |
cleanup test
|
853 |
eval{$builder->build_query("{a }")}; |
854 |
like($@, qr/\QTag "a" is not registered/, "tag not exist"); |
|
cleanup test
|
855 | |
cleanup test
|
856 |
$builder->register_tag({ |
857 |
q => 'string' |
|
858 |
}); |
|
cleanup test
|
859 | |
cleanup test
|
860 |
eval{$builder->build_query("{q}", {})}; |
861 |
like($@, qr/Tag "q" must be sub reference/, "tag not code ref"); |
|
cleanup test
|
862 | |
cleanup test
|
863 |
$builder->register_tag({ |
864 |
r => sub {} |
|
865 |
}); |
|
cleanup test
|
866 | |
cleanup test
|
867 |
eval{$builder->build_query("{r}")}; |
868 |
like($@, qr/\QTag "r" must return [STRING, ARRAY_REFERENCE]/, "tag return noting"); |
|
869 | ||
870 |
$builder->register_tag({ |
|
871 |
s => sub { return ["a", ""]} |
|
872 |
}); |
|
873 | ||
874 |
eval{$builder->build_query("{s}")}; |
|
875 |
like($@, qr/\QTag "s" must return [STRING, ARRAY_REFERENCE]/, "tag return not array columns"); |
|
876 | ||
877 |
$builder->register_tag( |
|
878 |
t => sub {return ["a", []]} |
|
cleanup test
|
879 |
); |
880 | ||
cleanup test
|
881 | |
882 |
test 'General error case'; |
|
883 |
$builder = DBIx::Custom->new->query_builder; |
|
884 |
$builder->register_tag( |
|
885 |
a => sub { |
|
886 |
return ["? ? ?", ['']]; |
|
887 |
} |
|
cleanup test
|
888 |
); |
cleanup test
|
889 |
eval{$builder->build_query("{a}")}; |
890 |
like($@, qr/\QPlaceholder count/, "placeholder count is invalid"); |
|
cleanup test
|
891 | |
cleanup test
|
892 | |
893 |
test 'Default tag Error case'; |
|
894 |
eval{$builder->build_query("{= }")}; |
|
895 |
like($@, qr/Column name must be specified in tag "{= }"/, "basic '=' : key not exist"); |
|
896 | ||
897 |
eval{$builder->build_query("{in }")}; |
|
898 |
like($@, qr/Column name and count of values must be specified in tag "{in }"/, "in : key not exist"); |
|
899 | ||
900 |
eval{$builder->build_query("{in a}")}; |
|
901 |
like($@, qr/\QColumn name and count of values must be specified in tag "{in }"/, |
|
902 |
"in : key not exist"); |
|
903 | ||
904 |
eval{$builder->build_query("{in a r}")}; |
|
905 |
like($@, qr/\QColumn name and count of values must be specified in tag "{in }"/, |
|
906 |
"in : key not exist"); |
|
907 | ||
908 |
test 'variouse source'; |
|
909 |
$source = "a {= b} c \\{ \\} {= \\{} {= \\}} d;"; |
|
910 |
$query = $builder->build_query($source); |
|
911 |
is($query->sql, 'a b = ? c { } { = ? } = ? d;', "basic : 1"); |
|
912 | ||
913 |
$source = "abc;"; |
|
914 |
$query = $builder->build_query($source); |
|
915 |
is($query->sql, 'abc;', "basic : 2"); |
|
916 | ||
917 |
$source = "{= a}"; |
|
918 |
$query = $builder->build_query($source); |
|
919 |
is($query->sql, 'a = ?;', "only tag"); |
|
920 | ||
921 |
$source = "000;"; |
|
922 |
$query = $builder->build_query($source); |
|
923 |
is($query->sql, '000;', "contain 0 value"); |
|
924 | ||
925 |
$source = "a {= b} }"; |
|
926 |
eval{$builder->build_query($source)}; |
|
927 |
like($@, qr/unexpected "}"/, "error : 1"); |
|
928 | ||
929 |
$source = "a {= {}"; |
|
930 |
eval{$builder->build_query($source)}; |
|
931 |
like($@, qr/unexpected "{"/, "error : 2"); |
|
932 | ||
test cleanup
|
933 | |
934 | ||
935 | ||
936 | ||
937 | ||
938 | ||
939 | ||
940 | ||
941 | ||
942 | ||
943 | ||
944 | ||
945 | ||
946 | ||
947 | ||
948 | ||
949 | ||
950 | ||
951 | ||
952 | ||
953 | ||
954 | ||
955 | ||
956 | ||
957 | ||
958 | ||
959 |
### a little complex test |
|
cleanup test
|
960 |
test 'select() wrap option'; |
961 |
$dbi = DBIx::Custom->connect; |
|
962 |
eval { $dbi->execute('drop table table1') }; |
|
963 |
$dbi->execute($create_table1); |
|
964 |
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2}); |
|
965 |
$dbi->insert(table => 'table1', param => {key1 => 2, key2 => 3}); |
|
966 |
$rows = $dbi->select( |
|
967 |
table => 'table1', |
|
968 |
column => 'key1', |
|
969 |
wrap => ['select * from (', ') as t where key1 = 1'] |
|
970 |
)->all; |
|
971 |
is_deeply($rows, [{key1 => 1}]); |
|
972 | ||
973 |
eval { |
|
974 |
$dbi->select( |
|
975 |
table => 'table1', |
|
976 |
column => 'key1', |
|
977 |
wrap => 'select * from (' |
|
978 |
) |
|
979 |
}; |
|
980 |
like($@, qr/array/); |
|
981 | ||
cleanup test
|
982 |
test 'dbi method from model'; |
983 |
$dbi = MyDBI9->connect; |
|
984 |
eval { $dbi->execute('drop table table1') }; |
|
985 |
$dbi->execute($create_table1); |
|
986 |
$dbi->setup_model; |
|
987 |
$model = $dbi->model('table1'); |
|
988 |
eval{$model->execute('select * from table1')}; |
|
989 |
ok(!$@); |
|
990 | ||
991 |
test 'column table option'; |
|
992 |
$dbi = MyDBI9->connect; |
|
993 |
eval { $dbi->execute('drop table table1') }; |
|
994 |
$dbi->execute($create_table1); |
|
995 |
eval { $dbi->execute('drop table table2') }; |
|
996 |
$dbi->execute($create_table2); |
|
997 |
$dbi->setup_model; |
|
998 |
$dbi->execute('insert into table1 (key1, key2) values (1, 2);'); |
|
999 |
$dbi->execute('insert into table2 (key1, key3) values (1, 4);'); |
|
1000 |
$model = $dbi->model('table1'); |
|
1001 |
$result = $model->select( |
|
1002 |
column => [ |
|
1003 |
$model->column('table2', {alias => 'table2_alias'}) |
|
1004 |
], |
|
1005 |
where => {'table2_alias.key3' => 4} |
|
1006 |
); |
|
1007 |
is_deeply($result->one, |
|
1008 |
{'table2_alias.key1' => 1, 'table2_alias.key3' => 4}); |
|
1009 | ||
1010 |
$dbi->separator('__'); |
|
1011 |
$result = $model->select( |
|
1012 |
column => [ |
|
1013 |
$model->column('table2', {alias => 'table2_alias'}) |
|
1014 |
], |
|
1015 |
where => {'table2_alias.key3' => 4} |
|
1016 |
); |
|
1017 |
is_deeply($result->one, |
|
1018 |
{'table2_alias__key1' => 1, 'table2_alias__key3' => 4}); |
|
1019 | ||
1020 |
$dbi->separator('-'); |
|
1021 |
$result = $model->select( |
|
1022 |
column => [ |
|
1023 |
$model->column('table2', {alias => 'table2_alias'}) |
|
1024 |
], |
|
1025 |
where => {'table2_alias.key3' => 4} |
|
1026 |
); |
|
1027 |
is_deeply($result->one, |
|
1028 |
{'table2_alias-key1' => 1, 'table2_alias-key3' => 4}); |
|
1029 | ||
1030 |
test 'create_model'; |
|
1031 |
$dbi = DBIx::Custom->connect; |
|
1032 |
eval { $dbi->execute('drop table table1') }; |
|
1033 |
eval { $dbi->execute('drop table table2') }; |
|
1034 |
$dbi->execute($create_table1); |
|
1035 |
$dbi->execute($create_table2); |
|
1036 | ||
1037 |
$dbi->create_model( |
|
1038 |
table => 'table1', |
|
1039 |
join => [ |
|
1040 |
'left outer join table2 on table1.key1 = table2.key1' |
|
1041 |
], |
|
1042 |
primary_key => ['key1'] |
|
1043 |
); |
|
1044 |
$model2 = $dbi->create_model( |
|
1045 |
table => 'table2' |
|
1046 |
); |
|
1047 |
$dbi->create_model( |
|
1048 |
table => 'table3', |
|
1049 |
filter => [ |
|
1050 |
key1 => {in => sub { uc $_[0] }} |
|
1051 |
] |
|
1052 |
); |
|
1053 |
$dbi->setup_model; |
|
1054 |
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2}); |
|
1055 |
$dbi->insert(table => 'table2', param => {key1 => 1, key3 => 3}); |
|
1056 |
$model = $dbi->model('table1'); |
|
1057 |
$result = $model->select( |
|
1058 |
column => [$model->mycolumn, $model->column('table2')], |
|
1059 |
where => {'table1.key1' => 1} |
|
1060 |
); |
|
1061 |
is_deeply($result->one, |
|
1062 |
{key1 => 1, key2 => 2, 'table2.key1' => 1, 'table2.key3' => 3}); |
|
1063 |
is_deeply($model2->select->one, {key1 => 1, key3 => 3}); |
|
1064 | ||
1065 |
test 'model method'; |
|
1066 |
$dbi = DBIx::Custom->connect; |
|
1067 |
eval { $dbi->execute('drop table table2') }; |
|
1068 |
$dbi->execute($create_table2); |
|
1069 |
$dbi->insert(table => 'table2', param => {key1 => 1, key3 => 3}); |
|
1070 |
$model = $dbi->create_model( |
|
1071 |
table => 'table2' |
|
1072 |
); |
|
1073 |
$model->method(foo => sub { shift->select(@_) }); |
|
1074 |
is_deeply($model->foo->one, {key1 => 1, key3 => 3}); |
|
1075 | ||
cleanup test
|
1076 |
test 'join'; |
1077 |
$dbi = DBIx::Custom->connect; |
|
1078 |
eval { $dbi->execute('drop table table1') }; |
|
1079 |
$dbi->execute($create_table1); |
|
1080 |
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2}); |
|
1081 |
$dbi->insert(table => 'table1', param => {key1 => 3, key2 => 4}); |
|
1082 |
$dbi->execute($create_table2); |
|
1083 |
$dbi->insert(table => 'table2', param => {key1 => 1, key3 => 5}); |
|
1084 |
$dbi->execute('create table table3 (key3 int, key4 int);'); |
|
1085 |
$dbi->insert(table => 'table3', param => {key3 => 5, key4 => 4}); |
|
1086 |
$rows = $dbi->select( |
|
1087 |
table => 'table1', |
|
1088 |
column => 'table1.key1 as table1_key1, table2.key1 as table2_key1, key2, key3', |
|
1089 |
where => {'table1.key2' => 2}, |
|
1090 |
join => ['left outer join table2 on table1.key1 = table2.key1'] |
|
1091 |
)->all; |
|
1092 |
is_deeply($rows, [{table1_key1 => 1, table2_key1 => 1, key2 => 2, key3 => 5}]); |
|
1093 | ||
1094 |
$rows = $dbi->select( |
|
1095 |
table => 'table1', |
|
1096 |
where => {'key1' => 1}, |
|
1097 |
join => ['left outer join table2 on table1.key1 = table2.key1'] |
|
1098 |
)->all; |
|
1099 |
is_deeply($rows, [{key1 => 1, key2 => 2}]); |
|
1100 | ||
1101 |
eval { |
|
1102 |
$rows = $dbi->select( |
|
1103 |
table => 'table1', |
|
1104 |
column => 'table1.key1 as table1_key1, table2.key1 as table2_key1, key2, key3', |
|
1105 |
where => {'table1.key2' => 2}, |
|
1106 |
join => {'table1.key1' => 'table2.key1'} |
|
1107 |
); |
|
1108 |
}; |
|
1109 |
like ($@, qr/array/); |
|
1110 | ||
1111 |
$rows = $dbi->select( |
|
1112 |
table => 'table1', |
|
1113 |
where => {'key1' => 1}, |
|
1114 |
join => ['left outer join table2 on table1.key1 = table2.key1', |
|
1115 |
'left outer join table3 on table2.key3 = table3.key3'] |
|
1116 |
)->all; |
|
1117 |
is_deeply($rows, [{key1 => 1, key2 => 2}]); |
|
1118 | ||
1119 |
$rows = $dbi->select( |
|
1120 |
column => 'table3.key4 as table3__key4', |
|
1121 |
table => 'table1', |
|
1122 |
where => {'table1.key1' => 1}, |
|
1123 |
join => ['left outer join table2 on table1.key1 = table2.key1', |
|
1124 |
'left outer join table3 on table2.key3 = table3.key3'] |
|
1125 |
)->all; |
|
1126 |
is_deeply($rows, [{table3__key4 => 4}]); |
|
1127 | ||
1128 |
$rows = $dbi->select( |
|
1129 |
column => 'table1.key1 as table1__key1', |
|
1130 |
table => 'table1', |
|
1131 |
where => {'table3.key4' => 4}, |
|
1132 |
join => ['left outer join table2 on table1.key1 = table2.key1', |
|
1133 |
'left outer join table3 on table2.key3 = table3.key3'] |
|
1134 |
)->all; |
|
1135 |
is_deeply($rows, [{table1__key1 => 1}]); |
|
1136 | ||
1137 |
$dbi = DBIx::Custom->connect; |
|
1138 |
$dbi->quote('"'); |
|
1139 |
eval { $dbi->execute('drop table table1') }; |
|
1140 |
$dbi->execute($create_table1); |
|
1141 |
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2}); |
|
1142 |
$dbi->execute($create_table2); |
|
1143 |
$dbi->insert(table => 'table2', param => {key1 => 1, key3 => 5}); |
|
1144 |
$rows = $dbi->select( |
|
1145 |
table => 'table1', |
|
1146 |
column => '"table1"."key1" as "table1_key1", "table2"."key1" as "table2_key1", "key2", "key3"', |
|
1147 |
where => {'table1.key2' => 2}, |
|
1148 |
join => ['left outer join "table2" on "table1"."key1" = "table2"."key1"'], |
|
1149 |
)->all; |
|
1150 |
is_deeply($rows, [{table1_key1 => 1, table2_key1 => 1, key2 => 2, key3 => 5}], |
|
1151 |
'quote'); |
|
1152 | ||
1153 | ||
1154 |
$dbi = DBIx::Custom->connect; |
|
1155 |
eval { $dbi->execute('drop table table1') }; |
|
1156 |
$dbi->execute($create_table1); |
|
1157 |
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2}); |
|
1158 |
$sql = <<"EOS"; |
|
1159 |
left outer join ( |
|
1160 |
select * from table1 as t1 |
|
1161 |
where t1.key2 = ( |
|
1162 |
select max(t2.key2) from table1 as t2 |
|
1163 |
where t1.key1 = t2.key1 |
|
1164 |
) |
|
1165 |
) as latest_table1 on table1.key1 = latest_table1.key1 |
|
1166 |
EOS |
|
1167 |
$join = [$sql]; |
|
1168 |
$rows = $dbi->select( |
|
1169 |
table => 'table1', |
|
1170 |
column => 'latest_table1.key1 as latest_table1__key1', |
|
1171 |
join => $join |
|
1172 |
)->all; |
|
1173 |
is_deeply($rows, [{latest_table1__key1 => 1}]); |
|
1174 | ||
1175 |
$dbi = DBIx::Custom->connect; |
|
1176 |
eval { $dbi->execute('drop table table1') }; |
|
1177 |
eval { $dbi->execute('drop table table2') }; |
|
1178 |
$dbi->execute($create_table1); |
|
1179 |
$dbi->execute($create_table2); |
|
1180 |
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2}); |
|
1181 |
$dbi->insert(table => 'table2', param => {key1 => 1, key3 => 4}); |
|
1182 |
$dbi->insert(table => 'table2', param => {key1 => 1, key3 => 5}); |
|
1183 |
$result = $dbi->select( |
|
1184 |
table => 'table1', |
|
1185 |
join => [ |
|
1186 |
"left outer join table2 on table2.key2 = '4' and table1.key1 = table2.key1" |
|
1187 |
] |
|
1188 |
); |
|
1189 |
is_deeply($result->all, [{key1 => 1, key2 => 2}]); |
|
1190 |
$result = $dbi->select( |
|
1191 |
table => 'table1', |
|
1192 |
column => [{table2 => ['key3']}], |
|
1193 |
join => [ |
|
1194 |
"left outer join table2 on table2.key3 = '4' and table1.key1 = table2.key1" |
|
1195 |
] |
|
1196 |
); |
|
1197 |
is_deeply($result->all, [{'table2.key3' => 4}]); |
|
1198 |
$result = $dbi->select( |
|
1199 |
table => 'table1', |
|
1200 |
column => [{table2 => ['key3']}], |
|
1201 |
join => [ |
|
1202 |
"left outer join table2 on table1.key1 = table2.key1 and table2.key3 = '4'" |
|
1203 |
] |
|
1204 |
); |
|
1205 |
is_deeply($result->all, [{'table2.key3' => 4}]); |
|
1206 | ||
1207 |
$dbi = DBIx::Custom->connect; |
|
1208 |
eval { $dbi->execute('drop table table1') }; |
|
1209 |
eval { $dbi->execute('drop table table2') }; |
|
1210 |
$dbi->execute($create_table1); |
|
1211 |
$dbi->execute($create_table2); |
|
1212 |
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2}); |
|
1213 |
$dbi->insert(table => 'table2', param => {key1 => 1, key3 => 4}); |
|
1214 |
$dbi->insert(table => 'table2', param => {key1 => 1, key3 => 5}); |
|
1215 |
$result = $dbi->select( |
|
1216 |
table => 'table1', |
|
1217 |
column => [{table2 => ['key3']}], |
|
1218 |
join => [ |
|
1219 |
{ |
|
1220 |
clause => "left outer join table2 on table2.key3 = '4' and table1.key1 = table2.key1", |
|
1221 |
table => ['table1', 'table2'] |
|
1222 |
} |
|
1223 |
] |
|
1224 |
); |
|
1225 |
is_deeply($result->all, [{'table2.key3' => 4}]); |
|
1226 | ||
test cleanup
|
1227 | |
1228 |
test 'update_param'; |
|
1229 |
$dbi = DBIx::Custom->connect; |
|
1230 |
eval { $dbi->execute('drop table table1') }; |
|
1231 |
$dbi->execute($create_table1_2); |
|
1232 |
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5}); |
|
1233 |
$dbi->insert(table => 'table1', param => {key1 => 6, key2 => 7, key3 => 8, key4 => 9, key5 => 10}); |
|
1234 | ||
1235 |
$param = {key2 => 11}; |
|
1236 |
$update_param = $dbi->update_param($param); |
|
1237 |
$sql = <<"EOS"; |
|
1238 |
update table1 $update_param |
|
1239 |
where key1 = 1 |
|
1240 |
EOS |
|
1241 |
$dbi->execute($sql, param => $param); |
|
1242 |
$result = $dbi->execute('select * from table1;', table => 'table1'); |
|
1243 |
$rows = $result->all; |
|
1244 |
is_deeply($rows, [{key1 => 1, key2 => 11, key3 => 3, key4 => 4, key5 => 5}, |
|
1245 |
{key1 => 6, key2 => 7, key3 => 8, key4 => 9, key5 => 10}], |
|
1246 |
"basic"); |
|
1247 | ||
1248 | ||
1249 |
$dbi = DBIx::Custom->connect; |
|
1250 |
eval { $dbi->execute('drop table table1') }; |
|
1251 |
$dbi->execute($create_table1_2); |
|
1252 |
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5}); |
|
1253 |
$dbi->insert(table => 'table1', param => {key1 => 6, key2 => 7, key3 => 8, key4 => 9, key5 => 10}); |
|
1254 | ||
1255 |
$param = {key2 => 11, key3 => 33}; |
|
1256 |
$update_param = $dbi->update_param($param); |
|
1257 |
$sql = <<"EOS"; |
|
1258 |
update table1 $update_param |
|
1259 |
where key1 = 1 |
|
1260 |
EOS |
|
1261 |
$dbi->execute($sql, param => $param); |
|
1262 |
$result = $dbi->execute('select * from table1;', table => 'table1'); |
|
1263 |
$rows = $result->all; |
|
1264 |
is_deeply($rows, [{key1 => 1, key2 => 11, key3 => 33, key4 => 4, key5 => 5}, |
|
1265 |
{key1 => 6, key2 => 7, key3 => 8, key4 => 9, key5 => 10}], |
|
1266 |
"basic"); |
|
1267 | ||
1268 |
$dbi = DBIx::Custom->connect; |
|
1269 |
eval { $dbi->execute('drop table table1') }; |
|
1270 |
$dbi->execute($create_table1_2); |
|
1271 |
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5}); |
|
1272 |
$dbi->insert(table => 'table1', param => {key1 => 6, key2 => 7, key3 => 8, key4 => 9, key5 => 10}); |
|
1273 | ||
1274 |
$param = {key2 => 11, key3 => 33}; |
|
1275 |
$update_param = $dbi->update_param($param, {no_set => 1}); |
|
1276 |
$sql = <<"EOS"; |
|
1277 |
update table1 set $update_param |
|
1278 |
where key1 = 1 |
|
1279 |
EOS |
|
1280 |
$dbi->execute($sql, param => $param); |
|
1281 |
$result = $dbi->execute('select * from table1;', table => 'table1'); |
|
1282 |
$rows = $result->all; |
|
1283 |
is_deeply($rows, [{key1 => 1, key2 => 11, key3 => 33, key4 => 4, key5 => 5}, |
|
1284 |
{key1 => 6, key2 => 7, key3 => 8, key4 => 9, key5 => 10}], |
|
1285 |
"update param no_set"); |
|
1286 | ||
1287 |
|
|
1288 |
eval { $dbi->update_param({";" => 1}) }; |
|
1289 |
like($@, qr/not safety/); |
|
1290 | ||
1291 | ||
1292 |
test 'update_param'; |
|
1293 |
$dbi = DBIx::Custom->connect; |
|
1294 |
eval { $dbi->execute('drop table table1') }; |
|
1295 |
$dbi->execute($create_table1_2); |
|
1296 |
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5}); |
|
1297 |
$dbi->insert(table => 'table1', param => {key1 => 6, key2 => 7, key3 => 8, key4 => 9, key5 => 10}); |
|
1298 | ||
1299 |
$param = {key2 => 11}; |
|
1300 |
$update_param = $dbi->assign_param($param); |
|
1301 |
$sql = <<"EOS"; |
|
1302 |
update table1 set $update_param |
|
1303 |
where key1 = 1 |
|
1304 |
EOS |
|
1305 |
$dbi->execute($sql, param => $param, table => 'table1'); |
|
1306 |
$result = $dbi->execute('select * from table1;'); |
|
1307 |
$rows = $result->all; |
|
1308 |
is_deeply($rows, [{key1 => 1, key2 => 11, key3 => 3, key4 => 4, key5 => 5}, |
|
1309 |
{key1 => 6, key2 => 7, key3 => 8, key4 => 9, key5 => 10}], |
|
1310 |
"basic"); |
|
1311 | ||
1312 | ||
cleanup test
|
1313 |
test 'type option'; # DEPRECATED! |
1314 |
$dbi = DBIx::Custom->connect( |
|
1315 |
data_source => 'dbi:SQLite:dbname=:memory:', |
|
1316 |
dbi_option => { |
|
1317 |
$DBD::SQLite::VERSION > 1.26 ? (sqlite_unicode => 1) : (unicode => 1) |
|
1318 |
} |
|
cleanup test
|
1319 |
); |
cleanup test
|
1320 |
$binary = pack("I3", 1, 2, 3); |
1321 |
eval { $dbi->execute('drop table table1') }; |
|
1322 |
$dbi->execute('create table table1(key1, key2)'); |
|
1323 |
$dbi->insert(table => 'table1', param => {key1 => $binary, key2 => 'あ'}, type => [key1 => DBI::SQL_BLOB]); |
|
1324 |
$result = $dbi->select(table => 'table1'); |
|
1325 |
$row = $result->one; |
|
1326 |
is_deeply($row, {key1 => $binary, key2 => 'あ'}, "basic"); |
|
1327 |
$result = $dbi->execute('select length(key1) as key1_length from table1'); |
|
1328 |
$row = $result->one; |
|
1329 |
is($row->{key1_length}, length $binary); |
|
cleanup test
|
1330 | |
cleanup test
|
1331 |
test 'type_rule from'; |
1332 |
$dbi = DBIx::Custom->connect; |
|
1333 |
$dbi->type_rule( |
|
1334 |
from1 => { |
|
1335 |
date => sub { uc $_[0] } |
|
1336 |
} |
|
cleanup test
|
1337 |
); |
cleanup test
|
1338 |
$dbi->execute("create table table1 (key1 Date, key2 datetime)"); |
1339 |
$dbi->insert({key1 => 'a'}, table => 'table1'); |
|
1340 |
$result = $dbi->select(table => 'table1'); |
|
1341 |
is($result->fetch_first->[0], 'A'); |
|
cleanup test
|
1342 | |
cleanup test
|
1343 |
$result = $dbi->select(table => 'table1'); |
1344 |
is($result->one->{key1}, 'A'); |
|
cleanup test
|
1345 | |
cleanup test
|
1346 | |
1347 |
test 'type_rule into'; |
|
test cleanup
|
1348 |
$dbi = DBIx::Custom->connect; |
cleanup test
|
1349 |
$dbi->execute("create table table1 (key1 Date, key2 datetime)"); |
1350 |
$dbi->type_rule( |
|
1351 |
into1 => { |
|
1352 |
date => sub { uc $_[0] } |
|
1353 |
} |
|
1354 |
); |
|
cleanup test
|
1355 |
$dbi->insert({key1 => 'a'}, table => 'table1'); |
cleanup test
|
1356 |
$result = $dbi->select(table => 'table1'); |
1357 |
is($result->one->{key1}, 'A'); |
|
1358 | ||
test cleanup
|
1359 |
$dbi = DBIx::Custom->connect; |
cleanup test
|
1360 |
$dbi->execute("create table table1 (key1 date, key2 datetime)"); |
1361 |
$dbi->type_rule( |
|
1362 |
into1 => [ |
|
1363 |
[qw/date datetime/] => sub { uc $_[0] } |
|
1364 |
] |
|
1365 |
); |
|
1366 |
$dbi->insert({key1 => 'a', key2 => 'b'}, table => 'table1'); |
|
1367 |
$result = $dbi->select(table => 'table1'); |
|
1368 |
$row = $result->one; |
|
1369 |
is($row->{key1}, 'A'); |
|
1370 |
is($row->{key2}, 'B'); |
|
cleanup test
|
1371 | |
test cleanup
|
1372 |
$dbi = DBIx::Custom->connect; |
cleanup test
|
1373 |
$dbi->execute("create table table1 (key1 Date, key2 datetime)"); |
1374 |
$dbi->insert({key1 => 'a', key2 => 'B'}, table => 'table1'); |
|
1375 |
$dbi->type_rule( |
|
1376 |
into1 => [ |
|
1377 |
[qw/date datetime/] => sub { uc $_[0] } |
|
1378 |
] |
|
1379 |
); |
|
1380 |
$result = $dbi->execute( |
|
1381 |
"select * from table1 where key1 = :key1 and key2 = :table1.key2;", |
|
1382 |
param => {key1 => 'a', 'table1.key2' => 'b'} |
|
1383 |
); |
|
1384 |
$row = $result->one; |
|
1385 |
is($row->{key1}, 'a'); |
|
1386 |
is($row->{key2}, 'B'); |
|
cleanup test
|
1387 | |
test cleanup
|
1388 |
$dbi = DBIx::Custom->connect; |
cleanup test
|
1389 |
$dbi->execute("create table table1 (key1 Date, key2 datetime)"); |
1390 |
$dbi->insert({key1 => 'A', key2 => 'B'}, table => 'table1'); |
|
1391 |
$dbi->type_rule( |
|
1392 |
into1 => [ |
|
1393 |
[qw/date datetime/] => sub { uc $_[0] } |
|
1394 |
] |
|
1395 |
); |
|
1396 |
$result = $dbi->execute( |
|
1397 |
"select * from table1 where key1 = :key1 and key2 = :table1.key2;", |
|
1398 |
param => {key1 => 'a', 'table1.key2' => 'b'}, |
|
1399 |
table => 'table1' |
|
1400 |
); |
|
1401 |
$row = $result->one; |
|
1402 |
is($row->{key1}, 'A'); |
|
1403 |
is($row->{key2}, 'B'); |
|
cleanup test
|
1404 | |
cleanup test
|
1405 |
$dbi = DBIx::Custom->connect; |
1406 |
$dbi->execute("create table table1 (key1 date, key2 datetime)"); |
|
1407 |
$dbi->register_filter(twice => sub { $_[0] * 2 }); |
|
1408 |
$dbi->type_rule( |
|
1409 |
from1 => { |
|
1410 |
date => 'twice', |
|
1411 |
}, |
|
1412 |
into1 => { |
|
1413 |
date => 'twice', |
|
1414 |
} |
|
1415 |
); |
|
1416 |
$dbi->insert({key1 => 2}, table => 'table1'); |
|
1417 |
$result = $dbi->select(table => 'table1'); |
|
1418 |
is($result->fetch->[0], 8); |
|
cleanup test
|
1419 | |
cleanup test
|
1420 |
test 'type_rule and filter order'; |
test cleanup
|
1421 |
$dbi = DBIx::Custom->connect; |
cleanup test
|
1422 |
$dbi->execute("create table table1 (key1 Date, key2 datetime)"); |
1423 |
$dbi->type_rule( |
|
1424 |
into1 => { |
|
1425 |
date => sub { $_[0] . 'b' } |
|
1426 |
}, |
|
1427 |
into2 => { |
|
1428 |
date => sub { $_[0] . 'c' } |
|
1429 |
}, |
|
1430 |
from1 => { |
|
1431 |
date => sub { $_[0] . 'd' } |
|
1432 |
}, |
|
1433 |
from2 => { |
|
1434 |
date => sub { $_[0] . 'e' } |
|
1435 |
} |
|
1436 |
); |
|
1437 |
$dbi->insert({key1 => '1'}, table => 'table1', filter => {key1 => sub { $_[0] . 'a' }}); |
|
1438 |
$result = $dbi->select(table => 'table1'); |
|
1439 |
$result->filter(key1 => sub { $_[0] . 'f' }); |
|
1440 |
is($result->fetch_first->[0], '1abcdef'); |
|
cleanup test
|
1441 | |
cleanup test
|
1442 |
$dbi = DBIx::Custom->connect; |
1443 |
$dbi->execute("create table table1 (key1 Date, key2 datetime)"); |
|
1444 |
$dbi->type_rule( |
|
1445 |
from1 => { |
|
1446 |
date => sub { $_[0] . 'p' } |
|
1447 |
}, |
|
1448 |
from2 => { |
|
1449 |
date => sub { $_[0] . 'q' } |
|
1450 |
}, |
|
1451 |
); |
|
1452 |
$dbi->insert({key1 => '1'}, table => 'table1'); |
|
1453 |
$result = $dbi->select(table => 'table1'); |
|
1454 |
$result->type_rule( |
|
1455 |
from1 => { |
|
1456 |
date => sub { $_[0] . 'd' } |
|
1457 |
}, |
|
1458 |
from2 => { |
|
1459 |
date => sub { $_[0] . 'e' } |
|
1460 |
} |
|
1461 |
); |
|
1462 |
$result->filter(key1 => sub { $_[0] . 'f' }); |
|
1463 |
is($result->fetch_first->[0], '1def'); |
|
cleanup test
|
1464 | |
cleanup test
|
1465 |
test 'type_rule_off'; |
1466 |
$dbi = DBIx::Custom->connect; |
|
1467 |
$dbi->execute("create table table1 (key1 Date, key2 datetime)"); |
|
1468 |
$dbi->type_rule( |
|
1469 |
from1 => { |
|
1470 |
date => sub { $_[0] * 2 }, |
|
1471 |
}, |
|
1472 |
into1 => { |
|
1473 |
date => sub { $_[0] * 2 }, |
|
1474 |
} |
|
1475 |
); |
|
1476 |
$dbi->insert({key1 => 2}, table => 'table1', type_rule_off => 1); |
|
1477 |
$result = $dbi->select(table => 'table1', type_rule_off => 1); |
|
1478 |
is($result->type_rule_off->fetch->[0], 2); |
|
cleanup test
|
1479 | |
cleanup test
|
1480 |
$dbi = DBIx::Custom->connect; |
1481 |
$dbi->execute("create table table1 (key1 Date, key2 datetime)"); |
|
1482 |
$dbi->type_rule( |
|
1483 |
from1 => { |
|
1484 |
date => sub { $_[0] * 2 }, |
|
1485 |
}, |
|
1486 |
into1 => { |
|
1487 |
date => sub { $_[0] * 3 }, |
|
1488 |
} |
|
1489 |
); |
|
1490 |
$dbi->insert({key1 => 2}, table => 'table1', type_rule_off => 1); |
|
1491 |
$result = $dbi->select(table => 'table1', type_rule_off => 1); |
|
1492 |
is($result->one->{key1}, 4); |
|
cleanup test
|
1493 | |
cleanup test
|
1494 |
$dbi = DBIx::Custom->connect; |
1495 |
$dbi->execute("create table table1 (key1 Date, key2 datetime)"); |
|
1496 |
$dbi->type_rule( |
|
1497 |
from1 => { |
|
1498 |
date => sub { $_[0] * 2 }, |
|
1499 |
}, |
|
1500 |
into1 => { |
|
1501 |
date => sub { $_[0] * 3 }, |
|
1502 |
} |
|
1503 |
); |
|
1504 |
$dbi->insert({key1 => 2}, table => 'table1'); |
|
1505 |
$result = $dbi->select(table => 'table1'); |
|
1506 |
is($result->one->{key1}, 12); |
|
cleanup test
|
1507 | |
cleanup test
|
1508 |
$dbi = DBIx::Custom->connect; |
1509 |
$dbi->execute("create table table1 (key1 Date, key2 datetime)"); |
|
1510 |
$dbi->type_rule( |
|
1511 |
from1 => { |
|
1512 |
date => sub { $_[0] * 2 }, |
|
1513 |
}, |
|
1514 |
into1 => { |
|
1515 |
date => sub { $_[0] * 3 }, |
|
1516 |
} |
|
cleanup test
|
1517 |
); |
cleanup test
|
1518 |
$dbi->insert({key1 => 2}, table => 'table1'); |
1519 |
$result = $dbi->select(table => 'table1'); |
|
1520 |
is($result->fetch->[0], 12); |
|
cleanup test
|
1521 | |
cleanup test
|
1522 |
$dbi = DBIx::Custom->connect; |
1523 |
$dbi->execute("create table table1 (key1 Date, key2 datetime)"); |
|
1524 |
$dbi->register_filter(ppp => sub { uc $_[0] }); |
|
1525 |
$dbi->type_rule( |
|
1526 |
into1 => { |
|
1527 |
date => 'ppp' |
|
cleanup test
|
1528 |
} |
cleanup test
|
1529 |
); |
1530 |
$dbi->insert({key1 => 'a'}, table => 'table1'); |
|
1531 |
$result = $dbi->select(table => 'table1'); |
|
1532 |
is($result->one->{key1}, 'A'); |
|
cleanup test
|
1533 | |
cleanup test
|
1534 |
eval{$dbi->type_rule( |
1535 |
into1 => { |
|
1536 |
date => 'pp' |
|
cleanup test
|
1537 |
} |
cleanup test
|
1538 |
)}; |
1539 |
like($@, qr/not registered/); |
|
cleanup test
|
1540 | |
test cleanup
|
1541 |
$dbi = DBIx::Custom->connect; |
cleanup test
|
1542 |
$dbi->execute("create table table1 (key1 Date, key2 datetime)"); |
1543 |
eval { |
|
1544 |
$dbi->type_rule( |
|
1545 |
from1 => { |
|
1546 |
Date => sub { $_[0] * 2 }, |
|
1547 |
} |
|
1548 |
); |
|
1549 |
}; |
|
1550 |
like($@, qr/lower/); |
|
clenup test
|
1551 | |
cleanup test
|
1552 |
eval { |
1553 |
$dbi->type_rule( |
|
1554 |
into1 => { |
|
1555 |
Date => sub { $_[0] * 2 }, |
|
1556 |
} |
|
1557 |
); |
|
1558 |
}; |
|
1559 |
like($@, qr/lower/); |
|
clenup test
|
1560 | |
cleanup test
|
1561 |
$dbi = DBIx::Custom->connect; |
1562 |
$dbi->execute("create table table1 (key1 Date, key2 datetime)"); |
|
1563 |
$dbi->type_rule( |
|
1564 |
from1 => { |
|
1565 |
date => sub { $_[0] * 2 }, |
|
1566 |
}, |
|
1567 |
into1 => { |
|
1568 |
date => sub { $_[0] * 3 }, |
|
1569 |
} |
|
1570 |
); |
|
1571 |
$dbi->insert({key1 => 2}, table => 'table1'); |
|
clenup test
|
1572 |
$result = $dbi->select(table => 'table1'); |
cleanup test
|
1573 |
$result->type_rule_off; |
1574 |
is($result->one->{key1}, 6); |
|
clenup test
|
1575 | |
cleanup test
|
1576 |
$dbi = DBIx::Custom->connect; |
1577 |
$dbi->execute("create table table1 (key1 Date, key2 datetime)"); |
|
1578 |
$dbi->type_rule( |
|
1579 |
from1 => { |
|
1580 |
date => sub { $_[0] * 2 }, |
|
1581 |
datetime => sub { $_[0] * 4 }, |
|
1582 |
}, |
|
1583 |
); |
|
1584 |
$dbi->insert({key1 => 2, key2 => 2}, table => 'table1'); |
|
clenup test
|
1585 |
$result = $dbi->select(table => 'table1'); |
cleanup test
|
1586 |
$result->type_rule( |
1587 |
from1 => { |
|
1588 |
date => sub { $_[0] * 3 } |
|
1589 |
} |
|
1590 |
); |
|
1591 |
$row = $result->one; |
|
1592 |
is($row->{key1}, 6); |
|
1593 |
is($row->{key2}, 2); |
|
clenup test
|
1594 | |
1595 |
$result = $dbi->select(table => 'table1'); |
|
cleanup test
|
1596 |
$result->type_rule( |
1597 |
from1 => { |
|
1598 |
date => sub { $_[0] * 3 } |
|
1599 |
} |
|
1600 |
); |
|
1601 |
$row = $result->one; |
|
1602 |
is($row->{key1}, 6); |
|
1603 |
is($row->{key2}, 2); |
|
clenup test
|
1604 | |
1605 |
$result = $dbi->select(table => 'table1'); |
|
cleanup test
|
1606 |
$result->type_rule( |
1607 |
from1 => { |
|
1608 |
date => sub { $_[0] * 3 } |
|
1609 |
} |
|
1610 |
); |
|
1611 |
$row = $result->one; |
|
1612 |
is($row->{key1}, 6); |
|
1613 |
is($row->{key2}, 2); |
|
clenup test
|
1614 |
$result = $dbi->select(table => 'table1'); |
cleanup test
|
1615 |
$result->type_rule( |
1616 |
from1 => [date => sub { $_[0] * 3 }] |
|
1617 |
); |
|
1618 |
$row = $result->one; |
|
1619 |
is($row->{key1}, 6); |
|
1620 |
is($row->{key2}, 2); |
|
1621 |
$dbi->register_filter(fivetimes => sub { $_[0] * 5}); |
|
clenup test
|
1622 |
$result = $dbi->select(table => 'table1'); |
cleanup test
|
1623 |
$result->type_rule( |
1624 |
from1 => [date => 'fivetimes'] |
|
1625 |
); |
|
1626 |
$row = $result->one; |
|
1627 |
is($row->{key1}, 10); |
|
1628 |
is($row->{key2}, 2); |
|
clenup test
|
1629 |
$result = $dbi->select(table => 'table1'); |
cleanup test
|
1630 |
$result->type_rule( |
1631 |
from1 => [date => undef] |
|
1632 |
); |
|
1633 |
$row = $result->one; |
|
1634 |
is($row->{key1}, 2); |
|
1635 |
is($row->{key2}, 2); |
|
clenup test
|
1636 | |
test cleanup
|
1637 |
$dbi = DBIx::Custom->connect; |
cleanup test
|
1638 |
$dbi->execute("create table table1 (key1 Date, key2 datetime)"); |
1639 |
$dbi->type_rule( |
|
1640 |
from1 => { |
|
1641 |
date => sub { $_[0] * 2 }, |
|
1642 |
}, |
|
1643 |
); |
|
1644 |
$dbi->insert({key1 => 2}, table => 'table1'); |
|
clenup test
|
1645 |
$result = $dbi->select(table => 'table1'); |
cleanup test
|
1646 |
$result->filter(key1 => sub { $_[0] * 3 }); |
1647 |
is($result->one->{key1}, 12); |
|
clenup test
|
1648 | |
cleanup test
|
1649 |
$dbi = DBIx::Custom->connect; |
1650 |
$dbi->execute("create table table1 (key1 Date, key2 datetime)"); |
|
1651 |
$dbi->type_rule( |
|
1652 |
from1 => { |
|
1653 |
date => sub { $_[0] * 2 }, |
|
1654 |
}, |
|
1655 |
); |
|
1656 |
$dbi->insert({key1 => 2}, table => 'table1'); |
|
clenup test
|
1657 |
$result = $dbi->select(table => 'table1'); |
cleanup test
|
1658 |
$result->filter(key1 => sub { $_[0] * 3 }); |
1659 |
is($result->fetch->[0], 12); |
|
clenup test
|
1660 | |
cleanup test
|
1661 |
$dbi = DBIx::Custom->connect; |
1662 |
$dbi->execute("create table table1 (key1 Date, key2 datetime)"); |
|
1663 |
$dbi->type_rule( |
|
1664 |
into1 => { |
|
1665 |
date => sub { $_[0] . 'b' } |
|
1666 |
}, |
|
1667 |
into2 => { |
|
1668 |
date => sub { $_[0] . 'c' } |
|
1669 |
}, |
|
1670 |
from1 => { |
|
1671 |
date => sub { $_[0] . 'd' } |
|
1672 |
}, |
|
1673 |
from2 => { |
|
1674 |
date => sub { $_[0] . 'e' } |
|
1675 |
} |
|
1676 |
); |
|
1677 |
$dbi->insert({key1 => '1'}, table => 'table1', type_rule_off => 1); |
|
clenup test
|
1678 |
$result = $dbi->select(table => 'table1'); |
cleanup test
|
1679 |
is($result->type_rule_off->fetch_first->[0], '1'); |
clenup test
|
1680 |
$result = $dbi->select(table => 'table1'); |
cleanup test
|
1681 |
is($result->type_rule_on->fetch_first->[0], '1de'); |
clenup test
|
1682 | |
cleanup test
|
1683 |
$dbi = DBIx::Custom->connect; |
1684 |
$dbi->execute("create table table1 (key1 Date, key2 datetime)"); |
|
1685 |
$dbi->type_rule( |
|
1686 |
into1 => { |
|
1687 |
date => sub { $_[0] . 'b' } |
|
cleanup test
|
1688 |
}, |
cleanup test
|
1689 |
into2 => { |
1690 |
date => sub { $_[0] . 'c' } |
|
cleanup test
|
1691 |
}, |
cleanup test
|
1692 |
from1 => { |
1693 |
date => sub { $_[0] . 'd' } |
|
cleanup test
|
1694 |
}, |
cleanup test
|
1695 |
from2 => { |
1696 |
date => sub { $_[0] . 'e' } |
|
cleanup test
|
1697 |
} |
1698 |
); |
|
cleanup test
|
1699 |
$dbi->insert({key1 => '1'}, table => 'table1', type_rule1_off => 1); |
1700 |
$result = $dbi->select(table => 'table1'); |
|
1701 |
is($result->type_rule1_off->fetch_first->[0], '1ce'); |
|
1702 |
$result = $dbi->select(table => 'table1'); |
|
1703 |
is($result->type_rule1_on->fetch_first->[0], '1cde'); |
|
cleanup test
|
1704 | |
cleanup test
|
1705 |
$dbi = DBIx::Custom->connect; |
1706 |
$dbi->execute("create table table1 (key1 Date, key2 datetime)"); |
|
1707 |
$dbi->type_rule( |
|
1708 |
into1 => { |
|
1709 |
date => sub { $_[0] . 'b' } |
|
1710 |
}, |
|
1711 |
into2 => { |
|
1712 |
date => sub { $_[0] . 'c' } |
|
1713 |
}, |
|
1714 |
from1 => { |
|
1715 |
date => sub { $_[0] . 'd' } |
|
1716 |
}, |
|
1717 |
from2 => { |
|
1718 |
date => sub { $_[0] . 'e' } |
|
cleanup test
|
1719 |
} |
1720 |
); |
|
cleanup test
|
1721 |
$dbi->insert({key1 => '1'}, table => 'table1', type_rule2_off => 1); |
1722 |
$result = $dbi->select(table => 'table1'); |
|
1723 |
is($result->type_rule2_off->fetch_first->[0], '1bd'); |
|
1724 |
$result = $dbi->select(table => 'table1'); |
|
1725 |
is($result->type_rule2_on->fetch_first->[0], '1bde'); |
|
test cleanup
|
1726 | |
1727 |
test 'prefix'; |
|
1728 |
$dbi = DBIx::Custom->connect; |
|
1729 |
eval { $dbi->execute('drop table table1') }; |
|
1730 |
$dbi->execute("create table table1 (key1 char(255), key2 char(255), primary key(key1))"); |
|
1731 |
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2}); |
|
1732 |
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 4}, prefix => 'or replace'); |
|
1733 |
$result = $dbi->execute('select * from table1;'); |
|
1734 |
$rows = $result->all; |
|
1735 |
is_deeply($rows, [{key1 => 1, key2 => 4}], "basic"); |
|
1736 | ||
1737 |
$dbi = DBIx::Custom->connect; |
|
1738 |
eval { $dbi->execute('drop table table1') }; |
|
1739 |
$dbi->execute("create table table1 (key1 char(255), key2 char(255), primary key(key1))"); |
|
1740 |
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2}); |
|
1741 |
$dbi->update(table => 'table1', param => {key2 => 4}, |
|
1742 |
where => {key1 => 1}, prefix => 'or replace'); |
|
1743 |
$result = $dbi->execute('select * from table1;'); |
|
1744 |
$rows = $result->all; |
|
1745 |
is_deeply($rows, [{key1 => 1, key2 => 4}], "basic"); |
|
1746 | ||
test cleanup
|
1747 |
test 'Model class'; |
1748 |
use MyDBI1; |
|
1749 |
$dbi = MyDBI1->connect; |
|
1750 |
eval { $dbi->execute('drop table book') }; |
|
1751 |
$dbi->execute("create table book (title, author)"); |
|
1752 |
$model = $dbi->model('book'); |
|
1753 |
$model->insert({title => 'a', author => 'b'}); |
|
1754 |
is_deeply($model->list->all, [{title => 'a', author => 'b'}], 'basic'); |
|
1755 |
$dbi->execute("create table company (name)"); |
|
1756 |
$model = $dbi->model('company'); |
|
1757 |
$model->insert({name => 'a'}); |
|
1758 |
is_deeply($model->list->all, [{name => 'a'}], 'basic'); |
|
1759 |
is($dbi->models->{'book'}, $dbi->model('book')); |
|
1760 |
is($dbi->models->{'company'}, $dbi->model('company')); |
|
1761 | ||
1762 |
$dbi = MyDBI4->connect; |
|
1763 |
eval { $dbi->execute('drop table book') }; |
|
1764 |
$dbi->execute("create table book (title, author)"); |
|
1765 |
$model = $dbi->model('book'); |
|
1766 |
$model->insert({title => 'a', author => 'b'}); |
|
1767 |
is_deeply($model->list->all, [{title => 'a', author => 'b'}], 'basic'); |
|
1768 |
$dbi->execute("create table company (name)"); |
|
1769 |
$model = $dbi->model('company'); |
|
1770 |
$model->insert({name => 'a'}); |
|
1771 |
is_deeply($model->list->all, [{name => 'a'}], 'basic'); |
|
test cleanup
|
1772 | |
test cleanup
|
1773 |
$dbi = MyDBI5->connect; |
1774 |
eval { $dbi->execute('drop table company') }; |
|
1775 |
eval { $dbi->execute('drop table table1') }; |
|
1776 |
$dbi->execute("create table company (name)"); |
|
1777 |
$dbi->execute("create table table1 (key1)"); |
|
1778 |
$model = $dbi->model('company'); |
|
1779 |
$model->insert({name => 'a'}); |
|
1780 |
is_deeply($model->list->all, [{name => 'a'}], 'include all model'); |
|
1781 |
$dbi->insert(table => 'table1', param => {key1 => 1}); |
|
1782 |
$model = $dbi->model('book'); |
|
1783 |
is_deeply($model->list->all, [{key1 => 1}], 'include all model'); |
|
test cleanup
|
1784 | |
test cleanup
|
1785 |
test 'primary_key'; |
1786 |
use MyDBI1; |
|
1787 |
$dbi = MyDBI1->connect; |
|
1788 |
$model = $dbi->model('book'); |
|
1789 |
$model->primary_key(['id', 'number']); |
|
1790 |
is_deeply($model->primary_key, ['id', 'number']); |
|
test cleanup
|
1791 | |
test cleanup
|
1792 |
test 'columns'; |
1793 |
use MyDBI1; |
|
1794 |
$dbi = MyDBI1->connect; |
|
1795 |
$model = $dbi->model('book'); |
|
1796 |
$model->columns(['id', 'number']); |
|
1797 |
is_deeply($model->columns, ['id', 'number']); |
|
test cleanup
|
1798 | |
test cleanup
|
1799 |
test 'setup_model'; |
1800 |
use MyDBI1; |
|
1801 |
$dbi = MyDBI1->connect; |
|
1802 |
eval { $dbi->execute('drop table book') }; |
|
1803 |
eval { $dbi->execute('drop table company') }; |
|
1804 |
eval { $dbi->execute('drop table test') }; |
|
test cleanup
|
1805 | |
test cleanup
|
1806 |
$dbi->execute('create table book (id)'); |
1807 |
$dbi->execute('create table company (id, name);'); |
|
1808 |
$dbi->execute('create table test (id, name, primary key (id, name));'); |
|
1809 |
$dbi->setup_model; |
|
1810 |
is_deeply($dbi->model('book')->columns, ['id']); |
|
1811 |
is_deeply($dbi->model('company')->columns, ['id', 'name']); |
|
test cleanup
|
1812 | |
1813 | ||
1814 | ||
1815 | ||
1816 | ||
1817 | ||
1818 | ||
1819 | ||
1820 | ||
1821 | ||
1822 | ||
1823 | ||
1824 | ||
1825 | ||
test cleanup
|
1826 |
### SQLite only test |
1827 |
test 'quote'; |
|
1828 |
$dbi = DBIx::Custom->connect; |
|
1829 |
$dbi->quote('"'); |
|
1830 |
eval { $dbi->execute("drop table ${q}table$p") }; |
|
1831 |
$dbi->execute($create_table_reserved); |
|
1832 |
$dbi->apply_filter('table', select => {out => sub { $_[0] * 2}}); |
|
1833 |
$dbi->insert(table => 'table', param => {select => 1}); |
|
1834 |
$dbi->delete(table => 'table', where => {select => 1}); |
|
1835 |
$result = $dbi->execute("select * from ${q}table$p"); |
|
1836 |
$rows = $result->all; |
|
1837 |
is_deeply($rows, [], "reserved word"); |
|
1838 | ||
1839 | ||
1840 | ||
1841 | ||
1842 | ||
1843 | ||
1844 | ||
test cleanup
|
1845 | |
1846 | ||
1847 | ||
1848 | ||
1849 | ||
1850 | ||
1851 | ||
1852 | ||
1853 | ||
1854 | ||
1855 |
# DEPRECATED! test |
|
1856 |
test 'filter __ expression'; |
|
1857 |
$dbi = DBIx::Custom->connect; |
|
1858 |
eval { $dbi->execute('drop table company') }; |
|
1859 |
eval { $dbi->execute('drop table location') }; |
|
1860 |
$dbi->execute('create table company (id, name, location_id)'); |
|
1861 |
$dbi->execute('create table location (id, name)'); |
|
1862 |
$dbi->apply_filter('location', |
|
1863 |
name => {in => sub { uc $_[0] } } |
|
1864 |
); |
|
1865 | ||
1866 |
$dbi->insert(table => 'company', param => {id => 1, name => 'a', location_id => 2}); |
|
1867 |
$dbi->insert(table => 'location', param => {id => 2, name => 'b'}); |
|
1868 | ||
1869 |
$result = $dbi->select( |
|
1870 |
table => ['company', 'location'], relation => {'company.location_id' => 'location.id'}, |
|
1871 |
column => ['location.name as location__name'] |
|
1872 |
); |
|
1873 |
is($result->fetch_first->[0], 'B'); |
|
1874 | ||
1875 |
$result = $dbi->select( |
|
1876 |
table => 'company', relation => {'company.location_id' => 'location.id'}, |
|
1877 |
column => ['location.name as location__name'] |
|
1878 |
); |
|
1879 |
is($result->fetch_first->[0], 'B'); |
|
1880 | ||
1881 |
$result = $dbi->select( |
|
1882 |
table => 'company', relation => {'company.location_id' => 'location.id'}, |
|
1883 |
column => ['location.name as "location.name"'] |
|
1884 |
); |
|
1885 |
is($result->fetch_first->[0], 'B'); |
|
test cleanup
|
1886 | |
1887 |
test 'reserved_word_quote'; |
|
1888 |
$dbi = DBIx::Custom->connect; |
|
1889 |
eval { $dbi->execute("drop table ${q}table$p") }; |
|
1890 |
$dbi->reserved_word_quote('"'); |
|
1891 |
$dbi->execute($create_table_reserved); |
|
1892 |
$dbi->apply_filter('table', select => {out => sub { $_[0] * 2}}); |
|
1893 |
$dbi->apply_filter('table', update => {out => sub { $_[0] * 3}}); |
|
1894 |
$dbi->insert(table => 'table', param => {select => 1}); |
|
1895 |
$dbi->update(table => 'table', where => {'table.select' => 1}, param => {update => 2}); |
|
1896 |
$result = $dbi->execute("select * from ${q}table$p"); |
|
1897 |
$rows = $result->all; |
|
1898 |
is_deeply($rows, [{select => 2, update => 6}], "reserved word"); |