| ... | ... |
@@ -486,9 +486,28 @@ sub execute {
|
| 486 | 486 |
} |
| 487 | 487 |
} |
| 488 | 488 |
} |
| 489 |
+ |
|
| 490 |
+ # Type rule |
|
| 491 |
+ my $applied_filter = {};
|
|
| 492 |
+ foreach my $name (keys %$param) {
|
|
| 493 |
+ my $table; |
|
| 494 |
+ my $column; |
|
| 495 |
+ if ($name =~ /(?:(.+)\.)?(.+)/) {
|
|
| 496 |
+ $table = $1; |
|
| 497 |
+ $column = $2; |
|
| 498 |
+ } |
|
| 499 |
+ $table ||= $main_table; |
|
| 500 |
+ |
|
| 501 |
+ my $into = $self->{_into} || {};
|
|
| 502 |
+ if (defined $table && $into->{$table} &&
|
|
| 503 |
+ (my $rule = $into->{$table}->{$column}))
|
|
| 504 |
+ {
|
|
| 505 |
+ $applied_filter->{$column} = $rule;
|
|
| 506 |
+ $applied_filter->{"$table.$column"} = $rule;
|
|
| 507 |
+ } |
|
| 508 |
+ } |
|
| 489 | 509 |
|
| 490 | 510 |
# Applied filter |
| 491 |
- my $applied_filter = {};
|
|
| 492 | 511 |
foreach my $table (@$tables) {
|
| 493 | 512 |
$applied_filter = {
|
| 494 | 513 |
%$applied_filter, |
| ... | ... |
@@ -993,6 +1012,17 @@ sub type_rule {
|
| 993 | 1012 |
if (@_) {
|
| 994 | 1013 |
my $type_rule = _array_to_hash([@_]); |
| 995 | 1014 |
$self->{type_rule} = $type_rule;
|
| 1015 |
+ $self->{_into} ||= {};
|
|
| 1016 |
+ $self->each_column(sub {
|
|
| 1017 |
+ my ($dbi, $table, $column, $column_info) = @_; |
|
| 1018 |
+ |
|
| 1019 |
+ my $type = $column_info->{TYPE_NAME};
|
|
| 1020 |
+ if ($type_rule->{$type} &&
|
|
| 1021 |
+ (my $rule = $type_rule->{$type}->{into}))
|
|
| 1022 |
+ {
|
|
| 1023 |
+ $self->{_into}{$table}{$column} = $rule;
|
|
| 1024 |
+ } |
|
| 1025 |
+ }); |
|
| 996 | 1026 |
|
| 997 | 1027 |
return $self; |
| 998 | 1028 |
} |
| ... | ... |
@@ -1984,14 +2014,9 @@ Table names for filtering. |
| 1984 | 2014 |
|
| 1985 | 2015 |
$dbi->execute(table => ['author', 'book']); |
| 1986 | 2016 |
|
| 1987 |
-C<execute()> is unlike C<insert()>, C<update()>, C<delete()>, C<select(), |
|
| 2017 |
+C<execute()> is unlike C<insert()>, C<update()>, C<delete()>, C<select()>, |
|
| 1988 | 2018 |
Filtering is off because we don't know what filter is applied. |
| 1989 | 2019 |
|
| 1990 |
- |
|
| 1991 |
- |
|
| 1992 |
- |
|
| 1993 |
- |
|
| 1994 |
- |
|
| 1995 | 2020 |
=item C<filter> |
| 1996 | 2021 |
|
| 1997 | 2022 |
Filter, executed before data is send to database. This is array reference. |
| ... | ... |
@@ -2368,6 +2393,21 @@ This is used by C<clause> of L<DBIx::Custom::Where> . |
| 2368 | 2393 |
|
| 2369 | 2394 |
Register filters, used by C<filter> option of many methods. |
| 2370 | 2395 |
|
| 2396 |
+=head2 C<type_rule> EXPERIMENTAL |
|
| 2397 |
+ |
|
| 2398 |
+ $dbi->type_rule( |
|
| 2399 |
+ DATE => {
|
|
| 2400 |
+ from => sub { ... },
|
|
| 2401 |
+ into => sub { ... }
|
|
| 2402 |
+ }, |
|
| 2403 |
+ DATETIME => {
|
|
| 2404 |
+ from => sub { ... }
|
|
| 2405 |
+ into => sub { ... }
|
|
| 2406 |
+ } |
|
| 2407 |
+ ); |
|
| 2408 |
+ |
|
| 2409 |
+Filter based on type. |
|
| 2410 |
+ |
|
| 2371 | 2411 |
=head2 C<select> |
| 2372 | 2412 |
|
| 2373 | 2413 |
my $result = $dbi->select( |
| ... | ... |
@@ -2577,4 +2577,63 @@ is($result->fetch_first->[0], 'A'); |
| 2577 | 2577 |
$result = $dbi->select(table => 'table1'); |
| 2578 | 2578 |
is($result->one->{key1}, 'A');
|
| 2579 | 2579 |
|
| 2580 |
+ |
|
| 2581 |
+test 'type_rule into'; |
|
| 2582 |
+$dbi = DBIx::Custom->connect(dsn => 'dbi:SQLite:dbname=:memory:'); |
|
| 2583 |
+$dbi->execute("create table table1 (key1 Date, key2 datetime)");
|
|
| 2584 |
+$dbi->type_rule( |
|
| 2585 |
+ Date => {
|
|
| 2586 |
+ into => sub { uc $_[0] }
|
|
| 2587 |
+ } |
|
| 2588 |
+); |
|
| 2589 |
+$dbi->insert({key1 => 'a'}, table => 'table1');
|
|
| 2590 |
+$result = $dbi->select(table => 'table1'); |
|
| 2591 |
+is($result->one->{key1}, 'A');
|
|
| 2592 |
+ |
|
| 2593 |
+$dbi = DBIx::Custom->connect(dsn => 'dbi:SQLite:dbname=:memory:'); |
|
| 2594 |
+$dbi->execute("create table table1 (key1 Date, key2 datetime)");
|
|
| 2595 |
+$dbi->type_rule( |
|
| 2596 |
+ [qw/Date datetime/] => {
|
|
| 2597 |
+ into => sub { uc $_[0] }
|
|
| 2598 |
+ } |
|
| 2599 |
+); |
|
| 2600 |
+$dbi->insert({key1 => 'a', key2 => 'b'}, table => 'table1');
|
|
| 2601 |
+$result = $dbi->select(table => 'table1'); |
|
| 2602 |
+$row = $result->one; |
|
| 2603 |
+is($row->{key1}, 'A');
|
|
| 2604 |
+is($row->{key2}, 'B');
|
|
| 2605 |
+ |
|
| 2606 |
+$dbi = DBIx::Custom->connect(dsn => 'dbi:SQLite:dbname=:memory:'); |
|
| 2607 |
+$dbi->execute("create table table1 (key1 Date, key2 datetime)");
|
|
| 2608 |
+$dbi->insert({key1 => 'a', key2 => 'B'}, table => 'table1');
|
|
| 2609 |
+$dbi->type_rule( |
|
| 2610 |
+ [qw/Date datetime/] => {
|
|
| 2611 |
+ into => sub { uc $_[0] }
|
|
| 2612 |
+ } |
|
| 2613 |
+); |
|
| 2614 |
+$result = $dbi->execute( |
|
| 2615 |
+ "select * from table1 where key1 = :key1 and key2 = :table1.key2;", |
|
| 2616 |
+ param => {key1 => 'a', 'table1.key2' => 'b'}
|
|
| 2617 |
+); |
|
| 2618 |
+$row = $result->one; |
|
| 2619 |
+is($row->{key1}, 'a');
|
|
| 2620 |
+is($row->{key2}, 'B');
|
|
| 2621 |
+ |
|
| 2622 |
+$dbi = DBIx::Custom->connect(dsn => 'dbi:SQLite:dbname=:memory:'); |
|
| 2623 |
+$dbi->execute("create table table1 (key1 Date, key2 datetime)");
|
|
| 2624 |
+$dbi->insert({key1 => 'A', key2 => 'B'}, table => 'table1');
|
|
| 2625 |
+$dbi->type_rule( |
|
| 2626 |
+ [qw/Date datetime/] => {
|
|
| 2627 |
+ into => sub { uc $_[0] }
|
|
| 2628 |
+ } |
|
| 2629 |
+); |
|
| 2630 |
+$result = $dbi->execute( |
|
| 2631 |
+ "select * from table1 where key1 = :key1 and key2 = :table1.key2;", |
|
| 2632 |
+ param => {key1 => 'a', 'table1.key2' => 'b'},
|
|
| 2633 |
+ table => 'table1' |
|
| 2634 |
+); |
|
| 2635 |
+$row = $result->one; |
|
| 2636 |
+is($row->{key1}, 'A');
|
|
| 2637 |
+is($row->{key2}, 'B');
|
|
| 2638 |
+ |
|
| 2580 | 2639 |
=cut |