[Bps-public-commit] dbix-searchbuilder branch, functions-in-searches, created. 1.59-48-gbf59489

Ruslan Zakirov ruz at bestpractical.com
Thu Jun 2 20:16:04 EDT 2011


The branch, functions-in-searches has been created
        at  bf5948927033e4059d0b7e6cb62d79faca1eda62 (commit)

- Log -----------------------------------------------------------------
commit 4166aeba1a285916eeffbbbedd0fa38fca6ddc4d
Author: Ruslan Zakirov <ruz at bestpractical.com>
Date:   Thu Jun 2 22:02:49 2011 +0400

    process FIELD without FUNCTION in CombineFunctionWithField
    
    if FIELD is not set then return FUNCTION or undef
    
    if FUNCTION is not set then return ALIAS.FIELD
    
    otherwise process like before

diff --git a/lib/DBIx/SearchBuilder.pm b/lib/DBIx/SearchBuilder.pm
index c76e0ea..9bc0d56 100755
--- a/lib/DBIx/SearchBuilder.pm
+++ b/lib/DBIx/SearchBuilder.pm
@@ -1594,11 +1594,14 @@ sub CombineFunctionWithField {
         @_
     );
 
-    my $func = $args{'FUNCTION'};
-    return $func unless $args{'FIELD'};
+    unless ( $args{'FIELD'} ) {
+        return $args{'FUNCTION'} || undef;
+    }
 
     my $field = ($args{'ALIAS'} || 'main') .'.'. $args{'FIELD'};
+    return $field unless $args{'FUNCTION'};
 
+    my $func = $args{'FUNCTION'};
     if ( $func =~ /^DISTINCT\s*COUNT$/i ) {
         $func = "COUNT(DISTINCT $field)";
     }

commit 6cfbf5586149f7122b1b059c9bb1fcc7a3398f07
Author: Ruslan Zakirov <ruz at bestpractical.com>
Date:   Thu Jun 2 22:21:39 2011 +0400

    use new CombineFunctionWithField, shrink code

diff --git a/lib/DBIx/SearchBuilder.pm b/lib/DBIx/SearchBuilder.pm
index 9bc0d56..8000b22 100755
--- a/lib/DBIx/SearchBuilder.pm
+++ b/lib/DBIx/SearchBuilder.pm
@@ -1151,14 +1151,11 @@ sub _GroupClause {
 
     my $clause = '';
     foreach my $row ( @{$self->{'group_by'}} ) {
-        if ( $row->{'FUNCTION'} ) {
-            $clause .= ', ' if $clause;
-            $clause .= $self->CombineFunctionWithField( %$row );
-        }
-        elsif ( $row->{'FIELD'} ) {
-            $clause .= ', ' if $clause;
-            $clause .= ($row->{'ALIAS'}||'main') .'.'. $row->{'FIELD'};
-        }
+        my $part = $self->CombineFunctionWithField( %$row )
+            or next;
+
+        $clause .= ', ' if $clause;
+        $clause .= $part;
     }
 
     return '' unless $clause;
@@ -1560,16 +1557,7 @@ sub Column {
 
     $args{'ALIAS'} ||= 'main';
 
-    my $name;
-    if ( $args{FUNCTION} ) {
-        $name = $self->CombineFunctionWithField( %args );
-    }
-    elsif ( $args{FIELD} ) {
-        $name = $args{'ALIAS'} .'.'. $args{'FIELD'};
-    }
-    else {
-        $name = 'NULL';
-    }
+    my $name = $self->CombineFunctionWithField( %args ) || 'NULL';
 
     my $column;
     if (

commit db29e084613b5daa27e0867cc785dec0c3252500
Author: Ruslan Zakirov <ruz at bestpractical.com>
Date:   Thu Jun 2 23:05:12 2011 +0400

    change how we use CombineFunctionWithField in Limit
    
    apply it unconditionaly as now it handles FIELD just fine
    
    apply it earlier as CaseInsensetivity relate code can wrap
    field into a function

diff --git a/lib/DBIx/SearchBuilder.pm b/lib/DBIx/SearchBuilder.pm
index 8000b22..fffe9e7 100755
--- a/lib/DBIx/SearchBuilder.pm
+++ b/lib/DBIx/SearchBuilder.pm
@@ -916,8 +916,7 @@ sub _GenericRestriction {
     # Set this to the name of the field and the alias, unless we've been
     # handed a subclause name
 
-    my $QualifiedField = $args{'ALIAS'} . "." . $args{'FIELD'};
-    my $ClauseId = $args{'SUBCLAUSE'} || $QualifiedField;
+    my $ClauseId = $args{'SUBCLAUSE'} || ($args{'ALIAS'} . "." . $args{'FIELD'});
 
     # If we're trying to get a leftjoin restriction, lets set
     # $restriction to point htere. otherwise, lets construct normally
@@ -934,6 +933,8 @@ sub _GenericRestriction {
         $restriction = $self->{'restrictions'}{ $ClauseId } ||= [];
     }
 
+    my $QualifiedField = $self->CombineFunctionWithField( %args );
+
     # If it's a new value or we're overwriting this sort of restriction,
 
     if ( $self->_Handle->CaseSensitive && defined $args{'VALUE'} && $args{'VALUE'} ne ''  && $args{'VALUE'} ne "''" && ($args{'OPERATOR'} !~/IS/ && $args{'VALUE'} !~ /^null$/i)) {
@@ -946,10 +947,6 @@ sub _GenericRestriction {
 
     }
 
-    if ( $args{'FUNCTION'} ) {
-        $QualifiedField = $self->CombineFunctionWithField( %args );
-    }
-
     my $clause = {
         field => $QualifiedField,
         op => $args{'OPERATOR'},

commit d51eb840495c708d8b35cf93590279fc4f264734
Author: Ruslan Zakirov <ruz at bestpractical.com>
Date:   Fri Jun 3 03:16:29 2011 +0400

    don't turn NULL into NULL(ALIAS.FIELD)

diff --git a/lib/DBIx/SearchBuilder.pm b/lib/DBIx/SearchBuilder.pm
index fffe9e7..f8a3d82 100755
--- a/lib/DBIx/SearchBuilder.pm
+++ b/lib/DBIx/SearchBuilder.pm
@@ -1597,7 +1597,7 @@ sub CombineFunctionWithField {
     }
 
     # If we want to call a simple function on the column
-    elsif ( $func !~ /\(/ )  {
+    elsif ( $func !~ /\(/ && lc($func) ne 'null' )  {
         $func = "\U$func\E($field)";
     }
 

commit 6ae682405139b9d7e9da266b898ca1604b2bd98a
Author: Ruslan Zakirov <ruz at bestpractical.com>
Date:   Fri Jun 3 03:43:29 2011 +0400

    documentation for CombineFunctionWithField

diff --git a/lib/DBIx/SearchBuilder.pm b/lib/DBIx/SearchBuilder.pm
index f8a3d82..b62bc6c 100755
--- a/lib/DBIx/SearchBuilder.pm
+++ b/lib/DBIx/SearchBuilder.pm
@@ -1570,6 +1570,58 @@ sub Column {
     return $column;
 }
 
+=head2 CombineFunctionWithField
+
+Takes a hash with three optional arguments: FUNCTION, FIELD and ALIAS.
+
+Returns SQL with all three arguments combined according to the following
+rules.
+
+=over 4
+
+=item * FUNCTION or undef returned when FIELD is not provided
+
+=item * 'main' ALIAS is used if not provided
+
+=item * ALIAS.FIELD returned when FUNCTION is not provided
+
+=item * NULL returned if FUNCTION is 'NULL'
+
+=item * If FUNCTION contains '?' (question marks) then they are
+replaced with ALIAS.FIELD and result returned.
+
+=item * If FUNCTION has no '(' (opening parenthesis) then
+ALIAS.FIELD is appended in parentheses and returned.
+
+=back
+
+Examples:
+
+    ()
+    undef
+
+    (FUNCTION => 'FOO')
+    'FOO'
+
+    (FIELD => 'foo')
+    'main.foo'
+
+    (ALIAS => 'bar', FIELD => 'foo')
+    'bar.foo'
+
+    (FUNCTION => 'FOO(?, ?)', FIELD => 'bar')
+    'FOO(main.bar, main.bar)'
+
+    (FUNCTION => 'FOO', FIELD => 'bar')
+    'FOO(main.bar)'
+
+    (FUNCTION => 'NULL', FIELD => 'bar')
+    'NULL'
+
+=cut
+
+
+
 sub CombineFunctionWithField {
     my $self = shift;
     my %args = (

commit bf5948927033e4059d0b7e6cb62d79faca1eda62
Author: Ruslan Zakirov <ruz at bestpractical.com>
Date:   Fri Jun 3 03:57:50 2011 +0400

    docs for subs using CombineFunctionWithField

diff --git a/lib/DBIx/SearchBuilder.pm b/lib/DBIx/SearchBuilder.pm
index b62bc6c..74d00e9 100755
--- a/lib/DBIx/SearchBuilder.pm
+++ b/lib/DBIx/SearchBuilder.pm
@@ -726,6 +726,11 @@ in the expression. )
 
 Column to be checked against.
 
+=item FUNCTION
+
+Function that should be checked against or applied to the FIELD before
+check. See L</CombineFunctionWithField> for rules.
+
 =item VALUE
 
 Should always be set and will always be quoted. 
@@ -1124,7 +1129,8 @@ sub _OrderClause {
 
 =head2 GroupByCols ARRAY_OF_HASHES
 
-Each hash contains the keys ALIAS and FIELD. ALIAS defaults to 'main' if ignored.
+Each hash contains the keys FIELD, FUNCTION and ALIAS. Hash
+combined into SQL with L</CombineFunctionWithField>.
 
 =cut
 
@@ -1491,8 +1497,7 @@ arguments:
 
 =item FIELD
 
-Column name to fetch or apply function to.  This can be omitted if a
-FUNCTION is given which is not a function of a field.
+Column name to fetch or apply function to.
 
 =item ALIAS
 
@@ -1500,29 +1505,11 @@ Alias of a table the field is in; defaults to C<main>
 
 =item FUNCTION
 
-A SQL function that should be selected as the result.  If a FIELD is
-provided, then it is inserted into the function according to the
-following rules:
-
-=over 4
-
-=item *
-
-If the text of the function contains a '?' (question mark), then it is
-replaced with qualified FIELD.
-
-=item *
-
-If the text of the function has no '(' (opening parenthesis), then the
-qualified FIELD is appended in parentheses to the text.
-
-=item *
-
-Otherwise, the function is inserted verbatim, with no substitution.
+A SQL function that should be selected instead of FIELD or applied to it.
 
 =back
 
-=back
+Above arguments combined according to L</CombineFunctionWithField>.
 
 If a FIELD is provided and it is in this table (ALIAS is 'main'), then
 the column named FIELD and can be accessed as usual by accessors:

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



More information about the Bps-public-commit mailing list