Yancy/Backend/Memory.pm

filter: Statementstmt 98.2% 219 / 223 Branchbran 88.4% 92 / 104 Conditioncond 83.0% 93 / 112 MC/DCmcdc 63.7% 37 / 58 Subroutinesub 100% 20 / 20 Podpod 11.1% 1 / 9 totaltotal 87.8% 462 / 526 CC 116 SCAR 26.1
CC
116
Cov
92%
_join
22.6
list
18.1
create
17.0
CRAP 13.6
« Yancy/Plugin/Form/Bootstrap4.pm ↑ Summary Yancy/Model/Schema.pm »
1 package Yancy::Backend::Memory;
2 23 our $VERSION = '1.088';
3 # ABSTRACT: A backend entirely in memory
4  
5 #pod =head1 DESCRIPTION
6 #pod
7 #pod An in-memory "database" backend for Yancy. Uses L<Yancy::Util/match> to implement
8 #pod basic searching for (</list>).
9 #pod
10 #pod =cut
11  
12 # XXX: TODO Remove references to Local::Test
13  
14 23 ▶use Mojo::Base '-base';
Subroutine BEGIN: called CC=1 SCAR=0.0
15 23 ▶use List::Util qw( max );
Subroutine BEGIN: called CC=1 SCAR=0.0
16 23 ▶use Mojo::JSON qw( true false from_json to_json encode_json );
Subroutine BEGIN: called CC=1 SCAR=0.0
17 23 ▶use Mojo::File qw( path );
Subroutine BEGIN: called CC=1 SCAR=0.0
18 23 ▶use Storable qw( dclone );
Subroutine BEGIN: called CC=1 SCAR=0.0
19 23 ▶use Role::Tiny qw( with );
Subroutine BEGIN: called CC=1 SCAR=0.0
20 23 with 'Yancy::Backend::Role::Sync';
21 23 ▶use Yancy::Util qw( match is_type order_by is_format );
Subroutine BEGIN: called CC=1 SCAR=0.0
22 23 ▶use Time::Piece;
Subroutine BEGIN: called CC=1 SCAR=0.0
23  
24 23 our %DATA;
25  
26 85 ▶sub new {
Subroutine new: called CC=5 SCAR=18.1
Pod documented
27 85 my ( $class, $url, $schema ) = @_;
28 85 ▶ if ( $url ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($url) 84 1 85
29 84 my ( $path ) = $url =~ m{^[^:]+://[^/]+(?:/(.+))?$};
30 84 ▶ if ( $path ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($path) 0 84 84
31 0 ▶ %DATA = %{ from_json( path( ( $ENV{MOJO_HOME} || () ), $path )->slurp ) };
Condition0%0/3
$ENV{'MOJO_HOME'} || ()
l!l&&r!l&&!r
000
MC/DC0%0/2
$ENV{'MOJO_HOME'} || ()
$ENV{'MOJO_HOME'} ()
Truth table0%0/3
A || B
A= $ENV{'MOJO_HOME'}B= ()
ABresult
1X1
011
000
32 }
33 }
34 85 ▶ $schema //= \%Local::Test::SCHEMA;
Condition100%2/2
$schema //= \%Local::Test::SCHEMA
l!l
796
MC/DC100%1/1
$schema //= \%Local::Test::SCHEMA
$schema
Truth table100%2/2
A
A= $schema
Aresult
11
00
35 85 return bless { init_arg => $url, schema => $schema }, $class;
36 }
37  
38 3203 ▶sub schema {
Subroutine schema: called CC=2 SCAR=6.9
Pod undocumented
39 3203 my ( $self, $schema ) = @_;
40 3203 ▶ if ( $schema ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($schema) 52 3151 3203
41 52 $self->{schema} = $schema;
42 52 return;
43 }
44 3151 $self->{schema};
45 }
46 sub collections;
47 23 *collections = *schema;
48  
49 116 ▶sub create {
Subroutine create: called CC=17 SCAR=28.3
Pod undocumented
50 116 my ( $self, $schema_name, $params ) = @_;
51 116 $params = { %$params };
52 116 my $props = $self->schema->{ $schema_name }{properties};
53 $params->{ $_ } = $props->{ $_ }{default} // undef
54 116 ▶ for grep !exists $params->{ $_ },
Condition100%2/2
$props->{$_}{"default"} // undef
l!l
149174
MC/DC100%1/1
$props->{$_}{"default"} // undef
$props->{$_}{"default"}
Truth table100%2/2
A
A= $props->{$_}{"default"}
Aresult
11
00
55 keys %$props;
56 116 $params = $self->_normalize( $schema_name, $params ); # makes a copy
57  
58 116 ▶ my $id_field = $self->schema->{ $schema_name }{ 'x-id-field' } || 'id';
Condition100%2/2
$self->schema->{$schema_name}{'x-id-field'} || 'id'
l!l
7145
MC/DC100%1/1
$self->schema->{$schema_name}{'x-id-field'} || 'id'
$self->schema->{$schema_name}{'x-id-field'}
Truth table100%2/2
A
A= $self->schema->{$schema_name}{'x-id-field'}
Aresult
11
00
59 116 ▶ my @id_fields = ref $id_field eq 'ARRAY' ? @$id_field : ( $id_field );
Branch: 1 branch
BranchTrueFalseTotal
ref $id_field eq 'ARRAY' ? : 5 111 116
60  
61 # Fill in any auto-increment data...
62 116 for my $id_field ( @id_fields ) {
63 # We haven't provided a value for an integer ID, assume it's autoinc
64 121 ▶ if ( !$params->{ $id_field } and $self->schema->{ $schema_name }{properties}{ $id_field }{type} eq 'integer' ) {
Branch: 2 branches
BranchTrueFalseTotal
if (not $params->{$id_field} and $self->schema->{$schema_name}{'properties'}{$id_field}{'type'} eq 'integer') { } 58 63 121
elsif ($params->{$id_field} and $id_field ne 'id' and exists $self->schema->{$schema_name}{'properties'}{'id'}) { } 46 23 69
Condition66%2/3
not $params->{$id_field} and $self->schema->{$schema_name}{"properties"}{$id_field}{"type"} eq "integer"
!ll&&!rl&&r
63058
MC/DC50%1/2
not $params->{$id_field} and $self->schema->{$schema_name}{"properties"}{$id_field}{"type"} eq "integer"
not $params->{$id_field} $self->schema->{$schema_name}{"properties"}{$id_field}{"type"} eq "integer"
Truth table66%2/3
not(A) and B
A= not $params->{$id_field}B= $self->schema->{$schema_name}{"properties"}{$id_field}{"type"} eq "integer"
ABresult
0X0
100
111
65 58 my @existing_ids = keys %{ $DATA{ $schema_name } };
66 58 ▶ $params->{ $id_field} = ( max( @existing_ids ) // 0 ) + 1;
Condition100%2/2
&max(@existing_ids) // 0
l!l
4018
MC/DC100%1/1
&max(@existing_ids) // 0
&max(@existing_ids)
Truth table100%2/2
A
A= &max(@existing_ids)
Aresult
11
00
67 }
68 # We have provided another ID, make 'id' another autoinc
69 ▶ elsif ( $params->{ $id_field }
Condition83%5/6
$params->{$id_field} and $id_field ne "id" and exists $self->schema->{$schema_name}{"properties"}{"id"}
!ll&&!rl&&r
61146
$params->{$id_field} and $id_field ne "id"
!ll&&!rl&&r
0657
MC/DC66%2/3
$params->{$id_field} and $id_field ne "id" and exists $self->schema->{$schema_name}{"properties"}{"id"}
$params->{$id_field} $id_field ne "id" exists $self->schema->{$schema_name}{"properties"}{"id"}
Truth table75%3/4
A and B and C
A= $params->{$id_field}B= $id_field ne "id"C= exists $self->schema->{$schema_name}{"properties"}{"id"}
ABCresult
0XX0
10X0
1100
1111
70 && $id_field ne 'id'
71 && exists $self->schema->{ $schema_name }{properties}{id}
72 ) {
73 46 my @existing_ids = map { $_->{ id } } values %{ $DATA{ $schema_name } };
74 46 ▶ $params->{id} = ( max( @existing_ids ) // 0 ) + 1;
Condition100%2/2
&max(@existing_ids) // 0
l!l
2917
MC/DC100%1/1
&max(@existing_ids) // 0
&max(@existing_ids)
Truth table100%2/2
A
A= &max(@existing_ids)
Aresult
11
00
75 }
76 }
77  
78 116 ▶ my $store = $DATA{ $schema_name } //= {};
Condition100%2/2
$Yancy::Backend::Memory::DATA{$schema_name} //= {}
l!l
1142
MC/DC100%1/1
$Yancy::Backend::Memory::DATA{$schema_name} //= {}
$Yancy::Backend::Memory::DATA{$schema_name}
Truth table100%2/2
A
A= $Yancy::Backend::Memory::DATA{$schema_name}
Aresult
11
00
79 116 for my $i ( 0 .. $#id_fields-1 ) {
80 5 ▶ $store = $store->{ $params->{ $id_fields[$i] } } //= {};
Condition100%2/2
$$store{$$params{$id_fields[$i]}} //= {}
l!l
14
MC/DC100%1/1
$$store{$$params{$id_fields[$i]}} //= {}
$$store{$$params{$id_fields[$i]}}
Truth table100%2/2
A
A= $$store{$$params{$id_fields[$i]}}
Aresult
11
00
81 }
82 116 $store->{ $params->{ $id_fields[-1] } } = $params;
83  
84 116 ▶ return @id_fields > 1 ? { map {; $_ => $params->{ $_ } } @id_fields } : $params->{ $id_field };
Branch: 1 branch
BranchTrueFalseTotal
@id_fields > 1 ? : 5 111 116
85 }
86  
87 326 ▶sub get {
Subroutine get: called CC=12 SCAR=24.9
Pod undocumented
88 326 my ( $self, $schema_name, $id, %opt ) = @_;
89 326 my $schema = $self->schema->{ $schema_name };
90 326 ▶ my $real_coll = ( $schema->{'x-view'} || {} )->{schema} // $schema_name;
Condition80%4/5
+($schema->{"x-view"} || {})->{"schema"} // $schema_name
l!l&&r!l&&!r
23240
$schema->{'x-view'} || {}
l!l
2324
MC/DC0%0/2
+($schema->{"x-view"} || {})->{"schema"} // $schema_name
$schema->{'x-view'} $schema_name
Truth table66%2/3
A // B
A= $schema->{'x-view'}B= $schema_name
ABresult
1X1
011
000
91  
92 326 ▶ my $id_field = $self->schema->{ $schema_name }{ 'x-id-field' } || 'id';
Condition100%2/2
$self->schema->{$schema_name}{'x-id-field'} || 'id'
l!l
164162
MC/DC100%1/1
$self->schema->{$schema_name}{'x-id-field'} || 'id'
$self->schema->{$schema_name}{'x-id-field'}
Truth table100%2/2
A
A= $self->schema->{$schema_name}{'x-id-field'}
Aresult
11
00
93 326 ▶ my @ids = ref $id_field eq 'ARRAY' ? map { $id->{ $_ } } @$id_field : ( $id );
Branch: 1 branch
BranchTrueFalseTotal
ref $id_field eq 'ARRAY' ? : 13 313 326
94 325 ▶ die "Missing composite ID parts" if @ids > 1 && ( !ref $id || keys %$id < @ids );
Branch: 1 branch
BranchTrueFalseTotal
if @ids > 1 and !ref($id) || keys %$id < @ids 1 324 325
Condition83%5/6
@ids > 1 and !ref($id) || keys %$id < @ids
!ll&&!rl&&r
313111
not ref $id or keys %$id < @ids
l!l&&r!l&&!r
0111
MC/DC66%2/3
@ids > 1 and !ref($id) || keys %$id < @ids
@ids > 1 not ref $id keys %$id < @ids
Truth table75%3/4
A and not(B) or C
A= @ids > 1B= not ref $idC= keys %$id < @ids
ABCresult
0XX0
1000
11X1
1011
95  
96 324 my $item = $DATA{ $real_coll };
97 324 for my $id ( @ids ) {
98 335 ▶ return undef if !defined $id;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $id 3 332 335
99 332 ▶ $item = $item->{ $id } // return undef;
Condition100%2/2
$item->{$id} // (return undef)
l!l
29141
MC/DC100%1/1
$item->{$id} // (return undef)
$item->{$id}
Truth table100%2/2
A
A= $item->{$id}
Aresult
11
00
100 }
101  
102 280 $item = $self->_viewise( $schema_name, $item );
103 280 ▶ if ( my $join = $opt{join} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $join = $opt{"join"}) 4 276 280
104 4 $item = $self->_join( $schema_name, $item, $join );
105 }
106  
107 280 return $item;
108 }
109  
110 21 ▶sub _join {
Subroutine _join: called CC=21 SCAR=31.2
111 21 my ( $self, $schema_name, $item, $join, $where ) = @_;
112 21 $item = { %$item };
113 21 my $schema = $self->schema->{ $schema_name };
114 21 my %props = %{ $schema->{properties} };
115 21 ▶ my $id_field = $schema->{ 'x-id-field' } || 'id';
Condition50%1/2
$schema->{'x-id-field'} || 'id'
l!l
210
MC/DC0%0/1
$schema->{'x-id-field'} || 'id'
$schema->{'x-id-field'}
Truth table50%1/2
A
A= $schema->{'x-id-field'}
Aresult
11
00
116 21 ▶ my @joins = ref $join eq 'ARRAY' ? @$join : ( $join );
Branch: 1 branch
BranchTrueFalseTotal
ref $join eq 'ARRAY' ? : 8 13 21
117 21 for my $join ( @joins ) {
118 29 ▶ if ( my $join_prop = $schema->{ properties }{ $join } ) {
Branch: 2 branches
BranchTrueFalseTotal
if (my $join_prop = $schema->{'properties'}{$join}) { } 21 8 29
elsif (my $join_schema = $self->schema->{$join}) { } 8 0 8
119 21 ▶ my $join_id = $item->{ $join } || next;
Condition100%2/2
$item->{$join} || (next)
l!l
174
MC/DC100%1/1
$item->{$join} || (next)
$item->{$join}
Truth table100%2/2
A
A= $item->{$join}
Aresult
11
00
120 17 my $join_schema_name = $join_prop->{'x-foreign-key'};
121 17 $item->{ $join } = $self->get( $join_schema_name, $join_id );
122 17 for my $key ( grep /^${join}\./, keys %$where ) {
123 7 my ( $k ) = $key =~ /^${join}\.(.+)$/;
124 7 ▶ if ( !match( { $k => $where->{ $key } }, $item->{ $join } ) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (match({$k, $where->{$key}}, $item->{$join})) 2 5 7
125 # Inner match fails, so this row is not in the
126 # results
127 2 return;
128 }
129 }
130 }
131 elsif ( my $join_schema = $self->schema->{ $join } ) {
132 8 my $join_schema_name = $join;
133 8 my $join_props = $join_schema->{properties};
134 my $join_where = {
135 2 map { s/^$join\.//r => $where->{ $_ } }
136 8 grep { /^$join\./ }
137 keys %$where
138 };
139  
140 # First try to find the foreign key on the local schema
141 8 ▶ if ( my ( $join_prop_name ) = grep { ($props{ $_ }{ 'x-foreign-key' }//'') =~ /^$join_schema_name(\.|$)/ } keys %props ) {
Branch: 2 branches
BranchTrueFalseTotal
if (my($join_prop_name) = grep({($props{$_}{'x-foreign-key'} // '') =~ /^$join_schema_name(\.|$)/u;} keys %props)) { } 3 5 8
elsif (($join_prop_name) = grep({($join_props->{$_}{'x-foreign-key'} // '') =~ /^$schema_name(\.|$)/u;} keys %$join_props)) { } 5 0 5
Condition100%2/2
$props{$_}{"x-foreign-key"} // ""
l!l
621
MC/DC100%1/1
$props{$_}{"x-foreign-key"} // ""
$props{$_}{"x-foreign-key"}
Truth table100%2/2
A
A= $props{$_}{"x-foreign-key"}
Aresult
11
00
142 3 my $join_prop = $props{ $join_prop_name };
143 3 my ( undef, $join_key_field ) = split /\./, $join_prop->{'x-foreign-key'};
144 3 ▶ $join_key_field //= $join_schema->{'x-id-field'} // 'id';
Condition50%2/4
$join_key_field //= $join_schema->{"x-id-field"} // "id"
l!l
03
$join_schema->{"x-id-field"} // "id"
l!l
30
MC/DC0%0/1
$join_key_field //= $join_schema->{"x-id-field"} // "id"
$join_key_field
Truth table50%1/2
A
A= $join_key_field
Aresult
11
00
145 # Find the one foreign item
146 3 my $res = $self->list( $join_schema_name, { %$join_where, $join_key_field => $item->{ $join_prop_name } } );
147 3 ▶ return if keys %$join_where && !$res->{total};
Branch: 1 branch
BranchTrueFalseTotal
if keys %$join_where and not $res->{"total"} 0 3 3
Condition33%1/3
keys %$join_where and not $res->{"total"}
!ll&&!rl&&r
300
MC/DC0%0/2
keys %$join_where and not $res->{"total"}
keys %$join_where not $res->{"total"}
Truth table33%1/3
A and not(B)
A= keys %$join_whereB= not $res->{"total"}
ABresult
0X0
100
111
148 3 $item->{ $join } = $res->{items}[0];
149 }
150 # Otherwise, try to find the foreign key on the foreign schema
151 20 ▶ elsif ( ( $join_prop_name ) = grep { ($join_props->{ $_ }{ 'x-foreign-key' }//'') =~ /^$schema_name(\.|$)/ } keys %$join_props ) {
Condition100%2/2
$join_props->{$_}{"x-foreign-key"} // ""
l!l
1010
MC/DC100%1/1
$join_props->{$_}{"x-foreign-key"} // ""
$join_props->{$_}{"x-foreign-key"}
Truth table100%2/2
A
A= $join_props->{$_}{"x-foreign-key"}
Aresult
11
00
152 5 my $join_prop = $join_props->{ $join_prop_name };
153 5 my $join_key_field;
154 5 ▶ if ( $join_prop->{'x-foreign-key'} =~ /\.(.+)$/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($join_prop->{'x-foreign-key'} =~ /\.(.+)$/u) { } 0 5 5
155 0 $join_key_field = $1;
156 }
157 else {
158 5 ▶ $join_key_field = $schema->{'x-id-field'} // 'id';
Condition50%1/2
$schema->{"x-id-field"} // "id"
l!l
50
MC/DC0%0/1
$schema->{"x-id-field"} // "id"
$schema->{"x-id-field"}
Truth table50%1/2
A
A= $schema->{"x-id-field"}
Aresult
11
00
159 }
160 # Find the list of foreign items
161 5 my $res = $self->list( $join_schema_name, { %$join_where, $join_prop_name => $item->{ $join_key_field } } );
162 5 ▶ return if keys %$join_where && !$res->{total};
Branch: 1 branch
BranchTrueFalseTotal
if keys %$join_where and not $res->{"total"} 1 4 5
Condition100%3/3
keys %$join_where and not $res->{"total"}
!ll&&!rl&&r
311
MC/DC100%2/2
keys %$join_where and not $res->{"total"}
keys %$join_where not $res->{"total"}
Truth table100%3/3
A and not(B)
A= keys %$join_whereB= not $res->{"total"}
ABresult
0X0
100
111
163 4 $item->{ $join } = $res->{items};
164 }
165 else {
166 0 die "Could not join $schema_name to $join: No x-foreign-key property found";
167 }
168 }
169 }
170 18 return $item;
171 }
172  
173 630 ▶sub _viewise {
Subroutine _viewise: called CC=8 SCAR=20.8
174 630 my ( $self, $schema_name, $item, $join ) = @_;
175 630 $item = dclone $item;
176 630 my $schema = $self->schema->{ $schema_name };
177 630 ▶ my $real_coll = ( $schema->{'x-view'} || {} )->{schema} // $schema_name;
Condition80%4/5
+($schema->{"x-view"} || {})->{"schema"} // $schema_name
l!l&&r!l&&!r
36270
$schema->{'x-view'} || {}
l!l
3627
MC/DC0%0/2
+($schema->{"x-view"} || {})->{"schema"} // $schema_name
$schema->{'x-view'} $schema_name
Truth table66%2/3
A // B
A= $schema->{'x-view'}B= $schema_name
ABresult
1X1
011
000
178 my %props = %{
179 630 $schema->{properties} || $self->schema->{ $real_coll }{properties}
180 630 ▶ };
Condition66%2/3
$schema->{'properties'} || $self->schema->{$real_coll}{'properties'}
l!l&&r!l&&!r
62910
MC/DC0%0/2
$schema->{'properties'} || $self->schema->{$real_coll}{'properties'}
$schema->{'properties'} $self->schema->{$real_coll}{'properties'}
Truth table66%2/3
A || B
A= $schema->{'properties'}B= $self->schema->{$real_coll}{'properties'}
ABresult
1X1
011
000
181 630 ▶ if ( $join ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($join) 13 617 630
182 13 ▶ $props{ $_ } = 1 for @{ ref $join eq 'ARRAY' ? $join : [ $join ] };
Branch: 1 branch
BranchTrueFalseTotal
ref $join eq 'ARRAY' ? : 4 9 13
183 }
184 630 delete $item->{$_} for grep !$props{ $_ }, keys %$item;
185 630 $item;
186 }
187  
188 336 ▶sub list {
Subroutine list: called CC=18 SCAR=29.0
Pod undocumented
189 336 my ( $self, $schema_name, $params, @opt ) = @_;
190 336 ▶ my $opt = @opt % 2 == 0 ? {@opt} : $opt[0];
Branch: 1 branch
BranchTrueFalseTotal
@opt % 2 == 0 ? : 160 176 336
191 336 my $schema = $self->schema->{ $schema_name };
192 336 ▶ die "list attempted on non-existent schema '$schema_name'" unless $schema;
Branch: 1 branch
BranchTrueFalseTotal
unless $schema 0 336 336
193 336 ▶ $params ||= {};
Condition100%2/2
$params ||= {}
l!l
212124
MC/DC100%1/1
$params ||= {}
$params
Truth table100%2/2
A
A= $params
Aresult
11
00
194  
195 336 ▶ my $id_field = $self->schema->{ $schema_name }{ 'x-id-field' } || 'id';
Condition100%2/2
$self->schema->{$schema_name}{'x-id-field'} || 'id'
l!l
181155
MC/DC100%1/1
$self->schema->{$schema_name}{'x-id-field'} || 'id'
$self->schema->{$schema_name}{'x-id-field'}
Truth table100%2/2
A
A= $self->schema->{$schema_name}{'x-id-field'}
Aresult
11
00
196 336 ▶ my @id_fields = ref $id_field eq 'ARRAY' ? @$id_field : ( $id_field );
Branch: 1 branch
BranchTrueFalseTotal
ref $id_field eq 'ARRAY' ? : 9 327 336
197  
198 336 ▶ my $real_coll = ( $schema->{'x-view'} || {} )->{schema} // $schema_name;
Condition80%4/5
+($schema->{"x-view"} || {})->{"schema"} // $schema_name
l!l&&r!l&&!r
13350
$schema->{'x-view'} || {}
l!l
1335
MC/DC0%0/2
+($schema->{"x-view"} || {})->{"schema"} // $schema_name
$schema->{'x-view'} $schema_name
Truth table66%2/3
A // B
A= $schema->{'x-view'}B= $schema_name
ABresult
1X1
011
000
199 my $props = $schema->{properties}
200 336 ▶ || $self->schema->{ $real_coll }{properties};
Condition33%1/3
$schema->{'properties'} || $self->schema->{$real_coll}{'properties'}
l!l&&r!l&&!r
33600
MC/DC0%0/2
$schema->{'properties'} || $self->schema->{$real_coll}{'properties'}
$schema->{'properties'} $self->schema->{$real_coll}{'properties'}
Truth table33%1/3
A || B
A= $schema->{'properties'}B= $self->schema->{$real_coll}{'properties'}
ABresult
1X1
011
000
201 336 my @rows = values %{ $DATA{ $real_coll } };
202 336 for my $id_field ( 1..$#id_fields ) {
203 9 @rows = map values %$_, @rows;
204 }
205 336 ▶ if ( $opt->{join} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opt->{"join"}) 7 329 336
206 7 @rows = map $self->_join( $schema_name, $_, $opt->{join}, $params ), @rows;
207 }
208 # Join queries have been resolved
209 336 ▶ for my $p ( ref $params eq 'ARRAY' ? @$params : ( $params ) ) {
Branch: 1 branch
BranchTrueFalseTotal
ref $params eq 'ARRAY' ? : 1 335 336
210 337 for my $key ( grep /\./, keys %$p ) {
211 4 delete $p->{ $key };
212 4 my ( $j ) = split /\./, $key;
213 4 $p->{ $j } = { '!=' => undef };
214 }
215 }
216 my $matched_rows = order_by(
217 $opt->{order_by} // \@id_fields,
218 336 ▶ [ grep { match( $params, $_ ) } @rows ],
Condition100%2/2
$opt->{"order_by"} // \@id_fields
l!l
41295
MC/DC100%1/1
$opt->{"order_by"} // \@id_fields
$opt->{"order_by"}
Truth table100%2/2
A
A= $opt->{"order_by"}
Aresult
11
00
219 );
220 336 ▶ my $first = $opt->{offset} // 0;
Condition100%2/2
$opt->{"offset"} // 0
l!l
78258
MC/DC100%1/1
$opt->{"offset"} // 0
$opt->{"offset"}
Truth table100%2/2
A
A= $opt->{"offset"}
Aresult
11
00
221 336 ▶ my $last = $opt->{limit} ? $opt->{limit} + $first - 1 : $#$matched_rows;
Branch: 1 branch
BranchTrueFalseTotal
$opt->{'limit'} ? : 131 205 336
222 336 ▶ if ( $last > $#$matched_rows ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($last > $matched_rows->$#*) 58 278 336
223 58 $last = $#$matched_rows;
224 }
225 336 my @items = map $self->_viewise( $schema_name, $_, $opt->{join} ), @$matched_rows[ $first .. $last ];
226 336 my $retval = {
227 items => \@items,
228 total => scalar @$matched_rows,
229 };
230 #; use Data::Dumper;
231 #; say Dumper $retval;
232 336 return $retval;
233 }
234  
235 58 ▶sub set {
Subroutine set: called CC=14 SCAR=26.4
Pod undocumented
236 58 my ( $self, $schema_name, $id, $params ) = @_;
237 58 ▶ my $id_field = $self->schema->{ $schema_name }{ 'x-id-field' } || 'id';
Condition100%2/2
$self->schema->{$schema_name}{'x-id-field'} || 'id'
l!l
2929
MC/DC100%1/1
$self->schema->{$schema_name}{'x-id-field'} || 'id'
$self->schema->{$schema_name}{'x-id-field'}
Truth table100%2/2
A
A= $self->schema->{$schema_name}{'x-id-field'}
Aresult
11
00
238 58 ▶ my @id_fields = ref $id_field eq 'ARRAY' ? @$id_field : ( $id_field );
Branch: 1 branch
BranchTrueFalseTotal
ref $id_field eq 'ARRAY' ? : 4 54 58
239 58 ▶ die "Missing composite ID parts" if @id_fields > 1 && ( !ref $id || keys %$id < @id_fields );
Branch: 1 branch
BranchTrueFalseTotal
if @id_fields > 1 and !ref($id) || keys %$id < @id_fields 2 56 58
Condition100%6/6
@id_fields > 1 and !ref($id) || keys %$id < @id_fields
!ll&&!rl&&r
5422
not ref $id or keys %$id < @id_fields
l!l&&r!l&&!r
112
MC/DC100%3/3
@id_fields > 1 and !ref($id) || keys %$id < @id_fields
@id_fields > 1 not ref $id keys %$id < @id_fields
Truth table100%4/4
A and not(B) or C
A= @id_fields > 1B= not ref $idC= keys %$id < @id_fields
ABCresult
0XX0
1000
11X1
1011
240  
241 # Fill in any missing params from the ID
242 56 for my $id_field ( @id_fields ) {
243 58 ▶ my $id_part = ref $id eq 'HASH' ? $id->{ $id_field } : $id;
Branch: 1 branch
BranchTrueFalseTotal
ref $id eq 'HASH' ? : 4 54 58
244 58 ▶ if ( !$params->{ $id_field } ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($params->{$id_field}) 34 24 58
245 34 $params->{ $id_field } = $id_part;
246 }
247 }
248  
249 56 $params = $self->_normalize( $schema_name, $params );
250  
251 56 my $store = $DATA{ $schema_name };
252 56 for my $i ( 0..$#id_fields-1 ) {
253 2 my $id_field = $id_fields[ $i ];
254 2 ▶ my $id_part = ref $id eq 'HASH' ? $id->{ $id_field } : $id;
Branch: 1 branch
BranchTrueFalseTotal
ref $id eq 'HASH' ? : 2 0 2
255 2 ▶ return 0 if !$store->{ $id_part };
Branch: 1 branch
BranchTrueFalseTotal
unless $store->{$id_part} 0 2 2
256 # Update the item's ID if it changes
257 2 my $item = delete $store->{ $id_part };
258 2 $store->{ $params->{ $id_field } } = $item;
259 2 $store = $item;
260 }
261 56 ▶ my $id_part = ref $id eq 'HASH' ? $id->{ $id_fields[-1] } : $id;
Branch: 1 branch
BranchTrueFalseTotal
ref $id eq 'HASH' ? : 2 54 56
262 56 ▶ return 0 if !$store->{ $id_part };
Branch: 1 branch
BranchTrueFalseTotal
unless $store->{$id_part} 10 46 56
263 $store->{ $params->{ $id_fields[-1] } } = {
264 46 %{ delete $store->{ $id_part } },
265 %$params,
266 };
267  
268 46 return 1;
269 }
270  
271 94 ▶sub delete {
Subroutine delete: called CC=12 SCAR=24.9
Pod undocumented
272 94 my ( $self, $schema_name, $id ) = @_;
273 94 ▶ return 0 if !$id;
Branch: 1 branch
BranchTrueFalseTotal
unless $id 0 94 94
274 94 ▶ my $id_field = $self->schema->{ $schema_name }{ 'x-id-field' } || 'id';
Condition100%2/2
$self->schema->{$schema_name}{'x-id-field'} || 'id'
l!l
5044
MC/DC100%1/1
$self->schema->{$schema_name}{'x-id-field'} || 'id'
$self->schema->{$schema_name}{'x-id-field'}
Truth table100%2/2
A
A= $self->schema->{$schema_name}{'x-id-field'}
Aresult
11
00
275 94 ▶ my @id_fields = ref $id_field eq 'ARRAY' ? @$id_field : ( $id_field );
Branch: 1 branch
BranchTrueFalseTotal
ref $id_field eq 'ARRAY' ? : 4 90 94
276 94 ▶ die "Missing composite ID parts" if @id_fields > 1 && ( !ref $id || keys %$id < @id_fields );
Branch: 1 branch
BranchTrueFalseTotal
if @id_fields > 1 and !ref($id) || keys %$id < @id_fields 2 92 94
Condition100%6/6
@id_fields > 1 and !ref($id) || keys %$id < @id_fields
!ll&&!rl&&r
9022
not ref $id or keys %$id < @id_fields
l!l&&r!l&&!r
112
MC/DC100%3/3
@id_fields > 1 and !ref($id) || keys %$id < @id_fields
@id_fields > 1 not ref $id keys %$id < @id_fields
Truth table100%4/4
A and not(B) or C
A= @id_fields > 1B= not ref $idC= keys %$id < @id_fields
ABCresult
0XX0
1000
11X1
1011
277 92 my $store = $DATA{ $schema_name };
278 92 for my $i ( 0..$#id_fields-1 ) {
279 2 my $id_field = $id_fields[ $i ];
280 2 ▶ my $id_part = ref $id eq 'HASH' ? $id->{ $id_field } : $id;
Branch: 1 branch
BranchTrueFalseTotal
ref $id eq 'HASH' ? : 2 0 2
281 2 ▶ $store = $store->{ $id_part } // return 0;
Condition50%1/2
$store->{$id_part} // (return 0)
l!l
20
MC/DC0%0/1
$store->{$id_part} // (return 0)
$store->{$id_part}
Truth table50%1/2
A
A= $store->{$id_part}
Aresult
11
00
282 }
283 92 ▶ my $id_part = ref $id eq 'HASH' ? $id->{ $id_fields[-1] } : $id;
Branch: 1 branch
BranchTrueFalseTotal
ref $id eq 'HASH' ? : 2 90 92
284 92 ▶ return 0 if !$store->{ $id_part };
Branch: 1 branch
BranchTrueFalseTotal
unless $store->{$id_part} 22 70 92
285 70 return !!delete $store->{ $id_part };
286 }
287  
288 172 ▶sub _normalize {
Subroutine _normalize: called CC=10 SCAR=23.0
289 172 my ( $self, $schema_name, $data ) = @_;
290 172 ▶ return undef if !$data;
Branch: 1 branch
BranchTrueFalseTotal
unless $data 0 172 172
291 172 my $schema = $self->schema->{ $schema_name }{ properties };
292 172 my %replace;
293 172 for my $key ( keys %$data ) {
294 1044 ▶ next if !defined $data->{ $key }; # leave nulls alone
Branch: 1 branch
BranchTrueFalseTotal
unless defined $data->{$key} 219 825 1044
295 825 my ( $type, $format ) = @{ $schema->{ $key } }{qw( type format )};
296 825 ▶ if ( is_type( $type, 'boolean' ) ) {
Branch: 2 branches
BranchTrueFalseTotal
if (is_type($type, 'boolean')) { } 56 769 825
elsif (is_type($type, 'string') and is_format($format, 'date-time')) { } 70 808 878
297 # Boolean: true (1, "true"), false (0, "false")
298 $replace{ $key }
299 56 ▶ = $data->{ $key } && $data->{ $key } !~ /^false$/i
Branch: 1 branch
BranchTrueFalseTotal
$data->{$key} && !($data->{$key} =~ /^false$/iu) ? : 24 32 56
Condition100%3/3
$data->{$key} and not $data->{$key} =~ /^false$/iu
!ll&&!rl&&r
30224
MC/DC100%2/2
$data->{$key} and not $data->{$key} =~ /^false$/iu
$data->{$key} not $data->{$key} =~ /^false$/iu
Truth table100%3/3
A and not(B)
A= $data->{$key}B= not $data->{$key} =~ /^false$/iu
ABresult
0X0
100
111
300 ? 1 : 0;
301 }
302 ▶ elsif ( is_type( $type, 'string' ) && is_format( $format, 'date-time' ) ) {
Condition100%3/3
is_type($type, "string") and is_format($format, "date-time")
!ll&&!rl&&r
10959070
MC/DC100%2/2
is_type($type, "string") and is_format($format, "date-time")
is_type($type, "string") is_format($format, "date-time")
Truth table100%3/3
A and B
A= is_type($type, "string")B= is_format($format, "date-time")
ABresult
0X0
100
111
303 70 ▶ if ( $data->{ $key } eq 'now' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($data->{$key} eq "now") 57 13 70
304 57 $replace{ $key } = Time::Piece->new->datetime;
305 }
306 }
307 }
308 172 +{ %$data, %replace };
309 }
310  
311 # Some databases can know other formats
312 23 my %db_formats = map { $_ => 1 } qw( date time date-time binary );
313  
314 106 ▶sub read_schema {
Subroutine read_schema: called CC=7 SCAR=19.5
Pod undocumented
315 106 my ( $self, @table_names ) = @_;
316 106 ▶ my $schema = %Local::Test::SCHEMA ? \%Local::Test::SCHEMA : $self->schema;
Branch: 1 branch
BranchTrueFalseTotal
%Local::Test::SCHEMA ? : 106 0 106
317 106 my $cloned = dclone $schema;
318 106 delete @$cloned{@Local::Test::SCHEMA_ADDED_COLLS}; # ones not in the "database" at all
319 # zap all things that DB can't know about
320 106 for my $c ( values %$cloned ) {
321 545 delete $c->{'x-list-columns'};
322 545 for my $p ( values %{ $c->{properties} } ) {
323 3150 delete @$p{ qw(description pattern title) };
324 3150 ▶ if ( $p->{format} && !$db_formats{ $p->{format} } ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($p->{"format"} and not $db_formats{$p->{"format"}}) 413 2737 3150
Condition100%3/3
$p->{"format"} and not $db_formats{$p->{"format"}}
!ll&&!rl&&r
2512225413
MC/DC100%2/2
$p->{"format"} and not $db_formats{$p->{"format"}}
$p->{"format"} not $db_formats{$p->{"format"}}
Truth table100%3/3
A and not(B)
A= $p->{"format"}B= not $db_formats{$p->{"format"}}
ABresult
0X0
100
111
325 413 delete $p->{format};
326 }
327 }
328 }
329 106 ▶ return @table_names ? @$cloned{ @table_names } : $cloned;
Branch: 1 branch
BranchTrueFalseTotal
@table_names ? : 7 99 106
330 }
331  
332 1 ▶sub supports { grep { $_[1] eq $_ } 'complex-type' }
Subroutine supports: called CC=1 SCAR=0.0
Pod undocumented
333  
334 23 1;
335  
336 __END__
« Yancy/Plugin/Form/Bootstrap4.pm ↑ Summary Yancy/Model/Schema.pm »