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