removed experimental base_table() for class expandabi...
...lity.
... | ... |
@@ -1,3 +1,7 @@ |
1 |
+0.1645 |
|
2 |
+ removed experimental base_table() for class expandability. |
|
3 |
+ experimental table() can't set table object any more. |
|
4 |
+ added experimental include_table(). |
|
1 | 5 |
0.1644 |
2 | 6 |
update pod |
3 | 7 |
0.1643 |
... | ... |
@@ -1,6 +1,6 @@ |
1 | 1 |
package DBIx::Custom; |
2 | 2 |
|
3 |
-our $VERSION = '0.1644'; |
|
3 |
+our $VERSION = '0.1645'; |
|
4 | 4 |
|
5 | 5 |
use 5.008001; |
6 | 6 |
use strict; |
... | ... |
@@ -24,7 +24,6 @@ __PACKAGE__->attr( |
24 | 24 |
dbi_option => sub { {} }, |
25 | 25 |
query_builder => sub { DBIx::Custom::QueryBuilder->new }, |
26 | 26 |
result_class => 'DBIx::Custom::Result', |
27 |
- base_table => sub { DBIx::Custom::Table->new(dbi => shift) }, |
|
28 | 27 |
safety_column_name => sub { qr/^[\w\.]*$/ }, |
29 | 28 |
stash => sub { {} } |
30 | 29 |
); |
... | ... |
@@ -588,36 +587,53 @@ sub select { |
588 | 587 |
} |
589 | 588 |
|
590 | 589 |
sub table { |
591 |
- my $self = shift; |
|
592 |
- my $name = shift; |
|
590 |
+ my ($self, $name, $table) = @_; |
|
591 |
+ |
|
592 |
+ # Set |
|
593 |
+ $self->{table} ||= {}; |
|
594 |
+ if ($table) { |
|
595 |
+ $self->{table}{$name} = $table; |
|
596 |
+ return $self; |
|
597 |
+ } |
|
598 |
+ |
|
599 |
+ # Check table existance |
|
600 |
+ croak qq{Table "$name" is not included} |
|
601 |
+ unless $self->{table}{$name}; |
|
602 |
+ |
|
603 |
+ # Get |
|
604 |
+ return $self->{table}{$name}; |
|
605 |
+} |
|
606 |
+ |
|
607 |
+sub include_table { |
|
608 |
+ my ($self, $name_space, $table_infos) = @_; |
|
593 | 609 |
|
594 |
- # Create table |
|
595 |
- $self->{_tables} ||= {}; |
|
596 |
- unless (defined $self->{_tables}->{$name}) { |
|
597 |
- # Base table |
|
598 |
- my $base_table = $self->base_table; |
|
610 |
+ foreach my $table_info (@$table_infos) { |
|
599 | 611 |
|
600 |
- # Base methods |
|
601 |
- my $bmethods = ref $base_table->{_methods} eq 'HASH' |
|
602 |
- ? $base_table->{_methods} |
|
603 |
- : {}; |
|
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}; |
|
618 |
+ } |
|
619 |
+ else { $table_name = $table_class = $table_info } |
|
620 |
+ my $tclass = "${name_space}::$table_class"; |
|
604 | 621 |
|
605 |
- # Copy Methods |
|
606 |
- my $methods = {}; |
|
607 |
- $methods->{$_} = $bmethods->{$_} for keys %$bmethods; |
|
608 |
- $methods->{"base_$_"} = $bmethods->{$_} for keys %$bmethods; |
|
622 |
+ # Load |
|
623 |
+ croak qq{"$tclass" is invalid class name} |
|
624 |
+ if $tclass =~ /[^\w:]/; |
|
625 |
+ unless ($tclass->can('isa')) { |
|
626 |
+ eval "use $tclass"; |
|
627 |
+ croak $@ if $@; |
|
628 |
+ } |
|
609 | 629 |
|
610 |
- # Create table |
|
611 |
- my $table = $base_table->new( |
|
612 |
- dbi => $self, |
|
613 |
- name => $name, |
|
614 |
- ); |
|
615 |
- $table->method($methods); |
|
630 |
+ # Instantiate |
|
631 |
+ my $table = $tclass->new(dbi => $self, name => $table_name); |
|
616 | 632 |
|
617 |
- $self->{_tables}->{$name} = $table; |
|
633 |
+ # Set |
|
634 |
+ $self->table($table_name, $table); |
|
618 | 635 |
} |
619 |
- |
|
620 |
- return $self->{_tables}{$name}; |
|
636 |
+ return $self; |
|
621 | 637 |
} |
622 | 638 |
|
623 | 639 |
our %VALID_UPDATE_ARGS |
... | ... |
@@ -1165,6 +1181,37 @@ You can do anything in callback. |
1165 | 1181 |
Callback receive four arguments, dbi object, table name, |
1166 | 1182 |
column name and columninformation. |
1167 | 1183 |
|
1184 |
+=head2 C<(experimental) include_table> |
|
1185 |
+ |
|
1186 |
+ $dbi->include_table( |
|
1187 |
+ 'MyTable' => [ |
|
1188 |
+ 'book', 'person', 'company' |
|
1189 |
+ ] |
|
1190 |
+ ); |
|
1191 |
+ |
|
1192 |
+Include tables. First argument is name space. |
|
1193 |
+Second argument is array reference of class base names. |
|
1194 |
+ |
|
1195 |
+The following table is instantiated and included. |
|
1196 |
+ |
|
1197 |
+ MyTable::book |
|
1198 |
+ MyTable::person |
|
1199 |
+ MyTable::company |
|
1200 |
+ |
|
1201 |
+You can get these instance by C<table()>. |
|
1202 |
+ |
|
1203 |
+ my $book_table = $dbi->table('book'); |
|
1204 |
+ |
|
1205 |
+If you want to other name as table class, |
|
1206 |
+you can do like this. |
|
1207 |
+ |
|
1208 |
+ $dbi->include_table( |
|
1209 |
+ 'MyTable' => [ |
|
1210 |
+ {'book' => 'Book'}, |
|
1211 |
+ {'person' => 'Person'} |
|
1212 |
+ ] |
|
1213 |
+ ); |
|
1214 |
+ |
|
1168 | 1215 |
=head2 C<(experimental) method> |
1169 | 1216 |
|
1170 | 1217 |
$dbi->method( |
... | ... |
@@ -959,43 +959,51 @@ L<DBIx::Custom::Where> object is embedded into SQL. |
959 | 959 |
|
960 | 960 |
$dbi->execute($sql, param => $param); |
961 | 961 |
|
962 |
-=head2 7. Table object |
|
962 |
+=head2 7. Table class |
|
963 | 963 |
|
964 |
-=head3 Create table object C<table()> |
|
964 |
+=head3 Define table clas |
|
965 | 965 |
|
966 |
-You can create table object to access table in database. |
|
967 |
-use C<tabel()> to create table object. |
|
966 |
+You can define table class extending L<DBIx::Custom::Table> |
|
967 |
+to improve source code view. |
|
968 | 968 |
|
969 |
- my $table = $dbi->table('book'); |
|
969 |
+ package MyTable::book; |
|
970 |
+ use base 'DBIx::Custom::Table'; |
|
971 |
+ |
|
972 |
+ sub insert { ... } |
|
973 |
+ sub list { ... } |
|
970 | 974 |
|
971 |
-Return value is L<DBIx::Custom::Table>. |
|
972 |
-From table object, you can call C<insert()>, C<update()>, C<update_all()>�A |
|
973 |
-C<delete()>, C<delete_all()>, C<select()>. |
|
974 |
-You don't have to specify table name. |
|
975 |
+You can include and instantiate this class. |
|
975 | 976 |
|
976 |
- $table->insert(param => $param); |
|
977 |
+ $dbi->include_table( |
|
978 |
+ MyTable => [ |
|
979 |
+ 'book', |
|
980 |
+ ] |
|
981 |
+ ); |
|
977 | 982 |
|
978 |
-You can add any method to table object. |
|
983 |
+First argument is name space. |
|
984 |
+Second argument is array reference of class base names. |
|
979 | 985 |
|
980 |
- $table->method( |
|
981 |
- register => sub { |
|
982 |
- my $self = shift; |
|
983 |
- my $table_name = $self->name; |
|
984 |
- # ... |
|
985 |
- }, |
|
986 |
- list => sub { ... } |
|
987 |
- ); |
|
986 |
+You can use this table like this. |
|
987 |
+ |
|
988 |
+ my $result = $dbi->table('book')->list; |
|
989 |
+ |
|
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. |
|
993 |
+ |
|
994 |
+ $dbi->table('book')->insert(param => $param); |
|
988 | 995 |
|
989 |
-You can get table name by C<name()>. |
|
996 |
+This table is L<DBIx::Custom::Table> object. |
|
990 | 997 |
|
991 |
-You can call these method from table object. |
|
998 |
+If you need table name, you can get C<name()>. |
|
992 | 999 |
|
993 |
- $table->register(...); |
|
994 |
- $table->list(...); |
|
1000 |
+ my $name = $table->name; |
|
995 | 1001 |
|
996 |
-=head2 Use L<DBIx::Custom> and <DBI> methods |
|
1002 |
+You can get L<DBIx::Custom> object. |
|
997 | 1003 |
|
998 |
-You can call all methods of L<DBIx::Custom> and L<DBI> |
|
1004 |
+ my $dbi = $table->dbi; |
|
1005 |
+ |
|
1006 |
+You can also call all methods of L<DBIx::Custom> and L<DBI>. |
|
999 | 1007 |
|
1000 | 1008 |
# DBIx::Custom method |
1001 | 1009 |
$table->execute($sql); |
... | ... |
@@ -1004,40 +1012,9 @@ You can call all methods of L<DBIx::Custom> and L<DBI> |
1004 | 1012 |
$table->begin_work; |
1005 | 1013 |
$table->commit; |
1006 | 1014 |
|
1007 |
-=head2 Add table shared method |
|
1008 |
- |
|
1009 |
-To share methods in all tables, |
|
1010 |
-add method to base table. |
|
1011 |
-You can get base table by C<base_table()>. |
|
1015 |
+=head2 Table class example |
|
1012 | 1016 |
|
1013 |
- $dbi->base_table->method( |
|
1014 |
- count => sub { |
|
1015 |
- my $self = shift; |
|
1016 |
- return $self->select(column => ['count(*)']); |
|
1017 |
- } |
|
1018 |
- ); |
|
1019 |
- |
|
1020 |
-This method is used by all talbes. |
|
1021 |
- |
|
1022 |
- $dbi->table('book')->count(...); |
|
1023 |
- |
|
1024 |
-Even if same method name is added to table, |
|
1025 |
-You can use base table method by C<base_METHOD()>. |
|
1026 |
- |
|
1027 |
- $table->method( |
|
1028 |
- count => sub { |
|
1029 |
- my $self = shift; |
|
1030 |
- |
|
1031 |
- $self->base_count(...); |
|
1032 |
- |
|
1033 |
- # ... |
|
1034 |
- } |
|
1035 |
- ); |
|
1036 |
- |
|
1037 |
-=head2 Model example |
|
1038 |
- |
|
1039 |
-Generally, it is good to create model in constructor |
|
1040 |
-in the class extending L<DBIx::Custom>. |
|
1017 |
+Model example. |
|
1041 | 1018 |
|
1042 | 1019 |
package MyDBI; |
1043 | 1020 |
|
... | ... |
@@ -1046,25 +1023,25 @@ in the class extending L<DBIx::Custom>. |
1046 | 1023 |
sub connect { |
1047 | 1024 |
my $self = shift->SUPER::connect(@_); |
1048 | 1025 |
|
1049 |
- $self->base_table->method( |
|
1050 |
- delete_multi => sub { ... } |
|
1051 |
- ); |
|
1052 |
- |
|
1053 |
- $self->table('book')->method( |
|
1054 |
- register => sub { ... }, |
|
1055 |
- remove => sub { ... }, |
|
1056 |
- ); |
|
1057 |
- |
|
1058 |
- $self->table('company')->method( |
|
1059 |
- register => sub { ... }, |
|
1060 |
- remove => sub { ... }, |
|
1026 |
+ $self->include_table( |
|
1027 |
+ MyTable => [ |
|
1028 |
+ 'book', |
|
1029 |
+ 'company' |
|
1030 |
+ ] |
|
1061 | 1031 |
); |
1062 | 1032 |
} |
1063 |
- |
|
1064 |
-You can use this class in the following way. |
|
1065 |
- |
|
1066 |
- my $dbi = MyDBI->connect(...); |
|
1067 |
- $dbi->table('book')->delete_multi(...); |
|
1033 |
+ |
|
1034 |
+ package MyTable::book; |
|
1035 |
+ use base 'DBIx::Custom::Table'; |
|
1036 |
+ |
|
1037 |
+ sub insert { ... } |
|
1038 |
+ sub list { ... } |
|
1039 |
+ |
|
1040 |
+ package MyTable::company; |
|
1041 |
+ use base 'DBIx::Custom::Table'; |
|
1042 |
+ |
|
1043 |
+ sub insert { ... } |
|
1044 |
+ sub list { ... } |
|
1068 | 1045 |
|
1069 | 1046 |
=head2 8. Improve performance |
1070 | 1047 |
|
... | ... |
@@ -986,45 +986,53 @@ C<execute()>との連携です。SQLを作成するときに埋め込むこと |
986 | 986 |
|
987 | 987 |
$dbi->execute($sql, param => $param); |
988 | 988 |
|
989 |
-=head2 7. テーブルオブジェクト |
|
989 |
+=head2 7. テーブルクラス |
|
990 | 990 |
|
991 |
-=head3 テーブルオブジェクトの作成 C<table()> |
|
991 |
+=head3 テーブルクラス |
|
992 | 992 |
|
993 |
-データベースのテーブルにアクセスするための |
|
994 |
-テーブルオブジェクトを作成することができます。 |
|
993 |
+ソースコードの見通しをよくするために、 |
|
994 |
+L<DBIx::Custom::Table>を継承してテーブルクラスを作成することができます。 |
|
995 | 995 |
|
996 |
-テーブルオブジェクトを生成するにはC<table()>を使用します。 |
|
996 |
+ package MyTable::book; |
|
997 |
+ use base 'DBIx::Custom::Table'; |
|
998 |
+ |
|
999 |
+ sub insert { ... } |
|
1000 |
+ sub list { ... } |
|
997 | 1001 |
|
998 |
- my $table = $dbi->table('book'); |
|
1002 |
+このクラスを取り込んで、インスタンス化することができます。 |
|
999 | 1003 |
|
1000 |
-戻り値はL<DBIx::Custom::Table>オブジェクトです。 |
|
1001 |
-テーブルオブジェクトからはC<insert()>、C<update()>、C<update_all()>、 |
|
1002 |
-C<delete()>、C<delete_all()>、C<select()>などのメソッド呼び出すことができます。 |
|
1003 |
-C<table>を指定する必要はありません。 |
|
1004 |
+ $dbi->include_table( |
|
1005 |
+ MyTable => [ |
|
1006 |
+ 'book', |
|
1007 |
+ ] |
|
1008 |
+ ); |
|
1004 | 1009 |
|
1005 |
- $table->insert(param => $param); |
|
1010 |
+最初の引数は名前空間です。第二引数は、クラスの末尾の名前を含む |
|
1011 |
+配列のリファレンスです。 |
|
1006 | 1012 |
|
1007 |
-テーブルオブジェクトには任意のメソッドを追加することができます。 |
|
1013 |
+このテーブルは次のように利用することができます。 |
|
1008 | 1014 |
|
1009 |
- $table->method( |
|
1010 |
- register => sub { |
|
1011 |
- my $self = shift; |
|
1012 |
- my $table_name = $self->name; |
|
1013 |
- # something |
|
1014 |
- }, |
|
1015 |
- list => sub { ... }, |
|
1016 |
- ); |
|
1015 |
+ my $result = $dbi->table('book')->list; |
|
1016 |
+ |
|
1017 |
+テーブルクラスでは、テーブル名を指定することなしに |
|
1018 |
+C<insert()>, C<update()>, C<update_all()>, |
|
1019 |
+C<delete()>, C<delete_all()>, C<select()>などのメソッドを |
|
1020 |
+利用できるので |
|
1021 |
+L<DBIx::Custom>よりも少し便利です。 |
|
1022 |
+ |
|
1023 |
+ $dbi->table('book')->insert(param => $param); |
|
1024 |
+ |
|
1025 |
+テーブルクラスはL<DBIx::Custom::Table>です。 |
|
1017 | 1026 |
|
1018 |
-C<name()>を使用して、テーブル名を取得することができます。 |
|
1027 |
+必要であれば、C<name()>でテーブル名を取得することができます。 |
|
1019 | 1028 |
|
1020 |
-このようにして登録したメソッドはテーブルオブジェクトから呼び出すことができます。 |
|
1029 |
+ my $name = $table->name; |
|
1021 | 1030 |
|
1022 |
- $table->register(...); |
|
1023 |
- $table->list(...); |
|
1031 |
+L<DBIx::Custom>オブジェクトを取得することもできます。 |
|
1024 | 1032 |
|
1025 |
-=head2 L<DBIx::Custom>と<DBI>のメソッドの利用 |
|
1033 |
+ my $dbi = $table->dbi; |
|
1026 | 1034 |
|
1027 |
-テーブルからはL<DBIx::Custom>とL<DBI>のすべてのメソッドを呼び出すことができます。 |
|
1035 |
+L<DBIx::Custom>とL<DBI>のすべてのメソッドを呼び出すこともできます。 |
|
1028 | 1036 |
|
1029 | 1037 |
# DBIx::Custom method |
1030 | 1038 |
$table->execute($sql); |
... | ... |
@@ -1033,40 +1041,9 @@ C<name()>を使用して、テーブル名を取得することができます |
1033 | 1041 |
$table->begin_work; |
1034 | 1042 |
$table->commit; |
1035 | 1043 |
|
1036 |
-=head2 テーブルで共有のメソッドの追加 |
|
1037 |
- |
|
1038 |
-すべてのテーブルでメソッドを共有するには |
|
1039 |
-ベーステーブルにメソッドを登録します。 |
|
1040 |
-ベーステーブルを取得するにはC<base_table>を使用します。 |
|
1041 |
- |
|
1042 |
- $dbi->base_table->method( |
|
1043 |
- count => sub { |
|
1044 |
- my $self = shift; |
|
1045 |
- return $self->select(column => ['count(*)']); |
|
1046 |
- } |
|
1047 |
- ); |
|
1048 |
- |
|
1049 |
-このメソッドはすべてのテーブルから利用することができます。 |
|
1050 |
- |
|
1051 |
- $dbi->table('book')->count(...); |
|
1052 |
- |
|
1053 |
-また同名のメソッドをテーブルに追加して、ベーステーブルの |
|
1054 |
-メソッドを利用したい場合はC<base_METHOD()>と記述します。 |
|
1044 |
+=head2 テーブルクラスのサンプル |
|
1055 | 1045 |
|
1056 |
- $table->method( |
|
1057 |
- count => sub { |
|
1058 |
- my $self = shift; |
|
1059 |
- |
|
1060 |
- $self->base_count(...); |
|
1061 |
- |
|
1062 |
- # ... |
|
1063 |
- } |
|
1064 |
- ); |
|
1065 |
- |
|
1066 |
-=head2 モデルのサンプル |
|
1067 |
- |
|
1068 |
-一般的には、L<DBIx::Custom>を継承してコンストラクタの中に、モデルを作成 |
|
1069 |
-するのがよいでしょう。 |
|
1046 |
+テーブルクラスのサンプルです。 |
|
1070 | 1047 |
|
1071 | 1048 |
package MyDBI; |
1072 | 1049 |
|
... | ... |
@@ -1075,26 +1052,25 @@ C<name()>を使用して、テーブル名を取得することができます |
1075 | 1052 |
sub connect { |
1076 | 1053 |
my $self = shift->SUPER::connect(@_); |
1077 | 1054 |
|
1078 |
- $self->base_table->method( |
|
1079 |
- delete_multi => sub { ... } |
|
1080 |
- ); |
|
1081 |
- |
|
1082 |
- $self->table('book')->method( |
|
1083 |
- register => sub { ... }, |
|
1084 |
- remove => sub { ... }, |
|
1085 |
- ); |
|
1086 |
- |
|
1087 |
- $self->table('company')->method( |
|
1088 |
- register => sub { ... }, |
|
1089 |
- remove => sub { ... }, |
|
1055 |
+ $self->include_table( |
|
1056 |
+ MyTable => [ |
|
1057 |
+ 'book', |
|
1058 |
+ 'company' |
|
1059 |
+ ] |
|
1090 | 1060 |
); |
1091 | 1061 |
} |
1092 |
- |
|
1093 |
-次のようにこのクラスを利用することができます。 |
|
1094 |
- |
|
1095 |
- my $dbi = MyDBI->connect(...); |
|
1096 |
- $dbi->table('book')->delete_multi(...); |
|
1097 |
- $dbi->table('book')->register(...); |
|
1062 |
+ |
|
1063 |
+ package MyTable::book; |
|
1064 |
+ use base 'DBIx::Custom::Table'; |
|
1065 |
+ |
|
1066 |
+ sub insert { ... } |
|
1067 |
+ sub list { ... } |
|
1068 |
+ |
|
1069 |
+ package MyTable::company; |
|
1070 |
+ use base 'DBIx::Custom::Table'; |
|
1071 |
+ |
|
1072 |
+ sub insert { ... } |
|
1073 |
+ sub list { ... } |
|
1098 | 1074 |
|
1099 | 1075 |
=head2 8. パフォーマンスの改善 |
1100 | 1076 |
|
... | ... |
@@ -15,6 +15,9 @@ BEGIN { |
15 | 15 |
use_ok('DBIx::Custom'); |
16 | 16 |
} |
17 | 17 |
|
18 |
+use FindBin; |
|
19 |
+use lib "$FindBin::Bin/dbix-custom-core-sqlite"; |
|
20 |
+ |
|
18 | 21 |
# Function for test name |
19 | 22 |
sub test { print "# $_[0]\n" } |
20 | 23 |
|
... | ... |
@@ -732,54 +735,6 @@ is_deeply($infos, |
732 | 735 |
|
733 | 736 |
); |
734 | 737 |
|
735 |
-test 'table'; |
|
736 |
-$dbi = DBIx::Custom->connect($NEW_ARGS->{0}); |
|
737 |
-$dbi->execute($CREATE_TABLE->{0}); |
|
738 |
-$table = $dbi->table('table1'); |
|
739 |
-$table->insert(param => {key1 => 1, key2 => 2}); |
|
740 |
-$table->insert(param => {key1 => 3, key2 => 4}); |
|
741 |
-$rows = $table->select->fetch_hash_all; |
|
742 |
-is_deeply($rows, [{key1 => 1, key2 => 2}, {key1 => 3, key2 => 4}], |
|
743 |
- "select"); |
|
744 |
-$rows = $table->select(where => {key2 => 2}, append => 'order by key1', |
|
745 |
- column => ['key1', 'key2'])->fetch_hash_all; |
|
746 |
-is_deeply($rows, [{key1 => 1, key2 => 2}], |
|
747 |
- "insert insert select"); |
|
748 |
-$table->update(param => {key1 => 3}, where => {key2 => 2}); |
|
749 |
-$table->update(param => {key1 => 5}, where => {key2 => 4}); |
|
750 |
-$rows = $table->select(where => {key2 => 2})->fetch_hash_all; |
|
751 |
-is_deeply($rows, [{key1 => 3, key2 => 2}], |
|
752 |
- "update"); |
|
753 |
-$table->delete(where => {key2 => 2}); |
|
754 |
-$rows = $table->select->fetch_hash_all; |
|
755 |
-is_deeply($rows, [{key1 => 5, key2 => 4}], "delete"); |
|
756 |
-$table->update_all(param => {key1 => 3}); |
|
757 |
-$rows = $table->select->fetch_hash_all; |
|
758 |
-is_deeply($rows, [{key1 => 3, key2 => 4}], "update_all"); |
|
759 |
-$table->delete_all; |
|
760 |
-$rows = $table->select->fetch_hash_all; |
|
761 |
-is_deeply($rows, [], "delete_all"); |
|
762 |
- |
|
763 |
-$dbi->dbh->do($CREATE_TABLE->{2}); |
|
764 |
-$dbi->table('table2')->method( |
|
765 |
- ppp => sub { |
|
766 |
- my $self = shift; |
|
767 |
- |
|
768 |
- return $self->name; |
|
769 |
- } |
|
770 |
-); |
|
771 |
-is($dbi->table('table2')->ppp, 'table2', "method"); |
|
772 |
- |
|
773 |
-$dbi->table('table2')->method({ |
|
774 |
- qqq => sub { |
|
775 |
- my $self = shift; |
|
776 |
- |
|
777 |
- return $self->name; |
|
778 |
- } |
|
779 |
-}); |
|
780 |
-is($dbi->table('table2')->qqq, 'table2', "method"); |
|
781 |
- |
|
782 |
- |
|
783 | 738 |
test 'limit'; |
784 | 739 |
$dbi = DBIx::Custom->connect($NEW_ARGS->{0}); |
785 | 740 |
$dbi->execute($CREATE_TABLE->{0}); |
... | ... |
@@ -1272,10 +1227,6 @@ ok(!defined $result->default_filter); |
1272 | 1227 |
$result->default_filter('one'); |
1273 | 1228 |
is($result->default_filter->(), 1); |
1274 | 1229 |
|
1275 |
-$dbi->table('book'); |
|
1276 |
-eval{$dbi->table('book')->no_exists}; |
|
1277 |
-like($@, qr/locate/); |
|
1278 |
- |
|
1279 | 1230 |
test 'dbi_option'; |
1280 | 1231 |
$dbi = DBIx::Custom->connect(data_source => 'dbi:SQLite:dbname=:memory:', |
1281 | 1232 |
dbi_option => {PrintError => 1}); |
... | ... |
@@ -1290,28 +1241,6 @@ is_deeply($result->stash, {}, 'default'); |
1290 | 1241 |
$result->stash->{foo} = 1; |
1291 | 1242 |
is($result->stash->{foo}, 1, 'get and set'); |
1292 | 1243 |
|
1293 |
-test 'base_table'; |
|
1294 |
-$dbi = DBIx::Custom->new; |
|
1295 |
-$dbi->base_table->method( |
|
1296 |
- twice => sub { |
|
1297 |
- my $self = shift; |
|
1298 |
- |
|
1299 |
- return $_[0] * 2; |
|
1300 |
- } |
|
1301 |
-); |
|
1302 |
-$table = $dbi->table('book'); |
|
1303 |
-$table->method( |
|
1304 |
- three_times => sub { |
|
1305 |
- my $self = shift; |
|
1306 |
- return $_[0] * 3; |
|
1307 |
- } |
|
1308 |
-); |
|
1309 |
-is($table->base_twice(1), 2, 'method'); |
|
1310 |
-is($table->twice(1), 2, 'inherit method'); |
|
1311 |
-is($table->three_times(1), 3, 'child table method'); |
|
1312 |
-eval {$dbi->base_two}; |
|
1313 |
-ok($@); |
|
1314 |
- |
|
1315 | 1244 |
test 'filter __ expression'; |
1316 | 1245 |
$dbi = DBIx::Custom->connect($NEW_ARGS->{0}); |
1317 | 1246 |
$dbi->execute('create table company (id, name, location_id)'); |
... | ... |
@@ -1335,3 +1264,85 @@ $dbi->execute($CREATE_TABLE->{0}); |
1335 | 1264 |
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2}); |
1336 | 1265 |
$result = $dbi->select(selection => '* from table1', where => {key1 => 1}); |
1337 | 1266 |
is_deeply($result->fetch_hash_all, [{key1 => 1, key2 => 2}]); |
1267 |
+ |
|
1268 |
+test 'Table class'; |
|
1269 |
+use MyDBI1; |
|
1270 |
+$dbi = MyDBI1->connect($NEW_ARGS->{0}); |
|
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'); |
|
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'); |
|
1279 |
+ |
|
1280 |
+$dbi->table('book'); |
|
1281 |
+eval{$dbi->table('book')->no_exists}; |
|
1282 |
+like($@, qr/locate/); |
|
1283 |
+ |
|
1284 |
+{ |
|
1285 |
+ package MyDBI4; |
|
1286 |
+ |
|
1287 |
+ use strict; |
|
1288 |
+ use warnings; |
|
1289 |
+ |
|
1290 |
+ use base 'DBIx::Custom'; |
|
1291 |
+ |
|
1292 |
+ sub connect { |
|
1293 |
+ my $self = shift->SUPER::connect(@_); |
|
1294 |
+ |
|
1295 |
+ $self->include_table( |
|
1296 |
+ MyTable2 => [ |
|
1297 |
+ 'book', |
|
1298 |
+ {company => 'Company'} |
|
1299 |
+ ] |
|
1300 |
+ ); |
|
1301 |
+ } |
|
1302 |
+ |
|
1303 |
+ package MyTable2::Base1; |
|
1304 |
+ |
|
1305 |
+ use strict; |
|
1306 |
+ use warnings; |
|
1307 |
+ |
|
1308 |
+ use base 'DBIx::Custom::Table'; |
|
1309 |
+ |
|
1310 |
+ package MyTable2::book; |
|
1311 |
+ |
|
1312 |
+ use strict; |
|
1313 |
+ use warnings; |
|
1314 |
+ |
|
1315 |
+ use base 'MyTable2::Base1'; |
|
1316 |
+ |
|
1317 |
+ sub insert { |
|
1318 |
+ my ($self, $param) = @_; |
|
1319 |
+ |
|
1320 |
+ return $self->SUPER::insert(param => $param); |
|
1321 |
+ } |
|
1322 |
+ |
|
1323 |
+ sub list { shift->select; } |
|
1324 |
+ |
|
1325 |
+ package MyTable2::Company; |
|
1326 |
+ |
|
1327 |
+ use strict; |
|
1328 |
+ use warnings; |
|
1329 |
+ |
|
1330 |
+ use base 'MyTable2::Base1'; |
|
1331 |
+ |
|
1332 |
+ sub insert { |
|
1333 |
+ my ($self, $param) = @_; |
|
1334 |
+ |
|
1335 |
+ return $self->SUPER::insert(param => $param); |
|
1336 |
+ } |
|
1337 |
+ |
|
1338 |
+ sub list { shift->select; } |
|
1339 |
+} |
|
1340 |
+$dbi = MyDBI4->connect($NEW_ARGS->{0}); |
|
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'); |
|
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'); |
... | ... |
@@ -0,0 +1,19 @@ |
1 |
+package MyDBI1; |
|
2 |
+ |
|
3 |
+use strict; |
|
4 |
+use warnings; |
|
5 |
+ |
|
6 |
+use base 'DBIx::Custom'; |
|
7 |
+ |
|
8 |
+sub connect { |
|
9 |
+ my $self = shift->SUPER::connect(@_); |
|
10 |
+ |
|
11 |
+ $self->include_table( |
|
12 |
+ MyTable1 => [ |
|
13 |
+ 'book', |
|
14 |
+ {company => 'Company'} |
|
15 |
+ ] |
|
16 |
+ ); |
|
17 |
+} |
|
18 |
+ |
|
19 |
+1; |
... | ... |
@@ -0,0 +1,17 @@ |
1 |
+package MyTable1::Company; |
|
2 |
+ |
|
3 |
+use strict; |
|
4 |
+use warnings; |
|
5 |
+ |
|
6 |
+use base 'DBIx::Custom::Table'; |
|
7 |
+ |
|
8 |
+ |
|
9 |
+sub insert { |
|
10 |
+ my ($self, $param) = @_; |
|
11 |
+ |
|
12 |
+ return $self->SUPER::insert(param => $param); |
|
13 |
+} |
|
14 |
+ |
|
15 |
+sub list { shift->select; } |
|
16 |
+ |
|
17 |
+1; |
... | ... |
@@ -0,0 +1,16 @@ |
1 |
+package MyTable1::book; |
|
2 |
+ |
|
3 |
+use strict; |
|
4 |
+use warnings; |
|
5 |
+ |
|
6 |
+use base 'DBIx::Custom::Table'; |
|
7 |
+ |
|
8 |
+sub insert { |
|
9 |
+ my ($self, $param) = @_; |
|
10 |
+ |
|
11 |
+ return $self->SUPER::insert(param => $param); |
|
12 |
+} |
|
13 |
+ |
|
14 |
+sub list { shift->select; } |
|
15 |
+ |
|
16 |
+1; |