lib/Project/Easy/DB.pm

filter: Statementstmt 89.8% 53 / 59 Branchbran 46.1% 12 / 26 Conditioncond 40.0% 2 / 5 MC/DCmcdc 0.0% 0 / 3 Subroutinesub 100% 5 / 5 Podpod 0.0% 0 / 3 totaltotal 71.2% 72 / 101 CC 17 SCAR 26.2
CC
17
Cov
74%
new
23.0
collection
5.0
entity
3.0
CRAP 13.8
« lib/Project/Easy/Helper.pm ↑ Summary lib/Project/Easy/Helper/Status.pm »
1 package Project::Easy::DB;
2  
3 9 ▶use Class::Easy;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 9 ▶use DBI;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 8 ▶sub new {
Subroutine new: called CC=11 SCAR=31.4
Pod undocumented
8 8 my $class = shift;
9 8 my $project = shift;
10 8 ▶ my $db_code = shift || 'default';
Condition50%1/2
shift() || 'default'
l!l
80
MC/DC0%0/1
shift() || 'default'
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
11
12 8 my $db_conf = $project->config->{db}->{$db_code};
13
14 die "db configuration: driver_name must be defined"
15 8 ▶ unless defined $db_conf->{driver_name};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $db_conf->{"driver_name"} 0 8 8
16  
17 8 my @connector = ('dbi', $db_conf->{driver_name});
18
19 8 ▶ if (exists $db_conf->{attributes}) {
Branch: 2 branches
BranchTrueFalseTotal
if (exists $db_conf->{'attributes'}) { } 8 0 8
elsif (exists $db_conf->{'dsn_suffix'}) { } 0 0 0
20 8 my %attrs = %{$db_conf->{attributes}};
21 8 push @connector, join ';', map {"$_=$attrs{$_}"} keys %attrs;
22 } elsif (exists $db_conf->{dsn_suffix}) {
23 0 die "db configuration: please use 'attributes' hash for setting dsn suffix string";
24 }
25  
26 8 my $dsn = join ':', @connector;
27
28 die "db configuration: key name 'opts' must be changed to 'options'"
29 8 ▶ if exists $db_conf->{opts};
Branch: 1 branch
BranchTrueFalseTotal
if exists $db_conf->{"opts"} 0 8 8
30
31 # connect to db
32 my $dbh = DBI->connect (
33 $dsn,
34 $db_conf->{user},
35 $db_conf->{pass},
36 $db_conf->{options},
37 8 );
38
39 8 ▶ if ($dbh and defined $db_conf->{do_after_connect}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($dbh and defined $db_conf->{"do_after_connect"}) 0 8 8
Condition33%1/3
$dbh and defined $db_conf->{"do_after_connect"}
!ll&&!rl&&r
080
MC/DC0%0/2
$dbh and defined $db_conf->{"do_after_connect"}
$dbh defined $db_conf->{"do_after_connect"}
Truth table33%1/3
A and B
A= $dbhB= defined $db_conf->{"do_after_connect"}
ABresult
0X0
100
111
40 0 my $sql_list = $db_conf->{do_after_connect};
41 0 ▶ if (! ref $sql_list) {
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $sql_list) 0 0 0
42 0 $sql_list = [$sql_list];
43 }
44  
45 0 foreach my $sql (@$sql_list) {
46 0 ▶ $dbh->do ($sql) or die "can't do $sql";
Branch: 1 branch
BranchTrueFalseTotal
$dbh->do($sql) or die "can't do $sql" 0 0 0
47 }
48 }
49  
50 # $dbh->trace (1, join ('/', $auction->root, 'var', 'log', 'dbi_trace'));
51
52 8 return $dbh;
53 }
54  
55 5 ▶sub entity {
Subroutine entity: called CC=3 SCAR=11.0
Pod undocumented
56 5 my $self = shift;
57 5 my $name = shift;
58
59 5 my ($entity_name, $ds_path, $ds_entity_name, $ds_config_name) = @_;
60
61 5 my $ds_package = $::project->entity_prefix . $ds_entity_name . 'Record';
62 5 my $ds_entity_package = $::project->entity_prefix . $entity_name;
63
64 5 my $use_result = try_to_use_quiet ($ds_entity_package);
65 5 ▶ if ($use_result) {
Branch: 1 branch
BranchTrueFalseTotal
if ($use_result) 3 2 5
66 3 debug "entity $ds_entity_package $use_result";
67 3 return $ds_entity_package
68 }
69
70 2 ▶ die "package $ds_entity_package compilation failed with error: $@"
Branch: 1 branch
BranchTrueFalseTotal
unless $! 0 2 2
71 unless $!;
72
73 2 my $prefix = substr ($::project->entity_prefix, 0, -2);
74
75 2 debug "virtual entity creation (prefix => $prefix, datasource entity package => $ds_entity_package, datasource path => $ds_path, datasource package => $ds_package)";
76
77 2 DBI::Easy::Helper->r (
78 $entity_name,
79 prefix => $prefix,
80 entity => $ds_package,
81 table_name => $ds_path,
82 );
83 }
84  
85 3 ▶sub collection {
Subroutine collection: called CC=5 SCAR=16.2
Pod undocumented
86 3 my $self = shift;
87 3 my $name = shift;
88
89 3 my ($entity_name, $ds_path, $ds_entity_name, $ds_config_name) = @_;
90  
91 # we must initialize entity prior to collection
92 3 my $entity_package = $self->entity ($name, $entity_name, $ds_path, $ds_entity_name, $ds_config_name);
93
94 3 my $ds_package = $::project->entity_prefix . $ds_entity_name . 'Collection';
95 3 my $ds_collection_package = $::project->entity_prefix . $entity_name. '::Collection';
96
97 3 my $use_result = try_to_use_quiet ($ds_collection_package);
98 3 ▶ if ($use_result) {
Branch: 1 branch
BranchTrueFalseTotal
if ($use_result) 1 2 3
99 1 debug "collection $ds_collection_package $use_result";
100 1 return $ds_collection_package;
101 }
102  
103 2 ▶ die "package $ds_collection_package compilation failed with error: $@"
Branch: 1 branch
BranchTrueFalseTotal
unless $! 0 2 2
104 unless $!;
105
106 2 my $prefix = substr ($::project->entity_prefix, 0, -2);
107
108 2 ▶ $ds_path = $entity_package->table_name
Branch: 1 branch
BranchTrueFalseTotal
if $entity_package->can("table_name") 2 0 2
109 if $entity_package->can ('table_name');
110
111 2 debug "virtual collection creation (prefix => $prefix, datasource collection package => $ds_collection_package, datasource path => $ds_path, datasource package => $ds_package)";
112
113 2 my @params = (
114 $entity_name,
115 prefix => $prefix,
116 entity => $ds_package,
117 table_name => $ds_path,
118 );
119
120 2 ▶ push @params, (column_prefix => $entity_package->column_prefix)
Branch: 1 branch
BranchTrueFalseTotal
if $entity_package->can("column_prefix") 2 0 2
121 if $entity_package->can ('column_prefix');
122
123 2 DBI::Easy::Helper->c (@params);
124 }
125  
126  
127 9 1;
« lib/Project/Easy/Helper.pm ↑ Summary lib/Project/Easy/Helper/Status.pm »