| 1 | package Clustericious::RouteBuilder::Search; | ||||||||||||||||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||||||||||||||||
| 3 | 1 | ▶ | use strict; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 4 | 1 | ▶ | use warnings; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 5 | 1 | ▶ | use Clustericious::Log; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||||||||||||||||
| 7 | # ABSTRACT: build routes for searching for objects | ||||||||||||||||||||||||||||||||||||||||||
| 8 | 1 | our $VERSION = '1.29'; # VERSION | |||||||||||||||||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||||||||||||||||
| 11 | 1 | ▶ | use Sub::Exporter -setup => { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 12 | exports => [ | ||||||||||||||||||||||||||||||||||||||||||
| 13 | "search" => \&_build_search, | ||||||||||||||||||||||||||||||||||||||||||
| 14 | ], | ||||||||||||||||||||||||||||||||||||||||||
| 15 | collectors => ['defaults'], | ||||||||||||||||||||||||||||||||||||||||||
| 16 | 1 | }; | |||||||||||||||||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||||||||||||||||
| 18 | 0 | ▶ | sub _build_search { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_build_search: not called CC=4 SCAR=30.0
| |||||||||||||||||||||||||||||||||||||||||||
| 19 | 0 | my ($class, $name, $arg, $defaults) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 20 | 0 | ▶ | my $finder = $arg->{finder} || $defaults->{defaults}{finder} || die "no finder defined"; | ||||||||||||||||||||||||||||||||||||||||
|
Condition0%0/5
$arg->{'finder'} || $defaults->{'defaults'}{'finder'} || die('no finder defined')
$arg->{"finder"} or $defaults->{"defaults"}{"finder"}
MC/DC0%0/2
$arg->{'finder'} || $defaults->{'defaults'}{'finder'} || die('no finder defined')
$arg->{"finder"}
$defaults->{"defaults"}{"finder"}
Truth table0%0/3
A or B
A= $arg->{"finder"}B= $defaults->{"defaults"}{"finder"}
| |||||||||||||||||||||||||||||||||||||||||||
| 21 | 0 | ▶ | $finder->can("find_class") or die "$finder must be able to find_class"; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 22 | 0 | ▶ | sub { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=18 SCAR=58.3
| |||||||||||||||||||||||||||||||||||||||||||
| 23 | 0 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||||||||||||||||
| 25 | 0 | ▶ | my $items = $self->stash("items") or LOGDIE "no items in request"; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 26 | |||||||||||||||||||||||||||||||||||||||||||
| 27 | 0 | ▶ | my $manager = $finder->find_class($items) or LOGDIE "no class for $items"; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 28 | 0 | ▶ | unless ($manager->can("object_class")) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 29 | # Allow tables names in addition to plurals | ||||||||||||||||||||||||||||||||||||||||||
| 30 | 0 | $manager .= "::Manager"; | |||||||||||||||||||||||||||||||||||||||||
| 31 | } | ||||||||||||||||||||||||||||||||||||||||||
| 32 | 0 | $self->parse_autodata; | |||||||||||||||||||||||||||||||||||||||||
| 33 | |||||||||||||||||||||||||||||||||||||||||||
| 34 | 0 | my $p = $self->stash->{autodata}; | |||||||||||||||||||||||||||||||||||||||||
| 35 | |||||||||||||||||||||||||||||||||||||||||||
| 36 | 0 | ▶ | if (my $mode = $p->{mode}) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 37 | 0 | DEBUG "Called search mode $mode"; | |||||||||||||||||||||||||||||||||||||||||
| 38 | # A search "mode" indicates that we use a pre-canned | ||||||||||||||||||||||||||||||||||||||||||
| 39 | # query stored in the manager class. This query is | ||||||||||||||||||||||||||||||||||||||||||
| 40 | # run by calling a method named "search_$mode" on the | ||||||||||||||||||||||||||||||||||||||||||
| 41 | # manager class. This method should return a count | ||||||||||||||||||||||||||||||||||||||||||
| 42 | # and a resultset (array ref of hashrefs). | ||||||||||||||||||||||||||||||||||||||||||
| 43 | 0 | my $method = "search_$mode"; | |||||||||||||||||||||||||||||||||||||||||
| 44 | 0 | my $got = $manager->$method($p); | |||||||||||||||||||||||||||||||||||||||||
| 45 | ERROR "search_$mode did not return count/resultset" | ||||||||||||||||||||||||||||||||||||||||||
| 46 | 0 | ▶ | unless ref($got) eq 'HASH' && exists($got->{count}) && exists($got->{resultset}); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/6
ref $got eq "HASH" and exists $got->{"count"} and exists $got->{"resultset"}
ref $got eq "HASH" and exists $got->{"count"}
MC/DC0%0/3
ref $got eq "HASH" and exists $got->{"count"} and exists $got->{"resultset"}
ref $got eq "HASH"
exists $got->{"count"}
exists $got->{"resultset"}
Truth table0%0/4
A and B and C
A= ref $got eq "HASH"B= exists $got->{"count"}C= exists $got->{"resultset"}
| |||||||||||||||||||||||||||||||||||||||||||
| 47 | 0 | $self->stash(autodata => $got); | |||||||||||||||||||||||||||||||||||||||||
| 48 | 0 | return; | |||||||||||||||||||||||||||||||||||||||||
| 49 | } | ||||||||||||||||||||||||||||||||||||||||||
| 50 | |||||||||||||||||||||||||||||||||||||||||||
| 51 | |||||||||||||||||||||||||||||||||||||||||||
| 52 | #TRACE "searching for $items : ".Dumper($p); | ||||||||||||||||||||||||||||||||||||||||||
| 53 | |||||||||||||||||||||||||||||||||||||||||||
| 54 | # maybe restrict, by first calling $manager->normalize_get_objects_args(%$p) | ||||||||||||||||||||||||||||||||||||||||||
| 55 | |||||||||||||||||||||||||||||||||||||||||||
| 56 | 0 | my $all = delete $p->{query_all}; | |||||||||||||||||||||||||||||||||||||||||
| 57 | # "If the first argument is a hash it is treated as 'query'" -- RDBOM docs | ||||||||||||||||||||||||||||||||||||||||||
| 58 | 0 | ▶ | my @args = $all || exists( $p->{query} ) ? %$p | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
Condition0%0/3
$all or exists $p->{"query"}
MC/DC0%0/2
$all or exists $p->{"query"}
$all
exists $p->{"query"}
Truth table0%0/3
A or B
A= $allB= exists $p->{"query"}
| |||||||||||||||||||||||||||||||||||||||||||
| 59 | : ( keys %$p > 0 ) ? $p | ||||||||||||||||||||||||||||||||||||||||||
| 60 | : (); | ||||||||||||||||||||||||||||||||||||||||||
| 61 | 0 | TRACE "args are @args"; | |||||||||||||||||||||||||||||||||||||||||
| 62 | 0 | push @args, object_class => $manager->object_class; | |||||||||||||||||||||||||||||||||||||||||
| 63 | 0 | my %a = @args; | |||||||||||||||||||||||||||||||||||||||||
| 64 | 0 | ▶ | if (!$a{limit} && !$a{page}) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
Condition0%0/3
$a{'limit'} || $a{'page'}
MC/DC0%0/2
$a{'limit'} || $a{'page'}
$a{'limit'}
$a{'page'}
Truth table0%0/3
A || B
A= $a{'limit'}B= $a{'page'}
| |||||||||||||||||||||||||||||||||||||||||||
| 65 | 0 | DEBUG "Adding limit 100 to query"; | |||||||||||||||||||||||||||||||||||||||||
| 66 | 0 | push @args, ( limit => 100 ); | |||||||||||||||||||||||||||||||||||||||||
| 67 | ▶ | } elsif ( ($a{limit} || $a{per_page} || 0) > 1000) { | |||||||||||||||||||||||||||||||||||||||||
|
Condition0%0/5
$a{'limit'} || $a{'per_page'} || 0
$a{"limit"} or $a{"per_page"}
MC/DC0%0/2
$a{'limit'} || $a{'per_page'} || 0
$a{"limit"}
$a{"per_page"}
Truth table0%0/3
A or B
A= $a{"limit"}B= $a{"per_page"}
| |||||||||||||||||||||||||||||||||||||||||||
| 68 | 0 | ▶ | WARN "Very large limit : ".($a{limit} || $a{per_page}); | ||||||||||||||||||||||||||||||||||||||||
|
Condition0%0/3
$a{'limit'} || $a{'per_page'}
MC/DC0%0/2
$a{'limit'} || $a{'per_page'}
$a{'limit'}
$a{'per_page'}
Truth table0%0/3
A || B
A= $a{'limit'}B= $a{'per_page'}
| |||||||||||||||||||||||||||||||||||||||||||
| 69 | } | ||||||||||||||||||||||||||||||||||||||||||
| 70 | |||||||||||||||||||||||||||||||||||||||||||
| 71 | 0 | my $count = $manager->get_objects_count( @args ); | |||||||||||||||||||||||||||||||||||||||||
| 72 | 0 | my @key = $manager->object_class->meta->primary_key_column_names; | |||||||||||||||||||||||||||||||||||||||||
| 73 | $self->stash(autodata => { | ||||||||||||||||||||||||||||||||||||||||||
| 74 | count => $count, | ||||||||||||||||||||||||||||||||||||||||||
| 75 | key => (@key > 1 ? \@key : $key[0]), | ||||||||||||||||||||||||||||||||||||||||||
| 76 | 0 | ▶ | resultset => [ map $_->as_hash, @{ $manager->get_objects( @args ) } ] | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 77 | }); | ||||||||||||||||||||||||||||||||||||||||||
| 78 | 0 | }; | |||||||||||||||||||||||||||||||||||||||||
| 79 | } | ||||||||||||||||||||||||||||||||||||||||||
| 80 | |||||||||||||||||||||||||||||||||||||||||||
| 81 | 1 | 1; | |||||||||||||||||||||||||||||||||||||||||
| 82 | |||||||||||||||||||||||||||||||||||||||||||
| 83 | __END__ | ||||||||||||||||||||||||||||||||||||||||||