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 varchar, key2 varchar);'; |
155 |
my $create_table1_2 = 'create table table1 (key1 varchar, key2 varchar, key3 varchar, key4 varchar, key5 varchar);'; |
|
156 |
my $create_table2 = 'create table table2 (key1 varchar, key3 varchar);'; |
|
157 |
my $create_table2_2 = "create table table2 (key1 varchar, key2 varchar, key3 varchar)"; |
|
158 |
my $create_table3 = "create table table3 (key1 varchar, key2 varchar, key3 varchar)"; |
|
159 |
my $create_table_reserved = 'create table "table" ("select" varchar, "update" varchar)'; |
|
test cleanup
|
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 | |
test cleanup
|
199 |
### a little complex test |
cleanup test
|
200 |
test 'table_alias'; |
201 |
$dbi = DBIx::Custom->connect; |
|
202 |
eval { $dbi->execute('drop table table1') }; |
|
203 |
$dbi->execute("create table table1 (key1 Date, key2 datetime)"); |
|
204 |
$dbi->type_rule( |
|
205 |
into1 => { |
|
206 |
date => sub { uc $_[0] } |
|
207 |
} |
|
208 |
); |
|
209 |
$dbi->execute("insert into table1 (key1) values (:table2.key1)", {'table2.key1' => 'a'}, |
|
210 |
table_alias => {table2 => 'table1'}); |
|
211 |
$result = $dbi->select(table => 'table1'); |
|
212 |
is($result->one->{key1}, 'A'); |
|
213 | ||
cleanup test
|
214 |
test 'type_rule into'; |
test cleanup
|
215 |
$dbi = DBIx::Custom->connect; |
cleanup test
|
216 |
$dbi->execute("create table table1 (key1 Date, key2 datetime)"); |
217 |
$dbi->type_rule( |
|
218 |
into1 => { |
|
219 |
date => sub { uc $_[0] } |
|
220 |
} |
|
221 |
); |
|
cleanup test
|
222 |
$dbi->insert({key1 => 'a'}, table => 'table1'); |
cleanup test
|
223 |
$result = $dbi->select(table => 'table1'); |
224 |
is($result->one->{key1}, 'A'); |
|
225 | ||
test cleanup
|
226 |
$dbi = DBIx::Custom->connect; |
cleanup test
|
227 |
$dbi->execute("create table table1 (key1 date, key2 datetime)"); |
228 |
$dbi->type_rule( |
|
229 |
into1 => [ |
|
230 |
[qw/date datetime/] => sub { uc $_[0] } |
|
231 |
] |
|
232 |
); |
|
233 |
$dbi->insert({key1 => 'a', key2 => 'b'}, table => 'table1'); |
|
234 |
$result = $dbi->select(table => 'table1'); |
|
235 |
$row = $result->one; |
|
236 |
is($row->{key1}, 'A'); |
|
237 |
is($row->{key2}, 'B'); |
|
cleanup test
|
238 | |
test cleanup
|
239 |
$dbi = DBIx::Custom->connect; |
cleanup test
|
240 |
$dbi->execute("create table table1 (key1 Date, key2 datetime)"); |
241 |
$dbi->insert({key1 => 'a', key2 => 'B'}, table => 'table1'); |
|
242 |
$dbi->type_rule( |
|
243 |
into1 => [ |
|
244 |
[qw/date datetime/] => sub { uc $_[0] } |
|
245 |
] |
|
246 |
); |
|
247 |
$result = $dbi->execute( |
|
248 |
"select * from table1 where key1 = :key1 and key2 = :table1.key2;", |
|
249 |
param => {key1 => 'a', 'table1.key2' => 'b'} |
|
250 |
); |
|
251 |
$row = $result->one; |
|
252 |
is($row->{key1}, 'a'); |
|
253 |
is($row->{key2}, 'B'); |
|
cleanup test
|
254 | |
test cleanup
|
255 |
$dbi = DBIx::Custom->connect; |
cleanup test
|
256 |
$dbi->execute("create table table1 (key1 Date, key2 datetime)"); |
257 |
$dbi->insert({key1 => 'A', key2 => 'B'}, table => 'table1'); |
|
258 |
$dbi->type_rule( |
|
259 |
into1 => [ |
|
260 |
[qw/date datetime/] => sub { uc $_[0] } |
|
261 |
] |
|
262 |
); |
|
263 |
$result = $dbi->execute( |
|
264 |
"select * from table1 where key1 = :key1 and key2 = :table1.key2;", |
|
265 |
param => {key1 => 'a', 'table1.key2' => 'b'}, |
|
266 |
table => 'table1' |
|
267 |
); |
|
268 |
$row = $result->one; |
|
269 |
is($row->{key1}, 'A'); |
|
270 |
is($row->{key2}, 'B'); |
|
cleanup test
|
271 | |
cleanup test
|
272 |
$dbi = DBIx::Custom->connect; |
273 |
$dbi->execute("create table table1 (key1 date, key2 datetime)"); |
|
274 |
$dbi->register_filter(twice => sub { $_[0] * 2 }); |
|
275 |
$dbi->type_rule( |
|
276 |
from1 => { |
|
277 |
date => 'twice', |
|
278 |
}, |
|
279 |
into1 => { |
|
280 |
date => 'twice', |
|
281 |
} |
|
282 |
); |
|
283 |
$dbi->insert({key1 => 2}, table => 'table1'); |
|
284 |
$result = $dbi->select(table => 'table1'); |
|
285 |
is($result->fetch->[0], 8); |
|
cleanup test
|
286 | |
cleanup test
|
287 |
test 'type_rule and filter order'; |
test cleanup
|
288 |
$dbi = DBIx::Custom->connect; |
cleanup test
|
289 |
$dbi->execute("create table table1 (key1 Date, key2 datetime)"); |
290 |
$dbi->type_rule( |
|
291 |
into1 => { |
|
292 |
date => sub { $_[0] . 'b' } |
|
293 |
}, |
|
294 |
into2 => { |
|
295 |
date => sub { $_[0] . 'c' } |
|
296 |
}, |
|
297 |
from1 => { |
|
298 |
date => sub { $_[0] . 'd' } |
|
299 |
}, |
|
300 |
from2 => { |
|
301 |
date => sub { $_[0] . 'e' } |
|
302 |
} |
|
303 |
); |
|
304 |
$dbi->insert({key1 => '1'}, table => 'table1', filter => {key1 => sub { $_[0] . 'a' }}); |
|
305 |
$result = $dbi->select(table => 'table1'); |
|
306 |
$result->filter(key1 => sub { $_[0] . 'f' }); |
|
307 |
is($result->fetch_first->[0], '1abcdef'); |
|
cleanup test
|
308 | |
cleanup test
|
309 |
$dbi = DBIx::Custom->connect; |
310 |
$dbi->execute("create table table1 (key1 Date, key2 datetime)"); |
|
311 |
$dbi->type_rule( |
|
312 |
from1 => { |
|
313 |
date => sub { $_[0] . 'p' } |
|
314 |
}, |
|
315 |
from2 => { |
|
316 |
date => sub { $_[0] . 'q' } |
|
317 |
}, |
|
318 |
); |
|
319 |
$dbi->insert({key1 => '1'}, table => 'table1'); |
|
320 |
$result = $dbi->select(table => 'table1'); |
|
321 |
$result->type_rule( |
|
322 |
from1 => { |
|
323 |
date => sub { $_[0] . 'd' } |
|
324 |
}, |
|
325 |
from2 => { |
|
326 |
date => sub { $_[0] . 'e' } |
|
327 |
} |
|
328 |
); |
|
329 |
$result->filter(key1 => sub { $_[0] . 'f' }); |
|
330 |
is($result->fetch_first->[0], '1def'); |
|
cleanup test
|
331 | |
cleanup test
|
332 |
test 'type_rule_off'; |
333 |
$dbi = DBIx::Custom->connect; |
|
334 |
$dbi->execute("create table table1 (key1 Date, key2 datetime)"); |
|
335 |
$dbi->type_rule( |
|
336 |
from1 => { |
|
337 |
date => sub { $_[0] * 2 }, |
|
338 |
}, |
|
339 |
into1 => { |
|
340 |
date => sub { $_[0] * 2 }, |
|
341 |
} |
|
342 |
); |
|
343 |
$dbi->insert({key1 => 2}, table => 'table1', type_rule_off => 1); |
|
344 |
$result = $dbi->select(table => 'table1', type_rule_off => 1); |
|
345 |
is($result->type_rule_off->fetch->[0], 2); |
|
cleanup test
|
346 | |
cleanup test
|
347 |
$dbi = DBIx::Custom->connect; |
348 |
$dbi->execute("create table table1 (key1 Date, key2 datetime)"); |
|
349 |
$dbi->type_rule( |
|
350 |
from1 => { |
|
351 |
date => sub { $_[0] * 2 }, |
|
352 |
}, |
|
353 |
into1 => { |
|
354 |
date => sub { $_[0] * 3 }, |
|
355 |
} |
|
356 |
); |
|
357 |
$dbi->insert({key1 => 2}, table => 'table1', type_rule_off => 1); |
|
358 |
$result = $dbi->select(table => 'table1', type_rule_off => 1); |
|
359 |
is($result->one->{key1}, 4); |
|
cleanup test
|
360 | |
cleanup test
|
361 |
$dbi = DBIx::Custom->connect; |
362 |
$dbi->execute("create table table1 (key1 Date, key2 datetime)"); |
|
363 |
$dbi->type_rule( |
|
364 |
from1 => { |
|
365 |
date => sub { $_[0] * 2 }, |
|
366 |
}, |
|
367 |
into1 => { |
|
368 |
date => sub { $_[0] * 3 }, |
|
369 |
} |
|
370 |
); |
|
371 |
$dbi->insert({key1 => 2}, table => 'table1'); |
|
372 |
$result = $dbi->select(table => 'table1'); |
|
373 |
is($result->one->{key1}, 12); |
|
cleanup test
|
374 | |
cleanup test
|
375 |
$dbi = DBIx::Custom->connect; |
376 |
$dbi->execute("create table table1 (key1 Date, key2 datetime)"); |
|
377 |
$dbi->type_rule( |
|
378 |
from1 => { |
|
379 |
date => sub { $_[0] * 2 }, |
|
380 |
}, |
|
381 |
into1 => { |
|
382 |
date => sub { $_[0] * 3 }, |
|
383 |
} |
|
cleanup test
|
384 |
); |
cleanup test
|
385 |
$dbi->insert({key1 => 2}, table => 'table1'); |
386 |
$result = $dbi->select(table => 'table1'); |
|
387 |
is($result->fetch->[0], 12); |
|
cleanup test
|
388 | |
cleanup test
|
389 |
$dbi = DBIx::Custom->connect; |
390 |
$dbi->execute("create table table1 (key1 Date, key2 datetime)"); |
|
391 |
$dbi->register_filter(ppp => sub { uc $_[0] }); |
|
392 |
$dbi->type_rule( |
|
393 |
into1 => { |
|
394 |
date => 'ppp' |
|
cleanup test
|
395 |
} |
cleanup test
|
396 |
); |
397 |
$dbi->insert({key1 => 'a'}, table => 'table1'); |
|
398 |
$result = $dbi->select(table => 'table1'); |
|
399 |
is($result->one->{key1}, 'A'); |
|
cleanup test
|
400 | |
cleanup test
|
401 |
eval{$dbi->type_rule( |
402 |
into1 => { |
|
403 |
date => 'pp' |
|
cleanup test
|
404 |
} |
cleanup test
|
405 |
)}; |
406 |
like($@, qr/not registered/); |
|
cleanup test
|
407 | |
test cleanup
|
408 |
$dbi = DBIx::Custom->connect; |
cleanup test
|
409 |
$dbi->execute("create table table1 (key1 Date, key2 datetime)"); |
410 |
eval { |
|
411 |
$dbi->type_rule( |
|
412 |
from1 => { |
|
413 |
Date => sub { $_[0] * 2 }, |
|
414 |
} |
|
415 |
); |
|
416 |
}; |
|
417 |
like($@, qr/lower/); |
|
clenup test
|
418 | |
cleanup test
|
419 |
eval { |
420 |
$dbi->type_rule( |
|
421 |
into1 => { |
|
422 |
Date => sub { $_[0] * 2 }, |
|
423 |
} |
|
424 |
); |
|
425 |
}; |
|
426 |
like($@, qr/lower/); |
|
clenup test
|
427 | |
cleanup test
|
428 |
$dbi = DBIx::Custom->connect; |
429 |
$dbi->execute("create table table1 (key1 Date, key2 datetime)"); |
|
430 |
$dbi->type_rule( |
|
431 |
from1 => { |
|
432 |
date => sub { $_[0] * 2 }, |
|
433 |
}, |
|
434 |
into1 => { |
|
435 |
date => sub { $_[0] * 3 }, |
|
436 |
} |
|
437 |
); |
|
438 |
$dbi->insert({key1 => 2}, table => 'table1'); |
|
clenup test
|
439 |
$result = $dbi->select(table => 'table1'); |
cleanup test
|
440 |
$result->type_rule_off; |
441 |
is($result->one->{key1}, 6); |
|
clenup test
|
442 | |
cleanup test
|
443 |
$dbi = DBIx::Custom->connect; |
444 |
$dbi->execute("create table table1 (key1 Date, key2 datetime)"); |
|
445 |
$dbi->type_rule( |
|
446 |
from1 => { |
|
447 |
date => sub { $_[0] * 2 }, |
|
448 |
datetime => sub { $_[0] * 4 }, |
|
449 |
}, |
|
450 |
); |
|
451 |
$dbi->insert({key1 => 2, key2 => 2}, table => 'table1'); |
|
clenup test
|
452 |
$result = $dbi->select(table => 'table1'); |
cleanup test
|
453 |
$result->type_rule( |
454 |
from1 => { |
|
455 |
date => sub { $_[0] * 3 } |
|
456 |
} |
|
457 |
); |
|
458 |
$row = $result->one; |
|
459 |
is($row->{key1}, 6); |
|
460 |
is($row->{key2}, 2); |
|
clenup test
|
461 | |
462 |
$result = $dbi->select(table => 'table1'); |
|
cleanup test
|
463 |
$result->type_rule( |
464 |
from1 => { |
|
465 |
date => sub { $_[0] * 3 } |
|
466 |
} |
|
467 |
); |
|
468 |
$row = $result->one; |
|
469 |
is($row->{key1}, 6); |
|
470 |
is($row->{key2}, 2); |
|
clenup test
|
471 | |
472 |
$result = $dbi->select(table => 'table1'); |
|
cleanup test
|
473 |
$result->type_rule( |
474 |
from1 => { |
|
475 |
date => sub { $_[0] * 3 } |
|
476 |
} |
|
477 |
); |
|
478 |
$row = $result->one; |
|
479 |
is($row->{key1}, 6); |
|
480 |
is($row->{key2}, 2); |
|
clenup test
|
481 |
$result = $dbi->select(table => 'table1'); |
cleanup test
|
482 |
$result->type_rule( |
483 |
from1 => [date => sub { $_[0] * 3 }] |
|
484 |
); |
|
485 |
$row = $result->one; |
|
486 |
is($row->{key1}, 6); |
|
487 |
is($row->{key2}, 2); |
|
488 |
$dbi->register_filter(fivetimes => sub { $_[0] * 5}); |
|
clenup test
|
489 |
$result = $dbi->select(table => 'table1'); |
cleanup test
|
490 |
$result->type_rule( |
491 |
from1 => [date => 'fivetimes'] |
|
492 |
); |
|
493 |
$row = $result->one; |
|
494 |
is($row->{key1}, 10); |
|
495 |
is($row->{key2}, 2); |
|
clenup test
|
496 |
$result = $dbi->select(table => 'table1'); |
cleanup test
|
497 |
$result->type_rule( |
498 |
from1 => [date => undef] |
|
499 |
); |
|
500 |
$row = $result->one; |
|
501 |
is($row->{key1}, 2); |
|
502 |
is($row->{key2}, 2); |
|
clenup test
|
503 | |
test cleanup
|
504 |
$dbi = DBIx::Custom->connect; |
cleanup test
|
505 |
$dbi->execute("create table table1 (key1 Date, key2 datetime)"); |
506 |
$dbi->type_rule( |
|
507 |
from1 => { |
|
508 |
date => sub { $_[0] * 2 }, |
|
509 |
}, |
|
510 |
); |
|
511 |
$dbi->insert({key1 => 2}, table => 'table1'); |
|
clenup test
|
512 |
$result = $dbi->select(table => 'table1'); |
cleanup test
|
513 |
$result->filter(key1 => sub { $_[0] * 3 }); |
514 |
is($result->one->{key1}, 12); |
|
clenup test
|
515 | |
cleanup test
|
516 |
$dbi = DBIx::Custom->connect; |
517 |
$dbi->execute("create table table1 (key1 Date, key2 datetime)"); |
|
518 |
$dbi->type_rule( |
|
519 |
from1 => { |
|
520 |
date => sub { $_[0] * 2 }, |
|
521 |
}, |
|
522 |
); |
|
523 |
$dbi->insert({key1 => 2}, table => 'table1'); |
|
clenup test
|
524 |
$result = $dbi->select(table => 'table1'); |
cleanup test
|
525 |
$result->filter(key1 => sub { $_[0] * 3 }); |
526 |
is($result->fetch->[0], 12); |
|
clenup test
|
527 | |
cleanup test
|
528 |
$dbi = DBIx::Custom->connect; |
529 |
$dbi->execute("create table table1 (key1 Date, key2 datetime)"); |
|
530 |
$dbi->type_rule( |
|
531 |
into1 => { |
|
532 |
date => sub { $_[0] . 'b' } |
|
533 |
}, |
|
534 |
into2 => { |
|
535 |
date => sub { $_[0] . 'c' } |
|
536 |
}, |
|
537 |
from1 => { |
|
538 |
date => sub { $_[0] . 'd' } |
|
539 |
}, |
|
540 |
from2 => { |
|
541 |
date => sub { $_[0] . 'e' } |
|
542 |
} |
|
543 |
); |
|
544 |
$dbi->insert({key1 => '1'}, table => 'table1', type_rule_off => 1); |
|
clenup test
|
545 |
$result = $dbi->select(table => 'table1'); |
cleanup test
|
546 |
is($result->type_rule_off->fetch_first->[0], '1'); |
clenup test
|
547 |
$result = $dbi->select(table => 'table1'); |
cleanup test
|
548 |
is($result->type_rule_on->fetch_first->[0], '1de'); |
clenup test
|
549 | |
cleanup test
|
550 |
$dbi = DBIx::Custom->connect; |
551 |
$dbi->execute("create table table1 (key1 Date, key2 datetime)"); |
|
552 |
$dbi->type_rule( |
|
553 |
into1 => { |
|
554 |
date => sub { $_[0] . 'b' } |
|
cleanup test
|
555 |
}, |
cleanup test
|
556 |
into2 => { |
557 |
date => sub { $_[0] . 'c' } |
|
cleanup test
|
558 |
}, |
cleanup test
|
559 |
from1 => { |
560 |
date => sub { $_[0] . 'd' } |
|
cleanup test
|
561 |
}, |
cleanup test
|
562 |
from2 => { |
563 |
date => sub { $_[0] . 'e' } |
|
cleanup test
|
564 |
} |
565 |
); |
|
cleanup test
|
566 |
$dbi->insert({key1 => '1'}, table => 'table1', type_rule1_off => 1); |
567 |
$result = $dbi->select(table => 'table1'); |
|
568 |
is($result->type_rule1_off->fetch_first->[0], '1ce'); |
|
569 |
$result = $dbi->select(table => 'table1'); |
|
570 |
is($result->type_rule1_on->fetch_first->[0], '1cde'); |
|
cleanup test
|
571 | |
cleanup test
|
572 |
$dbi = DBIx::Custom->connect; |
573 |
$dbi->execute("create table table1 (key1 Date, key2 datetime)"); |
|
574 |
$dbi->type_rule( |
|
575 |
into1 => { |
|
576 |
date => sub { $_[0] . 'b' } |
|
577 |
}, |
|
578 |
into2 => { |
|
579 |
date => sub { $_[0] . 'c' } |
|
580 |
}, |
|
581 |
from1 => { |
|
582 |
date => sub { $_[0] . 'd' } |
|
583 |
}, |
|
584 |
from2 => { |
|
585 |
date => sub { $_[0] . 'e' } |
|
cleanup test
|
586 |
} |
587 |
); |
|
cleanup test
|
588 |
$dbi->insert({key1 => '1'}, table => 'table1', type_rule2_off => 1); |
589 |
$result = $dbi->select(table => 'table1'); |
|
590 |
is($result->type_rule2_off->fetch_first->[0], '1bd'); |
|
591 |
$result = $dbi->select(table => 'table1'); |
|
592 |
is($result->type_rule2_on->fetch_first->[0], '1bde'); |
|
test cleanup
|
593 | |
test cleanup
|
594 |
test 'Model class'; |
595 |
use MyDBI1; |
|
596 |
$dbi = MyDBI1->connect; |
|
597 |
eval { $dbi->execute('drop table book') }; |
|
598 |
$dbi->execute("create table book (title, author)"); |
|
599 |
$model = $dbi->model('book'); |
|
600 |
$model->insert({title => 'a', author => 'b'}); |
|
601 |
is_deeply($model->list->all, [{title => 'a', author => 'b'}], 'basic'); |
|
602 |
$dbi->execute("create table company (name)"); |
|
603 |
$model = $dbi->model('company'); |
|
604 |
$model->insert({name => 'a'}); |
|
605 |
is_deeply($model->list->all, [{name => 'a'}], 'basic'); |
|
606 |
is($dbi->models->{'book'}, $dbi->model('book')); |
|
607 |
is($dbi->models->{'company'}, $dbi->model('company')); |
|
608 | ||
609 |
$dbi = MyDBI4->connect; |
|
610 |
eval { $dbi->execute('drop table book') }; |
|
611 |
$dbi->execute("create table book (title, author)"); |
|
612 |
$model = $dbi->model('book'); |
|
613 |
$model->insert({title => 'a', author => 'b'}); |
|
614 |
is_deeply($model->list->all, [{title => 'a', author => 'b'}], 'basic'); |
|
615 |
$dbi->execute("create table company (name)"); |
|
616 |
$model = $dbi->model('company'); |
|
617 |
$model->insert({name => 'a'}); |
|
618 |
is_deeply($model->list->all, [{name => 'a'}], 'basic'); |
|
test cleanup
|
619 | |
test cleanup
|
620 |
$dbi = MyDBI5->connect; |
621 |
eval { $dbi->execute('drop table company') }; |
|
622 |
eval { $dbi->execute('drop table table1') }; |
|
623 |
$dbi->execute("create table company (name)"); |
|
624 |
$dbi->execute("create table table1 (key1)"); |
|
625 |
$model = $dbi->model('company'); |
|
626 |
$model->insert({name => 'a'}); |
|
627 |
is_deeply($model->list->all, [{name => 'a'}], 'include all model'); |
|
628 |
$dbi->insert(table => 'table1', param => {key1 => 1}); |
|
629 |
$model = $dbi->model('book'); |
|
630 |
is_deeply($model->list->all, [{key1 => 1}], 'include all model'); |
|
test cleanup
|
631 | |
test cleanup
|
632 |
test 'primary_key'; |
633 |
use MyDBI1; |
|
634 |
$dbi = MyDBI1->connect; |
|
635 |
$model = $dbi->model('book'); |
|
636 |
$model->primary_key(['id', 'number']); |
|
637 |
is_deeply($model->primary_key, ['id', 'number']); |
|
test cleanup
|
638 | |
test cleanup
|
639 |
test 'columns'; |
640 |
use MyDBI1; |
|
641 |
$dbi = MyDBI1->connect; |
|
642 |
$model = $dbi->model('book'); |
|
643 |
$model->columns(['id', 'number']); |
|
644 |
is_deeply($model->columns, ['id', 'number']); |
|
test cleanup
|
645 | |
test cleanup
|
646 |
test 'setup_model'; |
647 |
use MyDBI1; |
|
648 |
$dbi = MyDBI1->connect; |
|
649 |
eval { $dbi->execute('drop table book') }; |
|
650 |
eval { $dbi->execute('drop table company') }; |
|
651 |
eval { $dbi->execute('drop table test') }; |
|
test cleanup
|
652 | |
test cleanup
|
653 |
$dbi->execute('create table book (id)'); |
654 |
$dbi->execute('create table company (id, name);'); |
|
test cleanup
|
655 |
$dbi->execute('create table test (id, name);'); |
test cleanup
|
656 |
$dbi->setup_model; |
657 |
is_deeply($dbi->model('book')->columns, ['id']); |
|
658 |
is_deeply($dbi->model('company')->columns, ['id', 'name']); |
|
test cleanup
|
659 | |
660 | ||
661 | ||
662 | ||
663 | ||
664 | ||
665 | ||
666 | ||
667 | ||
668 | ||
669 | ||
test cleanup
|
670 |
### SQLite only test |
671 |
test 'prefix'; |
|
672 |
$dbi = DBIx::Custom->connect; |
|
673 |
eval { $dbi->execute('drop table table1') }; |
|
674 |
$dbi->execute('create table table1 (key1 varchar, key2 varchar, primary key(key1));'); |
|
675 |
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2}); |
|
676 |
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 4}, prefix => 'or replace'); |
|
677 |
$result = $dbi->execute('select * from table1;'); |
|
678 |
$rows = $result->all; |
|
679 |
is_deeply($rows, [{key1 => 1, key2 => 4}], "basic"); |
|
test cleanup
|
680 | |
test cleanup
|
681 |
$dbi = DBIx::Custom->connect; |
682 |
eval { $dbi->execute('drop table table1') }; |
|
683 |
$dbi->execute('create table table1 (key1 varchar, key2 varchar, primary key(key1));'); |
|
684 |
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2}); |
|
685 |
$dbi->update(table => 'table1', param => {key2 => 4}, |
|
686 |
where => {key1 => 1}, prefix => 'or replace'); |
|
687 |
$result = $dbi->execute('select * from table1;'); |
|
688 |
$rows = $result->all; |
|
689 |
is_deeply($rows, [{key1 => 1, key2 => 4}], "basic"); |
|
test cleanup
|
690 | |
691 | ||
test cleanup
|
692 |
test 'quote'; |
693 |
$dbi = DBIx::Custom->connect; |
|
694 |
$dbi->quote('"'); |
|
695 |
eval { $dbi->execute("drop table ${q}table$p") }; |
|
696 |
$dbi->execute($create_table_reserved); |
|
697 |
$dbi->apply_filter('table', select => {out => sub { $_[0] * 2}}); |
|
698 |
$dbi->insert(table => 'table', param => {select => 1}); |
|
699 |
$dbi->delete(table => 'table', where => {select => 1}); |
|
700 |
$result = $dbi->execute("select * from ${q}table$p"); |
|
701 |
$rows = $result->all; |
|
702 |
is_deeply($rows, [], "reserved word"); |
|
703 | ||
test cleanup
|
704 |
test 'finish statement handle'; |
705 |
$dbi = DBIx::Custom->connect; |
|
706 |
$dbi->execute($create_table1); |
|
707 |
$dbi->insert({key1 => 1, key2 => 2}, table => 'table1'); |
|
708 |
$dbi->insert({key1 => 3, key2 => 4}, table => 'table1'); |
|
709 | ||
710 |
$result = $dbi->select(table => 'table1'); |
|
711 |
$row = $result->fetch_first; |
|
712 |
is_deeply($row, [1, 2], "row"); |
|
713 |
$row = $result->fetch; |
|
714 |
ok(!$row, "finished"); |
|
715 | ||
716 |
$result = $dbi->select(table => 'table1'); |
|
717 |
$row = $result->fetch_hash_first; |
|
718 |
is_deeply($row, {key1 => 1, key2 => 2}, "row"); |
|
719 |
$row = $result->fetch_hash; |
|
720 |
ok(!$row, "finished"); |
|
721 | ||
722 |
$dbi->execute('create table table2 (key1, key2);'); |
|
723 |
$result = $dbi->select(table => 'table2'); |
|
724 |
$row = $result->fetch_hash_first; |
|
725 |
ok(!$row, "no row fetch"); |
|
726 | ||
727 |
$dbi = DBIx::Custom->connect; |
|
728 |
eval { $dbi->execute('drop table table1') }; |
|
729 |
$dbi->execute($create_table1); |
|
730 |
$dbi->insert({key1 => 1, key2 => 2}, table => 'table1'); |
|
731 |
$dbi->insert({key1 => 3, key2 => 4}, table => 'table1'); |
|
732 |
$dbi->insert({key1 => 5, key2 => 6}, table => 'table1'); |
|
733 |
$dbi->insert({key1 => 7, key2 => 8}, table => 'table1'); |
|
734 |
$dbi->insert({key1 => 9, key2 => 10}, table => 'table1'); |
|
735 |
$result = $dbi->select(table => 'table1'); |
|
736 |
$rows = $result->fetch_multi(2); |
|
737 |
is_deeply($rows, [[1, 2], |
|
738 |
[3, 4]], "fetch_multi first"); |
|
739 |
$rows = $result->fetch_multi(2); |
|
740 |
is_deeply($rows, [[5, 6], |
|
741 |
[7, 8]], "fetch_multi secound"); |
|
742 |
$rows = $result->fetch_multi(2); |
|
743 |
is_deeply($rows, [[9, 10]], "fetch_multi third"); |
|
744 |
$rows = $result->fetch_multi(2); |
|
745 |
ok(!$rows); |
|
746 | ||
747 |
$result = $dbi->select(table => 'table1'); |
|
748 |
eval {$result->fetch_multi}; |
|
749 |
like($@, qr/Row count must be specified/, "Not specified row count"); |
|
750 | ||
751 |
$result = $dbi->select(table => 'table1'); |
|
752 |
$rows = $result->fetch_hash_multi(2); |
|
753 |
is_deeply($rows, [{key1 => 1, key2 => 2}, |
|
754 |
{key1 => 3, key2 => 4}], "fetch_multi first"); |
|
755 |
$rows = $result->fetch_hash_multi(2); |
|
756 |
is_deeply($rows, [{key1 => 5, key2 => 6}, |
|
757 |
{key1 => 7, key2 => 8}], "fetch_multi secound"); |
|
758 |
$rows = $result->fetch_hash_multi(2); |
|
759 |
is_deeply($rows, [{key1 => 9, key2 => 10}], "fetch_multi third"); |
|
760 |
$rows = $result->fetch_hash_multi(2); |
|
761 |
ok(!$rows); |
|
762 | ||
763 |
$result = $dbi->select(table => 'table1'); |
|
764 |
eval {$result->fetch_hash_multi}; |
|
765 |
like($@, qr/Row count must be specified/, "Not specified row count"); |
|
766 | ||
test cleanup
|
767 | |
cleanup test
|
768 |
test 'type option'; # DEPRECATED! |
769 |
$dbi = DBIx::Custom->connect( |
|
770 |
data_source => 'dbi:SQLite:dbname=:memory:', |
|
771 |
dbi_option => { |
|
772 |
$DBD::SQLite::VERSION > 1.26 ? (sqlite_unicode => 1) : (unicode => 1) |
|
773 |
} |
|
774 |
); |
|
775 |
$binary = pack("I3", 1, 2, 3); |
|
776 |
eval { $dbi->execute('drop table table1') }; |
|
777 |
$dbi->execute('create table table1(key1, key2)'); |
|
778 |
$dbi->insert(table => 'table1', param => {key1 => $binary, key2 => 'あ'}, type => [key1 => DBI::SQL_BLOB]); |
|
779 |
$result = $dbi->select(table => 'table1'); |
|
780 |
$row = $result->one; |
|
781 |
is_deeply($row, {key1 => $binary, key2 => 'あ'}, "basic"); |
|
782 |
$result = $dbi->execute('select length(key1) as key1_length from table1'); |
|
783 |
$row = $result->one; |
|
784 |
is($row->{key1_length}, length $binary); |
|
785 | ||
786 |
test 'type_rule from'; |
|
787 |
$dbi = DBIx::Custom->connect; |
|
788 |
$dbi->type_rule( |
|
789 |
from1 => { |
|
790 |
date => sub { uc $_[0] } |
|
791 |
} |
|
792 |
); |
|
793 |
$dbi->execute("create table table1 (key1 Date, key2 datetime)"); |
|
794 |
$dbi->insert({key1 => 'a'}, table => 'table1'); |
|
795 |
$result = $dbi->select(table => 'table1'); |
|
796 |
is($result->fetch_first->[0], 'A'); |
|
797 | ||
798 |
$result = $dbi->select(table => 'table1'); |
|
799 |
is($result->one->{key1}, 'A'); |
|
800 | ||
801 | ||
802 | ||
803 | ||
804 | ||
test cleanup
|
805 |
# DEPRECATED! test |
806 |
test 'filter __ expression'; |
|
807 |
$dbi = DBIx::Custom->connect; |
|
808 |
eval { $dbi->execute('drop table company') }; |
|
809 |
eval { $dbi->execute('drop table location') }; |
|
810 |
$dbi->execute('create table company (id, name, location_id)'); |
|
811 |
$dbi->execute('create table location (id, name)'); |
|
812 |
$dbi->apply_filter('location', |
|
813 |
name => {in => sub { uc $_[0] } } |
|
814 |
); |
|
815 | ||
816 |
$dbi->insert(table => 'company', param => {id => 1, name => 'a', location_id => 2}); |
|
817 |
$dbi->insert(table => 'location', param => {id => 2, name => 'b'}); |
|
818 | ||
819 |
$result = $dbi->select( |
|
820 |
table => ['company', 'location'], relation => {'company.location_id' => 'location.id'}, |
|
821 |
column => ['location.name as location__name'] |
|
822 |
); |
|
823 |
is($result->fetch_first->[0], 'B'); |
|
824 | ||
825 |
$result = $dbi->select( |
|
826 |
table => 'company', relation => {'company.location_id' => 'location.id'}, |
|
827 |
column => ['location.name as location__name'] |
|
828 |
); |
|
829 |
is($result->fetch_first->[0], 'B'); |
|
830 | ||
831 |
$result = $dbi->select( |
|
832 |
table => 'company', relation => {'company.location_id' => 'location.id'}, |
|
833 |
column => ['location.name as "location.name"'] |
|
834 |
); |
|
835 |
is($result->fetch_first->[0], 'B'); |
|
test cleanup
|
836 | |
837 |
test 'reserved_word_quote'; |
|
838 |
$dbi = DBIx::Custom->connect; |
|
839 |
eval { $dbi->execute("drop table ${q}table$p") }; |
|
840 |
$dbi->reserved_word_quote('"'); |
|
841 |
$dbi->execute($create_table_reserved); |
|
842 |
$dbi->apply_filter('table', select => {out => sub { $_[0] * 2}}); |
|
843 |
$dbi->apply_filter('table', update => {out => sub { $_[0] * 3}}); |
|
844 |
$dbi->insert(table => 'table', param => {select => 1}); |
|
845 |
$dbi->update(table => 'table', where => {'table.select' => 1}, param => {update => 2}); |
|
846 |
$result = $dbi->execute("select * from ${q}table$p"); |
|
847 |
$rows = $result->all; |
|
848 |
is_deeply($rows, [{select => 2, update => 6}], "reserved word"); |