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