Gideon/Driver/DBI.pm

filter: Statementstmt 12.3% 19 / 154 Branchbran 0.0% 0 / 38 Conditioncond 0.0% 0 / 12 MC/DCmcdc 0.0% 0 / 8 Subroutinesub 25.0% 5 / 20 Podpod n/a totaltotal 10.3% 24 / 232 CC 32 SCAR 33.4
CC
32
Cov
9%
_insert_object
90.0
_translate_order_by
42.0
_update_object
30.0
CRAP 28.2
« Gideon/ResultSet.pm ↑ Summary Gideon/Driver.pm »
1 package Gideon::Driver::DBI;
2 {
3 1 $Gideon::Driver::DBI::VERSION = '0.0.3';
4 }
5 1 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use Gideon::Meta::Attribute::Trait::DBI::Column;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use Gideon::Meta::Attribute::Trait::Inflate;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use Gideon::Meta::Attribute::Trait::DBI::Inflate::DateTime;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use SQL::Abstract::Limit;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 #ABSTRACT: Gideon DBI driver
12  
13 1 with 'Gideon::Driver';
14  
15 0 ▶sub _find {
Subroutine _find: not called CC=3 SCAR=24.8
16 0 my ( $self, $target, $query, $order_by, $limit ) = @_;
17  
18 0 my ( $dbh, $table ) = $self->_get_dbh_and_table($target);
19  
20 0 my $mapping = $self->_get_column_mapping($target);
21 0 my @columns_names = values %$mapping;
22 0 $query = $self->_translate_query( $query, $mapping );
23 0 $order_by = $self->_translate_order_by( $order_by, $mapping );
24  
25 0 my ( $stmt, @bind ) =
26 SQL::Abstract::Limit->new( limit_dialect => $dbh )
27 ->select( $table, \@columns_names, $query, $order_by, $limit );
28  
29 0 my $sth = $dbh->prepare_cached($stmt);
30 0 $sth->execute(@bind);
31  
32 0 my $inflators = $self->_get_inflators( $target, $dbh );
33 0 my @instances;
34  
35 0 ▶ while ( my $data = $sth->fetchrow_hashref ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $data = $sth->fetchrow_hashref) 0 0 0
36 0 @$data{ keys %$mapping } = delete @$data{ values %$mapping };
37 0 $data->{__is_persisted} = 1;
38 0 $data->{$_} = $inflators->{$_}->( $data->{$_} ) for keys %$inflators;
39 0 push @instances, $target->new(%$data);
40 }
41  
42 0 return \@instances;
43 }
44  
45 0 ▶sub _insert_object {
Subroutine _insert_object: not called CC=9 SCAR=45.0
46 0 my ( $self, $target ) = @_;
47  
48 0 my ( $dbh, $table ) = $self->_get_dbh_and_table($target);
49  
50 0 my @columns = $self->_get_columns($target);
51 0 my ($serial) = map { $_->name } grep { $_->serial } @columns;
52 0 my $mapping = $self->_get_column_mapping($target);
53  
54 0 my $deflators = $self->_get_deflators($target,$dbh);
55  
56 0 my %data;
57  
58 0 foreach my $attr ( keys %$mapping ) {
59 0 my $value = $target->$attr();
60 0 ▶ $value = $deflators->{$attr}->($value) if $deflators->{$attr};
Branch: 1 branch
BranchTrueFalseTotal
if $deflators->{$attr} 0 0 0
61 0 $data{ $mapping->{$attr} } = $value;
62 }
63  
64 0 ▶ delete $data{$serial} if $serial and not defined $data{$serial};
Branch: 1 branch
BranchTrueFalseTotal
if $serial and not defined $data{$serial} 0 0 0
Condition0%0/3
$serial and not defined $data{$serial}
!ll&&!rl&&r
000
MC/DC0%0/2
$serial and not defined $data{$serial}
$serial not defined $data{$serial}
Truth table0%0/3
A and not(B)
A= $serialB= not defined $data{$serial}
ABresult
0X0
100
111
65  
66 0 my ( $stmt, @bind ) =
67 SQL::Abstract::Limit->new( limit_dialect => $dbh )
68 ->insert( $table, \%data );
69  
70 0 my $sth = $dbh->prepare($stmt);
71 0 my $rv = $sth->execute(@bind);
72  
73 0 ▶ if ( $rv > 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rv > 0) 0 0 0
74 0 my @columns = $self->_get_columns($target);
75  
76 0 ▶ if ( $serial and not defined $data{$serial} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($serial and not defined $data{$serial}) 0 0 0
Condition0%0/3
$serial and not defined $data{$serial}
!ll&&!rl&&r
000
MC/DC0%0/2
$serial and not defined $data{$serial}
$serial not defined $data{$serial}
Truth table0%0/3
A and not(B)
A= $serialB= not defined $data{$serial}
ABresult
0X0
100
111
77 0 $target->$serial(
78 $dbh->last_insert_id( undef, undef, $table, $serial ) );
79 }
80 }
81  
82 0 ▶ $target->__is_persisted(1) if $rv > 0;
Branch: 1 branch
BranchTrueFalseTotal
if $rv > 0 0 0 0
83 }
84  
85 0 ▶sub _remove {
Subroutine _remove: not called CC=2 SCAR=17.9
86 0 my ( $self, $target, $orig_where, $limit ) = @_;
87  
88 0 my ( $dbh, $table ) = $self->_get_dbh_and_table($target);
89  
90 0 my $mapping = $self->_get_column_mapping($target);
91 0 my $where = $self->_translate_query( $orig_where, $mapping );
92  
93 0 my ( $stmt, @bind ) =
94 SQL::Abstract::Limit->new( limit_dialect => $dbh )
95 ->delete( $table, $where, $limit );
96  
97 0 my $sth = $dbh->prepare($stmt);
98 0 my $rv = $sth->execute(@bind);
99  
100 0 ▶ return 1 if $rv > 0;
Branch: 1 branch
BranchTrueFalseTotal
if $rv > 0 0 0 0
101 }
102  
103 0 ▶sub _remove_object {
Subroutine _remove_object: not called CC=2 SCAR=17.9
104 0 my ( $self, $target ) = @_;
105  
106 0 ▶ Gideon::Exception::ObjectNotStored->throw unless $target->__is_persisted;
Branch: 1 branch
BranchTrueFalseTotal
unless $target->__is_persisted 0 0 0
107  
108 0 my $where = $self->_compute_primary_key($target);
109 0 $self->_remove( $target, $where, 1 );
110 }
111  
112 0 ▶sub _update {
Subroutine _update: not called CC=2 SCAR=17.9
113 0 my ( $self, $target, $orig_changes, $orig_where, $limit ) = @_;
114  
115 0 my ( $dbh, $table ) = $self->_get_dbh_and_table($target);
116  
117 0 my $mapping = $self->_get_column_mapping($target);
118 0 my $where = $self->_translate_query( $orig_where, $mapping );
119 0 my $changes = $self->_translate_query( $orig_changes, $mapping );
120  
121 0 my ( $stmt, @bind ) =
122 SQL::Abstract::Limit->new( limit_dialect => $dbh )
123 ->update( $table, $changes, $where, $limit );
124  
125 0 my $sth = $dbh->prepare($stmt);
126 0 my $rv = $sth->execute(@bind);
127  
128 0 ▶ return 1 if $rv > 0;
Branch: 1 branch
BranchTrueFalseTotal
if $rv > 0 0 0 0
129 }
130  
131 0 ▶sub _update_object {
Subroutine _update_object: not called CC=5 SCAR=34.0
132 0 my ( $self, $target, $changes ) = @_;
133  
134 0 ▶ Gideon::Exception::ObjectNotStored->throw unless $target->__is_persisted;
Branch: 1 branch
BranchTrueFalseTotal
unless $target->__is_persisted 0 0 0
135  
136 0 ▶ $changes ||= $self->_compute_changes($target);
Condition0%0/3
$changes ||= $self->_compute_changes($target)
l!l&&r!l&&!r
000
MC/DC0%0/2
$changes ||= $self->_compute_changes($target)
$changes $self->_compute_changes($target)
Truth table0%0/3
A ||= B
A= $changesB= $self->_compute_changes($target)
ABresult
1X1
011
000
137 0 ▶ return 1 unless %$changes;
Branch: 1 branch
BranchTrueFalseTotal
unless %$changes 0 0 0
138  
139 0 my $where = $self->_compute_primary_key($target);
140 0 my $rv = $self->_update( $target, $changes, $where, 1 );
141  
142 0 ▶ if ($rv) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rv) 0 0 0
143 0 my @columns = $self->_get_columns($target);
144 }
145  
146 0 return $rv;
147 }
148  
149 0 ▶sub _compute_changes {
Subroutine _compute_changes: not called CC=2 SCAR=17.9
150 0 my ( $self, $target ) = @_;
151  
152 0 my @columns = $self->_get_columns( ref $target );
153 0 my $changes = {};
154  
155 0 $changes->{$_} = $target->$_() for map { $_->name } @columns;
156  
157 0 return $changes;
158 }
159  
160 0 ▶sub _compute_primary_key {
Subroutine _compute_primary_key: not called CC=3 SCAR=24.8
161 0 my ( $self, $target ) = @_;
162  
163 0 my @columns = $self->_get_columns( ref $target );
164 0 my @primary_key = grep { $_->primary_key } @columns;
165  
166 0 ▶ unless ( scalar @primary_key ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (scalar @primary_key) 0 0 0
167 0 @primary_key = @columns;
168 }
169  
170 0 my $where = {};
171 0 $where->{$_} = $target->$_() for map { $_->name } @primary_key;
172  
173 0 return $where;
174 }
175  
176 0 ▶sub _get_dbh_and_table {
Subroutine _get_dbh_and_table: not called CC=1 SCAR=6.9
177 0 my ( $self, $target ) = @_;
178  
179 0 my ( $store, $table ) = split ':', $target->meta->store;
180 0 my $dbh = Gideon::Registry->get_store($store);
181  
182 0 return ( $dbh, $table );
183 }
184  
185 0 ▶sub _get_columns {
Subroutine _get_columns: not called CC=1 SCAR=6.9
186 0 my ( $self, $target ) = @_;
187  
188 return
189 0 grep { $_->does('Gideon::DBI::Column') }
190 $target->meta->get_all_attributes;
191 }
192  
193 0 ▶sub _get_inflators {
Subroutine _get_inflators: not called CC=3 SCAR=24.8
194 0 my ( $self, $target, $source ) = @_;
195  
196 0 my @attributes = $target->meta->get_all_attributes;
197 0 my @inflated = grep { $_->does('Gideon::Inflated') } @attributes;
198  
199 0 my %inflators;
200  
201 0 for (@inflated) {
202 0 my $name = $_->name;
203 0 my $inflator = $_->get_inflator($source);
204 0 ▶ $inflators{$name} = $inflator if $inflator;
Branch: 1 branch
BranchTrueFalseTotal
if $inflator 0 0 0
205 }
206  
207 0 return \%inflators;
208 }
209  
210 0 ▶sub _get_deflators {
Subroutine _get_deflators: not called CC=3 SCAR=24.8
211 0 my ( $self, $target, $source ) = @_;
212  
213 0 my @attributes = $target->meta->get_all_attributes;
214 0 my @inflated = grep { $_->does('Gideon::Inflated') } @attributes;
215  
216 0 my %deflators;
217  
218 0 for (@inflated) {
219 0 my $name = $_->name;
220 0 my $deflator = $_->get_deflator($source);
221 0 ▶ $deflators{$name} = $deflator if $deflator;
Branch: 1 branch
BranchTrueFalseTotal
if $deflator 0 0 0
222 }
223  
224 0 return \%deflators;
225 }
226  
227  
228 0 ▶sub _get_column_mapping {
Subroutine _get_column_mapping: not called CC=2 SCAR=17.9
229 0 my ( $self, $target ) = @_;
230  
231 0 my @columns = $self->_get_columns($target);
232 0 ▶ my @column_names = map { $_->column || $_->name } @columns;
Condition0%0/3
$_->column || $_->name
l!l&&r!l&&!r
000
MC/DC0%0/2
$_->column || $_->name
$_->column $_->name
Truth table0%0/3
A || B
A= $_->columnB= $_->name
ABresult
1X1
011
000
233 0 my @attribute_names = map { $_->name } @columns;
234  
235 0 my %mapping;
236 0 @mapping{@attribute_names} = @column_names;
237  
238 0 return \%mapping;
239 }
240  
241 0 ▶sub _translate_query {
Subroutine _translate_query: not called CC=2 SCAR=17.9
242 0 my ( $self, $orig_query, $mapping ) = @_;
243  
244 0 my $query = \%$orig_query;
245 0 $query->{ $mapping->{$_} } = delete $query->{$_} for keys %$query;
246 0 return $query;
247 }
248  
249 0 ▶sub _translate_order_by {
Subroutine _translate_order_by: not called CC=6 SCAR=37.4
250 0 my ( $self, $order_by, $mapping ) = @_;
251  
252 0 ▶ return unless $order_by;
Branch: 1 branch
BranchTrueFalseTotal
unless $order_by 0 0 0
253  
254 0 ▶ if ( ref $order_by eq 'HASH' ) {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $order_by eq 'HASH') { } 0 0 0
elsif (ref $order_by eq 'ARRAY') { } 0 0 0
255 0 $order_by->{$_} = $mapping->{ $order_by->{$_} } for keys %$order_by;
256 }
257  
258 elsif ( ref $order_by eq 'ARRAY' ) {
259 0 my @new_order_by = map { $mapping->{$_} } @$order_by;
260 0 $order_by = \@new_order_by;
261 }
262  
263 else {
264 0 my ( $column, $order ) = split( ' ', $order_by );
265 0 ▶ $order_by = $mapping->{$column} . ( $order ? " $order" : '' );
Branch: 1 branch
BranchTrueFalseTotal
$order ? : 0 0 0
266 }
267  
268 0 return $order_by;
269 }
270  
271 1 1;
272  
273 __END__
« Gideon/ResultSet.pm ↑ Summary Gideon/Driver.pm »