[Rt-commit] [svn] r980 - in DBIx-SearchBuilder/trunk: . SearchBuilder SearchBuilder/Handle

jesse at pallas.eruditorum.org jesse at pallas.eruditorum.org
Sun May 30 02:57:18 EDT 2004


Author: jesse
Date: Sun May 30 02:57:17 2004
New Revision: 980

Modified:
   DBIx-SearchBuilder/trunk/   (props changed)
   DBIx-SearchBuilder/trunk/Changes
   DBIx-SearchBuilder/trunk/SearchBuilder.pm
   DBIx-SearchBuilder/trunk/SearchBuilder/Handle.pm
   DBIx-SearchBuilder/trunk/SearchBuilder/Handle/Pg.pm
Log:
Postgres patch from autrijus

Modified: DBIx-SearchBuilder/trunk/Changes
==============================================================================
--- DBIx-SearchBuilder/trunk/Changes	(original)
+++ DBIx-SearchBuilder/trunk/Changes	Sun May 30 02:57:17 2004
@@ -1,5 +1,8 @@
 Revision history for Perl extension DBIx::SearchBuilder.
 
+1.00_04	- Move Postgres specific join behaviour to the superclass so 
+	  everyone gets the benefit.
+
 1.00_03 - Remove "AS" from table name aliases on joins, since Oracle
           doesn't like em.
 

Modified: DBIx-SearchBuilder/trunk/SearchBuilder.pm
==============================================================================
--- DBIx-SearchBuilder/trunk/SearchBuilder.pm	(original)
+++ DBIx-SearchBuilder/trunk/SearchBuilder.pm	Sun May 30 02:57:17 2004
@@ -5,7 +5,7 @@
 use strict;
 use vars qw($VERSION);
 
-$VERSION = "1.00_03";
+$VERSION = "1.00_04";
 
 =head1 NAME
 
@@ -313,28 +313,7 @@
 sub _BuildJoins {
     my $self = shift;
 
-    # if we have a handle specific query builder, let's use that
-    if ( $self->_Handle->can('_BuildJoins') ) {
         return ( $self->_Handle->_BuildJoins($self) );
-    }
-
-    #Otherwise, let's do something generic
-
-    my $join_clause = $self->{'table'} . " main";
-
-    foreach my $join ( keys %{ $self->{'left_joins'} } ) {
-        $join_clause = "( "
-          . $join_clause
-          . $self->{'left_joins'}{$join}{'alias_string'}
-          . " ON  ("
-          . join ( ') AND ( ',
-                   values %{ $self->{'left_joins'}{$join}{'criteria'} } )
-          . "))";
-    }
-    my $aliases = join ( ", ", @{ $self->{'aliases'} } );
-    $join_clause .= ", $aliases" if ($aliases);
-
-    return ($join_clause);
 
 }
 

Modified: DBIx-SearchBuilder/trunk/SearchBuilder/Handle.pm
==============================================================================
--- DBIx-SearchBuilder/trunk/SearchBuilder/Handle.pm	(original)
+++ DBIx-SearchBuilder/trunk/SearchBuilder/Handle.pm	Sun May 30 02:57:17 2004
@@ -733,8 +733,105 @@
         @_
     );
 
-    my $sb = $args{'SearchBuilder'};
+    my $string;
+
+    my $alias;
+
+#If we're handed in an ALIAS2, we need to go remove it from the Aliases array.
+# Basically, if anyone generates an alias and then tries to use it in a join later, we want to be smart about
+# creating joins, so we need to go rip it out of the old aliases table and drop it in as an explicit join
+    if ( $args{'ALIAS2'} ) {
+
+        # this code is slow and wasteful, but it's clear.
+        my @aliases = @{ $args{'SearchBuilder'}->{'aliases'} };
+        my @new_aliases;
+        foreach my $old_alias (@aliases) {
+            if ( $old_alias =~ /^(.*?) ($args{'ALIAS2'})$/ ) {
+                $args{'TABLE2'} = $1;
+                $alias = $2;
+
+            }
+            else {
+                push @new_aliases, $old_alias;
+            }
+        }
+
+# If we found an alias, great. let's just pull out the table and alias for the other item
+        unless ($alias) {
+
+            # if we can't do that, can we reverse the join and have it work?
+            my $a1 = $args{'ALIAS1'};
+            my $f1 = $args{'FIELD1'};
+            $args{'ALIAS1'} = $args{'ALIAS2'};
+            $args{'FIELD1'} = $args{'FIELD2'};
+            $args{'ALIAS2'} = $a1;
+            $args{'FIELD2'} = $f1;
+
+            @aliases     = @{ $args{'SearchBuilder'}->{'aliases'} };
+            @new_aliases = ();
+            foreach my $old_alias (@aliases) {
+                if ( $old_alias =~ /^(.*?) ($args{'ALIAS2'})$/ ) {
+                    $args{'TABLE2'} = $1;
+                    $alias = $2;
+
+                }
+                else {
+                    push @new_aliases, $old_alias;
+                }
+            }
+
+        }
+
+        unless ($alias) {
+            return ( $self->_NormalJoin(%args) );
+        }
+        if ( $args{'ALIAS1'} ) {
+            return ( $self->_NormalJoin(%args) );
+        }
+
+        $args{'SearchBuilder'}->{'aliases'} = \@new_aliases;
+    }
+
+    else {
+        $alias = $args{'SearchBuilder'}->_GetAlias( $args{'TABLE2'} );
+
+    }
+
+    if ( $args{'TYPE'} =~ /LEFT/i ) {
+
+        $string = " LEFT JOIN " . $args{'TABLE2'} . " $alias ";
+
+    }
+    else {
+
+        $string = " JOIN " . $args{'TABLE2'} . " $alias ";
+
+    }
+    $args{'SearchBuilder'}->{'left_joins'}{"$alias"}{'alias_string'} = $string;
+    $args{'SearchBuilder'}->{'left_joins'}{"$alias"}{'depends_on'}   =
+      $args{'ALIAS1'};
+    $args{'SearchBuilder'}->{'left_joins'}{"$alias"}{'criteria'}
+      { 'criterion' . $args{'SearchBuilder'}->{'criteria_count'}++ } =
+      " $args{'ALIAS1'}.$args{'FIELD1'} = $alias.$args{'FIELD2'}";
 
+    return ($alias);
+}
+
+sub _NormalJoin {
+
+    my $self = shift;
+    my %args = (
+        SearchBuilder => undef,
+        TYPE          => 'normal',
+        FIELD1        => undef,
+        ALIAS1        => undef,
+        TABLE2        => undef,
+        FIELD2        => undef,
+        ALIAS2        => undef,
+        @_
+    );
+
+    my $sb = $args{'SearchBuilder'};
 
     if ( $args{'TYPE'} =~ /LEFT/i ) {
         my $alias = $sb->_GetAlias( $args{'TABLE2'} );
@@ -748,15 +845,53 @@
         return ($alias);
     }
     else {
-    $sb->DBIx::SearchBuilder::Limit(
-          ENTRYAGGREGATOR => 'AND',
-          QUOTEVALUE      => 0,
-          ALIAS           => $args{'ALIAS1'},
-          FIELD           => $args{'FIELD1'},
-          VALUE           => $args{'ALIAS2'} . "." . $args{'FIELD2'},
-          @_
-    ); 
+        $sb->DBIx::SearchBuilder::Limit(
+            ENTRYAGGREGATOR => 'AND',
+            QUOTEVALUE      => 0,
+            ALIAS           => $args{'ALIAS1'},
+            FIELD           => $args{'FIELD1'},
+            VALUE           => $args{'ALIAS2'} . "." . $args{'FIELD2'},
+            @_
+        );
+    }
+}
+
+# this code is all hacky and evil. but people desperately want _something_ and I'm 
+# super tired. refactoring gratefully appreciated.
+
+sub _BuildJoins {
+    my $self = shift;
+    my $sb   = shift;
+    my %seen_aliases;
+
+    $seen_aliases{'main'} = 1;
+
+    my $join_clause = $sb->{'table'} . " main ";
+
+    my @keys = ( keys %{ $sb->{'left_joins'} } );
+    my %seen;
+
+    while ( my $join = shift @keys ) {
+        if ( $seen_aliases{ $sb->{'left_joins'}{$join}{'depends_on'} } ) {
+            $join_clause = "(" . $join_clause;
+            $join_clause .=
+              $sb->{'left_joins'}{$join}{'alias_string'} . " ON (";
+            $join_clause .=
+              join ( ') AND( ',
+                values %{ $sb->{'left_joins'}{$join}{'criteria'} } );
+            $join_clause .= ")) ";
+
+            $seen_aliases{$join} = 1;
+        }
+        else {
+            push ( @keys, $join );
+            die "Unsatisfied dependency chain in Joins @keys"
+              if $seen{"@keys"}++;
+        }
+
     }
+    return ( join ( ", ", ( $join_clause, @{ $sb->{'aliases'} } ) ) );
+
 }
 
 # }}}

Modified: DBIx-SearchBuilder/trunk/SearchBuilder/Handle/Pg.pm
==============================================================================
--- DBIx-SearchBuilder/trunk/SearchBuilder/Handle/Pg.pm	(original)
+++ DBIx-SearchBuilder/trunk/SearchBuilder/Handle/Pg.pm	Sun May 30 02:57:17 2004
@@ -111,144 +111,6 @@
 
 }
 
-=head2 Join { Paramhash }
-
-Takes a paramhash of everything Searchbuildler::Record does + a parameter called 'SearchBuilder that contains a ref to a SearchBuilder object'.
-This performs the join.
-
-
-=cut
-
-
-sub Join {
-
-    my $self = shift;
-    my %args = (
-        SearchBuilder => undef,
-        TYPE          => 'normal',
-        FIELD1        => undef,
-        ALIAS1        => undef,
-        TABLE2        => undef,
-        FIELD2        => undef,
-        ALIAS2        => undef,
-        @_
-    );
-
-    my $string;
-
-    my $alias;
-
-#If we're handed in an ALIAS2, we need to go remove it from the Aliases array.
-# Basically, if anyone generates an alias and then tries to use it in a join later, we want to be smart about
-# creating joins, so we need to go rip it out of the old aliases table and drop it in as an explicit join
-    if ( $args{'ALIAS2'} ) {
-
-        # this code is slow and wasteful, but it's clear.
-        my @aliases = @{ $args{'SearchBuilder'}->{'aliases'} };
-        my @new_aliases;
-        foreach my $old_alias (@aliases) {
-            if ( $old_alias =~ /^(.*?) ($args{'ALIAS2'})$/ ) {
-                $args{'TABLE2'} = $1;
-                $alias = $2;
-
-            }
-            else {
-                push @new_aliases, $old_alias;
-            }
-        }
-        # If we found an alias, great. let's just pull out the table and alias for the other item
-        unless ($alias) {
-            # if we can't do that, can we reverse the join and have it work?
-            my $a1 = $args{'ALIAS1'};
-            my $f1 = $args{'FIELD1'};
-            $args{'ALIAS1'} = $args{'ALIAS2'};
-            $args{'FIELD1'} = $args{'FIELD2'};
-            $args{'ALIAS2'} = $a1;
-            $args{'FIELD2'} = $f1;
-
-            @aliases     = @{ $args{'SearchBuilder'}->{'aliases'} };
-            @new_aliases = ();
-            foreach my $old_alias (@aliases) {
-                if ( $old_alias =~ /^(.*?) ($args{'ALIAS2'})$/ ) {
-                    $args{'TABLE2'} = $1;
-                    $alias = $2;
-
-                }
-                else {
-                    push @new_aliases, $old_alias;
-                }
-            }
-
-        }
-
-        unless ($alias) {
-            return ( $self->SUPER::Join(%args) );
-        }
-        if ($args{'ALIAS1'}) {
-            return ( $self->SUPER::Join(%args) );
-        }
-
-        $args{'SearchBuilder'}->{'aliases'} = \@new_aliases;
-    }
-
-    else {
-        $alias = $args{'SearchBuilder'}->_GetAlias( $args{'TABLE2'} );
-
-    }
-
-    if ( $args{'TYPE'} =~ /LEFT/i ) {
-
-        $string = " LEFT JOIN " . $args{'TABLE2'} . " $alias ";
-
-    }
-    else {
-
-        $string = " JOIN " . $args{'TABLE2'} . " $alias ";
-
-    }
-    $args{'SearchBuilder'}->{'left_joins'}{"$alias"}{'alias_string'} = $string;
-    $args{'SearchBuilder'}->{'left_joins'}{"$alias"}{'depends_on'}   = $args{'ALIAS1'};
-    $args{'SearchBuilder'}->{'left_joins'}{"$alias"}{'criteria'} { 'criterion' . $args{'SearchBuilder'}->{'criteria_count'}++ } = " $args{'ALIAS1'}.$args{'FIELD1'} = $alias.$args{'FIELD2'}";
-
-    return ($alias);
-}
-
-
-
-# this code is all hacky and evil. but people desperately want _something_ and I'm 
-# super tired. refactoring gratefully appreciated.
-
-sub _BuildJoins {
-    my $self = shift;
-    my $sb   = shift;
-    my %seen_aliases;
-
-    $seen_aliases{'main'} = 1;
-
-    my $join_clause =$sb->{'table'} . " main " ;
-
-    my @keys = ( keys %{ $sb->{'left_joins'} } );
-
-    while ( my $join = shift @keys ) {
-        if ( $seen_aliases{ $sb->{'left_joins'}{$join}{'depends_on'} } ) {
-            $join_clause  = "(" . $join_clause;
-            $join_clause .= $sb->{'left_joins'}{$join}{'alias_string'} . " ON (";
-            $join_clause .=
-              join ( ') AND( ', values %{ $sb->{'left_joins'}{$join}{'criteria'} }
-              );
-            $join_clause .= ")) ";
-
-            $seen_aliases{$join} = 1;
-        }
-        else {
-            push ( @keys, $join );
-        }
-
-    }
-    return (
-                    join ( ", ", ($join_clause, @{ $sb->{'aliases'} }))) ;
-
-}
 # {{{ _MakeClauseCaseInsensitive
 
 =head2 _MakeClauseCaseInsensitive FIELD OPERATOR VALUE


More information about the Rt-commit mailing list