[Rt-commit] r4279 - in Jifty-DBI/trunk: . lib/Jifty/DBI

jesse at bestpractical.com jesse at bestpractical.com
Fri Dec 9 01:19:58 EST 2005


Author: jesse
Date: Fri Dec  9 01:19:57 2005
New Revision: 4279

Modified:
   Jifty-DBI/trunk/   (props changed)
   Jifty-DBI/trunk/lib/Jifty/DBI/Collection.pm
Log:
 r19851 at truegrounds:  jesse | 2005-12-09 01:08:24 -0500
 * delayed creation of group and orderby clauses


Modified: Jifty-DBI/trunk/lib/Jifty/DBI/Collection.pm
==============================================================================
--- Jifty-DBI/trunk/lib/Jifty/DBI/Collection.pm	(original)
+++ Jifty-DBI/trunk/lib/Jifty/DBI/Collection.pm	Fri Dec  9 01:19:57 2005
@@ -284,17 +284,7 @@
 sub _distinct_query {
     my $self         = shift;
     my $statementref = shift;
-    my $table        = shift;
-
-    # XXX - Postgres gets unhappy with distinct and order_by aliases
-    if ( exists $self->{'order_clause'}
-        && $self->{'order_clause'} =~ /(?<!main)\./ )
-    {
-        $$statementref = "SELECT main.* FROM $$statementref";
-    }
-    else {
-        $self->_handle->distinct_query( $statementref, $table );
-    }
+    $self->_handle->distinct_query($statementref, $self);
 }
 
 =head2 _build_joins
@@ -382,16 +372,14 @@
         $query_string .= $self->_where_clause . " "
     }
     if ( $self->_is_joined ) {
-        # DISTINCT query only required for multi-table selects
-        $self->_distinct_query( \$query_string, $self->table );
-    }
-    else {
-        $query_string = "SELECT main.* FROM $query_string";
-    }
-
-    $query_string .= ' ' . $self->_group_clause . ' ';
-
-    $query_string .= ' ' . $self->_order_clause . ' ';
+     # DISTINCT query only required for multi-table selects
+        $self->_distinct_query(\$query_string);
+     } else {
+         $query_string = "SELECT main.* FROM $query_string";
+        $query_string .= $self->_group_clause;
+        $query_string .= $self->_order_clause;
+     }
+ 
 
     $self->_apply_limits( \$query_string );
 
@@ -942,19 +930,31 @@
 =cut
 
 sub order_by {
-    my $self = shift;
-
-    my @args = @_;
-    unless (@args) {
-        return $self->_set_clause( order => '' );
-    }
-
+     my $self = shift;
+     my @args = @_;
+ 
     unless ( UNIVERSAL::isa( $args[0], 'HASH' ) ) {
         @args = {@args};
     }
+    $self->{'order_by'} = \@args;
+    $self->redo_search();
+}
+
+
+
+=head2 _order_clause
+
+returns the ORDER BY clause for the search.
+
+=cut
+
+sub _order_clause {
+    my $self = shift;
+
+    return '' unless $self->{'order_by'};
 
     my $clause = '';
-    foreach my $row (@args) {
+    foreach my $row ( @{$self->{'order_by'}} ) {
 
         my %rowhash = (
             alias  => 'main',
@@ -985,22 +985,10 @@
             $clause .= $rowhash{'order'};
         }
     }
-    $clause = "ORDER BY$clause" if $clause;
-    $self->_set_clause( order => $clause );
+    $clause = " ORDER BY$clause " if $clause;
+    return $clause;
 }
 
-=head2 _order_clause
-
-returns the ORDER BY clause for the search.
-
-=cut
-
-sub _order_clause {
-    my $self = shift;
-
-    return '' unless $self->{'order_clause'};
-    return ( $self->{'order_clause'} );
-}
 
 =head2 group_by_cols DEPRECATED
 
@@ -1037,17 +1025,33 @@
     my $self = shift;
 
     my @args = @_;
-    unless (@args) {
-        return $self->_set_clause( group => '' );
-    }
+
     unless ( UNIVERSAL::isa( $args[0], 'HASH' ) ) {
         @args = {@args};
     }
+    $self->{'group_by'} = \@args;
+    $self->redo_search();
+}
+
+
+=head2 _group_clause
+
+Private function to return the "GROUP BY" clause for this query.
+
+=cut
+
+sub _group_clause {
+    my $self = shift;
+    return '' unless $self->{'group_by'};
+
+     my $row;
+     my $clause;
+ 
+    foreach $row ( @{$self->{'group_by'}} ) {
+         my %rowhash = ( alias => 'main',
+
+
 
-    my $clause = '';
-    foreach my $row (@args) {
-        my %rowhash = (
-            alias  => 'main',
             column => undef,
             %$row
         );
@@ -1065,22 +1069,12 @@
             $clause .= $rowhash{'column'};
         }
     }
-
-    $clause = "GROUP BY" . $clause if $clause;
-    return $self->_set_clause( group => $clause );
-}
-
-=head2 _group_clause
-
-Private function to return the "GROUP BY" clause for this query.
-
-=cut
-
-sub _group_clause {
-    my $self = shift;
-
-    return '' unless $self->{'group_clause'};
-    return ( $self->{'group_clause'} );
+     if ($clause) {
+	return " GROUP BY" . $clause . " ";
+     }
+     else {
+	return '';
+     }
 }
 
 =head2 new_alias table_OR_CLASS
@@ -1578,6 +1572,3 @@
 
 =cut
 
-
-
-


More information about the Rt-commit mailing list