Changeset 564

Show
Ignore:
Timestamp:
02/05/09 00:27:19 (10 months ago)
Author:
athomason
Message:

Avoid joining to the same table multiple times.

Files:
1 modified

Legend:

Unmodified
Added
Removed
  • trunk/lib/Data/ObjectDriver/SQL.pm

    r552 r564  
    7272 
    7373    ## Add any explicit JOIN statements before the non-joined tables. 
     74    my %joined; 
     75    my @from = @{ $stmt->from || [] }; 
    7476    if ($stmt->joins && @{ $stmt->joins }) { 
    7577        my $initial_table_written = 0; 
     
    7880            $table = $stmt->_add_index_hint($table); ## index hint handling 
    7981            $sql .= $table unless $initial_table_written++; 
     82            $joined{$table}++; 
    8083            for my $join (@{ $j->{joins} }) { 
    8184                $sql .= ' ' . 
     
    8487            } 
    8588        } 
    86         $sql .= ', ' if @{ $stmt->from }; 
    87     } 
    88  
    89     if ($stmt->from && @{ $stmt->from }) { 
    90         $sql .= join ', ', map { $stmt->_add_index_hint($_) } @{ $stmt->from }; 
     89        @from = grep { ! $joined{ $_ } } @from; 
     90        $sql .= ', ' if @from; 
     91    } 
     92 
     93    if (@from) { 
     94        $sql .= join ', ', map { $stmt->_add_index_hint($_) } @from; 
    9195    } 
    9296