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

Grant DeGraw grant at wolfram.com
Tue Mar 11 20:39:38 EST 2003


Diff to SearchBuilder 0.8 attached.

OrderBy behaves as in the standard release.
MultiOrderBy behaves the same as OrderBy but on multiple fields, e.g.:

MultiOrderBy( { FIELD => 'Subject' },
               { FIELD => 'Owner', ORDER => 'DESC'} );


-Grant


Jesse Vincent wrote:
> 
> 
> 
> On Tue, Mar 04, 2003 at 10:21:27AM +0300, Sergey Gurov wrote:
> 
>>Just to clarify. I wonder is there any way in DBIx-SearchBuilder to
>>order query results by more than one field?
>>As I understand it is Jessy's creature so may be it makes sense to add
>>this feature if it is missed?
> 
> 
> I wouldn't object to a patch to DBIx::SearchBuilder to do just that.
> 
> 
>>Thanks,
>>Sergey.
-------------- next part --------------
--- /usr/local/src/perl/DBIx-SearchBuilder-0.80/SearchBuilder.pm	Sat Mar  8 02:42:25 2003
+++ SearchBuilder.pm	Tue Mar 11 17:35:33 2003
@@ -863,23 +863,70 @@
 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";
+		 FIELD => undef,
+		 ORDER => 'ASC',
+		 @_
+	       );
+    if ($args{'ORDER'} =~ /^des/i) {
+	$args{'ORDER'} = "DESC";
     }
     else {
-        $self->{'order_by_order'} = "ASC";
+	$args{'ORDER'} = "ASC";
     }
 
+    $self->{'order_clause'} = "";
+    if ( ($args{'ALIAS'}) and
+	 ($args{'FIELD'}) and
+	 ($args{'ORDER'}) ) {
+
+        $self->{'order_clause'} = "ORDER BY ";
+        $self->{'order_clause'} .= $args{'ALIAS'} . ".";
+        $self->{'order_clause'} .= $args{'FIELD'} . " ";
+        $self->{'order_clause'} .= $args{'ORDER'};
+    }
     $self->RedoSearch();
+}
+
+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_clause'} = "";
+    }
+    $self->RedoSearch();
 }
 
-# }}}
+# }}} 
 
 # {{{ sub _OrderClause
 
@@ -892,26 +939,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