| 1 | # ABSTRACT: mock repository - table class | ||||||||||||||||||||||||||||
| 2 | package Test::PONAPI::Repository::MockDB::Table; | ||||||||||||||||||||||||||||
| 3 | |||||||||||||||||||||||||||||
| 4 | 8 | ▶ | use Moose; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||
| 6 | 8 | ▶ | use Test::PONAPI::Repository::MockDB::Table::Relationships; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||
| 8 | 8 | has [qw/TYPE TABLE ID_COLUMN/] => ( | |||||||||||||||||||||||||||
| 9 | is => 'ro', | ||||||||||||||||||||||||||||
| 10 | isa => 'Str', | ||||||||||||||||||||||||||||
| 11 | required => 1, | ||||||||||||||||||||||||||||
| 12 | ); | ||||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||
| 14 | 8 | has COLUMNS => ( | |||||||||||||||||||||||||||
| 15 | is => 'ro', | ||||||||||||||||||||||||||||
| 16 | isa => 'ArrayRef', | ||||||||||||||||||||||||||||
| 17 | required => 1, | ||||||||||||||||||||||||||||
| 18 | ); | ||||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||
| 20 | has RELATIONS => ( | ||||||||||||||||||||||||||||
| 21 | is => 'ro', | ||||||||||||||||||||||||||||
| 22 | isa => 'HashRef[Test::PONAPI::Repository::MockDB::Table::Relationships]', | ||||||||||||||||||||||||||||
| 23 | 36 | ▶ | default => sub { {} }, | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 24 | 8 | ); | |||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||
| 26 | sub insert_stmt { | ||||||||||||||||||||||||||||
| 27 | 19 | ▶ | my ($self, %args) = @_; | ||||||||||||||||||||||||||
|
Subroutine
insert_stmt: called CC=2 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 28 | |||||||||||||||||||||||||||||
| 29 | 19 | my $table = $args{table}; | |||||||||||||||||||||||||||
| 30 | 19 | my $values = $args{values}; | |||||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||||||
| 32 | # NOTE: this is a bunch of bad practices rolled together. | ||||||||||||||||||||||||||||
| 33 | # We're crafting our own SQL and not escaping the table/columns, | ||||||||||||||||||||||||||||
| 34 | # as well as using sqlite-specific features. | ||||||||||||||||||||||||||||
| 35 | # Ordinarily, you'd use DBIx::Class or at least SQL::Composer | ||||||||||||||||||||||||||||
| 36 | # for this, but we got reports that packaging PONAPI::Server | ||||||||||||||||||||||||||||
| 37 | # becomes hugely complex by adding either of those as dependencies. | ||||||||||||||||||||||||||||
| 38 | # Since this is just for testing, let's forgo a couple of good practices | ||||||||||||||||||||||||||||
| 39 | # and do it all manually. | ||||||||||||||||||||||||||||
| 40 | 19 | my @keys = keys %$values; | |||||||||||||||||||||||||||
| 41 | 19 | my @values = values %$values; | |||||||||||||||||||||||||||
| 42 | 19 | ▶ | my $sql = "INSERT INTO $table " . (@keys | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 43 | ? '(' . join( ",", @keys) . ') VALUES (' . join(',', ('?') x @keys) . ')' | ||||||||||||||||||||||||||||
| 44 | : 'DEFAULT VALUES'); | ||||||||||||||||||||||||||||
| 45 | |||||||||||||||||||||||||||||
| 46 | 19 | my $stmt = { | |||||||||||||||||||||||||||
| 47 | sql => $sql, | ||||||||||||||||||||||||||||
| 48 | bind => \@values, | ||||||||||||||||||||||||||||
| 49 | }; | ||||||||||||||||||||||||||||
| 50 | |||||||||||||||||||||||||||||
| 51 | 19 | return $stmt; | |||||||||||||||||||||||||||
| 52 | } | ||||||||||||||||||||||||||||
| 53 | |||||||||||||||||||||||||||||
| 54 | sub delete_stmt { | ||||||||||||||||||||||||||||
| 55 | 20 | ▶ | my ($self, %args) = @_; | ||||||||||||||||||||||||||
|
Subroutine
delete_stmt: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 56 | |||||||||||||||||||||||||||||
| 57 | 20 | my $table = $args{table}; | |||||||||||||||||||||||||||
| 58 | 20 | my $where = $args{where}; | |||||||||||||||||||||||||||
| 59 | |||||||||||||||||||||||||||||
| 60 | 20 | my @keys = keys %$where; | |||||||||||||||||||||||||||
| 61 | 20 | my @values = values %$where; | |||||||||||||||||||||||||||
| 62 | |||||||||||||||||||||||||||||
| 63 | 20 | my $sql = "DELETE FROM $table WHERE " | |||||||||||||||||||||||||||
| 64 | . join " AND ", map "$_=?", @keys; | ||||||||||||||||||||||||||||
| 65 | |||||||||||||||||||||||||||||
| 66 | 20 | my $stmt = { sql => $sql, bind => \@values }; | |||||||||||||||||||||||||||
| 67 | |||||||||||||||||||||||||||||
| 68 | 20 | return $stmt; | |||||||||||||||||||||||||||
| 69 | } | ||||||||||||||||||||||||||||
| 70 | |||||||||||||||||||||||||||||
| 71 | sub select_stmt { | ||||||||||||||||||||||||||||
| 72 | 208 | ▶ | my ($self, %args) = @_; | ||||||||||||||||||||||||||
|
Subroutine
select_stmt: called CC=9 SCAR=22.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 73 | |||||||||||||||||||||||||||||
| 74 | 208 | my $type = $args{type}; | |||||||||||||||||||||||||||
| 75 | 208 | my $filters = $self->_stmt_filters($type, $args{filter}); | |||||||||||||||||||||||||||
| 76 | |||||||||||||||||||||||||||||
| 77 | 208 | ▶ | my %limit = %{ $args{page} || {} }; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 78 | 208 | ▶ | my $sort = $args{sort} || []; | ||||||||||||||||||||||||||
|
Condition100%2/2
$args{'sort'} || []
MC/DC100%1/1
$args{'sort'} || []
$args{'sort'}
Truth table100%2/2
A
A= $args{'sort'}
| |||||||||||||||||||||||||||||
| 79 | |||||||||||||||||||||||||||||
| 80 | my @order_by = map { | ||||||||||||||||||||||||||||
| 81 | 208 | my ($desc, $col) = /\A(-?)(.+)\z/s; | |||||||||||||||||||||||||||
| 82 | 8 | ▶ | join ' ', $col => uc( $desc ? 'desc' : 'asc' ); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 83 | } @$sort; | ||||||||||||||||||||||||||||
| 84 | |||||||||||||||||||||||||||||
| 85 | 208 | my $columns = $self->_stmt_columns(\%args); | |||||||||||||||||||||||||||
| 86 | 208 | ▶ | my @values = map { ref($_) ? @$_ : $_ } values %$filters; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 87 | my $sql = join "\n", | ||||||||||||||||||||||||||||
| 88 | 'SELECT ' . join(',', @$columns), | ||||||||||||||||||||||||||||
| 89 | 'FROM ' . $type, | ||||||||||||||||||||||||||||
| 90 | (%$filters | ||||||||||||||||||||||||||||
| 91 | ? 'WHERE ' . join(' AND ', map { | ||||||||||||||||||||||||||||
| 92 | 208 | ▶ | my $val = $filters->{$_}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 93 | 193 | ▶ | ref($val) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 94 | 24 | ▶ | ? "$_ IN (@{[ join ',', ('?') x @$val ]})" | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 95 | : "$_=?" | ||||||||||||||||||||||||||||
| 96 | } keys %$filters) | ||||||||||||||||||||||||||||
| 97 | : '' | ||||||||||||||||||||||||||||
| 98 | ), | ||||||||||||||||||||||||||||
| 99 | (@order_by ? 'ORDER BY ' . join(', ', @order_by) : ''), | ||||||||||||||||||||||||||||
| 100 | (%limit ? "LIMIT $limit{limit} OFFSET $limit{offset}" : '' ); | ||||||||||||||||||||||||||||
| 101 | |||||||||||||||||||||||||||||
| 102 | 208 | my $stmt = { | |||||||||||||||||||||||||||
| 103 | sql => $sql, | ||||||||||||||||||||||||||||
| 104 | bind => \@values, | ||||||||||||||||||||||||||||
| 105 | }; | ||||||||||||||||||||||||||||
| 106 | |||||||||||||||||||||||||||||
| 107 | 208 | return $stmt; | |||||||||||||||||||||||||||
| 108 | } | ||||||||||||||||||||||||||||
| 109 | |||||||||||||||||||||||||||||
| 110 | sub update_stmt { | ||||||||||||||||||||||||||||
| 111 | 8 | ▶ | my ($self, %args) = @_; | ||||||||||||||||||||||||||
|
Subroutine
update_stmt: called CC=2 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 112 | |||||||||||||||||||||||||||||
| 113 | 8 | my $id = $args{id}; | |||||||||||||||||||||||||||
| 114 | 8 | my $table = $args{table}; | |||||||||||||||||||||||||||
| 115 | 8 | ▶ | my $values = $args{values} || {}; | ||||||||||||||||||||||||||
|
Condition50%1/2
$args{'values'} || {}
MC/DC0%0/1
$args{'values'} || {}
$args{'values'}
Truth table50%1/2
A
A= $args{'values'}
| |||||||||||||||||||||||||||||
| 116 | 8 | my $where = $args{where}; | |||||||||||||||||||||||||||
| 117 | |||||||||||||||||||||||||||||
| 118 | 8 | my @cols = keys %$values; | |||||||||||||||||||||||||||
| 119 | 8 | my @values = values %$values; | |||||||||||||||||||||||||||
| 120 | 8 | push @values, values %$where; | |||||||||||||||||||||||||||
| 121 | |||||||||||||||||||||||||||||
| 122 | 8 | my $sql = join "\n", | |||||||||||||||||||||||||||
| 123 | "UPDATE $table", | ||||||||||||||||||||||||||||
| 124 | "SET " . join(', ', map "$_=?", @cols), | ||||||||||||||||||||||||||||
| 125 | "WHERE " . join( ' AND ', map "$_=?", keys %$where ); | ||||||||||||||||||||||||||||
| 126 | |||||||||||||||||||||||||||||
| 127 | 8 | my $stmt = { | |||||||||||||||||||||||||||
| 128 | sql => $sql, | ||||||||||||||||||||||||||||
| 129 | bind => \@values, | ||||||||||||||||||||||||||||
| 130 | }; | ||||||||||||||||||||||||||||
| 131 | |||||||||||||||||||||||||||||
| 132 | 8 | return $stmt; | |||||||||||||||||||||||||||
| 133 | } | ||||||||||||||||||||||||||||
| 134 | |||||||||||||||||||||||||||||
| 135 | sub _stmt_columns { | ||||||||||||||||||||||||||||
| 136 | 208 | ▶ | my $self = shift; | ||||||||||||||||||||||||||
|
Subroutine
_stmt_columns: called CC=4 SCAR=13.9
| |||||||||||||||||||||||||||||
| 137 | 208 | my $args = shift; | |||||||||||||||||||||||||||
| 138 | 208 | my ( $fields, $type ) = @{$args}{qw< fields type >}; | |||||||||||||||||||||||||||
| 139 | |||||||||||||||||||||||||||||
| 140 | 208 | my $ref = ref $fields; | |||||||||||||||||||||||||||
| 141 | |||||||||||||||||||||||||||||
| 142 | 208 | ▶ | return [ $self->ID_COLUMN, @$fields ] if $ref eq 'ARRAY'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 143 | |||||||||||||||||||||||||||||
| 144 | 76 | ▶ | $ref eq 'HASH' and exists $fields->{$type} | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$ref eq "HASH" and exists $fields->{$type}
MC/DC100%2/2
$ref eq "HASH" and exists $fields->{$type}
$ref eq "HASH"
exists $fields->{$type}
Truth table100%3/3
A and B
A= $ref eq "HASH"B= exists $fields->{$type}
| |||||||||||||||||||||||||||||
| 145 | or return $self->COLUMNS; | ||||||||||||||||||||||||||||
| 146 | |||||||||||||||||||||||||||||
| 147 | my @fields_minus_relationship_keys = | ||||||||||||||||||||||||||||
| 148 | 12 | grep { !exists $self->RELATIONS->{$_} } | |||||||||||||||||||||||||||
| 149 | 10 | @{ $fields->{$type} }; | |||||||||||||||||||||||||||
| 150 | |||||||||||||||||||||||||||||
| 151 | 10 | return +[ $self->ID_COLUMN, @fields_minus_relationship_keys ]; | |||||||||||||||||||||||||||
| 152 | } | ||||||||||||||||||||||||||||
| 153 | |||||||||||||||||||||||||||||
| 154 | sub _stmt_filters { | ||||||||||||||||||||||||||||
| 155 | 208 | ▶ | my ( $self, $type, $filter ) = @_; | ||||||||||||||||||||||||||
|
Subroutine
_stmt_filters: called CC=2 SCAR=7.0
| |||||||||||||||||||||||||||||
| 156 | |||||||||||||||||||||||||||||
| 157 | 208 | ▶ | return $filter if $self->TABLE ne $type; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 158 | |||||||||||||||||||||||||||||
| 159 | return +{ | ||||||||||||||||||||||||||||
| 160 | 193 | map { $_ => $filter->{$_} } | |||||||||||||||||||||||||||
| 161 | 640 | grep { exists $filter->{$_} } | |||||||||||||||||||||||||||
| 162 | 208 | @{ $self->COLUMNS } | |||||||||||||||||||||||||||
| 163 | }; | ||||||||||||||||||||||||||||
| 164 | } | ||||||||||||||||||||||||||||
| 165 | |||||||||||||||||||||||||||||
| 166 | 8 | __PACKAGE__->meta->make_immutable; | |||||||||||||||||||||||||||
| 167 | 8 | ▶ | no Moose; 1; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 168 | |||||||||||||||||||||||||||||
| 169 | __END__ | ||||||||||||||||||||||||||||
| 170 | |||||||||||||||||||||||||||||
| 171 | =pod | ||||||||||||||||||||||||||||
| 172 | |||||||||||||||||||||||||||||
| 173 | =encoding UTF-8 | ||||||||||||||||||||||||||||
| 174 | |||||||||||||||||||||||||||||
| 175 | =head1 NAME | ||||||||||||||||||||||||||||
| 176 | |||||||||||||||||||||||||||||
| 177 | Test::PONAPI::Repository::MockDB::Table - mock repository - table class | ||||||||||||||||||||||||||||
| 178 | |||||||||||||||||||||||||||||
| 179 | =head1 VERSION | ||||||||||||||||||||||||||||
| 180 | |||||||||||||||||||||||||||||
| 181 | version 0.002012 | ||||||||||||||||||||||||||||
| 182 | |||||||||||||||||||||||||||||
| 183 | =head1 AUTHORS | ||||||||||||||||||||||||||||
| 184 | |||||||||||||||||||||||||||||
| 185 | =over 4 | ||||||||||||||||||||||||||||
| 186 | |||||||||||||||||||||||||||||
| 187 | =item * | ||||||||||||||||||||||||||||
| 188 | |||||||||||||||||||||||||||||
| 189 | Mickey Nasriachi <mickey@cpan.org> | ||||||||||||||||||||||||||||
| 190 | |||||||||||||||||||||||||||||
| 191 | =item * | ||||||||||||||||||||||||||||
| 192 | |||||||||||||||||||||||||||||
| 193 | Stevan Little <stevan@cpan.org> | ||||||||||||||||||||||||||||
| 194 | |||||||||||||||||||||||||||||
| 195 | =item * | ||||||||||||||||||||||||||||
| 196 | |||||||||||||||||||||||||||||
| 197 | Brian Fraser <hugmeir@cpan.org> | ||||||||||||||||||||||||||||
| 198 | |||||||||||||||||||||||||||||
| 199 | =back | ||||||||||||||||||||||||||||
| 200 | |||||||||||||||||||||||||||||
| 201 | =head1 COPYRIGHT AND LICENSE | ||||||||||||||||||||||||||||
| 202 | |||||||||||||||||||||||||||||
| 203 | This software is copyright (c) 2016 by Mickey Nasriachi, Stevan Little, Brian Fraser. | ||||||||||||||||||||||||||||
| 204 | |||||||||||||||||||||||||||||
| 205 | This is free software; you can redistribute it and/or modify it under | ||||||||||||||||||||||||||||
| 206 | the same terms as the Perl 5 programming language system itself. | ||||||||||||||||||||||||||||
| 207 | |||||||||||||||||||||||||||||
| 208 | =cut | ||||||||||||||||||||||||||||