Showing 3 changed files with 71 additions and 4 deletions
+2 -1
Changes
... ...
@@ -1,6 +1,7 @@
1 1
 0.1670
2 2
     - removed EXPERIMETNAL select() column hash option. it's a little complex.
3
-    - added EXPERIMETAL select() param option.
3
+    - added EXPERIMENTAL select() param option.
4
+    - added EXPERIMENTAL replace().
4 5
 0.1669
5 6
     - renamed update_param to update_param_tag, update_param is DEPRECATED!
6 7
     - renamed insert_param to insert_param_tag, insert_param is DEPRECATED!
+50
lib/DBIx/Custom.pm
... ...
@@ -840,6 +840,25 @@ sub register_filter {
840 840
 
841 841
 sub register_tag { shift->query_builder->register_tag(@_) }
842 842
 
843
+sub replace {
844
+    my ($self, $join, $search, $replace) = @_;
845
+    
846
+    my @replace_join;
847
+    my $is_replaced;
848
+    foreach my $j (@$join) {
849
+        if ($search eq $j) {
850
+            push @replace_join, $replace;
851
+            $is_replaced = 1;
852
+        }
853
+        else {
854
+            push @replace_join, $j;
855
+        }
856
+    }
857
+    croak qq{Can't replace "$search" with "$replace"} unless $is_replaced;
858
+    
859
+    return @replace_join;
860
+}
861
+
843 862
 our %SELECT_ARGS
844 863
   = map { $_ => 1 } @COMMON_ARGS, qw/column where append relation join param/;
845 864
 
... ...
@@ -2326,6 +2345,21 @@ Column names is
2326 2345
 
2327 2346
     ['title', 'author']
2328 2347
 
2348
+=head2 C<replace> EXPERIMENTAL
2349
+    
2350
+    my $join = [
2351
+        'left outer join table2 on table1.key1 = table2.key1',
2352
+        'left outer join table3 on table2.key3 = table3.key3'
2353
+    ];
2354
+    $join = $dbi->replace(
2355
+        $join,
2356
+        'left outer join table2 on table1.key1 = table2.key1',
2357
+        'left outer join (select * from table2 where {= table2.key1}) ' . 
2358
+          'as table2 on table1.key1 = table2.key1'
2359
+    );
2360
+
2361
+Replace join clauses if match the expression.
2362
+
2329 2363
 =head2 C<select>
2330 2364
 
2331 2365
     my $result = $dbi->select(
... ...
@@ -2415,6 +2449,22 @@ In above select, the following SQL is created.
2415 2449
       left outer join company on book.company_id = company.id
2416 2450
     where company.name = Orange
2417 2451
 
2452
+=item C<param> EXPERIMETNAL
2453
+
2454
+Parameter shown before where clause.
2455
+    
2456
+    $dbi->select(
2457
+        table => 'table1',
2458
+        column => 'table1.key1 as table1_key1, key2, key3',
2459
+        where   => {'table1.key2' => 3},
2460
+        join  => ['inner join (select * from table2 where {= table2.key3})' . 
2461
+                  ' as table2 on table1.key1 = table2.key1'],
2462
+        param => {'table2.key3' => 5}
2463
+    );
2464
+
2465
+For example, if you want to contain tag in join clause, 
2466
+you can pass parameter by C<param> option.
2467
+
2418 2468
 =item C<append>
2419 2469
 
2420 2470
 Append statement to last of SQL. This is string.
+19 -3
t/dbix-custom-core-sqlite.t
... ...
@@ -72,6 +72,7 @@ my $model2;
72 72
 my $where;
73 73
 my $update_param;
74 74
 my $insert_param;
75
+my $join;
75 76
 
76 77
 # Prepare table
77 78
 $dbi = DBIx::Custom->connect($NEW_ARGS->{0});
... ...
@@ -2097,7 +2098,6 @@ $dbi->insert(table => 'table1', param => {key1 => 2, key2 => 3});
2097 2098
 $dbi->execute($CREATE_TABLE->{2});
2098 2099
 $dbi->insert(table => 'table2', param => {key1 => 1, key3 => 4});
2099 2100
 $dbi->insert(table => 'table2', param => {key1 => 2, key3 => 5});
2100
-$DB::single = 1;
2101 2101
 $rows = $dbi->select(
2102 2102
     table => 'table1',
2103 2103
     column => 'table1.key1 as table1_key1, key2, key3',
... ...
@@ -2116,12 +2116,28 @@ $dbi->insert(table => 'table1', param => {key1 => 2, key2 => 3});
2116 2116
 $dbi->execute($CREATE_TABLE->{2});
2117 2117
 $dbi->insert(table => 'table2', param => {key1 => 1, key3 => 4});
2118 2118
 $dbi->insert(table => 'table2', param => {key1 => 2, key3 => 5});
2119
+$join = ['inner join table2 on "table1"."key1" = "table2"."key1"'];
2120
+$join = $dbi->replace(
2121
+    $join,
2122
+    'inner join table2 on "table1"."key1" = "table2"."key1"',
2123
+    'inner join (select * from table2 where {= table2.key3}) as table2'
2124
+);
2125
+
2119 2126
 $rows = $dbi->select(
2120 2127
     table => 'table1',
2121 2128
     column => 'table1.key1 as table1_key1, key2, key3',
2122 2129
     where   => {'table1.key2' => 3},
2123
-    join  => ['inner join (select * from table2 where {= table2.key3})' . 
2124
-              ' as table2 on "table1"."key1" = "table2"."key1"'],
2130
+    join  => ['inner join table2 on "table1"."key1" = "table2"."key1"'],
2125 2131
     param => {'table2.key3' => 5}
2126 2132
 )->fetch_hash_all;
2127 2133
 is_deeply($rows, [{table1_key1 => 2, key2 => 3, key3 => 5}]);
2134
+
2135
+$join = ['inner join table2 on "table1"."key1" = "table2"."key1"'];
2136
+eval {
2137
+    $join = $dbi->replace(
2138
+        $join,
2139
+        'pppp inner join table2 on "table1"."key1" = "table2"."key1"',
2140
+        'inner join (select * from table2 where {= table2.key3}) as table2'
2141
+    );
2142
+};
2143
+like($@, qr/replace/);