[Rt-commit] r4014 - in Jifty-DBI/trunk: lib/Jifty/DBI t

ruz at bestpractical.com ruz at bestpractical.com
Tue Nov 1 21:50:19 EST 2005


Author: ruz
Date: Tue Nov  1 21:50:19 2005
New Revision: 4014

Modified:
   Jifty-DBI/trunk/lib/Jifty/DBI/Collection.pm
   Jifty-DBI/trunk/t/01searches.t
Log:
* order_by never supported default value for FIELD
  and should not
* (order|group)_by_cols are marked as deprecated
* added 'function' as paramhash field for the order_by
* private _set_clause method
* redo search only if new (group|order) clause is not equal to old
* documentation


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	Tue Nov  1 21:50:19 2005
@@ -2,8 +2,6 @@
 
 use strict;
 use vars qw($VERSION);
-use UNIVERSAL::can;
-
 
 =head1 NAME
 
@@ -874,39 +872,65 @@
     }
 }
 
-=head2 order_by PARAMHASH
-
-Orders the returned results by alias.column order. (by default 'main.id ASC')
+# set $self->{$type .'_clause'} to new value
+# redo_search only if new value is really new
+sub _set_clause {
+    my $self = shift;
+    my ($type, $value) = @_;
+    $type .= '_clause';
+    if( ($self->{$type} || '') ne ($value||'') ) {
+        $self->redo_search;
+    }
+    $self->{$type} = $value;
+}
 
-Takes a paramhash of alias, column and order.  
-alias defaults to main
-C<column> defaults to the primary key of the main table.  Also accepts C<function(column)> format
-order defaults to ASC(ending).  DESC(ending) is also a valid value for order_by
+=head2 order_by_cols DEPRECATED
 
+*DEPRECATED*. Use C<order_by> method.
 
 =cut
 
-sub order_by {
-    my $self = shift;
-    my %args = (@_);
-
-    $self->order_by_cols( \%args );
+sub order_by_cols {
+    require Carp;
+    Carp::cluck("order_by_cols is deprecated, use order_by method");
+    goto &order_by;
 }
 
-=head2 order_by_cols ARRAY
+=head2 order_by EMPTY|HASH|ARRAY_OF_HASHES
+
+Orders the returned results by column(s) and/or function(s) on column(s).
+
+Takes a paramhash of C<alias>, C<column> and C<order>
+or C<function> and C<order>.
+C<alias> defaults to main.
+C<order> defaults to ASC(ending), DES(cending) is also a valid value.
+C<column> and C<function> have no default values.
+
+Use C<function> field instead of C<alias> and C<column> to order by
+the function value. Note that if you want use a column as argument of
+the function then you have to build correct reference with alias
+in the C<alias.column> format.
+
+Use array of hashes to order by many columns/functions.
 
-order_by_cols takes an array of paramhashes of the form passed to
-order_by.  The result set is ordered by the items in the array.
+The results would be unordered if method called without arguments.
 
 =cut
 
-sub order_by_cols {
+sub order_by {
     my $self = shift;
+
     my @args = @_;
-    my $row;
-    my $clause;
+    unless( @args ) {
+        return $self->_set_clause( order => '' );
+    }
+
+    unless( UNIVERSAL::isa( $args[0], 'HASH' ) ) {
+        @args = { @args };
+    }
 
-    foreach $row (@args) {
+    my $clause = '';
+    foreach my $row (@args) {
 
         my %rowhash = (
             alias => 'main',
@@ -920,17 +944,14 @@
             $rowhash{'order'} = "ASC";
         }
 
-        if (    ( $rowhash{'alias'} )
-            and ( $rowhash{'column'} )
-            and ( $rowhash{'order'} ) )
-        {
-
-            if ( $rowhash{'column'} =~ /^(\w+\()(.*\))$/ ) {
+        if ( $rowhash{'function'} ) {
+            $clause .= ( $clause ? ", " : " " );
+            $clause .= $rowhash{'function'};
+            $clause .= $rowhash{'order'};
 
-                # handle 'function(column)' formatted fields
-                $rowhash{'alias'} = $1 . $rowhash{'alias'};
-                $rowhash{'column'} = $2;
-            }
+        } elsif ( ( $rowhash{'alias'} )
+            and ( $rowhash{'column'} ) )
+        {
 
             $clause .= ( $clause ? ", " : " " );
             $clause .= $rowhash{'alias'} . ".";
@@ -938,13 +959,8 @@
             $clause .= $rowhash{'order'};
         }
     }
-
-    if ($clause) {
-        $self->{'order_clause'} = "ORDER BY" . $clause;
-    } else {
-        $self->{'order_clause'} = "";
-    }
-    $self->redo_search();
+    $clause = "ORDER BY$clause" if $clause;
+    $self->_set_clause( order => $clause );
 }
 
 =head2 _order_clause
@@ -960,20 +976,50 @@
     return ( $self->{'order_clause'} );
 }
 
-=head2 group_by_cols ARRAY_OF_HASHES
+=head2 group_by_cols DEPRECATED
 
-Each hash contains the keys alias and column. alias defaults to 'main'
-if ignored.
+*DEPRECATED*. Use group_by method.
 
 =cut
 
 sub group_by_cols {
+    require Carp;
+    Carp::cluck( "group_by_cols is deprecated, use group_by method" );
+    goto &group_by;
+}
+
+=head2 group_by_cols EMPTY|HASH|ARRAY_OF_HASHES
+
+Groups the search results by column(s) and/or function(s) on column(s).
+
+Takes a paramhash of C<alias> and C<column> or C<function>.
+C<alias> defaults to main.
+C<column> and C<function> have no default values.
+
+Use C<function> field instead of C<alias> and C<column> to group by
+the function value. Note that if you want use a column as argument
+of the function then you have to build correct reference with alias
+in the C<alias.column> format.
+
+Use array of hashes to group by many columns/functions.
+
+The method is EXPERIMENTAL and subject to change.
+
+=cut
+
+sub group_by {
     my $self = shift;
+
     my @args = @_;
-    my $row;
-    my $clause;
+    unless( @args ) {
+        return $self->_set_clause( group => '' );
+    }
+    unless( UNIVERSAL::isa( $args[0], 'HASH' ) ) {
+        @args = { @args };
+    }
 
-    foreach $row (@args) {
+    my $clause = '';
+    foreach my $row (@args) {
         my %rowhash = (
             alias => 'main',
             column => undef,
@@ -993,12 +1039,8 @@
         }
     }
 
-    if ($clause) {
-        $self->{'group_clause'} = "GROUP BY" . $clause;
-    } else {
-        $self->{'group_clause'} = "";
-    }
-    $self->redo_search();
+    $clause = "GROUP BY" . $clause if $clause;
+    return $self->_set_clause( group => $clause );
 }
 
 =head2 _group_clause

Modified: Jifty-DBI/trunk/t/01searches.t
==============================================================================
--- Jifty-DBI/trunk/t/01searches.t	(original)
+++ Jifty-DBI/trunk/t/01searches.t	Tue Nov  1 21:50:19 2005
@@ -148,7 +148,7 @@
 	$users_obj->clean_slate;
 	is_deeply( $users_obj, $clean_obj, 'after clean_slate looks like new object');
 	$users_obj->unlimit;
-	$users_obj->group_by_cols({column => 'login'});
+	$users_obj->group_by(column => 'login');
 	$users_obj->order_by(column => 'login', order => 'desc');
 	$users_obj->column(column => 'login');
 	is( $users_obj->count, $count_all, "group by / order by finds right amount");


More information about the Rt-commit mailing list