| ... | ... | @@ -1,3 +1,6 @@ | 
| 1 | +0.1650 | |
| 2 | + - add experimental DBIx::Custom::Model name() attribute | |
| 3 | + - remove name specified feature from experimantal include_model(). | |
| 1 | 4 | 0.1649 | 
| 2 | 5 | - add experimental DBIx::Custom::Model column_clause() method. | 
| 3 | 6 | - select method column option can receive string. | 
| ... | ... | @@ -1,6 +1,6 @@ | 
| 1 | 1 | package DBIx::Custom; | 
| 2 | 2 |  | 
| 3 | -our $VERSION = '0.1649'; | |
| 3 | +our $VERSION = '0.1650'; | |
| 4 | 4 |  | 
| 5 | 5 | use 5.008001; | 
| 6 | 6 | use strict; | 
| ... | ... | @@ -741,14 +741,19 @@ sub include_model { | 
| 741 | 741 |  | 
| 742 | 742 |      foreach my $model_info (@$model_infos) { | 
| 743 | 743 |  | 
| 744 | - # Model name and class | |
| 745 | - my $model_name; | |
| 744 | + # Model class, name, table | |
| 746 | 745 | my $model_class; | 
| 746 | + my $model_name; | |
| 747 | + my $model_table; | |
| 747 | 748 |          if (ref $model_info eq 'HASH') { | 
| 748 | - $model_name = (keys %$model_info)[0]; | |
| 749 | -            $model_class = $model_info->{$model_name}; | |
| 749 | +            $model_class = $model_info->{class}; | |
| 750 | +            $model_name  = $model_info->{name}; | |
| 751 | +            $model_table = $model_info->{table}; | |
| 752 | + | |
| 753 | + $model_name ||= $model_class; | |
| 754 | + $model_table ||= $model_name; | |
| 750 | 755 | } | 
| 751 | -        else { $model_name = $model_class = $model_info } | |
| 756 | +        else { $model_class =$model_name = $model_table = $model_info } | |
| 752 | 757 |          my $mclass = "${name_space}::$model_class"; | 
| 753 | 758 |  | 
| 754 | 759 | # Load | 
| ... | ... | @@ -761,10 +766,11 @@ sub include_model { | 
| 761 | 766 |  | 
| 762 | 767 | # Instantiate | 
| 763 | 768 | my $model = $mclass->new(dbi => $self); | 
| 764 | - $model->table($model_name) unless $model->table; | |
| 769 | + $model->name($model_name) unless $model->name; | |
| 770 | + $model->table($model_table) unless $model->table; | |
| 765 | 771 |  | 
| 766 | 772 | # Set | 
| 767 | - $self->model($model_name, $model); | |
| 773 | + $self->model($model->name, $model); | |
| 768 | 774 | } | 
| 769 | 775 | return $self; | 
| 770 | 776 | } | 
| ... | ... | @@ -1005,41 +1005,44 @@ L<DBIx::Custom::Where> object is embedded into SQL. | 
| 1005 | 1005 | you can define model extending L<DBIx::Custom::Model> | 
| 1006 | 1006 | to improve source code view. | 
| 1007 | 1007 |  | 
| 1008 | - package MyModel::book; | |
| 1009 | - use base 'DBIx::Custom::Model'; | |
| 1010 | - | |
| 1011 | -    sub insert { ... } | |
| 1012 | -    sub list { ... } | |
| 1013 | - | |
| 1014 | -You can include and instantiate this class | |
| 1008 | +At first, you create basic model class extending <DBIx::Custom::Model>. | |
| 1015 | 1009 |  | 
| 1016 | - $dbi->include_model( | |
| 1017 | - MyModel => [ | |
| 1018 | - 'book', | |
| 1019 | - ] | |
| 1020 | - ); | |
| 1010 | + package MyModel; | |
| 1011 | + | |
| 1012 | + use base 'DBIx::Custom::Model'; | |
| 1021 | 1013 |  | 
| 1022 | -First argument is name space. | |
| 1023 | -Second argument is array reference of class base names. | |
| 1014 | +Next, you create each model classes. | |
| 1024 | 1015 |  | 
| 1025 | -If you don't specify second argument, All models under name space is | |
| 1026 | -included. | |
| 1016 | +MyModel::book | |
| 1027 | 1017 |  | 
| 1028 | -    $dbi->include_model('MyModel'); | |
| 1018 | + package MyModel::book; | |
| 1019 | + | |
| 1020 | + use base 'MyModel'; | |
| 1021 | + | |
| 1022 | +    sub insert { ... } | |
| 1023 | +    sub list { ... } | |
| 1029 | 1024 |  | 
| 1030 | -Note that in this case name spece module is needed. | |
| 1025 | +MyModel::company | |
| 1031 | 1026 |  | 
| 1032 | - # MyModel.pm | |
| 1033 | - package MyModel; | |
| 1027 | + package MyModel::company; | |
| 1034 | 1028 |  | 
| 1035 | - use base 'DBIx::Custom::Model'; | |
| 1029 | + use base 'MyModel'; | |
| 1030 | + | |
| 1031 | +    sub insert { ... } | |
| 1032 | +    sub list { ... } | |
| 1036 | 1033 |  | 
| 1037 | 1034 | The follwoing modules location is needed. | 
| 1038 | 1035 |  | 
| 1039 | 1036 | MyModel.pm | 
| 1040 | 1037 | MyModel / book.pm | 
| 1041 | 1038 | / company.pm | 
| 1042 | - | |
| 1039 | + | |
| 1040 | +You can include these models by C<include_model()> | |
| 1041 | + | |
| 1042 | +    $dbi->include_model('MyModel'); | |
| 1043 | + | |
| 1044 | +First argument is name space of model. | |
| 1045 | + | |
| 1043 | 1046 | You can use model like this. | 
| 1044 | 1047 |  | 
| 1045 | 1048 |      my $result = $dbi->model('book')->list; | 
| ... | ... | @@ -1096,6 +1099,40 @@ You can set C<relation> | 
| 1096 | 1099 |  | 
| 1097 | 1100 | This relation is used by C<select()>, C<select_at()> | 
| 1098 | 1101 |  | 
| 1102 | +=head2 Class name, Model name, Table name | |
| 1103 | + | |
| 1104 | +Class name, model name, and table name is a little different. | |
| 1105 | +Generally Class name is model name, and table name is model name. | |
| 1106 | + | |
| 1107 | + CLASS MODEL TABLE | |
| 1108 | + book (CLASS) -> book (MODEL) -> book | |
| 1109 | + | |
| 1110 | +You can change model name. | |
| 1111 | + | |
| 1112 | + package MyModel::book; | |
| 1113 | + | |
| 1114 | + __PACAKGE__->attr(name => 'book_model'); | |
| 1115 | + | |
| 1116 | + CLASS MODEL TABLE | |
| 1117 | + book book_model (MODEL) -> book_model | |
| 1118 | + | |
| 1119 | +Model name is the name used by L<model()> of L<DBIx::Custom>. | |
| 1120 | + | |
| 1121 | +    $dbi->model('book_model'); | |
| 1122 | + | |
| 1123 | +You can change table name. | |
| 1124 | + | |
| 1125 | + package MyModel::book; | |
| 1126 | + | |
| 1127 | + __PACAKGE__->attr(table => 'book_table'); | |
| 1128 | + | |
| 1129 | + CLASS MODEL TABLE | |
| 1130 | + book (CLASS) -> book book_table | |
| 1131 | + | |
| 1132 | +Table name is the table really accessed. | |
| 1133 | + | |
| 1134 | +    $dbi->model('book')->insert(...); # access to "book_table" | |
| 1135 | + | |
| 1099 | 1136 | =head2 Create column clause automatically : column_clause() | 
| 1100 | 1137 |  | 
| 1101 | 1138 | To create column clause automatically, use C<column_clause()>. | 
| ... | ... | @@ -1032,42 +1032,45 @@ C<execute()>との連携です。SQLを作成するときに埋め込むこと | 
| 1032 | 1032 | ソースコードの見通しをよくするために、 | 
| 1033 | 1033 | L<DBIx::Custom::Model>を継承してモデルを作成することができます。 | 
| 1034 | 1034 |  | 
| 1035 | - package MyModel::book; | |
| 1036 | - use base 'DBIx::Custom::Model'; | |
| 1037 | - | |
| 1038 | -    sub insert { ... } | |
| 1039 | -    sub list { ... } | |
| 1040 | - | |
| 1041 | -このクラスを取り込んで、インスタンス化することができます。 | |
| 1035 | +まず最初にモデルの元になるクラスを<DBIx::Custom::Model> | |
| 1036 | +を継承して作成します。 | |
| 1042 | 1037 |  | 
| 1043 | - $dbi->include_model( | |
| 1044 | - MyModel => [ | |
| 1045 | - 'book', | |
| 1046 | - ] | |
| 1047 | - ); | |
| 1038 | + package MyModel; | |
| 1039 | + | |
| 1040 | + use base 'DBIx::Custom::Model'; | |
| 1048 | 1041 |  | 
| 1049 | -最初の引数は名前空間です。第二引数は、クラスの末尾の名前を含む | |
| 1050 | -配列のリファレンスです。 | |
| 1042 | +次に個々のモデルクラスを作成します。 | |
| 1051 | 1043 |  | 
| 1052 | -第二引数を指定しなかった場合は、名前空間以下にあるすべてのモデル | |
| 1053 | -が取り込まれます。 | |
| 1044 | +MyModel::book | |
| 1054 | 1045 |  | 
| 1055 | -    $dbi->include_model('MyModel'); | |
| 1046 | + package MyModel::book; | |
| 1047 | + | |
| 1048 | + use base 'MyModel'; | |
| 1049 | + | |
| 1050 | +    sub insert { ... } | |
| 1051 | +    sub list { ... } | |
| 1056 | 1052 |  | 
| 1057 | -ただし、名前空間名そのものを表すモジュールが | |
| 1058 | -存在していることが必要なことに注意してください。 | |
| 1053 | +MyModel::company | |
| 1059 | 1054 |  | 
| 1060 | - # MyModel.pm | |
| 1061 | - package MyModel; | |
| 1055 | + package MyModel::company; | |
| 1062 | 1056 |  | 
| 1063 | - use base 'DBIx::Custom::Model'; | |
| 1057 | + use base 'MyModel'; | |
| 1058 | + | |
| 1059 | +    sub insert { ... } | |
| 1060 | +    sub list { ... } | |
| 1064 | 1061 |  | 
| 1065 | -次のようなモジュールの配置が必要です。 | |
| 1062 | +このように作成したモジュールを次のように配置してください。 | |
| 1066 | 1063 |  | 
| 1067 | 1064 | MyModel.pm | 
| 1068 | 1065 | MyModel / book.pm | 
| 1069 | 1066 | / company.pm | 
| 1070 | - | |
| 1067 | + | |
| 1068 | +このように作成したモデルはC<include_model()>で取り込むことができます。 | |
| 1069 | + | |
| 1070 | +    $dbi->include_model('MyModel'); | |
| 1071 | + | |
| 1072 | +第一引数は、モデルの名前空間になります。 | |
| 1073 | + | |
| 1071 | 1074 | モデルは次のように利用することができます。 | 
| 1072 | 1075 |  | 
| 1073 | 1076 |      my $result = $dbi->model('book')->list; | 
| ... | ... | @@ -1131,6 +1134,40 @@ C<select_at()>で利用されます。 | 
| 1131 | 1134 |  | 
| 1132 | 1135 | ここで設定したリレーションはC<select()>, C<select_at()>で利用されます。 | 
| 1133 | 1136 |  | 
| 1137 | +=head2 クラス名、モデル名、テーブル名 | |
| 1138 | + | |
| 1139 | +クラス名とモデル名とテーブル名の関係について書いておきます。 | |
| 1140 | +通常はクラス名がモデル名に利用され、テーブル名にはモデル名が利用されます。 | |
| 1141 | + | |
| 1142 | + クラス名 モデル名 テーブル名 | |
| 1143 | + book (クラス名) -> book (モデル名) -> book | |
| 1144 | + | |
| 1145 | +モデル名を変更することもできます。 | |
| 1146 | + | |
| 1147 | + package MyModel::book; | |
| 1148 | + | |
| 1149 | + __PACAKGE__->attr(name => 'book_model'); | |
| 1150 | + | |
| 1151 | + クラス名 モデル名 テーブル名 | |
| 1152 | + book book_model (モデル名) -> book_model | |
| 1153 | + | |
| 1154 | +モデル名というのは、L<DBIx::Custom>のL<model()>で利用される名前です。 | |
| 1155 | + | |
| 1156 | +    $dbi->model('book_model'); | |
| 1157 | + | |
| 1158 | +テーブル名を変更することもできます。 | |
| 1159 | + | |
| 1160 | + package MyModel::book; | |
| 1161 | + | |
| 1162 | + __PACAKGE__->attr(table => 'book_table'); | |
| 1163 | + | |
| 1164 | + クラス名 モデル名 テーブル名 | |
| 1165 | + book (クラス名) -> book book_table | |
| 1166 | + | |
| 1167 | +テーブル名というのは、実際にアクセスされるテーブルです。 | |
| 1168 | + | |
| 1169 | +    $dbi->model('book')->insert(...); # book_tableにアクセス | |
| 1170 | + | |
| 1134 | 1171 | =head2 列名の自動生成 : column_clause() | 
| 1135 | 1172 |  | 
| 1136 | 1173 | 列名の節を自動生成するにはC<column_clause()>を使用します。 | 
| ... | ... | @@ -11,7 +11,7 @@ use Carp 'croak'; | 
| 11 | 11 | push @DBIx::Custom::CARP_NOT, __PACKAGE__; | 
| 12 | 12 |  | 
| 13 | 13 | __PACKAGE__->attr( | 
| 14 | - ['dbi', 'table'], | |
| 14 | + ['dbi', 'name', 'table'], | |
| 15 | 15 |      columns => sub { [] }, | 
| 16 | 16 |      primary_key => sub { [] }, | 
| 17 | 17 |      relation => sub { {} } | 
| ... | ... | @@ -169,13 +169,21 @@ my $table = DBIx::Custom::Model->new(table => 'books'); | 
| 169 | 169 |  | 
| 170 | 170 | L<DBIx::Custom> object. | 
| 171 | 171 |  | 
| 172 | +=head2 C<name> | |
| 173 | + | |
| 174 | + my $name = $model->name; | |
| 175 | +    $model   = $model->name('book'); | |
| 176 | + | |
| 177 | +Model name. | |
| 178 | + | |
| 172 | 179 | =head2 C<table> | 
| 173 | 180 |  | 
| 174 | 181 | my $table = $model->table; | 
| 175 | 182 |      $model    = $model->table('book'); | 
| 176 | 183 |  | 
| 177 | -Table name. | |
| 178 | - | |
| 184 | +Table name. Model name and table name is different. | |
| 185 | +Table name is real table name in database. | |
| 186 | + | |
| 179 | 187 | =head2 C<primary_key> | 
| 180 | 188 |  | 
| 181 | 189 | my $primary_key = $model->primary_key; | 
| ... | ... | @@ -1304,7 +1304,7 @@ like($@, qr/locate/); | 
| 1304 | 1304 | $self->include_model( | 
| 1305 | 1305 | MyModel2 => [ | 
| 1306 | 1306 | 'book', | 
| 1307 | -                {company => 'Company'} | |
| 1307 | +                {class => 'Company', name => 'company'} | |
| 1308 | 1308 | ] | 
| 1309 | 1309 | ); | 
| 1310 | 1310 | } | 
| ... | ... | @@ -1534,6 +1534,12 @@ $dbi->execute($CREATE_TABLE->{1}); | 
| 1534 | 1534 |  $dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3}); | 
| 1535 | 1535 |  $dbi->model('table1')->delete_at(where => [1, 2]); | 
| 1536 | 1536 | is_deeply($dbi->select(table => 'table1')->fetch_hash_all, []); | 
| 1537 | +$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3}); | |
| 1538 | +$dbi->model('table1_1')->delete_at(where => [1, 2]); | |
| 1539 | +is_deeply($dbi->select(table => 'table1')->fetch_hash_all, []); | |
| 1540 | +$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3}); | |
| 1541 | +$dbi->model('table1_3')->delete_at(where => [1, 2]); | |
| 1542 | +is_deeply($dbi->select(table => 'table1')->fetch_hash_all, []); | |
| 1537 | 1543 |  | 
| 1538 | 1544 |  | 
| 1539 | 1545 | test 'update_at'; | 
| ... | ... | @@ -11,7 +11,7 @@ sub connect { | 
| 11 | 11 | $self->include_model( | 
| 12 | 12 | MyModel1 => [ | 
| 13 | 13 | 'book', | 
| 14 | -            {company => 'Company'} | |
| 14 | +            {class => 'Company', name => 'company'} | |
| 15 | 15 | ] | 
| 16 | 16 | ); | 
| 17 | 17 | } | 
| ... | ... | @@ -0,0 +1,12 @@ | 
| 1 | +package MyModel5::table1_1; | |
| 2 | + | |
| 3 | +use strict; | |
| 4 | +use warnings; | |
| 5 | + | |
| 6 | +use base 'MyModel5'; | |
| 7 | + | |
| 8 | +__PACKAGE__->attr(table => 'table1'); | |
| 9 | + | |
| 10 | +__PACKAGE__->attr('primary_key' => sub { ['key1', 'key2'] }); | |
| 11 | + | |
| 12 | +1; | 
| ... | ... | @@ -0,0 +1,13 @@ | 
| 1 | +package MyModel5::table1_2; | |
| 2 | + | |
| 3 | +use strict; | |
| 4 | +use warnings; | |
| 5 | + | |
| 6 | +use base 'MyModel5'; | |
| 7 | + | |
| 8 | +__PACKAGE__->attr(name => 'table1_3'); | |
| 9 | +__PACKAGE__->attr(table => 'table1'); | |
| 10 | + | |
| 11 | +__PACKAGE__->attr('primary_key' => sub { ['key1', 'key2'] }); | |
| 12 | + | |
| 13 | +1; |