Showing 3 changed files with 76 additions and 16 deletions
+2
Changes
... ...
@@ -1,3 +1,5 @@
1
+0.1703
2
+    - added join new syntax.
1 3
 0.1702
2 4
     - removed EXPERIMENTAL status of some methods.
3 5
     - fixed some join bug
+48 -16
lib/DBIx/Custom.pm
... ...
@@ -1,7 +1,7 @@
1 1
 package DBIx::Custom;
2 2
 use Object::Simple -base;
3 3
 
4
-our $VERSION = '0.1702';
4
+our $VERSION = '0.1703';
5 5
 use 5.008001;
6 6
 
7 7
 use Carp 'croak';
... ...
@@ -1313,29 +1313,46 @@ sub _push_join {
1313 1313
     for (my $i = 0; $i < @$join; $i++) {
1314 1314
         
1315 1315
         # Search table in join clause
1316
-        my $join_clause = $join->[$i];
1316
+        my $join_clause;;
1317
+        my $option;
1318
+        if (ref $join->[$i] eq 'HASH') {
1319
+            $join_clause = $join->[$i]->{clause};
1320
+            $option = {table => $join->[$i]->{table}};
1321
+        }
1322
+        else {
1323
+            $join_clause = $join->[$i];
1324
+            $option = {};
1325
+        };
1317 1326
         my $j_clause = (split /\s+on\s+/, $join_clause)[-1];
1318 1327
         $j_clause =~ s/'.+?'//g;
1319 1328
         my $q_re = quotemeta($q);
1320 1329
         $j_clause =~ s/$q_re//g;
1321 1330
         my $c = $self->safety_character;
1322 1331
         my $join_re = qr/(?:^|\s)($c+)\.$c+\s+=\s+($c+)\.$c+/;
1323
-        if ($j_clause =~ $join_re) {
1324
-            my $table1 = $1;
1325
-            my $table2 = $2;
1326
-            croak qq{right side table of "$join_clause" must be unique }
1327
-                . _subname
1328
-              if exists $tree->{$table2};
1329
-            croak qq{Same table "$table1" is specified} . _subname
1330
-              if $table1 eq $table2;
1331
-            
1332
-            $tree->{$table2}
1333
-              = {position => $i, parent => $table1, join => $join_clause};
1332
+        
1333
+        my $table1;
1334
+        my $table2;
1335
+        if (my $table = $option->{table}) {
1336
+            $table1 = $table->[0];
1337
+            $table2 = $table->[1];
1334 1338
         }
1335
-        else {
1336
-            croak qq{join clause must have two table name after "on" keyword. } .
1337
-                  qq{"$join_clause" is passed }  . _subname
1339
+        elsif ($j_clause =~ $join_re) {
1340
+            $table1 = $1;
1341
+            $table2 = $2;
1338 1342
         }
1343
+        croak qq{join clause must have two table name after "on" keyword. } .
1344
+              qq{"$join_clause" is passed }  . _subname
1345
+          unless defined $table1 && defined $table2;
1346
+
1347
+        croak qq{right side table of "$join_clause" must be unique }
1348
+            . _subname
1349
+          if exists $tree->{$table2};
1350
+        
1351
+        croak qq{Same table "$table1" is specified} . _subname
1352
+          if $table1 eq $table2;
1353
+        
1354
+        $tree->{$table2}
1355
+          = {position => $i, parent => $table1, join => $join_clause};
1339 1356
     }
1340 1357
     
1341 1358
     # Search need tables
... ...
@@ -2758,6 +2775,21 @@ the following SQL is created
2758 2775
       left outer join company on book.company_id = company.id
2759 2776
     where company.name = ?;
2760 2777
 
2778
+You can specify two table by yourself. This is useful when join parser can't parse
2779
+the join clause correctly.
2780
+
2781
+    $dbi->select(
2782
+        table => 'book',
2783
+        column => ['company.location_id as location_id'],
2784
+        where => {'company.name' => 'Orange'},
2785
+        join => [
2786
+            {
2787
+                clause => 'left outer join location on company.location_id = location.id',
2788
+                table => ['company', 'location']
2789
+            }
2790
+        ]
2791
+    );
2792
+
2761 2793
 =item C<primary_key>
2762 2794
 
2763 2795
     primary_key => 'id'
+26
t/dbix-custom-core-sqlite.t
... ...
@@ -2189,6 +2189,32 @@ $result = $dbi->select(
2189 2189
     ]
2190 2190
 );
2191 2191
 is_deeply($result->all, [{'table2.key3' => 4}]);
2192
+$result = $dbi->select(
2193
+    table => 'table1',
2194
+    column => [{table2 => ['key3']}],
2195
+    join => [
2196
+        "left outer join table2 on table1.key1 = table2.key1 and table2.key3 = '4'"
2197
+    ]
2198
+);
2199
+is_deeply($result->all, [{'table2.key3' => 4}]);
2200
+
2201
+$dbi = DBIx::Custom->connect($NEW_ARGS->{0});
2202
+$dbi->execute($CREATE_TABLE->{0});
2203
+$dbi->execute($CREATE_TABLE->{2});
2204
+$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
2205
+$dbi->insert(table => 'table2', param => {key1 => 1, key3 => 4});
2206
+$dbi->insert(table => 'table2', param => {key1 => 1, key3 => 5});
2207
+$result = $dbi->select(
2208
+    table => 'table1',
2209
+    column => [{table2 => ['key3']}],
2210
+    join => [
2211
+        {
2212
+            clause => "left outer join table2 on table2.key3 = '4' and table1.key1 = table2.key1",
2213
+            table => ['table1', 'table2']
2214
+        }
2215
+    ]
2216
+);
2217
+is_deeply($result->all, [{'table2.key3' => 4}]);
2192 2218
 
2193 2219
 test 'mycolumn';
2194 2220
 $dbi = MyDBI8->connect($NEW_ARGS->{0});