Showing 4 changed files with 73 additions and 11 deletions
+2 -1
Changes
... ...
@@ -1,7 +1,8 @@
1 1
 0.1689
2 2
     - added EXPERIMENTAL available_data_type
3 3
     - simplified type_rule
4
-    - changed type_rule arguments format.
4
+    - changed type_rule arguments format
5
+    - added DBIx::Custom result_filter attribute
5 6
 0.1688
6 7
     - fixed bug that model insert, update, delete select can't
7 8
       odd number arguments
+11 -1
lib/DBIx/Custom.pm
... ...
@@ -84,7 +84,9 @@ sub AUTOLOAD {
84 84
     }
85 85
 }
86 86
 
87
-sub apply_filter {
87
+sub apply_filter { shift->_apply_filter(@_) }
88
+
89
+sub _apply_filter {
88 90
     my ($self, $table, @cinfos) = @_;
89 91
 
90 92
     # Initialize filters
... ...
@@ -389,6 +391,13 @@ sub create_model {
389 391
                ? [%{$model->filter}]
390 392
                : $model->filter;
391 393
     $self->apply_filter($model->table, @$filter);
394
+    my $result_filter = ref $model->result_filter eq 'HASH'
395
+               ? [%{$model->result_filter}]
396
+               : $model->result_filter;
397
+    for (my $i = 1; $i < @$result_filter; $i += 2) {
398
+        $result_filter->[$i] = {in => $result_filter->[$i]};
399
+    }
400
+    $self->apply_filter($model->table, @$result_filter);
392 401
     
393 402
     # Associate table with model
394 403
     croak "Table name is duplicated " . _subname
... ...
@@ -579,6 +588,7 @@ sub execute {
579 588
         my $filter = {};
580 589
         $filter->{in}  = {};
581 590
         $filter->{end} = {};
591
+        push @$tables, $main_table if $main_table;
582 592
         foreach my $table (@$tables) {
583 593
             foreach my $way (qw/in end/) {
584 594
                 $filter->{$way} = {
+23 -9
lib/DBIx/Custom/Model.pm
... ...
@@ -16,6 +16,7 @@ __PACKAGE__->attr(
16 16
     table_alias => sub { {} },
17 17
     columns => sub { [] },
18 18
     filter => sub { [] },
19
+    result_filter => sub { [] },
19 20
     join => sub { [] },
20 21
     type => sub { [] },
21 22
     primary_key => sub { [] }
... ...
@@ -163,7 +164,10 @@ L<DBIx::Custom> object.
163 164
 =head2 C<filter>
164 165
 
165 166
     my $dbi = $model->filter
166
-    $model  = $model->filter({out => 'tp_to_date', in => 'date_to_tp'});
167
+    $model  = $model->filter(
168
+        title => {out => 'tp_to_date', in => 'date_to_tp'}
169
+        author => {out => ..., in => ...},
170
+    );
167 171
 
168 172
 This filter is applied when L<DBIx::Custom>'s C<include_model()> is called.
169 173
 
... ...
@@ -183,6 +187,24 @@ Model name.
183 187
     
184 188
 Join clause, this is used as C<select()>'s C<join> option.
185 189
 
190
+=head2 C<primary_key>
191
+
192
+    my $primary_key = $model->primary_key;
193
+    $model          = $model->primary_key(['id', 'number']);
194
+
195
+Foreign key, this is used as C<primary_key> of C<insert_at>,C<update_at()>,
196
+C<delete_at()>,C<select_at()>.
197
+
198
+=head2 C<result_filter>
199
+
200
+    my $dbi = $model->result_filter
201
+    $model  = $model->result_filter(
202
+        title => sub { ... },
203
+        author => sub { ... }
204
+    );
205
+
206
+This filter is applied when L<DBIx::Custom>'s C<include_model()> or C<create_model> is called.
207
+
186 208
 =head2 C<table>
187 209
 
188 210
     my $table = $model->table;
... ...
@@ -200,14 +222,6 @@ Table alias. If you define table alias,
200 222
 same filter as the table is avaliable
201 223
 , and can write $dbi->column('user1') to get all columns.
202 224
 
203
-=head2 C<primary_key>
204
-
205
-    my $primary_key = $model->primary_key;
206
-    $model          = $model->primary_key(['id', 'number']);
207
-
208
-Foreign key, this is used as C<primary_key> of C<insert_at>,C<update_at()>,
209
-C<delete_at()>,C<select_at()>.
210
-
211 225
 =head2 C<type>
212 226
 
213 227
     my $type = $model->type;
+37
t/dbix-custom-core-sqlite.t
... ...
@@ -2684,4 +2684,41 @@ $dbi->insert({key1 => 2}, table => 'table1', type_rule_off => 1);
2684 2684
 $result = $dbi->select(table => 'table1', type_rule_off => 1);
2685 2685
 is($result->one->{key1}, 2);
2686 2686
 
2687
+
2688
+test 'result_filter';
2689
+$dbi = DBIx::Custom->connect($NEW_ARGS->{0});
2690
+$dbi->execute($CREATE_TABLE->{0});
2691
+$dbi->execute($CREATE_TABLE->{2});
2692
+
2693
+$dbi->create_model(
2694
+    table => 'table1',
2695
+    join => [
2696
+       'left outer join table2 on table1.key1 = table2.key1'
2697
+    ],
2698
+    primary_key => ['key1'],
2699
+    result_filter => {
2700
+        key1 => sub { $_[0] * 2 }
2701
+    }
2702
+);
2703
+$model2 = $dbi->create_model(
2704
+    table => 'table2',
2705
+    result_filter => [
2706
+        [qw/key1 key3/] => sub { $_[0] * 3 }
2707
+    ]
2708
+);
2709
+$dbi->setup_model;
2710
+$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
2711
+$dbi->insert(table => 'table2', param => {key1 => 1, key3 => 3});
2712
+$model = $dbi->model('table1');
2713
+$result = $model->select(
2714
+    column => [
2715
+        $model->mycolumn,
2716
+        {table2 => [qw/key1 key3/]}
2717
+    ],
2718
+    where => {'table1.key1' => 1}
2719
+);
2720
+is_deeply($result->one,
2721
+          {key1 => 2, key2 => 2, 'table2.key1' => 3, 'table2.key3' => 9});
2722
+is_deeply($model2->select->one, {key1 => 3, key3 => 9});
2723
+
2687 2724
 =cut