[Bps-public-commit] r11308 - DBIx-SearchBuilder/trunk/SearchBuilder/Handle

ruz at bestpractical.com ruz at bestpractical.com
Mon Mar 31 17:59:34 EDT 2008


Author: ruz
Date: Mon Mar 31 17:59:34 2008
New Revision: 11308

Modified:
   DBIx-SearchBuilder/trunk/SearchBuilder/Handle/mysql.pm

Log:
* fix sorting by outer column on Pg

Modified: DBIx-SearchBuilder/trunk/SearchBuilder/Handle/mysql.pm
==============================================================================
--- DBIx-SearchBuilder/trunk/SearchBuilder/Handle/mysql.pm	(original)
+++ DBIx-SearchBuilder/trunk/SearchBuilder/Handle/mysql.pm	Mon Mar 31 17:59:34 2008
@@ -76,6 +76,50 @@
     return(undef);
 }
 
+sub DistinctQuery {
+    my $self = shift;
+    my $statementref = shift;
+    my $sb = shift;
+
+    return $self->SUPER::DistinctQuery( $statementref, $sb, @_ )
+        if $sb->_GroupClause || $sb->_OrderClause !~ /(?<!main)\./;
+
+    if ( substr($self->DatabaseVersion, 1) == 4 ) {
+        local $sb->{'group_by'} = [{FIELD => 'id'}];
+
+        my ($idx, @tmp, @specials) = (0, ());
+        foreach ( @{$sb->{'order_by'}} ) {
+            if ( !exists $_->{'ALIAS'} || ($_->{'ALIAS'}||'') eq "main" ) {
+                push @tmp, $_; next;
+            }
+
+            push @specials,
+                ((($_->{'ORDER'}||'') =~ /^des/i)?'MAX':'MIN')
+                ."(". $_->{'ALIAS'} .".". $_->{'FIELD'} .")"
+                ." __special_sort_$idx";
+            push @tmp, { ALIAS => '', FIELD => "__special_sort_$idx", ORDER => $_->{'ORDER'} };
+            $idx++;
+        }
+
+        local $sb->{'order_by'} = \@tmp;
+        $$statementref = "SELECT ". join( ", ", 'main.*', @specials ) ." FROM $$statementref";
+        $$statementref .= $sb->_GroupClause;
+        $$statementref .= $sb->_OrderClause;
+    } else {
+        local $sb->{'group_by'} = [{FIELD => 'id'}];
+        local $sb->{'order_by'} = [
+            map {
+                ($_->{'ALIAS'}||'') ne "main"
+                ? { %{$_}, FIELD => ((($_->{'ORDER'}||'') =~ /^des/i)?'MAX':'MIN') ."(".$_->{FIELD}.")" }
+                : $_
+            }
+            @{$sb->{'order_by'}}
+        ];
+        $$statementref = "SELECT main.* FROM $$statementref";
+        $$statementref .= $sb->_GroupClause;
+        $$statementref .= $sb->_OrderClause;
+    }
+}
 
 1;
 



More information about the Bps-public-commit mailing list