X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?p=catagits%2FCatalyst-Controller-DBIC-API.git;a=blobdiff_plain;f=lib%2FCatalyst%2FController%2FDBIC%2FAPI%2FRequestArguments.pm;h=a6939f2c09e89aaee5f6fb9cc844495a3eb5b07b;hp=54ec8ebf30797da2f94a54c3b73de1aaa0217fa5;hb=28098e5de1724ed64d9908f593ec60e3ab3ebdbb;hpb=fa2501f0e73eb2e57f012f53b43d8a567e9e1c41 diff --git a/lib/Catalyst/Controller/DBIC/API/RequestArguments.pm b/lib/Catalyst/Controller/DBIC/API/RequestArguments.pm index 54ec8eb..a6939f2 100644 --- a/lib/Catalyst/Controller/DBIC/API/RequestArguments.pm +++ b/lib/Catalyst/Controller/DBIC/API/RequestArguments.pm @@ -6,53 +6,46 @@ use Catalyst::Controller::DBIC::API::Types(':all'); use MooseX::Types::Moose(':all'); use Scalar::Util('reftype'); use Data::Dumper; +use Catalyst::Controller::DBIC::API::Validator; use namespace::autoclean; use Catalyst::Controller::DBIC::API::JoinBuilder; - =attribute_private search_validator A Catalyst::Controller::DBIC::API::Validator instance used solely to validate search parameters =cut -with 'MooseX::Role::BuildInstanceOf' => -{ - 'target' => 'Catalyst::Controller::DBIC::API::Validator', - 'prefix' => 'search_validator', -}; - =attribute_private select_validator A Catalyst::Controller::DBIC::API::Validator instance used solely to validate select parameters =cut -with 'MooseX::Role::BuildInstanceOf' => -{ - 'target' => 'Catalyst::Controller::DBIC::API::Validator', - 'prefix' => 'select_validator', -}; - =attribute_private prefetch_validator A Catalyst::Controller::DBIC::API::Validator instance used solely to validate prefetch parameters =cut -with 'MooseX::Role::BuildInstanceOf' => -{ - 'target' => 'Catalyst::Controller::DBIC::API::Validator', - 'prefix' => 'prefetch_validator', -}; +has [qw( search_validator select_validator prefetch_validator )] => ( + is => 'ro', + isa => 'Catalyst::Controller::DBIC::API::Validator', + lazy => 1, + builder => '_build_validator', +); + +sub _build_validator { + return Catalyst::Controller::DBIC::API::Validator->new; +} parameter static => ( isa => Bool, default => 0 ); role { - + my $p = shift; - + if($p->static) { requires qw/check_has_relation check_column_relation/; @@ -146,7 +139,7 @@ prefetch is passed to ->search to optimize the number of database fetches for jo ( is => 'ro', writer => '_set_prefetch', - isa => Prefetch, + isa => Prefetch, default => sub { $p->static ? [] : undef }, coerce => 1, trigger => sub @@ -191,20 +184,20 @@ Like the synopsis in DBIC::API shows, you can declare a "template" of what is al ( is => 'ro', writer => '_set_prefetch_allows', - isa => ArrayRef[ArrayRef|Str|HashRef], + isa => ArrayRef[ArrayRef|Str|HashRef], default => sub { [ ] }, predicate => 'has_prefetch_allows', trigger => sub { my ($self, $new) = @_; - sub check_rel { + sub _check_rel { my ($self, $rel, $static) = @_; if(ArrayRef->check($rel)) { foreach my $rel_sub (@$rel) { - $self->check_rel($rel_sub, $static); + $self->_check_rel($rel_sub, $static); } } elsif(HashRef->check($rel)) @@ -224,7 +217,7 @@ Like the synopsis in DBIC::API shows, you can declare a "template" of what is al foreach my $rel (@$new) { - $self->check_rel($rel, $p->static); + $self->_check_rel($rel, $p->static); } }, ); @@ -269,7 +262,7 @@ Please see L for details on how the format work trigger => sub { my ($self, $new) = @_; - + if($self->has_search_exposes and @{$self->search_exposes}) { foreach my $foo (@$new) @@ -292,7 +285,7 @@ Please see L for details on how the format work } } } - + my ($search_parameters, $search_attributes) = $self->generate_parameters_attributes($new); $self->_set_search_parameters($search_parameters); $self->_set_search_attributes($search_attributes); @@ -384,7 +377,7 @@ Please see L for more details. default => sub { $p->static ? [] : undef }, coerce => 1, trigger => sub - { + { my ($self, $new) = @_; if($self->has_select_exposes) { @@ -462,10 +455,12 @@ request_data holds the raw (but deserialized) data for ths request is => 'ro', isa => HashRef, writer => '_set_request_data', + predicate => 'has_request_data', trigger => sub { my ($self, $new) = @_; my $controller = $self->_controller; + return unless defined($new) && keys %$new; $self->_set_prefetch($new->{$controller->prefetch_arg}) if exists $new->{$controller->prefetch_arg}; $self->_set_select($new->{$controller->select_arg}) if exists $new->{$controller->select_arg}; $self->_set_as($new->{$controller->as_arg}) if exists $new->{$controller->as_arg}; @@ -488,9 +483,8 @@ format_search_parameters iterates through the provided params ArrayRef, calling method format_search_parameters => sub { - $DB::single = 1; my ($self, $params) = @_; - + my $genparams = []; foreach my $param (@$params) @@ -509,24 +503,23 @@ generate_column_parameters recursively generates properly aliased parameters for method generate_column_parameters => sub { - $DB::single = 1; my ($self, $source, $param, $join, $base) = @_; $base ||= 'me'; - my $search_params; + my $search_params = {}; # build up condition foreach my $column (keys %$param) { - if($source->has_relationship($column)) + if ($source->has_relationship($column)) { + # check if the value isn't a hashref unless (ref($param->{$column}) && reftype($param->{$column}) eq 'HASH') { $search_params->{join('.', $base, $column)} = $param->{$column}; next; } - %$search_params = - %{ + $search_params = { %$search_params, %{ $self->generate_column_parameters ( $source->related_source($column), @@ -534,12 +527,24 @@ generate_column_parameters recursively generates properly aliased parameters for Catalyst::Controller::DBIC::API::JoinBuilder->new(parent => $join, name => $column), $column ) - }; + }}; } - else + elsif ($source->has_column($column)) { $search_params->{join('.', $base, $column)} = $param->{$column}; } + # might be a sql function instead of a column name + # e.g. {colname => {like => '%foo%'}} + else + { + # but only if it's not a hashref + unless (ref($param->{$column}) && reftype($param->{$column}) eq 'HASH') { + $search_params->{join('.', $base, $column)} = $param->{$column}; + } + else { + die "$column is neither a relationship nor a column\n"; + } + } } return $search_params; @@ -553,7 +558,6 @@ generate_parameters_attributes takes the raw search arguments and formats the pa method generate_parameters_attributes => sub { - $DB::single = 1; my ($self, $args) = @_; return ( $self->format_search_parameters($args), $self->search_attributes ); @@ -569,7 +573,7 @@ This builder method generates the search attributes { my ($self, $args) = @_; my $static = $self->_controller; - my $search_attributes = + my $search_attributes = { group_by => $self->grouped_by || ((scalar(@{$static->grouped_by})) ? $static->grouped_by : undef), order_by => $self->ordered_by || ((scalar(@{$static->ordered_by})) ? $static->ordered_by : undef), @@ -577,6 +581,7 @@ This builder method generates the search attributes as => $self->as || ((scalar(@{$static->as})) ? $static->as : undef), prefetch => $self->prefetch || $static->prefetch || undef, rows => $self->count || $static->count, + page => $static->page, offset => $self->offset, join => $self->build_joins, }; @@ -590,15 +595,15 @@ This builder method generates the search attributes $search_attributes->{page} = $search_attributes->{offset} / $search_attributes->{rows} + 1; delete $search_attributes->{offset}; } - - $search_attributes = - { + + $search_attributes = + { map { @$_ } grep { - defined($_->[1]) - ? + defined($_->[1]) + ? (ref($_->[1]) && reftype($_->[1]) eq 'HASH' && keys %{$_->[1]}) || (ref($_->[1]) && reftype($_->[1]) eq 'ARRAY' && @{$_->[1]}) || length($_->[1]) @@ -613,7 +618,7 @@ This builder method generates the search attributes if ($search_attributes->{page} && !$search_attributes->{rows}) { die 'list_page can only be used with list_count'; } - + if ($search_attributes->{select}) { # make sure all columns have an alias to avoid ambiguous issues # but allow non strings (eg. hashrefs for db procs like 'count') @@ -622,7 +627,7 @@ This builder method generates the search attributes } return $search_attributes; - + }; };