[rt-devel] Re: [rt-users] DBIx-SearchBuilder - sort order

Grant DeGraw grant at wolfram.com
Tue Mar 11 21:18:28 EST 2003


Changed as requested, diff to 0.8 attached.


-Grant


Jesse Vincent wrote:
> 
> 
> On Tue, Mar 11, 2003 at 07:39:38PM -0600, Grant DeGraw wrote:
> 
>>Diff to SearchBuilder 0.8 attached.
> 
> 
> Thanks very much. would you be up to changing it so that OrderBy just
> calls MultiOrderBy?
> 
> 
-------------- next part --------------
--- /usr/local/src/perl/DBIx-SearchBuilder-0.80/SearchBuilder.pm	Sat Mar  8 02:42:25 2003
+++ SearchBuilder.pm	Tue Mar 11 20:14:16 2003
@@ -862,24 +862,51 @@
 
 sub OrderBy {
     my $self = shift;
-    my %args = ( ALIAS => 'main',
-                 FIELD => undef,
-                 ORDER => 'ASC',
-                 @_ );
-    $self->{'order_by_alias'} = $args{'ALIAS'};
-    $self->{'order_by_field'} = $args{'FIELD'};
-    if ( $args{'ORDER'} =~ /^des/i ) {
-        $self->{'order_by_order'} = "DESC";
+    my %args = ( @_ );
+
+    $self->MultiOrderBy( \%args );
+}
+
+sub MultiOrderBy {
+    my $self = shift;
+    my @args = @_;
+    my $row;
+    my $clause;
+
+    foreach $row ( @args ) {
+        my %rowhash = ( ALIAS => 'main',
+			FIELD => undef,
+			ORDER => 'ASC',
+			%$row
+		      );
+        if ($rowhash{'ORDER'} =~ /^des/i) {
+	    $rowhash{'ORDER'} = "DESC";
+        }
+        else {
+	    $rowhash{'ORDER'} = "ASC";
+        }
+
+        if ( ($rowhash{'ALIAS'}) and
+	     ($rowhash{'FIELD'}) and
+             ($rowhash{'ORDER'}) ) {
+
+            $clause .= ($clause ? ", " : " ");
+            $clause .= $rowhash{'ALIAS'} . ".";
+            $clause .= $rowhash{'FIELD'} . " ";
+            $clause .= $rowhash{'ORDER'};
+        }
+    }
+
+    if ($clause) {
+	$self->{'order_clause'} = "ORDER BY" . $clause;
     }
     else {
-        $self->{'order_by_order'} = "ASC";
+	$self->{'order_clause'} = "";
     }
-
     $self->RedoSearch();
-
 }
 
-# }}}
+# }}} 
 
 # {{{ sub _OrderClause
 
@@ -892,26 +919,10 @@
 sub _OrderClause {
     my $self = shift;
 
-    my $clause = "";
-
-    #If we don't have an order defined, set the defaults
-    unless ( defined $self->{'order_by_field'} ) {
-        $self->OrderBy();
+    unless ( defined $self->{'order_clause'} ) {
+	return "";
     }
-
-    if (     ( $self->{'order_by_field'} )
-         and ( $self->{'order_by_alias'} )
-         and ( $self->{'order_by_order'} ) ) {
-
-        $clause = "ORDER BY ";
-        $clause .= $self->{'order_by_alias'} . "."
-          if ( $self->{'order_by_alias'} );
-        $clause .= $self->{'order_by_field'};
-        $clause .= " " . $self->{'order_by_order'}
-          if ( $self->{'order_by_order'} );
-    }
-
-    return ($clause);
+    return ($self->{'order_clause'});
 }
 
 # }}}


More information about the Rt-devel mailing list