[Bps-public-commit] dbix-searchbuilder branch, column-method-cleanup, created. 1.59-16-g69891fd

Ruslan Zakirov ruz at bestpractical.com
Fri May 20 18:06:10 EDT 2011


The branch, column-method-cleanup has been created
        at  69891fddc9d0be1a5e65056efa0776c477a8b602 (commit)

- Log -----------------------------------------------------------------
commit afab7b1a93a99cbcac4b76eedfda86b4a6c0ca09
Author: Ruslan Zakirov <ruz at bestpractical.com>
Date:   Sat May 21 00:21:25 2011 +0400

    move code closer to the place we need it
    
    for futher changes

diff --git a/lib/DBIx/SearchBuilder.pm b/lib/DBIx/SearchBuilder.pm
index b7e01e0..7cceb1c 100755
--- a/lib/DBIx/SearchBuilder.pm
+++ b/lib/DBIx/SearchBuilder.pm
@@ -1519,16 +1519,6 @@ sub Column {
                FUNCTION => undef,
                @_);
 
-    my $table = $args{TABLE} || do {
-        if ( my $alias = $args{ALIAS} ) {
-            $alias =~ s/_\d+$//;
-            $alias;
-        }
-        else {
-            $self->Table;
-        }
-    };
-
     my $name = ( $args{ALIAS} || 'main' ) . '.' . $args{FIELD};
     if ( my $func = $args{FUNCTION} ) {
         if ( $func =~ /^DISTINCT\s*COUNT$/i ) {
@@ -1547,6 +1537,16 @@ sub Column {
         
     }
 
+    my $table = $args{TABLE} || do {
+        if ( my $alias = $args{ALIAS} ) {
+            $alias =~ s/_\d+$//;
+            $alias;
+        }
+        else {
+            $self->Table;
+        }
+    };
+
     my $column = "col" . @{ $self->{columns} ||= [] };
     $column = $args{FIELD} if $table eq $self->Table and !$args{ALIAS};
     push @{ $self->{columns} }, "$name AS \L$column";

commit e5b690e3ca573babcad00f4db8848cab407c1e8b
Author: Ruslan Zakirov <ruz at bestpractical.com>
Date:   Sat May 21 00:37:02 2011 +0400

    drop code that never affected anything
    
    $table is used only once and only when $ARGS{'ALIAS'} is false, so
    the following block can be written as $ARGS{'TABLE'} || $self->Table.
    
        my $table = $args{TABLE} || do {
            if ( my $alias = $args{ALIAS} ) {
                $alias =~ s/_\d+$//;
                $alias;
            }
            else {
                $self->Table;
            }
        };

diff --git a/lib/DBIx/SearchBuilder.pm b/lib/DBIx/SearchBuilder.pm
index 7cceb1c..c3f9a49 100755
--- a/lib/DBIx/SearchBuilder.pm
+++ b/lib/DBIx/SearchBuilder.pm
@@ -1537,18 +1537,8 @@ sub Column {
         
     }
 
-    my $table = $args{TABLE} || do {
-        if ( my $alias = $args{ALIAS} ) {
-            $alias =~ s/_\d+$//;
-            $alias;
-        }
-        else {
-            $self->Table;
-        }
-    };
-
     my $column = "col" . @{ $self->{columns} ||= [] };
-    $column = $args{FIELD} if $table eq $self->Table and !$args{ALIAS};
+    $column = $args{FIELD} if ($args{'TABLE'}||$self->Table) eq $self->Table && !$args{ALIAS};
     push @{ $self->{columns} }, "$name AS \L$column";
     return $column;
 }

commit 53f8417d3d1b68c9413eab9529b6b5b3580af741
Author: Ruslan Zakirov <ruz at bestpractical.com>
Date:   Sat May 21 00:48:14 2011 +0400

    make sure special naming works in more cases
    
    Whole idea was to name function in the result set
    as field it's based on, but only if function applied
    on field from this table.
    
    This didn't work when alias is provided in arguments,
    but is 'main' actually.

diff --git a/lib/DBIx/SearchBuilder.pm b/lib/DBIx/SearchBuilder.pm
index c3f9a49..66d18b3 100755
--- a/lib/DBIx/SearchBuilder.pm
+++ b/lib/DBIx/SearchBuilder.pm
@@ -1519,7 +1519,9 @@ sub Column {
                FUNCTION => undef,
                @_);
 
-    my $name = ( $args{ALIAS} || 'main' ) . '.' . $args{FIELD};
+    $args{'ALIAS'} ||= 'main';
+
+    my $name = $args{ALIAS} . '.' . $args{FIELD};
     if ( my $func = $args{FUNCTION} ) {
         if ( $func =~ /^DISTINCT\s*COUNT$/i ) {
             $name = "COUNT(DISTINCT $name)";
@@ -1538,7 +1540,7 @@ sub Column {
     }
 
     my $column = "col" . @{ $self->{columns} ||= [] };
-    $column = $args{FIELD} if ($args{'TABLE'}||$self->Table) eq $self->Table && !$args{ALIAS};
+    $column = $args{FIELD} if ($args{'TABLE'}||$self->Table) eq $self->Table && $args{ALIAS} eq 'main';
     push @{ $self->{columns} }, "$name AS \L$column";
     return $column;
 }

commit 32a174692fadf03e1aeb26581557717c5a21f52d
Author: Ruslan Zakirov <ruz at bestpractical.com>
Date:   Sat May 21 01:26:27 2011 +0400

    make it possible to provide function w/o field

diff --git a/lib/DBIx/SearchBuilder.pm b/lib/DBIx/SearchBuilder.pm
index 66d18b3..20f802e 100755
--- a/lib/DBIx/SearchBuilder.pm
+++ b/lib/DBIx/SearchBuilder.pm
@@ -1521,8 +1521,11 @@ sub Column {
 
     $args{'ALIAS'} ||= 'main';
 
-    my $name = $args{ALIAS} . '.' . $args{FIELD};
-    if ( my $func = $args{FUNCTION} ) {
+    my $name;
+    if ( $args{FIELD} && $args{FUNCTION} ) {
+        $name = $args{'ALIAS'} .'.'. $args{'FIELD'};
+
+        my $func = $args{FUNCTION};
         if ( $func =~ /^DISTINCT\s*COUNT$/i ) {
             $name = "COUNT(DISTINCT $name)";
         }
@@ -1536,7 +1539,12 @@ sub Column {
         } else {
             $name = $func;
         }
-        
+    }
+    elsif ( $args{FUNCTION} ) {
+        $name = $args{FUNCTION};
+    }
+    else {
+        $name = 'NULL';
     }
 
     my $column = "col" . @{ $self->{columns} ||= [] };

commit c8414546f9f4d23a59a72aaa04088230f178f543
Author: Ruslan Zakirov <ruz at bestpractical.com>
Date:   Sat May 21 01:35:53 2011 +0400

    make sure we don't get 'xxxx AS ,' in select list
    
    we want FIELD to be empty in some cases

diff --git a/lib/DBIx/SearchBuilder.pm b/lib/DBIx/SearchBuilder.pm
index 20f802e..37d8535 100755
--- a/lib/DBIx/SearchBuilder.pm
+++ b/lib/DBIx/SearchBuilder.pm
@@ -1547,9 +1547,17 @@ sub Column {
         $name = 'NULL';
     }
 
-    my $column = "col" . @{ $self->{columns} ||= [] };
-    $column = $args{FIELD} if ($args{'TABLE'}||$self->Table) eq $self->Table && $args{ALIAS} eq 'main';
-    push @{ $self->{columns} }, "$name AS \L$column";
+    my $column;
+    if (
+        $args{FIELD} && $args{ALIAS} eq 'main'
+        && (!$args{'TABLE'} || $args{'TABLE'} eq $self->Table )
+    ) {
+        $column = $args{FIELD};
+    }
+    else {
+        $column = "col" . @{ $self->{columns} ||= [] };
+    }
+    push @{ $self->{columns} ||= [] }, "$name AS \L$column";
     return $column;
 }
 

commit 69891fddc9d0be1a5e65056efa0776c477a8b602
Author: Ruslan Zakirov <ruz at bestpractical.com>
Date:   Sat May 21 02:05:37 2011 +0400

    docs for SB->Column method

diff --git a/lib/DBIx/SearchBuilder.pm b/lib/DBIx/SearchBuilder.pm
index 37d8535..15aac6a 100755
--- a/lib/DBIx/SearchBuilder.pm
+++ b/lib/DBIx/SearchBuilder.pm
@@ -1501,13 +1501,61 @@ sub IsLast {
 }
 
 
-=head2 Column { FIELD => undef } 
+=head2 Column
 
-Specify that we want to load the column  FIELD. 
+Call to specify which columns should be loaded from
+the table. Each calls adds one column to the set.
+Takes a hash with the following named arguments:
 
-Other parameters are TABLE ALIAS AND FUNCTION.
+=over 4
+
+=item FIELD
+
+Optional column name to fetch or apply function
+to.
+
+=item ALIAS
+
+Alias of a table the field is in. main by default.
+
+=item FUNCTION
+
+An SQL function that should be selected as result.
+If FIELD is provided then it's inserted into
+function according to the following rules:
+
+=over 4
+
+=item * if text of the function contains '?' (a question mark),
+then it's replaced with qualified FIELD
+
+=item * if text of the function has no '(' (opening parenthesis)
+then qualified FIELD appended in parentheses to the text
+
+=back
+
+Only one rule applies.
+
+=back
+
+If FIELD is provided and it's this table (ALIAS
+is 'main') then column named as FIELD and can be
+accessed by accessors, for example:
+
+    $articles->Column(FIELD => 'id');
+    $articles->Column(FIELD => 'Subject', FUNCTION => 'SUBSTR(?, 1, 20)');
+    my $article = $articles->First;
+    my $aid = $article->id;
+    my $subject_prefix = $article->Subject;
+
+Returns alias for the column. If FIELD is not provided or
+it's not in this table or it's used more than once then
+use returned alias and L<DBIx::SearchBuilder::Record/_Value>
+method to retrieve result.
 
-Autrijus and Ruslan owe docs.
+    my $time_alias = $articles->Column(FUNCTION => 'NOW()');
+    my $article = $articles->First;
+    my $now = $article->_Value( $time_alias );
 
 =cut
 

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



More information about the Bps-public-commit mailing list