| 1 | package DBIx::NoSQL::Search; | ||||||||||||||||||||||
| 2 | |||||||||||||||||||||||
| 3 | 7 | ▶ | use strict; | ||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||
| 4 | 7 | ▶ | use warnings; | ||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||
| 5 | |||||||||||||||||||||||
| 6 | 7 | ▶ | use Any::Moose; | ||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||
| 7 | 7 | ▶ | use Hash::Merge::Simple qw/ merge /; | ||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||
| 8 | |||||||||||||||||||||||
| 9 | 7 | has model => qw/ is ro required 1 /, handles => [qw/ store storage /]; | |||||||||||||||||||||
| 10 | |||||||||||||||||||||||
| 11 | 7 | has [qw/ _where /] => qw/ is rw isa Maybe[HashRef] /; | |||||||||||||||||||||
| 12 | 7 | has [qw/ _order_by /] => qw/ is rw isa Maybe[ArrayRef] /; | |||||||||||||||||||||
| 13 | 7 | has [qw/ _limit _offset /] => qw/ is rw /; | |||||||||||||||||||||
| 14 | |||||||||||||||||||||||
| 15 | 7 | has cursor => qw/ is ro lazy_build 1 /; | |||||||||||||||||||||
| 16 | sub _build_cursor { | ||||||||||||||||||||||
| 17 | 7 | ▶ | my $self = shift; | ||||||||||||||||||||
|
Subroutine
_build_cursor: called CC=1 SCAR=0.0
| |||||||||||||||||||||||
| 18 | 7 | $self->_cursor( 'value' ); | |||||||||||||||||||||
| 19 | } | ||||||||||||||||||||||
| 20 | |||||||||||||||||||||||
| 21 | sub _cursor { | ||||||||||||||||||||||
| 22 | 14 | ▶ | my $self = shift; | ||||||||||||||||||||
|
Subroutine
_cursor: called CC=1 SCAR=0.0
| |||||||||||||||||||||||
| 23 | 14 | my $target = shift; | |||||||||||||||||||||
| 24 | 14 | my ( $statement, @bind ) = $self->prepare( $target ); | |||||||||||||||||||||
| 25 | 14 | return $self->storage->cursor( $statement, \@bind ); | |||||||||||||||||||||
| 26 | } | ||||||||||||||||||||||
| 27 | |||||||||||||||||||||||
| 28 | sub search { | ||||||||||||||||||||||
| 29 | 0 | ▶ | my $self = shift; | ||||||||||||||||||||
|
Subroutine
search: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||
| 30 | 0 | return $self->where( @_ ); | |||||||||||||||||||||
| 31 | } | ||||||||||||||||||||||
| 32 | |||||||||||||||||||||||
| 33 | sub where { | ||||||||||||||||||||||
| 34 | 0 | ▶ | my $self = shift; | ||||||||||||||||||||
|
Subroutine
where: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||||||||||||||
| 35 | 0 | my $where = shift; | |||||||||||||||||||||
| 36 | |||||||||||||||||||||||
| 37 | 0 | ▶ | if ( my $_where = $self->_where ) { | ||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||
| 38 | 0 | $where = merge $_where, $where; | |||||||||||||||||||||
| 39 | } | ||||||||||||||||||||||
| 40 | |||||||||||||||||||||||
| 41 | 0 | return $self->clone( _where => $where ); | |||||||||||||||||||||
| 42 | } | ||||||||||||||||||||||
| 43 | |||||||||||||||||||||||
| 44 | sub order_by { | ||||||||||||||||||||||
| 45 | 5 | ▶ | my $self = shift; | ||||||||||||||||||||
|
Subroutine
order_by: called CC=3 SCAR=11.2
Pod
undocumented
| |||||||||||||||||||||||
| 46 | 5 | my $order_by = shift; | |||||||||||||||||||||
| 47 | |||||||||||||||||||||||
| 48 | 5 | ▶ | $order_by = [ $order_by ] unless ref $order_by; | ||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||
| 49 | |||||||||||||||||||||||
| 50 | 5 | ▶ | if ( my $_order_by = $self->_order_by ) { | ||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||
| 51 | 0 | $order_by = [ @$_order_by, @$order_by ]; | |||||||||||||||||||||
| 52 | } | ||||||||||||||||||||||
| 53 | |||||||||||||||||||||||
| 54 | 5 | return $self->clone( _order_by => $order_by ); | |||||||||||||||||||||
| 55 | } | ||||||||||||||||||||||
| 56 | |||||||||||||||||||||||
| 57 | |||||||||||||||||||||||
| 58 | sub clone { | ||||||||||||||||||||||
| 59 | 5 | ▶ | my $self = shift; | ||||||||||||||||||||
|
Subroutine
clone: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||
| 60 | 5 | my @override = @_; | |||||||||||||||||||||
| 61 | |||||||||||||||||||||||
| 62 | 5 | return ( ref $self )->new( | |||||||||||||||||||||
| 63 | model => $self->model, | ||||||||||||||||||||||
| 64 | _where => $self->_where, | ||||||||||||||||||||||
| 65 | _order_by => $self->_order_by, | ||||||||||||||||||||||
| 66 | _limit => $self->_limit, | ||||||||||||||||||||||
| 67 | _offset => $self->_offset, | ||||||||||||||||||||||
| 68 | @override | ||||||||||||||||||||||
| 69 | ); | ||||||||||||||||||||||
| 70 | } | ||||||||||||||||||||||
| 71 | |||||||||||||||||||||||
| 72 | 7 | ▶ | use DBIx::Class::SQLMaker; | ||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||
| 73 | sub prepare { | ||||||||||||||||||||||
| 74 | 16 | ▶ | my $self = shift; | ||||||||||||||||||||
|
Subroutine
prepare: called CC=5 SCAR=16.1
Pod
undocumented
| |||||||||||||||||||||||
| 75 | 16 | my $target = shift; | |||||||||||||||||||||
| 76 | |||||||||||||||||||||||
| 77 | 16 | ▶ | $target = 'value' unless defined $target; | ||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||
| 78 | |||||||||||||||||||||||
| 79 | 16 | my %options; | |||||||||||||||||||||
| 80 | 16 | ▶ | if ( my $order_by = $self->_order_by ) { | ||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||
| 81 | 5 | $options{ order_by } = $order_by; | |||||||||||||||||||||
| 82 | } | ||||||||||||||||||||||
| 83 | |||||||||||||||||||||||
| 84 | 16 | my @where_order_limit_offset = ( | |||||||||||||||||||||
| 85 | $self->_where, | ||||||||||||||||||||||
| 86 | \%options, | ||||||||||||||||||||||
| 87 | $self->_limit, | ||||||||||||||||||||||
| 88 | $self->_offset, | ||||||||||||||||||||||
| 89 | ); | ||||||||||||||||||||||
| 90 | |||||||||||||||||||||||
| 91 | 16 | my $maker = DBIx::Class::SQLMaker->new; | |||||||||||||||||||||
| 92 | |||||||||||||||||||||||
| 93 | 16 | my $entity_table = '__Store__'; | |||||||||||||||||||||
| 94 | 16 | my $model_name = $self->model->name; | |||||||||||||||||||||
| 95 | 16 | my $search_table = $model_name; | |||||||||||||||||||||
| 96 | 16 | my $search_key_column = 'key'; | |||||||||||||||||||||
| 97 | |||||||||||||||||||||||
| 98 | 16 | ▶ | if ( $target eq 'value' ) { $target = '__Store__.__value__' } | ||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||
| 99 | 7 | elsif ( $target eq 'count' ) { $target = 'COUNT(*)' } | |||||||||||||||||||||
| 100 | 0 | else { die "Invalid target ($target)" } | |||||||||||||||||||||
| 101 | |||||||||||||||||||||||
| 102 | 16 | my ( $statement, @bind ) = $maker->select( | |||||||||||||||||||||
| 103 | [ | ||||||||||||||||||||||
| 104 | { me => $search_table }, | ||||||||||||||||||||||
| 105 | [ | ||||||||||||||||||||||
| 106 | { '-join-type' => 'LEFT', '__Store__' => $entity_table }, | ||||||||||||||||||||||
| 107 | { "__Store__.__key__" => \"= me.$search_key_column", '__Store__.__model__' => \"= '$model_name'" }, | ||||||||||||||||||||||
| 108 | #{ "__Store__.__key__" => "me.$search_key_column", '__Store__.__model__' => "'$model_name'" }, | ||||||||||||||||||||||
| 109 | ] | ||||||||||||||||||||||
| 110 | ], | ||||||||||||||||||||||
| 111 | $target, | ||||||||||||||||||||||
| 112 | @where_order_limit_offset, | ||||||||||||||||||||||
| 113 | ); | ||||||||||||||||||||||
| 114 | |||||||||||||||||||||||
| 115 | 16 | return ( $statement, @bind ); | |||||||||||||||||||||
| 116 | } | ||||||||||||||||||||||
| 117 | |||||||||||||||||||||||
| 118 | sub all { | ||||||||||||||||||||||
| 119 | 7 | ▶ | my $self = shift; | ||||||||||||||||||||
|
Subroutine
all: called CC=7 SCAR=22.2
Pod
undocumented
| |||||||||||||||||||||||
| 120 | 7 | my %options = @_; | |||||||||||||||||||||
| 121 | |||||||||||||||||||||||
| 122 | 7 | my $model = $self->model; | |||||||||||||||||||||
| 123 | 7 | ▶ | my $as = $options{ as } || 'object'; | ||||||||||||||||||||
|
Condition100%2/2
$options{'as'} || 'object'
MC/DC100%1/1
$options{'as'} || 'object'
$options{'as'}
Truth table100%2/2
A
A= $options{'as'}
| |||||||||||||||||||||||
| 124 | |||||||||||||||||||||||
| 125 | 7 | my $cursor = $self->cursor; | |||||||||||||||||||||
| 126 | 7 | my $all = $self->cursor->all; | |||||||||||||||||||||
| 127 | |||||||||||||||||||||||
| 128 | 7 | ▶ | if ( $as eq 'object' ) { return map { $model->create_object( $_->[0] ) } @$all } | ||||||||||||||||||||
|
Branch: 4 branches
| |||||||||||||||||||||||
| 129 | 0 | elsif ( $as eq 'entity' ) { return map { $model->create_entity( $_->[0] ) } @$all } | |||||||||||||||||||||
| 130 | ▶ | elsif ( $as eq 'data' || | |||||||||||||||||||||
|
Condition33%1/3
$as eq "data" or $as eq "hash"
MC/DC0%0/2
$as eq "data" or $as eq "hash"
$as eq "data"
$as eq "hash"
Truth table33%1/3
A or B
A= $as eq "data"B= $as eq "hash"
| |||||||||||||||||||||||
| 131 | 3 | $as eq 'hash' ) { return map { $model->create_data( $_->[0] ) } @$all } | |||||||||||||||||||||
| 132 | 0 | elsif ( $as eq 'value' ) { return @$all } | |||||||||||||||||||||
| 133 | 0 | else { die "Invalid inflation target ($as)" } | |||||||||||||||||||||
| 134 | } | ||||||||||||||||||||||
| 135 | |||||||||||||||||||||||
| 136 | sub next { | ||||||||||||||||||||||
| 137 | 2 | ▶ | my $self = shift; | ||||||||||||||||||||
|
Subroutine
next: called CC=9 SCAR=30.0
Pod
undocumented
| |||||||||||||||||||||||
| 138 | 2 | my %options = @_; | |||||||||||||||||||||
| 139 | |||||||||||||||||||||||
| 140 | 2 | my $model = $self->model; | |||||||||||||||||||||
| 141 | 2 | ▶ | my $as = $options{ as } || 'object'; | ||||||||||||||||||||
|
Condition50%1/2
$options{'as'} || 'object'
MC/DC0%0/1
$options{'as'} || 'object'
$options{'as'}
Truth table50%1/2
A
A= $options{'as'}
| |||||||||||||||||||||||
| 142 | |||||||||||||||||||||||
| 143 | 2 | my $cursor = $self->cursor; | |||||||||||||||||||||
| 144 | 2 | my $value = $cursor->next; | |||||||||||||||||||||
| 145 | |||||||||||||||||||||||
| 146 | # cursor returns an arrayref, we want its first element | ||||||||||||||||||||||
| 147 | 2 | ▶ | $value = $value->[0] if ref $value; | ||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||
| 148 | |||||||||||||||||||||||
| 149 | # nothing found? eject | ||||||||||||||||||||||
| 150 | 2 | ▶ | return unless $value; | ||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||
| 151 | |||||||||||||||||||||||
| 152 | 1 | ▶ | if ( $as eq 'object' ) { return $model->create_object( $value ) } | ||||||||||||||||||||
|
Branch: 4 branches
| |||||||||||||||||||||||
| 153 | 0 | elsif ( $as eq 'entity' ) { return $model->create_entity( $value ) } | |||||||||||||||||||||
| 154 | ▶ | elsif ( $as eq 'data' || | |||||||||||||||||||||
|
Condition0%0/3
$as eq "data" or $as eq "hash"
MC/DC0%0/2
$as eq "data" or $as eq "hash"
$as eq "data"
$as eq "hash"
Truth table0%0/3
A or B
A= $as eq "data"B= $as eq "hash"
| |||||||||||||||||||||||
| 155 | 0 | $as eq 'hash' ) { return $model->create_data( $value ) } | |||||||||||||||||||||
| 156 | 0 | elsif ( $as eq 'value' ) { return $value } | |||||||||||||||||||||
| 157 | 0 | else { die "Invalid inflation target ($as)" } | |||||||||||||||||||||
| 158 | } | ||||||||||||||||||||||
| 159 | |||||||||||||||||||||||
| 160 | sub offset { | ||||||||||||||||||||||
| 161 | 0 | ▶ | my $self = shift; | ||||||||||||||||||||
|
Subroutine
offset: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||
| 162 | 0 | my $offset = shift; | |||||||||||||||||||||
| 163 | |||||||||||||||||||||||
| 164 | 0 | return $self->clone( _offset => $offset ); | |||||||||||||||||||||
| 165 | } | ||||||||||||||||||||||
| 166 | |||||||||||||||||||||||
| 167 | sub limit { | ||||||||||||||||||||||
| 168 | 0 | ▶ | my $self = shift; | ||||||||||||||||||||
|
Subroutine
limit: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||
| 169 | 0 | my $limit = shift; | |||||||||||||||||||||
| 170 | |||||||||||||||||||||||
| 171 | 0 | return $self->clone( _limit => $limit ); | |||||||||||||||||||||
| 172 | } | ||||||||||||||||||||||
| 173 | |||||||||||||||||||||||
| 174 | sub slice { | ||||||||||||||||||||||
| 175 | 0 | ▶ | my $self = shift; | ||||||||||||||||||||
|
Subroutine
slice: not called CC=3 SCAR=24.8
Pod
undocumented
| |||||||||||||||||||||||
| 176 | 0 | my $from = shift; | |||||||||||||||||||||
| 177 | 0 | my $to = shift; | |||||||||||||||||||||
| 178 | |||||||||||||||||||||||
| 179 | 0 | ▶ | my $offset = $self->_offset || 0; | ||||||||||||||||||||
|
Condition0%0/2
$self->_offset || 0
MC/DC0%0/1
$self->_offset || 0
$self->_offset
Truth table0%0/2
A
A= $self->_offset
| |||||||||||||||||||||||
| 180 | |||||||||||||||||||||||
| 181 | 0 | $offset += $from; | |||||||||||||||||||||
| 182 | 0 | ▶ | my $limit = $to ? ( $to - $from + 1 ) : 1; | ||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||
| 183 | |||||||||||||||||||||||
| 184 | 0 | return $self->clone( _offset => $offset, _limit => $limit ); | |||||||||||||||||||||
| 185 | } | ||||||||||||||||||||||
| 186 | |||||||||||||||||||||||
| 187 | sub count { | ||||||||||||||||||||||
| 188 | 7 | ▶ | my $self = shift; | ||||||||||||||||||||
|
Subroutine
count: called CC=2 SCAR=7.0
Pod
undocumented
| |||||||||||||||||||||||
| 189 | |||||||||||||||||||||||
| 190 | 7 | my $cursor = $self->_cursor( 'count' ); | |||||||||||||||||||||
| 191 | 7 | ▶ | return unless my $result = $cursor->next; | ||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||
| 192 | 7 | return $result->[0]; | |||||||||||||||||||||
| 193 | } | ||||||||||||||||||||||
| 194 | |||||||||||||||||||||||
| 195 | sub fetch { | ||||||||||||||||||||||
| 196 | 3 | ▶ | my $self = shift; | ||||||||||||||||||||
|
Subroutine
fetch: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||
| 197 | 3 | return $self->all( as => 'data', @_ ); | |||||||||||||||||||||
| 198 | } | ||||||||||||||||||||||
| 199 | |||||||||||||||||||||||
| 200 | sub get { | ||||||||||||||||||||||
| 201 | 0 | ▶ | my $self = shift; | ||||||||||||||||||||
|
Subroutine
get: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||
| 202 | 0 | return $self->all( @_ ); | |||||||||||||||||||||
| 203 | } | ||||||||||||||||||||||
| 204 | |||||||||||||||||||||||
| 205 | 7 | 1; | |||||||||||||||||||||