| 1 | package Yancy::Model::Schema; | ||||||||||||||||||||||||||||||||||||||||||
| 2 | 21 | our $VERSION = '1.088'; | |||||||||||||||||||||||||||||||||||||||||
| 3 | # ABSTRACT: Interface to a single schema | ||||||||||||||||||||||||||||||||||||||||||
| 4 | |||||||||||||||||||||||||||||||||||||||||||
| 5 | #pod =head1 SYNOPSIS | ||||||||||||||||||||||||||||||||||||||||||
| 6 | #pod | ||||||||||||||||||||||||||||||||||||||||||
| 7 | #pod my $schema = $app->model->schema( 'foo' ); | ||||||||||||||||||||||||||||||||||||||||||
| 8 | #pod | ||||||||||||||||||||||||||||||||||||||||||
| 9 | #pod my $id = $schema->create( $data ); | ||||||||||||||||||||||||||||||||||||||||||
| 10 | #pod my $item = $schema->get( $id ); | ||||||||||||||||||||||||||||||||||||||||||
| 11 | #pod my $count = $schema->delete( $id ); | ||||||||||||||||||||||||||||||||||||||||||
| 12 | #pod my $count = $schema->delete( $where ); | ||||||||||||||||||||||||||||||||||||||||||
| 13 | #pod my $count = $schema->set( $id, $data ); | ||||||||||||||||||||||||||||||||||||||||||
| 14 | #pod my $count = $schema->set( $where, $data ); | ||||||||||||||||||||||||||||||||||||||||||
| 15 | #pod | ||||||||||||||||||||||||||||||||||||||||||
| 16 | #pod my $res = $schema->list( $where, $opts ); | ||||||||||||||||||||||||||||||||||||||||||
| 17 | #pod for my $item ( @{ $res->{items} } ) { ... } | ||||||||||||||||||||||||||||||||||||||||||
| 18 | #pod | ||||||||||||||||||||||||||||||||||||||||||
| 19 | #pod =head1 DESCRIPTION | ||||||||||||||||||||||||||||||||||||||||||
| 20 | #pod | ||||||||||||||||||||||||||||||||||||||||||
| 21 | #pod B<NOTE>: This module is experimental and its API may change before | ||||||||||||||||||||||||||||||||||||||||||
| 22 | #pod Yancy v2! | ||||||||||||||||||||||||||||||||||||||||||
| 23 | #pod | ||||||||||||||||||||||||||||||||||||||||||
| 24 | #pod For information on how to extend this module to add your own schema | ||||||||||||||||||||||||||||||||||||||||||
| 25 | #pod and item methods, see L<Yancy::Guides::Model>. | ||||||||||||||||||||||||||||||||||||||||||
| 26 | #pod | ||||||||||||||||||||||||||||||||||||||||||
| 27 | #pod =head1 SEE ALSO | ||||||||||||||||||||||||||||||||||||||||||
| 28 | #pod | ||||||||||||||||||||||||||||||||||||||||||
| 29 | #pod L<Yancy::Guides::Model>, L<Yancy::Model> | ||||||||||||||||||||||||||||||||||||||||||
| 30 | #pod | ||||||||||||||||||||||||||||||||||||||||||
| 31 | #pod =cut | ||||||||||||||||||||||||||||||||||||||||||
| 32 | |||||||||||||||||||||||||||||||||||||||||||
| 33 | 21 | ▶ | use Mojo::Base -base; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 34 | 21 | ▶ | use Mojo::JSON qw( true false ); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 35 | 21 | ▶ | use Yancy::Util qw( json_validator is_type derp ); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 36 | |||||||||||||||||||||||||||||||||||||||||||
| 37 | #pod =attr model | ||||||||||||||||||||||||||||||||||||||||||
| 38 | #pod | ||||||||||||||||||||||||||||||||||||||||||
| 39 | #pod The L<Yancy::Model> object that created this schema object. | ||||||||||||||||||||||||||||||||||||||||||
| 40 | #pod | ||||||||||||||||||||||||||||||||||||||||||
| 41 | #pod =cut | ||||||||||||||||||||||||||||||||||||||||||
| 42 | |||||||||||||||||||||||||||||||||||||||||||
| 43 | 0 | ▶ | has model => sub { die 'model is required' }; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=1.2
| |||||||||||||||||||||||||||||||||||||||||||
| 44 | |||||||||||||||||||||||||||||||||||||||||||
| 45 | #pod =attr name | ||||||||||||||||||||||||||||||||||||||||||
| 46 | #pod | ||||||||||||||||||||||||||||||||||||||||||
| 47 | #pod The name of the schema. | ||||||||||||||||||||||||||||||||||||||||||
| 48 | #pod | ||||||||||||||||||||||||||||||||||||||||||
| 49 | #pod =cut | ||||||||||||||||||||||||||||||||||||||||||
| 50 | |||||||||||||||||||||||||||||||||||||||||||
| 51 | 0 | ▶ | has name => sub { die 'name is required' }; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=1.2
| |||||||||||||||||||||||||||||||||||||||||||
| 52 | |||||||||||||||||||||||||||||||||||||||||||
| 53 | #pod =attr json_schema | ||||||||||||||||||||||||||||||||||||||||||
| 54 | #pod | ||||||||||||||||||||||||||||||||||||||||||
| 55 | #pod The JSON Schema for this schema. | ||||||||||||||||||||||||||||||||||||||||||
| 56 | #pod | ||||||||||||||||||||||||||||||||||||||||||
| 57 | #pod =cut | ||||||||||||||||||||||||||||||||||||||||||
| 58 | |||||||||||||||||||||||||||||||||||||||||||
| 59 | 0 | ▶ | has json_schema => sub { die 'json_schema is required' }; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=1.2
| |||||||||||||||||||||||||||||||||||||||||||
| 60 | |||||||||||||||||||||||||||||||||||||||||||
| 61 | 275 | ▶ | sub _backend { shift->model->backend }; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_backend: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 62 | 26 | ▶ | has _item_class => sub { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 63 | 26 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||
| 64 | 26 | return $self->model->find_class( Item => $self->name ); | |||||||||||||||||||||||||||||||||||||||||
| 65 | 21 | }; | |||||||||||||||||||||||||||||||||||||||||
| 66 | 8 | ▶ | sub _log { shift->model->log }; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_log: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 67 | |||||||||||||||||||||||||||||||||||||||||||
| 68 | 575 | ▶ | sub new { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
new: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 69 | 575 | my ( $class, @args ) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 70 | 575 | my $self = $class->SUPER::new( @args ); | |||||||||||||||||||||||||||||||||||||||||
| 71 | 575 | $self->_check_json_schema; | |||||||||||||||||||||||||||||||||||||||||
| 72 | 573 | return $self; | |||||||||||||||||||||||||||||||||||||||||
| 73 | } | ||||||||||||||||||||||||||||||||||||||||||
| 74 | |||||||||||||||||||||||||||||||||||||||||||
| 75 | #pod =method id_field | ||||||||||||||||||||||||||||||||||||||||||
| 76 | #pod | ||||||||||||||||||||||||||||||||||||||||||
| 77 | #pod The ID field for this schema. Either a single string, or an arrayref of | ||||||||||||||||||||||||||||||||||||||||||
| 78 | #pod strings (for composite keys). | ||||||||||||||||||||||||||||||||||||||||||
| 79 | #pod | ||||||||||||||||||||||||||||||||||||||||||
| 80 | #pod =cut | ||||||||||||||||||||||||||||||||||||||||||
| 81 | |||||||||||||||||||||||||||||||||||||||||||
| 82 | 1359 | ▶ | sub id_field { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
id_field: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 83 | 1359 | my ( $self ) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 84 | 1359 | ▶ | return $self->json_schema->{'x-id-field'} // 'id'; | ||||||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
$self->json_schema->{"x-id-field"} // "id"
MC/DC100%1/1
$self->json_schema->{"x-id-field"} // "id"
$self->json_schema->{"x-id-field"}
Truth table100%2/2
A
A= $self->json_schema->{"x-id-field"}
| |||||||||||||||||||||||||||||||||||||||||||
| 85 | } | ||||||||||||||||||||||||||||||||||||||||||
| 86 | |||||||||||||||||||||||||||||||||||||||||||
| 87 | #pod =method build_item | ||||||||||||||||||||||||||||||||||||||||||
| 88 | #pod | ||||||||||||||||||||||||||||||||||||||||||
| 89 | #pod Turn a hashref of row data into a L<Yancy::Model::Item> object using | ||||||||||||||||||||||||||||||||||||||||||
| 90 | #pod L<Yancy::Model/find_class> to find the correct class. | ||||||||||||||||||||||||||||||||||||||||||
| 91 | #pod | ||||||||||||||||||||||||||||||||||||||||||
| 92 | #pod =cut | ||||||||||||||||||||||||||||||||||||||||||
| 93 | |||||||||||||||||||||||||||||||||||||||||||
| 94 | 235 | ▶ | sub build_item { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
build_item: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 95 | 235 | my ( $self, $data ) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 96 | 235 | return $self->_item_class->new( { data => $data, schema => $self } ); | |||||||||||||||||||||||||||||||||||||||||
| 97 | } | ||||||||||||||||||||||||||||||||||||||||||
| 98 | |||||||||||||||||||||||||||||||||||||||||||
| 99 | #pod =method validate | ||||||||||||||||||||||||||||||||||||||||||
| 100 | #pod | ||||||||||||||||||||||||||||||||||||||||||
| 101 | #pod Validate an item. Returns a list of errors (if any). | ||||||||||||||||||||||||||||||||||||||||||
| 102 | #pod | ||||||||||||||||||||||||||||||||||||||||||
| 103 | #pod =cut | ||||||||||||||||||||||||||||||||||||||||||
| 104 | |||||||||||||||||||||||||||||||||||||||||||
| 105 | 76 | ▶ | sub validate { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
validate: called CC=21 SCAR=30.5
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 106 | 76 | my ( $self, $item, %opt ) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 107 | 76 | my $schema = $self->json_schema; | |||||||||||||||||||||||||||||||||||||||||
| 108 | |||||||||||||||||||||||||||||||||||||||||||
| 109 | 76 | ▶ | if ( $opt{ properties } ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 110 | # Only validate these properties | ||||||||||||||||||||||||||||||||||||||||||
| 111 | $schema = { | ||||||||||||||||||||||||||||||||||||||||||
| 112 | type => 'object', | ||||||||||||||||||||||||||||||||||||||||||
| 113 | required => [ | ||||||||||||||||||||||||||||||||||||||||||
| 114 | 135 | ▶ | grep { my $f = $_; grep { $_ eq $f } @{ $schema->{required} || [] } } | ||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$schema->{'required'} || []
MC/DC0%0/1
$schema->{'required'} || []
$schema->{'required'}
Truth table50%1/2
A
A= $schema->{'required'}
| |||||||||||||||||||||||||||||||||||||||||||
| 115 | 35 | @{ $opt{ properties } } | |||||||||||||||||||||||||||||||||||||||||
| 116 | ], | ||||||||||||||||||||||||||||||||||||||||||
| 117 | properties => { | ||||||||||||||||||||||||||||||||||||||||||
| 118 | 134 | map { $_ => $schema->{properties}{$_} } | |||||||||||||||||||||||||||||||||||||||||
| 119 | 135 | grep { exists $schema->{properties}{$_} } | |||||||||||||||||||||||||||||||||||||||||
| 120 | 35 | @{ $opt{ properties } } | |||||||||||||||||||||||||||||||||||||||||
| 121 | }, | ||||||||||||||||||||||||||||||||||||||||||
| 122 | additionalProperties => 0, # Disallow any other properties | ||||||||||||||||||||||||||||||||||||||||||
| 123 | }; | ||||||||||||||||||||||||||||||||||||||||||
| 124 | } | ||||||||||||||||||||||||||||||||||||||||||
| 125 | |||||||||||||||||||||||||||||||||||||||||||
| 126 | 76 | my $v = json_validator(); | |||||||||||||||||||||||||||||||||||||||||
| 127 | 76 | $v->schema( $schema ); | |||||||||||||||||||||||||||||||||||||||||
| 128 | |||||||||||||||||||||||||||||||||||||||||||
| 129 | 76 | my @errors; | |||||||||||||||||||||||||||||||||||||||||
| 130 | # This is a shallow copy of the item that we will change to pass | ||||||||||||||||||||||||||||||||||||||||||
| 131 | # Yancy-specific additions to schema validation | ||||||||||||||||||||||||||||||||||||||||||
| 132 | 76 | my %check_item = %$item; | |||||||||||||||||||||||||||||||||||||||||
| 133 | 76 | for my $prop_name ( keys %{ $schema->{properties} } ) { | |||||||||||||||||||||||||||||||||||||||||
| 134 | 438 | my $prop = $schema->{properties}{ $prop_name }; | |||||||||||||||||||||||||||||||||||||||||
| 135 | |||||||||||||||||||||||||||||||||||||||||||
| 136 | # These blocks fix problems with validation only. If the | ||||||||||||||||||||||||||||||||||||||||||
| 137 | # problem is the database understanding the value, it must be | ||||||||||||||||||||||||||||||||||||||||||
| 138 | # fixed in the backend class. | ||||||||||||||||||||||||||||||||||||||||||
| 139 | |||||||||||||||||||||||||||||||||||||||||||
| 140 | # Pre-filter booleans | ||||||||||||||||||||||||||||||||||||||||||
| 141 | 438 | ▶ | if ( is_type( $prop->{type}, 'boolean' ) && defined $check_item{ $prop_name } ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
is_type($prop->{"type"}, "boolean") and defined $check_item{$prop_name}
MC/DC100%2/2
is_type($prop->{"type"}, "boolean") and defined $check_item{$prop_name}
is_type($prop->{"type"}, "boolean")
defined $check_item{$prop_name}
Truth table100%3/3
A and B
A= is_type($prop->{"type"}, "boolean")B= defined $check_item{$prop_name}
| |||||||||||||||||||||||||||||||||||||||||||
| 142 | 20 | my $value = $check_item{ $prop_name }; | |||||||||||||||||||||||||||||||||||||||||
| 143 | 20 | ▶ | if ( $value eq 'false' or !$value ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$value eq "false" or not $value
MC/DC100%2/2
$value eq "false" or not $value
$value eq "false"
not $value
Truth table100%3/3
A or not(B)
A= $value eq "false"B= not $value
| |||||||||||||||||||||||||||||||||||||||||||
| 144 | 12 | $value = false; | |||||||||||||||||||||||||||||||||||||||||
| 145 | } else { | ||||||||||||||||||||||||||||||||||||||||||
| 146 | 8 | $value = true; | |||||||||||||||||||||||||||||||||||||||||
| 147 | } | ||||||||||||||||||||||||||||||||||||||||||
| 148 | 20 | $check_item{ $prop_name } = $value; | |||||||||||||||||||||||||||||||||||||||||
| 149 | } | ||||||||||||||||||||||||||||||||||||||||||
| 150 | # An empty date-time, date, or time must become undef: The empty | ||||||||||||||||||||||||||||||||||||||||||
| 151 | # string will never pass the format check, but properties that | ||||||||||||||||||||||||||||||||||||||||||
| 152 | # are allowed to be null can be validated. | ||||||||||||||||||||||||||||||||||||||||||
| 153 | 438 | ▶ | if ( is_type( $prop->{type}, 'string' ) && $prop->{format} && $prop->{format} =~ /^(?:date-time|date|time)$/ ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%6/6
is_type($prop->{"type"}, "string") and $prop->{"format"} and $prop->{"format"} =~ /^(?:date-time|date|time)$/u
is_type($prop->{"type"}, "string") and $prop->{"format"}
MC/DC100%3/3
is_type($prop->{"type"}, "string") and $prop->{"format"} and $prop->{"format"} =~ /^(?:date-time|date|time)$/u
is_type($prop->{"type"}, "string")
$prop->{"format"}
$prop->{"format"} =~ /^(?:date-time|date|time)$/u
Truth table100%4/4
A and B and C
A= is_type($prop->{"type"}, "string")B= $prop->{"format"}C= $prop->{"format"} =~ /^(?:date-time|date|time)$/u
| |||||||||||||||||||||||||||||||||||||||||||
| 154 | 24 | ▶ | if ( exists $check_item{ $prop_name } && !$check_item{ $prop_name } ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
Condition100%3/3
exists $check_item{$prop_name} and not $check_item{$prop_name}
MC/DC100%2/2
exists $check_item{$prop_name} and not $check_item{$prop_name}
exists $check_item{$prop_name}
not $check_item{$prop_name}
Truth table100%3/3
A and not(B)
A= exists $check_item{$prop_name}B= not $check_item{$prop_name}
| |||||||||||||||||||||||||||||||||||||||||||
| 155 | 1 | $check_item{ $prop_name } = undef; | |||||||||||||||||||||||||||||||||||||||||
| 156 | } | ||||||||||||||||||||||||||||||||||||||||||
| 157 | # The "now" special value will not validate yet, but will be | ||||||||||||||||||||||||||||||||||||||||||
| 158 | # replaced by the Backend with something useful | ||||||||||||||||||||||||||||||||||||||||||
| 159 | ▶ | elsif ( ($check_item{ $prop_name }//$prop->{default}//'') eq 'now' ) { | |||||||||||||||||||||||||||||||||||||||||
|
Condition60%3/5
($check_item{$prop_name} // $prop->{"default"}) // ""
$check_item{$prop_name} // $prop->{"default"}
MC/DC0%0/2
($check_item{$prop_name} // $prop->{"default"}) // ""
$check_item{$prop_name}
$prop->{"default"}
Truth table66%2/3
A // B
A= $check_item{$prop_name}B= $prop->{"default"}
| |||||||||||||||||||||||||||||||||||||||||||
| 160 | 20 | $check_item{ $prop_name } = '2021-01-01 00:00:00'; | |||||||||||||||||||||||||||||||||||||||||
| 161 | } | ||||||||||||||||||||||||||||||||||||||||||
| 162 | } | ||||||||||||||||||||||||||||||||||||||||||
| 163 | # Always add dummy passwords to pass required checks | ||||||||||||||||||||||||||||||||||||||||||
| 164 | 438 | ▶ | if ( $prop->{format} && $prop->{format} eq 'password' && !$check_item{ $prop_name } ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition83%5/6
$prop->{"format"} and $prop->{"format"} eq "password" and not $check_item{$prop_name}
$prop->{"format"} and $prop->{"format"} eq "password"
MC/DC0%0/3
$prop->{"format"} and $prop->{"format"} eq "password" and not $check_item{$prop_name}
$prop->{"format"}
$prop->{"format"} eq "password"
not $check_item{$prop_name}
Truth table75%3/4
A and B and not(C)
A= $prop->{"format"}B= $prop->{"format"} eq "password"C= not $check_item{$prop_name}
| |||||||||||||||||||||||||||||||||||||||||||
| 165 | 0 | $check_item{ $prop_name } = '<PASSWORD>'; | |||||||||||||||||||||||||||||||||||||||||
| 166 | } | ||||||||||||||||||||||||||||||||||||||||||
| 167 | |||||||||||||||||||||||||||||||||||||||||||
| 168 | # XXX: JSON::Validator 4 moved support for readOnly/writeOnly to | ||||||||||||||||||||||||||||||||||||||||||
| 169 | # the OpenAPI schema classes, but we use JSON Schema internally, | ||||||||||||||||||||||||||||||||||||||||||
| 170 | # so we need to make support ourselves for now... | ||||||||||||||||||||||||||||||||||||||||||
| 171 | 438 | ▶ | if ( $prop->{readOnly} && exists $check_item{ $prop_name } ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$prop->{"readOnly"} and exists $check_item{$prop_name}
MC/DC100%2/2
$prop->{"readOnly"} and exists $check_item{$prop_name}
$prop->{"readOnly"}
exists $check_item{$prop_name}
Truth table100%3/3
A and B
A= $prop->{"readOnly"}B= exists $check_item{$prop_name}
| |||||||||||||||||||||||||||||||||||||||||||
| 172 | 1 | push @errors, JSON::Validator::Error->new( | |||||||||||||||||||||||||||||||||||||||||
| 173 | "/$prop_name", "Read-only.", | ||||||||||||||||||||||||||||||||||||||||||
| 174 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 175 | } | ||||||||||||||||||||||||||||||||||||||||||
| 176 | } | ||||||||||||||||||||||||||||||||||||||||||
| 177 | |||||||||||||||||||||||||||||||||||||||||||
| 178 | 76 | push @errors, $v->validate( \%check_item ); | |||||||||||||||||||||||||||||||||||||||||
| 179 | 76 | return @errors; | |||||||||||||||||||||||||||||||||||||||||
| 180 | } | ||||||||||||||||||||||||||||||||||||||||||
| 181 | |||||||||||||||||||||||||||||||||||||||||||
| 182 | #pod =method get | ||||||||||||||||||||||||||||||||||||||||||
| 183 | #pod | ||||||||||||||||||||||||||||||||||||||||||
| 184 | #pod Get an item by its ID. Returns a L<Yancy::Model::Item> object. | ||||||||||||||||||||||||||||||||||||||||||
| 185 | #pod | ||||||||||||||||||||||||||||||||||||||||||
| 186 | #pod =cut | ||||||||||||||||||||||||||||||||||||||||||
| 187 | |||||||||||||||||||||||||||||||||||||||||||
| 188 | 117 | ▶ | sub get { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
get: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 189 | 117 | my ( $self, $id, %opt ) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 190 | 117 | ▶ | return $self->build_item( $self->_backend->get( $self->name, $id, %opt ) // return undef ); | ||||||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
$self->_backend->get($self->name, $id, %opt) // (return undef)
MC/DC100%1/1
$self->_backend->get($self->name, $id, %opt) // (return undef)
$self->_backend->get($self->name, $id, %opt)
Truth table100%2/2
A
A= $self->_backend->get($self->name, $id, %opt)
| |||||||||||||||||||||||||||||||||||||||||||
| 191 | } | ||||||||||||||||||||||||||||||||||||||||||
| 192 | |||||||||||||||||||||||||||||||||||||||||||
| 193 | #pod =method list | ||||||||||||||||||||||||||||||||||||||||||
| 194 | #pod | ||||||||||||||||||||||||||||||||||||||||||
| 195 | #pod List items. Returns a hash reference with C<items> and C<total> keys. The C<items> is | ||||||||||||||||||||||||||||||||||||||||||
| 196 | #pod an array ref of L<Yancy::Model::Item> objects. C<total> is the total number of items | ||||||||||||||||||||||||||||||||||||||||||
| 197 | #pod that would be returned without any C<offset> or C<limit> options. | ||||||||||||||||||||||||||||||||||||||||||
| 198 | #pod | ||||||||||||||||||||||||||||||||||||||||||
| 199 | #pod =cut | ||||||||||||||||||||||||||||||||||||||||||
| 200 | |||||||||||||||||||||||||||||||||||||||||||
| 201 | 68 | ▶ | sub list { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
list: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 202 | 68 | my ( $self, $where, $opt ) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 203 | 68 | my $res = $self->_backend->list( $self->name, $where, $opt ); | |||||||||||||||||||||||||||||||||||||||||
| 204 | 68 | return { items => [ map { $self->build_item( $_ ) } @{ $res->{items} } ], total => $res->{total} }; | |||||||||||||||||||||||||||||||||||||||||
| 205 | } | ||||||||||||||||||||||||||||||||||||||||||
| 206 | |||||||||||||||||||||||||||||||||||||||||||
| 207 | #pod =method create | ||||||||||||||||||||||||||||||||||||||||||
| 208 | #pod | ||||||||||||||||||||||||||||||||||||||||||
| 209 | #pod Create a new item. Returns the ID of the created item. | ||||||||||||||||||||||||||||||||||||||||||
| 210 | #pod | ||||||||||||||||||||||||||||||||||||||||||
| 211 | #pod =cut | ||||||||||||||||||||||||||||||||||||||||||
| 212 | |||||||||||||||||||||||||||||||||||||||||||
| 213 | 41 | ▶ | sub create { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
create: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 214 | 41 | my ( $self, $data ) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 215 | 41 | ▶ | if ( my @errors = $self->validate( $data ) ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 216 | 2 | $self->_log->error( | |||||||||||||||||||||||||||||||||||||||||
| 217 | sprintf 'Error validating new item in schema "%s": %s', | ||||||||||||||||||||||||||||||||||||||||||
| 218 | $self->name, | ||||||||||||||||||||||||||||||||||||||||||
| 219 | join ', ', @errors | ||||||||||||||||||||||||||||||||||||||||||
| 220 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 221 | 2 | die \@errors; # XXX: Throw an exception instead that can stringify to something useful | |||||||||||||||||||||||||||||||||||||||||
| 222 | } | ||||||||||||||||||||||||||||||||||||||||||
| 223 | 39 | my $retval = eval { $self->_backend->create( $self->name, $data ) }; | |||||||||||||||||||||||||||||||||||||||||
| 224 | 39 | ▶ | if ( my $error = $@ ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 225 | 2 | $self->_log->error( | |||||||||||||||||||||||||||||||||||||||||
| 226 | sprintf 'Error creating item in schema "%s": %s', | ||||||||||||||||||||||||||||||||||||||||||
| 227 | $self->name, $error, | ||||||||||||||||||||||||||||||||||||||||||
| 228 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 229 | 2 | die $error; | |||||||||||||||||||||||||||||||||||||||||
| 230 | } | ||||||||||||||||||||||||||||||||||||||||||
| 231 | 37 | return $retval; | |||||||||||||||||||||||||||||||||||||||||
| 232 | } | ||||||||||||||||||||||||||||||||||||||||||
| 233 | |||||||||||||||||||||||||||||||||||||||||||
| 234 | #pod =method set | ||||||||||||||||||||||||||||||||||||||||||
| 235 | #pod | ||||||||||||||||||||||||||||||||||||||||||
| 236 | #pod Set the given fields in an item. See also L<Yancy::Model::Item/set>. | ||||||||||||||||||||||||||||||||||||||||||
| 237 | #pod | ||||||||||||||||||||||||||||||||||||||||||
| 238 | #pod =cut | ||||||||||||||||||||||||||||||||||||||||||
| 239 | |||||||||||||||||||||||||||||||||||||||||||
| 240 | 35 | ▶ | sub set { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
set: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 241 | 35 | my ( $self, $id, $data ) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 242 | 35 | ▶ | if ( my @errors = $self->validate( $data, properties => [ keys %$data ] ) ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 243 | 2 | $self->_log->error( | |||||||||||||||||||||||||||||||||||||||||
| 244 | sprintf 'Error validating item with ID "%s" in schema "%s": %s', | ||||||||||||||||||||||||||||||||||||||||||
| 245 | $id, $self->name, | ||||||||||||||||||||||||||||||||||||||||||
| 246 | join ', ', @errors | ||||||||||||||||||||||||||||||||||||||||||
| 247 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 248 | 2 | die \@errors; # XXX: Throw an exception instead that can stringify to something useful | |||||||||||||||||||||||||||||||||||||||||
| 249 | } | ||||||||||||||||||||||||||||||||||||||||||
| 250 | 33 | my $retval = eval { $self->_backend->set( $self->name, $id, $data ) }; | |||||||||||||||||||||||||||||||||||||||||
| 251 | 33 | ▶ | if ( my $error = $@ ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 252 | 2 | $self->_log->error( | |||||||||||||||||||||||||||||||||||||||||
| 253 | sprintf 'Error setting item with ID "%s" in schema "%s": %s', | ||||||||||||||||||||||||||||||||||||||||||
| 254 | $id, $self->name, $error, | ||||||||||||||||||||||||||||||||||||||||||
| 255 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 256 | 2 | die $error; | |||||||||||||||||||||||||||||||||||||||||
| 257 | } | ||||||||||||||||||||||||||||||||||||||||||
| 258 | 31 | return $retval; | |||||||||||||||||||||||||||||||||||||||||
| 259 | } | ||||||||||||||||||||||||||||||||||||||||||
| 260 | |||||||||||||||||||||||||||||||||||||||||||
| 261 | #pod =method delete | ||||||||||||||||||||||||||||||||||||||||||
| 262 | #pod | ||||||||||||||||||||||||||||||||||||||||||
| 263 | #pod Delete an item. See also L<Yancy::Model::Item/delete>. | ||||||||||||||||||||||||||||||||||||||||||
| 264 | #pod | ||||||||||||||||||||||||||||||||||||||||||
| 265 | #pod =cut | ||||||||||||||||||||||||||||||||||||||||||
| 266 | |||||||||||||||||||||||||||||||||||||||||||
| 267 | 18 | ▶ | sub delete { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
delete: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 268 | 18 | my ( $self, $id ) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 269 | # XXX: Use get() to get the item instance first? Then they could | ||||||||||||||||||||||||||||||||||||||||||
| 270 | # override delete() to do things... | ||||||||||||||||||||||||||||||||||||||||||
| 271 | 18 | return $self->_backend->delete( $self->name, $id ); | |||||||||||||||||||||||||||||||||||||||||
| 272 | } | ||||||||||||||||||||||||||||||||||||||||||
| 273 | |||||||||||||||||||||||||||||||||||||||||||
| 274 | 867 | ▶ | sub _check_json_schema { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_check_json_schema: called CC=10 SCAR=23.0
| |||||||||||||||||||||||||||||||||||||||||||
| 275 | 867 | my ( $self ) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 276 | 867 | my $name = $self->name; | |||||||||||||||||||||||||||||||||||||||||
| 277 | 867 | my $json_schema = $self->json_schema; | |||||||||||||||||||||||||||||||||||||||||
| 278 | |||||||||||||||||||||||||||||||||||||||||||
| 279 | # Deprecate x-view. Yancy::Model is a much better | ||||||||||||||||||||||||||||||||||||||||||
| 280 | # solution to that. | ||||||||||||||||||||||||||||||||||||||||||
| 281 | derp q{x-view is deprecated and will be removed in v2. } | ||||||||||||||||||||||||||||||||||||||||||
| 282 | . q{Use Yancy::Model or your database's CREATE VIEW instead} | ||||||||||||||||||||||||||||||||||||||||||
| 283 | 867 | ▶ | if $json_schema->{'x-view'}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 284 | |||||||||||||||||||||||||||||||||||||||||||
| 285 | 867 | ▶ | $json_schema->{ type } //= 'object'; | ||||||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
$json_schema->{"type"} //= "object"
MC/DC100%1/1
$json_schema->{"type"} //= "object"
$json_schema->{"type"}
Truth table100%2/2
A
A= $json_schema->{"type"}
| |||||||||||||||||||||||||||||||||||||||||||
| 286 | 867 | my $props = $json_schema->{properties}; | |||||||||||||||||||||||||||||||||||||||||
| 287 | 867 | ▶ | if ( $json_schema->{'x-view'} && !$props ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$json_schema->{"x-view"} and not $props
MC/DC100%2/2
$json_schema->{"x-view"} and not $props
$json_schema->{"x-view"}
not $props
Truth table100%3/3
A and not(B)
A= $json_schema->{"x-view"}B= not $props
| |||||||||||||||||||||||||||||||||||||||||||
| 288 | 116 | my $real_name = $json_schema->{'x-view'}->{schema}; | |||||||||||||||||||||||||||||||||||||||||
| 289 | 116 | ▶ | my $real_schema = $self->model->schema( $real_name ) | ||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$self->model->schema($real_name) // die(qq[Could not find x-view schema "$real_name" for schema "$name"])
MC/DC0%0/1
$self->model->schema($real_name) // die(qq[Could not find x-view schema "$real_name" for schema "$name"])
$self->model->schema($real_name)
Truth table50%1/2
A
A= $self->model->schema($real_name)
| |||||||||||||||||||||||||||||||||||||||||||
| 290 | // die qq{Could not find x-view schema "$real_name" for schema "$name"}; | ||||||||||||||||||||||||||||||||||||||||||
| 291 | 116 | $props = $real_schema->json_schema->{properties}; | |||||||||||||||||||||||||||||||||||||||||
| 292 | } | ||||||||||||||||||||||||||||||||||||||||||
| 293 | 867 | ▶ | die qq{Schema "$name" has no properties. Does it exist?} if !$props; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 294 | |||||||||||||||||||||||||||||||||||||||||||
| 295 | 867 | my $id_field = $self->id_field; | |||||||||||||||||||||||||||||||||||||||||
| 296 | 867 | ▶ | my @id_fields = ref $id_field eq 'ARRAY' ? @$id_field : ( $id_field ); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 297 | # ; say "$name ID field: @id_fields"; | ||||||||||||||||||||||||||||||||||||||||||
| 298 | # ; use Data::Dumper; | ||||||||||||||||||||||||||||||||||||||||||
| 299 | # ; say Dumper $props; | ||||||||||||||||||||||||||||||||||||||||||
| 300 | |||||||||||||||||||||||||||||||||||||||||||
| 301 | 867 | for my $field ( @id_fields ) { | |||||||||||||||||||||||||||||||||||||||||
| 302 | 915 | ▶ | if ( !$props->{ $field } ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 303 | 2 | die sprintf "ID field missing in properties for schema '%s', field '%s'." | |||||||||||||||||||||||||||||||||||||||||
| 304 | . " Add x-id-field to configure the correct ID field name, or" | ||||||||||||||||||||||||||||||||||||||||||
| 305 | . " add x-ignore to ignore this schema.", | ||||||||||||||||||||||||||||||||||||||||||
| 306 | $name, $field; | ||||||||||||||||||||||||||||||||||||||||||
| 307 | } | ||||||||||||||||||||||||||||||||||||||||||
| 308 | } | ||||||||||||||||||||||||||||||||||||||||||
| 309 | } | ||||||||||||||||||||||||||||||||||||||||||
| 310 | |||||||||||||||||||||||||||||||||||||||||||
| 311 | 21 | 1; | |||||||||||||||||||||||||||||||||||||||||
| 312 | |||||||||||||||||||||||||||||||||||||||||||
| 313 | __END__ | ||||||||||||||||||||||||||||||||||||||||||