| ... | ... |
@@ -1,5 +1,6 @@ |
| 1 | 1 |
0.1638 |
| 2 |
- added experimental base_table attribute and removed experimental table_class attribute |
|
| 2 |
+ table object call dbi object method if not found method. |
|
| 3 |
+ added experimental base_table attribute and removed experimental table_class attribute |
|
| 3 | 4 |
renamed helper to method. |
| 4 | 5 |
added experimental DBIx::Custom::Result::stash() |
| 5 | 6 |
renamed experimental DBIx::Custom::Table helper to method |
| ... | ... |
@@ -14,15 +14,16 @@ use DBIx::Custom::Result; |
| 14 | 14 |
use DBIx::Custom::Query; |
| 15 | 15 |
use DBIx::Custom::QueryBuilder; |
| 16 | 16 |
use DBIx::Custom::Where; |
| 17 |
+use DBIx::Custom::Table; |
|
| 17 | 18 |
use Encode qw/encode_utf8 decode_utf8/; |
| 18 | 19 |
|
| 19 | 20 |
__PACKAGE__->attr( |
| 20 | 21 |
[qw/data_source dbh password user/], |
| 21 | 22 |
cache => 1, |
| 22 |
- dbi_option => sub { {} },
|
|
| 23 |
+ dbi_option => sub { {} },
|
|
| 23 | 24 |
query_builder => sub { DBIx::Custom::QueryBuilder->new },
|
| 24 | 25 |
result_class => 'DBIx::Custom::Result', |
| 25 |
- table_class => 'DBIx::Custom::Table' |
|
| 26 |
+ base_table => sub { DBIx::Custom::Table->new(dbi => shift) }
|
|
| 26 | 27 |
); |
| 27 | 28 |
|
| 28 | 29 |
__PACKAGE__->attr( |
| ... | ... |
@@ -569,19 +570,30 @@ sub table {
|
| 569 | 570 |
my $self = shift; |
| 570 | 571 |
my $name = shift; |
| 571 | 572 |
|
| 572 |
- # Table class |
|
| 573 |
- my $table_class = $self->table_class; |
|
| 574 |
- croak qq{Invalid table class name "$table_class"}
|
|
| 575 |
- unless $table_class =~ /^[\w:]+$/; |
|
| 576 |
- unless ($table_class->can('isa')) {
|
|
| 577 |
- eval "require $table_class"; |
|
| 578 |
- croak $@ if $@; |
|
| 579 |
- } |
|
| 580 | 573 |
# Create table |
| 581 | 574 |
$self->{_tables} ||= {};
|
| 582 |
- $self->{_tables}->{$name}
|
|
| 583 |
- = $table_class->new(name => $name, dbi => $self) |
|
| 584 |
- unless defined $self->{_tables}->{$name};
|
|
| 575 |
+ unless (defined $self->{_tables}->{$name}) {
|
|
| 576 |
+ # Base table |
|
| 577 |
+ my $base_table = $self->base_table; |
|
| 578 |
+ |
|
| 579 |
+ # Base methods |
|
| 580 |
+ my $bmethods = ref $base_table->{_methods} eq 'HASH'
|
|
| 581 |
+ ? $base_table->{_methods}
|
|
| 582 |
+ : {};
|
|
| 583 |
+ |
|
| 584 |
+ # Copy Methods |
|
| 585 |
+ my $methods = {};
|
|
| 586 |
+ $methods->{$_} = $bmethods->{$_} for keys %$bmethods;
|
|
| 587 |
+ |
|
| 588 |
+ # Create table |
|
| 589 |
+ my $table = $base_table->new( |
|
| 590 |
+ dbi => $self, |
|
| 591 |
+ name => $name, |
|
| 592 |
+ base => $base_table, |
|
| 593 |
+ _methods => $methods |
|
| 594 |
+ ); |
|
| 595 |
+ $self->{_tables}->{$name} = $table;
|
|
| 596 |
+ } |
|
| 585 | 597 |
|
| 586 | 598 |
return $self->{_tables}{$name};
|
| 587 | 599 |
} |
| ... | ... |
@@ -10,7 +10,7 @@ use Carp 'croak'; |
| 10 | 10 |
# Carp trust relationship |
| 11 | 11 |
push @DBIx::Custom::CARP_NOT, __PACKAGE__; |
| 12 | 12 |
|
| 13 |
-__PACKAGE__->attr(['dbi', 'name']); |
|
| 13 |
+__PACKAGE__->attr(['dbi', 'name', 'base']); |
|
| 14 | 14 |
|
| 15 | 15 |
our $AUTOLOAD; |
| 16 | 16 |
|
| ... | ... |
@@ -22,11 +22,14 @@ sub AUTOLOAD {
|
| 22 | 22 |
|
| 23 | 23 |
# Method |
| 24 | 24 |
$self->{_methods} ||= {};
|
| 25 |
- croak qq/Can't locate object method "$mname" via "$package"/ |
|
| 26 |
- unless my $method = $self->{_methods}->{$mname};
|
|
| 27 |
- |
|
| 28 |
- # Execute |
|
| 29 |
- return $self->$method(@_); |
|
| 25 |
+ |
|
| 26 |
+ # Method |
|
| 27 |
+ if (my $method = $self->{_methods}->{$mname}) {
|
|
| 28 |
+ return $self->$method(@_) |
|
| 29 |
+ } |
|
| 30 |
+ |
|
| 31 |
+ # DBI method |
|
| 32 |
+ return $self->dbi->$mname(@_); |
|
| 30 | 33 |
} |
| 31 | 34 |
|
| 32 | 35 |
sub method {
|
| ... | ... |
@@ -16,7 +16,7 @@ BEGIN {
|
| 16 | 16 |
} |
| 17 | 17 |
|
| 18 | 18 |
# Function for test name |
| 19 |
-sub test { "# $_[0]\n" }
|
|
| 19 |
+sub test { print "# $_[0]\n" }
|
|
| 20 | 20 |
|
| 21 | 21 |
# Constant varialbes for test |
| 22 | 22 |
my $CREATE_TABLE = {
|
| ... | ... |
@@ -1019,13 +1019,7 @@ is_deeply($row, {key1 => 2, key2 => 2});
|
| 1019 | 1019 |
eval {$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2},
|
| 1020 | 1020 |
filter => {key1 => 'no'}) };
|
| 1021 | 1021 |
like($@, qr//); |
| 1022 |
-$dbi->table_class('!!!!');
|
|
| 1023 |
-eval {$dbi->table};
|
|
| 1024 |
-like($@, qr/Invalid table/); |
|
| 1025 | 1022 |
|
| 1026 |
-$dbi->table_class('NOTEXIST');
|
|
| 1027 |
-eval {$dbi->table};
|
|
| 1028 |
-ok($@); |
|
| 1029 | 1023 |
$dbi->register_filter(one => sub { });
|
| 1030 | 1024 |
$dbi->default_fetch_filter('one');
|
| 1031 | 1025 |
ok($dbi->default_fetch_filter); |
| ... | ... |
@@ -1072,3 +1066,26 @@ $result = DBIx::Custom::Result->new; |
| 1072 | 1066 |
is_deeply($result->stash, {}, 'default');
|
| 1073 | 1067 |
$result->stash->{foo} = 1;
|
| 1074 | 1068 |
is($result->stash->{foo}, 1, 'get and set');
|
| 1069 |
+ |
|
| 1070 |
+test 'base_table'; |
|
| 1071 |
+$dbi = DBIx::Custom->new; |
|
| 1072 |
+$dbi->base_table->method( |
|
| 1073 |
+ one => sub { 1 }
|
|
| 1074 |
+); |
|
| 1075 |
+$table = $dbi->table('book');
|
|
| 1076 |
+$table->method( |
|
| 1077 |
+ two => sub { 2 }
|
|
| 1078 |
+); |
|
| 1079 |
+is($dbi->base_table->one, 1, 'method'); |
|
| 1080 |
+is($table->one, 1, 'inherit method'); |
|
| 1081 |
+is($table->two, 2, 'child table method'); |
|
| 1082 |
+eval {$dbi->base_table->two};
|
|
| 1083 |
+ok($@); |
|
| 1084 |
+ |
|
| 1085 |
+$dbi = DBIx::Custom->connect($NEW_ARGS->{0});
|
|
| 1086 |
+$dbi->execute($CREATE_TABLE->{0});
|
|
| 1087 |
+$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
|
|
| 1088 |
+$result = $dbi->base_table->execute("select * from table1");
|
|
| 1089 |
+is_deeply($result->fetch_hash_all, [{key1 => 1, key2 => 2}], 'dbi method from base_table');
|
|
| 1090 |
+$result = $dbi->table('table1')->execute("select * from table1");
|
|
| 1091 |
+is_deeply($result->fetch_hash_all, [{key1 => 1, key2 => 2}], 'dbi method from table');
|
| ... | ... |
@@ -7,7 +7,7 @@ use DBIx::Custom::QueryBuilder; |
| 7 | 7 |
|
| 8 | 8 |
# Function for test name |
| 9 | 9 |
my $test; |
| 10 |
-sub test { "# $_[0]\n" }
|
|
| 10 |
+sub test { print "# $_[0]\n" }
|
|
| 11 | 11 |
|
| 12 | 12 |
# Variables for test |
| 13 | 13 |
my $dbi; |
| ... | ... |
@@ -5,7 +5,7 @@ use warnings; |
| 5 | 5 |
use DBIx::Custom::Query; |
| 6 | 6 |
|
| 7 | 7 |
# Function for test name |
| 8 |
-sub test{ "# $_[0]\n" }
|
|
| 8 |
+sub test{ print "# $_[0]\n" }
|
|
| 9 | 9 |
|
| 10 | 10 |
# Variables for test |
| 11 | 11 |
my $query; |
| ... | ... |
@@ -6,7 +6,7 @@ use Test::More 'no_plan'; |
| 6 | 6 |
use DBIx::Custom::QueryBuilder; |
| 7 | 7 |
|
| 8 | 8 |
# Function for test name |
| 9 |
-sub test{ "# $_[0]\n" }
|
|
| 9 |
+sub test{ print "# $_[0]\n" }
|
|
| 10 | 10 |
|
| 11 | 11 |
# Variable for test |
| 12 | 12 |
my $datas; |
| ... | ... |
@@ -14,7 +14,7 @@ BEGIN {
|
| 14 | 14 |
} |
| 15 | 15 |
|
| 16 | 16 |
# Function for test name |
| 17 |
-sub test { "# $_[0]\n" }
|
|
| 17 |
+sub test { print "# $_[0]\n" }
|
|
| 18 | 18 |
|
| 19 | 19 |
# Constant varialbes for test |
| 20 | 20 |
my $CREATE_TABLE = {
|