| 1 | package Yancy::Backend::MojoDB; | ||||||||||||||||||||||||||||||||||||||||||
| 2 | our $VERSION = '1.078'; | ||||||||||||||||||||||||||||||||||||||||||
| 3 | # ABSTRACT: Abstract base class for drivers based on Mojo DB drivers | ||||||||||||||||||||||||||||||||||||||||||
| 4 | |||||||||||||||||||||||||||||||||||||||||||
| 5 | #pod =head1 SYNOPSIS | ||||||||||||||||||||||||||||||||||||||||||
| 6 | #pod | ||||||||||||||||||||||||||||||||||||||||||
| 7 | #pod package Yancy::Backend::RDBMS; | ||||||||||||||||||||||||||||||||||||||||||
| 8 | #pod use Mojo::Base 'Yancy::Backend::MojoDB'; | ||||||||||||||||||||||||||||||||||||||||||
| 9 | #pod | ||||||||||||||||||||||||||||||||||||||||||
| 10 | #pod =head1 DESCRIPTION | ||||||||||||||||||||||||||||||||||||||||||
| 11 | #pod | ||||||||||||||||||||||||||||||||||||||||||
| 12 | #pod This is an abstract base class for the Mojo database drivers: | ||||||||||||||||||||||||||||||||||||||||||
| 13 | #pod | ||||||||||||||||||||||||||||||||||||||||||
| 14 | #pod =over | ||||||||||||||||||||||||||||||||||||||||||
| 15 | #pod =item L<Mojo::Pg> (L<Yancy::Backend::Pg>) | ||||||||||||||||||||||||||||||||||||||||||
| 16 | #pod =item L<Mojo::mysql> (L<Yancy::Backend::Mysql>) | ||||||||||||||||||||||||||||||||||||||||||
| 17 | #pod =item L<Mojo::SQLite> (L<Yancy::Backend::Sqlite>) | ||||||||||||||||||||||||||||||||||||||||||
| 18 | #pod =back | ||||||||||||||||||||||||||||||||||||||||||
| 19 | #pod | ||||||||||||||||||||||||||||||||||||||||||
| 20 | #pod =head1 SEE ALSO | ||||||||||||||||||||||||||||||||||||||||||
| 21 | #pod | ||||||||||||||||||||||||||||||||||||||||||
| 22 | #pod L<Yancy::Backend::Role::DBI>, L<Yancy::Backend> | ||||||||||||||||||||||||||||||||||||||||||
| 23 | #pod | ||||||||||||||||||||||||||||||||||||||||||
| 24 | #pod =cut | ||||||||||||||||||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||||||||||||||||
| 26 | 1 | ▶ | use Mojo::Base 'Yancy::Backend'; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 27 | 1 | ▶ | use Role::Tiny 'with'; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 28 | 1 | ▶ | use Scalar::Util qw( blessed looks_like_number ); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 29 | 1 | ▶ | use Mojo::JSON qw( true encode_json ); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 30 | 1 | ▶ | use Carp qw( croak ); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 31 | 1 | ▶ | use Yancy::Util qw( is_type is_format ); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 32 | |||||||||||||||||||||||||||||||||||||||||||
| 33 | with 'Yancy::Backend::Role::DBI'; | ||||||||||||||||||||||||||||||||||||||||||
| 34 | has driver =>; | ||||||||||||||||||||||||||||||||||||||||||
| 35 | 0 | ▶ | sub sql_abstract { shift->driver->abstract } | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
sql_abstract: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||||||
| 36 | 0 | ▶ | sub dbh { shift->driver->db->dbh } | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
dbh: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||||||
| 37 | |||||||||||||||||||||||||||||||||||||||||||
| 38 | sub create { | ||||||||||||||||||||||||||||||||||||||||||
| 39 | 0 | ▶ | my ( $self, $schema_name, $params ) = @_; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
create: not called CC=6 SCAR=37.4
| |||||||||||||||||||||||||||||||||||||||||||
| 40 | 0 | $params = $self->normalize( $schema_name, $params ); | |||||||||||||||||||||||||||||||||||||||||
| 41 | 0 | my $id_field = $self->id_field( $schema_name ); | |||||||||||||||||||||||||||||||||||||||||
| 42 | # For databases that do not have a 'RETURNING' syntax, we must pass in all | ||||||||||||||||||||||||||||||||||||||||||
| 43 | # parts of a composite key. In the future, we could add a surrogate | ||||||||||||||||||||||||||||||||||||||||||
| 44 | # key which is auto-increment that could be used to find the | ||||||||||||||||||||||||||||||||||||||||||
| 45 | # newly-created row so that we can return the correct key fields | ||||||||||||||||||||||||||||||||||||||||||
| 46 | # here. For now, assume id field is correct if passed, created | ||||||||||||||||||||||||||||||||||||||||||
| 47 | # otherwise. | ||||||||||||||||||||||||||||||||||||||||||
| 48 | die "Missing composite ID parts: " . join( ', ', grep !exists $params->{$_}, @$id_field ) | ||||||||||||||||||||||||||||||||||||||||||
| 49 | 0 | ▶ | if ref $id_field eq 'ARRAY' && @$id_field > grep exists $params->{$_}, @$id_field; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
ref $id_field eq "ARRAY" and @$id_field > grep(exists $params->{$_}, @$id_field)
MC/DC0%0/2
ref $id_field eq "ARRAY" and @$id_field > grep(exists $params->{$_}, @$id_field)
ref $id_field eq "ARRAY"
@$id_field > grep(exists $params->{$_}, @$id_field)
Truth table0%0/3
A and B
A= ref $id_field eq "ARRAY"B= @$id_field > grep(exists $params->{$_}, @$id_field)
| |||||||||||||||||||||||||||||||||||||||||||
| 50 | 0 | my $res = $self->driver->db->insert( $schema_name, $params ); | |||||||||||||||||||||||||||||||||||||||||
| 51 | 0 | my $inserted_id = $res->last_insert_id; | |||||||||||||||||||||||||||||||||||||||||
| 52 | return ref $id_field eq 'ARRAY' | ||||||||||||||||||||||||||||||||||||||||||
| 53 | 0 | ▶ | ? { map { $_ => $params->{$_} // $inserted_id } @$id_field } | ||||||||||||||||||||||||||||||||||||||||
|
Condition0%0/6
$params->{$_} // $inserted_id
$params->{$id_field} // $inserted_id
MC/DC0%0/2
$params->{$_} // $inserted_id
$params->{$_}
$inserted_id
MC/DC0%0/2
$params->{$id_field} // $inserted_id
$params->{$id_field}
$inserted_id
Truth table0%0/3
A // B
A= $params->{$_}B= $inserted_id
Truth table0%0/3
A // B
A= $params->{$id_field}B= $inserted_id
| |||||||||||||||||||||||||||||||||||||||||||
| 54 | 0 | ▶ | : $params->{ $id_field } // $inserted_id | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 55 | ; | ||||||||||||||||||||||||||||||||||||||||||
| 56 | } | ||||||||||||||||||||||||||||||||||||||||||
| 57 | |||||||||||||||||||||||||||||||||||||||||||
| 58 | sub delete { | ||||||||||||||||||||||||||||||||||||||||||
| 59 | 0 | ▶ | my ( $self, $schema_name, $id ) = @_; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
delete: not called CC=2 SCAR=17.9
| |||||||||||||||||||||||||||||||||||||||||||
| 60 | 0 | my %where = $self->id_where( $schema_name, $id ); | |||||||||||||||||||||||||||||||||||||||||
| 61 | 0 | my $res = eval { $self->driver->db->delete( $schema_name, \%where ) }; | |||||||||||||||||||||||||||||||||||||||||
| 62 | 0 | ▶ | if ( $@ ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 63 | 0 | croak "Error on delete '$schema_name'=$id: $@"; | |||||||||||||||||||||||||||||||||||||||||
| 64 | } | ||||||||||||||||||||||||||||||||||||||||||
| 65 | 0 | return !!$res->rows; | |||||||||||||||||||||||||||||||||||||||||
| 66 | } | ||||||||||||||||||||||||||||||||||||||||||
| 67 | |||||||||||||||||||||||||||||||||||||||||||
| 68 | sub set { | ||||||||||||||||||||||||||||||||||||||||||
| 69 | 0 | ▶ | my ( $self, $schema_name, $id, $params ) = @_; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
set: not called CC=2 SCAR=17.9
| |||||||||||||||||||||||||||||||||||||||||||
| 70 | 0 | $params = $self->normalize( $schema_name, $params ); | |||||||||||||||||||||||||||||||||||||||||
| 71 | 0 | my %where = $self->id_where( $schema_name, $id ); | |||||||||||||||||||||||||||||||||||||||||
| 72 | 0 | my $res = eval { $self->driver->db->update( $schema_name, $params, \%where ) }; | |||||||||||||||||||||||||||||||||||||||||
| 73 | 0 | ▶ | if ( $@ ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 74 | 0 | croak "Error on set '$schema_name'=$id: $@"; | |||||||||||||||||||||||||||||||||||||||||
| 75 | } | ||||||||||||||||||||||||||||||||||||||||||
| 76 | 0 | return !!$res->rows; | |||||||||||||||||||||||||||||||||||||||||
| 77 | } | ||||||||||||||||||||||||||||||||||||||||||
| 78 | |||||||||||||||||||||||||||||||||||||||||||
| 79 | sub _sql_select { | ||||||||||||||||||||||||||||||||||||||||||
| 80 | 0 | ▶ | my ( $self, $schema_name, $where, $opt ) = @_; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_sql_select: not called CC=13 SCAR=52.0
| |||||||||||||||||||||||||||||||||||||||||||
| 81 | 0 | my $schema = $self->schema->{ $schema_name }; | |||||||||||||||||||||||||||||||||||||||||
| 82 | 0 | ▶ | my $from = ( $schema->{'x-view'} || {} )->{schema} // $schema_name; | ||||||||||||||||||||||||||||||||||||||||
|
Condition0%0/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 table0%0/3
A // B
A= $schema->{'x-view'}B= $schema_name
| |||||||||||||||||||||||||||||||||||||||||||
| 83 | 0 | ▶ | my %props = %{ $schema->{properties} || $self->schema->{ $from }{properties} }; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 84 | 0 | my @cols = keys %props; | |||||||||||||||||||||||||||||||||||||||||
| 85 | |||||||||||||||||||||||||||||||||||||||||||
| 86 | 0 | ▶ | if ( my $join = $opt->{join} ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 87 | # Make sure everything is fully-qualified | ||||||||||||||||||||||||||||||||||||||||||
| 88 | 0 | @cols = map { "$schema_name.$_" } @cols; | |||||||||||||||||||||||||||||||||||||||||
| 89 | 0 | $where->{ "$schema_name.$_" } = delete $where->{ $_ } for grep !/\./, keys %$where; | |||||||||||||||||||||||||||||||||||||||||
| 90 | |||||||||||||||||||||||||||||||||||||||||||
| 91 | 0 | $from = [ $from ]; | |||||||||||||||||||||||||||||||||||||||||
| 92 | 0 | ▶ | my @joins = ref $join eq 'ARRAY' ? @$join : ( $join ); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 93 | 0 | for my $j ( @joins ) { | |||||||||||||||||||||||||||||||||||||||||
| 94 | 0 | ▶ | if ( exists $props{ $j } ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||
| 95 | 0 | my $join_prop = $props{ $j }; | |||||||||||||||||||||||||||||||||||||||||
| 96 | 0 | my $join_schema_name = $join_prop->{'x-foreign-key'}; | |||||||||||||||||||||||||||||||||||||||||
| 97 | 0 | my $join_schema = $self->schema->{ $join_schema_name }; | |||||||||||||||||||||||||||||||||||||||||
| 98 | 0 | my $join_props = $join_schema->{properties}; | |||||||||||||||||||||||||||||||||||||||||
| 99 | 0 | ▶ | my $join_key_field = $join_schema->{'x-id-field'} // 'id'; | ||||||||||||||||||||||||||||||||||||||||
|
Condition0%0/2
$join_schema->{"x-id-field"} // "id"
MC/DC0%0/1
$join_schema->{"x-id-field"} // "id"
$join_schema->{"x-id-field"}
Truth table0%0/2
A
A= $join_schema->{"x-id-field"}
| |||||||||||||||||||||||||||||||||||||||||||
| 100 | 0 | push @{ $from }, [ -left => \("$join_prop->{'x-foreign-key'} AS $j"), "$j.$j", $join_key_field ]; | |||||||||||||||||||||||||||||||||||||||||
| 101 | 0 | push @cols, map { [ "${j}.$_", "${j}_$_" ] } keys %{ $join_props }; | |||||||||||||||||||||||||||||||||||||||||
| 102 | } | ||||||||||||||||||||||||||||||||||||||||||
| 103 | elsif ( exists $self->schema->{ $j } ) { | ||||||||||||||||||||||||||||||||||||||||||
| 104 | 0 | my $join_schema_name = $j; | |||||||||||||||||||||||||||||||||||||||||
| 105 | 0 | my $join_schema = $self->schema->{ $j }; | |||||||||||||||||||||||||||||||||||||||||
| 106 | 0 | my $join_props = $join_schema->{properties}; | |||||||||||||||||||||||||||||||||||||||||
| 107 | 0 | ▶ | my ( $join_prop_name ) = grep { ($join_props->{$_}{'x-foreign-key'}//'') eq $schema_name } keys %$join_props; | ||||||||||||||||||||||||||||||||||||||||
|
Condition0%0/2
$join_props->{$_}{"x-foreign-key"} // ""
MC/DC0%0/1
$join_props->{$_}{"x-foreign-key"} // ""
$join_props->{$_}{"x-foreign-key"}
Truth table0%0/2
A
A= $join_props->{$_}{"x-foreign-key"}
| |||||||||||||||||||||||||||||||||||||||||||
| 108 | 0 | ▶ | my $join_key_field = $schema->{'x-id-field'} // 'id'; | ||||||||||||||||||||||||||||||||||||||||
|
Condition0%0/2
$schema->{"x-id-field"} // "id"
MC/DC0%0/1
$schema->{"x-id-field"} // "id"
$schema->{"x-id-field"}
Truth table0%0/2
A
A= $schema->{"x-id-field"}
| |||||||||||||||||||||||||||||||||||||||||||
| 109 | 0 | push @{ $from }, [ -left => $join_schema_name, $join_key_field, $join_prop_name ]; | |||||||||||||||||||||||||||||||||||||||||
| 110 | 0 | push @cols, map { [ "$join_schema_name.$_", "${j}_$_" ] } keys %{ $join_props }; | |||||||||||||||||||||||||||||||||||||||||
| 111 | } | ||||||||||||||||||||||||||||||||||||||||||
| 112 | } | ||||||||||||||||||||||||||||||||||||||||||
| 113 | } | ||||||||||||||||||||||||||||||||||||||||||
| 114 | 0 | return $from, \@cols, $where; | |||||||||||||||||||||||||||||||||||||||||
| 115 | } | ||||||||||||||||||||||||||||||||||||||||||
| 116 | |||||||||||||||||||||||||||||||||||||||||||
| 117 | sub _expand_join { | ||||||||||||||||||||||||||||||||||||||||||
| 118 | 0 | ▶ | my ( $self, $schema_name, $res, $joins ) = @_; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_expand_join: not called CC=13 SCAR=52.0
| |||||||||||||||||||||||||||||||||||||||||||
| 119 | 0 | ▶ | my @joins = ref $joins eq 'ARRAY' ? @$joins : ( $joins ); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 120 | 0 | my $id_field = $self->id_field( $schema_name ); | |||||||||||||||||||||||||||||||||||||||||
| 121 | 0 | my %props = %{ $self->schema->{ $schema_name }{properties} }; | |||||||||||||||||||||||||||||||||||||||||
| 122 | 0 | my ( @rows, %rows ); | |||||||||||||||||||||||||||||||||||||||||
| 123 | 0 | ▶ | while ( my $r = $res->hash ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 124 | 0 | my $row = $rows{ $r->{$id_field} }; | |||||||||||||||||||||||||||||||||||||||||
| 125 | 0 | ▶ | if ( !$row ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 126 | 0 | $row = $r; | |||||||||||||||||||||||||||||||||||||||||
| 127 | 0 | push @rows, $row; | |||||||||||||||||||||||||||||||||||||||||
| 128 | 0 | $rows{ $r->{ $id_field } } = $row; | |||||||||||||||||||||||||||||||||||||||||
| 129 | # First instance of the row fills in all one-to-one | ||||||||||||||||||||||||||||||||||||||||||
| 130 | # relationships | ||||||||||||||||||||||||||||||||||||||||||
| 131 | 0 | for my $j ( @joins ) { | |||||||||||||||||||||||||||||||||||||||||
| 132 | 0 | ▶ | my $j_id = $self->schema->{ $j } ? ($self->schema->{$j}{'x-id-field'}//'id') : $j; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/2
$self->schema->{$j}{"x-id-field"} // "id"
MC/DC0%0/1
$self->schema->{$j}{"x-id-field"} // "id"
$self->schema->{$j}{"x-id-field"}
Truth table0%0/2
A
A= $self->schema->{$j}{"x-id-field"}
| |||||||||||||||||||||||||||||||||||||||||||
| 133 | # If the ID field isn't defined, then there was no row | ||||||||||||||||||||||||||||||||||||||||||
| 134 | # to join. So just remove the columns for the join | ||||||||||||||||||||||||||||||||||||||||||
| 135 | 0 | ▶ | if ( !defined $row->{ "${j}_${j_id}" } ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 136 | %$row = ( | ||||||||||||||||||||||||||||||||||||||||||
| 137 | # Keys not in the join | ||||||||||||||||||||||||||||||||||||||||||
| 138 | 0 | ( map { $_ => $row->{$_} } grep !/^${j}_/, keys %$row ), | |||||||||||||||||||||||||||||||||||||||||
| 139 | # Empty array, if needed | ||||||||||||||||||||||||||||||||||||||||||
| 140 | 0 | ( $j => [] )x!!$self->schema->{ $j }, | |||||||||||||||||||||||||||||||||||||||||
| 141 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 142 | 0 | next; | |||||||||||||||||||||||||||||||||||||||||
| 143 | } | ||||||||||||||||||||||||||||||||||||||||||
| 144 | %$row = ( | ||||||||||||||||||||||||||||||||||||||||||
| 145 | # Keys not in the join | ||||||||||||||||||||||||||||||||||||||||||
| 146 | 0 | ▶ | ( map { $_ => $row->{$_} } grep !/^${j}_/, keys %$row ), | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 147 | # Keys in the join | ||||||||||||||||||||||||||||||||||||||||||
| 148 | $j => exists $props{ $j } | ||||||||||||||||||||||||||||||||||||||||||
| 149 | # One-to-one relationship | ||||||||||||||||||||||||||||||||||||||||||
| 150 | 0 | ? { map { s/^${j}_//r => $row->{$_} } grep /^${j}_/, keys %$row } | |||||||||||||||||||||||||||||||||||||||||
| 151 | # One-to-many relationship | ||||||||||||||||||||||||||||||||||||||||||
| 152 | 0 | : [{ map { s/^${j}_//r => $row->{$_} } grep /^${j}_/, keys %$row }] | |||||||||||||||||||||||||||||||||||||||||
| 153 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 154 | } | ||||||||||||||||||||||||||||||||||||||||||
| 155 | 0 | next; | |||||||||||||||||||||||||||||||||||||||||
| 156 | } | ||||||||||||||||||||||||||||||||||||||||||
| 157 | # Subsequent rows add to one-to-many relationship | ||||||||||||||||||||||||||||||||||||||||||
| 158 | 0 | for my $j ( grep !exists $props{ $_ }, @joins ) { | |||||||||||||||||||||||||||||||||||||||||
| 159 | 0 | ▶ | my $j_id = $self->schema->{$j}{'x-id-field'}//'id'; | ||||||||||||||||||||||||||||||||||||||||
|
Condition0%0/2
$self->schema->{$j}{"x-id-field"} // "id"
MC/DC0%0/1
$self->schema->{$j}{"x-id-field"} // "id"
$self->schema->{$j}{"x-id-field"}
Truth table0%0/2
A
A= $self->schema->{$j}{"x-id-field"}
| |||||||||||||||||||||||||||||||||||||||||||
| 160 | # If the ID field isn't defined, then there was no row | ||||||||||||||||||||||||||||||||||||||||||
| 161 | # to join. So just skip this. | ||||||||||||||||||||||||||||||||||||||||||
| 162 | 0 | ▶ | next if !defined $r->{ "${j}_${j_id}" }; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 163 | 0 | push @{ $row->{ $j } }, { map { s/^${j}_//r => $r->{$_} } grep /^${j}_/, keys %$r }; | |||||||||||||||||||||||||||||||||||||||||
| 164 | } | ||||||||||||||||||||||||||||||||||||||||||
| 165 | } | ||||||||||||||||||||||||||||||||||||||||||
| 166 | 0 | ▶ | return wantarray ? @rows : $rows[-1]; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 167 | } | ||||||||||||||||||||||||||||||||||||||||||
| 168 | |||||||||||||||||||||||||||||||||||||||||||
| 169 | sub get { | ||||||||||||||||||||||||||||||||||||||||||
| 170 | 0 | ▶ | my ( $self, $schema_name, $id, %opt ) = @_; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
get: not called CC=3 SCAR=24.8
| |||||||||||||||||||||||||||||||||||||||||||
| 171 | 0 | my %where = $self->id_where( $schema_name, $id ); | |||||||||||||||||||||||||||||||||||||||||
| 172 | 0 | my ( $from, $cols, $where ) = $self->_sql_select( $schema_name, \%where, \%opt ); | |||||||||||||||||||||||||||||||||||||||||
| 173 | 0 | my ( $sql, @params ) = $self->driver->abstract->select( $from, $cols, $where ); | |||||||||||||||||||||||||||||||||||||||||
| 174 | 0 | my $res = $self->driver->db->query( $sql, @params ); | |||||||||||||||||||||||||||||||||||||||||
| 175 | 0 | ▶ | my $row = $opt{join} ? $self->_expand_join( $schema_name, $res, $opt{join} ) : $res->hash; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 176 | 0 | ▶ | return $row if !$row; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 177 | 0 | return $self->normalize( $schema_name, $row ); | |||||||||||||||||||||||||||||||||||||||||
| 178 | } | ||||||||||||||||||||||||||||||||||||||||||
| 179 | |||||||||||||||||||||||||||||||||||||||||||
| 180 | sub get_p { | ||||||||||||||||||||||||||||||||||||||||||
| 181 | 0 | ▶ | my ( $self, $schema_name, $id, %opt ) = @_; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
get_p: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||||||
| 182 | 0 | my %where = $self->id_where( $schema_name, $id ); | |||||||||||||||||||||||||||||||||||||||||
| 183 | 0 | my ( $from, $cols, $where ) = $self->_sql_select( $schema_name, \%where, \%opt ); | |||||||||||||||||||||||||||||||||||||||||
| 184 | 0 | my ( $sql, @params ) = $self->driver->abstract->select( $from, $cols, $where ); | |||||||||||||||||||||||||||||||||||||||||
| 185 | 0 | my $p = $self->driver->db->query_p( $sql, @params ); | |||||||||||||||||||||||||||||||||||||||||
| 186 | return $p->then( sub { | ||||||||||||||||||||||||||||||||||||||||||
| 187 | 0 | ▶ | my ( $res ) = @_; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=2 SCAR=17.9
| |||||||||||||||||||||||||||||||||||||||||||
| 188 | 0 | ▶ | my $row = $opt{join} ? $self->_expand_join( $schema_name, $res, $opt{join} ) : $res->hash; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 189 | 0 | return $self->normalize( $schema_name, $row ); | |||||||||||||||||||||||||||||||||||||||||
| 190 | 0 | }); | |||||||||||||||||||||||||||||||||||||||||
| 191 | } | ||||||||||||||||||||||||||||||||||||||||||
| 192 | |||||||||||||||||||||||||||||||||||||||||||
| 193 | sub list { | ||||||||||||||||||||||||||||||||||||||||||
| 194 | 0 | ▶ | my ( $self, $schema_name, $params, @opt ) = @_; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
list: not called CC=5 SCAR=34.0
| |||||||||||||||||||||||||||||||||||||||||||
| 195 | 0 | ▶ | my $opt = @opt % 2 == 0 ? {@opt} : $opt[0]; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 196 | 0 | ▶ | $params ||= {}; $opt ||= {}; | ||||||||||||||||||||||||||||||||||||||||
|
Condition0%0/4
$params ||= {}
$opt ||= {}
MC/DC0%0/1
$params ||= {}
$params
MC/DC0%0/1
$opt ||= {}
$opt
Truth table0%0/2
A
A= $params
Truth table0%0/2
A
A= $opt
| |||||||||||||||||||||||||||||||||||||||||||
| 197 | 0 | my $driver = $self->driver; | |||||||||||||||||||||||||||||||||||||||||
| 198 | 0 | my ( $query, $total_query, @params ) = $self->list_sqls( $schema_name, $params, $opt ); | |||||||||||||||||||||||||||||||||||||||||
| 199 | 0 | my $res = $driver->db->query( $query, @params ); | |||||||||||||||||||||||||||||||||||||||||
| 200 | 0 | ▶ | my @items = $opt->{join} ? $self->_expand_join( $schema_name, $res, $opt->{join} ) : @{$res->hashes}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 201 | return { | ||||||||||||||||||||||||||||||||||||||||||
| 202 | items => [ map $self->normalize( $schema_name, $_ ), @items ], | ||||||||||||||||||||||||||||||||||||||||||
| 203 | total => $driver->db->query( $total_query, @params )->hash->{total}, | ||||||||||||||||||||||||||||||||||||||||||
| 204 | 0 | }; | |||||||||||||||||||||||||||||||||||||||||
| 205 | } | ||||||||||||||||||||||||||||||||||||||||||
| 206 | |||||||||||||||||||||||||||||||||||||||||||
| 207 | sub create_p { | ||||||||||||||||||||||||||||||||||||||||||
| 208 | 0 | ▶ | my ( $self, $schema_name, $params ) = @_; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
create_p: not called CC=3 SCAR=24.8
| |||||||||||||||||||||||||||||||||||||||||||
| 209 | 0 | $params = $self->normalize( $schema_name, $params ); | |||||||||||||||||||||||||||||||||||||||||
| 210 | 0 | my $id_field = $self->id_field( $schema_name ); | |||||||||||||||||||||||||||||||||||||||||
| 211 | # For databases that do not have a 'RETURNING' syntax, we must pass in all | ||||||||||||||||||||||||||||||||||||||||||
| 212 | # parts of a composite key. In the future, we could add a surrogate | ||||||||||||||||||||||||||||||||||||||||||
| 213 | # key which is auto-increment that could be used to find the | ||||||||||||||||||||||||||||||||||||||||||
| 214 | # newly-created row so that we can return the correct key fields | ||||||||||||||||||||||||||||||||||||||||||
| 215 | # here. For now, assume id field is correct if passed, created | ||||||||||||||||||||||||||||||||||||||||||
| 216 | # otherwise. | ||||||||||||||||||||||||||||||||||||||||||
| 217 | die "Missing composite ID parts: " . join( ', ', grep !exists $params->{$_}, @$id_field ) | ||||||||||||||||||||||||||||||||||||||||||
| 218 | 0 | ▶ | if ref $id_field eq 'ARRAY' && @$id_field > grep exists $params->{$_}, @$id_field; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
ref $id_field eq "ARRAY" and @$id_field > grep(exists $params->{$_}, @$id_field)
MC/DC0%0/2
ref $id_field eq "ARRAY" and @$id_field > grep(exists $params->{$_}, @$id_field)
ref $id_field eq "ARRAY"
@$id_field > grep(exists $params->{$_}, @$id_field)
Truth table0%0/3
A and B
A= ref $id_field eq "ARRAY"B= @$id_field > grep(exists $params->{$_}, @$id_field)
| |||||||||||||||||||||||||||||||||||||||||||
| 219 | return $self->driver->db->insert_p( $schema_name, $params ) | ||||||||||||||||||||||||||||||||||||||||||
| 220 | ->then( sub { | ||||||||||||||||||||||||||||||||||||||||||
| 221 | 0 | ▶ | my $inserted_id = shift->last_insert_id; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=4 SCAR=30.0
| |||||||||||||||||||||||||||||||||||||||||||
| 222 | return ref $id_field eq 'ARRAY' | ||||||||||||||||||||||||||||||||||||||||||
| 223 | 0 | ▶ | ? { map { $_ => $params->{$_} // $inserted_id } @$id_field } | ||||||||||||||||||||||||||||||||||||||||
|
Condition0%0/6
$params->{$_} // $inserted_id
$params->{$id_field} // $inserted_id
MC/DC0%0/2
$params->{$_} // $inserted_id
$params->{$_}
$inserted_id
MC/DC0%0/2
$params->{$id_field} // $inserted_id
$params->{$id_field}
$inserted_id
Truth table0%0/3
A // B
A= $params->{$_}B= $inserted_id
Truth table0%0/3
A // B
A= $params->{$id_field}B= $inserted_id
| |||||||||||||||||||||||||||||||||||||||||||
| 224 | 0 | ▶ | : $params->{ $id_field } // $inserted_id | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 225 | ; | ||||||||||||||||||||||||||||||||||||||||||
| 226 | 0 | } ); | |||||||||||||||||||||||||||||||||||||||||
| 227 | } | ||||||||||||||||||||||||||||||||||||||||||
| 228 | |||||||||||||||||||||||||||||||||||||||||||
| 229 | sub delete_p { | ||||||||||||||||||||||||||||||||||||||||||
| 230 | 0 | ▶ | my ( $self, $schema_name, $id ) = @_; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
delete_p: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||||||
| 231 | 0 | my %where = $self->id_where( $schema_name, $id ); | |||||||||||||||||||||||||||||||||||||||||
| 232 | $self->driver->db->delete_p( $schema_name, \%where ) | ||||||||||||||||||||||||||||||||||||||||||
| 233 | 0 | ▶ | ->catch(sub { croak "Error on delete '$schema_name'=$id: $_[0]" }) | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||||||
| 234 | 0 | ▶ | ->then(sub { !!shift->rows } ); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||||||
| 235 | } | ||||||||||||||||||||||||||||||||||||||||||
| 236 | |||||||||||||||||||||||||||||||||||||||||||
| 237 | sub set_p { | ||||||||||||||||||||||||||||||||||||||||||
| 238 | 0 | ▶ | my ( $self, $schema_name, $id, $params ) = @_; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
set_p: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||||||
| 239 | 0 | $params = $self->normalize( $schema_name, $params ); | |||||||||||||||||||||||||||||||||||||||||
| 240 | 0 | my %where = $self->id_where( $schema_name, $id ); | |||||||||||||||||||||||||||||||||||||||||
| 241 | $self->driver->db->update_p( $schema_name, $params, \%where ) | ||||||||||||||||||||||||||||||||||||||||||
| 242 | 0 | ▶ | ->catch(sub { croak "Error on set '$schema_name'=$id: $_[0]" }) | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||||||
| 243 | 0 | ▶ | ->then(sub { !!shift->rows } ); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||||||
| 244 | } | ||||||||||||||||||||||||||||||||||||||||||
| 245 | |||||||||||||||||||||||||||||||||||||||||||
| 246 | # XXX: If needed, this can be broken out into its own role based on | ||||||||||||||||||||||||||||||||||||||||||
| 247 | # SQL::Abstract. | ||||||||||||||||||||||||||||||||||||||||||
| 248 | sub list_sqls { | ||||||||||||||||||||||||||||||||||||||||||
| 249 | 0 | ▶ | my ( $self, $schema_name, $where, $opt ) = @_; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
list_sqls: not called CC=12 SCAR=50.5
| |||||||||||||||||||||||||||||||||||||||||||
| 250 | 0 | my $schema = $self->schema->{ $schema_name }; | |||||||||||||||||||||||||||||||||||||||||
| 251 | 0 | ▶ | my $id_field = $schema->{'x-id-field'} // 'id'; | ||||||||||||||||||||||||||||||||||||||||
|
Condition0%0/2
$schema->{"x-id-field"} // "id"
MC/DC0%0/1
$schema->{"x-id-field"} // "id"
$schema->{"x-id-field"}
Truth table0%0/2
A
A= $schema->{"x-id-field"}
| |||||||||||||||||||||||||||||||||||||||||||
| 252 | 0 | ▶ | my $real_schema_name = ( $schema->{'x-view'} || {} )->{schema} // $schema_name; | ||||||||||||||||||||||||||||||||||||||||
|
Condition0%0/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 table0%0/3
A // B
A= $schema->{'x-view'}B= $schema_name
| |||||||||||||||||||||||||||||||||||||||||||
| 253 | 0 | my $sqla = $self->sql_abstract; | |||||||||||||||||||||||||||||||||||||||||
| 254 | |||||||||||||||||||||||||||||||||||||||||||
| 255 | 0 | ( my $from, my $cols, $where ) = $self->_sql_select( $schema_name, $where, $opt ); | |||||||||||||||||||||||||||||||||||||||||
| 256 | my ( $query, @params ) = $sqla->select( | ||||||||||||||||||||||||||||||||||||||||||
| 257 | $from, $cols, $where, | ||||||||||||||||||||||||||||||||||||||||||
| 258 | { | ||||||||||||||||||||||||||||||||||||||||||
| 259 | order_by => $opt->{order_by}, | ||||||||||||||||||||||||||||||||||||||||||
| 260 | }, | ||||||||||||||||||||||||||||||||||||||||||
| 261 | 0 | ); | |||||||||||||||||||||||||||||||||||||||||
| 262 | |||||||||||||||||||||||||||||||||||||||||||
| 263 | # XXX: SQL::Abstract::mysql destroys the $from joined table arrays | ||||||||||||||||||||||||||||||||||||||||||
| 264 | 0 | ( $from, $cols, $where ) = $self->_sql_select( $schema_name, $where, $opt ); | |||||||||||||||||||||||||||||||||||||||||
| 265 | my ( $total_query, @total_params ) = $sqla->select( | ||||||||||||||||||||||||||||||||||||||||||
| 266 | $from, | ||||||||||||||||||||||||||||||||||||||||||
| 267 | 0 | ▶ | [ grep { !ref && ( /^$schema_name\./ || !/\./ ) } @$cols ], | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/6
!ref($_) && (/^$schema_name\./u || !/\./u)
/^$schema_name\./u or not /\./u
MC/DC0%0/3
!ref($_) && (/^$schema_name\./u || !/\./u)
!ref($_)
/^$schema_name\./u
not /\./u
Truth table0%0/4
not(A) && B or not(C)
A= !ref($_)B= /^$schema_name\./uC= not /\./u
| |||||||||||||||||||||||||||||||||||||||||||
| 268 | $where, | ||||||||||||||||||||||||||||||||||||||||||
| 269 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 270 | 0 | $total_query =~ s/SELECT/SELECT DISTINCT/i; | |||||||||||||||||||||||||||||||||||||||||
| 271 | 0 | $total_query = 'SELECT COUNT(*) AS total FROM (' . $total_query . ') total_query'; | |||||||||||||||||||||||||||||||||||||||||
| 272 | |||||||||||||||||||||||||||||||||||||||||||
| 273 | 0 | ▶ | if ( scalar grep defined, @{ $opt }{qw( limit offset )} ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 274 | # XXX: SQL::Abstract now handles this, so we should move this. | ||||||||||||||||||||||||||||||||||||||||||
| 275 | 0 | ▶ | die "Limit must be number" if $opt->{limit} && !looks_like_number $opt->{limit}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$opt->{"limit"} and not &looks_like_number($opt->{"limit"})
MC/DC0%0/2
$opt->{"limit"} and not &looks_like_number($opt->{"limit"})
$opt->{"limit"}
not &looks_like_number($opt->{"limit"})
Truth table0%0/3
A and not(B)
A= $opt->{"limit"}B= not &looks_like_number($opt->{"limit"})
| |||||||||||||||||||||||||||||||||||||||||||
| 276 | 0 | ▶ | $query .= ' LIMIT ' . ( $opt->{limit} // 2**32 ); | ||||||||||||||||||||||||||||||||||||||||
|
Condition0%0/2
$opt->{"limit"} // "4294967296"
MC/DC0%0/1
$opt->{"limit"} // "4294967296"
$opt->{"limit"}
Truth table0%0/2
A
A= $opt->{"limit"}
| |||||||||||||||||||||||||||||||||||||||||||
| 277 | 0 | ▶ | if ( $opt->{offset} ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 278 | 0 | ▶ | die "Offset must be number" if !looks_like_number $opt->{offset}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 279 | 0 | $query .= ' OFFSET ' . $opt->{offset}; | |||||||||||||||||||||||||||||||||||||||||
| 280 | } | ||||||||||||||||||||||||||||||||||||||||||
| 281 | } | ||||||||||||||||||||||||||||||||||||||||||
| 282 | #; say $query; | ||||||||||||||||||||||||||||||||||||||||||
| 283 | #; say $total_query; | ||||||||||||||||||||||||||||||||||||||||||
| 284 | 0 | return ( $query, $total_query, @params ); | |||||||||||||||||||||||||||||||||||||||||
| 285 | } | ||||||||||||||||||||||||||||||||||||||||||
| 286 | |||||||||||||||||||||||||||||||||||||||||||
| 287 | sub list_p { | ||||||||||||||||||||||||||||||||||||||||||
| 288 | 0 | ▶ | my ( $self, $schema_name, $params, @opt ) = @_; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
list_p: not called CC=3 SCAR=24.8
| |||||||||||||||||||||||||||||||||||||||||||
| 289 | 0 | ▶ | my $opt = @opt % 2 == 0 ? {@opt} : $opt[0]; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 290 | 0 | ▶ | $params ||= {}; | ||||||||||||||||||||||||||||||||||||||||
|
Condition0%0/2
$params ||= {}
MC/DC0%0/1
$params ||= {}
$params
Truth table0%0/2
A
A= $params
| |||||||||||||||||||||||||||||||||||||||||||
| 291 | 0 | my $driver = $self->driver; | |||||||||||||||||||||||||||||||||||||||||
| 292 | 0 | my ( $query, $total_query, @params ) = $self->list_sqls( $schema_name, $params, $opt ); | |||||||||||||||||||||||||||||||||||||||||
| 293 | $driver->db->query_p( $query, @params )->then( | ||||||||||||||||||||||||||||||||||||||||||
| 294 | sub { | ||||||||||||||||||||||||||||||||||||||||||
| 295 | 0 | ▶ | my ( $res ) = @_; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||||||
| 296 | 0 | my $items = $res->hashes; | |||||||||||||||||||||||||||||||||||||||||
| 297 | return { | ||||||||||||||||||||||||||||||||||||||||||
| 298 | items => [ map $self->normalize( $schema_name, $_ ), @$items ], | ||||||||||||||||||||||||||||||||||||||||||
| 299 | total => $driver->db->query( $total_query, @params )->hash->{total}, | ||||||||||||||||||||||||||||||||||||||||||
| 300 | 0 | }; | |||||||||||||||||||||||||||||||||||||||||
| 301 | }, | ||||||||||||||||||||||||||||||||||||||||||
| 302 | 0 | ); | |||||||||||||||||||||||||||||||||||||||||
| 303 | } | ||||||||||||||||||||||||||||||||||||||||||
| 304 | |||||||||||||||||||||||||||||||||||||||||||
| 305 | 1; | ||||||||||||||||||||||||||||||||||||||||||
| 306 | |||||||||||||||||||||||||||||||||||||||||||
| 307 | __END__ | ||||||||||||||||||||||||||||||||||||||||||