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

jifty-commit at lists.jifty.org jifty-commit at lists.jifty.org
Wed May 3 15:46:59 EDT 2006


Author: jesse
Date: Wed May  3 15:46:59 2006
New Revision: 979

Modified:
   Jifty-DBI/trunk/   (props changed)
   Jifty-DBI/trunk/lib/Jifty/DBI/Collection.pm
   Jifty-DBI/trunk/lib/Jifty/DBI/Handle/Oracle.pm
   Jifty-DBI/trunk/lib/Jifty/DBI/Handle/Pg.pm

Log:
 r13184 at hualien:  jesse | 2006-05-03 15:44:28 -0400
 * created an api to allow distinct toggling


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	Wed May  3 15:46:59 2006
@@ -373,7 +373,7 @@
     if ( $self->_is_limited ) {
         $query_string .= $self->_where_clause . " ";
     }
-    if ( $self->_is_joined ) {
+    if ( $self->distinct_required ) {
 
         # DISTINCT query only required for multi-table selects
         $self->_distinct_query( \$query_string );
@@ -389,6 +389,26 @@
 
 }
 
+
+=head2 distinct_required
+
+Returns true if Jifty::DBI expects that this result set will end up
+with repeated rows and should be "condensed" down to a single row for
+each unique primary key.  Out of the box, this method returns true if
+you've joined to another table.  If you're smarter than Jifty::DBI,
+feel free to override this method in your subclass.
+
+XXX TODO: it should be possible to create a better heuristic than the simple "is it joined?" question we're asking now. Something along the lines of "are we joining this table to something that is not the other table's primary key"
+
+=cut
+
+
+sub distinct_required {
+    my $self = shift;
+    return $self->_is_joined ? 1 : 0;
+
+}
+
 =head2 build_select_count_query
 
 Builds a SELECT statement to find the number of rows this collection

Modified: Jifty-DBI/trunk/lib/Jifty/DBI/Handle/Oracle.pm
==============================================================================
--- Jifty-DBI/trunk/lib/Jifty/DBI/Handle/Oracle.pm	(original)
+++ Jifty-DBI/trunk/lib/Jifty/DBI/Handle/Oracle.pm	Wed May  3 15:46:59 2006
@@ -226,36 +226,39 @@
 =cut
 
 sub distinct_query {
-    my $self         = shift;
-    my $statementref = shift;
-    my $sb           = shift;
-    my $table        = $sb->Table;
-
-    # Wrapp select query in a subselect as Oracle doesn't allow
-    # DISTINCT against CLOB/BLOB column types.
-    if ( $sb->_order_clause =~ /(?<!main)\./ ) {
-
-        # If we are ordering by something not in 'main', we need to GROUP
-        # BY and adjust the ORDER_BY accordingly
-        local $sb->{group_by}
-            = [ @{ $sb->{group_by} || [] }, { column => 'id' } ];
-        local $sb->{order_by} = [
-            map {
-                ( $_->{alias} and $_->{alias} ne "main" )
-                    ? { %{$_}, column => "min(" . $_->{column} . ")" }
-                    : $_
-                } @{ $sb->{order_by} }
-        ];
-        my $group = $sb->_group_clause;
-        my $order = $sb->_order_clause;
-        $$statementref
-            = "SELECT main.* FROM ( SELECT main.id FROM $$statementref $group $order ) distinctquery, $table main WHERE (main.id = distinctquery.id)";
-    } else {
-        $$statementref
-            = "SELECT main.* FROM ( SELECT DISTINCT main.id FROM $$statementref ) distinctquery, $table main WHERE (main.id = distinctquery.id) ";
-        $$statementref .= $sb->_group_clause;
-        $$statementref .= $sb->_order_clause;
-    }
+  my $self         = shift;
+  my $statementref = shift;
+  my $sb           = shift;
+  my $table        = $sb->Table;
+
+  # Wrapp select query in a subselect as Oracle doesn't allow
+  # DISTINCT against CLOB/BLOB column types.
+  if ( grep { $_->{'alias'} ne 'main' || defined $_->{'function'} }
+    @{ $sb->order_by } )
+  {
+
+    # If we are ordering by something not in 'main', we need to GROUP
+    # BY and adjust the ORDER_BY accordingly
+    local $sb->{group_by}
+      = [ @{ $sb->{group_by} || [] }, { column => 'id' } ];
+    local $sb->{order_by} = [
+      map {
+            ( $_->{alias} and $_->{alias} ne "main" )
+          ? { %{$_}, column => "min(" . $_->{column} . ")" }
+          : $_
+        } @{ $sb->{order_by} }
+    ];
+    my $group = $sb->_group_clause;
+    my $order = $sb->_order_clause;
+    $$statementref
+      = "SELECT main.* FROM ( SELECT main.id FROM $$statementref $group $order ) distinctquery, $table main WHERE (main.id = distinctquery.id)";
+  }
+  else {
+    $$statementref
+      = "SELECT main.* FROM ( SELECT DISTINCT main.id FROM $$statementref ) distinctquery, $table main WHERE (main.id = distinctquery.id) ";
+    $$statementref .= $sb->_group_clause;
+    $$statementref .= $sb->_order_clause;
+  }
 }
 
 1;

Modified: Jifty-DBI/trunk/lib/Jifty/DBI/Handle/Pg.pm
==============================================================================
--- Jifty-DBI/trunk/lib/Jifty/DBI/Handle/Pg.pm	(original)
+++ Jifty-DBI/trunk/lib/Jifty/DBI/Handle/Pg.pm	Wed May  3 15:46:59 2006
@@ -199,7 +199,9 @@
     my $sb           = shift;
     my $table        = $sb->table;
 
-    if ( $sb->_order_clause =~ /(?<!main)\./ ) {
+    if ( grep {    $_->{'alias'} ne 'main' 
+                || defined $_->{'function'} }
+      @{ $sb->order_by } ) {
 
         # If we are ordering by something not in 'main', we need to GROUP
         # BY and adjust the ORDER_BY accordingly


More information about the Jifty-commit mailing list