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