cleanup test
|
1 |
use Test::More; |
2 |
use strict; |
|
3 |
use warnings; |
|
4 |
use utf8; |
|
5 |
use Encode qw/encode_utf8 decode_utf8/; |
|
test cleanup
|
6 |
use FindBin; |
cleanup test
|
7 |
use lib "$FindBin::Bin/common"; |
cleanup test
|
8 | |
9 |
BEGIN { |
|
10 |
eval { require DBD::SQLite; 1 } |
|
11 |
or plan skip_all => 'DBD::SQLite required'; |
|
12 |
eval { DBD::SQLite->VERSION >= 1.25 } |
|
13 |
or plan skip_all => 'DBD::SQLite >= 1.25 required'; |
|
14 | ||
15 |
plan 'no_plan'; |
|
16 |
use_ok('DBIx::Custom'); |
|
17 |
} |
|
18 | ||
test cleanup
|
19 |
$SIG{__WARN__} = sub { warn $_[0] unless $_[0] =~ /DEPRECATED/}; |
cleanup test
|
20 |
sub test { print "# $_[0]\n" } |
21 | ||
test cleanup
|
22 |
use DBIx::Custom; |
test cleanup
|
23 |
{ |
24 |
package DBIx::Custom; |
|
25 |
has dsn => sub { 'dbi:SQLite:dbname=:memory:' } |
|
26 |
} |
|
test cleanup
|
27 |
use MyDBI1; |
28 |
{ |
|
29 |
package MyDBI4; |
|
30 | ||
31 |
use strict; |
|
32 |
use warnings; |
|
33 | ||
34 |
use base 'DBIx::Custom'; |
|
35 | ||
36 |
sub connect { |
|
37 |
my $self = shift->SUPER::connect(@_); |
|
38 |
|
|
39 |
$self->include_model( |
|
40 |
MyModel2 => [ |
|
41 |
'book', |
|
42 |
{class => 'Company', name => 'company'} |
|
43 |
] |
|
44 |
); |
|
45 |
} |
|
46 | ||
47 |
package MyModel2::Base1; |
|
48 | ||
49 |
use strict; |
|
50 |
use warnings; |
|
51 | ||
52 |
use base 'DBIx::Custom::Model'; |
|
53 | ||
54 |
package MyModel2::book; |
|
55 | ||
56 |
use strict; |
|
57 |
use warnings; |
|
58 | ||
59 |
use base 'MyModel2::Base1'; |
|
60 | ||
61 |
sub insert { |
|
62 |
my ($self, $param) = @_; |
|
63 |
|
|
64 |
return $self->SUPER::insert(param => $param); |
|
65 |
} |
|
66 | ||
67 |
sub list { shift->select; } |
|
68 | ||
69 |
package MyModel2::Company; |
|
70 | ||
71 |
use strict; |
|
72 |
use warnings; |
|
73 | ||
74 |
use base 'MyModel2::Base1'; |
|
75 | ||
76 |
sub insert { |
|
77 |
my ($self, $param) = @_; |
|
78 |
|
|
79 |
return $self->SUPER::insert(param => $param); |
|
80 |
} |
|
81 | ||
82 |
sub list { shift->select; } |
|
83 |
} |
|
84 |
{ |
|
85 |
package MyDBI5; |
|
86 | ||
87 |
use strict; |
|
88 |
use warnings; |
|
89 | ||
90 |
use base 'DBIx::Custom'; |
|
91 | ||
92 |
sub connect { |
|
93 |
my $self = shift->SUPER::connect(@_); |
|
94 |
|
|
95 |
$self->include_model('MyModel4'); |
|
96 |
} |
|
97 |
} |
|
98 |
{ |
|
99 |
package MyDBI6; |
|
100 |
|
|
101 |
use base 'DBIx::Custom'; |
|
102 |
|
|
103 |
sub connect { |
|
104 |
my $self = shift->SUPER::connect(@_); |
|
105 |
|
|
106 |
$self->include_model('MyModel5'); |
|
107 |
|
|
108 |
return $self; |
|
109 |
} |
|
110 |
} |
|
111 |
{ |
|
112 |
package MyDBI7; |
|
113 |
|
|
114 |
use base 'DBIx::Custom'; |
|
115 |
|
|
116 |
sub connect { |
|
117 |
my $self = shift->SUPER::connect(@_); |
|
118 |
|
|
119 |
$self->include_model('MyModel6'); |
|
120 |
|
|
121 |
|
|
122 |
return $self; |
|
123 |
} |
|
124 |
} |
|
125 |
{ |
|
126 |
package MyDBI8; |
|
127 |
|
|
128 |
use base 'DBIx::Custom'; |
|
129 |
|
|
130 |
sub connect { |
|
131 |
my $self = shift->SUPER::connect(@_); |
|
132 |
|
|
133 |
$self->include_model('MyModel7'); |
|
134 |
|
|
135 |
return $self; |
|
136 |
} |
|
137 |
} |
|
138 | ||
139 |
{ |
|
140 |
package MyDBI9; |
|
141 |
|
|
142 |
use base 'DBIx::Custom'; |
|
143 |
|
|
144 |
sub connect { |
|
145 |
my $self = shift->SUPER::connect(@_); |
|
146 |
|
|
147 |
$self->include_model('MyModel8')->setup_model; |
|
148 |
|
|
149 |
return $self; |
|
150 |
} |
|
151 |
} |
|
test cleanup
|
152 | |
cleanup test
|
153 |
# Constant |
cleanup test
|
154 |
my $create_table1 = 'create table table1 (key1 char(255), key2 char(255));'; |
155 |
my $create_table1_2 = 'create table table1 (key1 char(255), key2 char(255), key3 char(255), key4 char(255), key5 char(255));'; |
|
test cleanup
|
156 |
my $create_table2 = 'create table table2 (key1 char(255), key3 char(255));'; |
cleanup test
|
157 |
my $create_table2_2 = "create table table2 (key1, key2, key3)"; |
158 |
my $create_table3 = "create table table3 (key1, key2, key3)"; |
|
test cleanup
|
159 |
my $create_table_reserved = 'create table "table" ("select", "update")'; |
160 | ||
test cleanup
|
161 |
my $q = '"'; |
162 |
my $p = '"'; |
|
cleanup test
|
163 | |
cleanup test
|
164 |
# Variables |
cleanup test
|
165 |
my $builder; |
166 |
my $datas; |
|
cleanup test
|
167 |
my $dbi; |
168 |
my $sth; |
|
169 |
my $source; |
|
170 |
my @sources; |
|
cleanup test
|
171 |
my $select_source; |
172 |
my $insert_source; |
|
173 |
my $update_source; |
|
cleanup test
|
174 |
my $param; |
175 |
my $params; |
|
176 |
my $sql; |
|
177 |
my $result; |
|
178 |
my $row; |
|
179 |
my @rows; |
|
180 |
my $rows; |
|
181 |
my $query; |
|
182 |
my @queries; |
|
183 |
my $select_query; |
|
184 |
my $insert_query; |
|
185 |
my $update_query; |
|
186 |
my $ret_val; |
|
187 |
my $infos; |
|
188 |
my $model; |
|
189 |
my $model2; |
|
190 |
my $where; |
|
191 |
my $update_param; |
|
192 |
my $insert_param; |
|
cleanup test
|
193 |
my $join; |
cleanup test
|
194 |
my $binary; |
cleanup test
|
195 | |
196 |
# Prepare table |
|
test cleanup
|
197 |
$dbi = DBIx::Custom->connect; |
cleanup test
|
198 | |
199 |
test 'join'; |
|
test cleanup
|
200 |
$dbi = DBIx::Custom->connect; |
cleanup test
|
201 |
eval { $dbi->execute('drop table table1') }; |
cleanup test
|
202 |
$dbi->execute($create_table1); |
cleanup test
|
203 |
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2}); |
204 |
$dbi->insert(table => 'table1', param => {key1 => 3, key2 => 4}); |
|
test cleanup
|
205 |
$dbi->execute($create_table2); |
cleanup test
|
206 |
$dbi->insert(table => 'table2', param => {key1 => 1, key3 => 5}); |
cleanup test
|
207 |
$dbi->execute('create table table3 (key3 int, key4 int);'); |
cleanup test
|
208 |
$dbi->insert(table => 'table3', param => {key3 => 5, key4 => 4}); |
209 |
$rows = $dbi->select( |
|
210 |
table => 'table1', |
|
211 |
column => 'table1.key1 as table1_key1, table2.key1 as table2_key1, key2, key3', |
|
212 |
where => {'table1.key2' => 2}, |
|
213 |
join => ['left outer join table2 on table1.key1 = table2.key1'] |
|
214 |
)->all; |
|
215 |
is_deeply($rows, [{table1_key1 => 1, table2_key1 => 1, key2 => 2, key3 => 5}]); |
|
216 | ||
217 |
$rows = $dbi->select( |
|
218 |
table => 'table1', |
|
219 |
where => {'key1' => 1}, |
|
220 |
join => ['left outer join table2 on table1.key1 = table2.key1'] |
|
221 |
)->all; |
|
222 |
is_deeply($rows, [{key1 => 1, key2 => 2}]); |
|
223 | ||
224 |
eval { |
|
225 |
$rows = $dbi->select( |
|
226 |
table => 'table1', |
|
227 |
column => 'table1.key1 as table1_key1, table2.key1 as table2_key1, key2, key3', |
|
228 |
where => {'table1.key2' => 2}, |
|
229 |
join => {'table1.key1' => 'table2.key1'} |
|
230 |
); |
|
231 |
}; |
|
232 |
like ($@, qr/array/); |
|
233 | ||
234 |
$rows = $dbi->select( |
|
235 |
table => 'table1', |
|
236 |
where => {'key1' => 1}, |
|
237 |
join => ['left outer join table2 on table1.key1 = table2.key1', |
|
238 |
'left outer join table3 on table2.key3 = table3.key3'] |
|
239 |
)->all; |
|
240 |
is_deeply($rows, [{key1 => 1, key2 => 2}]); |
|
241 | ||
242 |
$rows = $dbi->select( |
|
243 |
column => 'table3.key4 as table3__key4', |
|
244 |
table => 'table1', |
|
245 |
where => {'table1.key1' => 1}, |
|
246 |
join => ['left outer join table2 on table1.key1 = table2.key1', |
|
247 |
'left outer join table3 on table2.key3 = table3.key3'] |
|
248 |
)->all; |
|
249 |
is_deeply($rows, [{table3__key4 => 4}]); |
|
250 | ||
251 |
$rows = $dbi->select( |
|
252 |
column => 'table1.key1 as table1__key1', |
|
253 |
table => 'table1', |
|
254 |
where => {'table3.key4' => 4}, |
|
255 |
join => ['left outer join table2 on table1.key1 = table2.key1', |
|
256 |
'left outer join table3 on table2.key3 = table3.key3'] |
|
257 |
)->all; |
|
258 |
is_deeply($rows, [{table1__key1 => 1}]); |
|
259 | ||
test cleanup
|
260 |
$dbi = DBIx::Custom->connect; |
cleanup test
|
261 |
$dbi->quote('"'); |
cleanup test
|
262 |
eval { $dbi->execute('drop table table1') }; |
cleanup test
|
263 |
$dbi->execute($create_table1); |
cleanup test
|
264 |
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2}); |
test cleanup
|
265 |
$dbi->execute($create_table2); |
cleanup test
|
266 |
$dbi->insert(table => 'table2', param => {key1 => 1, key3 => 5}); |
267 |
$rows = $dbi->select( |
|
268 |
table => 'table1', |
|
269 |
column => '"table1"."key1" as "table1_key1", "table2"."key1" as "table2_key1", "key2", "key3"', |
|
270 |
where => {'table1.key2' => 2}, |
|
271 |
join => ['left outer join "table2" on "table1"."key1" = "table2"."key1"'], |
|
272 |
)->all; |
|
273 |
is_deeply($rows, [{table1_key1 => 1, table2_key1 => 1, key2 => 2, key3 => 5}], |
|
274 |
'quote'); |
|
275 | ||
cleanup test
|
276 | |
test cleanup
|
277 |
$dbi = DBIx::Custom->connect; |
cleanup test
|
278 |
eval { $dbi->execute('drop table table1') }; |
cleanup test
|
279 |
$dbi->execute($create_table1); |
cleanup test
|
280 |
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2}); |
281 |
$sql = <<"EOS"; |
|
cleanup test
|
282 |
left outer join ( |
283 |
select * from table1 as t1 |
|
284 |
where t1.key2 = ( |
|
285 |
select max(t2.key2) from table1 as t2 |
|
286 |
where t1.key1 = t2.key1 |
|
287 |
) |
|
288 |
) as latest_table1 on table1.key1 = latest_table1.key1 |
|
289 |
EOS |
|
cleanup test
|
290 |
$join = [$sql]; |
291 |
$rows = $dbi->select( |
|
292 |
table => 'table1', |
|
293 |
column => 'latest_table1.key1 as latest_table1__key1', |
|
294 |
join => $join |
|
295 |
)->all; |
|
296 |
is_deeply($rows, [{latest_table1__key1 => 1}]); |
|
297 | ||
test cleanup
|
298 |
$dbi = DBIx::Custom->connect; |
cleanup test
|
299 |
eval { $dbi->execute('drop table table1') }; |
300 |
eval { $dbi->execute('drop table table2') }; |
|
cleanup test
|
301 |
$dbi->execute($create_table1); |
test cleanup
|
302 |
$dbi->execute($create_table2); |
cleanup test
|
303 |
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2}); |
304 |
$dbi->insert(table => 'table2', param => {key1 => 1, key3 => 4}); |
|
305 |
$dbi->insert(table => 'table2', param => {key1 => 1, key3 => 5}); |
|
306 |
$result = $dbi->select( |
|
307 |
table => 'table1', |
|
308 |
join => [ |
|
309 |
"left outer join table2 on table2.key2 = '4' and table1.key1 = table2.key1" |
|
310 |
] |
|
311 |
); |
|
312 |
is_deeply($result->all, [{key1 => 1, key2 => 2}]); |
|
313 |
$result = $dbi->select( |
|
314 |
table => 'table1', |
|
315 |
column => [{table2 => ['key3']}], |
|
316 |
join => [ |
|
317 |
"left outer join table2 on table2.key3 = '4' and table1.key1 = table2.key1" |
|
318 |
] |
|
319 |
); |
|
320 |
is_deeply($result->all, [{'table2.key3' => 4}]); |
|
321 |
$result = $dbi->select( |
|
322 |
table => 'table1', |
|
323 |
column => [{table2 => ['key3']}], |
|
324 |
join => [ |
|
325 |
"left outer join table2 on table1.key1 = table2.key1 and table2.key3 = '4'" |
|
326 |
] |
|
327 |
); |
|
328 |
is_deeply($result->all, [{'table2.key3' => 4}]); |
|
329 | ||
test cleanup
|
330 |
$dbi = DBIx::Custom->connect; |
cleanup test
|
331 |
eval { $dbi->execute('drop table table1') }; |
332 |
eval { $dbi->execute('drop table table2') }; |
|
cleanup test
|
333 |
$dbi->execute($create_table1); |
test cleanup
|
334 |
$dbi->execute($create_table2); |
cleanup test
|
335 |
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2}); |
336 |
$dbi->insert(table => 'table2', param => {key1 => 1, key3 => 4}); |
|
337 |
$dbi->insert(table => 'table2', param => {key1 => 1, key3 => 5}); |
|
338 |
$result = $dbi->select( |
|
339 |
table => 'table1', |
|
340 |
column => [{table2 => ['key3']}], |
|
341 |
join => [ |
|
342 |
{ |
|
343 |
clause => "left outer join table2 on table2.key3 = '4' and table1.key1 = table2.key1", |
|
344 |
table => ['table1', 'table2'] |
|
345 |
} |
|
346 |
] |
|
347 |
); |
|
348 |
is_deeply($result->all, [{'table2.key3' => 4}]); |
|
349 | ||
350 |
test 'mycolumn'; |
|
test cleanup
|
351 |
$dbi = MyDBI8->connect; |
cleanup test
|
352 |
eval { $dbi->execute('drop table table1') }; |
353 |
eval { $dbi->execute('drop table table2') }; |
|
cleanup test
|
354 |
$dbi->execute($create_table1); |
test cleanup
|
355 |
$dbi->execute($create_table2); |
cleanup test
|
356 |
$dbi->setup_model; |
357 |
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2}); |
|
358 |
$dbi->insert(table => 'table2', param => {key1 => 1, key3 => 3}); |
|
359 |
$model = $dbi->model('table1'); |
|
360 |
$result = $model->select_at( |
|
361 |
column => [ |
|
362 |
$model->mycolumn, |
|
363 |
$model->column('table2') |
|
364 |
] |
|
365 |
); |
|
366 |
is_deeply($result->one, |
|
367 |
{key1 => 1, key2 => 2, 'table2.key1' => 1, 'table2.key3' => 3}); |
|
368 | ||
369 |
$result = $model->select_at( |
|
370 |
column => [ |
|
371 |
$model->mycolumn(['key1']), |
|
372 |
$model->column(table2 => ['key1']) |
|
373 |
] |
|
374 |
); |
|
375 |
is_deeply($result->one, |
|
376 |
{key1 => 1, 'table2.key1' => 1}); |
|
377 |
$result = $model->select_at( |
|
378 |
column => [ |
|
379 |
$model->mycolumn(['key1']), |
|
380 |
{table2 => ['key1']} |
|
381 |
] |
|
382 |
); |
|
383 |
is_deeply($result->one, |
|
384 |
{key1 => 1, 'table2.key1' => 1}); |
|
385 | ||
386 |
$result = $model->select_at( |
|
387 |
column => [ |
|
388 |
$model->mycolumn(['key1']), |
|
389 |
['table2.key1', as => 'table2.key1'] |
|
390 |
] |
|
391 |
); |
|
392 |
is_deeply($result->one, |
|
393 |
{key1 => 1, 'table2.key1' => 1}); |
|
394 | ||
395 |
$result = $model->select_at( |
|
396 |
column => [ |
|
397 |
$model->mycolumn(['key1']), |
|
398 |
['table2.key1' => 'table2.key1'] |
|
399 |
] |
|
400 |
); |
|
401 |
is_deeply($result->one, |
|
402 |
{key1 => 1, 'table2.key1' => 1}); |
|
403 | ||
404 |
test 'dbi method from model'; |
|
test cleanup
|
405 |
$dbi = MyDBI9->connect; |
cleanup test
|
406 |
eval { $dbi->execute('drop table table1') }; |
cleanup test
|
407 |
$dbi->execute($create_table1); |
cleanup test
|
408 |
$model = $dbi->model('table1'); |
409 |
eval{$model->execute('select * from table1')}; |
|
410 |
ok(!$@); |
|
411 | ||
412 |
test 'column table option'; |
|
test cleanup
|
413 |
$dbi = MyDBI9->connect; |
cleanup test
|
414 |
eval { $dbi->execute('drop table table1') }; |
cleanup test
|
415 |
$dbi->execute($create_table1); |
test cleanup
|
416 |
$dbi->execute($create_table2); |
cleanup test
|
417 |
$dbi->setup_model; |
418 |
$dbi->execute('insert into table1 (key1, key2) values (1, 2);'); |
|
419 |
$dbi->execute('insert into table2 (key1, key3) values (1, 4);'); |
|
420 |
$model = $dbi->model('table1'); |
|
421 |
$result = $model->select( |
|
422 |
column => [ |
|
423 |
$model->column('table2', {alias => 'table2_alias'}) |
|
424 |
], |
|
425 |
where => {'table2_alias.key3' => 4} |
|
426 |
); |
|
427 |
is_deeply($result->one, |
|
428 |
{'table2_alias.key1' => 1, 'table2_alias.key3' => 4}); |
|
429 | ||
430 |
$dbi->separator('__'); |
|
431 |
$result = $model->select( |
|
432 |
column => [ |
|
433 |
$model->column('table2', {alias => 'table2_alias'}) |
|
434 |
], |
|
435 |
where => {'table2_alias.key3' => 4} |
|
436 |
); |
|
437 |
is_deeply($result->one, |
|
438 |
{'table2_alias__key1' => 1, 'table2_alias__key3' => 4}); |
|
439 | ||
440 |
$dbi->separator('-'); |
|
441 |
$result = $model->select( |
|
442 |
column => [ |
|
443 |
$model->column('table2', {alias => 'table2_alias'}) |
|
444 |
], |
|
445 |
where => {'table2_alias.key3' => 4} |
|
446 |
); |
|
447 |
is_deeply($result->one, |
|
448 |
{'table2_alias-key1' => 1, 'table2_alias-key3' => 4}); |
|
449 | ||
450 |
test 'type option'; # DEPRECATED! |
|
451 |
$dbi = DBIx::Custom->connect( |
|
452 |
dbi_option => { |
|
453 |
$DBD::SQLite::VERSION > 1.26 ? (sqlite_unicode => 1) : (unicode => 1) |
|
454 |
} |
|
455 |
); |
|
cleanup test
|
456 |
$binary = pack("I3", 1, 2, 3); |
457 |
eval { $dbi->execute('drop table table1') }; |
|
cleanup test
|
458 |
$dbi->execute('create table table1(key1, key2)'); |
459 |
$dbi->insert(table => 'table1', param => {key1 => $binary, key2 => 'あ'}, type => [key1 => DBI::SQL_BLOB]); |
|
460 |
$result = $dbi->select(table => 'table1'); |
|
461 |
$row = $result->one; |
|
462 |
is_deeply($row, {key1 => $binary, key2 => 'あ'}, "basic"); |
|
463 |
$result = $dbi->execute('select length(key1) as key1_length from table1'); |
|
464 |
$row = $result->one; |
|
465 |
is($row->{key1_length}, length $binary); |
|
466 | ||
467 |
$dbi->insert(table => 'table1', param => {key1 => $binary, key2 => 'あ'}, type => [['key1'] => DBI::SQL_BLOB]); |
|
468 |
$result = $dbi->select(table => 'table1'); |
|
469 |
$row = $result->one; |
|
470 |
is_deeply($row, {key1 => $binary, key2 => 'あ'}, "basic"); |
|
471 |
$result = $dbi->execute('select length(key1) as key1_length from table1'); |
|
472 |
$row = $result->one; |
|
473 |
is($row->{key1_length}, length $binary); |
|
474 | ||
475 | ||
476 |
test 'bind_type option'; |
|
477 |
$dbi = DBIx::Custom->connect( |
|
478 |
dbi_option => { |
|
479 |
$DBD::SQLite::VERSION > 1.26 ? (sqlite_unicode => 1) : (unicode => 1) |
|
480 |
} |
|
481 |
); |
|
482 |
$binary = pack("I3", 1, 2, 3); |
|
cleanup test
|
483 |
eval { $dbi->execute('drop table table1') }; |
cleanup test
|
484 |
$dbi->execute('create table table1(key1, key2)'); |
485 |
$dbi->insert(table => 'table1', param => {key1 => $binary, key2 => 'あ'}, bind_type => [key1 => DBI::SQL_BLOB]); |
|
486 |
$result = $dbi->select(table => 'table1'); |
|
487 |
$row = $result->one; |
|
488 |
is_deeply($row, {key1 => $binary, key2 => 'あ'}, "basic"); |
|
489 |
$result = $dbi->execute('select length(key1) as key1_length from table1'); |
|
490 |
$row = $result->one; |
|
491 |
is($row->{key1_length}, length $binary); |
|
492 | ||
493 |
$dbi->insert(table => 'table1', param => {key1 => $binary, key2 => 'あ'}, bind_type => [['key1'] => DBI::SQL_BLOB]); |
|
494 |
$result = $dbi->select(table => 'table1'); |
|
495 |
$row = $result->one; |
|
496 |
is_deeply($row, {key1 => $binary, key2 => 'あ'}, "basic"); |
|
497 |
$result = $dbi->execute('select length(key1) as key1_length from table1'); |
|
498 |
$row = $result->one; |
|
499 |
is($row->{key1_length}, length $binary); |
|
500 | ||
501 |
test 'model type attribute'; |
|
502 |
$dbi = DBIx::Custom->connect( |
|
503 |
dbi_option => { |
|
504 |
$DBD::SQLite::VERSION > 1.26 ? (sqlite_unicode => 1) : (unicode => 1) |
|
505 |
} |
|
506 |
); |
|
507 |
$binary = pack("I3", 1, 2, 3); |
|
cleanup test
|
508 |
eval { $dbi->execute('drop table table1') }; |
cleanup test
|
509 |
$dbi->execute('create table table1(key1, key2)'); |
510 |
$model = $dbi->create_model(table => 'table1', bind_type => [key1 => DBI::SQL_BLOB]); |
|
511 |
$model->insert(param => {key1 => $binary, key2 => 'あ'}); |
|
512 |
$result = $dbi->select(table => 'table1'); |
|
513 |
$row = $result->one; |
|
514 |
is_deeply($row, {key1 => $binary, key2 => 'あ'}, "basic"); |
|
515 |
$result = $dbi->execute('select length(key1) as key1_length from table1'); |
|
516 |
$row = $result->one; |
|
517 |
is($row->{key1_length}, length $binary); |
|
518 | ||
519 |
test 'create_model'; |
|
test cleanup
|
520 |
$dbi = DBIx::Custom->connect; |
cleanup test
|
521 |
eval { $dbi->execute('drop table table1') }; |
522 |
eval { $dbi->execute('drop table table2') }; |
|
cleanup test
|
523 |
$dbi->execute($create_table1); |
test cleanup
|
524 |
$dbi->execute($create_table2); |
cleanup test
|
525 | |
526 |
$dbi->create_model( |
|
527 |
table => 'table1', |
|
528 |
join => [ |
|
529 |
'left outer join table2 on table1.key1 = table2.key1' |
|
530 |
], |
|
531 |
primary_key => ['key1'] |
|
532 |
); |
|
533 |
$model2 = $dbi->create_model( |
|
534 |
table => 'table2' |
|
535 |
); |
|
536 |
$dbi->create_model( |
|
537 |
table => 'table3', |
|
538 |
filter => [ |
|
539 |
key1 => {in => sub { uc $_[0] }} |
|
540 |
] |
|
541 |
); |
|
542 |
$dbi->setup_model; |
|
543 |
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2}); |
|
544 |
$dbi->insert(table => 'table2', param => {key1 => 1, key3 => 3}); |
|
545 |
$model = $dbi->model('table1'); |
|
546 |
$result = $model->select( |
|
547 |
column => [$model->mycolumn, $model->column('table2')], |
|
548 |
where => {'table1.key1' => 1} |
|
549 |
); |
|
550 |
is_deeply($result->one, |
|
551 |
{key1 => 1, key2 => 2, 'table2.key1' => 1, 'table2.key3' => 3}); |
|
552 |
is_deeply($model2->select->one, {key1 => 1, key3 => 3}); |
|
553 | ||
554 |
test 'model method'; |
|
555 |
test 'create_model'; |
|
test cleanup
|
556 |
$dbi = DBIx::Custom->connect; |
cleanup test
|
557 |
eval { $dbi->execute('drop table table2') }; |
test cleanup
|
558 |
$dbi->execute($create_table2); |
cleanup test
|
559 |
$dbi->insert(table => 'table2', param => {key1 => 1, key3 => 3}); |
560 |
$model = $dbi->create_model( |
|
561 |
table => 'table2' |
|
562 |
); |
|
563 |
$model->method(foo => sub { shift->select(@_) }); |
|
564 |
is_deeply($model->foo->one, {key1 => 1, key3 => 3}); |
|
565 | ||
566 |
test 'merge_param'; |
|
cleanup test
|
567 |
$dbi = DBIx::Custom->new; |
568 |
$params = [ |
|
569 |
{key1 => 1, key2 => 2, key3 => 3}, |
|
570 |
{key1 => 1, key2 => 2}, |
|
571 |
{key1 => 1} |
|
572 |
]; |
|
573 |
$param = $dbi->merge_param($params->[0], $params->[1], $params->[2]); |
|
574 |
is_deeply($param, {key1 => [1, 1, 1], key2 => [2, 2], key3 => 3}); |
|
575 | ||
576 |
$params = [ |
|
577 |
{key1 => [1, 2], key2 => 1, key3 => [1, 2]}, |
|
578 |
{key1 => [3, 4], key2 => [2, 3], key3 => 3} |
|
579 |
]; |
|
580 |
$param = $dbi->merge_param($params->[0], $params->[1]); |
|
581 |
is_deeply($param, {key1 => [1, 2, 3, 4], key2 => [1, 2, 3], key3 => [1, 2, 3]}); |
|
cleanup test
|
582 | |
583 |
test 'select() param option'; |
|
test cleanup
|
584 |
$dbi = DBIx::Custom->connect; |
cleanup test
|
585 |
eval { $dbi->execute('drop table table1') }; |
cleanup test
|
586 |
$dbi->execute($create_table1); |
cleanup test
|
587 |
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2}); |
588 |
$dbi->insert(table => 'table1', param => {key1 => 2, key2 => 3}); |
|
test cleanup
|
589 |
$dbi->execute($create_table2); |
cleanup test
|
590 |
$dbi->insert(table => 'table2', param => {key1 => 1, key3 => 4}); |
591 |
$dbi->insert(table => 'table2', param => {key1 => 2, key3 => 5}); |
|
592 |
$rows = $dbi->select( |
|
593 |
table => 'table1', |
|
594 |
column => 'table1.key1 as table1_key1, key2, key3', |
|
595 |
where => {'table1.key2' => 3}, |
|
596 |
join => ['inner join (select * from table2 where {= table2.key3})' . |
|
597 |
' as table2 on table1.key1 = table2.key1'], |
|
598 |
param => {'table2.key3' => 5} |
|
599 |
)->all; |
|
600 |
is_deeply($rows, [{table1_key1 => 2, key2 => 3, key3 => 5}]); |
|
601 | ||
602 | ||
603 |
test 'select() wrap option'; |
|
test cleanup
|
604 |
$dbi = DBIx::Custom->connect; |
cleanup test
|
605 |
eval { $dbi->execute('drop table table1') }; |
cleanup test
|
606 |
$dbi->execute($create_table1); |
cleanup test
|
607 |
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2}); |
608 |
$dbi->insert(table => 'table1', param => {key1 => 2, key2 => 3}); |
|
609 |
$rows = $dbi->select( |
|
610 |
table => 'table1', |
|
611 |
column => 'key1', |
|
612 |
wrap => ['select * from (', ') as t where key1 = 1'] |
|
613 |
)->all; |
|
614 |
is_deeply($rows, [{key1 => 1}]); |
|
615 | ||
616 |
eval { |
|
617 |
$dbi->select( |
|
618 |
table => 'table1', |
|
619 |
column => 'key1', |
|
620 |
wrap => 'select * from (' |
|
621 |
) |
|
622 |
}; |
|
623 |
like($@, qr/array/); |
|
624 | ||
625 |
test 'select() string where'; |
|
test cleanup
|
626 |
$dbi = DBIx::Custom->connect; |
cleanup test
|
627 |
eval { $dbi->execute('drop table table1') }; |
cleanup test
|
628 |
$dbi->execute($create_table1); |
cleanup test
|
629 |
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2}); |
630 |
$dbi->insert(table => 'table1', param => {key1 => 2, key2 => 3}); |
|
631 |
$rows = $dbi->select( |
|
632 |
table => 'table1', |
|
633 |
where => 'key1 = :key1 and key2 = :key2', |
|
634 |
where_param => {key1 => 1, key2 => 2} |
|
635 |
)->all; |
|
636 |
is_deeply($rows, [{key1 => 1, key2 => 2}]); |
|
637 | ||
test cleanup
|
638 |
$dbi = DBIx::Custom->connect; |
cleanup test
|
639 |
eval { $dbi->execute('drop table table1') }; |
cleanup test
|
640 |
$dbi->execute($create_table1); |
cleanup test
|
641 |
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2}); |
642 |
$dbi->insert(table => 'table1', param => {key1 => 2, key2 => 3}); |
|
643 |
$rows = $dbi->select( |
|
644 |
table => 'table1', |
|
645 |
where => [ |
|
646 |
'key1 = :key1 and key2 = :key2', |
|
647 |
{key1 => 1, key2 => 2} |
|
648 |
] |
|
649 |
)->all; |
|
650 |
is_deeply($rows, [{key1 => 1, key2 => 2}]); |
|
651 | ||
652 |
test 'delete() string where'; |
|
test cleanup
|
653 |
$dbi = DBIx::Custom->connect; |
cleanup test
|
654 |
eval { $dbi->execute('drop table table1') }; |
cleanup test
|
655 |
$dbi->execute($create_table1); |
cleanup test
|
656 |
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2}); |
657 |
$dbi->insert(table => 'table1', param => {key1 => 2, key2 => 3}); |
|
658 |
$dbi->delete( |
|
659 |
table => 'table1', |
|
660 |
where => 'key1 = :key1 and key2 = :key2', |
|
661 |
where_param => {key1 => 1, key2 => 2} |
|
662 |
); |
|
663 |
$rows = $dbi->select(table => 'table1')->all; |
|
664 |
is_deeply($rows, [{key1 => 2, key2 => 3}]); |
|
665 | ||
test cleanup
|
666 |
$dbi = DBIx::Custom->connect; |
cleanup test
|
667 |
eval { $dbi->execute('drop table table1') }; |
cleanup test
|
668 |
$dbi->execute($create_table1); |
cleanup test
|
669 |
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2}); |
670 |
$dbi->insert(table => 'table1', param => {key1 => 2, key2 => 3}); |
|
671 |
$dbi->delete( |
|
672 |
table => 'table1', |
|
673 |
where => [ |
|
674 |
'key1 = :key1 and key2 = :key2', |
|
675 |
{key1 => 1, key2 => 2} |
|
676 |
] |
|
677 |
); |
|
678 |
$rows = $dbi->select(table => 'table1')->all; |
|
679 |
is_deeply($rows, [{key1 => 2, key2 => 3}]); |
|
680 | ||
681 | ||
682 |
test 'update() string where'; |
|
test cleanup
|
683 |
$dbi = DBIx::Custom->connect; |
cleanup test
|
684 |
eval { $dbi->execute('drop table table1') }; |
cleanup test
|
685 |
$dbi->execute($create_table1); |
cleanup test
|
686 |
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2}); |
687 |
$dbi->update( |
|
688 |
table => 'table1', |
|
689 |
param => {key1 => 5}, |
|
690 |
where => 'key1 = :key1 and key2 = :key2', |
|
691 |
where_param => {key1 => 1, key2 => 2} |
|
692 |
); |
|
693 |
$rows = $dbi->select(table => 'table1')->all; |
|
694 |
is_deeply($rows, [{key1 => 5, key2 => 2}]); |
|
695 | ||
test cleanup
|
696 |
$dbi = DBIx::Custom->connect; |
cleanup test
|
697 |
eval { $dbi->execute('drop table table1') }; |
cleanup test
|
698 |
$dbi->execute($create_table1); |
cleanup test
|
699 |
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2}); |
700 |
$dbi->update( |
|
701 |
table => 'table1', |
|
702 |
param => {key1 => 5}, |
|
703 |
where => [ |
|
704 |
'key1 = :key1 and key2 = :key2', |
|
705 |
{key1 => 1, key2 => 2} |
|
706 |
] |
|
707 |
); |
|
708 |
$rows = $dbi->select(table => 'table1')->all; |
|
709 |
is_deeply($rows, [{key1 => 5, key2 => 2}]); |
|
710 | ||
711 |
test 'insert id and primary_key option'; |
|
test cleanup
|
712 |
$dbi = DBIx::Custom->connect; |
cleanup test
|
713 |
eval { $dbi->execute('drop table table1') }; |
cleanup test
|
714 |
$dbi->execute($create_table1_2); |
cleanup test
|
715 |
$dbi->insert( |
716 |
primary_key => ['key1', 'key2'], |
|
717 |
table => 'table1', |
|
718 |
id => [1, 2], |
|
719 |
param => {key3 => 3} |
|
720 |
); |
|
721 |
is($dbi->select(table => 'table1')->one->{key1}, 1); |
|
722 |
is($dbi->select(table => 'table1')->one->{key2}, 2); |
|
723 |
is($dbi->select(table => 'table1')->one->{key3}, 3); |
|
724 | ||
725 |
$dbi->delete_all(table => 'table1'); |
|
726 |
$dbi->insert( |
|
727 |
primary_key => 'key1', |
|
728 |
table => 'table1', |
|
729 |
id => 0, |
|
730 |
param => {key2 => 2, key3 => 3} |
|
731 |
); |
|
732 | ||
733 |
is($dbi->select(table => 'table1')->one->{key1}, 0); |
|
734 |
is($dbi->select(table => 'table1')->one->{key2}, 2); |
|
735 |
is($dbi->select(table => 'table1')->one->{key3}, 3); |
|
736 | ||
test cleanup
|
737 |
$dbi = DBIx::Custom->connect; |
cleanup test
|
738 |
eval { $dbi->execute('drop table table1') }; |
cleanup test
|
739 |
$dbi->execute($create_table1_2); |
cleanup test
|
740 |
$dbi->insert( |
741 |
{key3 => 3}, |
|
742 |
primary_key => ['key1', 'key2'], |
|
743 |
table => 'table1', |
|
744 |
id => [1, 2], |
|
745 |
); |
|
746 |
is($dbi->select(table => 'table1')->one->{key1}, 1); |
|
747 |
is($dbi->select(table => 'table1')->one->{key2}, 2); |
|
748 |
is($dbi->select(table => 'table1')->one->{key3}, 3); |
|
749 | ||
750 | ||
751 |
test 'model insert id and primary_key option'; |
|
test cleanup
|
752 |
$dbi = MyDBI6->connect; |
cleanup test
|
753 |
eval { $dbi->execute('drop table table1') }; |
cleanup test
|
754 |
$dbi->execute($create_table1_2); |
cleanup test
|
755 |
$dbi->model('table1')->insert( |
756 |
id => [1, 2], |
|
757 |
param => {key3 => 3} |
|
758 |
); |
|
759 |
$result = $dbi->model('table1')->select; |
|
760 |
$row = $result->one; |
|
761 |
is($row->{key1}, 1); |
|
762 |
is($row->{key2}, 2); |
|
763 |
is($row->{key3}, 3); |
|
764 | ||
test cleanup
|
765 |
$dbi = MyDBI6->connect; |
cleanup test
|
766 |
eval { $dbi->execute('drop table table1') }; |
cleanup test
|
767 |
$dbi->execute($create_table1_2); |
cleanup test
|
768 |
$dbi->model('table1')->insert( |
769 |
{key3 => 3}, |
|
770 |
id => [1, 2] |
|
771 |
); |
|
772 |
$result = $dbi->model('table1')->select; |
|
773 |
$row = $result->one; |
|
774 |
is($row->{key1}, 1); |
|
775 |
is($row->{key2}, 2); |
|
776 |
is($row->{key3}, 3); |
|
777 | ||
778 |
test 'update and id option'; |
|
test cleanup
|
779 |
$dbi = DBIx::Custom->connect; |
cleanup test
|
780 |
eval { $dbi->execute('drop table table1') }; |
cleanup test
|
781 |
$dbi->execute($create_table1_2); |
cleanup test
|
782 |
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3}); |
783 |
$dbi->update( |
|
784 |
table => 'table1', |
|
785 |
primary_key => ['key1', 'key2'], |
|
786 |
id => [1, 2], |
|
787 |
param => {key3 => 4} |
|
788 |
); |
|
789 |
is($dbi->select(table => 'table1')->one->{key1}, 1); |
|
790 |
is($dbi->select(table => 'table1')->one->{key2}, 2); |
|
791 |
is($dbi->select(table => 'table1')->one->{key3}, 4); |
|
792 | ||
793 |
$dbi->delete_all(table => 'table1'); |
|
794 |
$dbi->insert(table => 'table1', param => {key1 => 0, key2 => 2, key3 => 3}); |
|
795 |
$dbi->update( |
|
796 |
table => 'table1', |
|
797 |
primary_key => 'key1', |
|
798 |
id => 0, |
|
799 |
param => {key3 => 4} |
|
800 |
); |
|
801 |
is($dbi->select(table => 'table1')->one->{key1}, 0); |
|
802 |
is($dbi->select(table => 'table1')->one->{key2}, 2); |
|
803 |
is($dbi->select(table => 'table1')->one->{key3}, 4); |
|
804 | ||
test cleanup
|
805 |
$dbi = DBIx::Custom->connect; |
cleanup test
|
806 |
eval { $dbi->execute('drop table table1') }; |
cleanup test
|
807 |
$dbi->execute($create_table1_2); |
cleanup test
|
808 |
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3}); |
809 |
$dbi->update( |
|
810 |
{key3 => 4}, |
|
811 |
table => 'table1', |
|
812 |
primary_key => ['key1', 'key2'], |
|
813 |
id => [1, 2] |
|
814 |
); |
|
815 |
is($dbi->select(table => 'table1')->one->{key1}, 1); |
|
816 |
is($dbi->select(table => 'table1')->one->{key2}, 2); |
|
817 |
is($dbi->select(table => 'table1')->one->{key3}, 4); |
|
818 | ||
819 | ||
820 |
test 'model update and id option'; |
|
test cleanup
|
821 |
$dbi = MyDBI6->connect; |
cleanup test
|
822 |
eval { $dbi->execute('drop table table1') }; |
cleanup test
|
823 |
$dbi->execute($create_table1_2); |
cleanup test
|
824 |
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3}); |
825 |
$dbi->model('table1')->update( |
|
826 |
id => [1, 2], |
|
827 |
param => {key3 => 4} |
|
828 |
); |
|
829 |
$result = $dbi->model('table1')->select; |
|
830 |
$row = $result->one; |
|
831 |
is($row->{key1}, 1); |
|
832 |
is($row->{key2}, 2); |
|
833 |
is($row->{key3}, 4); |
|
834 | ||
835 | ||
836 |
test 'delete and id option'; |
|
test cleanup
|
837 |
$dbi = DBIx::Custom->connect; |
cleanup test
|
838 |
eval { $dbi->execute('drop table table1') }; |
cleanup test
|
839 |
$dbi->execute($create_table1_2); |
cleanup test
|
840 |
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3}); |
841 |
$dbi->delete( |
|
842 |
table => 'table1', |
|
843 |
primary_key => ['key1', 'key2'], |
|
844 |
id => [1, 2], |
|
845 |
); |
|
846 |
is_deeply($dbi->select(table => 'table1')->all, []); |
|
847 | ||
848 |
$dbi->insert(table => 'table1', param => {key1 => 0, key2 => 2, key3 => 3}); |
|
849 |
$dbi->delete( |
|
850 |
table => 'table1', |
|
851 |
primary_key => 'key1', |
|
852 |
id => 0, |
|
853 |
); |
|
854 |
is_deeply($dbi->select(table => 'table1')->all, []); |
|
855 | ||
856 | ||
857 |
test 'model delete and id option'; |
|
test cleanup
|
858 |
$dbi = MyDBI6->connect; |
cleanup test
|
859 |
eval { $dbi->execute('drop table table1') }; |
860 |
eval { $dbi->execute('drop table table2') }; |
|
861 |
eval { $dbi->execute('drop table table3') }; |
|
cleanup test
|
862 |
$dbi->execute($create_table1_2); |
cleanup test
|
863 |
$dbi->execute($create_table2_2); |
864 |
$dbi->execute($create_table3); |
|
cleanup test
|
865 |
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3}); |
866 |
$dbi->model('table1')->delete(id => [1, 2]); |
|
867 |
is_deeply($dbi->select(table => 'table1')->all, []); |
|
868 |
$dbi->insert(table => 'table2', param => {key1 => 1, key2 => 2, key3 => 3}); |
|
869 |
$dbi->model('table1_1')->delete(id => [1, 2]); |
|
870 |
is_deeply($dbi->select(table => 'table1')->all, []); |
|
871 |
$dbi->insert(table => 'table3', param => {key1 => 1, key2 => 2, key3 => 3}); |
|
872 |
$dbi->model('table1_3')->delete(id => [1, 2]); |
|
873 |
is_deeply($dbi->select(table => 'table1')->all, []); |
|
874 | ||
875 | ||
876 |
test 'select and id option'; |
|
test cleanup
|
877 |
$dbi = DBIx::Custom->connect; |
cleanup test
|
878 |
eval { $dbi->execute('drop table table1') }; |
cleanup test
|
879 |
$dbi->execute($create_table1_2); |
cleanup test
|
880 |
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3}); |
881 |
$result = $dbi->select( |
|
882 |
table => 'table1', |
|
883 |
primary_key => ['key1', 'key2'], |
|
884 |
id => [1, 2] |
|
885 |
); |
|
886 |
$row = $result->one; |
|
887 |
is($row->{key1}, 1); |
|
888 |
is($row->{key2}, 2); |
|
889 |
is($row->{key3}, 3); |
|
890 | ||
891 |
$dbi->delete_all(table => 'table1'); |
|
892 |
$dbi->insert(table => 'table1', param => {key1 => 0, key2 => 2, key3 => 3}); |
|
893 |
$result = $dbi->select( |
|
894 |
table => 'table1', |
|
895 |
primary_key => 'key1', |
|
896 |
id => 0, |
|
897 |
); |
|
898 |
$row = $result->one; |
|
899 |
is($row->{key1}, 0); |
|
900 |
is($row->{key2}, 2); |
|
901 |
is($row->{key3}, 3); |
|
902 | ||
903 |
$dbi->delete_all(table => 'table1'); |
|
904 |
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3}); |
|
905 |
$result = $dbi->select( |
|
906 |
table => 'table1', |
|
907 |
primary_key => ['key1', 'key2'], |
|
908 |
id => [1, 2] |
|
909 |
); |
|
910 |
$row = $result->one; |
|
911 |
is($row->{key1}, 1); |
|
912 |
is($row->{key2}, 2); |
|
913 |
is($row->{key3}, 3); |
|
914 | ||
915 | ||
916 |
test 'model select_at'; |
|
test cleanup
|
917 |
$dbi = MyDBI6->connect; |
cleanup test
|
918 |
eval { $dbi->execute('drop table table1') }; |
cleanup test
|
919 |
$dbi->execute($create_table1_2); |
cleanup test
|
920 |
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3}); |
921 |
$result = $dbi->model('table1')->select(id => [1, 2]); |
|
922 |
$row = $result->one; |
|
923 |
is($row->{key1}, 1); |
|
924 |
is($row->{key2}, 2); |
|
925 |
is($row->{key3}, 3); |
|
926 | ||
927 |
test 'column separator is default .'; |
|
test cleanup
|
928 |
$dbi = MyDBI7->connect; |
cleanup test
|
929 |
eval { $dbi->execute('drop table table1') }; |
930 |
eval { $dbi->execute('drop table table2') }; |
|
cleanup test
|
931 |
$dbi->execute($create_table1); |
test cleanup
|
932 |
$dbi->execute($create_table2); |
cleanup test
|
933 |
$dbi->setup_model; |
934 |
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2}); |
|
935 |
$dbi->insert(table => 'table2', param => {key1 => 1, key3 => 3}); |
|
936 |
$model = $dbi->model('table1'); |
|
937 |
$result = $model->select( |
|
938 |
column => [$model->column('table2')], |
|
939 |
where => {'table1.key1' => 1} |
|
940 |
); |
|
941 |
is_deeply($result->one, |
|
942 |
{'table2.key1' => 1, 'table2.key3' => 3}); |
|
943 | ||
944 |
$result = $model->select( |
|
945 |
column => [$model->column('table2' => [qw/key1 key3/])], |
|
946 |
where => {'table1.key1' => 1} |
|
947 |
); |
|
948 |
is_deeply($result->one, |
|
949 |
{'table2.key1' => 1, 'table2.key3' => 3}); |
|
950 | ||
951 | ||
cleanup test
|
952 | |
953 |
test 'separator'; |
|
test cleanup
|
954 |
$dbi = DBIx::Custom->connect; |
cleanup test
|
955 |
eval { $dbi->execute('drop table table1') }; |
956 |
eval { $dbi->execute('drop table table2') }; |
|
957 |
$dbi->execute($create_table1); |
|
test cleanup
|
958 |
$dbi->execute($create_table2); |
cleanup test
|
959 | |
960 |
$dbi->create_model( |
|
961 |
table => 'table1', |
|
962 |
join => [ |
|
963 |
'left outer join table2 on table1.key1 = table2.key1' |
|
964 |
], |
|
965 |
primary_key => ['key1'], |
|
cleanup test
|
966 |
); |
cleanup test
|
967 |
$model2 = $dbi->create_model( |
968 |
table => 'table2', |
|
969 |
); |
|
970 |
$dbi->setup_model; |
|
971 |
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2}); |
|
972 |
$dbi->insert(table => 'table2', param => {key1 => 1, key3 => 3}); |
|
973 |
$model = $dbi->model('table1'); |
|
974 |
$result = $model->select( |
|
975 |
column => [ |
|
976 |
$model->mycolumn, |
|
977 |
{table2 => [qw/key1 key3/]} |
|
978 |
], |
|
979 |
where => {'table1.key1' => 1} |
|
980 |
); |
|
981 |
is_deeply($result->one, |
|
982 |
{key1 => 1, key2 => 2, 'table2.key1' => 1, 'table2.key3' => 3}); |
|
983 |
is_deeply($model2->select->one, {key1 => 1, key3 => 3}); |
|
cleanup test
|
984 | |
cleanup test
|
985 |
$dbi->separator('__'); |
986 |
$model = $dbi->model('table1'); |
|
987 |
$result = $model->select( |
|
988 |
column => [ |
|
989 |
$model->mycolumn, |
|
990 |
{table2 => [qw/key1 key3/]} |
|
991 |
], |
|
992 |
where => {'table1.key1' => 1} |
|
993 |
); |
|
994 |
is_deeply($result->one, |
|
995 |
{key1 => 1, key2 => 2, 'table2__key1' => 1, 'table2__key3' => 3}); |
|
996 |
is_deeply($model2->select->one, {key1 => 1, key3 => 3}); |
|
cleanup test
|
997 | |
cleanup test
|
998 |
$dbi->separator('-'); |
999 |
$model = $dbi->model('table1'); |
|
1000 |
$result = $model->select( |
|
1001 |
column => [ |
|
1002 |
$model->mycolumn, |
|
1003 |
{table2 => [qw/key1 key3/]} |
|
1004 |
], |
|
1005 |
where => {'table1.key1' => 1} |
|
1006 |
); |
|
1007 |
is_deeply($result->one, |
|
1008 |
{key1 => 1, key2 => 2, 'table2-key1' => 1, 'table2-key3' => 3}); |
|
1009 |
is_deeply($model2->select->one, {key1 => 1, key3 => 3}); |
|
cleanup test
|
1010 | |
cleanup test
|
1011 | |
1012 |
test 'filter_off'; |
|
test cleanup
|
1013 |
$dbi = DBIx::Custom->connect; |
cleanup test
|
1014 |
eval { $dbi->execute('drop table table1') }; |
1015 |
eval { $dbi->execute('drop table table2') }; |
|
1016 |
$dbi->execute($create_table1); |
|
test cleanup
|
1017 |
$dbi->execute($create_table2); |
cleanup test
|
1018 | |
1019 |
$dbi->create_model( |
|
1020 |
table => 'table1', |
|
1021 |
join => [ |
|
1022 |
'left outer join table2 on table1.key1 = table2.key1' |
|
1023 |
], |
|
1024 |
primary_key => ['key1'], |
|
cleanup test
|
1025 |
); |
cleanup test
|
1026 |
$dbi->setup_model; |
1027 |
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2}); |
|
1028 |
$model = $dbi->model('table1'); |
|
1029 |
$result = $model->select(column => 'key1'); |
|
1030 |
$result->filter(key1 => sub { $_[0] * 2 }); |
|
1031 |
is_deeply($result->one, {key1 => 2}); |
|
cleanup test
|
1032 | |
cleanup test
|
1033 |
test 'available_datetype'; |
test cleanup
|
1034 |
$dbi = DBIx::Custom->connect; |
cleanup test
|
1035 |
ok($dbi->can('available_datatype')); |
1036 | ||
cleanup test
|
1037 | |
cleanup test
|
1038 |
test 'select prefix option'; |
test cleanup
|
1039 |
$dbi = DBIx::Custom->connect; |
cleanup test
|
1040 |
eval { $dbi->execute('drop table table1') }; |
1041 |
$dbi->execute($create_table1); |
|
1042 |
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2}); |
|
1043 |
$rows = $dbi->select(prefix => 'key1,', column => 'key2', table => 'table1')->all; |
|
1044 |
is_deeply($rows, [{key1 => 1, key2 => 2}], "table"); |
|
1045 | ||
1046 | ||
1047 |
test 'separator'; |
|
1048 |
$dbi = DBIx::Custom->connect; |
|
1049 |
is($dbi->separator, '.'); |
|
1050 |
$dbi->separator('-'); |
|
1051 |
is($dbi->separator, '-'); |
|
1052 |
$dbi->separator('__'); |
|
1053 |
is($dbi->separator, '__'); |
|
1054 |
eval { $dbi->separator('?') }; |
|
1055 |
like($@, qr/Separator/); |
|
1056 | ||
1057 | ||
1058 |
test 'map_param'; |
|
1059 |
$dbi = DBIx::Custom->connect; |
|
1060 |
$param = $dbi->map_param( |
|
1061 |
{id => 1, author => 'Ken', price => 1900}, |
|
1062 |
id => 'book.id', |
|
1063 |
author => ['book.author', sub { '%' . $_[0] . '%' }], |
|
1064 |
price => ['book.price', {if => sub { $_[0] eq 1900 }}] |
|
cleanup test
|
1065 |
); |
cleanup test
|
1066 |
is_deeply($param, {'book.id' => 1, 'book.author' => '%Ken%', |
1067 |
'book.price' => 1900}); |
|
1068 | ||
1069 |
$param = $dbi->map_param( |
|
1070 |
{id => 0, author => 0, price => 0}, |
|
1071 |
id => 'book.id', |
|
1072 |
author => ['book.author', sub { '%' . $_[0] . '%' }], |
|
1073 |
price => ['book.price', sub { '%' . $_[0] . '%' }, |
|
1074 |
{if => sub { $_[0] eq 0 }}] |
|
cleanup test
|
1075 |
); |
cleanup test
|
1076 |
is_deeply($param, {'book.id' => 0, 'book.author' => '%0%', 'book.price' => '%0%'}); |
cleanup test
|
1077 | |
cleanup test
|
1078 |
$param = $dbi->map_param( |
1079 |
{id => '', author => '', price => ''}, |
|
1080 |
id => 'book.id', |
|
1081 |
author => ['book.author', sub { '%' . $_[0] . '%' }], |
|
1082 |
price => ['book.price', sub { '%' . $_[0] . '%' }, |
|
1083 |
{if => sub { $_[0] eq 1 }}] |
|
cleanup test
|
1084 |
); |
cleanup test
|
1085 |
is_deeply($param, {}); |
1086 | ||
1087 |
$param = $dbi->map_param( |
|
1088 |
{id => undef, author => undef, price => undef}, |
|
1089 |
id => 'book.id', |
|
1090 |
price => ['book.price', {if => 'exists'}] |
|
cleanup test
|
1091 |
); |
cleanup test
|
1092 |
is_deeply($param, {'book.price' => undef}); |
1093 | ||
1094 |
$param = $dbi->map_param( |
|
1095 |
{price => 'a'}, |
|
1096 |
id => ['book.id', {if => 'exists'}], |
|
1097 |
price => ['book.price', sub { '%' . $_[0] }, {if => 'exists'}] |
|
1098 |
); |
|
1099 |
is_deeply($param, {'book.price' => '%a'}); |
|
cleanup test
|
1100 | |
cleanup test
|
1101 | |
1102 |
test 'table_alias'; |
|
test cleanup
|
1103 |
$dbi = DBIx::Custom->connect; |
cleanup test
|
1104 |
eval { $dbi->execute('drop table table1') }; |
1105 |
$dbi->execute("create table table1 (key1 Date, key2 datetime)"); |
|
cleanup test
|
1106 |
$dbi->type_rule( |
1107 |
into1 => { |
|
cleanup test
|
1108 |
date => sub { uc $_[0] } |
cleanup test
|
1109 |
} |
1110 |
); |
|
cleanup test
|
1111 |
$dbi->execute("insert into table1 (key1) values (:table2.key1)", {'table2.key1' => 'a'}, |
1112 |
table_alias => {table2 => 'table1'}); |
|
cleanup test
|
1113 |
$result = $dbi->select(table => 'table1'); |
cleanup test
|
1114 |
is($result->one->{key1}, 'A'); |
cleanup test
|
1115 | |
1116 | ||
cleanup test
|
1117 |
test 'order'; |
test cleanup
|
1118 |
$dbi = DBIx::Custom->connect; |
cleanup test
|
1119 |
eval { $dbi->execute('drop table table1') }; |
1120 |
$dbi->execute("create table table1 (key1, key2)"); |
|
1121 |
$dbi->insert({key1 => 1, key2 => 1}, table => 'table1'); |
|
1122 |
$dbi->insert({key1 => 1, key2 => 3}, table => 'table1'); |
|
1123 |
$dbi->insert({key1 => 2, key2 => 2}, table => 'table1'); |
|
1124 |
$dbi->insert({key1 => 2, key2 => 4}, table => 'table1'); |
|
1125 |
my $order = $dbi->order; |
|
1126 |
$order->prepend('key1', 'key2 desc'); |
|
1127 |
$result = $dbi->select(table => 'table1', append => "$order"); |
|
1128 |
is_deeply($result->all, [{key1 => 1, key2 => 3}, {key1 => 1, key2 => 1}, |
|
1129 |
{key1 => 2, key2 => 4}, {key1 => 2, key2 => 2}]); |
|
1130 |
$order->prepend('key1 desc'); |
|
1131 |
$result = $dbi->select(table => 'table1', append => "$order"); |
|
1132 |
is_deeply($result->all, [{key1 => 2, key2 => 4}, {key1 => 2, key2 => 2}, |
|
1133 |
{key1 => 1, key2 => 3}, {key1 => 1, key2 => 1}]); |
|
cleanup test
|
1134 | |
cleanup test
|
1135 |
$order = $dbi->order; |
1136 |
$order->prepend(['table1-key1'], [qw/table1-key2 desc/]); |
|
1137 |
$result = $dbi->select(table => 'table1', |
|
1138 |
column => [[key1 => 'table1-key1'], [key2 => 'table1-key2']], |
|
1139 |
append => "$order"); |
|
1140 |
is_deeply($result->all, [{'table1-key1' => 1, 'table1-key2' => 3}, |
|
1141 |
{'table1-key1' => 1, 'table1-key2' => 1}, |
|
1142 |
{'table1-key1' => 2, 'table1-key2' => 4}, |
|
1143 |
{'table1-key1' => 2, 'table1-key2' => 2}]); |
|
cleanup test
|
1144 | |
cleanup test
|
1145 |
test 'tag_parse'; |
test cleanup
|
1146 |
$dbi = DBIx::Custom->connect; |
cleanup test
|
1147 |
$dbi->tag_parse(0); |
1148 |
eval { $dbi->execute('drop table table1') }; |
|
1149 |
$dbi->execute("create table table1 (key1, key2)"); |
|
1150 |
$dbi->insert({key1 => 1, key2 => 1}, table => 'table1'); |
|
1151 |
eval {$dbi->execute("select * from table1 where {= key1}", {key1 => 1})}; |
|
1152 |
ok($@); |
|
cleanup test
|
1153 | |
cleanup test
|
1154 |
test 'last_sql'; |
test cleanup
|
1155 |
$dbi = DBIx::Custom->connect; |
cleanup test
|
1156 |
eval { $dbi->execute('drop table table1') }; |
1157 |
$dbi->execute("create table table1 (key1, key2)"); |
|
1158 |
$dbi->execute('select * from table1'); |
|
1159 |
is($dbi->last_sql, 'select * from table1;'); |
|
cleanup test
|
1160 | |
cleanup test
|
1161 |
eval{$dbi->execute("aaa")}; |
1162 |
is($dbi->last_sql, 'aaa;'); |
|
cleanup test
|
1163 | |
cleanup test
|
1164 |
test 'DBIx::Custom header'; |
test cleanup
|
1165 |
$dbi = DBIx::Custom->connect; |
cleanup test
|
1166 |
eval { $dbi->execute('drop table table1') }; |
1167 |
$dbi->execute("create table table1 (key1, key2)"); |
|
1168 |
$result = $dbi->execute('select key1 as h1, key2 as h2 from table1'); |
|
1169 |
is_deeply($result->header, [qw/h1 h2/]); |
|
cleanup test
|
1170 | |
cleanup test
|
1171 |
test 'Named placeholder :name(operater) syntax'; |
1172 |
$dbi->execute('drop table table1'); |
|
1173 |
$dbi->execute($create_table1_2); |
|
1174 |
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5}); |
|
1175 |
$dbi->insert(table => 'table1', param => {key1 => 6, key2 => 7, key3 => 8, key4 => 9, key5 => 10}); |
|
cleanup test
|
1176 | |
cleanup test
|
1177 |
$source = "select * from table1 where :key1{=} and :key2{=}"; |
1178 |
$result = $dbi->execute($source, param => {key1 => 1, key2 => 2}); |
|
1179 |
$rows = $result->all; |
|
1180 |
is_deeply($rows, [{key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5}]); |
|
cleanup test
|
1181 | |
cleanup test
|
1182 |
$source = "select * from table1 where :key1{ = } and :key2{=}"; |
1183 |
$result = $dbi->execute($source, param => {key1 => 1, key2 => 2}); |
|
1184 |
$rows = $result->all; |
|
1185 |
is_deeply($rows, [{key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5}]); |
|
cleanup test
|
1186 | |
cleanup test
|
1187 |
$source = "select * from table1 where :key1{<} and :key2{=}"; |
1188 |
$result = $dbi->execute($source, param => {key1 => 5, key2 => 2}); |
|
1189 |
$rows = $result->all; |
|
1190 |
is_deeply($rows, [{key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5}]); |
|
cleanup test
|
1191 | |
cleanup test
|
1192 |
$source = "select * from table1 where :table1.key1{=} and :table1.key2{=}"; |
1193 |
$result = $dbi->execute( |
|
1194 |
$source, |
|
1195 |
param => {'table1.key1' => 1, 'table1.key2' => 1}, |
|
1196 |
filter => {'table1.key2' => sub { $_[0] * 2 }} |
|
cleanup test
|
1197 |
); |
cleanup test
|
1198 |
$rows = $result->all; |
1199 |
is_deeply($rows, [{key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5}]); |
|
cleanup test
|
1200 | |
cleanup test
|
1201 |
test 'high perfomance way'; |
1202 |
$dbi->execute('drop table table1'); |
|
1203 |
$dbi->execute("create table table1 (ab, bc, ik, hi, ui, pq, dc);"); |
|
1204 |
$rows = [ |
|
1205 |
{ab => 1, bc => 2, ik => 3, hi => 4, ui => 5, pq => 6, dc => 7}, |
|
1206 |
{ab => 1, bc => 2, ik => 3, hi => 4, ui => 5, pq => 6, dc => 8}, |
|
1207 |
]; |
|
1208 |
{ |
|
1209 |
my $query; |
|
1210 |
foreach my $row (@$rows) { |
|
1211 |
$query ||= $dbi->insert($row, table => 'table1', query => 1); |
|
1212 |
$dbi->execute($query, $row, filter => {ab => sub { $_[0] * 2 }}); |
|
cleanup test
|
1213 |
} |
cleanup test
|
1214 |
is_deeply($dbi->select(table => 'table1')->all, |
1215 |
[ |
|
1216 |
{ab => 2, bc => 2, ik => 3, hi => 4, ui => 5, pq => 6, dc => 7}, |
|
1217 |
{ab => 2, bc => 2, ik => 3, hi => 4, ui => 5, pq => 6, dc => 8}, |
|
1218 |
] |
|
1219 |
); |
|
1220 |
} |
|
1221 | ||
1222 |
$dbi->execute('drop table table1'); |
|
1223 |
$dbi->execute("create table table1 (ab, bc, ik, hi, ui, pq, dc);"); |
|
1224 |
$rows = [ |
|
1225 |
{ab => 1, bc => 2, ik => 3, hi => 4, ui => 5, pq => 6, dc => 7}, |
|
1226 |
{ab => 1, bc => 2, ik => 3, hi => 4, ui => 5, pq => 6, dc => 8}, |
|
1227 |
]; |
|
1228 |
{ |
|
1229 |
my $query; |
|
1230 |
my $sth; |
|
1231 |
foreach my $row (@$rows) { |
|
1232 |
$query ||= $dbi->insert($row, table => 'table1', query => 1); |
|
1233 |
$sth ||= $query->sth; |
|
1234 |
$sth->execute(map { $row->{$_} } sort keys %$row); |
|
cleanup test
|
1235 |
} |
cleanup test
|
1236 |
is_deeply($dbi->select(table => 'table1')->all, |
1237 |
[ |
|
1238 |
{ab => 1, bc => 2, ik => 3, hi => 4, ui => 5, pq => 6, dc => 7}, |
|
1239 |
{ab => 1, bc => 2, ik => 3, hi => 4, ui => 5, pq => 6, dc => 8}, |
|
1240 |
] |
|
1241 |
); |
|
1242 |
} |
|
cleanup test
|
1243 | |
cleanup test
|
1244 |
test 'result'; |
test cleanup
|
1245 |
$dbi = DBIx::Custom->connect; |
cleanup test
|
1246 |
eval { $dbi->execute('drop table table1') }; |
1247 |
$dbi->execute($create_table1); |
|
1248 |
$dbi->insert({key1 => 1, key2 => 2}, table => 'table1'); |
|
1249 |
$dbi->insert({key1 => 3, key2 => 4}, table => 'table1'); |
|
1250 | ||
cleanup test
|
1251 |
$result = $dbi->select(table => 'table1'); |
cleanup test
|
1252 |
@rows = (); |
1253 |
while (my $row = $result->fetch) { |
|
1254 |
push @rows, [@$row]; |
|
1255 |
} |
|
1256 |
is_deeply(\@rows, [[1, 2], [3, 4]]); |
|
cleanup test
|
1257 | |
1258 |
$result = $dbi->select(table => 'table1'); |
|
cleanup test
|
1259 |
@rows = (); |
1260 |
while (my $row = $result->fetch_hash) { |
|
1261 |
push @rows, {%$row}; |
|
1262 |
} |
|
1263 |
is_deeply(\@rows, [{key1 => 1, key2 => 2}, {key1 => 3, key2 => 4}]); |
|
cleanup test
|
1264 | |
1265 |
$result = $dbi->select(table => 'table1'); |
|
cleanup test
|
1266 |
$row = $result->fetch_first; |
1267 |
is_deeply($row, [1, 2], "row"); |
|
1268 |
$row = $result->fetch; |
|
1269 |
ok(!$row, "finished"); |
|
1270 | ||
cleanup test
|
1271 |
$result = $dbi->select(table => 'table1'); |
cleanup test
|
1272 |
$row = $result->fetch_hash_first; |
1273 |
is_deeply($row, {key1 => 1, key2 => 2}, "row"); |
|
1274 |
$row = $result->fetch_hash; |
|
1275 |
ok(!$row, "finished"); |
|
1276 | ||
1277 |
$dbi->execute('create table table2 (key1, key2);'); |
|
1278 |
$result = $dbi->select(table => 'table2'); |
|
1279 |
$row = $result->fetch_hash_first; |
|
1280 |
ok(!$row, "no row fetch"); |
|
cleanup test
|
1281 | |
test cleanup
|
1282 |
$dbi = DBIx::Custom->connect; |
cleanup test
|
1283 |
eval { $dbi->execute('drop table table1') }; |
1284 |
$dbi->execute($create_table1); |
|
1285 |
$dbi->insert({key1 => 1, key2 => 2}, table => 'table1'); |
|
1286 |
$dbi->insert({key1 => 3, key2 => 4}, table => 'table1'); |
|
1287 |
$dbi->insert({key1 => 5, key2 => 6}, table => 'table1'); |
|
1288 |
$dbi->insert({key1 => 7, key2 => 8}, table => 'table1'); |
|
1289 |
$dbi->insert({key1 => 9, key2 => 10}, table => 'table1'); |
|
cleanup test
|
1290 |
$result = $dbi->select(table => 'table1'); |
cleanup test
|
1291 |
$rows = $result->fetch_multi(2); |
1292 |
is_deeply($rows, [[1, 2], |
|
1293 |
[3, 4]], "fetch_multi first"); |
|
1294 |
$rows = $result->fetch_multi(2); |
|
1295 |
is_deeply($rows, [[5, 6], |
|
1296 |
[7, 8]], "fetch_multi secound"); |
|
1297 |
$rows = $result->fetch_multi(2); |
|
1298 |
is_deeply($rows, [[9, 10]], "fetch_multi third"); |
|
1299 |
$rows = $result->fetch_multi(2); |
|
1300 |
ok(!$rows); |
|
1301 | ||
cleanup test
|
1302 |
$result = $dbi->select(table => 'table1'); |
cleanup test
|
1303 |
eval {$result->fetch_multi}; |
1304 |
like($@, qr/Row count must be specified/, "Not specified row count"); |
|
cleanup test
|
1305 | |
1306 |
$result = $dbi->select(table => 'table1'); |
|
cleanup test
|
1307 |
$rows = $result->fetch_hash_multi(2); |
1308 |
is_deeply($rows, [{key1 => 1, key2 => 2}, |
|
1309 |
{key1 => 3, key2 => 4}], "fetch_multi first"); |
|
1310 |
$rows = $result->fetch_hash_multi(2); |
|
1311 |
is_deeply($rows, [{key1 => 5, key2 => 6}, |
|
1312 |
{key1 => 7, key2 => 8}], "fetch_multi secound"); |
|
1313 |
$rows = $result->fetch_hash_multi(2); |
|
1314 |
is_deeply($rows, [{key1 => 9, key2 => 10}], "fetch_multi third"); |
|
1315 |
$rows = $result->fetch_hash_multi(2); |
|
1316 |
ok(!$rows); |
|
1317 | ||
cleanup test
|
1318 |
$result = $dbi->select(table => 'table1'); |
cleanup test
|
1319 |
eval {$result->fetch_hash_multi}; |
1320 |
like($@, qr/Row count must be specified/, "Not specified row count"); |
|
cleanup test
|
1321 | |
test cleanup
|
1322 |
$dbi = DBIx::Custom->connect; |
cleanup test
|
1323 |
eval { $dbi->execute('drop table table1') }; |
cleanup test
|
1324 |
$dbi->execute($create_table1); |
cleanup test
|
1325 |
$dbi->insert({key1 => 1, key2 => 2}, table => 'table1'); |
1326 |
$dbi->insert({key1 => 3, key2 => 4}, table => 'table1'); |
|
cleanup test
|
1327 | |
cleanup test
|
1328 |
test 'fetch_all'; |
1329 |
$result = $dbi->select(table => 'table1'); |
|
1330 |
$rows = $result->fetch_all; |
|
1331 |
is_deeply($rows, [[1, 2], [3, 4]]); |
|
cleanup test
|
1332 | |
cleanup test
|
1333 |
$result = $dbi->select(table => 'table1'); |
1334 |
$rows = $result->fetch_hash_all; |
|
1335 |
is_deeply($rows, [{key1 => 1, key2 => 2}, {key1 => 3, key2 => 4}]); |
|
cleanup test
|
1336 | |
cleanup test
|
1337 |
$result = $dbi->select(table => 'table1'); |
1338 |
$result->dbi->filters({three_times => sub { $_[0] * 3}}); |
|
1339 |
$result->filter({key1 => 'three_times'}); |
|
cleanup test
|
1340 | |
cleanup test
|
1341 |
$rows = $result->fetch_all; |
1342 |
is_deeply($rows, [[3, 2], [9, 4]], "array"); |
|
cleanup test
|
1343 | |
cleanup test
|
1344 |
$result = $dbi->select(table => 'table1'); |
1345 |
$result->dbi->filters({three_times => sub { $_[0] * 3}}); |
|
1346 |
$result->filter({key1 => 'three_times'}); |
|
1347 |
$rows = $result->fetch_hash_all; |
|
1348 |
is_deeply($rows, [{key1 => 3, key2 => 2}, {key1 => 9, key2 => 4}], "hash"); |
|
1349 | ||
1350 |
test "query_builder"; |
|
1351 |
$datas = [ |
|
1352 |
# Basic tests |
|
1353 |
{ name => 'placeholder basic', |
|
1354 |
source => "a {? k1} b {= k2} {<> k3} {> k4} {< k5} {>= k6} {<= k7} {like k8}", , |
|
1355 |
sql_expected => "a ? b k2 = ? k3 <> ? k4 > ? k5 < ? k6 >= ? k7 <= ? k8 like ?;", |
|
1356 |
columns_expected => [qw/k1 k2 k3 k4 k5 k6 k7 k8/] |
|
1357 |
}, |
|
1358 |
{ |
|
1359 |
name => 'placeholder in', |
|
1360 |
source => "{in k1 3};", |
|
1361 |
sql_expected => "k1 in (?, ?, ?);", |
|
1362 |
columns_expected => [qw/k1 k1 k1/] |
|
1363 |
}, |
|
1364 |
|
|
1365 |
# Table name |
|
1366 |
{ |
|
1367 |
name => 'placeholder with table name', |
|
1368 |
source => "{= a.k1} {= a.k2}", |
|
1369 |
sql_expected => "a.k1 = ? a.k2 = ?;", |
|
1370 |
columns_expected => [qw/a.k1 a.k2/] |
|
1371 |
}, |
|
1372 |
{ |
|
1373 |
name => 'placeholder in with table name', |
|
1374 |
source => "{in a.k1 2} {in b.k2 2}", |
|
1375 |
sql_expected => "a.k1 in (?, ?) b.k2 in (?, ?);", |
|
1376 |
columns_expected => [qw/a.k1 a.k1 b.k2 b.k2/] |
|
1377 |
}, |
|
1378 |
{ |
|
1379 |
name => 'not contain tag', |
|
1380 |
source => "aaa", |
|
1381 |
sql_expected => "aaa;", |
|
1382 |
columns_expected => [], |
|
1383 |
} |
|
1384 |
]; |
|
1385 | ||
1386 |
for (my $i = 0; $i < @$datas; $i++) { |
|
1387 |
my $data = $datas->[$i]; |
|
1388 |
my $builder = DBIx::Custom->new->query_builder; |
|
1389 |
my $query = $builder->build_query($data->{source}); |
|
1390 |
is($query->{sql}, $data->{sql_expected}, "$data->{name} : sql"); |
|
1391 |
is_deeply($query->columns, $data->{columns_expected}, "$data->{name} : columns"); |
|
1392 |
} |
|
1393 | ||
1394 |
$builder = DBIx::Custom->new->query_builder; |
|
1395 |
$ret_val = $builder->register_tag( |
|
1396 |
p => sub { |
|
1397 |
my @args = @_; |
|
1398 |
|
|
1399 |
my $expand = "? $args[0] $args[1]"; |
|
1400 |
my $columns = [2]; |
|
1401 |
return [$expand, $columns]; |
|
1402 |
} |
|
cleanup test
|
1403 |
); |
1404 | ||
cleanup test
|
1405 |
$query = $builder->build_query("{p a b}"); |
1406 |
is($query->{sql}, "? a b;", "register_tag sql"); |
|
1407 |
is_deeply($query->{columns}, [2], "register_tag columns"); |
|
1408 |
isa_ok($ret_val, 'DBIx::Custom::QueryBuilder'); |
|
cleanup test
|
1409 | |
cleanup test
|
1410 |
$builder = DBIx::Custom->new->query_builder; |
cleanup test
|
1411 | |
cleanup test
|
1412 |
eval{$builder->build_query('{? }')}; |
1413 |
like($@, qr/\QColumn name must be specified in tag "{? }"/, "? not arguments"); |
|
cleanup test
|
1414 | |
cleanup test
|
1415 |
eval{$builder->build_query("{a }")}; |
1416 |
like($@, qr/\QTag "a" is not registered/, "tag not exist"); |
|
cleanup test
|
1417 | |
cleanup test
|
1418 |
$builder->register_tag({ |
1419 |
q => 'string' |
|
1420 |
}); |
|
cleanup test
|
1421 | |
cleanup test
|
1422 |
eval{$builder->build_query("{q}", {})}; |
1423 |
like($@, qr/Tag "q" must be sub reference/, "tag not code ref"); |
|
cleanup test
|
1424 | |
cleanup test
|
1425 |
$builder->register_tag({ |
1426 |
r => sub {} |
|
1427 |
}); |
|
cleanup test
|
1428 | |
cleanup test
|
1429 |
eval{$builder->build_query("{r}")}; |
1430 |
like($@, qr/\QTag "r" must return [STRING, ARRAY_REFERENCE]/, "tag return noting"); |
|
1431 | ||
1432 |
$builder->register_tag({ |
|
1433 |
s => sub { return ["a", ""]} |
|
1434 |
}); |
|
1435 | ||
1436 |
eval{$builder->build_query("{s}")}; |
|
1437 |
like($@, qr/\QTag "s" must return [STRING, ARRAY_REFERENCE]/, "tag return not array columns"); |
|
1438 | ||
1439 |
$builder->register_tag( |
|
1440 |
t => sub {return ["a", []]} |
|
cleanup test
|
1441 |
); |
1442 | ||
cleanup test
|
1443 | |
1444 |
test 'General error case'; |
|
1445 |
$builder = DBIx::Custom->new->query_builder; |
|
1446 |
$builder->register_tag( |
|
1447 |
a => sub { |
|
1448 |
return ["? ? ?", ['']]; |
|
1449 |
} |
|
cleanup test
|
1450 |
); |
cleanup test
|
1451 |
eval{$builder->build_query("{a}")}; |
1452 |
like($@, qr/\QPlaceholder count/, "placeholder count is invalid"); |
|
cleanup test
|
1453 | |
cleanup test
|
1454 | |
1455 |
test 'Default tag Error case'; |
|
1456 |
eval{$builder->build_query("{= }")}; |
|
1457 |
like($@, qr/Column name must be specified in tag "{= }"/, "basic '=' : key not exist"); |
|
1458 | ||
1459 |
eval{$builder->build_query("{in }")}; |
|
1460 |
like($@, qr/Column name and count of values must be specified in tag "{in }"/, "in : key not exist"); |
|
1461 | ||
1462 |
eval{$builder->build_query("{in a}")}; |
|
1463 |
like($@, qr/\QColumn name and count of values must be specified in tag "{in }"/, |
|
1464 |
"in : key not exist"); |
|
1465 | ||
1466 |
eval{$builder->build_query("{in a r}")}; |
|
1467 |
like($@, qr/\QColumn name and count of values must be specified in tag "{in }"/, |
|
1468 |
"in : key not exist"); |
|
1469 | ||
1470 |
test 'variouse source'; |
|
1471 |
$source = "a {= b} c \\{ \\} {= \\{} {= \\}} d;"; |
|
1472 |
$query = $builder->build_query($source); |
|
1473 |
is($query->sql, 'a b = ? c { } { = ? } = ? d;', "basic : 1"); |
|
1474 | ||
1475 |
$source = "abc;"; |
|
1476 |
$query = $builder->build_query($source); |
|
1477 |
is($query->sql, 'abc;', "basic : 2"); |
|
1478 | ||
1479 |
$source = "{= a}"; |
|
1480 |
$query = $builder->build_query($source); |
|
1481 |
is($query->sql, 'a = ?;', "only tag"); |
|
1482 | ||
1483 |
$source = "000;"; |
|
1484 |
$query = $builder->build_query($source); |
|
1485 |
is($query->sql, '000;', "contain 0 value"); |
|
1486 | ||
1487 |
$source = "a {= b} }"; |
|
1488 |
eval{$builder->build_query($source)}; |
|
1489 |
like($@, qr/unexpected "}"/, "error : 1"); |
|
1490 | ||
1491 |
$source = "a {= {}"; |
|
1492 |
eval{$builder->build_query($source)}; |
|
1493 |
like($@, qr/unexpected "{"/, "error : 2"); |
|
1494 | ||
test cleanup
|
1495 | |
1496 | ||
1497 | ||
1498 | ||
1499 | ||
1500 | ||
1501 | ||
1502 | ||
1503 | ||
1504 | ||
1505 | ||
1506 | ||
1507 | ||
1508 | ||
1509 | ||
1510 | ||
1511 | ||
1512 | ||
1513 | ||
1514 | ||
1515 | ||
1516 | ||
1517 | ||
1518 | ||
1519 | ||
1520 | ||
1521 |
### a little complex test |
|
test cleanup
|
1522 | |
1523 |
test 'update_param'; |
|
1524 |
$dbi = DBIx::Custom->connect; |
|
1525 |
eval { $dbi->execute('drop table table1') }; |
|
1526 |
$dbi->execute($create_table1_2); |
|
1527 |
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5}); |
|
1528 |
$dbi->insert(table => 'table1', param => {key1 => 6, key2 => 7, key3 => 8, key4 => 9, key5 => 10}); |
|
1529 | ||
1530 |
$param = {key2 => 11}; |
|
1531 |
$update_param = $dbi->update_param($param); |
|
1532 |
$sql = <<"EOS"; |
|
1533 |
update table1 $update_param |
|
1534 |
where key1 = 1 |
|
1535 |
EOS |
|
1536 |
$dbi->execute($sql, param => $param); |
|
1537 |
$result = $dbi->execute('select * from table1;', table => 'table1'); |
|
1538 |
$rows = $result->all; |
|
1539 |
is_deeply($rows, [{key1 => 1, key2 => 11, key3 => 3, key4 => 4, key5 => 5}, |
|
1540 |
{key1 => 6, key2 => 7, key3 => 8, key4 => 9, key5 => 10}], |
|
1541 |
"basic"); |
|
1542 | ||
1543 | ||
1544 |
$dbi = DBIx::Custom->connect; |
|
1545 |
eval { $dbi->execute('drop table table1') }; |
|
1546 |
$dbi->execute($create_table1_2); |
|
1547 |
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5}); |
|
1548 |
$dbi->insert(table => 'table1', param => {key1 => 6, key2 => 7, key3 => 8, key4 => 9, key5 => 10}); |
|
1549 | ||
1550 |
$param = {key2 => 11, key3 => 33}; |
|
1551 |
$update_param = $dbi->update_param($param); |
|
1552 |
$sql = <<"EOS"; |
|
1553 |
update table1 $update_param |
|
1554 |
where key1 = 1 |
|
1555 |
EOS |
|
1556 |
$dbi->execute($sql, param => $param); |
|
1557 |
$result = $dbi->execute('select * from table1;', table => 'table1'); |
|
1558 |
$rows = $result->all; |
|
1559 |
is_deeply($rows, [{key1 => 1, key2 => 11, key3 => 33, key4 => 4, key5 => 5}, |
|
1560 |
{key1 => 6, key2 => 7, key3 => 8, key4 => 9, key5 => 10}], |
|
1561 |
"basic"); |
|
1562 | ||
1563 |
$dbi = DBIx::Custom->connect; |
|
1564 |
eval { $dbi->execute('drop table table1') }; |
|
1565 |
$dbi->execute($create_table1_2); |
|
1566 |
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5}); |
|
1567 |
$dbi->insert(table => 'table1', param => {key1 => 6, key2 => 7, key3 => 8, key4 => 9, key5 => 10}); |
|
1568 | ||
1569 |
$param = {key2 => 11, key3 => 33}; |
|
1570 |
$update_param = $dbi->update_param($param, {no_set => 1}); |
|
1571 |
$sql = <<"EOS"; |
|
1572 |
update table1 set $update_param |
|
1573 |
where key1 = 1 |
|
1574 |
EOS |
|
1575 |
$dbi->execute($sql, param => $param); |
|
1576 |
$result = $dbi->execute('select * from table1;', table => 'table1'); |
|
1577 |
$rows = $result->all; |
|
1578 |
is_deeply($rows, [{key1 => 1, key2 => 11, key3 => 33, key4 => 4, key5 => 5}, |
|
1579 |
{key1 => 6, key2 => 7, key3 => 8, key4 => 9, key5 => 10}], |
|
1580 |
"update param no_set"); |
|
1581 | ||
1582 |
|
|
1583 |
eval { $dbi->update_param({";" => 1}) }; |
|
1584 |
like($@, qr/not safety/); |
|
1585 | ||
1586 | ||
1587 |
test 'update_param'; |
|
1588 |
$dbi = DBIx::Custom->connect; |
|
1589 |
eval { $dbi->execute('drop table table1') }; |
|
1590 |
$dbi->execute($create_table1_2); |
|
1591 |
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5}); |
|
1592 |
$dbi->insert(table => 'table1', param => {key1 => 6, key2 => 7, key3 => 8, key4 => 9, key5 => 10}); |
|
1593 | ||
1594 |
$param = {key2 => 11}; |
|
1595 |
$update_param = $dbi->assign_param($param); |
|
1596 |
$sql = <<"EOS"; |
|
1597 |
update table1 set $update_param |
|
1598 |
where key1 = 1 |
|
1599 |
EOS |
|
1600 |
$dbi->execute($sql, param => $param, table => 'table1'); |
|
1601 |
$result = $dbi->execute('select * from table1;'); |
|
1602 |
$rows = $result->all; |
|
1603 |
is_deeply($rows, [{key1 => 1, key2 => 11, key3 => 3, key4 => 4, key5 => 5}, |
|
1604 |
{key1 => 6, key2 => 7, key3 => 8, key4 => 9, key5 => 10}], |
|
1605 |
"basic"); |
|
1606 | ||
1607 | ||
cleanup test
|
1608 |
test 'type option'; # DEPRECATED! |
1609 |
$dbi = DBIx::Custom->connect( |
|
1610 |
data_source => 'dbi:SQLite:dbname=:memory:', |
|
1611 |
dbi_option => { |
|
1612 |
$DBD::SQLite::VERSION > 1.26 ? (sqlite_unicode => 1) : (unicode => 1) |
|
1613 |
} |
|
cleanup test
|
1614 |
); |
cleanup test
|
1615 |
$binary = pack("I3", 1, 2, 3); |
1616 |
eval { $dbi->execute('drop table table1') }; |
|
1617 |
$dbi->execute('create table table1(key1, key2)'); |
|
1618 |
$dbi->insert(table => 'table1', param => {key1 => $binary, key2 => 'あ'}, type => [key1 => DBI::SQL_BLOB]); |
|
1619 |
$result = $dbi->select(table => 'table1'); |
|
1620 |
$row = $result->one; |
|
1621 |
is_deeply($row, {key1 => $binary, key2 => 'あ'}, "basic"); |
|
1622 |
$result = $dbi->execute('select length(key1) as key1_length from table1'); |
|
1623 |
$row = $result->one; |
|
1624 |
is($row->{key1_length}, length $binary); |
|
cleanup test
|
1625 | |
cleanup test
|
1626 |
test 'type_rule from'; |
1627 |
$dbi = DBIx::Custom->connect; |
|
1628 |
$dbi->type_rule( |
|
1629 |
from1 => { |
|
1630 |
date => sub { uc $_[0] } |
|
1631 |
} |
|
cleanup test
|
1632 |
); |
cleanup test
|
1633 |
$dbi->execute("create table table1 (key1 Date, key2 datetime)"); |
1634 |
$dbi->insert({key1 => 'a'}, table => 'table1'); |
|
1635 |
$result = $dbi->select(table => 'table1'); |
|
1636 |
is($result->fetch_first->[0], 'A'); |
|
cleanup test
|
1637 | |
cleanup test
|
1638 |
$result = $dbi->select(table => 'table1'); |
1639 |
is($result->one->{key1}, 'A'); |
|
cleanup test
|
1640 | |
cleanup test
|
1641 | |
1642 |
test 'type_rule into'; |
|
test cleanup
|
1643 |
$dbi = DBIx::Custom->connect; |
cleanup test
|
1644 |
$dbi->execute("create table table1 (key1 Date, key2 datetime)"); |
1645 |
$dbi->type_rule( |
|
1646 |
into1 => { |
|
1647 |
date => sub { uc $_[0] } |
|
1648 |
} |
|
1649 |
); |
|
cleanup test
|
1650 |
$dbi->insert({key1 => 'a'}, table => 'table1'); |
cleanup test
|
1651 |
$result = $dbi->select(table => 'table1'); |
1652 |
is($result->one->{key1}, 'A'); |
|
1653 | ||
test cleanup
|
1654 |
$dbi = DBIx::Custom->connect; |
cleanup test
|
1655 |
$dbi->execute("create table table1 (key1 date, key2 datetime)"); |
1656 |
$dbi->type_rule( |
|
1657 |
into1 => [ |
|
1658 |
[qw/date datetime/] => sub { uc $_[0] } |
|
1659 |
] |
|
1660 |
); |
|
1661 |
$dbi->insert({key1 => 'a', key2 => 'b'}, table => 'table1'); |
|
1662 |
$result = $dbi->select(table => 'table1'); |
|
1663 |
$row = $result->one; |
|
1664 |
is($row->{key1}, 'A'); |
|
1665 |
is($row->{key2}, 'B'); |
|
cleanup test
|
1666 | |
test cleanup
|
1667 |
$dbi = DBIx::Custom->connect; |
cleanup test
|
1668 |
$dbi->execute("create table table1 (key1 Date, key2 datetime)"); |
1669 |
$dbi->insert({key1 => 'a', key2 => 'B'}, table => 'table1'); |
|
1670 |
$dbi->type_rule( |
|
1671 |
into1 => [ |
|
1672 |
[qw/date datetime/] => sub { uc $_[0] } |
|
1673 |
] |
|
1674 |
); |
|
1675 |
$result = $dbi->execute( |
|
1676 |
"select * from table1 where key1 = :key1 and key2 = :table1.key2;", |
|
1677 |
param => {key1 => 'a', 'table1.key2' => 'b'} |
|
1678 |
); |
|
1679 |
$row = $result->one; |
|
1680 |
is($row->{key1}, 'a'); |
|
1681 |
is($row->{key2}, 'B'); |
|
cleanup test
|
1682 | |
test cleanup
|
1683 |
$dbi = DBIx::Custom->connect; |
cleanup test
|
1684 |
$dbi->execute("create table table1 (key1 Date, key2 datetime)"); |
1685 |
$dbi->insert({key1 => 'A', key2 => 'B'}, table => 'table1'); |
|
1686 |
$dbi->type_rule( |
|
1687 |
into1 => [ |
|
1688 |
[qw/date datetime/] => sub { uc $_[0] } |
|
1689 |
] |
|
1690 |
); |
|
1691 |
$result = $dbi->execute( |
|
1692 |
"select * from table1 where key1 = :key1 and key2 = :table1.key2;", |
|
1693 |
param => {key1 => 'a', 'table1.key2' => 'b'}, |
|
1694 |
table => 'table1' |
|
1695 |
); |
|
1696 |
$row = $result->one; |
|
1697 |
is($row->{key1}, 'A'); |
|
1698 |
is($row->{key2}, 'B'); |
|
cleanup test
|
1699 | |
cleanup test
|
1700 |
$dbi = DBIx::Custom->connect; |
1701 |
$dbi->execute("create table table1 (key1 date, key2 datetime)"); |
|
1702 |
$dbi->register_filter(twice => sub { $_[0] * 2 }); |
|
1703 |
$dbi->type_rule( |
|
1704 |
from1 => { |
|
1705 |
date => 'twice', |
|
1706 |
}, |
|
1707 |
into1 => { |
|
1708 |
date => 'twice', |
|
1709 |
} |
|
1710 |
); |
|
1711 |
$dbi->insert({key1 => 2}, table => 'table1'); |
|
1712 |
$result = $dbi->select(table => 'table1'); |
|
1713 |
is($result->fetch->[0], 8); |
|
cleanup test
|
1714 | |
cleanup test
|
1715 |
test 'type_rule and filter order'; |
test cleanup
|
1716 |
$dbi = DBIx::Custom->connect; |
cleanup test
|
1717 |
$dbi->execute("create table table1 (key1 Date, key2 datetime)"); |
1718 |
$dbi->type_rule( |
|
1719 |
into1 => { |
|
1720 |
date => sub { $_[0] . 'b' } |
|
1721 |
}, |
|
1722 |
into2 => { |
|
1723 |
date => sub { $_[0] . 'c' } |
|
1724 |
}, |
|
1725 |
from1 => { |
|
1726 |
date => sub { $_[0] . 'd' } |
|
1727 |
}, |
|
1728 |
from2 => { |
|
1729 |
date => sub { $_[0] . 'e' } |
|
1730 |
} |
|
1731 |
); |
|
1732 |
$dbi->insert({key1 => '1'}, table => 'table1', filter => {key1 => sub { $_[0] . 'a' }}); |
|
1733 |
$result = $dbi->select(table => 'table1'); |
|
1734 |
$result->filter(key1 => sub { $_[0] . 'f' }); |
|
1735 |
is($result->fetch_first->[0], '1abcdef'); |
|
cleanup test
|
1736 | |
cleanup test
|
1737 |
$dbi = DBIx::Custom->connect; |
1738 |
$dbi->execute("create table table1 (key1 Date, key2 datetime)"); |
|
1739 |
$dbi->type_rule( |
|
1740 |
from1 => { |
|
1741 |
date => sub { $_[0] . 'p' } |
|
1742 |
}, |
|
1743 |
from2 => { |
|
1744 |
date => sub { $_[0] . 'q' } |
|
1745 |
}, |
|
1746 |
); |
|
1747 |
$dbi->insert({key1 => '1'}, table => 'table1'); |
|
1748 |
$result = $dbi->select(table => 'table1'); |
|
1749 |
$result->type_rule( |
|
1750 |
from1 => { |
|
1751 |
date => sub { $_[0] . 'd' } |
|
1752 |
}, |
|
1753 |
from2 => { |
|
1754 |
date => sub { $_[0] . 'e' } |
|
1755 |
} |
|
1756 |
); |
|
1757 |
$result->filter(key1 => sub { $_[0] . 'f' }); |
|
1758 |
is($result->fetch_first->[0], '1def'); |
|
cleanup test
|
1759 | |
cleanup test
|
1760 |
test 'type_rule_off'; |
1761 |
$dbi = DBIx::Custom->connect; |
|
1762 |
$dbi->execute("create table table1 (key1 Date, key2 datetime)"); |
|
1763 |
$dbi->type_rule( |
|
1764 |
from1 => { |
|
1765 |
date => sub { $_[0] * 2 }, |
|
1766 |
}, |
|
1767 |
into1 => { |
|
1768 |
date => sub { $_[0] * 2 }, |
|
1769 |
} |
|
1770 |
); |
|
1771 |
$dbi->insert({key1 => 2}, table => 'table1', type_rule_off => 1); |
|
1772 |
$result = $dbi->select(table => 'table1', type_rule_off => 1); |
|
1773 |
is($result->type_rule_off->fetch->[0], 2); |
|
cleanup test
|
1774 | |
cleanup test
|
1775 |
$dbi = DBIx::Custom->connect; |
1776 |
$dbi->execute("create table table1 (key1 Date, key2 datetime)"); |
|
1777 |
$dbi->type_rule( |
|
1778 |
from1 => { |
|
1779 |
date => sub { $_[0] * 2 }, |
|
1780 |
}, |
|
1781 |
into1 => { |
|
1782 |
date => sub { $_[0] * 3 }, |
|
1783 |
} |
|
1784 |
); |
|
1785 |
$dbi->insert({key1 => 2}, table => 'table1', type_rule_off => 1); |
|
1786 |
$result = $dbi->select(table => 'table1', type_rule_off => 1); |
|
1787 |
is($result->one->{key1}, 4); |
|
cleanup test
|
1788 | |
cleanup test
|
1789 |
$dbi = DBIx::Custom->connect; |
1790 |
$dbi->execute("create table table1 (key1 Date, key2 datetime)"); |
|
1791 |
$dbi->type_rule( |
|
1792 |
from1 => { |
|
1793 |
date => sub { $_[0] * 2 }, |
|
1794 |
}, |
|
1795 |
into1 => { |
|
1796 |
date => sub { $_[0] * 3 }, |
|
1797 |
} |
|
1798 |
); |
|
1799 |
$dbi->insert({key1 => 2}, table => 'table1'); |
|
1800 |
$result = $dbi->select(table => 'table1'); |
|
1801 |
is($result->one->{key1}, 12); |
|
cleanup test
|
1802 | |
cleanup test
|
1803 |
$dbi = DBIx::Custom->connect; |
1804 |
$dbi->execute("create table table1 (key1 Date, key2 datetime)"); |
|
1805 |
$dbi->type_rule( |
|
1806 |
from1 => { |
|
1807 |
date => sub { $_[0] * 2 }, |
|
1808 |
}, |
|
1809 |
into1 => { |
|
1810 |
date => sub { $_[0] * 3 }, |
|
1811 |
} |
|
cleanup test
|
1812 |
); |
cleanup test
|
1813 |
$dbi->insert({key1 => 2}, table => 'table1'); |
1814 |
$result = $dbi->select(table => 'table1'); |
|
1815 |
is($result->fetch->[0], 12); |
|
cleanup test
|
1816 | |
cleanup test
|
1817 |
$dbi = DBIx::Custom->connect; |
1818 |
$dbi->execute("create table table1 (key1 Date, key2 datetime)"); |
|
1819 |
$dbi->register_filter(ppp => sub { uc $_[0] }); |
|
1820 |
$dbi->type_rule( |
|
1821 |
into1 => { |
|
1822 |
date => 'ppp' |
|
cleanup test
|
1823 |
} |
cleanup test
|
1824 |
); |
1825 |
$dbi->insert({key1 => 'a'}, table => 'table1'); |
|
1826 |
$result = $dbi->select(table => 'table1'); |
|
1827 |
is($result->one->{key1}, 'A'); |
|
cleanup test
|
1828 | |
cleanup test
|
1829 |
eval{$dbi->type_rule( |
1830 |
into1 => { |
|
1831 |
date => 'pp' |
|
cleanup test
|
1832 |
} |
cleanup test
|
1833 |
)}; |
1834 |
like($@, qr/not registered/); |
|
cleanup test
|
1835 | |
test cleanup
|
1836 |
$dbi = DBIx::Custom->connect; |
cleanup test
|
1837 |
$dbi->execute("create table table1 (key1 Date, key2 datetime)"); |
1838 |
eval { |
|
1839 |
$dbi->type_rule( |
|
1840 |
from1 => { |
|
1841 |
Date => sub { $_[0] * 2 }, |
|
1842 |
} |
|
1843 |
); |
|
1844 |
}; |
|
1845 |
like($@, qr/lower/); |
|
clenup test
|
1846 | |
cleanup test
|
1847 |
eval { |
1848 |
$dbi->type_rule( |
|
1849 |
into1 => { |
|
1850 |
Date => sub { $_[0] * 2 }, |
|
1851 |
} |
|
1852 |
); |
|
1853 |
}; |
|
1854 |
like($@, qr/lower/); |
|
clenup test
|
1855 | |
cleanup test
|
1856 |
$dbi = DBIx::Custom->connect; |
1857 |
$dbi->execute("create table table1 (key1 Date, key2 datetime)"); |
|
1858 |
$dbi->type_rule( |
|
1859 |
from1 => { |
|
1860 |
date => sub { $_[0] * 2 }, |
|
1861 |
}, |
|
1862 |
into1 => { |
|
1863 |
date => sub { $_[0] * 3 }, |
|
1864 |
} |
|
1865 |
); |
|
1866 |
$dbi->insert({key1 => 2}, table => 'table1'); |
|
clenup test
|
1867 |
$result = $dbi->select(table => 'table1'); |
cleanup test
|
1868 |
$result->type_rule_off; |
1869 |
is($result->one->{key1}, 6); |
|
clenup test
|
1870 | |
cleanup test
|
1871 |
$dbi = DBIx::Custom->connect; |
1872 |
$dbi->execute("create table table1 (key1 Date, key2 datetime)"); |
|
1873 |
$dbi->type_rule( |
|
1874 |
from1 => { |
|
1875 |
date => sub { $_[0] * 2 }, |
|
1876 |
datetime => sub { $_[0] * 4 }, |
|
1877 |
}, |
|
1878 |
); |
|
1879 |
$dbi->insert({key1 => 2, key2 => 2}, table => 'table1'); |
|
clenup test
|
1880 |
$result = $dbi->select(table => 'table1'); |
cleanup test
|
1881 |
$result->type_rule( |
1882 |
from1 => { |
|
1883 |
date => sub { $_[0] * 3 } |
|
1884 |
} |
|
1885 |
); |
|
1886 |
$row = $result->one; |
|
1887 |
is($row->{key1}, 6); |
|
1888 |
is($row->{key2}, 2); |
|
clenup test
|
1889 | |
1890 |
$result = $dbi->select(table => 'table1'); |
|
cleanup test
|
1891 |
$result->type_rule( |
1892 |
from1 => { |
|
1893 |
date => sub { $_[0] * 3 } |
|
1894 |
} |
|
1895 |
); |
|
1896 |
$row = $result->one; |
|
1897 |
is($row->{key1}, 6); |
|
1898 |
is($row->{key2}, 2); |
|
clenup test
|
1899 | |
1900 |
$result = $dbi->select(table => 'table1'); |
|
cleanup test
|
1901 |
$result->type_rule( |
1902 |
from1 => { |
|
1903 |
date => sub { $_[0] * 3 } |
|
1904 |
} |
|
1905 |
); |
|
1906 |
$row = $result->one; |
|
1907 |
is($row->{key1}, 6); |
|
1908 |
is($row->{key2}, 2); |
|
clenup test
|
1909 |
$result = $dbi->select(table => 'table1'); |
cleanup test
|
1910 |
$result->type_rule( |
1911 |
from1 => [date => sub { $_[0] * 3 }] |
|
1912 |
); |
|
1913 |
$row = $result->one; |
|
1914 |
is($row->{key1}, 6); |
|
1915 |
is($row->{key2}, 2); |
|
1916 |
$dbi->register_filter(fivetimes => sub { $_[0] * 5}); |
|
clenup test
|
1917 |
$result = $dbi->select(table => 'table1'); |
cleanup test
|
1918 |
$result->type_rule( |
1919 |
from1 => [date => 'fivetimes'] |
|
1920 |
); |
|
1921 |
$row = $result->one; |
|
1922 |
is($row->{key1}, 10); |
|
1923 |
is($row->{key2}, 2); |
|
clenup test
|
1924 |
$result = $dbi->select(table => 'table1'); |
cleanup test
|
1925 |
$result->type_rule( |
1926 |
from1 => [date => undef] |
|
1927 |
); |
|
1928 |
$row = $result->one; |
|
1929 |
is($row->{key1}, 2); |
|
1930 |
is($row->{key2}, 2); |
|
clenup test
|
1931 | |
test cleanup
|
1932 |
$dbi = DBIx::Custom->connect; |
cleanup test
|
1933 |
$dbi->execute("create table table1 (key1 Date, key2 datetime)"); |
1934 |
$dbi->type_rule( |
|
1935 |
from1 => { |
|
1936 |
date => sub { $_[0] * 2 }, |
|
1937 |
}, |
|
1938 |
); |
|
1939 |
$dbi->insert({key1 => 2}, table => 'table1'); |
|
clenup test
|
1940 |
$result = $dbi->select(table => 'table1'); |
cleanup test
|
1941 |
$result->filter(key1 => sub { $_[0] * 3 }); |
1942 |
is($result->one->{key1}, 12); |
|
clenup test
|
1943 | |
cleanup test
|
1944 |
$dbi = DBIx::Custom->connect; |
1945 |
$dbi->execute("create table table1 (key1 Date, key2 datetime)"); |
|
1946 |
$dbi->type_rule( |
|
1947 |
from1 => { |
|
1948 |
date => sub { $_[0] * 2 }, |
|
1949 |
}, |
|
1950 |
); |
|
1951 |
$dbi->insert({key1 => 2}, table => 'table1'); |
|
clenup test
|
1952 |
$result = $dbi->select(table => 'table1'); |
cleanup test
|
1953 |
$result->filter(key1 => sub { $_[0] * 3 }); |
1954 |
is($result->fetch->[0], 12); |
|
clenup test
|
1955 | |
cleanup test
|
1956 |
$dbi = DBIx::Custom->connect; |
1957 |
$dbi->execute("create table table1 (key1 Date, key2 datetime)"); |
|
1958 |
$dbi->type_rule( |
|
1959 |
into1 => { |
|
1960 |
date => sub { $_[0] . 'b' } |
|
1961 |
}, |
|
1962 |
into2 => { |
|
1963 |
date => sub { $_[0] . 'c' } |
|
1964 |
}, |
|
1965 |
from1 => { |
|
1966 |
date => sub { $_[0] . 'd' } |
|
1967 |
}, |
|
1968 |
from2 => { |
|
1969 |
date => sub { $_[0] . 'e' } |
|
1970 |
} |
|
1971 |
); |
|
1972 |
$dbi->insert({key1 => '1'}, table => 'table1', type_rule_off => 1); |
|
clenup test
|
1973 |
$result = $dbi->select(table => 'table1'); |
cleanup test
|
1974 |
is($result->type_rule_off->fetch_first->[0], '1'); |
clenup test
|
1975 |
$result = $dbi->select(table => 'table1'); |
cleanup test
|
1976 |
is($result->type_rule_on->fetch_first->[0], '1de'); |
clenup test
|
1977 | |
cleanup test
|
1978 |
$dbi = DBIx::Custom->connect; |
1979 |
$dbi->execute("create table table1 (key1 Date, key2 datetime)"); |
|
1980 |
$dbi->type_rule( |
|
1981 |
into1 => { |
|
1982 |
date => sub { $_[0] . 'b' } |
|
cleanup test
|
1983 |
}, |
cleanup test
|
1984 |
into2 => { |
1985 |
date => sub { $_[0] . 'c' } |
|
cleanup test
|
1986 |
}, |
cleanup test
|
1987 |
from1 => { |
1988 |
date => sub { $_[0] . 'd' } |
|
cleanup test
|
1989 |
}, |
cleanup test
|
1990 |
from2 => { |
1991 |
date => sub { $_[0] . 'e' } |
|
cleanup test
|
1992 |
} |
1993 |
); |
|
cleanup test
|
1994 |
$dbi->insert({key1 => '1'}, table => 'table1', type_rule1_off => 1); |
1995 |
$result = $dbi->select(table => 'table1'); |
|
1996 |
is($result->type_rule1_off->fetch_first->[0], '1ce'); |
|
1997 |
$result = $dbi->select(table => 'table1'); |
|
1998 |
is($result->type_rule1_on->fetch_first->[0], '1cde'); |
|
cleanup test
|
1999 | |
cleanup test
|
2000 |
$dbi = DBIx::Custom->connect; |
2001 |
$dbi->execute("create table table1 (key1 Date, key2 datetime)"); |
|
2002 |
$dbi->type_rule( |
|
2003 |
into1 => { |
|
2004 |
date => sub { $_[0] . 'b' } |
|
2005 |
}, |
|
2006 |
into2 => { |
|
2007 |
date => sub { $_[0] . 'c' } |
|
2008 |
}, |
|
2009 |
from1 => { |
|
2010 |
date => sub { $_[0] . 'd' } |
|
2011 |
}, |
|
2012 |
from2 => { |
|
2013 |
date => sub { $_[0] . 'e' } |
|
cleanup test
|
2014 |
} |
2015 |
); |
|
cleanup test
|
2016 |
$dbi->insert({key1 => '1'}, table => 'table1', type_rule2_off => 1); |
2017 |
$result = $dbi->select(table => 'table1'); |
|
2018 |
is($result->type_rule2_off->fetch_first->[0], '1bd'); |
|
2019 |
$result = $dbi->select(table => 'table1'); |
|
2020 |
is($result->type_rule2_on->fetch_first->[0], '1bde'); |
|
test cleanup
|
2021 | |
2022 |
test 'prefix'; |
|
2023 |
$dbi = DBIx::Custom->connect; |
|
2024 |
eval { $dbi->execute('drop table table1') }; |
|
2025 |
$dbi->execute("create table table1 (key1 char(255), key2 char(255), primary key(key1))"); |
|
2026 |
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2}); |
|
2027 |
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 4}, prefix => 'or replace'); |
|
2028 |
$result = $dbi->execute('select * from table1;'); |
|
2029 |
$rows = $result->all; |
|
2030 |
is_deeply($rows, [{key1 => 1, key2 => 4}], "basic"); |
|
2031 | ||
2032 |
$dbi = DBIx::Custom->connect; |
|
2033 |
eval { $dbi->execute('drop table table1') }; |
|
2034 |
$dbi->execute("create table table1 (key1 char(255), key2 char(255), primary key(key1))"); |
|
2035 |
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2}); |
|
2036 |
$dbi->update(table => 'table1', param => {key2 => 4}, |
|
2037 |
where => {key1 => 1}, prefix => 'or replace'); |
|
2038 |
$result = $dbi->execute('select * from table1;'); |
|
2039 |
$rows = $result->all; |
|
2040 |
is_deeply($rows, [{key1 => 1, key2 => 4}], "basic"); |
|
2041 | ||
test cleanup
|
2042 |
test 'Model class'; |
2043 |
use MyDBI1; |
|
2044 |
$dbi = MyDBI1->connect; |
|
2045 |
eval { $dbi->execute('drop table book') }; |
|
2046 |
$dbi->execute("create table book (title, author)"); |
|
2047 |
$model = $dbi->model('book'); |
|
2048 |
$model->insert({title => 'a', author => 'b'}); |
|
2049 |
is_deeply($model->list->all, [{title => 'a', author => 'b'}], 'basic'); |
|
2050 |
$dbi->execute("create table company (name)"); |
|
2051 |
$model = $dbi->model('company'); |
|
2052 |
$model->insert({name => 'a'}); |
|
2053 |
is_deeply($model->list->all, [{name => 'a'}], 'basic'); |
|
2054 |
is($dbi->models->{'book'}, $dbi->model('book')); |
|
2055 |
is($dbi->models->{'company'}, $dbi->model('company')); |
|
2056 | ||
2057 |
$dbi = MyDBI4->connect; |
|
2058 |
eval { $dbi->execute('drop table book') }; |
|
2059 |
$dbi->execute("create table book (title, author)"); |
|
2060 |
$model = $dbi->model('book'); |
|
2061 |
$model->insert({title => 'a', author => 'b'}); |
|
2062 |
is_deeply($model->list->all, [{title => 'a', author => 'b'}], 'basic'); |
|
2063 |
$dbi->execute("create table company (name)"); |
|
2064 |
$model = $dbi->model('company'); |
|
2065 |
$model->insert({name => 'a'}); |
|
2066 |
is_deeply($model->list->all, [{name => 'a'}], 'basic'); |
|
test cleanup
|
2067 | |
test cleanup
|
2068 |
$dbi = MyDBI5->connect; |
2069 |
eval { $dbi->execute('drop table company') }; |
|
2070 |
eval { $dbi->execute('drop table table1') }; |
|
2071 |
$dbi->execute("create table company (name)"); |
|
2072 |
$dbi->execute("create table table1 (key1)"); |
|
2073 |
$model = $dbi->model('company'); |
|
2074 |
$model->insert({name => 'a'}); |
|
2075 |
is_deeply($model->list->all, [{name => 'a'}], 'include all model'); |
|
2076 |
$dbi->insert(table => 'table1', param => {key1 => 1}); |
|
2077 |
$model = $dbi->model('book'); |
|
2078 |
is_deeply($model->list->all, [{key1 => 1}], 'include all model'); |
|
test cleanup
|
2079 | |
test cleanup
|
2080 |
test 'primary_key'; |
2081 |
use MyDBI1; |
|
2082 |
$dbi = MyDBI1->connect; |
|
2083 |
$model = $dbi->model('book'); |
|
2084 |
$model->primary_key(['id', 'number']); |
|
2085 |
is_deeply($model->primary_key, ['id', 'number']); |
|
test cleanup
|
2086 | |
test cleanup
|
2087 |
test 'columns'; |
2088 |
use MyDBI1; |
|
2089 |
$dbi = MyDBI1->connect; |
|
2090 |
$model = $dbi->model('book'); |
|
2091 |
$model->columns(['id', 'number']); |
|
2092 |
is_deeply($model->columns, ['id', 'number']); |
|
test cleanup
|
2093 | |
test cleanup
|
2094 |
test 'setup_model'; |
2095 |
use MyDBI1; |
|
2096 |
$dbi = MyDBI1->connect; |
|
2097 |
eval { $dbi->execute('drop table book') }; |
|
2098 |
eval { $dbi->execute('drop table company') }; |
|
2099 |
eval { $dbi->execute('drop table test') }; |
|
test cleanup
|
2100 | |
test cleanup
|
2101 |
$dbi->execute('create table book (id)'); |
2102 |
$dbi->execute('create table company (id, name);'); |
|
2103 |
$dbi->execute('create table test (id, name, primary key (id, name));'); |
|
2104 |
$dbi->setup_model; |
|
2105 |
is_deeply($dbi->model('book')->columns, ['id']); |
|
2106 |
is_deeply($dbi->model('company')->columns, ['id', 'name']); |
|
test cleanup
|
2107 | |
2108 | ||
2109 | ||
2110 | ||
2111 | ||
2112 | ||
2113 | ||
2114 | ||
2115 | ||
2116 | ||
2117 | ||
2118 | ||
2119 | ||
2120 | ||
test cleanup
|
2121 |
### SQLite only test |
2122 |
test 'quote'; |
|
2123 |
$dbi = DBIx::Custom->connect; |
|
2124 |
$dbi->quote('"'); |
|
2125 |
eval { $dbi->execute("drop table ${q}table$p") }; |
|
2126 |
$dbi->execute($create_table_reserved); |
|
2127 |
$dbi->apply_filter('table', select => {out => sub { $_[0] * 2}}); |
|
2128 |
$dbi->insert(table => 'table', param => {select => 1}); |
|
2129 |
$dbi->delete(table => 'table', where => {select => 1}); |
|
2130 |
$result = $dbi->execute("select * from ${q}table$p"); |
|
2131 |
$rows = $result->all; |
|
2132 |
is_deeply($rows, [], "reserved word"); |
|
2133 | ||
2134 | ||
2135 | ||
2136 | ||
2137 | ||
2138 | ||
2139 | ||
test cleanup
|
2140 | |
2141 | ||
2142 | ||
2143 | ||
2144 | ||
2145 | ||
2146 | ||
2147 | ||
2148 | ||
2149 | ||
2150 |
# DEPRECATED! test |
|
2151 |
test 'filter __ expression'; |
|
2152 |
$dbi = DBIx::Custom->connect; |
|
2153 |
eval { $dbi->execute('drop table company') }; |
|
2154 |
eval { $dbi->execute('drop table location') }; |
|
2155 |
$dbi->execute('create table company (id, name, location_id)'); |
|
2156 |
$dbi->execute('create table location (id, name)'); |
|
2157 |
$dbi->apply_filter('location', |
|
2158 |
name => {in => sub { uc $_[0] } } |
|
2159 |
); |
|
2160 | ||
2161 |
$dbi->insert(table => 'company', param => {id => 1, name => 'a', location_id => 2}); |
|
2162 |
$dbi->insert(table => 'location', param => {id => 2, name => 'b'}); |
|
2163 | ||
2164 |
$result = $dbi->select( |
|
2165 |
table => ['company', 'location'], relation => {'company.location_id' => 'location.id'}, |
|
2166 |
column => ['location.name as location__name'] |
|
2167 |
); |
|
2168 |
is($result->fetch_first->[0], 'B'); |
|
2169 | ||
2170 |
$result = $dbi->select( |
|
2171 |
table => 'company', relation => {'company.location_id' => 'location.id'}, |
|
2172 |
column => ['location.name as location__name'] |
|
2173 |
); |
|
2174 |
is($result->fetch_first->[0], 'B'); |
|
2175 | ||
2176 |
$result = $dbi->select( |
|
2177 |
table => 'company', relation => {'company.location_id' => 'location.id'}, |
|
2178 |
column => ['location.name as "location.name"'] |
|
2179 |
); |
|
2180 |
is($result->fetch_first->[0], 'B'); |
|
test cleanup
|
2181 | |
2182 |
test 'reserved_word_quote'; |
|
2183 |
$dbi = DBIx::Custom->connect; |
|
2184 |
eval { $dbi->execute("drop table ${q}table$p") }; |
|
2185 |
$dbi->reserved_word_quote('"'); |
|
2186 |
$dbi->execute($create_table_reserved); |
|
2187 |
$dbi->apply_filter('table', select => {out => sub { $_[0] * 2}}); |
|
2188 |
$dbi->apply_filter('table', update => {out => sub { $_[0] * 3}}); |
|
2189 |
$dbi->insert(table => 'table', param => {select => 1}); |
|
2190 |
$dbi->update(table => 'table', where => {'table.select' => 1}, param => {update => 2}); |
|
2191 |
$result = $dbi->execute("select * from ${q}table$p"); |
|
2192 |
$rows = $result->all; |
|
2193 |
is_deeply($rows, [{select => 2, update => 6}], "reserved word"); |