Showing 5 changed files with 51 additions and 9 deletions
+1
Changes
... ...
@@ -1,4 +1,5 @@
1 1
 0.1643
2
+  add table tag
2 3
   fix  bug : filter can't overwirite undef value.
3 4
   add feature to apply_filter(). you can apply end filter.
4 5
   add feature to apply_filter(). TABLE__COLUMN is filterded now.
+20 -6
lib/DBIx/Custom.pm
... ...
@@ -183,7 +183,10 @@ sub create_query {
183 183
         my $q = $self->cache_method->($self, $source);
184 184
         
185 185
         # Create query
186
-        $query = DBIx::Custom::Query->new($q) if $q;
186
+        if ($q) {
187
+            $query = DBIx::Custom::Query->new($q);
188
+            $query->filters($self->filters);
189
+        }
187 190
     }
188 191
     
189 192
     unless ($query) {
... ...
@@ -197,7 +200,8 @@ sub create_query {
197 200
         # Cache query
198 201
         $self->cache_method->($self, $source,
199 202
                              {sql     => $query->sql, 
200
-                              columns => $query->columns})
203
+                              columns => $query->columns,
204
+                              tables  => $query->tables})
201 205
           if $cache;
202 206
     }
203 207
     
... ...
@@ -299,11 +303,13 @@ sub execute{
299 303
     $query = $self->create_query($query)
300 304
       unless ref $query;
301 305
     
302
-    # Auto filter
306
+    # Applied filter
303 307
     my $filter = {};
304
-    my $tables = $args{table} || [];
305
-    $tables = [$tables]
306
-      unless ref $tables eq 'ARRAY';
308
+    my $tables = $query->tables;
309
+    my $arg_tables = $args{table} || [];
310
+    $arg_tables = [$arg_tables]
311
+      unless ref $arg_tables eq 'ARRAY';
312
+    push @$tables, @$arg_tables;
307 313
     foreach my $table (@$tables) {
308 314
         $filter = {
309 315
             %$filter,
... ...
@@ -1316,6 +1322,14 @@ Method to set and get caches.
1316 1322
 
1317 1323
 The following tags is available.
1318 1324
 
1325
+=head2 C<table>
1326
+
1327
+Table tag
1328
+
1329
+    {table TABLE}    ->    TABLE
1330
+
1331
+This is used to teach what is applied table to C<execute()>.
1332
+
1319 1333
 =head2 C<?>
1320 1334
 
1321 1335
 Placeholder tag.
+1 -1
lib/DBIx/Custom/Query.pm
... ...
@@ -7,7 +7,7 @@ use base 'Object::Simple';
7 7
 
8 8
 use Carp 'croak';
9 9
 
10
-__PACKAGE__->attr([qw/columns sql sth filters/]);
10
+__PACKAGE__->attr([qw/columns sql sth filters tables/]);
11 11
 
12 12
 sub filter {
13 13
     my $self = shift;
+16 -1
lib/DBIx/Custom/QueryBuilder.pm
... ...
@@ -46,6 +46,9 @@ sub _build_query {
46 46
     # All Columns
47 47
     my $all_columns = [];
48 48
     
49
+    # Tables
50
+    my $tables = [];
51
+    
49 52
     # Build SQL 
50 53
     foreach my $node (@$tree) {
51 54
         
... ...
@@ -61,6 +64,14 @@ sub _build_query {
61 64
             # Tag arguments
62 65
             my $tag_args = $node->{tag_args};
63 66
             
67
+            # Table
68
+            if ($tag_name eq 'table') {
69
+                my $table = $tag_args->[0];
70
+                push @$tables, $table;
71
+                $sql .= $table;
72
+                next;
73
+            }
74
+
64 75
             # Get tag
65 76
             my $tag = $self->tag_processors->{$tag_name}
66 77
                              || $self->tags->{$tag_name};
... ...
@@ -101,7 +112,11 @@ sub _build_query {
101 112
     $sql .= ';' unless $sql =~ /;$/;
102 113
     
103 114
     # Query
104
-    my $query = DBIx::Custom::Query->new(sql => $sql, columns => $all_columns);
115
+    my $query = DBIx::Custom::Query->new(
116
+        sql => $sql,
117
+        columns => $all_columns,
118
+        tables => $tables
119
+    );
105 120
     
106 121
     return $query;
107 122
 }
+13 -1
t/dbix-custom-core-sqlite.t
... ...
@@ -481,7 +481,7 @@ $dbi->execute($CREATE_TABLE->{0});
481 481
 $source = 'select * from table1 where {= key1} and {= key2};';
482 482
 $dbi->create_query($source);
483 483
 is_deeply($dbi->{_cached}->{$source}, 
484
-          {sql => "select * from table1 where key1 = ? and key2 = ?;", columns => ['key1', 'key2']}, "cache");
484
+          {sql => "select * from table1 where key1 = ? and key2 = ?;", columns => ['key1', 'key2'], tables => []}, "cache");
485 485
 
486 486
 $dbi = DBIx::Custom->connect($NEW_ARGS->{0});
487 487
 $dbi->execute($CREATE_TABLE->{0});
... ...
@@ -664,6 +664,18 @@ $result = $dbi->execute("select * from table1 where {= key1} and {= key2};",
664 664
 $rows   = $result->fetch_hash_all;
665 665
 is_deeply($rows, [{key1 => 4, key2 => 2}], "execute");
666 666
 
667
+$dbi = DBIx::Custom->connect($NEW_ARGS->{0});
668
+$dbi->execute($CREATE_TABLE->{0});
669
+$dbi->register_filter(twice => sub { $_[0] * 2 });
670
+$dbi->apply_filter(
671
+    'table1', 'key1' => {out => 'twice', in => 'twice'}
672
+);
673
+$dbi->insert(table => 'table1', param => {key1 => 2, key2 => 2}, filter => {key1 => undef});
674
+$result = $dbi->execute("select * from {table table1} where {= key1} and {= key2};",
675
+                        param => {key1 => 1, key2 => 2});
676
+$rows   = $result->fetch_hash_all;
677
+is_deeply($rows, [{key1 => 4, key2 => 2}], "execute table tag");
678
+
667 679
 $dbi = DBIx::Custom->connect($NEW_ARGS->{0});
668 680
 $dbi->execute($CREATE_TABLE->{0});
669 681
 $dbi->execute($CREATE_TABLE->{2});