| 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'
MC/DC0%0/1
shift() || 'default'
shift()
Truth table50%1/2
A
A= shift()
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||
| 17 | 8 | my @connector = ('dbi', $db_conf->{driver_name}); | |||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||
| 19 | 8 | ▶ | if (exists $db_conf->{attributes}) { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
Condition33%1/3
$dbh and defined $db_conf->{"do_after_connect"}
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"}
| |||||||||||||||||||||||||||||
| 40 | 0 | my $sql_list = $db_conf->{do_after_connect}; | |||||||||||||||||||||||||||
| 41 | 0 | ▶ | if (! ref $sql_list) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 121 | if $entity_package->can ('column_prefix'); | ||||||||||||||||||||||||||||
| 122 | |||||||||||||||||||||||||||||
| 123 | 2 | DBI::Easy::Helper->c (@params); | |||||||||||||||||||||||||||
| 124 | } | ||||||||||||||||||||||||||||
| 125 | |||||||||||||||||||||||||||||
| 126 | |||||||||||||||||||||||||||||
| 127 | 9 | 1; | |||||||||||||||||||||||||||