[Bps-public-commit] r17298 - in DBIx-SearchBuilder/trunk: .

alexmv at bestpractical.com alexmv at bestpractical.com
Mon Dec 22 18:28:28 EST 2008


Author: alexmv
Date: Mon Dec 22 18:28:27 2008
New Revision: 17298

Modified:
   DBIx-SearchBuilder/trunk/   (props changed)
   DBIx-SearchBuilder/trunk/SearchBuilder.pm
   DBIx-SearchBuilder/trunk/SearchBuilder/Handle.pm

Log:
 r40245 at kohr-ah:  chmrr | 2008-12-22 18:19:00 -0500
  * Allor ->Join to pre-existing collection object


Modified: DBIx-SearchBuilder/trunk/SearchBuilder.pm
==============================================================================
--- DBIx-SearchBuilder/trunk/SearchBuilder.pm	(original)
+++ DBIx-SearchBuilder/trunk/SearchBuilder.pm	Mon Dec 22 18:28:27 2008
@@ -1233,19 +1233,25 @@
 rather than merely an alias.  For this type of join, it will return
 the alias generated by the join.
 
-Instead of ALIAS1/FIELD1, it's possible to specify EXPRESSION, to join ALIAS2/TABLE2 on an arbitrary expression.
+Instead of ALIAS1/FIELD1, it's possible to specify EXPRESSION, to join
+ALIAS2/TABLE2 on an arbitrary expression.
+
+It is also possible to join to a pre-existing, already-limited
+L<DBIx::SearchBuilder> object, by passing it as COLLECTION2, instead
+of providing an ALIAS2 or TABLE2.
 
 =cut
 
 sub Join {
     my $self = shift;
     my %args = (
-        TYPE   => 'normal',
-        FIELD1 => undef,
-        ALIAS1 => 'main',
-        TABLE2 => undef,
-        FIELD2 => undef,
-        ALIAS2 => undef,
+        TYPE        => 'normal',
+        FIELD1      => undef,
+        ALIAS1      => 'main',
+        TABLE2      => undef,
+        COLLECTION2 => undef,
+        FIELD2      => undef,
+        ALIAS2      => undef,
         @_
     );
 

Modified: DBIx-SearchBuilder/trunk/SearchBuilder/Handle.pm
==============================================================================
--- DBIx-SearchBuilder/trunk/SearchBuilder/Handle.pm	(original)
+++ DBIx-SearchBuilder/trunk/SearchBuilder/Handle.pm	Mon Dec 22 18:28:27 2008
@@ -885,6 +885,7 @@
         ALIAS1        => 'main',
         FIELD1        => undef,
         TABLE2        => undef,
+        COLLECTION2   => undef,
         FIELD2        => undef,
         ALIAS2        => undef,
         EXPRESSION    => undef,
@@ -943,11 +944,37 @@
             return ( $self->_NormalJoin(%args) );
         }
         $args{'SearchBuilder'}->{'aliases'} = \@new_aliases;
-    }
+    } elsif ( $args{'COLLECTION2'} ) {
+        # We're joining to a pre-limited collection.  We need to take
+        # all clauses in the other collection, munge 'main.' to a new
+        # alias, apply them locally, then proceed as usual.
+        my $collection = delete $args{'COLLECTION2'};
+        $alias = $args{ALIAS2} = $args{'SearchBuilder'}->_GetAlias( $collection->Table );
+        $args{TABLE2} = $collection->Table;
+
+        eval {$collection->_ProcessRestrictions}; # RT hate
+
+        # Move over unused aliases
+        push @{$args{SearchBuilder}{aliases}}, @{$collection->{aliases}};
+
+        # Move over joins, as well
+        for my $join (keys %{$collection->{left_joins}}) {
+            my %alias = %{$collection->{left_joins}{$join}};
+            $alias{depends_on} = $alias if $alias{depends_on} eq "main";
+            $alias{criteria} = $self->_RenameRestriction(
+                RESTRICTIONS => $alias{criteria},
+                NEW          => $alias
+            );
+            $args{SearchBuilder}{left_joins}{$join} = \%alias;
+        }
 
-    else {
+        my $restrictions = $self->_RenameRestriction(
+            RESTRICTIONS => $collection->{restrictions},
+            NEW          => $alias
+        );
+        $args{SearchBuilder}{restrictions}{$_} = $restrictions->{$_} for keys %{$restrictions};
+    } else {
         $alias = $args{'SearchBuilder'}->_GetAlias( $args{'TABLE2'} );
-
     }
 
     my $meta = $args{'SearchBuilder'}->{'left_joins'}{"$alias"} ||= {};
@@ -968,6 +995,35 @@
     return ($alias);
 }
 
+sub _RenameRestriction {
+    my $self = shift;
+    my %args = (
+        RESTRICTIONS => undef,
+        OLD          => "main",
+        NEW          => undef,
+        @_,
+    );
+
+    my %return;
+    for my $key ( keys %{$args{RESTRICTIONS}} ) {
+        my $newkey = $key;
+        $newkey =~ s/^\Q$args{OLD}\E\./$args{NEW}./;
+        my @parts;
+        for my $part ( @{ $args{RESTRICTIONS}{$key} } ) {
+            if ( ref $part ) {
+                my %part = %{$part};
+                $part{field} =~ s/^\Q$args{OLD}\E\./$args{NEW}./;
+                $part{value} =~ s/^\Q$args{OLD}\E\./$args{NEW}./;
+                push @parts, \%part;
+            } else {
+                push @parts, $part;
+            }
+        }
+        $return{$newkey} = \@parts;
+    }
+    return \%return;
+}
+
 sub _NormalJoin {
 
     my $self = shift;



More information about the Bps-public-commit mailing list