Slick/DatabaseExecutor.pm untested

filter: Statementstmt 0.0% 0 / 50 Branchbran 0.0% 0 / 4 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 0.0% 0 / 10 Podpod 0.0% 0 / 2 totaltotal 0.0% 0 / 66 CC 2 SCAR 10.0
CC
2
Cov
0%
select_one
6.0
select
2.0
BEGIN
2.0
CRAP 2.7
1 package Slick::DatabaseExecutor;
2  
3 use 5.036;
4  
5 use Moo::Role;
6 use Types::Standard qw(HashRef);
7 use Carp qw(croak);
8 use SQL::Abstract;
9 use Scalar::Util qw(blessed);
10 use Slick::Util;
11 use namespace::clean;
12  
13 foreach my $sql_verb (qw(insert update delete)) {
14 Slick::Util::monkey_patch(
15 __PACKAGE__,
16 $sql_verb,
17 sub {
18 my ( $self, @args ) = @_;
19 my ( $stmt, @bind ) = $self->sql->$sql_verb(@args);
20 my $sth = $self->prepare($stmt);
21 return $sth->execute(@bind);
22 }
23 );
24  
25 }
26  
27 has sql => (
28 is => 'ro',
29 isa => sub {
30 my $s = shift;
31 croak qq{Invalid type for sql, expected SQL::Abstract got } . $s
32 unless blessed($s) eq 'SQL::Abstract';
33 },
34 default => sub { SQL::Abstract->new; }
35 );
36  
37 has dbi => ( is => 'ro', handles => [ 'do', 'prepare' ] );
38  
39 has connection => ( is => 'ro' );
40  
41 has dbi_options => (
42 is => 'rw',
43 isa => HashRef,
44 default => sub {
45 return {
46 AutoCommit => 1,
47 PrintError => 1,
48 RaiseError => 0
49 };
50 }
51 );
52  
53 ## no critic qw(Subroutines::ProhibitBuiltinHomonyms)
54 sub select {
55 my ( $self, @args ) = @_;
56 my ( $stmt, @bind ) = $self->sql->select(@args);
57 my $sth = $self->prepare( $stmt, @bind );
58 $sth->execute(@bind);
59 return $sth->fetchall_arrayref( {} );
60 }
61  
62 ## no critic qw(Subroutines::ProhibitExplicitReturnUndef)
63 sub select_one {
64 my ( $self, @args ) = @_;
65 my $ar = $self->select(@args);
66  
67 if ( $ar->@* ) {
68 return $ar->[0];
69 }
70  
71 return undef;
72 }
73  
74 1;
75  
76 =encoding utf8
77  
78 =head1 NAME
79  
80 Slick::DatabaseExecutor
81  
82 =head1 SYNOPSIS
83  
84 A L<Moo::Role> implemented by all of the databases supported by L<Slick>.
85  
86 =head1 See also
87  
88 =over2
89  
90 =item * L<Slick::CacheExecutor>
91  
92 =item * L<Slick::CacheExecutor::Redis>
93  
94 =item * L<Slick::CacheExecutor::Memcached>
95  
96 =item * L<Slick::Context>
97  
98 =item * L<Slick::Database>
99  
100 =item * L<Slick::DatabaseExecutor::MySQL>
101  
102 =item * L<Slick::DatabaseExecutor::Pg>
103  
104 =item * L<Slick::EventHandler>
105  
106 =item * L<Slick::Events>
107  
108 =item * L<Slick::Methods>
109  
110 =item * L<Slick::RouteMap>
111  
112 =item * L<Slick::Util>
113  
114 =back
115  
116 =cut