add feture. all model class in namespace is included...
...by include_model
| ... | ... |
@@ -1,3 +1,9 @@ |
| 1 |
+0.1646 |
|
| 2 |
+ add feture. all model class in namespace is included by include_model |
|
| 3 |
+ rename experimental include_table to include_model |
|
| 4 |
+ rename experimental table to model |
|
| 5 |
+ rename experimental DBIx::Custom::Table to DBIx::Custom::Model |
|
| 6 |
+ remame experimental DBIx::Custom::Table::name() to DBIx::Custom::Model::table(); |
|
| 1 | 7 |
0.1645 |
| 2 | 8 |
removed experimental base_table() for class expandability. |
| 3 | 9 |
experimental table() can't set table object any more. |
| ... | ... |
@@ -1,6 +1,6 @@ |
| 1 | 1 |
package DBIx::Custom; |
| 2 | 2 |
|
| 3 |
-our $VERSION = '0.1645'; |
|
| 3 |
+our $VERSION = '0.1646'; |
|
| 4 | 4 |
|
| 5 | 5 |
use 5.008001; |
| 6 | 6 |
use strict; |
| ... | ... |
@@ -14,7 +14,7 @@ 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 |
+use DBIx::Custom::Model; |
|
| 18 | 18 |
use DBIx::Custom::Tag; |
| 19 | 19 |
use Encode qw/encode_utf8 decode_utf8/; |
| 20 | 20 |
|
| ... | ... |
@@ -63,11 +63,15 @@ sub AUTOLOAD {
|
| 63 | 63 |
|
| 64 | 64 |
# Method |
| 65 | 65 |
$self->{_methods} ||= {};
|
| 66 |
- my $method = $self->{_methods}->{$mname};
|
|
| 67 |
- return $self->$method(@_) if $method; |
|
| 68 |
- |
|
| 69 |
- # DBI method |
|
| 70 |
- return $self->dbh->$mname(@_); |
|
| 66 |
+ if (my $method = $self->{_methods}->{$mname}) {
|
|
| 67 |
+ return $self->$method(@_) |
|
| 68 |
+ } |
|
| 69 |
+ elsif ($self->dbh->can($mname)) {
|
|
| 70 |
+ $self->dbh->$mname(@_); |
|
| 71 |
+ } |
|
| 72 |
+ else {
|
|
| 73 |
+ croak qq/Can't locate object method "$mname" via "$package"/ |
|
| 74 |
+ } |
|
| 71 | 75 |
} |
| 72 | 76 |
|
| 73 | 77 |
sub apply_filter {
|
| ... | ... |
@@ -586,52 +590,75 @@ sub select {
|
| 586 | 590 |
return $result; |
| 587 | 591 |
} |
| 588 | 592 |
|
| 589 |
-sub table {
|
|
| 590 |
- my ($self, $name, $table) = @_; |
|
| 593 |
+sub model {
|
|
| 594 |
+ my ($self, $name, $model) = @_; |
|
| 591 | 595 |
|
| 592 | 596 |
# Set |
| 593 |
- $self->{table} ||= {};
|
|
| 594 |
- if ($table) {
|
|
| 595 |
- $self->{table}{$name} = $table;
|
|
| 597 |
+ $self->{model} ||= {};
|
|
| 598 |
+ if ($model) {
|
|
| 599 |
+ $self->{model}{$name} = $model;
|
|
| 596 | 600 |
return $self; |
| 597 | 601 |
} |
| 598 | 602 |
|
| 599 |
- # Check table existance |
|
| 600 |
- croak qq{Table "$name" is not included}
|
|
| 601 |
- unless $self->{table}{$name};
|
|
| 603 |
+ # Check model existance |
|
| 604 |
+ croak qq{Model "$name" is not included}
|
|
| 605 |
+ unless $self->{model}{$name};
|
|
| 602 | 606 |
|
| 603 | 607 |
# Get |
| 604 |
- return $self->{table}{$name};
|
|
| 608 |
+ return $self->{model}{$name};
|
|
| 605 | 609 |
} |
| 606 | 610 |
|
| 607 |
-sub include_table {
|
|
| 608 |
- my ($self, $name_space, $table_infos) = @_; |
|
| 611 |
+sub include_model {
|
|
| 612 |
+ my ($self, $name_space, $model_infos) = @_; |
|
| 613 |
+ |
|
| 614 |
+ $name_space ||= ''; |
|
| 615 |
+ unless ($model_infos) {
|
|
| 616 |
+ # Load name space module |
|
| 617 |
+ croak qq{"$name_space" is invalid class name}
|
|
| 618 |
+ if $name_space =~ /[^\w:]/; |
|
| 619 |
+ eval "use $name_space"; |
|
| 620 |
+ croak qq{Name space module "$name_space.pm" is needed. $@} if $@;
|
|
| 621 |
+ |
|
| 622 |
+ # Search model modules |
|
| 623 |
+ my $path = $INC{"$name_space.pm"};
|
|
| 624 |
+ $path =~ s/\.pm$//; |
|
| 625 |
+ opendir my $dh, $path |
|
| 626 |
+ or croak qq{Can't open directory "$path": $!};
|
|
| 627 |
+ $model_infos = []; |
|
| 628 |
+ while (my $module = readdir $dh) {
|
|
| 629 |
+ push @$model_infos, $module |
|
| 630 |
+ if $module =~ s/\.pm$//; |
|
| 631 |
+ } |
|
| 632 |
+ |
|
| 633 |
+ close $dh; |
|
| 634 |
+ } |
|
| 609 | 635 |
|
| 610 |
- foreach my $table_info (@$table_infos) {
|
|
| 636 |
+ foreach my $model_info (@$model_infos) {
|
|
| 611 | 637 |
|
| 612 |
- # Table name and class |
|
| 613 |
- my $table_name; |
|
| 614 |
- my $table_class; |
|
| 615 |
- if (ref $table_info eq 'HASH') {
|
|
| 616 |
- $table_name = (keys %$table_info)[0]; |
|
| 617 |
- $table_class = $table_info->{$table_name};
|
|
| 638 |
+ # Model name and class |
|
| 639 |
+ my $model_name; |
|
| 640 |
+ my $model_class; |
|
| 641 |
+ if (ref $model_info eq 'HASH') {
|
|
| 642 |
+ $model_name = (keys %$model_info)[0]; |
|
| 643 |
+ $model_class = $model_info->{$model_name};
|
|
| 618 | 644 |
} |
| 619 |
- else { $table_name = $table_class = $table_info }
|
|
| 620 |
- my $tclass = "${name_space}::$table_class";
|
|
| 645 |
+ else { $model_name = $model_class = $model_info }
|
|
| 646 |
+ my $mclass = "${name_space}::$model_class";
|
|
| 621 | 647 |
|
| 622 | 648 |
# Load |
| 623 |
- croak qq{"$tclass" is invalid class name}
|
|
| 624 |
- if $tclass =~ /[^\w:]/; |
|
| 625 |
- unless ($tclass->can('isa')) {
|
|
| 626 |
- eval "use $tclass"; |
|
| 649 |
+ croak qq{"$mclass" is invalid class name}
|
|
| 650 |
+ if $mclass =~ /[^\w:]/; |
|
| 651 |
+ unless ($mclass->can('isa')) {
|
|
| 652 |
+ eval "use $mclass"; |
|
| 627 | 653 |
croak $@ if $@; |
| 628 | 654 |
} |
| 629 | 655 |
|
| 630 | 656 |
# Instantiate |
| 631 |
- my $table = $tclass->new(dbi => $self, name => $table_name); |
|
| 657 |
+ my $model = $mclass->new(dbi => $self); |
|
| 658 |
+ $model->table($model_name) unless $model->table; |
|
| 632 | 659 |
|
| 633 | 660 |
# Set |
| 634 |
- $self->table($table_name, $table); |
|
| 661 |
+ $self->model($model_name, $model); |
|
| 635 | 662 |
} |
| 636 | 663 |
return $self; |
| 637 | 664 |
} |
| ... | ... |
@@ -1181,32 +1208,44 @@ You can do anything in callback. |
| 1181 | 1208 |
Callback receive four arguments, dbi object, table name, |
| 1182 | 1209 |
column name and columninformation. |
| 1183 | 1210 |
|
| 1184 |
-=head2 C<(experimental) include_table> |
|
| 1211 |
+=head2 C<(experimental) include_model> |
|
| 1185 | 1212 |
|
| 1186 |
- $dbi->include_table( |
|
| 1187 |
- 'MyTable' => [ |
|
| 1213 |
+ $dbi->include_model( |
|
| 1214 |
+ 'MyModel' => [ |
|
| 1188 | 1215 |
'book', 'person', 'company' |
| 1189 | 1216 |
] |
| 1190 | 1217 |
); |
| 1191 | 1218 |
|
| 1192 |
-Include tables. First argument is name space. |
|
| 1219 |
+Include models. First argument is name space. |
|
| 1193 | 1220 |
Second argument is array reference of class base names. |
| 1194 | 1221 |
|
| 1195 |
-The following table is instantiated and included. |
|
| 1222 |
+If you don't specify second argument, All models under name space is |
|
| 1223 |
+included. |
|
| 1224 |
+ |
|
| 1225 |
+ $dbi->include_model('MyModel');
|
|
| 1226 |
+ |
|
| 1227 |
+Note that in this case name spece module is needed. |
|
| 1196 | 1228 |
|
| 1197 |
- MyTable::book |
|
| 1198 |
- MyTable::person |
|
| 1199 |
- MyTable::company |
|
| 1229 |
+ # MyModel.pm |
|
| 1230 |
+ package MyModel; |
|
| 1231 |
+ |
|
| 1232 |
+ use base 'DBIx::Custom::Model'; |
|
| 1233 |
+ |
|
| 1234 |
+The following model is instantiated and included. |
|
| 1200 | 1235 |
|
| 1201 |
-You can get these instance by C<table()>. |
|
| 1236 |
+ MyModel::book |
|
| 1237 |
+ MyModel::person |
|
| 1238 |
+ MyModel::company |
|
| 1202 | 1239 |
|
| 1203 |
- my $book_table = $dbi->table('book');
|
|
| 1240 |
+You can get these instance by C<model()>. |
|
| 1204 | 1241 |
|
| 1205 |
-If you want to other name as table class, |
|
| 1242 |
+ my $book_model = $dbi->model('book');
|
|
| 1243 |
+ |
|
| 1244 |
+If you want to other name as model class, |
|
| 1206 | 1245 |
you can do like this. |
| 1207 | 1246 |
|
| 1208 |
- $dbi->include_table( |
|
| 1209 |
- 'MyTable' => [ |
|
| 1247 |
+ $dbi->include_model( |
|
| 1248 |
+ 'MyModel' => [ |
|
| 1210 | 1249 |
{'book' => 'Book'},
|
| 1211 | 1250 |
{'person' => 'Person'}
|
| 1212 | 1251 |
] |
| ... | ... |
@@ -1341,17 +1380,16 @@ default to 0. This is experimental. |
| 1341 | 1380 |
This is overwrites C<default_bind_filter>. |
| 1342 | 1381 |
Return value of C<update()> is the count of affected rows. |
| 1343 | 1382 |
|
| 1344 |
-=head2 C<(experimental) table> |
|
| 1383 |
+=head2 C<(experimental) model> |
|
| 1345 | 1384 |
|
| 1346 |
- $dbi->table('book')->method(
|
|
| 1385 |
+ $dbi->model('book')->method(
|
|
| 1347 | 1386 |
insert => sub { ... },
|
| 1348 | 1387 |
update => sub { ... }
|
| 1349 | 1388 |
); |
| 1350 | 1389 |
|
| 1351 |
- my $table = $dbi->table('book');
|
|
| 1390 |
+ my $model = $dbi->model('book');
|
|
| 1352 | 1391 |
|
| 1353 |
-Create a L<DBIx::Custom::Table> object, |
|
| 1354 |
-or get a L<DBIx::Custom::Table> object. |
|
| 1392 |
+Set and get a L<DBIx::Custom::Model> object, |
|
| 1355 | 1393 |
|
| 1356 | 1394 |
=head2 C<update_all> |
| 1357 | 1395 |
|
| ... | ... |
@@ -959,23 +959,23 @@ L<DBIx::Custom::Where> object is embedded into SQL. |
| 959 | 959 |
|
| 960 | 960 |
$dbi->execute($sql, param => $param); |
| 961 | 961 |
|
| 962 |
-=head2 7. Table class |
|
| 962 |
+=head2 7. Model |
|
| 963 | 963 |
|
| 964 |
-=head3 Define table clas |
|
| 964 |
+=head3 Model |
|
| 965 | 965 |
|
| 966 |
-You can define table class extending L<DBIx::Custom::Table> |
|
| 966 |
+you can define model extending L<DBIx::Custom::Model> |
|
| 967 | 967 |
to improve source code view. |
| 968 | 968 |
|
| 969 |
- package MyTable::book; |
|
| 970 |
- use base 'DBIx::Custom::Table'; |
|
| 969 |
+ package MyModel::book; |
|
| 970 |
+ use base 'DBIx::Custom::Model'; |
|
| 971 | 971 |
|
| 972 | 972 |
sub insert { ... }
|
| 973 | 973 |
sub list { ... }
|
| 974 | 974 |
|
| 975 |
-You can include and instantiate this class. |
|
| 975 |
+You can include and instantiate this class |
|
| 976 | 976 |
|
| 977 |
- $dbi->include_table( |
|
| 978 |
- MyTable => [ |
|
| 977 |
+ $dbi->include_model( |
|
| 978 |
+ MyModel => [ |
|
| 979 | 979 |
'book', |
| 980 | 980 |
] |
| 981 | 981 |
); |
| ... | ... |
@@ -983,38 +983,57 @@ You can include and instantiate this class. |
| 983 | 983 |
First argument is name space. |
| 984 | 984 |
Second argument is array reference of class base names. |
| 985 | 985 |
|
| 986 |
-You can use this table like this. |
|
| 986 |
+If you don't specify second argument, All models under name space is |
|
| 987 |
+included. |
|
| 987 | 988 |
|
| 988 |
- my $result = $dbi->table('book')->list;
|
|
| 989 |
+ $dbi->include_model('MyModel');
|
|
| 989 | 990 |
|
| 990 |
-Table class is a little more useful than L<DBIx::Custom>, |
|
| 991 |
-because you can use method C<insert()>, C<update()>, C<update_all()>, |
|
| 992 |
-C<delete()>, C<delete_all()>, C<select()> without table name. |
|
| 991 |
+Note that in this case name spece module is needed. |
|
| 993 | 992 |
|
| 994 |
- $dbi->table('book')->insert(param => $param);
|
|
| 993 |
+ # MyModel.pm |
|
| 994 |
+ package MyModel; |
|
| 995 |
+ |
|
| 996 |
+ use base 'DBIx::Custom::Model'; |
|
| 997 |
+ |
|
| 998 |
+The follwoing modules location is needed. |
|
| 999 |
+ |
|
| 1000 |
+ MyModel.pm |
|
| 1001 |
+ MyModel / book.pm |
|
| 1002 |
+ / company.pm |
|
| 1003 |
+ |
|
| 1004 |
+You can use model like this. |
|
| 1005 |
+ |
|
| 1006 |
+ my $result = $dbi->model('book')->list;
|
|
| 1007 |
+ |
|
| 1008 |
+In mode, You can use such as methods, |
|
| 1009 |
+C<insert()>, C<update()>, C<update_all()>, |
|
| 1010 |
+C<delete()>, C<delete_all()>, C<select()> |
|
| 1011 |
+without C<table> option. |
|
| 1012 |
+ |
|
| 1013 |
+ $dbi->model('book')->insert(param => $param);
|
|
| 995 | 1014 |
|
| 996 |
-This table is L<DBIx::Custom::Table> object. |
|
| 1015 |
+Model is L<DBIx::Custom::Model>. |
|
| 997 | 1016 |
|
| 998 |
-If you need table name, you can get C<name()>. |
|
| 1017 |
+If you need table name�Ayou can get it by C<table()>. |
|
| 999 | 1018 |
|
| 1000 |
- my $name = $table->name; |
|
| 1019 |
+ my $table = $model->table; |
|
| 1001 | 1020 |
|
| 1002 |
-You can get L<DBIx::Custom> object. |
|
| 1021 |
+You can get L<DBIx::Custom>. |
|
| 1003 | 1022 |
|
| 1004 |
- my $dbi = $table->dbi; |
|
| 1023 |
+ my $dbi = $model->dbi; |
|
| 1005 | 1024 |
|
| 1006 |
-You can also call all methods of L<DBIx::Custom> and L<DBI>. |
|
| 1025 |
+You can also call all methods of L<DBIx::Custom> and L<DBI>. |
|
| 1007 | 1026 |
|
| 1008 | 1027 |
# DBIx::Custom method |
| 1009 |
- $table->execute($sql); |
|
| 1028 |
+ $model->execute($sql); |
|
| 1010 | 1029 |
|
| 1011 | 1030 |
# DBI method |
| 1012 |
- $table->begin_work; |
|
| 1013 |
- $table->commit; |
|
| 1031 |
+ $model->begin_work; |
|
| 1032 |
+ $model->commit; |
|
| 1014 | 1033 |
|
| 1015 |
-=head2 Table class example |
|
| 1034 |
+=head2 Model Examples |
|
| 1016 | 1035 |
|
| 1017 |
-Model example. |
|
| 1036 |
+Model examples |
|
| 1018 | 1037 |
|
| 1019 | 1038 |
package MyDBI; |
| 1020 | 1039 |
|
| ... | ... |
@@ -1023,22 +1042,22 @@ Model example. |
| 1023 | 1042 |
sub connect {
|
| 1024 | 1043 |
my $self = shift->SUPER::connect(@_); |
| 1025 | 1044 |
|
| 1026 |
- $self->include_table( |
|
| 1027 |
- MyTable => [ |
|
| 1045 |
+ $self->include_model( |
|
| 1046 |
+ MyModel => [ |
|
| 1028 | 1047 |
'book', |
| 1029 | 1048 |
'company' |
| 1030 | 1049 |
] |
| 1031 | 1050 |
); |
| 1032 | 1051 |
} |
| 1033 | 1052 |
|
| 1034 |
- package MyTable::book; |
|
| 1035 |
- use base 'DBIx::Custom::Table'; |
|
| 1053 |
+ package MyModel::book; |
|
| 1054 |
+ use base 'DBIx::Custom::Model'; |
|
| 1036 | 1055 |
|
| 1037 | 1056 |
sub insert { ... }
|
| 1038 | 1057 |
sub list { ... }
|
| 1039 | 1058 |
|
| 1040 |
- package MyTable::company; |
|
| 1041 |
- use base 'DBIx::Custom::Table'; |
|
| 1059 |
+ package MyModel::company; |
|
| 1060 |
+ use base 'DBIx::Custom::Model'; |
|
| 1042 | 1061 |
|
| 1043 | 1062 |
sub insert { ... }
|
| 1044 | 1063 |
sub list { ... }
|
| ... | ... |
@@ -986,23 +986,23 @@ C<execute()>との連携です。SQLを作成するときに埋め込むこと |
| 986 | 986 |
|
| 987 | 987 |
$dbi->execute($sql, param => $param); |
| 988 | 988 |
|
| 989 |
-=head2 7. テーブルクラス |
|
| 989 |
+=head2 7. モデル |
|
| 990 | 990 |
|
| 991 |
-=head3 テーブルクラス |
|
| 991 |
+=head3 モデル |
|
| 992 | 992 |
|
| 993 | 993 |
ソースコードの見通しをよくするために、 |
| 994 |
-L<DBIx::Custom::Table>を継承してテーブルクラスを作成することができます。 |
|
| 994 |
+L<DBIx::Custom::Model>を継承してモデルを作成することができます。 |
|
| 995 | 995 |
|
| 996 |
- package MyTable::book; |
|
| 997 |
- use base 'DBIx::Custom::Table'; |
|
| 996 |
+ package MyModel::book; |
|
| 997 |
+ use base 'DBIx::Custom::Model'; |
|
| 998 | 998 |
|
| 999 | 999 |
sub insert { ... }
|
| 1000 | 1000 |
sub list { ... }
|
| 1001 | 1001 |
|
| 1002 | 1002 |
このクラスを取り込んで、インスタンス化することができます。 |
| 1003 | 1003 |
|
| 1004 |
- $dbi->include_table( |
|
| 1005 |
- MyTable => [ |
|
| 1004 |
+ $dbi->include_model( |
|
| 1005 |
+ MyModel => [ |
|
| 1006 | 1006 |
'book', |
| 1007 | 1007 |
] |
| 1008 | 1008 |
); |
| ... | ... |
@@ -1010,40 +1010,58 @@ L<DBIx::Custom::Table>を継承してテーブルクラスを作成すること |
| 1010 | 1010 |
最初の引数は名前空間です。第二引数は、クラスの末尾の名前を含む |
| 1011 | 1011 |
配列のリファレンスです。 |
| 1012 | 1012 |
|
| 1013 |
-このテーブルは次のように利用することができます。 |
|
| 1013 |
+第二引数を指定しなかった場合は、名前空間以下にあるすべてのモデル |
|
| 1014 |
+が取り込まれます。 |
|
| 1014 | 1015 |
|
| 1015 |
- my $result = $dbi->table('book')->list;
|
|
| 1016 |
+ $dbi->include_model('MyModel');
|
|
| 1016 | 1017 |
|
| 1017 |
-テーブルクラスでは、テーブル名を指定することなしに |
|
| 1018 |
+ただし、名前空間名そのものを表すモジュールが |
|
| 1019 |
+存在していることが必要なことに注意してください。 |
|
| 1020 |
+ |
|
| 1021 |
+ # MyModel.pm |
|
| 1022 |
+ package MyModel; |
|
| 1023 |
+ |
|
| 1024 |
+ use base 'DBIx::Custom::Model'; |
|
| 1025 |
+ |
|
| 1026 |
+次のようなモジュールの配置が必要です。 |
|
| 1027 |
+ |
|
| 1028 |
+ MyModel.pm |
|
| 1029 |
+ MyModel / book.pm |
|
| 1030 |
+ / company.pm |
|
| 1031 |
+ |
|
| 1032 |
+モデルは次のように利用することができます。 |
|
| 1033 |
+ |
|
| 1034 |
+ my $result = $dbi->model('book')->list;
|
|
| 1035 |
+ |
|
| 1036 |
+モデルではテーブル名を指定することなしに |
|
| 1018 | 1037 |
C<insert()>, C<update()>, C<update_all()>, |
| 1019 | 1038 |
C<delete()>, C<delete_all()>, C<select()>などのメソッドを |
| 1020 |
-利用できるので |
|
| 1021 |
-L<DBIx::Custom>よりも少し便利です。 |
|
| 1039 |
+利用できます。 |
|
| 1022 | 1040 |
|
| 1023 |
- $dbi->table('book')->insert(param => $param);
|
|
| 1041 |
+ $dbi->model('book')->insert(param => $param);
|
|
| 1024 | 1042 |
|
| 1025 |
-テーブルクラスはL<DBIx::Custom::Table>です。 |
|
| 1043 |
+モデルはL<DBIx::Custom::Model>です。 |
|
| 1026 | 1044 |
|
| 1027 |
-必要であれば、C<name()>でテーブル名を取得することができます。 |
|
| 1045 |
+必要であれば、C<table()>でテーブル名を取得することができます。 |
|
| 1028 | 1046 |
|
| 1029 |
- my $name = $table->name; |
|
| 1047 |
+ my $table = $model->table; |
|
| 1030 | 1048 |
|
| 1031 | 1049 |
L<DBIx::Custom>オブジェクトを取得することもできます。 |
| 1032 | 1050 |
|
| 1033 |
- my $dbi = $table->dbi; |
|
| 1051 |
+ my $dbi = $model->dbi; |
|
| 1034 | 1052 |
|
| 1035 | 1053 |
L<DBIx::Custom>とL<DBI>のすべてのメソッドを呼び出すこともできます。 |
| 1036 | 1054 |
|
| 1037 | 1055 |
# DBIx::Custom method |
| 1038 |
- $table->execute($sql); |
|
| 1056 |
+ $model->execute($sql); |
|
| 1039 | 1057 |
|
| 1040 | 1058 |
# DBI method |
| 1041 |
- $table->begin_work; |
|
| 1042 |
- $table->commit; |
|
| 1059 |
+ $model->begin_work; |
|
| 1060 |
+ $model->commit; |
|
| 1043 | 1061 |
|
| 1044 |
-=head2 テーブルクラスのサンプル |
|
| 1062 |
+=head2 モデルのサンプル |
|
| 1045 | 1063 |
|
| 1046 |
-テーブルクラスのサンプルです。 |
|
| 1064 |
+モデルのサンプルです。 |
|
| 1047 | 1065 |
|
| 1048 | 1066 |
package MyDBI; |
| 1049 | 1067 |
|
| ... | ... |
@@ -1052,22 +1070,22 @@ L<DBIx::Custom>とL<DBI>のすべてのメソッドを呼び出すこともで |
| 1052 | 1070 |
sub connect {
|
| 1053 | 1071 |
my $self = shift->SUPER::connect(@_); |
| 1054 | 1072 |
|
| 1055 |
- $self->include_table( |
|
| 1056 |
- MyTable => [ |
|
| 1073 |
+ $self->include_model( |
|
| 1074 |
+ MyModel => [ |
|
| 1057 | 1075 |
'book', |
| 1058 | 1076 |
'company' |
| 1059 | 1077 |
] |
| 1060 | 1078 |
); |
| 1061 | 1079 |
} |
| 1062 | 1080 |
|
| 1063 |
- package MyTable::book; |
|
| 1064 |
- use base 'DBIx::Custom::Table'; |
|
| 1081 |
+ package MyModel::book; |
|
| 1082 |
+ use base 'DBIx::Custom::Model'; |
|
| 1065 | 1083 |
|
| 1066 | 1084 |
sub insert { ... }
|
| 1067 | 1085 |
sub list { ... }
|
| 1068 | 1086 |
|
| 1069 |
- package MyTable::company; |
|
| 1070 |
- use base 'DBIx::Custom::Table'; |
|
| 1087 |
+ package MyModel::company; |
|
| 1088 |
+ use base 'DBIx::Custom::Model'; |
|
| 1071 | 1089 |
|
| 1072 | 1090 |
sub insert { ... }
|
| 1073 | 1091 |
sub list { ... }
|
| ... | ... |
@@ -1,4 +1,4 @@ |
| 1 |
-package DBIx::Custom::Table; |
|
| 1 |
+package DBIx::Custom::Model; |
|
| 2 | 2 |
|
| 3 | 3 |
use strict; |
| 4 | 4 |
use warnings; |
| ... | ... |
@@ -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', 'table']); |
|
| 14 | 14 |
|
| 15 | 15 |
our $AUTOLOAD; |
| 16 | 16 |
|
| ... | ... |
@@ -22,14 +22,18 @@ sub AUTOLOAD {
|
| 22 | 22 |
|
| 23 | 23 |
# Method |
| 24 | 24 |
$self->{_methods} ||= {};
|
| 25 |
- |
|
| 26 |
- # Method |
|
| 27 | 25 |
if (my $method = $self->{_methods}->{$mname}) {
|
| 28 | 26 |
return $self->$method(@_) |
| 29 | 27 |
} |
| 30 |
- |
|
| 31 |
- # DBI method |
|
| 32 |
- return $self->dbi->$mname(@_); |
|
| 28 |
+ elsif ($self->dbi->can($mname)) {
|
|
| 29 |
+ $self->dbi->$mname(@_); |
|
| 30 |
+ } |
|
| 31 |
+ elsif ($self->dbi->dbh->can($mname)) {
|
|
| 32 |
+ $self->dbi->dbh->$mname(@_); |
|
| 33 |
+ } |
|
| 34 |
+ else {
|
|
| 35 |
+ croak qq/Can't locate object method "$mname" via "$package"/ |
|
| 36 |
+ } |
|
| 33 | 37 |
} |
| 34 | 38 |
|
| 35 | 39 |
sub method {
|
| ... | ... |
@@ -51,7 +55,7 @@ sub new {
|
| 51 | 55 |
$self->method( |
| 52 | 56 |
$method => sub {
|
| 53 | 57 |
my $self = shift; |
| 54 |
- return $self->dbi->$method(table => $self->name, @_); |
|
| 58 |
+ return $self->dbi->$method(table => $self->table, @_); |
|
| 55 | 59 |
} |
| 56 | 60 |
); |
| 57 | 61 |
} |
| ... | ... |
@@ -71,7 +75,7 @@ DBIx::Custom::Table - Table base class(experimental) |
| 71 | 75 |
|
| 72 | 76 |
use DBIx::Custom::Table; |
| 73 | 77 |
|
| 74 |
-my $table = DBIx::Custom::Table->new(name => 'books'); |
|
| 78 |
+my $table = DBIx::Custom::Model->new(table => 'books'); |
|
| 75 | 79 |
|
| 76 | 80 |
=head1 METHODS |
| 77 | 81 |
|
| ... | ... |
@@ -62,7 +62,7 @@ my $insert_query; |
| 62 | 62 |
my $update_query; |
| 63 | 63 |
my $ret_val; |
| 64 | 64 |
my $infos; |
| 65 |
-my $table; |
|
| 65 |
+my $model; |
|
| 66 | 66 |
my $where; |
| 67 | 67 |
|
| 68 | 68 |
# Prepare table |
| ... | ... |
@@ -1265,20 +1265,20 @@ $dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
|
| 1265 | 1265 |
$result = $dbi->select(selection => '* from table1', where => {key1 => 1});
|
| 1266 | 1266 |
is_deeply($result->fetch_hash_all, [{key1 => 1, key2 => 2}]);
|
| 1267 | 1267 |
|
| 1268 |
-test 'Table class'; |
|
| 1268 |
+test 'Model class'; |
|
| 1269 | 1269 |
use MyDBI1; |
| 1270 | 1270 |
$dbi = MyDBI1->connect($NEW_ARGS->{0});
|
| 1271 | 1271 |
$dbi->execute("create table book (title, author)");
|
| 1272 |
-$table = $dbi->table('book');
|
|
| 1273 |
-$table->insert({title => 'a', author => 'b'});
|
|
| 1274 |
-is_deeply($table->list->fetch_hash_all, [{title => 'a', author => 'b'}], 'basic');
|
|
| 1272 |
+$model = $dbi->model('book');
|
|
| 1273 |
+$model->insert({title => 'a', author => 'b'});
|
|
| 1274 |
+is_deeply($model->list->fetch_hash_all, [{title => 'a', author => 'b'}], 'basic');
|
|
| 1275 | 1275 |
$dbi->execute("create table company (name)");
|
| 1276 |
-$table = $dbi->table('company');
|
|
| 1277 |
-$table->insert({name => 'a'});
|
|
| 1278 |
-is_deeply($table->list->fetch_hash_all, [{name => 'a'}], 'basic');
|
|
| 1276 |
+$model = $dbi->model('company');
|
|
| 1277 |
+$model->insert({name => 'a'});
|
|
| 1278 |
+is_deeply($model->list->fetch_hash_all, [{name => 'a'}], 'basic');
|
|
| 1279 | 1279 |
|
| 1280 |
-$dbi->table('book');
|
|
| 1281 |
-eval{$dbi->table('book')->no_exists};
|
|
| 1280 |
+$dbi->model('book');
|
|
| 1281 |
+eval{$dbi->model('book')->no_exists};
|
|
| 1282 | 1282 |
like($@, qr/locate/); |
| 1283 | 1283 |
|
| 1284 | 1284 |
{
|
| ... | ... |
@@ -1292,27 +1292,27 @@ like($@, qr/locate/); |
| 1292 | 1292 |
sub connect {
|
| 1293 | 1293 |
my $self = shift->SUPER::connect(@_); |
| 1294 | 1294 |
|
| 1295 |
- $self->include_table( |
|
| 1296 |
- MyTable2 => [ |
|
| 1295 |
+ $self->include_model( |
|
| 1296 |
+ MyModel2 => [ |
|
| 1297 | 1297 |
'book', |
| 1298 | 1298 |
{company => 'Company'}
|
| 1299 | 1299 |
] |
| 1300 | 1300 |
); |
| 1301 | 1301 |
} |
| 1302 | 1302 |
|
| 1303 |
- package MyTable2::Base1; |
|
| 1303 |
+ package MyModel2::Base1; |
|
| 1304 | 1304 |
|
| 1305 | 1305 |
use strict; |
| 1306 | 1306 |
use warnings; |
| 1307 | 1307 |
|
| 1308 |
- use base 'DBIx::Custom::Table'; |
|
| 1308 |
+ use base 'DBIx::Custom::Model'; |
|
| 1309 | 1309 |
|
| 1310 |
- package MyTable2::book; |
|
| 1310 |
+ package MyModel2::book; |
|
| 1311 | 1311 |
|
| 1312 | 1312 |
use strict; |
| 1313 | 1313 |
use warnings; |
| 1314 | 1314 |
|
| 1315 |
- use base 'MyTable2::Base1'; |
|
| 1315 |
+ use base 'MyModel2::Base1'; |
|
| 1316 | 1316 |
|
| 1317 | 1317 |
sub insert {
|
| 1318 | 1318 |
my ($self, $param) = @_; |
| ... | ... |
@@ -1322,12 +1322,12 @@ like($@, qr/locate/); |
| 1322 | 1322 |
|
| 1323 | 1323 |
sub list { shift->select; }
|
| 1324 | 1324 |
|
| 1325 |
- package MyTable2::Company; |
|
| 1325 |
+ package MyModel2::Company; |
|
| 1326 | 1326 |
|
| 1327 | 1327 |
use strict; |
| 1328 | 1328 |
use warnings; |
| 1329 | 1329 |
|
| 1330 |
- use base 'MyTable2::Base1'; |
|
| 1330 |
+ use base 'MyModel2::Base1'; |
|
| 1331 | 1331 |
|
| 1332 | 1332 |
sub insert {
|
| 1333 | 1333 |
my ($self, $param) = @_; |
| ... | ... |
@@ -1339,10 +1339,34 @@ like($@, qr/locate/); |
| 1339 | 1339 |
} |
| 1340 | 1340 |
$dbi = MyDBI4->connect($NEW_ARGS->{0});
|
| 1341 | 1341 |
$dbi->execute("create table book (title, author)");
|
| 1342 |
-$table = $dbi->table('book');
|
|
| 1343 |
-$table->insert({title => 'a', author => 'b'});
|
|
| 1344 |
-is_deeply($table->list->fetch_hash_all, [{title => 'a', author => 'b'}], 'basic');
|
|
| 1342 |
+$model = $dbi->model('book');
|
|
| 1343 |
+$model->insert({title => 'a', author => 'b'});
|
|
| 1344 |
+is_deeply($model->list->fetch_hash_all, [{title => 'a', author => 'b'}], 'basic');
|
|
| 1345 | 1345 |
$dbi->execute("create table company (name)");
|
| 1346 |
-$table = $dbi->table('company');
|
|
| 1347 |
-$table->insert({name => 'a'});
|
|
| 1348 |
-is_deeply($table->list->fetch_hash_all, [{name => 'a'}], 'basic');
|
|
| 1346 |
+$model = $dbi->model('company');
|
|
| 1347 |
+$model->insert({name => 'a'});
|
|
| 1348 |
+is_deeply($model->list->fetch_hash_all, [{name => 'a'}], 'basic');
|
|
| 1349 |
+ |
|
| 1350 |
+{
|
|
| 1351 |
+ package MyDBI5; |
|
| 1352 |
+ |
|
| 1353 |
+ use strict; |
|
| 1354 |
+ use warnings; |
|
| 1355 |
+ |
|
| 1356 |
+ use base 'DBIx::Custom'; |
|
| 1357 |
+ |
|
| 1358 |
+ sub connect {
|
|
| 1359 |
+ my $self = shift->SUPER::connect(@_); |
|
| 1360 |
+ |
|
| 1361 |
+ $self->include_model('MyModel4');
|
|
| 1362 |
+ } |
|
| 1363 |
+} |
|
| 1364 |
+$dbi = MyDBI5->connect($NEW_ARGS->{0});
|
|
| 1365 |
+$dbi->execute("create table company (name)");
|
|
| 1366 |
+$model = $dbi->model('company');
|
|
| 1367 |
+$model->insert({name => 'a'});
|
|
| 1368 |
+is_deeply($model->list->fetch_hash_all, [{name => 'a'}], 'include all model');
|
|
| 1369 |
+$model = $dbi->model('book');
|
|
| 1370 |
+is_deeply($model->list->fetch_hash_all, [{name => 'a'}], 'include all model');
|
|
| 1371 |
+ |
|
| 1372 |
+ |
| ... | ... |
@@ -8,8 +8,8 @@ use base 'DBIx::Custom'; |
| 8 | 8 |
sub connect {
|
| 9 | 9 |
my $self = shift->SUPER::connect(@_); |
| 10 | 10 |
|
| 11 |
- $self->include_table( |
|
| 12 |
- MyTable1 => [ |
|
| 11 |
+ $self->include_model( |
|
| 12 |
+ MyModel1 => [ |
|
| 13 | 13 |
'book', |
| 14 | 14 |
{company => 'Company'}
|
| 15 | 15 |
] |
| ... | ... |
@@ -1,9 +1,9 @@ |
| 1 |
-package MyTable1::Company; |
|
| 1 |
+package MyModel1::Company; |
|
| 2 | 2 |
|
| 3 | 3 |
use strict; |
| 4 | 4 |
use warnings; |
| 5 | 5 |
|
| 6 |
-use base 'DBIx::Custom::Table'; |
|
| 6 |
+use base 'DBIx::Custom::Model'; |
|
| 7 | 7 |
|
| 8 | 8 |
|
| 9 | 9 |
sub insert {
|
| ... | ... |
@@ -1,9 +1,9 @@ |
| 1 |
-package MyTable1::book; |
|
| 1 |
+package MyModel1::book; |
|
| 2 | 2 |
|
| 3 | 3 |
use strict; |
| 4 | 4 |
use warnings; |
| 5 | 5 |
|
| 6 |
-use base 'DBIx::Custom::Table'; |
|
| 6 |
+use base 'DBIx::Custom::Model'; |
|
| 7 | 7 |
|
| 8 | 8 |
sub insert {
|
| 9 | 9 |
my ($self, $param) = @_; |
| ... | ... |
@@ -0,0 +1,5 @@ |
| 1 |
+package MyModel4; |
|
| 2 |
+ |
|
| 3 |
+use base 'DBIx::Custom::Model'; |
|
| 4 |
+ |
|
| 5 |
+1; |
| ... | ... |
@@ -0,0 +1,9 @@ |
| 1 |
+package MyModel4::book; |
|
| 2 |
+ |
|
| 3 |
+use base 'MyModel4'; |
|
| 4 |
+ |
|
| 5 |
+sub table { 'company' }
|
|
| 6 |
+ |
|
| 7 |
+sub list { shift->select }
|
|
| 8 |
+ |
|
| 9 |
+1; |
| ... | ... |
@@ -0,0 +1,8 @@ |
| 1 |
+package MyModel4::company; |
|
| 2 |
+ |
|
| 3 |
+use base 'MyModel4'; |
|
| 4 |
+ |
|
| 5 |
+sub insert { shift->SUPER::insert(param => $_[0]) }
|
|
| 6 |
+sub list { shift->select }
|
|
| 7 |
+ |
|
| 8 |
+1; |