[Jifty-commit] jifty-dbi branch, master, updated. 0.64-8-g57079cd

Jifty commits jifty-commit at lists.jifty.org
Fri Feb 4 16:52:51 EST 2011


The branch, master has been updated
       via  57079cda5d620a7592e423ac369fc507ed8da495 (commit)
      from  8671029e73ff61565fcf7372be010520cdcf32dd (commit)

Summary of changes:
 lib/Jifty/DBI/Collection.pm |   12 +++++++++---
 t/13collection.t            |   23 ++++++++++++++++++++++-
 2 files changed, 31 insertions(+), 4 deletions(-)

- Log -----------------------------------------------------------------
commit 57079cda5d620a7592e423ac369fc507ed8da495
Author: Thomas Sibley <trs at bestpractical.com>
Date:   Fri Feb 4 16:51:33 2011 -0500

    Provide a sane way to wrap a function around an aliased column in order_by

diff --git a/lib/Jifty/DBI/Collection.pm b/lib/Jifty/DBI/Collection.pm
index 2f9df85..6f71ed9 100755
--- a/lib/Jifty/DBI/Collection.pm
+++ b/lib/Jifty/DBI/Collection.pm
@@ -1568,6 +1568,10 @@ 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.
 
+If you specify C<function> and C<column>, the column (and C<alias>) will be
+wrapped in the function.  This is useful for simple functions like C<min> or
+C<lower>.
+
 Use array of hashes to order by many columns/functions.
 
 Calling this I<sets> the ordering, it doesn't refine it. If you want to keep
@@ -1648,7 +1652,7 @@ sub _order_clause {
             $rowhash{'order'} = "ASC";
         }
 
-        if ( $rowhash{'function'} ) {
+        if ( $rowhash{'function'} and not defined $rowhash{'column'} ) {
             $clause .= ( $clause ? ", " : " " );
             $clause .= $rowhash{'function'} . ' ';
             $clause .= $rowhash{'order'};
@@ -1658,9 +1662,11 @@ sub _order_clause {
         {
 
             $clause .= ( $clause ? ", " : " " );
+            $clause .= $rowhash{'function'} . "(" if $rowhash{'function'};
             $clause .= $rowhash{'alias'} . "." if $rowhash{'alias'};
-            $clause .= $rowhash{'column'} . " ";
-            $clause .= $rowhash{'order'};
+            $clause .= $rowhash{'column'};
+            $clause .= ")" if $rowhash{'function'};
+            $clause .= " " . $rowhash{'order'};
         }
     }
     $clause = " ORDER BY$clause " if $clause;
diff --git a/t/13collection.t b/t/13collection.t
index 9815c27..3b73762 100644
--- a/t/13collection.t
+++ b/t/13collection.t
@@ -3,7 +3,7 @@
 use strict;
 use warnings;
 
-use Test::More tests => 8;
+use Test::More tests => 10;
 
 my $package;
 BEGIN { 
@@ -123,5 +123,26 @@ is $obj->_order_clause,
    ' ORDER BY ab.msg_id DESC, main.yaks ASC ',
    "add_order_by works when passing a list-as-hash directly";
 
+# test specifying just function
+$obj->order_by(
+    function => 'min(foo)',
+);
+
+is $obj->_order_clause,
+   ' ORDER BY min(foo) ASC ',
+   "order_by function and column works";
+
+# test specifying function and column
+$obj->order_by(
+    function => 'lower',
+    column => 'name',
+    order => 'DESC',
+);
+
+is $obj->_order_clause,
+   ' ORDER BY lower(main.name) DESC ',
+   "order_by function and column works";
+
 $obj->clear_order_by;
 is($obj->_order_clause, '', "clear_order_by works");
+

-----------------------------------------------------------------------


More information about the Jifty-commit mailing list