Skip Menu |

This queue is for tickets about the DBIx-SearchBuilder CPAN distribution.

Report information
The Basics
Id: 6450
Status: resolved
Priority: 0/
Queue: DBIx-SearchBuilder

People
Owner: Nobody in particular
Requestors: autrijus [...] autrijus.org
Cc:
AdminCc:

Bug Information
Severity: (no value)
Broken in: (no value)
Fixed in: (no value)



Date: Fri, 28 May 2004 17:48:27 +0800
From: Autrijus Tang <autrijus [...] autrijus.org>
To: bug-dbix-searchbuilder [...] rt.cpan.org
Subject: [PATCH] Make the Pg Join code the default
Here is the promised diff. Note that since _BuildJoins is now moved to base, the extra check of ->can('_BuildJoins') is redundant, but the speed hit is miniscule so I did not microoptimize it away. Thanks, /Autrijus/ diff -dur DBIx-SearchBuilder-1.00_03.orig/SearchBuilder/Handle/Pg.pm DBIx-SearchBuilder-1.00_03/SearchBuilder/Handle/Pg.pm --- DBIx-SearchBuilder-1.00_03.orig/SearchBuilder/Handle/Pg.pm Fri May 28 12:00:10 2004 +++ DBIx-SearchBuilder-1.00_03/SearchBuilder/Handle/Pg.pm Fri May 28 17:34: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 diff -dur DBIx-SearchBuilder-1.00_03.orig/SearchBuilder/Handle.pm DBIx-SearchBuilder-1.00_03/SearchBuilder/Handle.pm --- DBIx-SearchBuilder-1.00_03.orig/SearchBuilder/Handle.pm Fri May 28 12:00:10 2004 +++ DBIx-SearchBuilder-1.00_03/SearchBuilder/Handle.pm Fri May 28 17:36:46 2004 @@ -733,6 +733,99 @@ @_ ); + 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'}; @@ -757,6 +850,43 @@ @_ ); } +} + +# 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'} }))) ; + } # }}}
Download (untitled)
application/pgp-signature 187b

Message body not shown because it is not plain text.

Date: Fri, 28 May 2004 13:42:33 -0400
From: Jesse <jesse [...] fsck.com>
To: Autrijus Tang via RT <bug-DBIx-SearchBuilder [...] rt.cpan.org>
Subject: Re: [cpan #6450] [PATCH] Make the Pg Join code the default
RT-Send-Cc:
On Fri, May 28, 2004 at 05:47:27AM -0400, Autrijus Tang via RT wrote: Show quoted text
> > This message about DBIx-SearchBuilder was sent to you by autrijus@autrijus.org <autrijus@autrijus.org> via rt.cpan.org > > Full context and any attached attachments can be found at: > <URL: https://rt.cpan.org/Ticket/Display.html?id=6450 > > > Here is the promised diff. Note that since _BuildJoins is now > moved to base, the extra check of ->can('_BuildJoins') is redundant, > but the speed hit is miniscule so I did not microoptimize it away.
Does this patch just move code around or does it also fix the possible endless loop? Show quoted text
> Thanks, > /Autrijus/ >