[Catalyst-commits] r12536 - in Catalyst-Controller-DBIC-API: .
1.004/trunk 1.004/trunk/lib/Catalyst/Controller/DBIC
1.004/trunk/lib/Catalyst/Controller/DBIC/API
1.004/trunk/t/rest 1.004/trunk/t/rpc
nperez at dev.catalyst.perl.org
nperez at dev.catalyst.perl.org
Wed Jan 6 22:16:24 GMT 2010
Author: nperez
Date: 2010-01-06 22:16:24 +0000 (Wed, 06 Jan 2010)
New Revision: 12536
Modified:
Catalyst-Controller-DBIC-API/
Catalyst-Controller-DBIC-API/1.004/trunk/Changes
Catalyst-Controller-DBIC-API/1.004/trunk/lib/Catalyst/Controller/DBIC/API.pm
Catalyst-Controller-DBIC-API/1.004/trunk/lib/Catalyst/Controller/DBIC/API/Base.pm
Catalyst-Controller-DBIC-API/1.004/trunk/lib/Catalyst/Controller/DBIC/API/RequestArguments.pm
Catalyst-Controller-DBIC-API/1.004/trunk/lib/Catalyst/Controller/DBIC/API/StaticArguments.pm
Catalyst-Controller-DBIC-API/1.004/trunk/lib/Catalyst/Controller/DBIC/API/Types.pm
Catalyst-Controller-DBIC-API/1.004/trunk/t/rest/list.t
Catalyst-Controller-DBIC-API/1.004/trunk/t/rpc/list.t
Log:
r5304 at nicklaptop: nicholas | 2010-01-06 16:14:17 -0600
Implement 'as' functionality
Property changes on: Catalyst-Controller-DBIC-API
___________________________________________________________________
Name: svk:merge
- 992f488a-d630-404b-95f9-f7d0fdf28443:/local/dbic-api:5300
+ 992f488a-d630-404b-95f9-f7d0fdf28443:/local/dbic-api:5304
Modified: Catalyst-Controller-DBIC-API/1.004/trunk/Changes
===================================================================
--- Catalyst-Controller-DBIC-API/1.004/trunk/Changes 2010-01-06 15:59:41 UTC (rev 12535)
+++ Catalyst-Controller-DBIC-API/1.004/trunk/Changes 2010-01-06 22:16:24 UTC (rev 12536)
@@ -1,5 +1,8 @@
Revision history for Catalyst-Controller-DBIC-API
+1.004002
+- Implement 'as' as a complement to 'select'
+
1.004001
- Allow for more complex prefetch_allows (multiple keys in hash)
- Skip non-existant parameters in deserialization
Modified: Catalyst-Controller-DBIC-API/1.004/trunk/lib/Catalyst/Controller/DBIC/API/Base.pm
===================================================================
--- Catalyst-Controller-DBIC-API/1.004/trunk/lib/Catalyst/Controller/DBIC/API/Base.pm 2010-01-06 15:59:41 UTC (rev 12535)
+++ Catalyst-Controller-DBIC-API/1.004/trunk/lib/Catalyst/Controller/DBIC/API/Base.pm 2010-01-06 22:16:24 UTC (rev 12536)
@@ -86,6 +86,7 @@
# set request arguments
$c->req->_set_prefetch($req_params->{$self->prefetch_arg}) if exists $req_params->{$self->prefetch_arg};
$c->req->_set_select($req_params->{$self->select_arg}) if exists $req_params->{$self->select_arg};
+ $c->req->_set_as($req_params->{$self->as_arg}) if exists $req_params->{$self->as_arg};
$c->req->_set_grouped_by($req_params->{$self->grouped_by_arg}) if exists $req_params->{$self->grouped_by_arg};
$c->req->_set_ordered_by($req_params->{$self->ordered_by_arg}) if exists $req_params->{$self->ordered_by_arg};
$c->req->_set_search($req_params->{$self->search_arg}) if exists $req_params->{$self->search_arg};
@@ -169,9 +170,12 @@
$args->{select} = $req->select || ((scalar(@{$self->select})) ? $self->select : undef);
if ($args->{select}) {
# make sure all columns have an alias to avoid ambiguous issues
- $args->{select} = [map { ($_ =~ m/\./) ? $_ : "me.$_" } (ref $args->{select}) ? @{$args->{select}} : $args->{select}];
+ # but allow non strings (eg. hashrefs for db procs like 'count')
+ # to pass through unmolested
+ $args->{select} = [map { (Str->check($_) && $_ !~ m/\./) ? "me.$_" : $_ } (ref $args->{select}) ? @{$args->{select}} : $args->{select}];
}
+ $args->{as} = $req->as || ((scalar(@{$self->as})) ? $self->as : undef);
$args->{join} = $join;
if ( my $action_name = $self->setup_dbic_args_method ) {
my $format_action = $self->action_for($action_name);
Modified: Catalyst-Controller-DBIC-API/1.004/trunk/lib/Catalyst/Controller/DBIC/API/RequestArguments.pm
===================================================================
--- Catalyst-Controller-DBIC-API/1.004/trunk/lib/Catalyst/Controller/DBIC/API/RequestArguments.pm 2010-01-06 15:59:41 UTC (rev 12535)
+++ Catalyst-Controller-DBIC-API/1.004/trunk/lib/Catalyst/Controller/DBIC/API/RequestArguments.pm 2010-01-06 22:16:24 UTC (rev 12536)
@@ -222,6 +222,7 @@
is => 'ro',
writer => '_set_select',
isa => SelectColumns,
+ predicate => 'has_select',
default => sub { $p->static ? [] : undef },
traits => ['Aliased'],
alias => 'list_returns',
@@ -244,6 +245,27 @@
},
);
+ has as =>
+ (
+ is => 'ro',
+ writer => '_set_as',
+ isa => AsAliases,
+ default => sub { $p->static ? [] : undef },
+ trigger => sub
+ {
+ my ($self, $new) = @_;
+ if($self->has_select)
+ {
+ die "'as' argument count (${\scalar(@$new)}) must match 'select' argument count (${\scalar(@{$self->select})})"
+ unless @$new == @{$self->select};
+ }
+ elsif(defined $new)
+ {
+ die "'as' is only valid if 'select is also provided'";
+ }
+ }
+ );
+
has 'request_data' =>
(
is => 'ro',
Modified: Catalyst-Controller-DBIC-API/1.004/trunk/lib/Catalyst/Controller/DBIC/API/StaticArguments.pm
===================================================================
--- Catalyst-Controller-DBIC-API/1.004/trunk/lib/Catalyst/Controller/DBIC/API/StaticArguments.pm 2010-01-06 15:59:41 UTC (rev 12535)
+++ Catalyst-Controller-DBIC-API/1.004/trunk/lib/Catalyst/Controller/DBIC/API/StaticArguments.pm 2010-01-06 22:16:24 UTC (rev 12536)
@@ -37,6 +37,7 @@
has 'count_arg' => ( is => 'ro', isa => Str, default => 'list_count' );
has 'page_arg' => ( is => 'ro', isa => Str, default => 'list_page' );
has 'select_arg' => ( is => 'ro', isa => Str, default => 'list_returns' );
+has 'as_arg' => ( is => 'ro', isa => Str, default => 'as' );
has 'search_arg' => ( is => 'ro', isa => Str, default => 'search' );
has 'grouped_by_arg' => ( is => 'ro', isa => Str, default => 'list_grouped_by' );
has 'ordered_by_arg' => ( is => 'ro', isa => Str, default => 'list_ordered_by' );
Modified: Catalyst-Controller-DBIC-API/1.004/trunk/lib/Catalyst/Controller/DBIC/API/Types.pm
===================================================================
--- Catalyst-Controller-DBIC-API/1.004/trunk/lib/Catalyst/Controller/DBIC/API/Types.pm 2010-01-06 15:59:41 UTC (rev 12535)
+++ Catalyst-Controller-DBIC-API/1.004/trunk/lib/Catalyst/Controller/DBIC/API/Types.pm 2010-01-06 22:16:24 UTC (rev 12536)
@@ -4,7 +4,7 @@
use warnings;
use strict;
-use MooseX::Types -declare => [qw/OrderedBy GroupedBy Prefetch SelectColumns/];
+use MooseX::Types -declare => [qw/OrderedBy GroupedBy Prefetch SelectColumns AsAliases/];
use MooseX::Types::Moose(':all');
subtype Prefetch, as Maybe[ArrayRef[Str|HashRef]];
@@ -19,4 +19,6 @@
subtype SelectColumns, as Maybe[ArrayRef[Str|HashRef]];
coerce SelectColumns, from Str, via { [$_] };
+subtype AsAliases, as Maybe[ArrayRef[Str]];
+
1;
Modified: Catalyst-Controller-DBIC-API/1.004/trunk/lib/Catalyst/Controller/DBIC/API.pm
===================================================================
--- Catalyst-Controller-DBIC-API/1.004/trunk/lib/Catalyst/Controller/DBIC/API.pm 2010-01-06 15:59:41 UTC (rev 12535)
+++ Catalyst-Controller-DBIC-API/1.004/trunk/lib/Catalyst/Controller/DBIC/API.pm 2010-01-06 22:16:24 UTC (rev 12536)
@@ -97,7 +97,7 @@
By default, the response success status is set to a string value of "true" or "false". If this attribute is true, JSON::Any's true() and false() will be used instead. Note, this does not effect other internal processing of boolean values.
-head2 count_arg, page_arg, select_arg, search_arg, grouped_by_arg, ordered_by_arg, prefetch_arg
+head2 count_arg, page_arg, select_arg, search_arg, grouped_by_arg, ordered_by_arg, prefetch_arg, as_arg
These attributes allow customization of the component to understand requests made by clients where these argument names are not flexible and cannot conform to this components defaults.
@@ -117,6 +117,10 @@
Arguments to pass to L<DBIx::Class::ResultSet/select> when performing search for L</list>.
+=head2 as
+
+Complements arguments passed to L<DBIx::Class::ResultSet/select> when performing a search. This allows you to specify column names in the result for RDBMS functions, etc.
+
=head2 select_exposes
Columns and related columns that are okay to return in the resultset since clients can request more or less information specified than the above select argument.
@@ -272,7 +276,7 @@
=head2 list
-List level action chained from L</setup>. By default populates $c->stash->{response}->{$self->data_root} with a list of hashrefs representing each object in the class resultset. If the L</select> config param is defined then the hashes will contain only those columns, otherwise all columns in the object will be returned. Similarly L</count>, L</page>, L</grouped_by> and L</ordered_by> affect the maximum number of rows returned as well as the ordering and grouping. Note that if select, count, ordered_by or grouped_by request parameters are present then these will override the values set on the class with select becoming bound by the select_exposes attribute.
+List level action chained from L</setup>. By default populates $c->stash->{response}->{$self->data_root} with a list of hashrefs representing each object in the class resultset. If the L</select> config param is defined then the hashes will contain only those columns, otherwise all columns in the object will be returned. L</select> of course supports the function/procedure calling semantics that L<DBIx::Class::ResultSet/select>. In order to have proper column names in the result, provide arguments in L</as> (which also follows L<DBIx::Class::ResultSet/as> semantics. Similarly L</count>, L</page>, L</grouped_by> and L</ordered_by> affect the maximum number of rows returned as well as the ordering and grouping. Note that if select, count, ordered_by or grouped_by request parameters are present then these will override the values set on the class with select becoming bound by the select_exposes attribute.
If not all objects in the resultset are required then it's possible to pass conditions to the method as request parameters. You can use a JSON string as the 'search' parameter for maximum flexibility or use L</CGI::Expand> syntax. In the second case the request parameters are expanded into a structure and then $c->req->params->{search} is used as the search condition.
Modified: Catalyst-Controller-DBIC-API/1.004/trunk/t/rest/list.t
===================================================================
--- Catalyst-Controller-DBIC-API/1.004/trunk/t/rest/list.t 2010-01-06 15:59:41 UTC (rev 12535)
+++ Catalyst-Controller-DBIC-API/1.004/trunk/t/rest/list.t 2010-01-06 22:16:24 UTC (rev 12536)
@@ -10,7 +10,7 @@
use RestTest;
use DBICTest;
use URI;
-use Test::More tests => 13;
+use Test::More tests => 15;
use Test::WWW::Mechanize::Catalyst 'RestTest';
use HTTP::Request::Common;
use JSON::Syck;
@@ -82,6 +82,18 @@
}
{
+ my $uri = URI->new( $artist_list_url );
+ $uri->query_form({ 'search.cds.title' => 'Forkful of bees', 'list_returns.0.count' => '*', 'as.0' => 'count'});
+ my $req = GET( $uri, 'Accept' => 'text/x-json' );
+ $mech->request($req);
+ cmp_ok( $mech->status, '==', 200, 'search related request okay' );
+
+ my @expected_response = map { { $_->get_columns } } $schema->resultset('Artist')->search({ 'cds.title' => 'Forkful of bees' }, { select => [ {count => '*'} ], as => [ 'count' ], join => 'cds' })->all;
+ my $response = JSON::Syck::Load( $mech->content);
+ is_deeply( { list => \@expected_response, success => 'true' }, $response, 'correct data returned for count' );
+}
+
+{
my $uri = URI->new( $filtered_artist_list_url );
$uri->query_form({ 'search.artistid' => '2' });
my $req = GET( $uri, 'Accept' => 'text/x-json' );
Modified: Catalyst-Controller-DBIC-API/1.004/trunk/t/rpc/list.t
===================================================================
--- Catalyst-Controller-DBIC-API/1.004/trunk/t/rpc/list.t 2010-01-06 15:59:41 UTC (rev 12535)
+++ Catalyst-Controller-DBIC-API/1.004/trunk/t/rpc/list.t 2010-01-06 22:16:24 UTC (rev 12536)
@@ -61,6 +61,18 @@
}
{
+ my $uri = URI->new( $artist_list_url );
+ $uri->query_form({ 'search.name.LIKE' => '%waul%', 'list_returns.0.count' => '*', 'as.0' => 'count'});
+ my $req = GET( $uri, 'Accept' => 'text/x-json' );
+ $mech->request($req);
+ cmp_ok( $mech->status, '==', 200, 'attempt with basic count' );
+
+ my @expected_response = map { { $_->get_columns } } $schema->resultset('Artist')->search({ name => { LIKE => '%waul%' }}, { select => [ {count => '*'} ], as => ['count'] } )->all;
+ my $response = JSON::Syck::Load( $mech->content);
+ is_deeply( { list => \@expected_response, success => 'true' }, $response, 'correct data returned for count' );
+}
+
+{
my $uri = URI->new( $producer_list_url );
my $req = GET( $uri, 'Accept' => 'text/x-json' );
$mech->request($req);
More information about the Catalyst-commits
mailing list