Showing 6 changed files with 87 additions and 1 deletions
+1 -1
Changes
... ...
@@ -1,5 +1,5 @@
1 1
 0.1649
2
-    - add DBIx::Custom::Model column_clause() method.
2
+    - add experimental DBIx::Custom::Model column_clause() method.
3 3
     - select method column option can receive string.
4 4
     - DBIx::Custom::Model select() and select_at() think about relation attirbute
5 5
 0.1648
+1
lib/DBIx/Custom.pm
... ...
@@ -781,6 +781,7 @@ sub setup_model {
781 781
             }
782 782
         }
783 783
     );
784
+    return $self;
784 785
 }
785 786
 
786 787
 our %VALID_UPDATE_ARGS
+22
lib/DBIx/Custom/Guide.pod
... ...
@@ -1096,6 +1096,28 @@ You can set C<relation>
1096 1096
 
1097 1097
 This relation is used by C<select()>, C<select_at()>
1098 1098
 
1099
+=head2 Create column clause automatically : column_clause()
1100
+
1101
+To create column clause automatically, use C<column_clause()>.
1102
+Valude of C<table> and C<columns> is used.
1103
+
1104
+    my $column_clause = $model->column_clause;
1105
+
1106
+If C<table> is 'book'�AC<column> is ['id', 'name'],
1107
+the following clause is created.
1108
+
1109
+    book.id as id, book.name as name
1110
+
1111
+These column name is for removing column name ambiguities.
1112
+
1113
+If you remove some columns, use C<remove> option.
1114
+
1115
+    my $column_clause = $model->column_clause(remove => ['id']);
1116
+
1117
+If you add some column, use C<add> option.
1118
+
1119
+    my $column_clause = $model->column_clause(add => ['company.id as company__id']);
1120
+
1099 1121
 =head2 Model Examples
1100 1122
 
1101 1123
 Model examples
+22
lib/DBIx/Custom/Guide/Ja.pod
... ...
@@ -1131,6 +1131,28 @@ C<select_at()>で利用されます。
1131 1131
 
1132 1132
 ここで設定したリレーションはC<select()>, C<select_at()>で利用されます。
1133 1133
 
1134
+=head2 列名の自動生成 : column_clause()
1135
+
1136
+列名の節を自動生成するにはC<column_clause()>を使用します。
1137
+C<table>とC<columns>の値が利用されます。
1138
+
1139
+    my $column_clause = $model->column_clause;
1140
+
1141
+C<table>の値が'book'、C<column>の値が['id', 'name']で
1142
+あった場合は次のような列名の節が生成されます。
1143
+
1144
+    book.id as id, book.name as name
1145
+
1146
+このように列名の節を生成するのは、列名のあいまいさをなくすためです。
1147
+
1148
+不必要な列がある場合はC<remove>オプションで指定することができます。
1149
+
1150
+    my $column_clause = $model->column_clause(remove => ['id']);
1151
+
1152
+追加したい列がある場合は、C<add>オプションで追加することができます。
1153
+
1154
+    my $column_clause = $model->column_clause(add => ['company.id as company__id']);
1155
+
1134 1156
 =head2 モデルのサンプル
1135 1157
 
1136 1158
 モデルのサンプルです。
+22
lib/DBIx/Custom/Model.pm
... ...
@@ -190,6 +190,28 @@ L<DBIx::Custom> inherits all methods from L<Object::Simple>,
190 190
 and you can use all methods of the object set to C<dbi>.
191 191
 and implements the following new ones.
192 192
 
193
+=head2 C<column_clause()>
194
+
195
+To create column clause automatically, use C<column_clause()>.
196
+Valude of C<table> and C<columns> is used.
197
+
198
+    my $column_clause = $model->column_clause;
199
+
200
+If C<table> is 'book'�AC<column> is ['id', 'name'],
201
+the following clause is created.
202
+
203
+    book.id as id, book.name as name
204
+
205
+These column name is for removing column name ambiguities.
206
+
207
+If you remove some columns, use C<remove> option.
208
+
209
+    my $column_clause = $model->column_clause(remove => ['id']);
210
+
211
+If you add some column, use C<add> option.
212
+
213
+    my $column_clause = $model->column_clause(add => ['company.id as company__id']);
214
+
193 215
 =head2 C<delete>
194 216
 
195 217
     $table->delete(...);
+19
t/dbix-custom-core-sqlite.t
... ...
@@ -4,6 +4,7 @@ use warnings;
4 4
 
5 5
 use utf8;
6 6
 use Encode qw/encode_utf8 decode_utf8/;
7
+use Data::Dumper;
7 8
 
8 9
 BEGIN {
9 10
     eval { require DBD::SQLite; 1 }
... ...
@@ -1571,6 +1572,7 @@ test 'model select relation';
1571 1572
         
1572 1573
         $self->include_model('MyModel6');
1573 1574
         
1575
+        
1574 1576
         return $self;
1575 1577
     }
1576 1578
 }
... ...
@@ -1583,3 +1585,20 @@ $result = $dbi->model('table1')->select(column => ['key3'], where => {'table1.ke
1583 1585
 is($result->fetch_hash_first->{key3}, 3);
1584 1586
 $result = $dbi->model('table1')->select_at(column => ['key3'], where => [1]);
1585 1587
 is($result->fetch_hash_first->{key3}, 3);
1588
+
1589
+test 'column_clause';
1590
+$dbi = MyDBI7->connect($NEW_ARGS->{0});
1591
+$dbi->execute($CREATE_TABLE->{0});
1592
+$dbi->execute($CREATE_TABLE->{2});
1593
+$dbi->setup_model;
1594
+$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
1595
+$dbi->insert(table => 'table2', param => {key1 => 1, key3 => 3});
1596
+$model = $dbi->model('table1');
1597
+$result = $model->select(column => $model->column_clause, where => {'table1.key1' => 1});
1598
+is_deeply($result->fetch_hash_first, {key1 => 1, key2 => 2});
1599
+$result = $model->select(column => $model->column_clause(remove => ['key1']), where => {'table1.key1' => 1});
1600
+is_deeply($result->fetch_hash_first, {key2 => 2});
1601
+$result = $model->select(column => $model->column_clause(add => ['key3']), where => {'table1.key1' => 1});
1602
+is_deeply($result->fetch_hash_first, {key1 => 1, key2 => 2, key3 => 3});
1603
+
1604
+