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 | ||
202 | ||
cleanup test
|
203 | |
cleanup test
|
204 | |
205 | ||
cleanup test
|
206 | |
test cleanup
|
207 | |
208 | ||
209 | ||
210 | ||
211 | ||
212 | ||
213 | ||
214 | ||
215 | ||
216 | ||
217 | ||
218 | ||
219 | ||
220 | ||
221 | ||
222 | ||
223 | ||
224 | ||
225 | ||
226 | ||
227 | ||
228 | ||
229 | ||
230 | ||
231 | ||
232 | ||
233 |
### a little complex test |
|
cleanup test
|
234 |
test 'table_alias'; |
235 |
$dbi = DBIx::Custom->connect; |
|
236 |
eval { $dbi->execute('drop table table1') }; |
|
237 |
$dbi->execute("create table table1 (key1 Date, key2 datetime)"); |
|
238 |
$dbi->type_rule( |
|
239 |
into1 => { |
|
240 |
date => sub { uc $_[0] } |
|
241 |
} |
|
242 |
); |
|
243 |
$dbi->execute("insert into table1 (key1) values (:table2.key1)", {'table2.key1' => 'a'}, |
|
244 |
table_alias => {table2 => 'table1'}); |
|
245 |
$result = $dbi->select(table => 'table1'); |
|
246 |
is($result->one->{key1}, 'A'); |
|
247 | ||
cleanup test
|
248 |
test 'select() wrap option'; |
249 |
$dbi = DBIx::Custom->connect; |
|
250 |
eval { $dbi->execute('drop table table1') }; |
|
251 |
$dbi->execute($create_table1); |
|
252 |
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2}); |
|
253 |
$dbi->insert(table => 'table1', param => {key1 => 2, key2 => 3}); |
|
254 |
$rows = $dbi->select( |
|
255 |
table => 'table1', |
|
256 |
column => 'key1', |
|
257 |
wrap => ['select * from (', ') as t where key1 = 1'] |
|
258 |
)->all; |
|
259 |
is_deeply($rows, [{key1 => 1}]); |
|
260 | ||
261 |
eval { |
|
262 |
$dbi->select( |
|
263 |
table => 'table1', |
|
264 |
column => 'key1', |
|
265 |
wrap => 'select * from (' |
|
266 |
) |
|
267 |
}; |
|
268 |
like($@, qr/array/); |
|
269 | ||
cleanup test
|
270 |
test 'dbi method from model'; |
271 |
$dbi = MyDBI9->connect; |
|
272 |
eval { $dbi->execute('drop table table1') }; |
|
273 |
$dbi->execute($create_table1); |
|
274 |
$dbi->setup_model; |
|
275 |
$model = $dbi->model('table1'); |
|
276 |
eval{$model->execute('select * from table1')}; |
|
277 |
ok(!$@); |
|
278 | ||
279 |
test 'column table option'; |
|
280 |
$dbi = MyDBI9->connect; |
|
281 |
eval { $dbi->execute('drop table table1') }; |
|
282 |
$dbi->execute($create_table1); |
|
283 |
eval { $dbi->execute('drop table table2') }; |
|
284 |
$dbi->execute($create_table2); |
|
285 |
$dbi->setup_model; |
|
286 |
$dbi->execute('insert into table1 (key1, key2) values (1, 2);'); |
|
287 |
$dbi->execute('insert into table2 (key1, key3) values (1, 4);'); |
|
288 |
$model = $dbi->model('table1'); |
|
289 |
$result = $model->select( |
|
290 |
column => [ |
|
291 |
$model->column('table2', {alias => 'table2_alias'}) |
|
292 |
], |
|
293 |
where => {'table2_alias.key3' => 4} |
|
294 |
); |
|
295 |
is_deeply($result->one, |
|
296 |
{'table2_alias.key1' => 1, 'table2_alias.key3' => 4}); |
|
297 | ||
298 |
$dbi->separator('__'); |
|
299 |
$result = $model->select( |
|
300 |
column => [ |
|
301 |
$model->column('table2', {alias => 'table2_alias'}) |
|
302 |
], |
|
303 |
where => {'table2_alias.key3' => 4} |
|
304 |
); |
|
305 |
is_deeply($result->one, |
|
306 |
{'table2_alias__key1' => 1, 'table2_alias__key3' => 4}); |
|
307 | ||
308 |
$dbi->separator('-'); |
|
309 |
$result = $model->select( |
|
310 |
column => [ |
|
311 |
$model->column('table2', {alias => 'table2_alias'}) |
|
312 |
], |
|
313 |
where => {'table2_alias.key3' => 4} |
|
314 |
); |
|
315 |
is_deeply($result->one, |
|
316 |
{'table2_alias-key1' => 1, 'table2_alias-key3' => 4}); |
|
317 | ||
318 |
test 'create_model'; |
|
319 |
$dbi = DBIx::Custom->connect; |
|
320 |
eval { $dbi->execute('drop table table1') }; |
|
321 |
eval { $dbi->execute('drop table table2') }; |
|
322 |
$dbi->execute($create_table1); |
|
323 |
$dbi->execute($create_table2); |
|
324 | ||
325 |
$dbi->create_model( |
|
326 |
table => 'table1', |
|
327 |
join => [ |
|
328 |
'left outer join table2 on table1.key1 = table2.key1' |
|
329 |
], |
|
330 |
primary_key => ['key1'] |
|
331 |
); |
|
332 |
$model2 = $dbi->create_model( |
|
333 |
table => 'table2' |
|
334 |
); |
|
335 |
$dbi->create_model( |
|
336 |
table => 'table3', |
|
337 |
filter => [ |
|
338 |
key1 => {in => sub { uc $_[0] }} |
|
339 |
] |
|
340 |
); |
|
341 |
$dbi->setup_model; |
|
342 |
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2}); |
|
343 |
$dbi->insert(table => 'table2', param => {key1 => 1, key3 => 3}); |
|
344 |
$model = $dbi->model('table1'); |
|
345 |
$result = $model->select( |
|
346 |
column => [$model->mycolumn, $model->column('table2')], |
|
347 |
where => {'table1.key1' => 1} |
|
348 |
); |
|
349 |
is_deeply($result->one, |
|
350 |
{key1 => 1, key2 => 2, 'table2.key1' => 1, 'table2.key3' => 3}); |
|
351 |
is_deeply($model2->select->one, {key1 => 1, key3 => 3}); |
|
352 | ||
353 |
test 'model method'; |
|
354 |
$dbi = DBIx::Custom->connect; |
|
355 |
eval { $dbi->execute('drop table table2') }; |
|
356 |
$dbi->execute($create_table2); |
|
357 |
$dbi->insert(table => 'table2', param => {key1 => 1, key3 => 3}); |
|
358 |
$model = $dbi->create_model( |
|
359 |
table => 'table2' |
|
360 |
); |
|
361 |
$model->method(foo => sub { shift->select(@_) }); |
|
362 |
is_deeply($model->foo->one, {key1 => 1, key3 => 3}); |
|
363 | ||
cleanup test
|
364 |
test 'join'; |
365 |
$dbi = DBIx::Custom->connect; |
|
366 |
eval { $dbi->execute('drop table table1') }; |
|
367 |
$dbi->execute($create_table1); |
|
368 |
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2}); |
|
369 |
$dbi->insert(table => 'table1', param => {key1 => 3, key2 => 4}); |
|
370 |
$dbi->execute($create_table2); |
|
371 |
$dbi->insert(table => 'table2', param => {key1 => 1, key3 => 5}); |
|
372 |
$dbi->execute('create table table3 (key3 int, key4 int);'); |
|
373 |
$dbi->insert(table => 'table3', param => {key3 => 5, key4 => 4}); |
|
374 |
$rows = $dbi->select( |
|
375 |
table => 'table1', |
|
376 |
column => 'table1.key1 as table1_key1, table2.key1 as table2_key1, key2, key3', |
|
377 |
where => {'table1.key2' => 2}, |
|
378 |
join => ['left outer join table2 on table1.key1 = table2.key1'] |
|
379 |
)->all; |
|
380 |
is_deeply($rows, [{table1_key1 => 1, table2_key1 => 1, key2 => 2, key3 => 5}]); |
|
381 | ||
382 |
$rows = $dbi->select( |
|
383 |
table => 'table1', |
|
384 |
where => {'key1' => 1}, |
|
385 |
join => ['left outer join table2 on table1.key1 = table2.key1'] |
|
386 |
)->all; |
|
387 |
is_deeply($rows, [{key1 => 1, key2 => 2}]); |
|
388 | ||
389 |
eval { |
|
390 |
$rows = $dbi->select( |
|
391 |
table => 'table1', |
|
392 |
column => 'table1.key1 as table1_key1, table2.key1 as table2_key1, key2, key3', |
|
393 |
where => {'table1.key2' => 2}, |
|
394 |
join => {'table1.key1' => 'table2.key1'} |
|
395 |
); |
|
396 |
}; |
|
397 |
like ($@, qr/array/); |
|
398 | ||
399 |
$rows = $dbi->select( |
|
400 |
table => 'table1', |
|
401 |
where => {'key1' => 1}, |
|
402 |
join => ['left outer join table2 on table1.key1 = table2.key1', |
|
403 |
'left outer join table3 on table2.key3 = table3.key3'] |
|
404 |
)->all; |
|
405 |
is_deeply($rows, [{key1 => 1, key2 => 2}]); |
|
406 | ||
407 |
$rows = $dbi->select( |
|
408 |
column => 'table3.key4 as table3__key4', |
|
409 |
table => 'table1', |
|
410 |
where => {'table1.key1' => 1}, |
|
411 |
join => ['left outer join table2 on table1.key1 = table2.key1', |
|
412 |
'left outer join table3 on table2.key3 = table3.key3'] |
|
413 |
)->all; |
|
414 |
is_deeply($rows, [{table3__key4 => 4}]); |
|
415 | ||
416 |
$rows = $dbi->select( |
|
417 |
column => 'table1.key1 as table1__key1', |
|
418 |
table => 'table1', |
|
419 |
where => {'table3.key4' => 4}, |
|
420 |
join => ['left outer join table2 on table1.key1 = table2.key1', |
|
421 |
'left outer join table3 on table2.key3 = table3.key3'] |
|
422 |
)->all; |
|
423 |
is_deeply($rows, [{table1__key1 => 1}]); |
|
424 | ||
425 |
$dbi = DBIx::Custom->connect; |
|
426 |
$dbi->quote('"'); |
|
427 |
eval { $dbi->execute('drop table table1') }; |
|
428 |
$dbi->execute($create_table1); |
|
429 |
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2}); |
|
430 |
$dbi->execute($create_table2); |
|
431 |
$dbi->insert(table => 'table2', param => {key1 => 1, key3 => 5}); |
|
432 |
$rows = $dbi->select( |
|
433 |
table => 'table1', |
|
434 |
column => '"table1"."key1" as "table1_key1", "table2"."key1" as "table2_key1", "key2", "key3"', |
|
435 |
where => {'table1.key2' => 2}, |
|
436 |
join => ['left outer join "table2" on "table1"."key1" = "table2"."key1"'], |
|
437 |
)->all; |
|
438 |
is_deeply($rows, [{table1_key1 => 1, table2_key1 => 1, key2 => 2, key3 => 5}], |
|
439 |
'quote'); |
|
440 | ||
441 | ||
442 |
$dbi = DBIx::Custom->connect; |
|
443 |
eval { $dbi->execute('drop table table1') }; |
|
444 |
$dbi->execute($create_table1); |
|
445 |
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2}); |
|
446 |
$sql = <<"EOS"; |
|
447 |
left outer join ( |
|
448 |
select * from table1 as t1 |
|
449 |
where t1.key2 = ( |
|
450 |
select max(t2.key2) from table1 as t2 |
|
451 |
where t1.key1 = t2.key1 |
|
452 |
) |
|
453 |
) as latest_table1 on table1.key1 = latest_table1.key1 |
|
454 |
EOS |
|
455 |
$join = [$sql]; |
|
456 |
$rows = $dbi->select( |
|
457 |
table => 'table1', |
|
458 |
column => 'latest_table1.key1 as latest_table1__key1', |
|
459 |
join => $join |
|
460 |
)->all; |
|
461 |
is_deeply($rows, [{latest_table1__key1 => 1}]); |
|
462 | ||
463 |
$dbi = DBIx::Custom->connect; |
|
464 |
eval { $dbi->execute('drop table table1') }; |
|
465 |
eval { $dbi->execute('drop table table2') }; |
|
466 |
$dbi->execute($create_table1); |
|
467 |
$dbi->execute($create_table2); |
|
468 |
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2}); |
|
469 |
$dbi->insert(table => 'table2', param => {key1 => 1, key3 => 4}); |
|
470 |
$dbi->insert(table => 'table2', param => {key1 => 1, key3 => 5}); |
|
471 |
$result = $dbi->select( |
|
472 |
table => 'table1', |
|
473 |
join => [ |
|
474 |
"left outer join table2 on table2.key2 = '4' and table1.key1 = table2.key1" |
|
475 |
] |
|
476 |
); |
|
477 |
is_deeply($result->all, [{key1 => 1, key2 => 2}]); |
|
478 |
$result = $dbi->select( |
|
479 |
table => 'table1', |
|
480 |
column => [{table2 => ['key3']}], |
|
481 |
join => [ |
|
482 |
"left outer join table2 on table2.key3 = '4' and table1.key1 = table2.key1" |
|
483 |
] |
|
484 |
); |
|
485 |
is_deeply($result->all, [{'table2.key3' => 4}]); |
|
486 |
$result = $dbi->select( |
|
487 |
table => 'table1', |
|
488 |
column => [{table2 => ['key3']}], |
|
489 |
join => [ |
|
490 |
"left outer join table2 on table1.key1 = table2.key1 and table2.key3 = '4'" |
|
491 |
] |
|
492 |
); |
|
493 |
is_deeply($result->all, [{'table2.key3' => 4}]); |
|
494 | ||
495 |
$dbi = DBIx::Custom->connect; |
|
496 |
eval { $dbi->execute('drop table table1') }; |
|
497 |
eval { $dbi->execute('drop table table2') }; |
|
498 |
$dbi->execute($create_table1); |
|
499 |
$dbi->execute($create_table2); |
|
500 |
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2}); |
|
501 |
$dbi->insert(table => 'table2', param => {key1 => 1, key3 => 4}); |
|
502 |
$dbi->insert(table => 'table2', param => {key1 => 1, key3 => 5}); |
|
503 |
$result = $dbi->select( |
|
504 |
table => 'table1', |
|
505 |
column => [{table2 => ['key3']}], |
|
506 |
join => [ |
|
507 |
{ |
|
508 |
clause => "left outer join table2 on table2.key3 = '4' and table1.key1 = table2.key1", |
|
509 |
table => ['table1', 'table2'] |
|
510 |
} |
|
511 |
] |
|
512 |
); |
|
513 |
is_deeply($result->all, [{'table2.key3' => 4}]); |
|
514 | ||
test cleanup
|
515 | |
516 |
test 'update_param'; |
|
517 |
$dbi = DBIx::Custom->connect; |
|
518 |
eval { $dbi->execute('drop table table1') }; |
|
519 |
$dbi->execute($create_table1_2); |
|
520 |
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5}); |
|
521 |
$dbi->insert(table => 'table1', param => {key1 => 6, key2 => 7, key3 => 8, key4 => 9, key5 => 10}); |
|
522 | ||
523 |
$param = {key2 => 11}; |
|
524 |
$update_param = $dbi->update_param($param); |
|
525 |
$sql = <<"EOS"; |
|
526 |
update table1 $update_param |
|
527 |
where key1 = 1 |
|
528 |
EOS |
|
529 |
$dbi->execute($sql, param => $param); |
|
530 |
$result = $dbi->execute('select * from table1;', table => 'table1'); |
|
531 |
$rows = $result->all; |
|
532 |
is_deeply($rows, [{key1 => 1, key2 => 11, key3 => 3, key4 => 4, key5 => 5}, |
|
533 |
{key1 => 6, key2 => 7, key3 => 8, key4 => 9, key5 => 10}], |
|
534 |
"basic"); |
|
535 | ||
536 | ||
537 |
$dbi = DBIx::Custom->connect; |
|
538 |
eval { $dbi->execute('drop table table1') }; |
|
539 |
$dbi->execute($create_table1_2); |
|
540 |
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5}); |
|
541 |
$dbi->insert(table => 'table1', param => {key1 => 6, key2 => 7, key3 => 8, key4 => 9, key5 => 10}); |
|
542 | ||
543 |
$param = {key2 => 11, key3 => 33}; |
|
544 |
$update_param = $dbi->update_param($param); |
|
545 |
$sql = <<"EOS"; |
|
546 |
update table1 $update_param |
|
547 |
where key1 = 1 |
|
548 |
EOS |
|
549 |
$dbi->execute($sql, param => $param); |
|
550 |
$result = $dbi->execute('select * from table1;', table => 'table1'); |
|
551 |
$rows = $result->all; |
|
552 |
is_deeply($rows, [{key1 => 1, key2 => 11, key3 => 33, key4 => 4, key5 => 5}, |
|
553 |
{key1 => 6, key2 => 7, key3 => 8, key4 => 9, key5 => 10}], |
|
554 |
"basic"); |
|
555 | ||
556 |
$dbi = DBIx::Custom->connect; |
|
557 |
eval { $dbi->execute('drop table table1') }; |
|
558 |
$dbi->execute($create_table1_2); |
|
559 |
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5}); |
|
560 |
$dbi->insert(table => 'table1', param => {key1 => 6, key2 => 7, key3 => 8, key4 => 9, key5 => 10}); |
|
561 | ||
562 |
$param = {key2 => 11, key3 => 33}; |
|
563 |
$update_param = $dbi->update_param($param, {no_set => 1}); |
|
564 |
$sql = <<"EOS"; |
|
565 |
update table1 set $update_param |
|
566 |
where key1 = 1 |
|
567 |
EOS |
|
568 |
$dbi->execute($sql, param => $param); |
|
569 |
$result = $dbi->execute('select * from table1;', table => 'table1'); |
|
570 |
$rows = $result->all; |
|
571 |
is_deeply($rows, [{key1 => 1, key2 => 11, key3 => 33, key4 => 4, key5 => 5}, |
|
572 |
{key1 => 6, key2 => 7, key3 => 8, key4 => 9, key5 => 10}], |
|
573 |
"update param no_set"); |
|
574 | ||
575 |
|
|
576 |
eval { $dbi->update_param({";" => 1}) }; |
|
577 |
like($@, qr/not safety/); |
|
578 | ||
579 | ||
580 |
test 'update_param'; |
|
581 |
$dbi = DBIx::Custom->connect; |
|
582 |
eval { $dbi->execute('drop table table1') }; |
|
583 |
$dbi->execute($create_table1_2); |
|
584 |
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5}); |
|
585 |
$dbi->insert(table => 'table1', param => {key1 => 6, key2 => 7, key3 => 8, key4 => 9, key5 => 10}); |
|
586 | ||
587 |
$param = {key2 => 11}; |
|
588 |
$update_param = $dbi->assign_param($param); |
|
589 |
$sql = <<"EOS"; |
|
590 |
update table1 set $update_param |
|
591 |
where key1 = 1 |
|
592 |
EOS |
|
593 |
$dbi->execute($sql, param => $param, table => 'table1'); |
|
594 |
$result = $dbi->execute('select * from table1;'); |
|
595 |
$rows = $result->all; |
|
596 |
is_deeply($rows, [{key1 => 1, key2 => 11, key3 => 3, key4 => 4, key5 => 5}, |
|
597 |
{key1 => 6, key2 => 7, key3 => 8, key4 => 9, key5 => 10}], |
|
598 |
"basic"); |
|
599 | ||
600 | ||
cleanup test
|
601 |
test 'type option'; # DEPRECATED! |
602 |
$dbi = DBIx::Custom->connect( |
|
603 |
data_source => 'dbi:SQLite:dbname=:memory:', |
|
604 |
dbi_option => { |
|
605 |
$DBD::SQLite::VERSION > 1.26 ? (sqlite_unicode => 1) : (unicode => 1) |
|
606 |
} |
|
cleanup test
|
607 |
); |
cleanup test
|
608 |
$binary = pack("I3", 1, 2, 3); |
609 |
eval { $dbi->execute('drop table table1') }; |
|
610 |
$dbi->execute('create table table1(key1, key2)'); |
|
611 |
$dbi->insert(table => 'table1', param => {key1 => $binary, key2 => 'あ'}, type => [key1 => DBI::SQL_BLOB]); |
|
612 |
$result = $dbi->select(table => 'table1'); |
|
613 |
$row = $result->one; |
|
614 |
is_deeply($row, {key1 => $binary, key2 => 'あ'}, "basic"); |
|
615 |
$result = $dbi->execute('select length(key1) as key1_length from table1'); |
|
616 |
$row = $result->one; |
|
617 |
is($row->{key1_length}, length $binary); |
|
cleanup test
|
618 | |
cleanup test
|
619 |
test 'type_rule from'; |
620 |
$dbi = DBIx::Custom->connect; |
|
621 |
$dbi->type_rule( |
|
622 |
from1 => { |
|
623 |
date => sub { uc $_[0] } |
|
624 |
} |
|
cleanup test
|
625 |
); |
cleanup test
|
626 |
$dbi->execute("create table table1 (key1 Date, key2 datetime)"); |
627 |
$dbi->insert({key1 => 'a'}, table => 'table1'); |
|
628 |
$result = $dbi->select(table => 'table1'); |
|
629 |
is($result->fetch_first->[0], 'A'); |
|
cleanup test
|
630 | |
cleanup test
|
631 |
$result = $dbi->select(table => 'table1'); |
632 |
is($result->one->{key1}, 'A'); |
|
cleanup test
|
633 | |
cleanup test
|
634 | |
635 |
test 'type_rule into'; |
|
test cleanup
|
636 |
$dbi = DBIx::Custom->connect; |
cleanup test
|
637 |
$dbi->execute("create table table1 (key1 Date, key2 datetime)"); |
638 |
$dbi->type_rule( |
|
639 |
into1 => { |
|
640 |
date => sub { uc $_[0] } |
|
641 |
} |
|
642 |
); |
|
cleanup test
|
643 |
$dbi->insert({key1 => 'a'}, table => 'table1'); |
cleanup test
|
644 |
$result = $dbi->select(table => 'table1'); |
645 |
is($result->one->{key1}, 'A'); |
|
646 | ||
test cleanup
|
647 |
$dbi = DBIx::Custom->connect; |
cleanup test
|
648 |
$dbi->execute("create table table1 (key1 date, key2 datetime)"); |
649 |
$dbi->type_rule( |
|
650 |
into1 => [ |
|
651 |
[qw/date datetime/] => sub { uc $_[0] } |
|
652 |
] |
|
653 |
); |
|
654 |
$dbi->insert({key1 => 'a', key2 => 'b'}, table => 'table1'); |
|
655 |
$result = $dbi->select(table => 'table1'); |
|
656 |
$row = $result->one; |
|
657 |
is($row->{key1}, 'A'); |
|
658 |
is($row->{key2}, 'B'); |
|
cleanup test
|
659 | |
test cleanup
|
660 |
$dbi = DBIx::Custom->connect; |
cleanup test
|
661 |
$dbi->execute("create table table1 (key1 Date, key2 datetime)"); |
662 |
$dbi->insert({key1 => 'a', key2 => 'B'}, table => 'table1'); |
|
663 |
$dbi->type_rule( |
|
664 |
into1 => [ |
|
665 |
[qw/date datetime/] => sub { uc $_[0] } |
|
666 |
] |
|
667 |
); |
|
668 |
$result = $dbi->execute( |
|
669 |
"select * from table1 where key1 = :key1 and key2 = :table1.key2;", |
|
670 |
param => {key1 => 'a', 'table1.key2' => 'b'} |
|
671 |
); |
|
672 |
$row = $result->one; |
|
673 |
is($row->{key1}, 'a'); |
|
674 |
is($row->{key2}, 'B'); |
|
cleanup test
|
675 | |
test cleanup
|
676 |
$dbi = DBIx::Custom->connect; |
cleanup test
|
677 |
$dbi->execute("create table table1 (key1 Date, key2 datetime)"); |
678 |
$dbi->insert({key1 => 'A', key2 => 'B'}, table => 'table1'); |
|
679 |
$dbi->type_rule( |
|
680 |
into1 => [ |
|
681 |
[qw/date datetime/] => sub { uc $_[0] } |
|
682 |
] |
|
683 |
); |
|
684 |
$result = $dbi->execute( |
|
685 |
"select * from table1 where key1 = :key1 and key2 = :table1.key2;", |
|
686 |
param => {key1 => 'a', 'table1.key2' => 'b'}, |
|
687 |
table => 'table1' |
|
688 |
); |
|
689 |
$row = $result->one; |
|
690 |
is($row->{key1}, 'A'); |
|
691 |
is($row->{key2}, 'B'); |
|
cleanup test
|
692 | |
cleanup test
|
693 |
$dbi = DBIx::Custom->connect; |
694 |
$dbi->execute("create table table1 (key1 date, key2 datetime)"); |
|
695 |
$dbi->register_filter(twice => sub { $_[0] * 2 }); |
|
696 |
$dbi->type_rule( |
|
697 |
from1 => { |
|
698 |
date => 'twice', |
|
699 |
}, |
|
700 |
into1 => { |
|
701 |
date => 'twice', |
|
702 |
} |
|
703 |
); |
|
704 |
$dbi->insert({key1 => 2}, table => 'table1'); |
|
705 |
$result = $dbi->select(table => 'table1'); |
|
706 |
is($result->fetch->[0], 8); |
|
cleanup test
|
707 | |
cleanup test
|
708 |
test 'type_rule and filter order'; |
test cleanup
|
709 |
$dbi = DBIx::Custom->connect; |
cleanup test
|
710 |
$dbi->execute("create table table1 (key1 Date, key2 datetime)"); |
711 |
$dbi->type_rule( |
|
712 |
into1 => { |
|
713 |
date => sub { $_[0] . 'b' } |
|
714 |
}, |
|
715 |
into2 => { |
|
716 |
date => sub { $_[0] . 'c' } |
|
717 |
}, |
|
718 |
from1 => { |
|
719 |
date => sub { $_[0] . 'd' } |
|
720 |
}, |
|
721 |
from2 => { |
|
722 |
date => sub { $_[0] . 'e' } |
|
723 |
} |
|
724 |
); |
|
725 |
$dbi->insert({key1 => '1'}, table => 'table1', filter => {key1 => sub { $_[0] . 'a' }}); |
|
726 |
$result = $dbi->select(table => 'table1'); |
|
727 |
$result->filter(key1 => sub { $_[0] . 'f' }); |
|
728 |
is($result->fetch_first->[0], '1abcdef'); |
|
cleanup test
|
729 | |
cleanup test
|
730 |
$dbi = DBIx::Custom->connect; |
731 |
$dbi->execute("create table table1 (key1 Date, key2 datetime)"); |
|
732 |
$dbi->type_rule( |
|
733 |
from1 => { |
|
734 |
date => sub { $_[0] . 'p' } |
|
735 |
}, |
|
736 |
from2 => { |
|
737 |
date => sub { $_[0] . 'q' } |
|
738 |
}, |
|
739 |
); |
|
740 |
$dbi->insert({key1 => '1'}, table => 'table1'); |
|
741 |
$result = $dbi->select(table => 'table1'); |
|
742 |
$result->type_rule( |
|
743 |
from1 => { |
|
744 |
date => sub { $_[0] . 'd' } |
|
745 |
}, |
|
746 |
from2 => { |
|
747 |
date => sub { $_[0] . 'e' } |
|
748 |
} |
|
749 |
); |
|
750 |
$result->filter(key1 => sub { $_[0] . 'f' }); |
|
751 |
is($result->fetch_first->[0], '1def'); |
|
cleanup test
|
752 | |
cleanup test
|
753 |
test 'type_rule_off'; |
754 |
$dbi = DBIx::Custom->connect; |
|
755 |
$dbi->execute("create table table1 (key1 Date, key2 datetime)"); |
|
756 |
$dbi->type_rule( |
|
757 |
from1 => { |
|
758 |
date => sub { $_[0] * 2 }, |
|
759 |
}, |
|
760 |
into1 => { |
|
761 |
date => sub { $_[0] * 2 }, |
|
762 |
} |
|
763 |
); |
|
764 |
$dbi->insert({key1 => 2}, table => 'table1', type_rule_off => 1); |
|
765 |
$result = $dbi->select(table => 'table1', type_rule_off => 1); |
|
766 |
is($result->type_rule_off->fetch->[0], 2); |
|
cleanup test
|
767 | |
cleanup test
|
768 |
$dbi = DBIx::Custom->connect; |
769 |
$dbi->execute("create table table1 (key1 Date, key2 datetime)"); |
|
770 |
$dbi->type_rule( |
|
771 |
from1 => { |
|
772 |
date => sub { $_[0] * 2 }, |
|
773 |
}, |
|
774 |
into1 => { |
|
775 |
date => sub { $_[0] * 3 }, |
|
776 |
} |
|
777 |
); |
|
778 |
$dbi->insert({key1 => 2}, table => 'table1', type_rule_off => 1); |
|
779 |
$result = $dbi->select(table => 'table1', type_rule_off => 1); |
|
780 |
is($result->one->{key1}, 4); |
|
cleanup test
|
781 | |
cleanup test
|
782 |
$dbi = DBIx::Custom->connect; |
783 |
$dbi->execute("create table table1 (key1 Date, key2 datetime)"); |
|
784 |
$dbi->type_rule( |
|
785 |
from1 => { |
|
786 |
date => sub { $_[0] * 2 }, |
|
787 |
}, |
|
788 |
into1 => { |
|
789 |
date => sub { $_[0] * 3 }, |
|
790 |
} |
|
791 |
); |
|
792 |
$dbi->insert({key1 => 2}, table => 'table1'); |
|
793 |
$result = $dbi->select(table => 'table1'); |
|
794 |
is($result->one->{key1}, 12); |
|
cleanup test
|
795 | |
cleanup test
|
796 |
$dbi = DBIx::Custom->connect; |
797 |
$dbi->execute("create table table1 (key1 Date, key2 datetime)"); |
|
798 |
$dbi->type_rule( |
|
799 |
from1 => { |
|
800 |
date => sub { $_[0] * 2 }, |
|
801 |
}, |
|
802 |
into1 => { |
|
803 |
date => sub { $_[0] * 3 }, |
|
804 |
} |
|
cleanup test
|
805 |
); |
cleanup test
|
806 |
$dbi->insert({key1 => 2}, table => 'table1'); |
807 |
$result = $dbi->select(table => 'table1'); |
|
808 |
is($result->fetch->[0], 12); |
|
cleanup test
|
809 | |
cleanup test
|
810 |
$dbi = DBIx::Custom->connect; |
811 |
$dbi->execute("create table table1 (key1 Date, key2 datetime)"); |
|
812 |
$dbi->register_filter(ppp => sub { uc $_[0] }); |
|
813 |
$dbi->type_rule( |
|
814 |
into1 => { |
|
815 |
date => 'ppp' |
|
cleanup test
|
816 |
} |
cleanup test
|
817 |
); |
818 |
$dbi->insert({key1 => 'a'}, table => 'table1'); |
|
819 |
$result = $dbi->select(table => 'table1'); |
|
820 |
is($result->one->{key1}, 'A'); |
|
cleanup test
|
821 | |
cleanup test
|
822 |
eval{$dbi->type_rule( |
823 |
into1 => { |
|
824 |
date => 'pp' |
|
cleanup test
|
825 |
} |
cleanup test
|
826 |
)}; |
827 |
like($@, qr/not registered/); |
|
cleanup test
|
828 | |
test cleanup
|
829 |
$dbi = DBIx::Custom->connect; |
cleanup test
|
830 |
$dbi->execute("create table table1 (key1 Date, key2 datetime)"); |
831 |
eval { |
|
832 |
$dbi->type_rule( |
|
833 |
from1 => { |
|
834 |
Date => sub { $_[0] * 2 }, |
|
835 |
} |
|
836 |
); |
|
837 |
}; |
|
838 |
like($@, qr/lower/); |
|
clenup test
|
839 | |
cleanup test
|
840 |
eval { |
841 |
$dbi->type_rule( |
|
842 |
into1 => { |
|
843 |
Date => sub { $_[0] * 2 }, |
|
844 |
} |
|
845 |
); |
|
846 |
}; |
|
847 |
like($@, qr/lower/); |
|
clenup test
|
848 | |
cleanup test
|
849 |
$dbi = DBIx::Custom->connect; |
850 |
$dbi->execute("create table table1 (key1 Date, key2 datetime)"); |
|
851 |
$dbi->type_rule( |
|
852 |
from1 => { |
|
853 |
date => sub { $_[0] * 2 }, |
|
854 |
}, |
|
855 |
into1 => { |
|
856 |
date => sub { $_[0] * 3 }, |
|
857 |
} |
|
858 |
); |
|
859 |
$dbi->insert({key1 => 2}, table => 'table1'); |
|
clenup test
|
860 |
$result = $dbi->select(table => 'table1'); |
cleanup test
|
861 |
$result->type_rule_off; |
862 |
is($result->one->{key1}, 6); |
|
clenup test
|
863 | |
cleanup test
|
864 |
$dbi = DBIx::Custom->connect; |
865 |
$dbi->execute("create table table1 (key1 Date, key2 datetime)"); |
|
866 |
$dbi->type_rule( |
|
867 |
from1 => { |
|
868 |
date => sub { $_[0] * 2 }, |
|
869 |
datetime => sub { $_[0] * 4 }, |
|
870 |
}, |
|
871 |
); |
|
872 |
$dbi->insert({key1 => 2, key2 => 2}, table => 'table1'); |
|
clenup test
|
873 |
$result = $dbi->select(table => 'table1'); |
cleanup test
|
874 |
$result->type_rule( |
875 |
from1 => { |
|
876 |
date => sub { $_[0] * 3 } |
|
877 |
} |
|
878 |
); |
|
879 |
$row = $result->one; |
|
880 |
is($row->{key1}, 6); |
|
881 |
is($row->{key2}, 2); |
|
clenup test
|
882 | |
883 |
$result = $dbi->select(table => 'table1'); |
|
cleanup test
|
884 |
$result->type_rule( |
885 |
from1 => { |
|
886 |
date => sub { $_[0] * 3 } |
|
887 |
} |
|
888 |
); |
|
889 |
$row = $result->one; |
|
890 |
is($row->{key1}, 6); |
|
891 |
is($row->{key2}, 2); |
|
clenup test
|
892 | |
893 |
$result = $dbi->select(table => 'table1'); |
|
cleanup test
|
894 |
$result->type_rule( |
895 |
from1 => { |
|
896 |
date => sub { $_[0] * 3 } |
|
897 |
} |
|
898 |
); |
|
899 |
$row = $result->one; |
|
900 |
is($row->{key1}, 6); |
|
901 |
is($row->{key2}, 2); |
|
clenup test
|
902 |
$result = $dbi->select(table => 'table1'); |
cleanup test
|
903 |
$result->type_rule( |
904 |
from1 => [date => sub { $_[0] * 3 }] |
|
905 |
); |
|
906 |
$row = $result->one; |
|
907 |
is($row->{key1}, 6); |
|
908 |
is($row->{key2}, 2); |
|
909 |
$dbi->register_filter(fivetimes => sub { $_[0] * 5}); |
|
clenup test
|
910 |
$result = $dbi->select(table => 'table1'); |
cleanup test
|
911 |
$result->type_rule( |
912 |
from1 => [date => 'fivetimes'] |
|
913 |
); |
|
914 |
$row = $result->one; |
|
915 |
is($row->{key1}, 10); |
|
916 |
is($row->{key2}, 2); |
|
clenup test
|
917 |
$result = $dbi->select(table => 'table1'); |
cleanup test
|
918 |
$result->type_rule( |
919 |
from1 => [date => undef] |
|
920 |
); |
|
921 |
$row = $result->one; |
|
922 |
is($row->{key1}, 2); |
|
923 |
is($row->{key2}, 2); |
|
clenup test
|
924 | |
test cleanup
|
925 |
$dbi = DBIx::Custom->connect; |
cleanup test
|
926 |
$dbi->execute("create table table1 (key1 Date, key2 datetime)"); |
927 |
$dbi->type_rule( |
|
928 |
from1 => { |
|
929 |
date => sub { $_[0] * 2 }, |
|
930 |
}, |
|
931 |
); |
|
932 |
$dbi->insert({key1 => 2}, table => 'table1'); |
|
clenup test
|
933 |
$result = $dbi->select(table => 'table1'); |
cleanup test
|
934 |
$result->filter(key1 => sub { $_[0] * 3 }); |
935 |
is($result->one->{key1}, 12); |
|
clenup test
|
936 | |
cleanup test
|
937 |
$dbi = DBIx::Custom->connect; |
938 |
$dbi->execute("create table table1 (key1 Date, key2 datetime)"); |
|
939 |
$dbi->type_rule( |
|
940 |
from1 => { |
|
941 |
date => sub { $_[0] * 2 }, |
|
942 |
}, |
|
943 |
); |
|
944 |
$dbi->insert({key1 => 2}, table => 'table1'); |
|
clenup test
|
945 |
$result = $dbi->select(table => 'table1'); |
cleanup test
|
946 |
$result->filter(key1 => sub { $_[0] * 3 }); |
947 |
is($result->fetch->[0], 12); |
|
clenup test
|
948 | |
cleanup test
|
949 |
$dbi = DBIx::Custom->connect; |
950 |
$dbi->execute("create table table1 (key1 Date, key2 datetime)"); |
|
951 |
$dbi->type_rule( |
|
952 |
into1 => { |
|
953 |
date => sub { $_[0] . 'b' } |
|
954 |
}, |
|
955 |
into2 => { |
|
956 |
date => sub { $_[0] . 'c' } |
|
957 |
}, |
|
958 |
from1 => { |
|
959 |
date => sub { $_[0] . 'd' } |
|
960 |
}, |
|
961 |
from2 => { |
|
962 |
date => sub { $_[0] . 'e' } |
|
963 |
} |
|
964 |
); |
|
965 |
$dbi->insert({key1 => '1'}, table => 'table1', type_rule_off => 1); |
|
clenup test
|
966 |
$result = $dbi->select(table => 'table1'); |
cleanup test
|
967 |
is($result->type_rule_off->fetch_first->[0], '1'); |
clenup test
|
968 |
$result = $dbi->select(table => 'table1'); |
cleanup test
|
969 |
is($result->type_rule_on->fetch_first->[0], '1de'); |
clenup test
|
970 | |
cleanup test
|
971 |
$dbi = DBIx::Custom->connect; |
972 |
$dbi->execute("create table table1 (key1 Date, key2 datetime)"); |
|
973 |
$dbi->type_rule( |
|
974 |
into1 => { |
|
975 |
date => sub { $_[0] . 'b' } |
|
cleanup test
|
976 |
}, |
cleanup test
|
977 |
into2 => { |
978 |
date => sub { $_[0] . 'c' } |
|
cleanup test
|
979 |
}, |
cleanup test
|
980 |
from1 => { |
981 |
date => sub { $_[0] . 'd' } |
|
cleanup test
|
982 |
}, |
cleanup test
|
983 |
from2 => { |
984 |
date => sub { $_[0] . 'e' } |
|
cleanup test
|
985 |
} |
986 |
); |
|
cleanup test
|
987 |
$dbi->insert({key1 => '1'}, table => 'table1', type_rule1_off => 1); |
988 |
$result = $dbi->select(table => 'table1'); |
|
989 |
is($result->type_rule1_off->fetch_first->[0], '1ce'); |
|
990 |
$result = $dbi->select(table => 'table1'); |
|
991 |
is($result->type_rule1_on->fetch_first->[0], '1cde'); |
|
cleanup test
|
992 | |
cleanup test
|
993 |
$dbi = DBIx::Custom->connect; |
994 |
$dbi->execute("create table table1 (key1 Date, key2 datetime)"); |
|
995 |
$dbi->type_rule( |
|
996 |
into1 => { |
|
997 |
date => sub { $_[0] . 'b' } |
|
998 |
}, |
|
999 |
into2 => { |
|
1000 |
date => sub { $_[0] . 'c' } |
|
1001 |
}, |
|
1002 |
from1 => { |
|
1003 |
date => sub { $_[0] . 'd' } |
|
1004 |
}, |
|
1005 |
from2 => { |
|
1006 |
date => sub { $_[0] . 'e' } |
|
cleanup test
|
1007 |
} |
1008 |
); |
|
cleanup test
|
1009 |
$dbi->insert({key1 => '1'}, table => 'table1', type_rule2_off => 1); |
1010 |
$result = $dbi->select(table => 'table1'); |
|
1011 |
is($result->type_rule2_off->fetch_first->[0], '1bd'); |
|
1012 |
$result = $dbi->select(table => 'table1'); |
|
1013 |
is($result->type_rule2_on->fetch_first->[0], '1bde'); |
|
test cleanup
|
1014 | |
1015 |
test 'prefix'; |
|
1016 |
$dbi = DBIx::Custom->connect; |
|
1017 |
eval { $dbi->execute('drop table table1') }; |
|
1018 |
$dbi->execute("create table table1 (key1 char(255), key2 char(255), primary key(key1))"); |
|
1019 |
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2}); |
|
1020 |
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 4}, prefix => 'or replace'); |
|
1021 |
$result = $dbi->execute('select * from table1;'); |
|
1022 |
$rows = $result->all; |
|
1023 |
is_deeply($rows, [{key1 => 1, key2 => 4}], "basic"); |
|
1024 | ||
1025 |
$dbi = DBIx::Custom->connect; |
|
1026 |
eval { $dbi->execute('drop table table1') }; |
|
1027 |
$dbi->execute("create table table1 (key1 char(255), key2 char(255), primary key(key1))"); |
|
1028 |
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2}); |
|
1029 |
$dbi->update(table => 'table1', param => {key2 => 4}, |
|
1030 |
where => {key1 => 1}, prefix => 'or replace'); |
|
1031 |
$result = $dbi->execute('select * from table1;'); |
|
1032 |
$rows = $result->all; |
|
1033 |
is_deeply($rows, [{key1 => 1, key2 => 4}], "basic"); |
|
1034 | ||
test cleanup
|
1035 |
test 'Model class'; |
1036 |
use MyDBI1; |
|
1037 |
$dbi = MyDBI1->connect; |
|
1038 |
eval { $dbi->execute('drop table book') }; |
|
1039 |
$dbi->execute("create table book (title, author)"); |
|
1040 |
$model = $dbi->model('book'); |
|
1041 |
$model->insert({title => 'a', author => 'b'}); |
|
1042 |
is_deeply($model->list->all, [{title => 'a', author => 'b'}], 'basic'); |
|
1043 |
$dbi->execute("create table company (name)"); |
|
1044 |
$model = $dbi->model('company'); |
|
1045 |
$model->insert({name => 'a'}); |
|
1046 |
is_deeply($model->list->all, [{name => 'a'}], 'basic'); |
|
1047 |
is($dbi->models->{'book'}, $dbi->model('book')); |
|
1048 |
is($dbi->models->{'company'}, $dbi->model('company')); |
|
1049 | ||
1050 |
$dbi = MyDBI4->connect; |
|
1051 |
eval { $dbi->execute('drop table book') }; |
|
1052 |
$dbi->execute("create table book (title, author)"); |
|
1053 |
$model = $dbi->model('book'); |
|
1054 |
$model->insert({title => 'a', author => 'b'}); |
|
1055 |
is_deeply($model->list->all, [{title => 'a', author => 'b'}], 'basic'); |
|
1056 |
$dbi->execute("create table company (name)"); |
|
1057 |
$model = $dbi->model('company'); |
|
1058 |
$model->insert({name => 'a'}); |
|
1059 |
is_deeply($model->list->all, [{name => 'a'}], 'basic'); |
|
test cleanup
|
1060 | |
test cleanup
|
1061 |
$dbi = MyDBI5->connect; |
1062 |
eval { $dbi->execute('drop table company') }; |
|
1063 |
eval { $dbi->execute('drop table table1') }; |
|
1064 |
$dbi->execute("create table company (name)"); |
|
1065 |
$dbi->execute("create table table1 (key1)"); |
|
1066 |
$model = $dbi->model('company'); |
|
1067 |
$model->insert({name => 'a'}); |
|
1068 |
is_deeply($model->list->all, [{name => 'a'}], 'include all model'); |
|
1069 |
$dbi->insert(table => 'table1', param => {key1 => 1}); |
|
1070 |
$model = $dbi->model('book'); |
|
1071 |
is_deeply($model->list->all, [{key1 => 1}], 'include all model'); |
|
test cleanup
|
1072 | |
test cleanup
|
1073 |
test 'primary_key'; |
1074 |
use MyDBI1; |
|
1075 |
$dbi = MyDBI1->connect; |
|
1076 |
$model = $dbi->model('book'); |
|
1077 |
$model->primary_key(['id', 'number']); |
|
1078 |
is_deeply($model->primary_key, ['id', 'number']); |
|
test cleanup
|
1079 | |
test cleanup
|
1080 |
test 'columns'; |
1081 |
use MyDBI1; |
|
1082 |
$dbi = MyDBI1->connect; |
|
1083 |
$model = $dbi->model('book'); |
|
1084 |
$model->columns(['id', 'number']); |
|
1085 |
is_deeply($model->columns, ['id', 'number']); |
|
test cleanup
|
1086 | |
test cleanup
|
1087 |
test 'setup_model'; |
1088 |
use MyDBI1; |
|
1089 |
$dbi = MyDBI1->connect; |
|
1090 |
eval { $dbi->execute('drop table book') }; |
|
1091 |
eval { $dbi->execute('drop table company') }; |
|
1092 |
eval { $dbi->execute('drop table test') }; |
|
test cleanup
|
1093 | |
test cleanup
|
1094 |
$dbi->execute('create table book (id)'); |
1095 |
$dbi->execute('create table company (id, name);'); |
|
1096 |
$dbi->execute('create table test (id, name, primary key (id, name));'); |
|
1097 |
$dbi->setup_model; |
|
1098 |
is_deeply($dbi->model('book')->columns, ['id']); |
|
1099 |
is_deeply($dbi->model('company')->columns, ['id', 'name']); |
|
test cleanup
|
1100 | |
1101 | ||
1102 | ||
1103 | ||
1104 | ||
1105 | ||
1106 | ||
1107 | ||
1108 | ||
1109 | ||
1110 | ||
1111 | ||
1112 | ||
1113 | ||
test cleanup
|
1114 |
### SQLite only test |
1115 |
test 'quote'; |
|
1116 |
$dbi = DBIx::Custom->connect; |
|
1117 |
$dbi->quote('"'); |
|
1118 |
eval { $dbi->execute("drop table ${q}table$p") }; |
|
1119 |
$dbi->execute($create_table_reserved); |
|
1120 |
$dbi->apply_filter('table', select => {out => sub { $_[0] * 2}}); |
|
1121 |
$dbi->insert(table => 'table', param => {select => 1}); |
|
1122 |
$dbi->delete(table => 'table', where => {select => 1}); |
|
1123 |
$result = $dbi->execute("select * from ${q}table$p"); |
|
1124 |
$rows = $result->all; |
|
1125 |
is_deeply($rows, [], "reserved word"); |
|
1126 | ||
test cleanup
|
1127 |
test 'finish statement handle'; |
1128 |
$dbi = DBIx::Custom->connect; |
|
1129 |
$dbi->execute($create_table1); |
|
1130 |
$dbi->insert({key1 => 1, key2 => 2}, table => 'table1'); |
|
1131 |
$dbi->insert({key1 => 3, key2 => 4}, table => 'table1'); |
|
1132 | ||
1133 |
$result = $dbi->select(table => 'table1'); |
|
1134 |
$row = $result->fetch_first; |
|
1135 |
is_deeply($row, [1, 2], "row"); |
|
1136 |
$row = $result->fetch; |
|
1137 |
ok(!$row, "finished"); |
|
1138 | ||
1139 |
$result = $dbi->select(table => 'table1'); |
|
1140 |
$row = $result->fetch_hash_first; |
|
1141 |
is_deeply($row, {key1 => 1, key2 => 2}, "row"); |
|
1142 |
$row = $result->fetch_hash; |
|
1143 |
ok(!$row, "finished"); |
|
1144 | ||
1145 |
$dbi->execute('create table table2 (key1, key2);'); |
|
1146 |
$result = $dbi->select(table => 'table2'); |
|
1147 |
$row = $result->fetch_hash_first; |
|
1148 |
ok(!$row, "no row fetch"); |
|
1149 | ||
1150 |
$dbi = DBIx::Custom->connect; |
|
1151 |
eval { $dbi->execute('drop table table1') }; |
|
1152 |
$dbi->execute($create_table1); |
|
1153 |
$dbi->insert({key1 => 1, key2 => 2}, table => 'table1'); |
|
1154 |
$dbi->insert({key1 => 3, key2 => 4}, table => 'table1'); |
|
1155 |
$dbi->insert({key1 => 5, key2 => 6}, table => 'table1'); |
|
1156 |
$dbi->insert({key1 => 7, key2 => 8}, table => 'table1'); |
|
1157 |
$dbi->insert({key1 => 9, key2 => 10}, table => 'table1'); |
|
1158 |
$result = $dbi->select(table => 'table1'); |
|
1159 |
$rows = $result->fetch_multi(2); |
|
1160 |
is_deeply($rows, [[1, 2], |
|
1161 |
[3, 4]], "fetch_multi first"); |
|
1162 |
$rows = $result->fetch_multi(2); |
|
1163 |
is_deeply($rows, [[5, 6], |
|
1164 |
[7, 8]], "fetch_multi secound"); |
|
1165 |
$rows = $result->fetch_multi(2); |
|
1166 |
is_deeply($rows, [[9, 10]], "fetch_multi third"); |
|
1167 |
$rows = $result->fetch_multi(2); |
|
1168 |
ok(!$rows); |
|
1169 | ||
1170 |
$result = $dbi->select(table => 'table1'); |
|
1171 |
eval {$result->fetch_multi}; |
|
1172 |
like($@, qr/Row count must be specified/, "Not specified row count"); |
|
1173 | ||
1174 |
$result = $dbi->select(table => 'table1'); |
|
1175 |
$rows = $result->fetch_hash_multi(2); |
|
1176 |
is_deeply($rows, [{key1 => 1, key2 => 2}, |
|
1177 |
{key1 => 3, key2 => 4}], "fetch_multi first"); |
|
1178 |
$rows = $result->fetch_hash_multi(2); |
|
1179 |
is_deeply($rows, [{key1 => 5, key2 => 6}, |
|
1180 |
{key1 => 7, key2 => 8}], "fetch_multi secound"); |
|
1181 |
$rows = $result->fetch_hash_multi(2); |
|
1182 |
is_deeply($rows, [{key1 => 9, key2 => 10}], "fetch_multi third"); |
|
1183 |
$rows = $result->fetch_hash_multi(2); |
|
1184 |
ok(!$rows); |
|
1185 | ||
1186 |
$result = $dbi->select(table => 'table1'); |
|
1187 |
eval {$result->fetch_hash_multi}; |
|
1188 |
like($@, qr/Row count must be specified/, "Not specified row count"); |
|
1189 | ||
1190 | ||
1191 | ||
test cleanup
|
1192 | |
1193 | ||
1194 | ||
1195 | ||
1196 | ||
1197 | ||
test cleanup
|
1198 | |
1199 | ||
1200 | ||
1201 | ||
1202 | ||
1203 | ||
1204 | ||
1205 | ||
1206 | ||
1207 | ||
1208 |
# DEPRECATED! test |
|
1209 |
test 'filter __ expression'; |
|
1210 |
$dbi = DBIx::Custom->connect; |
|
1211 |
eval { $dbi->execute('drop table company') }; |
|
1212 |
eval { $dbi->execute('drop table location') }; |
|
1213 |
$dbi->execute('create table company (id, name, location_id)'); |
|
1214 |
$dbi->execute('create table location (id, name)'); |
|
1215 |
$dbi->apply_filter('location', |
|
1216 |
name => {in => sub { uc $_[0] } } |
|
1217 |
); |
|
1218 | ||
1219 |
$dbi->insert(table => 'company', param => {id => 1, name => 'a', location_id => 2}); |
|
1220 |
$dbi->insert(table => 'location', param => {id => 2, name => 'b'}); |
|
1221 | ||
1222 |
$result = $dbi->select( |
|
1223 |
table => ['company', 'location'], relation => {'company.location_id' => 'location.id'}, |
|
1224 |
column => ['location.name as location__name'] |
|
1225 |
); |
|
1226 |
is($result->fetch_first->[0], 'B'); |
|
1227 | ||
1228 |
$result = $dbi->select( |
|
1229 |
table => 'company', relation => {'company.location_id' => 'location.id'}, |
|
1230 |
column => ['location.name as location__name'] |
|
1231 |
); |
|
1232 |
is($result->fetch_first->[0], 'B'); |
|
1233 | ||
1234 |
$result = $dbi->select( |
|
1235 |
table => 'company', relation => {'company.location_id' => 'location.id'}, |
|
1236 |
column => ['location.name as "location.name"'] |
|
1237 |
); |
|
1238 |
is($result->fetch_first->[0], 'B'); |
|
test cleanup
|
1239 | |
1240 |
test 'reserved_word_quote'; |
|
1241 |
$dbi = DBIx::Custom->connect; |
|
1242 |
eval { $dbi->execute("drop table ${q}table$p") }; |
|
1243 |
$dbi->reserved_word_quote('"'); |
|
1244 |
$dbi->execute($create_table_reserved); |
|
1245 |
$dbi->apply_filter('table', select => {out => sub { $_[0] * 2}}); |
|
1246 |
$dbi->apply_filter('table', update => {out => sub { $_[0] * 3}}); |
|
1247 |
$dbi->insert(table => 'table', param => {select => 1}); |
|
1248 |
$dbi->update(table => 'table', where => {'table.select' => 1}, param => {update => 2}); |
|
1249 |
$result = $dbi->execute("select * from ${q}table$p"); |
|
1250 |
$rows = $result->all; |
|
1251 |
is_deeply($rows, [{select => 2, update => 6}], "reserved word"); |