[Jifty-commit] r4036 - Jifty-DBI/trunk/lib/Jifty/DBI

jifty-commit at lists.jifty.org jifty-commit at lists.jifty.org
Mon Sep 3 07:29:10 EDT 2007


Author: clkao
Date: Mon Sep  3 07:29:09 2007
New Revision: 4036

Modified:
   Jifty-DBI/trunk/lib/Jifty/DBI/Collection.pm
   Jifty-DBI/trunk/lib/Jifty/DBI/Handle.pm

Log:
* Allow is_distinct when joining collections, hinting the
  resulting collection is still distinct.

* If table2 is given as a Jifty::DBI::Collection object,
  detect if the join is still distinct.


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	Mon Sep  3 07:29:09 2007
@@ -399,7 +399,26 @@
     } else {
         return ( @{ $self->{'aliases'} } );
     }
+}
+
+=head2 _is_distinctly_joined
+
+Returns true if this collection is joining multiple table, but is
+joining other table's distinct fields, hence resulting in distinct
+resultsets.  The behaviour is undefined if called on a non-joining
+collection.
+
+=cut
+
+sub _is_distinctly_joined {
+    my $self = shift;
+    if ( $self->{'leftjoins'} ) {
+	for (values %{ $self->{'leftjoins'} } ) {
+	    return 0 unless $_->{is_distinct};
+	}
 
+	return 1;
+    }
 }
 
 # LIMIT clauses are used for restricting ourselves to subsets of the
@@ -603,7 +622,7 @@
 
 sub distinct_required {
     my $self = shift;
-    return( $self->_is_joined ? 1 : 0 );
+    return( $self->_is_joined ? !$self->_is_distinctly_joined : 0 );
 }
 
 =head2 build_select_count_query

Modified: Jifty-DBI/trunk/lib/Jifty/DBI/Handle.pm
==============================================================================
--- Jifty-DBI/trunk/lib/Jifty/DBI/Handle.pm	(original)
+++ Jifty-DBI/trunk/lib/Jifty/DBI/Handle.pm	Mon Sep  3 07:29:09 2007
@@ -820,11 +820,27 @@
         column2    => undef,
         expression => undef,
         operator   => '=',
+        is_distinct=> 0,
         @_
     );
 
     my $alias;
 
+    # If we're handed in a table2 as a Collection object, make notes
+    # about if the result of the join is still distinct for the
+    # calling collection
+    if ( $args{'table2'} &&
+         UNIVERSAL::isa($args{'table2'}, 'Jifty::DBI::Collection') ) {
+	if ($args{'operator'} eq '=' ) {
+	    my $x = $args{'table2'}->new_item->column($args{column2});
+	    if ($x->type eq 'serial' || $x->distinct) {
+		$args{'is_distinct'} = 1;
+	    }
+	}
+
+        $args{'table2'} = $args{'table2'}->table;
+    }
+
     #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
@@ -894,6 +910,7 @@
     $meta->{'depends_on'} = $args{'alias1'};
     $meta->{'entry_aggregator'} = $args{'entry_aggregator'}
         if $args{'entry_aggregator'};
+    $meta->{'is_distinct'} = $args{'is_distinct'};
 
     my $criterion = $args{'expression'} || "$args{'alias1'}.$args{'column1'}";
     $meta->{'criteria'}{ 'base_criterion' } = [{


More information about the Jifty-commit mailing list