Simple.pm

filter: Statementstmt 73.8% 341 / 462 Branchbran 53.7% 128 / 238 Conditioncond 36.8% 38 / 103 MC/DCmcdc 11.8% 7 / 59 Subroutinesub 77.9% 53 / 68 Podpod 87.1% 27 / 31 totaltotal 61.8% 594 / 961 CC 186 SCAR 23.9
CC
186
Cov
63%
has_one
42.0
_insert
33.5
__ANON__
29.9
CRAP 10.9
« Simple/Find.pm ↑ Summary Simple/Utils.pm »
1 package ActiveRecord::Simple;
2  
3 13 ▶use 5.010;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 13 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 13 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 13 our $VERSION = '1.11';
8  
9 13 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 13 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 13 ▶use Scalar::Util qw/blessed/;
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 13 ▶use ActiveRecord::Simple::QueryManager;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 13 ▶use ActiveRecord::Simple::Utils qw/all_blessed class_to_table_name load_module/;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 13 ▶use ActiveRecord::Simple::Connect;
Subroutine BEGIN: called CC=1 SCAR=0.0
16  
17 13 our $connector;
18 13 my $qm = ActiveRecord::Simple::QueryManager->new();
19  
20  
21 141 ▶sub new {
Subroutine new: called CC=4 SCAR=13.9
Pod documented
22 141 my $class = shift;
23 141 ▶ my $params = (scalar @_ > 1) ? {@_} : $_[0];
Branch: 1 branch
BranchTrueFalseTotal
scalar @_ > 1 ? : 2 139 141
24  
25 # relations
26 141 ▶ $class->_init_relations if $class->can('_get_relations');
Branch: 1 branch
BranchTrueFalseTotal
if $class->can("_get_relations") 60 81 141
27  
28 141 ▶ return bless $params || {}, $class;
Condition100%2/2
$params || {}
l!l
1374
MC/DC100%1/1
$params || {}
$params
Truth table100%2/2
A
A= $params
Aresult
11
00
29 }
30  
31 10 ▶sub auto_load {
Subroutine auto_load: called CC=7 SCAR=20.1
Pod documented
32 10 my ($class) = @_;
33  
34 10 my $table_name = class_to_table_name($class);
35  
36 # 0. check the name
37 10 my $table_info_sth = $class->dbh->table_info('', '%', $table_name, 'TABLE');
38 10 ▶ $table_info_sth->fetchrow_hashref or croak "Can't find table '$table_name' in the database";
Branch: 1 branch
BranchTrueFalseTotal
$table_info_sth->fetchrow_hashref or croak("Can't find table '${table_name}' in the database") 10 0 10
39  
40 # 1. columns list
41 10 my $column_info_sth = $class->dbh->column_info(undef, undef, $table_name, undef);
42 10 my $cols = $column_info_sth->fetchall_arrayref({});
43  
44 10 my @columns = ();
45 10 push @columns, $_->{COLUMN_NAME} for @$cols;
46  
47 # 2. Primary key
48 10 my $primary_key_sth = $class->dbh->primary_key_info(undef, undef, $table_name);
49 10 my $primary_key_data = $primary_key_sth->fetchrow_hashref;
50 10 ▶ my $primary_key = ($primary_key_data) ? $primary_key_data->{COLUMN_NAME} : undef;
Branch: 1 branch
BranchTrueFalseTotal
$primary_key_data ? : 10 0 10
51  
52 10 ▶ $class->table_name($table_name) if $table_name;
Branch: 1 branch
BranchTrueFalseTotal
if $table_name 10 0 10
53 10 ▶ $class->primary_key($primary_key) if $primary_key;
Branch: 1 branch
BranchTrueFalseTotal
if $primary_key 10 0 10
54 10 ▶ $class->columns(@columns) if @columns;
Branch: 1 branch
BranchTrueFalseTotal
if @columns 10 0 10
55 }
56  
57 6 ▶sub connect {
Subroutine connect: called CC=3 SCAR=13.3
Pod documented
58 6 my ($class, $dsn, $username, $password, $options) = @_;
59  
60 6 eval { require DBIx::Connector };
61  
62 0 ▶ $options->{HandleError} = sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
63 0 my ($error_message, $DBI_st) = @_;
64  
65 0 ▶ $error_message or return;
Branch: 1 branch
BranchTrueFalseTotal
$error_message or return 0 0 0
66 0 croak $error_message;
67  
68 6 ▶ } if ! exists $options->{HandleError};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $options->{"HandleError"} 6 0 6
69  
70 6 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) { } 6 0 6
71 6 $connector = ActiveRecord::Simple::Connect->new($dsn, $username, $password, $options);
72 6 $connector->db_connect;
73 }
74 else {
75 0 $connector = DBIx::Connector->new($dsn, $username, $password, $options);
76 }
77  
78 6 return 1;
79 }
80  
81 5 ▶sub belongs_to {
Subroutine belongs_to: called CC=5 SCAR=19.7
Pod documented
82 5 my ($class, $rel_name, $rel_class, $params) = @_;
83  
84 5 my $new_relation = {
85 class => $rel_class,
86 type => 'one',
87 };
88  
89 my $primary_key = $params->{pk} ||
90 $params->{primary_key} ||
91 5 ▶ _guess(primary_key => $class);
Condition33%2/6
$params->{'pk'} || $params->{'primary_key'} || _guess('primary_key', $class)
l!l&&r!l&&!r
050
$params->{"pk"} or $params->{"primary_key"}
l!l&&r!l&&!r
005
MC/DC0%0/3
$params->{'pk'} || $params->{'primary_key'} || _guess('primary_key', $class)
$params->{"pk"} $params->{"primary_key"} _guess('primary_key', $class)
Truth table25%1/4
A or B || C
A= $params->{"pk"}B= $params->{"primary_key"}C= _guess('primary_key', $class)
ABCresult
1XX1
01X1
0011
0000
92  
93 my $foreign_key = $params->{fk} ||
94 $params->{foreign_key} ||
95 5 ▶ _guess(foreign_key => $rel_class);
Condition33%2/6
$params->{'fk'} || $params->{'foreign_key'} || _guess('foreign_key', $rel_class)
l!l&&r!l&&!r
050
$params->{"fk"} or $params->{"foreign_key"}
l!l&&r!l&&!r
005
MC/DC0%0/3
$params->{'fk'} || $params->{'foreign_key'} || _guess('foreign_key', $rel_class)
$params->{"fk"} $params->{"foreign_key"} _guess('foreign_key', $rel_class)
Truth table25%1/4
A or B || C
A= $params->{"fk"}B= $params->{"foreign_key"}C= _guess('foreign_key', $rel_class)
ABCresult
1XX1
01X1
0011
0000
96  
97 $new_relation->{params} = {
98 5 pk => $primary_key,
99 fk => $foreign_key,
100 };
101  
102 5 $class->_append_relation($rel_name => $new_relation);
103 #$class->_mk_relations_accessors;
104 }
105  
106 17 ▶sub has_many {
Subroutine has_many: called CC=7 SCAR=21.8
Pod documented
107 17 my ($class, $rel_name, $rel_class, $params) = @_;
108  
109 17 my $new_relation = {
110 class => $rel_class,
111 type => 'many',
112 };
113  
114 17 ▶ $params ||= {};
Condition100%2/2
$params ||= {}
l!l
107
MC/DC100%1/1
$params ||= {}
$params
Truth table100%2/2
A
A= $params
Aresult
11
00
115 my $primary_key = $params->{pk} ||
116 $params->{primary_key} ||
117 17 ▶ _guess(primary_key => $class);
Condition33%2/6
$params->{'pk'} || $params->{'primary_key'} || _guess('primary_key', $class)
l!l&&r!l&&!r
0170
$params->{"pk"} or $params->{"primary_key"}
l!l&&r!l&&!r
0017
MC/DC0%0/3
$params->{'pk'} || $params->{'primary_key'} || _guess('primary_key', $class)
$params->{"pk"} $params->{"primary_key"} _guess('primary_key', $class)
Truth table25%1/4
A or B || C
A= $params->{"pk"}B= $params->{"primary_key"}C= _guess('primary_key', $class)
ABCresult
1XX1
01X1
0011
0000
118  
119 my $foreign_key = $params->{fk} ||
120 $params->{foreign_key} ||
121 17 ▶ _guess(foreign_key => $class);
Condition33%2/6
$params->{'fk'} || $params->{'foreign_key'} || _guess('foreign_key', $class)
l!l&&r!l&&!r
0170
$params->{"fk"} or $params->{"foreign_key"}
l!l&&r!l&&!r
0017
MC/DC0%0/3
$params->{'fk'} || $params->{'foreign_key'} || _guess('foreign_key', $class)
$params->{"fk"} $params->{"foreign_key"} _guess('foreign_key', $class)
Truth table25%1/4
A or B || C
A= $params->{"fk"}B= $params->{"foreign_key"}C= _guess('foreign_key', $class)
ABCresult
1XX1
01X1
0011
0000
122  
123 $new_relation->{params} = {
124 17 pk => $primary_key,
125 fk => $foreign_key,
126 };
127  
128 17 ▶ $new_relation->{via_table} = $params->{via} if $params->{via};
Branch: 1 branch
BranchTrueFalseTotal
if $params->{"via"} 10 7 17
129  
130 17 $class->_append_relation($rel_name => $new_relation);
131 #$class->_mk_relations_accessors;
132 }
133  
134 0 ▶sub has_one {
Subroutine has_one: not called CC=6 SCAR=37.4
Pod documented
135 0 my ($class, $rel_name, $rel_class, $params) = @_;
136  
137 0 my $new_relation = {
138 class => $rel_class,
139 type => 'only',
140 };
141  
142 0 ▶ $params ||= {};
Condition0%0/2
$params ||= {}
l!l
00
MC/DC0%0/1
$params ||= {}
$params
Truth table0%0/2
A
A= $params
Aresult
11
00
143 #my ($primary_key, $foreign_key);
144 my $primary_key = $params->{pk} ||
145 $params->{primary_key} ||
146 0 ▶ _guess(primary_key => $class);
Condition0%0/6
$params->{'pk'} || $params->{'primary_key'} || _guess('primary_key', $class)
l!l&&r!l&&!r
000
$params->{"pk"} or $params->{"primary_key"}
l!l&&r!l&&!r
000
MC/DC0%0/3
$params->{'pk'} || $params->{'primary_key'} || _guess('primary_key', $class)
$params->{"pk"} $params->{"primary_key"} _guess('primary_key', $class)
Truth table0%0/4
A or B || C
A= $params->{"pk"}B= $params->{"primary_key"}C= _guess('primary_key', $class)
ABCresult
1XX1
01X1
0011
0000
147  
148 my $foreign_key = $params->{fk} ||
149 $params->{foreign_key} ||
150 0 ▶ _guess(foreign_key => $class);
Condition0%0/6
$params->{'fk'} || $params->{'foreign_key'} || _guess('foreign_key', $class)
l!l&&r!l&&!r
000
$params->{"fk"} or $params->{"foreign_key"}
l!l&&r!l&&!r
000
MC/DC0%0/3
$params->{'fk'} || $params->{'foreign_key'} || _guess('foreign_key', $class)
$params->{"fk"} $params->{"foreign_key"} _guess('foreign_key', $class)
Truth table0%0/4
A or B || C
A= $params->{"fk"}B= $params->{"foreign_key"}C= _guess('foreign_key', $class)
ABCresult
1XX1
01X1
0011
0000
151  
152 $new_relation->{params} = {
153 0 pk => $primary_key,
154 fk => $foreign_key,
155 };
156  
157 0 $class->_append_relation($rel_name => $new_relation);
158 #$class->_mk_relations_accessors;
159 }
160  
161 0 ▶sub generic {
Subroutine generic: not called CC=1 SCAR=6.9
Pod documented
162 0 my ($class, $rel_name, $rel_class, $key) = @_;
163  
164 0 my $new_relation = {
165 class => $rel_class,
166 type => 'generic',
167 key => $key
168 };
169  
170 0 return $class->_append_relation($rel_name => $new_relation);
171 0 $class->_mk_relations_accessors;
172 }
173  
174 22 ▶sub columns {
Subroutine columns: called CC=5 SCAR=17.2
Pod documented
175 22 my ($class, @columns_list) = @_;
176  
177 22 ▶ croak "Error: array-ref no longer supported for 'columns' method, sorry"
Branch: 1 branch
BranchTrueFalseTotal
if scalar @columns_list == 1 and ref $columns_list[0] eq "ARRAY" 0 22 22
Condition33%1/3
scalar @columns_list == 1 and ref $columns_list[0] eq "ARRAY"
!ll&&!rl&&r
2200
MC/DC0%0/2
scalar @columns_list == 1 and ref $columns_list[0] eq "ARRAY"
scalar @columns_list == 1 ref $columns_list[0] eq "ARRAY"
Truth table33%1/3
A and B
A= scalar @columns_list == 1B= ref $columns_list[0] eq "ARRAY"
ABresult
0X0
100
111
178 if scalar @columns_list == 1 && ref $columns_list[0] eq 'ARRAY';
179  
180 22 $class->_mk_attribute_getter('_get_columns', \@columns_list);
181 22 ▶ $class->_mk_rw_accessors(\@columns_list) unless $class->can('_make_columns_accessors') && $class->_make_columns_accessors == 0;
Branch: 1 branch
BranchTrueFalseTotal
unless $class->can("_make_columns_accessors") and $class->_make_columns_accessors == 0 21 1 22
Condition66%2/3
$class->can("_make_columns_accessors") and $class->_make_columns_accessors == 0
!ll&&!rl&&r
2101
MC/DC50%1/2
$class->can("_make_columns_accessors") and $class->_make_columns_accessors == 0
$class->can("_make_columns_accessors") $class->_make_columns_accessors == 0
Truth table66%2/3
A and B
A= $class->can("_make_columns_accessors")B= $class->_make_columns_accessors == 0
ABresult
0X0
100
111
182 }
183  
184 1 ▶sub make_columns_accessors {
Subroutine make_columns_accessors: called CC=2 SCAR=7.1
Pod documented
185 1 my ($class, $flag) = @_;
186  
187 1 ▶ $flag //= 1; # default value
Condition50%1/2
$flag //= 1
l!l
10
MC/DC0%0/1
$flag //= 1
$flag
Truth table50%1/2
A
A= $flag
Aresult
11
00
188  
189 1 $class->_mk_attribute_getter('_make_columns_accessors', $flag);
190 }
191  
192 1 ▶sub mixins {
Subroutine mixins: called CC=1 SCAR=0.0
Pod documented
193 1 my ($class, %mixins) = @_;
194  
195 1 $class->_mk_attribute_getter('_get_mixins', \%mixins);
196 1 $class->_mk_ro_accessors([keys %mixins]);
197 }
198  
199 22 ▶sub primary_key {
Subroutine primary_key: called CC=1 SCAR=0.0
Pod documented
200 22 my ($class, $primary_key) = @_;
201  
202 22 $class->_mk_attribute_getter('_get_primary_key', $primary_key);
203 }
204  
205 0 ▶sub secondary_key {
Subroutine secondary_key: not called CC=1 SCAR=6.9
Pod documented
206 0 my ($class, $key) = @_;
207  
208 0 $class->_mk_attribute_getter('_get_secondary_key', $key);
209 }
210  
211 22 ▶sub table_name {
Subroutine table_name: called CC=1 SCAR=0.0
Pod documented
212 22 my ($class, $table_name) = @_;
213  
214 22 $class->_mk_attribute_getter('_get_table_name', $table_name);
215 }
216  
217 22 ▶sub relations {
Subroutine relations: called CC=1 SCAR=0.0
Pod documented
218 22 my ($class, $relations) = @_;
219  
220 22 $class->_mk_attribute_getter('_get_relations', $relations);
221 }
222  
223 360 ▶sub dbh {
Subroutine dbh: called CC=3 SCAR=11.2
Pod documented
224 360 my ($self, $dbh) = @_;
225  
226 360 ▶ if ($dbh) {
Branch: 1 branch
BranchTrueFalseTotal
if ($dbh) 2 358 360
227 2 ▶ if ($connector) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ActiveRecord::Simple::connector) { } 0 2 2
228 0 $connector->dbh($dbh);
229 }
230 else {
231 2 $connector = ActiveRecord::Simple::Connect->new();
232 2 $connector->dbh($dbh);
233 }
234 }
235  
236 360 return $connector->dbh;
237 }
238  
239 74 ▶sub objects {
Subroutine objects: called CC=1 SCAR=0.0
Pod documented
240 74 $qm->{caller} = shift;
241 74 return $qm;
242 }
243  
244 11 ▶sub save {
Subroutine save: called CC=22 SCAR=33.5
Pod documented
245 11 my ($self) = @_;
246  
247 #return unless $self->dbh;
248 11 ▶ croak "Undefined database handler" unless $self->dbh;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->dbh 0 11 11
249  
250 croak 'Object is read-only'
251 11 ▶ if exists $self->{read_only} && $self->{read_only} == 1;
Branch: 1 branch
BranchTrueFalseTotal
if exists $self->{"read_only"} and $self->{"read_only"} == 1 0 11 11
Condition33%1/3
exists $self->{"read_only"} and $self->{"read_only"} == 1
!ll&&!rl&&r
1100
MC/DC0%0/2
exists $self->{"read_only"} and $self->{"read_only"} == 1
exists $self->{"read_only"} $self->{"read_only"} == 1
Truth table33%1/3
A and B
A= exists $self->{"read_only"}B= $self->{"read_only"} == 1
ABresult
0X0
100
111
252  
253 11 my $save_param = {};
254 11 my $fields = $self->_get_columns;
255  
256 11 ▶ my $pkey = ($self->can('_get_primary_key')) ? $self->_get_primary_key : undef;
Branch: 1 branch
BranchTrueFalseTotal
$self->can('_get_primary_key') ? : 11 0 11
257  
258 FIELD:
259 11 for my $field (@$fields) {
260 39 ▶ next FIELD if defined $pkey && $field eq $pkey && !$self->{$pkey};
Branch: 1 branch
BranchTrueFalseTotal
if defined $pkey and $field eq $pkey and not $self->{$pkey} 3 36 39
Condition83%5/6
defined $pkey and $field eq $pkey and not $self->{$pkey}
!ll&&!rl&&r
2883
defined $pkey and $field eq $pkey
!ll&&!rl&&r
02811
MC/DC66%2/3
defined $pkey and $field eq $pkey and not $self->{$pkey}
defined $pkey $field eq $pkey not $self->{$pkey}
Truth table75%3/4
A and B and not(C)
A= defined $pkeyB= $field eq $pkeyC= not $self->{$pkey}
ABCresult
0XX0
10X0
1100
1111
261 36 ▶ next FIELD if ref $field && ref $field eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
if ref $field and ref $field eq "HASH" 0 36 36
Condition33%1/3
ref $field and ref $field eq "HASH"
!ll&&!rl&&r
3600
MC/DC0%0/2
ref $field and ref $field eq "HASH"
ref $field ref $field eq "HASH"
Truth table33%1/3
A and B
A= ref $fieldB= ref $field eq "HASH"
ABresult
0X0
100
111
262 36 $save_param->{$field} = $self->{$field};
263 }
264  
265 ### Get additional fields from related objects:
266 11 for my $field (keys %$self) {
267 44 ▶ next unless ref $self->{$field};
Branch: 1 branch
BranchTrueFalseTotal
unless ref $self->{$field} 42 2 44
268 2 ▶ next unless $self->can('_get_relations');
Branch: 1 branch
BranchTrueFalseTotal
unless $self->can("_get_relations") 0 2 2
269 2 ▶ next unless grep { $_ eq $field } keys %{ $self->_get_relations };
Branch: 1 branch
BranchTrueFalseTotal
unless grep {$_ eq $field;} keys %{$self->_get_relations;} 1 1 2
270  
271 1 ▶ my $relation = $self->_get_relations->{$field} or next;
Branch: 1 branch
BranchTrueFalseTotal
my $relation = $self->_get_relations->{$field} or next 1 0 1
272 1 ▶ next unless $relation->{type} && $relation->{type} eq 'one';
Branch: 1 branch
BranchTrueFalseTotal
unless $relation->{"type"} and $relation->{"type"} eq "one" 0 1 1
Condition33%1/3
$relation->{"type"} and $relation->{"type"} eq "one"
!ll&&!rl&&r
001
MC/DC0%0/2
$relation->{"type"} and $relation->{"type"} eq "one"
$relation->{"type"} $relation->{"type"} eq "one"
Truth table33%1/3
A and B
A= $relation->{"type"}B= $relation->{"type"} eq "one"
ABresult
0X0
100
111
273  
274 1 my $fk = $relation->{params}{fk};
275 1 my $pk = $relation->{params}{pk};
276  
277 1 $save_param->{$fk} = $self->{$field}->$pk;
278 }
279  
280 11 my $result;
281 11 ▶ if ($self->{isin_database}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'isin_database'}) { } 5 6 11
282 5 $result = $self->_update($save_param);
283 }
284 else {
285 6 $result = $self->_insert($save_param);
286 }
287 11 ▶ $self->{need_to_save} = 0 if $result;
Branch: 1 branch
BranchTrueFalseTotal
if $result 11 0 11
288 11 ▶ delete $self->{SQL} if $result;
Branch: 1 branch
BranchTrueFalseTotal
if $result 11 0 11
289  
290 11 ▶ return (defined $result) ? $self : undef;
Branch: 1 branch
BranchTrueFalseTotal
defined $result ? : 11 0 11
291 }
292  
293 0 ▶sub update {
Subroutine update: not called CC=4 SCAR=30.0
Pod documented
294 0 my ($self, $params) = @_;
295  
296 0 my $fields = $self->_get_columns();
297 FIELD:
298 0 for my $field (@$fields) {
299 0 ▶ next FIELD if ! exists $params->{$field};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $params->{$field} 0 0 0
300 0 ▶ next FIELD if ! $params->{$field};
Branch: 1 branch
BranchTrueFalseTotal
unless $params->{$field} 0 0 0
301  
302 0 $self->{$field} = $params->{$field};
303 }
304  
305 0 return $self;
306 }
307  
308 # param:
309 # cascade => 1
310 1 ▶sub delete {
Subroutine delete: called CC=6 SCAR=18.7
Pod documented
311 1 my ($self, $param) = @_;
312  
313 1 ▶ return unless $self->dbh;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->dbh 0 1 1
314  
315 #my $table_name = $self->_table_name;
316 1 my $table_name = _what_is_the_table_name($self);
317 1 my $pkey = $self->_get_primary_key;
318  
319 1 ▶ return unless $self->{$pkey};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{$pkey} 0 1 1
320  
321 1 my $sql = qq{
322 DELETE FROM "$table_name" WHERE $pkey = ?
323 };
324 1 ▶ $sql .= ' CASCADE ' if $param && $param->{cascade};
Branch: 1 branch
BranchTrueFalseTotal
if $param and $param->{"cascade"} 0 1 1
Condition33%1/3
$param and $param->{"cascade"}
!ll&&!rl&&r
100
MC/DC0%0/2
$param and $param->{"cascade"}
$param $param->{"cascade"}
Truth table33%1/3
A and B
A= $paramB= $param->{"cascade"}
ABresult
0X0
100
111
325  
326 1 my $res = undef;
327 1 $sql = ActiveRecord::Simple::Utils::quote_sql_stmt($sql, $self->dbh->{Driver}{Name});
328  
329 1 ▶ if ( $self->dbh->do($sql, undef, $self->{$pkey}) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->dbh->do($sql, undef, $self->{$pkey})) 1 0 1
330 1 $self->{isin_database} = undef;
331 1 delete $self->{$pkey};
332  
333 1 $res = 1;
334 }
335  
336 1 return $res;
337 }
338  
339 0 ▶sub is_defined {
Subroutine is_defined: not called CC=1 SCAR=6.9
Pod documented
340 0 my ($self) = @_;
341  
342 0 return grep { defined $self->{$_} } @{ $self->_get_columns };
343 }
344  
345 # param:
346 # only_defined_fields => 1
347 ### TODO: refactor this
348 3 ▶sub to_hash {
Subroutine to_hash: called CC=7 SCAR=19.5
Pod documented
349 3 my ($self, $param) = @_;
350  
351 3 my $field_names = $self->_get_columns;
352 3 ▶ push @$field_names, keys %{ $self->_get_mixins } if $self->can('_get_mixins');
Branch: 1 branch
BranchTrueFalseTotal
if $self->can("_get_mixins") 2 1 3
353 3 my $attrs = {};
354  
355 3 for my $field (@$field_names) {
356 15 ▶ next if ref $field;
Branch: 1 branch
BranchTrueFalseTotal
if ref $field 0 15 15
357 15 ▶ if ( $param && $param->{only_defined_fields} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($param and $param->{'only_defined_fields'}) { } 7 8 15
Condition66%2/3
$param and $param->{"only_defined_fields"}
!ll&&!rl&&r
807
MC/DC50%1/2
$param and $param->{"only_defined_fields"}
$param $param->{"only_defined_fields"}
Truth table66%2/3
A and B
A= $paramB= $param->{"only_defined_fields"}
ABresult
0X0
100
111
358 7 ▶ $attrs->{$field} = $self->{$field} if defined $self->{$field};
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->{$field} 3 4 7
359 }
360 else {
361 8 $attrs->{$field} = $self->{$field};
362 }
363 }
364  
365 3 return $attrs;
366 }
367  
368 0 ▶sub increment {
Subroutine increment: not called CC=3 SCAR=24.8
Pod documented
369 0 my ($self, @fields) = @_;
370  
371 FIELD:
372 0 for my $field (@fields) {
373 0 ▶ next FIELD if not exists $self->{$field};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $self->{$field} 0 0 0
374 0 $self->{$field} += 1;
375 }
376  
377 0 return $self;
378 }
379  
380 0 ▶sub decrement {
Subroutine decrement: not called CC=3 SCAR=24.8
Pod documented
381 0 my ($self, @fields) = @_;
382  
383 FIELD:
384 0 for my $field (@fields) {
385 0 ▶ next FIELD if not exists $self->{$field};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $self->{$field} 0 0 0
386 0 $self->{$field} -= 1;
387 }
388  
389 0 return $self;
390 }
391  
392 #### Find ####
393  
394 0 ▶sub find {
Subroutine find: not called CC=1 SCAR=6.9
Pod documented
395 0 $qm->{caller} = shift;
396 carp
397 q/[DEPRECATED] This method is DEPRECATED since version 1.10. '/ .
398 0 q/Please, use "find" via "objects" method: / . $qm->{caller} . q/->objects->find/;
399 0 $qm->find(@_);
400 }
401  
402 0 ▶sub all {
Subroutine all: not called CC=1 SCAR=6.9
Pod documented
403 0 $qm->{caller} = shift;
404 carp
405 q/[DEPRECATED] This method is DEPRECATED since version 1.10. '/ .
406 0 q/Please, use "all" via "objects" method: / . $qm->{caller} . q/->objects->all/;
407 0 $qm->all();
408 }
409  
410 0 ▶sub get {
Subroutine get: not called CC=1 SCAR=6.9
Pod documented
411 0 $qm->{caller} = shift;
412 carp
413 q/[DEPRECATED] This method is DEPRECATED since version 1.10. '/ .
414 0 q/Please, use "get" via "objects" method: / . $qm->{caller} . q/->objects->get/;
415 0 $qm->get(@_);
416 }
417  
418 0 ▶sub exists {
Subroutine exists: not called CC=4 SCAR=30.0
Pod documented
419 0 my $first_arg = shift;
420  
421 0 my ($class, @search_criteria);
422 0 ▶ if (ref $first_arg) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $first_arg) { } 0 0 0
423 # FIXME: Ugly solution, need some beautifulness =)
424 # object method
425 0 $class = ref $first_arg;
426  
427 0 ▶ if ($class eq 'ActiveRecord::Simple::Find') {
Branch: 1 branch
BranchTrueFalseTotal
if ($class eq 'ActiveRecord::Simple::Find') { } 0 0 0
428 0 return $first_arg->exists;
429 }
430 else {
431 0 return ActiveRecord::Simple::Find->new($class, $first_arg->to_hash({ only_defined_fields => 1 }))->exists;
432 }
433 }
434 else {
435 0 carp '[DEPRECATED] This way of using method "exists" is deprecated. Please, see documentation to know how does it work now.';
436 0 $class = $first_arg;
437 0 @search_criteria = @_;
438 0 ▶ return (defined $class->find(@search_criteria)->fetch) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
defined $class->find(@search_criteria)->fetch ? : 0 0 0
439 }
440 }
441  
442 14 ▶sub _find_many_to_many { ActiveRecord::Simple::Find->_find_many_to_many(shift, @_) }
Subroutine _find_many_to_many: called CC=1 SCAR=0.0
443  
444 0 ▶sub DESTROY {}
Subroutine DESTROY: not called CC=1 SCAR=0.0
445  
446  
447 ### Private
448  
449 0 ▶sub _get_primary_key_value {
Subroutine _get_primary_key_value: not called CC=2 SCAR=17.9
450 0 my ($self) = @_;
451  
452 0 ▶ croak "Sory, you can call method '_get_primary_key_value' on unblessed scalar."
Branch: 1 branch
BranchTrueFalseTotal
unless builtin::blessed($self) 0 0 0
453 unless blessed $self;
454  
455 0 my $pk = $self->_get_primary_key;
456 0 return $self->$pk;
457 }
458  
459  
460 15 ▶sub _get_relation_type {
Subroutine _get_relation_type: called CC=4 SCAR=13.9
461 15 my ($class, $relation) = @_;
462  
463 15 my $type = $relation->{type};
464 15 $type .= '_to_';
465  
466 15 my $related_class = _get_related_class($relation);
467  
468 #eval { load $related_class }; ### TODO: check module is loaded
469 #load $related_class;
470  
471 #load $related_class unless is_loaded $related_class;
472 #mark_as_loaded $related_class;
473 15 load_module $related_class;
474  
475 15 my $rel_type = undef;
476 15 ▶ while (my ($rel_key, $rel_opts) = each %{ $related_class->_get_relations }) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %{$related_class->_get_relations;}) 22 15 37
477 22 ▶ next if $class ne _get_related_class($rel_opts);
Branch: 1 branch
BranchTrueFalseTotal
if $class ne _get_related_class($rel_opts) 7 15 22
478 15 $rel_type = $rel_opts->{type};
479 }
480  
481 15 ▶ croak 'Oops! Looks like related class ' . $related_class . ' has no relations with ' . $class unless $rel_type;
Branch: 1 branch
BranchTrueFalseTotal
unless $rel_type 0 15 15
482  
483 15 $type .= $rel_type;
484  
485 15 return $type;
486 }
487  
488 16 ▶sub _get_related_subclass {
Subroutine _get_related_subclass: called CC=4 SCAR=24.6
489 16 my ($relation) = @_;
490  
491 16 ▶ return undef if !ref $relation->{class};
Branch: 1 branch
BranchTrueFalseTotal
unless ref $relation->{"class"} 16 0 16
492  
493 0 my $subclass;
494 0 ▶ if (ref $relation->{class} eq 'HASH') {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $relation->{'class'} eq 'HASH') { } 0 0 0
elsif (ref $relation->{'class'} eq 'ARRAY') { } 0 0 0
495 0 $subclass = (keys %{ $relation->{class} })[0];
496 }
497 elsif (ref $relation->{class} eq 'ARRAY') {
498 0 $subclass = $relation->{class}[0];
499 }
500  
501 0 return $subclass;
502 }
503  
504 52 ▶sub _get_related_class {
Subroutine _get_related_class: called CC=4 SCAR=24.6
505 52 my ($relation) = @_;
506  
507 52 ▶ return $relation->{class} if !ref $relation->{class};
Branch: 1 branch
BranchTrueFalseTotal
unless ref $relation->{"class"} 52 0 52
508  
509 0 my $related_class;
510 0 ▶ if (ref $relation->{class} eq 'HASH') {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $relation->{'class'} eq 'HASH') { } 0 0 0
elsif (ref $relation->{'class'} eq 'ARRAY') { } 0 0 0
511 0 $related_class = ( %{ $relation->{class} } )[1]
512 }
513 elsif (ref $relation->{class} eq 'ARRAY') {
514 0 $related_class = $relation->{class}[1];
515 }
516  
517 0 return $related_class;
518 }
519  
520 6 ▶sub _insert {
Subroutine _insert: called CC=16 SCAR=35.1
521 6 my ($self, $param) = @_;
522  
523 6 ▶ return unless $self->dbh && $param;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->dbh and $param 0 6 6
Condition33%1/3
$self->dbh and $param
!ll&&!rl&&r
006
MC/DC0%0/2
$self->dbh and $param
$self->dbh $param
Truth table33%1/3
A and B
A= $self->dbhB= $param
ABresult
0X0
100
111
524  
525 #my $table_name = $self->_table_name;
526 6 my $table_name = _what_is_the_table_name($self);
527 6 my @field_names = grep { defined $param->{$_} } sort keys %$param;
528 6 ▶ my $primary_key = ($self->can('_get_primary_key')) ? $self->_get_primary_key :
Branch: 2 branches
BranchTrueFalseTotal
$self->can('_get_primary_key') ? : 6 0 6
$self->can('_get_secondary_key') ? : 0 0 0
529 ($self->can('_get_secondary_key')) ? $self->_get_secondary_key : undef;
530  
531 6 my $field_names_str = join q/, /, map { q/"/ . $_ . q/"/ } @field_names;
532  
533 6 my (@bind, @values_list);
534 6 for (@field_names) {
535 15 ▶ if (ref $param->{$_} eq 'SCALAR') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $param->{$_} eq 'SCALAR') { } 0 15 15
536 0 push @values_list, ${ $param->{$_} };
537 }
538 else {
539 15 push @values_list, '?';
540 15 push @bind, $param->{$_};
541 }
542 }
543 6 my $values = join q/, /, @values_list;
544 6 my $pkey_val;
545 6 my $sql_stm = qq{
546 INSERT INTO "$table_name" ($field_names_str)
547 VALUES ($values)
548 };
549  
550 6 ▶ if ( $self->dbh->{Driver}{Name} eq 'Pg' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->dbh->{'Driver'}{'Name'} eq 'Pg') { } 0 6 6
551 0 ▶ if ($primary_key) {
Branch: 1 branch
BranchTrueFalseTotal
if ($primary_key) { } 0 0 0
552 0 ▶ $sql_stm .= ' RETURINIG ' . $primary_key if $primary_key;
Branch: 1 branch
BranchTrueFalseTotal
if $primary_key 0 0 0
553 0 $sql_stm = ActiveRecord::Simple::Utils::quote_sql_stmt($sql_stm, $self->dbh->{Driver}{Name});
554 0 $pkey_val = $self->dbh->selectrow_array($sql_stm, undef, @bind);
555 }
556 else {
557 my $sth = $self->dbh->prepare(
558 ActiveRecord::Simple::Utils::quote_sql_stmt($sql_stm, $self->dbh->{Driver}{Name})
559 0 );
560  
561 0 $sth->execute(@bind);
562 }
563 }
564 else {
565  
566 my $sth = $self->dbh->prepare(
567 ActiveRecord::Simple::Utils::quote_sql_stmt($sql_stm, $self->dbh->{Driver}{Name})
568 6 );
569 6 $sth->execute(@bind);
570  
571 6 ▶ if ( $primary_key && defined $self->{$primary_key} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($primary_key and defined $self->{$primary_key}) { } 3 3 6
Condition66%2/3
$primary_key and defined $self->{$primary_key}
!ll&&!rl&&r
033
MC/DC50%1/2
$primary_key and defined $self->{$primary_key}
$primary_key defined $self->{$primary_key}
Truth table66%2/3
A and B
A= $primary_keyB= defined $self->{$primary_key}
ABresult
0X0
100
111
572 3 $pkey_val = $self->{$primary_key};
573 }
574 else {
575 $pkey_val =
576 exists $sth->{mysql_insertid} # mysql only
577 ? $sth->{mysql_insertid}
578 3 ▶ : $self->dbh->last_insert_id(undef, undef, $table_name, undef);
Branch: 1 branch
BranchTrueFalseTotal
exists $sth->{'mysql_insertid'} ? : 0 3 3
579 }
580 }
581  
582 6 ▶ if (defined $primary_key && $self->can($primary_key) && $pkey_val) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $primary_key and $self->can($primary_key) and $pkey_val) 6 0 6
Condition33%2/6
defined $primary_key and $self->can($primary_key) and $pkey_val
!ll&&!rl&&r
006
defined $primary_key and $self->can($primary_key)
!ll&&!rl&&r
006
MC/DC0%0/3
defined $primary_key and $self->can($primary_key) and $pkey_val
defined $primary_key $self->can($primary_key) $pkey_val
Truth table25%1/4
A and B and C
A= defined $primary_keyB= $self->can($primary_key)C= $pkey_val
ABCresult
0XX0
10X0
1100
1111
583 #$self->$primary_key($pkey_val);
584 6 $self->{$primary_key} = $pkey_val;
585 }
586 6 $self->{isin_database} = 1;
587  
588 6 return $pkey_val;
589 }
590  
591 5 ▶sub _update {
Subroutine _update: called CC=7 SCAR=21.8
592 5 my ($self, $param) = @_;
593  
594 5 ▶ return unless $self->dbh && $param;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->dbh and $param 0 5 5
Condition33%1/3
$self->dbh and $param
!ll&&!rl&&r
005
MC/DC0%0/2
$self->dbh and $param
$self->dbh $param
Truth table33%1/3
A and B
A= $self->dbhB= $param
ABresult
0X0
100
111
595  
596 #my $table_name = $self->_table_name;
597 5 my $table_name = _what_is_the_table_name($self);
598 5 my @field_names = sort keys %$param;
599 5 ▶ my $primary_key = ($self->can('_get_primary_key')) ? $self->_get_primary_key :
Branch: 2 branches
BranchTrueFalseTotal
$self->can('_get_primary_key') ? : 5 0 5
$self->can('_get_secondary_key') ? : 0 0 0
600 ($self->can('_get_secondary_key')) ? $self->_get_secondary_key : undef;
601  
602 5 my (@set_list, @bind);
603 5 for (@field_names) {
604 21 ▶ if (ref $param->{$_} eq 'SCALAR') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $param->{$_} eq 'SCALAR') { } 0 21 21
605 0 push @set_list, $_ . ' = ' . ${ $param->{$_} };
606 }
607 else {
608 21 push @set_list, "$_ = ?";
609 21 push @bind, $param->{$_};
610 }
611 }
612 5 my $setstring = join q/, /, @set_list;
613 5 push @bind, $self->{$primary_key};
614  
615 my $sql_stm = ActiveRecord::Simple::Utils::quote_sql_stmt(
616 qq{
617 UPDATE "$table_name" SET $setstring
618 WHERE
619 $primary_key = ?
620 },
621 $self->dbh->{Driver}{Name}
622 5 );
623  
624 5 return $self->dbh->do($sql_stm, undef, @bind);
625 }
626  
627 21 ▶sub _mk_rw_accessors {
Subroutine _mk_rw_accessors: called CC=4 SCAR=15.6
628 21 my ($class, $fields) = @_;
629  
630 21 ▶ return unless $fields;
Branch: 1 branch
BranchTrueFalseTotal
unless $fields 0 21 21
631 21 ▶ return if $class->can('_make_columns_accessors') && $class->_make_columns_accessors == 0;
Branch: 1 branch
BranchTrueFalseTotal
if $class->can("_make_columns_accessors") and $class->_make_columns_accessors == 0 0 21 21
Condition33%1/3
$class->can("_make_columns_accessors") and $class->_make_columns_accessors == 0
!ll&&!rl&&r
2100
MC/DC0%0/2
$class->can("_make_columns_accessors") and $class->_make_columns_accessors == 0
$class->can("_make_columns_accessors") $class->_make_columns_accessors == 0
Truth table33%1/3
A and B
A= $class->can("_make_columns_accessors")B= $class->_make_columns_accessors == 0
ABresult
0X0
100
111
632  
633 21 $class->_mk_accessors($fields, 'rw');
634 }
635  
636  
637 4 ▶sub _mk_ro_accessors {
Subroutine _mk_ro_accessors: called CC=4 SCAR=15.6
638 4 my ($class, $fields) = @_;
639  
640 4 ▶ return unless $fields;
Branch: 1 branch
BranchTrueFalseTotal
unless $fields 0 4 4
641 4 ▶ return if $class->can('_make_columns_accessors') && $class->_make_columns_accessors == 0;
Branch: 1 branch
BranchTrueFalseTotal
if $class->can("_make_columns_accessors") and $class->_make_columns_accessors == 0 0 4 4
Condition33%1/3
$class->can("_make_columns_accessors") and $class->_make_columns_accessors == 0
!ll&&!rl&&r
400
MC/DC0%0/2
$class->can("_make_columns_accessors") and $class->_make_columns_accessors == 0
$class->can("_make_columns_accessors") $class->_make_columns_accessors == 0
Truth table33%1/3
A and B
A= $class->can("_make_columns_accessors")B= $class->_make_columns_accessors == 0
ABresult
0X0
100
111
642  
643 4 $class->_mk_accessors($fields, 'ro');
644 }
645  
646 25 ▶sub _mk_accessors {
Subroutine _mk_accessors: called CC=8 SCAR=21.0
647 25 my ($class, $fields, $type) = @_;
648  
649 25 ▶ $type ||= 'rw';
Condition50%1/2
$type ||= "rw"
l!l
250
MC/DC0%0/1
$type ||= "rw"
$type
Truth table50%1/2
A
A= $type
Aresult
11
00
650 25 my $code_string = q//;
651 METHOD_NAME:
652 25 for my $method_name (@$fields) {
653 84 ▶ next METHOD_NAME if $class->can($method_name);
Branch: 1 branch
BranchTrueFalseTotal
if $class->can($method_name) 0 84 84
654 84 $code_string .= "sub $method_name {\n";
655 84 ▶ if ($type eq 'rw') {
Branch: 2 branches
BranchTrueFalseTotal
if ($type eq 'rw') { } 78 6 84
elsif ($type eq 'ro') { } 6 0 6
656 78 $code_string .= "if (\@_ > 1) { \$_[0]->{$method_name} = \$_[1]; return \$_[0] }\n";
657 }
658 elsif ($type eq 'ro') {
659 6 $code_string .= "die 'Object is read-only, sorry' if \@_ > 1;\n";
660 }
661 84 $code_string .= "return \$_[0]->{$method_name};\n }\n";
662 }
663  
664 0 ▶ eval "package $class;\n $code_string" if $code_string;
Subroutine email: called CC=2 SCAR=7.0
Subroutine first_name: called CC=2 SCAR=7.0
Subroutine id: called CC=2 SCAR=7.0
Subroutine last_name: called CC=2 SCAR=7.0
Subroutine say_hello: called CC=2 SCAR=7.0
Pod undocumented
Pod undocumented
Pod undocumented
Pod undocumented
Branch: 8 branches
BranchTrueFalseTotal
if $code_string 25 0 25
if (@_ > 1) 1 2 3
if (@_ > 1) 1 17 18
if (@_ > 1) 2 25 27
if (@_ > 1) 1 6 7
if @_ > 1 1 6 7
if (@_ > 1) 0 3 3
if (@_ > 1) 0 2 2
665  
666 25 ▶ say $@ if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 25 25
667  
668 }
669  
670 44 ▶sub _guess {
Subroutine _guess: called CC=4 SCAR=14.0
671 44 my ($what_key, $class) = @_;
672  
673 44 ▶ return 'id' if $what_key eq 'primary_key';
Branch: 1 branch
BranchTrueFalseTotal
if $what_key eq "primary_key" 22 22 44
674  
675 #eval { load $class }; ### TODO: check class has been loaded
676 #load $class unless is_loaded $class;
677 #mark_as_loaded $class;
678 22 load_module $class;
679  
680  
681 22 my $table_name = _what_is_the_table_name($class);
682
683 22 ▶ $table_name =~ s/s$// if $what_key eq 'foreign_key';
Branch: 1 branch
BranchTrueFalseTotal
if $what_key eq "foreign_key" 22 0 22
684  
685 22 ▶ return ($what_key eq 'foreign_key') ? "$table_name\_id" : undef;
Branch: 1 branch
BranchTrueFalseTotal
$what_key eq 'foreign_key' ? : 22 0 22
686 }
687  
688 0 ▶sub _delete_keys {
Subroutine _delete_keys: not called CC=2 SCAR=17.9
689 0 my ($self, $rx) = @_;
690  
691 0 ▶ map { delete $self->{$_} if $_ =~ $rx } keys %$self;
Branch: 1 branch
BranchTrueFalseTotal
if $_ =~ /$rx/ 0 0 0
692 }
693  
694 22 ▶sub _append_relation {
Subroutine _append_relation: called CC=2 SCAR=6.9
695 22 my ($class, $rel_name, $rel_hashref) = @_;
696  
697 22 ▶ if ($class->can('_get_relations')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($class->can('_get_relations')) { } 6 16 22
698 6 my $relations = $class->_get_relations();
699 6 $relations->{$rel_name} = $rel_hashref;
700 6 $class->relations($relations);
701 }
702 else {
703 16 $class->relations({ $rel_name => $rel_hashref });
704 }
705  
706 22 return $rel_hashref;
707 }
708  
709 90 ▶sub _mk_attribute_getter {
Subroutine _mk_attribute_getter: called CC=2 SCAR=7.0
710 90 my ($class, $method_name, $return) = @_;
711  
712 90 ▶ return if $class->can($method_name);
Branch: 1 branch
BranchTrueFalseTotal
if $class->can($method_name) 6 84 90
713  
714 0 ▶ eval "package $class; \n sub $method_name { \$return }";
Subroutine _get_columns: called CC=1 SCAR=0.1
Subroutine _get_primary_key: called CC=1 SCAR=0.1
715 }
716  
717 67 ▶sub _init_relations {
Subroutine _init_relations: called CC=8 SCAR=24.7
718 67 my ($class) = @_;
719  
720 166 my $relations = $class->_get_relations;
721  
722 13 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
723 RELATION_NAME:
724 261 for my $relation_name ( keys %{ $relations }) {
725 186 my $pkg_method_name = $class . '::' . $relation_name;
726 122 ▶ next RELATION_NAME if $class->can($pkg_method_name); ### FIXME: orrrr $relation_name???
Branch: 1 branch
BranchTrueFalseTotal
if $class->can($pkg_method_name) 66 15 81
727  
728 44 my $relation = $relations->{$relation_name};
729 15 my $full_relation_type = _get_relation_type($class, $relation);
730 15 my $related_class = _get_related_class($relation);
731  
732 ### TODO: check for error if returns undef
733 15 my $pk = $relation->{params}{pk};
734 15 my $fk = $relation->{params}{fk};
735  
736 15 my $instance_name = "relation_instance_$relation_name";
737  
738 15 ▶ if (grep { $full_relation_type eq $_ } qw/one_to_many one_to_one one_to_only/) {
Branch: 5 branches
BranchTrueFalseTotal
if (grep {$full_relation_type eq $_;} 'one_to_many', 'one_to_one', 'one_to_only') { } 4 11 15
elsif ($full_relation_type eq 'only_to_one') { } 0 11 11
elsif ($full_relation_type eq 'many_to_one') { } 4 7 11
elsif ($full_relation_type eq 'many_to_many') { } 7 0 7
elsif ($full_relation_type eq 'generic_to_generic') { } 0 0 0
739 4 ▶ *{$pkg_method_name} = sub {
Subroutine __ANON__: called CC=7 SCAR=20.3
740 8 my ($self, @args) = @_;
741 8 ▶ if (@args) {
Branch: 1 branch
BranchTrueFalseTotal
if (@args) 1 7 8
742 1 my $object = shift @args;
743 1 ▶ croak "Using unblessed scalar as an object reference"
Branch: 1 branch
BranchTrueFalseTotal
unless builtin::blessed($object) 0 1 1
744 unless blessed $object;
745  
746 1 ▶ $object->save() if ! exists $object->{isin_database} && !$object->{isin_database} == 1;
Branch: 1 branch
BranchTrueFalseTotal
if not exists $object->{"isin_database"} and !$object->{"isin_database"} == 1 0 1 1
Condition33%1/3
not exists $object->{"isin_database"} and !$object->{"isin_database"} == 1
!ll&&!rl&&r
100
MC/DC0%0/2
not exists $object->{"isin_database"} and !$object->{"isin_database"} == 1
not exists $object->{"isin_database"} !$object->{"isin_database"} == 1
Truth table33%1/3
not(A) and not(B)
A= not exists $object->{"isin_database"}B= !$object->{"isin_database"} == 1
ABresult
0X0
100
111
747  
748 #$self->$fk($object->$pk);
749 1 $self->{$fk} = $object->{$pk};
750 1 $self->{$instance_name} = $object;
751  
752 1 return $self;
753 }
754 # else
755 7 ▶ if (!$self->{$instance_name}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{$instance_name}) 4 3 7
756 4 ▶ $self->{$instance_name} = $related_class->objects->get($self->{$fk}) // $related_class;
Condition33%1/3
$related_class->objects->get($self->{$fk}) // $related_class
l!l&&r!l&&!r
400
MC/DC0%0/2
$related_class->objects->get($self->{$fk}) // $related_class
$related_class->objects->get($self->{$fk}) $related_class
Truth table33%1/3
A // B
A= $related_class->objects->get($self->{$fk})B= $related_class
ABresult
1X1
011
000
757 }
758  
759 7 return $self->{$instance_name};
760 }
761 4 }
762 elsif ($full_relation_type eq 'only_to_one') {
763 0 ▶ *{$pkg_method_name} = sub {
Subroutine __ANON__: called CC=2 SCAR=17.9
764 0 my ($self, @args) = @_;
765  
766 0 ▶ if (!$self->{$instance_name}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{$instance_name}) 0 0 0
767 0 $self->{$instance_name} = $related_class->find("$fk = ?", $self->{$pk})->fetch;
768 }
769  
770 0 return $self->{$instance_name};
771 }
772 0 }
773 elsif ($full_relation_type eq 'many_to_one') {
774 4 ▶ *{$pkg_method_name} = sub {
Subroutine __ANON__: called CC=7 SCAR=20.0
775 5 my ($self, @args) = @_;
776  
777 5 ▶ if (@args) {
Branch: 1 branch
BranchTrueFalseTotal
if (@args) 1 4 5
778 1 ▶ unless (all_blessed(\@args)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (all_blessed(\@args)) 0 1 1
779 0 return $related_class->find(@args)->left_join($self->_get_table_name);
780 }
781  
782 OBJECT:
783 1 for my $object (@args) {
784 1 ▶ next OBJECT if !blessed $object;
Branch: 1 branch
BranchTrueFalseTotal
unless builtin::blessed($object) 0 1 1
785  
786 1 my $pk = $self->_get_primary_key;
787 #$object->$fk($self->$pk)->save;
788 1 $object->{$fk} = $self->{$pk};
789 1 $object->save();
790 }
791  
792 1 return $self;
793 }
794 # else
795 4 ▶ return $related_class->new() if not $self->can('_get_primary_key');
Branch: 1 branch
BranchTrueFalseTotal
unless $self->can("_get_primary_key") 0 4 4
796  
797 4 ▶ if (!$self->{$instance_name}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{$instance_name}) 4 0 4
798 4 $self->{$instance_name} = $related_class->objects->find("$fk = ?", $self->{$pk});
799 }
800  
801 4 return $self->{$instance_name};
802 }
803 4 }
804 elsif ($full_relation_type eq 'many_to_many') {
805 7 ▶ *{$pkg_method_name} = sub {
Subroutine __ANON__: called CC=13 SCAR=34.0
806 16 my ($self, @args) = @_;
807  
808 16 ▶ if (@args) {
Branch: 1 branch
BranchTrueFalseTotal
if (@args) 8 8 16
809  
810 8 my $related_subclass = _get_related_subclass($relation);
811  
812 8 ▶ unless (all_blessed(\@args)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (all_blessed(\@args)) 6 2 8
813 return $related_class->_find_many_to_many({
814 root_class => $class,
815 via_table => $relation->{via_table},
816 6 m_class => $related_subclass,
817 self => $self,
818 where_statement => \@args,
819 });
820 }
821  
822  
823 2 ▶ if (defined $related_subclass) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $related_subclass) { } 0 2 2
824 0 my ($fk1, $fk2);
825  
826 0 $fk1 = $fk;
827  
828 RELATED_CLASS_RELATION:
829 0 for my $related_class_relation (values %{ $related_class->_get_relations }) {
830 next RELATED_CLASS_RELATION
831 0 ▶ unless _get_related_subclass($related_class_relation)
Branch: 1 branch
BranchTrueFalseTotal
unless _get_related_subclass($related_class_relation) and $related_subclass eq _get_related_subclass($related_class_relation) 0 0 0
Condition0%0/3
_get_related_subclass($related_class_relation) and $related_subclass eq _get_related_subclass($related_class_relation)
!ll&&!rl&&r
000
MC/DC0%0/2
_get_related_subclass($related_class_relation) and $related_subclass eq _get_related_subclass($related_class_relation)
_get_related_subclass($related_class_relation) $related_subclass eq _get_related_subclass($related_class_relation)
Truth table0%0/3
A and B
A= _get_related_subclass($related_class_relation)B= $related_subclass eq _get_related_subclass($related_class_relation)
ABresult
0X0
100
111
832 && $related_subclass eq _get_related_subclass($related_class_relation);
833  
834 0 $fk2 = $related_class_relation->{params}{fk};
835 }
836  
837 0 my $pk1_name = $self->_get_primary_key;
838 0 my $pk1 = $self->{$pk1_name};
839  
840 0 ▶ defined $pk1 or croak 'You are trying to create relations between unsaved objects. Save your ' . $class . ' object first';
Branch: 1 branch
BranchTrueFalseTotal
defined $pk1 or croak('You are trying to create relations between unsaved objects. Save your ' . $class . ' object first') 0 0 0
841  
842 OBJECT:
843 0 for my $object (@args) {
844 0 ▶ next OBJECT if !blessed $object;
Branch: 1 branch
BranchTrueFalseTotal
unless builtin::blessed($object) 0 0 0
845  
846 0 my $pk2_name = $object->_get_primary_key;
847 0 my $pk2 = $object->{$pk2_name};
848  
849 0 $related_subclass->new($fk1 => $pk1, $fk2 => $pk2)->save;
850 }
851 }
852 else {
853 2 my ($fk1, $fk2);
854 2 $fk1 = $fk;
855  
856 2 $fk2 = class_to_table_name($related_class) . '_id';
857  
858 2 my $pk1_name = $self->_get_primary_key;
859 2 my $pk1 = $self->{$pk1_name};
860  
861 2 my $via_table = $relation->{via_table};
862  
863 OBJECT:
864 2 for my $object (@args) {
865 2 ▶ next OBJECT if !blessed $object;
Branch: 1 branch
BranchTrueFalseTotal
unless builtin::blessed($object) 0 2 2
866  
867 2 my $pk2_name = $object->_get_primary_key;
868 2 my $pk2 = $object->{$pk2_name};
869  
870 2 my $sql = qq/INSERT INTO "$via_table" ("$fk1", "$fk2") VALUES (?, ?)/;
871 2 $self->dbh->do($sql, undef, $pk1, $pk2);
872 }
873 }
874  
875 2 return $self;
876 }
877 # else
878  
879 8 ▶ if (!$self->{$instance_name}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{$instance_name}) 8 0 8
880 $self->{$instance_name} = $related_class->_find_many_to_many({
881 root_class => $class,
882 m_class => _get_related_subclass($relation),
883 via_table => $relation->{via_table},
884 8 self => $self,
885 });
886 }
887  
888 8 return $self->{$instance_name};
889 }
890 7 }
891 elsif ($full_relation_type eq 'generic_to_generic') {
892 0 ▶ *{$pkg_method_name} = sub {
Subroutine __ANON__: called CC=3 SCAR=24.8
893 0 my ($self, @args) = @_;
894  
895 0 ▶ if (!$self->{$instance_name}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{$instance_name}) 0 0 0
896 0 my %find_attrs;
897 0 ▶ while (my ($k, $v) = each %{ $relation->{key} }) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %{$$relation{"key"};}) 0 0 0
898 0 $find_attrs{$v} = $self->{$k};
899 }
900 0 $self->{$instance_name} = $related_class->find(\%find_attrs);
901 }
902  
903 0 return $self->{$instance_name};
904 }
905 0 }
906 }
907  
908 13 ▶ use strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
909 }
910  
911 63 ▶sub _what_is_the_table_name {
Subroutine _what_is_the_table_name: called CC=4 SCAR=13.9
912 34 ▶ my $class = ref $_[0] ? ref $_[0] : $_[0];
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] ? : 12 22 34
913  
914 34 ▶ croak 'Invalid data class' if $class =~ /^ActiveRecord::Simple/;
Branch: 1 branch
BranchTrueFalseTotal
if $class =~ /^ActiveRecord::Simple/ 0 34 34
915  
916 34 ▶ my $table_name =
Branch: 1 branch
BranchTrueFalseTotal
$class->can('_get_table_name') ? : 33 1 34
917 $class->can('_get_table_name') ?
918 $class->_get_table_name
919 : class_to_table_name($class);
920  
921 34 return $table_name;
922 }
923  
924 13 1;
925  
926 __END__;
« Simple/Find.pm ↑ Summary Simple/Utils.pm »