[Bps-public-commit] dbix-searchbuilder branch, collection-column-selection, created. 1.63-2-gb4f6821

Thomas Sibley trs at bestpractical.com
Mon Jan 28 19:02:05 EST 2013


The branch, collection-column-selection has been created
        at  b4f6821000589bf36f4fec2073271f4a1f56e248 (commit)

- Log -----------------------------------------------------------------
commit 12a447502a69607869a721372390fd047d9b387f
Author: Thomas Sibley <trs at bestpractical.com>
Date:   Mon Jan 28 15:45:11 2013 -0800

    Add an AS parameter to Column method in collections
    
    The AS parameter lets callers set the column alias explicitly, or avoid
    a column alias entirely, rather than relying on the autogenerated
    aliases.  For collections using the Column method, this alleviates the
    need to pass around the returned aliases between pieces of code.

diff --git a/lib/DBIx/SearchBuilder.pm b/lib/DBIx/SearchBuilder.pm
index d042d27..04ad849 100755
--- a/lib/DBIx/SearchBuilder.pm
+++ b/lib/DBIx/SearchBuilder.pm
@@ -1539,6 +1539,13 @@ Otherwise, the function is inserted verbatim, with no substitution.
 
 =back
 
+=item AS
+
+The B<column> alias to use instead of the default.  The default column alias is
+either the column's name (i.e. what is passed to FIELD) if it is in this table
+(ALIAS is 'main') or an autogenerated alias.  Pass C<undef> to skip column
+aliasing entirely.
+
 =back
 
 If a FIELD is provided and it is in this table (ALIAS is 'main'), then
@@ -1559,6 +1566,9 @@ column's result:
     my $article = $articles->First;
     my $now = $article->_Value( $time_alias );
 
+To choose the column's alias yourself, pass a value for the AS parameter (see
+above).  Be careful not to conflict with existing column aliases.
+
 =cut
 
 sub Column {
@@ -1600,25 +1610,28 @@ sub Column {
         $name = 'NULL';
     }
 
-    my $column;
-    if (
-        $args{FIELD} && $args{ALIAS} eq 'main'
-        && (!$args{'TABLE'} || $args{'TABLE'} eq $self->Table )
-    ) {
-        $column = $args{FIELD};
-
-        # make sure we don't fetch columns with duplicate aliases
-        if ( $self->{columns} ) {
-            my $suffix = " AS \L$column";
-            if ( grep index($_, $suffix, -length $suffix) >= 0, @{ $self->{columns} } ) {
-                $column .= scalar @{ $self->{columns} };
+    my $column = $args{'AS'};
+
+    if (not defined $column and not exists $args{'AS'}) {
+        if (
+            $args{FIELD} && $args{ALIAS} eq 'main'
+            && (!$args{'TABLE'} || $args{'TABLE'} eq $self->Table )
+        ) {
+            $column = $args{FIELD};
+
+            # make sure we don't fetch columns with duplicate aliases
+            if ( $self->{columns} ) {
+                my $suffix = " AS \L$column";
+                if ( grep index($_, $suffix, -length $suffix) >= 0, @{ $self->{columns} } ) {
+                    $column .= scalar @{ $self->{columns} };
+                }
             }
         }
+        else {
+            $column = "col" . @{ $self->{columns} ||= [] };
+        }
     }
-    else {
-        $column = "col" . @{ $self->{columns} ||= [] };
-    }
-    push @{ $self->{columns} ||= [] }, "$name AS \L$column";
+    push @{ $self->{columns} ||= [] }, defined($column) ? "$name AS \L$column" : $name;
     return $column;
 }
 
diff --git a/t/01searches.t b/t/01searches.t
index 9fdfc45..e2e7f66 100644
--- a/t/01searches.t
+++ b/t/01searches.t
@@ -7,7 +7,7 @@ use Test::More;
 BEGIN { require "t/utils.pl" }
 our (@AvailableDrivers);
 
-use constant TESTS_PER_DRIVER => 117;
+use constant TESTS_PER_DRIVER => 125;
 
 my $total = scalar(@AvailableDrivers) * TESTS_PER_DRIVER;
 plan tests => $total;
@@ -325,6 +325,23 @@ SKIP: {
         is ( $u->_Value($id_alias), $u->id, "fetched with '?' function" );
     }
 
+    $users_obj = TestApp::Users->new( $handle );
+    $users_obj->UnLimit;
+    {
+        is( $users_obj->Column(FIELD => 'id'), "id" );
+        is( my $id_alias = $users_obj->Column(FIELD => 'id', AS => 'foo'), "foo" );
+        my $u = $users_obj->Next;
+        is( $u->_Value($id_alias), $u->id, "fetched id with custom alias" );
+    }
+
+    $users_obj = TestApp::Users->new( $handle );
+    $users_obj->UnLimit;
+    {
+        is( $users_obj->Column(FUNCTION => "main.*", AS => undef), undef );
+        my $u = $users_obj->Next;
+        ok $u->{fetched}{"\L$_"}, "fetched field $_" for keys %{$u->_ClassAccessible};
+    }
+
 	cleanup_schema( 'TestApp', $handle );
 }} # SKIP, foreach blocks
 

commit b4f6821000589bf36f4fec2073271f4a1f56e248
Author: Thomas Sibley <trs at bestpractical.com>
Date:   Mon Jan 28 15:53:19 2013 -0800

    Add an AdditionalColumn method to collections
    
    Very similar to the Column method but adds to the table's standard
    columns instead of replacing them in the select.  This lets collections
    easily grab the standard columns while also grabbing the result of a
    function, or a column from a joined table, etc.

diff --git a/lib/DBIx/SearchBuilder.pm b/lib/DBIx/SearchBuilder.pm
index 04ad849..690679f 100755
--- a/lib/DBIx/SearchBuilder.pm
+++ b/lib/DBIx/SearchBuilder.pm
@@ -1649,6 +1649,23 @@ sub Columns {
     $self->Column( FIELD => $_ ) for @_;
 }
 
+=head2 AdditionalColumn
+
+Calls L</Column>, but first ensures that this table's standard columns are
+selected as well.  Thus, each call to this method results in an additional
+column selected instead of replacing the default columns.
+
+Takes a hash of parameters which is the same as L</Column>.  Returns the result
+of calling L</Column>.
+
+=cut
+
+sub AdditionalColumn {
+    my $self = shift;
+    $self->Column( FUNCTION => "main.*", AS => undef )
+        unless grep { /^\Qmain.*\E$/ } @{$self->{columns}};
+    return $self->Column(@_);
+}
 
 =head2 Fields TABLE
 
diff --git a/t/01searches.t b/t/01searches.t
index e2e7f66..2f83a83 100644
--- a/t/01searches.t
+++ b/t/01searches.t
@@ -7,7 +7,7 @@ use Test::More;
 BEGIN { require "t/utils.pl" }
 our (@AvailableDrivers);
 
-use constant TESTS_PER_DRIVER => 125;
+use constant TESTS_PER_DRIVER => 131;
 
 my $total = scalar(@AvailableDrivers) * TESTS_PER_DRIVER;
 plan tests => $total;
@@ -342,6 +342,15 @@ SKIP: {
         ok $u->{fetched}{"\L$_"}, "fetched field $_" for keys %{$u->_ClassAccessible};
     }
 
+    $users_obj = TestApp::Users->new( $handle );
+    $users_obj->UnLimit;
+    {
+        is( my $id_alias = $users_obj->AdditionalColumn(FIELD => 'id', AS => 'foo'), "foo" );
+        my $u = $users_obj->Next;
+        is( $u->_Value($id_alias), $u->id, "fetched id with custom alias" );
+        ok $u->{fetched}{"\L$_"}, "fetched normal field $_" for keys %{$u->_ClassAccessible};
+    }
+
 	cleanup_schema( 'TestApp', $handle );
 }} # SKIP, foreach blocks
 

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



More information about the Bps-public-commit mailing list