| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 29 | 84 | my ( $path ) = $url =~ m{^[^:]+://[^/]+(?:/(.+))?$}; | |||||||||||||||||||||||||||||||||||||||||
| 30 | 84 | ▶ | if ( $path ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 31 | 0 | ▶ | %DATA = %{ from_json( path( ( $ENV{MOJO_HOME} || () ), $path )->slurp ) }; | ||||||||||||||||||||||||||||||||||||||||
|
Condition0%0/3
$ENV{'MOJO_HOME'} || ()
MC/DC0%0/2
$ENV{'MOJO_HOME'} || ()
$ENV{'MOJO_HOME'}
()
Truth table0%0/3
A || B
A= $ENV{'MOJO_HOME'}B= ()
| |||||||||||||||||||||||||||||||||||||||||||
| 32 | } | ||||||||||||||||||||||||||||||||||||||||||
| 33 | } | ||||||||||||||||||||||||||||||||||||||||||
| 34 | 85 | ▶ | $schema //= \%Local::Test::SCHEMA; | ||||||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
$schema //= \%Local::Test::SCHEMA
MC/DC100%1/1
$schema //= \%Local::Test::SCHEMA
$schema
Truth table100%2/2
A
A= $schema
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 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
MC/DC100%1/1
$props->{$_}{"default"} // undef
$props->{$_}{"default"}
Truth table100%2/2
A
A= $props->{$_}{"default"}
| |||||||||||||||||||||||||||||||||||||||||||
| 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'
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'}
| |||||||||||||||||||||||||||||||||||||||||||
| 59 | 116 | ▶ | my @id_fields = ref $id_field eq 'ARRAY' ? @$id_field : ( $id_field ); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 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
Condition66%2/3
not $params->{$id_field} and $self->schema->{$schema_name}{"properties"}{$id_field}{"type"} eq "integer"
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"
| |||||||||||||||||||||||||||||||||||||||||||
| 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
MC/DC100%1/1
&max(@existing_ids) // 0
&max(@existing_ids)
Truth table100%2/2
A
A= &max(@existing_ids)
| |||||||||||||||||||||||||||||||||||||||||||
| 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"}
$params->{$id_field} and $id_field ne "id"
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"}
| |||||||||||||||||||||||||||||||||||||||||||
| 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
MC/DC100%1/1
&max(@existing_ids) // 0
&max(@existing_ids)
Truth table100%2/2
A
A= &max(@existing_ids)
| |||||||||||||||||||||||||||||||||||||||||||
| 75 | } | ||||||||||||||||||||||||||||||||||||||||||
| 76 | } | ||||||||||||||||||||||||||||||||||||||||||
| 77 | |||||||||||||||||||||||||||||||||||||||||||
| 78 | 116 | ▶ | my $store = $DATA{ $schema_name } //= {}; | ||||||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
$Yancy::Backend::Memory::DATA{$schema_name} //= {}
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}
| |||||||||||||||||||||||||||||||||||||||||||
| 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]}} //= {}
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]}}
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 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
$schema->{'x-view'} || {}
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
| |||||||||||||||||||||||||||||||||||||||||||
| 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'
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'}
| |||||||||||||||||||||||||||||||||||||||||||
| 93 | 326 | ▶ | my @ids = ref $id_field eq 'ARRAY' ? map { $id->{ $_ } } @$id_field : ( $id ); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 94 | 325 | ▶ | die "Missing composite ID parts" if @ids > 1 && ( !ref $id || keys %$id < @ids ); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition83%5/6
@ids > 1 and !ref($id) || keys %$id < @ids
not ref $id or keys %$id < @ids
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
| |||||||||||||||||||||||||||||||||||||||||||
| 95 | |||||||||||||||||||||||||||||||||||||||||||
| 96 | 324 | my $item = $DATA{ $real_coll }; | |||||||||||||||||||||||||||||||||||||||||
| 97 | 324 | for my $id ( @ids ) { | |||||||||||||||||||||||||||||||||||||||||
| 98 | 335 | ▶ | return undef if !defined $id; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 99 | 332 | ▶ | $item = $item->{ $id } // return undef; | ||||||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
$item->{$id} // (return undef)
MC/DC100%1/1
$item->{$id} // (return undef)
$item->{$id}
Truth table100%2/2
A
A= $item->{$id}
| |||||||||||||||||||||||||||||||||||||||||||
| 100 | } | ||||||||||||||||||||||||||||||||||||||||||
| 101 | |||||||||||||||||||||||||||||||||||||||||||
| 102 | 280 | $item = $self->_viewise( $schema_name, $item ); | |||||||||||||||||||||||||||||||||||||||||
| 103 | 280 | ▶ | if ( my $join = $opt{join} ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 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'
MC/DC0%0/1
$schema->{'x-id-field'} || 'id'
$schema->{'x-id-field'}
Truth table50%1/2
A
A= $schema->{'x-id-field'}
| |||||||||||||||||||||||||||||||||||||||||||
| 116 | 21 | ▶ | my @joins = ref $join eq 'ARRAY' ? @$join : ( $join ); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 117 | 21 | for my $join ( @joins ) { | |||||||||||||||||||||||||||||||||||||||||
| 118 | 29 | ▶ | if ( my $join_prop = $schema->{ properties }{ $join } ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||
| 119 | 21 | ▶ | my $join_id = $item->{ $join } || next; | ||||||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
$item->{$join} || (next)
MC/DC100%1/1
$item->{$join} || (next)
$item->{$join}
Truth table100%2/2
A
A= $item->{$join}
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 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
Condition100%2/2
$props{$_}{"x-foreign-key"} // ""
MC/DC100%1/1
$props{$_}{"x-foreign-key"} // ""
$props{$_}{"x-foreign-key"}
Truth table100%2/2
A
A= $props{$_}{"x-foreign-key"}
| |||||||||||||||||||||||||||||||||||||||||||
| 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"
$join_schema->{"x-id-field"} // "id"
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
| |||||||||||||||||||||||||||||||||||||||||||
| 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
Condition33%1/3
keys %$join_where and not $res->{"total"}
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"}
| |||||||||||||||||||||||||||||||||||||||||||
| 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"} // ""
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"}
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 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"
MC/DC0%0/1
$schema->{"x-id-field"} // "id"
$schema->{"x-id-field"}
Truth table50%1/2
A
A= $schema->{"x-id-field"}
| |||||||||||||||||||||||||||||||||||||||||||
| 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
Condition100%3/3
keys %$join_where and not $res->{"total"}
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"}
| |||||||||||||||||||||||||||||||||||||||||||
| 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
$schema->{'x-view'} || {}
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
| |||||||||||||||||||||||||||||||||||||||||||
| 178 | my %props = %{ | ||||||||||||||||||||||||||||||||||||||||||
| 179 | 630 | $schema->{properties} || $self->schema->{ $real_coll }{properties} | |||||||||||||||||||||||||||||||||||||||||
| 180 | 630 | ▶ | }; | ||||||||||||||||||||||||||||||||||||||||
|
Condition66%2/3
$schema->{'properties'} || $self->schema->{$real_coll}{'properties'}
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'}
| |||||||||||||||||||||||||||||||||||||||||||
| 181 | 630 | ▶ | if ( $join ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 182 | 13 | ▶ | $props{ $_ } = 1 for @{ ref $join eq 'ARRAY' ? $join : [ $join ] }; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 191 | 336 | my $schema = $self->schema->{ $schema_name }; | |||||||||||||||||||||||||||||||||||||||||
| 192 | 336 | ▶ | die "list attempted on non-existent schema '$schema_name'" unless $schema; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 193 | 336 | ▶ | $params ||= {}; | ||||||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
$params ||= {}
MC/DC100%1/1
$params ||= {}
$params
Truth table100%2/2
A
A= $params
| |||||||||||||||||||||||||||||||||||||||||||
| 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'
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'}
| |||||||||||||||||||||||||||||||||||||||||||
| 196 | 336 | ▶ | my @id_fields = ref $id_field eq 'ARRAY' ? @$id_field : ( $id_field ); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 197 | |||||||||||||||||||||||||||||||||||||||||||
| 198 | 336 | ▶ | my $real_coll = ( $schema->{'x-view'} || {} )->{schema} // $schema_name; | ||||||||||||||||||||||||||||||||||||||||
|
Condition80%4/5
+($schema->{"x-view"} || {})->{"schema"} // $schema_name
$schema->{'x-view'} || {}
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
| |||||||||||||||||||||||||||||||||||||||||||
| 199 | my $props = $schema->{properties} | ||||||||||||||||||||||||||||||||||||||||||
| 200 | 336 | ▶ | || $self->schema->{ $real_coll }{properties}; | ||||||||||||||||||||||||||||||||||||||||
|
Condition33%1/3
$schema->{'properties'} || $self->schema->{$real_coll}{'properties'}
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'}
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 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
MC/DC100%1/1
$opt->{"order_by"} // \@id_fields
$opt->{"order_by"}
Truth table100%2/2
A
A= $opt->{"order_by"}
| |||||||||||||||||||||||||||||||||||||||||||
| 219 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 220 | 336 | ▶ | my $first = $opt->{offset} // 0; | ||||||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
$opt->{"offset"} // 0
MC/DC100%1/1
$opt->{"offset"} // 0
$opt->{"offset"}
Truth table100%2/2
A
A= $opt->{"offset"}
| |||||||||||||||||||||||||||||||||||||||||||
| 221 | 336 | ▶ | my $last = $opt->{limit} ? $opt->{limit} + $first - 1 : $#$matched_rows; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 222 | 336 | ▶ | if ( $last > $#$matched_rows ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 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'
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'}
| |||||||||||||||||||||||||||||||||||||||||||
| 238 | 58 | ▶ | my @id_fields = ref $id_field eq 'ARRAY' ? @$id_field : ( $id_field ); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 239 | 58 | ▶ | die "Missing composite ID parts" if @id_fields > 1 && ( !ref $id || keys %$id < @id_fields ); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%6/6
@id_fields > 1 and !ref($id) || keys %$id < @id_fields
not ref $id or keys %$id < @id_fields
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
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 244 | 58 | ▶ | if ( !$params->{ $id_field } ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 255 | 2 | ▶ | return 0 if !$store->{ $id_part }; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 262 | 56 | ▶ | return 0 if !$store->{ $id_part }; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 274 | 94 | ▶ | my $id_field = $self->schema->{ $schema_name }{ 'x-id-field' } || 'id'; | ||||||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
$self->schema->{$schema_name}{'x-id-field'} || 'id'
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'}
| |||||||||||||||||||||||||||||||||||||||||||
| 275 | 94 | ▶ | my @id_fields = ref $id_field eq 'ARRAY' ? @$id_field : ( $id_field ); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 276 | 94 | ▶ | die "Missing composite ID parts" if @id_fields > 1 && ( !ref $id || keys %$id < @id_fields ); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%6/6
@id_fields > 1 and !ref($id) || keys %$id < @id_fields
not ref $id or keys %$id < @id_fields
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
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 281 | 2 | ▶ | $store = $store->{ $id_part } // return 0; | ||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$store->{$id_part} // (return 0)
MC/DC0%0/1
$store->{$id_part} // (return 0)
$store->{$id_part}
Truth table50%1/2
A
A= $store->{$id_part}
| |||||||||||||||||||||||||||||||||||||||||||
| 282 | } | ||||||||||||||||||||||||||||||||||||||||||
| 283 | 92 | ▶ | my $id_part = ref $id eq 'HASH' ? $id->{ $id_fields[-1] } : $id; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 284 | 92 | ▶ | return 0 if !$store->{ $id_part }; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 295 | 825 | my ( $type, $format ) = @{ $schema->{ $key } }{qw( type format )}; | |||||||||||||||||||||||||||||||||||||||||
| 296 | 825 | ▶ | if ( is_type( $type, 'boolean' ) ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||
| 297 | # Boolean: true (1, "true"), false (0, "false") | ||||||||||||||||||||||||||||||||||||||||||
| 298 | $replace{ $key } | ||||||||||||||||||||||||||||||||||||||||||
| 299 | 56 | ▶ | = $data->{ $key } && $data->{ $key } !~ /^false$/i | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$data->{$key} and not $data->{$key} =~ /^false$/iu
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
| |||||||||||||||||||||||||||||||||||||||||||
| 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")
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")
| |||||||||||||||||||||||||||||||||||||||||||
| 303 | 70 | ▶ | if ( $data->{ $key } eq 'now' ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 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
Condition100%3/3
$p->{"format"} and not $db_formats{$p->{"format"}}
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"}}
| |||||||||||||||||||||||||||||||||||||||||||
| 325 | 413 | delete $p->{format}; | |||||||||||||||||||||||||||||||||||||||||
| 326 | } | ||||||||||||||||||||||||||||||||||||||||||
| 327 | } | ||||||||||||||||||||||||||||||||||||||||||
| 328 | } | ||||||||||||||||||||||||||||||||||||||||||
| 329 | 106 | ▶ | return @table_names ? @$cloned{ @table_names } : $cloned; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 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__ | ||||||||||||||||||||||||||||||||||||||||||