| 1 | package DBI::Easy::Helper; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | 6 | ▶ | use Class::Easy; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 6 | ▶ | use Time::Piece; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||
| 6 | # collection constructor | ||||||||||||||||||||||||||||
| 7 | 21 | ▶ | sub _connector_maker { | ||||||||||||||||||||||||||
|
Subroutine
_connector_maker: called CC=9 SCAR=22.4
| |||||||||||||||||||||||||||||
| 8 | 21 | my $class = shift; | |||||||||||||||||||||||||||
| 9 | 21 | my $type = shift; | |||||||||||||||||||||||||||
| 10 | 21 | my $name = shift; # actually, is entity name | |||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||
| 12 | 21 | ▶ | if ($type !~ /^(Collection|Record)$/i) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 13 | 0 | warn "no correct type supplied - '$type' (expecting 'collection' or 'record')"; | |||||||||||||||||||||||||||
| 14 | 0 | return; | |||||||||||||||||||||||||||
| 15 | } | ||||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||
| 17 | 21 | my %params = @_; | |||||||||||||||||||||||||||
| 18 | 21 | ▶ | my $prefix = $params{prefix} || 'Entity'; | ||||||||||||||||||||||||||
|
Condition50%1/2
$params{'prefix'} || 'Entity'
MC/DC0%0/1
$params{'prefix'} || 'Entity'
$params{'prefix'}
Truth table50%1/2
A
A= $params{'prefix'}
| |||||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||
| 20 | 21 | my @pack_chunks = ($prefix, package_from_table ($name)); | |||||||||||||||||||||||||||
| 21 | 21 | ▶ | push @pack_chunks, 'Collection' | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 22 | if $type =~ /^collection$/i; | ||||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||
| 24 | 21 | my $pack = join '::', @pack_chunks; | |||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||
| 26 | 21 | debug "creation package $pack"; | |||||||||||||||||||||||||||
| 27 | |||||||||||||||||||||||||||||
| 28 | # check for existing package | ||||||||||||||||||||||||||||
| 29 | 21 | ▶ | return $pack | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 30 | if try_to_use_inc_quiet ($pack); | ||||||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||||||
| 32 | 21 | my $code; | |||||||||||||||||||||||||||
| 33 | |||||||||||||||||||||||||||||
| 34 | |||||||||||||||||||||||||||||
| 35 | 21 | ▶ | if ($params{entity}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 36 | 21 | my $table_name = ''; | |||||||||||||||||||||||||||
| 37 | $table_name = "has 'table_name', global => 1, is => 'rw', default => '" . $params{table_name} . "';\n" | ||||||||||||||||||||||||||||
| 38 | 21 | ▶ | if $params{table_name}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 39 | |||||||||||||||||||||||||||||
| 40 | 21 | my $column_prefix = ''; | |||||||||||||||||||||||||||
| 41 | $column_prefix = "has 'column_prefix', global => 1, is => 'rw', default => '" . $params{column_prefix} . "';\n" | ||||||||||||||||||||||||||||
| 42 | 21 | ▶ | if $params{column_prefix}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 43 | |||||||||||||||||||||||||||||
| 44 | 21 | $code = "package $pack;\nuse Class::Easy;\nuse base '$params{entity}';\n$table_name$column_prefix\npackage main;\nimport $pack;\n"; | |||||||||||||||||||||||||||
| 45 | |||||||||||||||||||||||||||||
| 46 | } else { | ||||||||||||||||||||||||||||
| 47 | 0 | warn "error: no entity package provided"; | |||||||||||||||||||||||||||
| 48 | 0 | return; | |||||||||||||||||||||||||||
| 49 | } | ||||||||||||||||||||||||||||
| 50 | |||||||||||||||||||||||||||||
| 51 | 5 | ▶ | eval $code; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
Subroutine
BEGIN: called CC=1 SCAR=0.0
Subroutine
BEGIN: called CC=1 SCAR=0.0
Subroutine
BEGIN: called CC=1 SCAR=0.0
Subroutine
BEGIN: called CC=1 SCAR=0.0
Subroutine
BEGIN: called CC=1 SCAR=0.0
Subroutine
BEGIN: called CC=1 SCAR=0.0
Subroutine
BEGIN: called CC=1 SCAR=0.0
Subroutine
BEGIN: called CC=1 SCAR=0.0
Subroutine
BEGIN: called CC=1 SCAR=0.0
Subroutine
BEGIN: called CC=1 SCAR=0.0
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 52 | |||||||||||||||||||||||||||||
| 53 | 21 | ▶ | if ($@) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 54 | 0 | warn "something wrong happens: $@"; | |||||||||||||||||||||||||||
| 55 | 0 | return; | |||||||||||||||||||||||||||
| 56 | } else { | ||||||||||||||||||||||||||||
| 57 | 21 | return $pack; | |||||||||||||||||||||||||||
| 58 | } | ||||||||||||||||||||||||||||
| 59 | } | ||||||||||||||||||||||||||||
| 60 | |||||||||||||||||||||||||||||
| 61 | # collection constructor | ||||||||||||||||||||||||||||
| 62 | 6 | ▶ | sub c { | ||||||||||||||||||||||||||
|
Subroutine
c: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 63 | 6 | my $self = shift; | |||||||||||||||||||||||||||
| 64 | 6 | return $self->_connector_maker ('collection', @_); | |||||||||||||||||||||||||||
| 65 | } | ||||||||||||||||||||||||||||
| 66 | |||||||||||||||||||||||||||||
| 67 | # record constructor | ||||||||||||||||||||||||||||
| 68 | 15 | ▶ | sub r { | ||||||||||||||||||||||||||
|
Subroutine
r: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 69 | 15 | my $self = shift; | |||||||||||||||||||||||||||
| 70 | 15 | return $self->_connector_maker ('record', @_); | |||||||||||||||||||||||||||
| 71 | } | ||||||||||||||||||||||||||||
| 72 | |||||||||||||||||||||||||||||
| 73 | 6 | our $types; | |||||||||||||||||||||||||||
| 74 | |||||||||||||||||||||||||||||
| 75 | map { | ||||||||||||||||||||||||||||
| 76 | 6 | $types->{$_} = 'date' | |||||||||||||||||||||||||||
| 77 | } split (/\|/, 'DATE|TIMESTAMP(6)|DATETIME|TIMESTAMP|timestamp|timestamp without time zone'); | ||||||||||||||||||||||||||||
| 78 | |||||||||||||||||||||||||||||
| 79 | 81 | ▶ | sub is_rich_type { | ||||||||||||||||||||||||||
|
Subroutine
is_rich_type: called CC=3 SCAR=12.5
Pod
undocumented
| |||||||||||||||||||||||||||||
| 80 | 81 | my $pack = shift; | |||||||||||||||||||||||||||
| 81 | 81 | my $type = shift; | |||||||||||||||||||||||||||
| 82 | |||||||||||||||||||||||||||||
| 83 | return $types->{$type} | ||||||||||||||||||||||||||||
| 84 | 81 | ▶ | if defined $type and exists $types->{$type}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
defined $type and exists $DBI::Easy::Helper::types->{$type}
MC/DC0%0/2
defined $type and exists $DBI::Easy::Helper::types->{$type}
defined $type
exists $DBI::Easy::Helper::types->{$type}
Truth table33%1/3
A and B
A= defined $typeB= exists $DBI::Easy::Helper::types->{$type}
| |||||||||||||||||||||||||||||
| 85 | } | ||||||||||||||||||||||||||||
| 86 | |||||||||||||||||||||||||||||
| 87 | 0 | ▶ | sub value_from_type { | ||||||||||||||||||||||||||
|
Subroutine
value_from_type: not called CC=5 SCAR=34.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 88 | 0 | my $pack = shift; | |||||||||||||||||||||||||||
| 89 | 0 | my $type = shift; | |||||||||||||||||||||||||||
| 90 | 0 | my $value = shift; | |||||||||||||||||||||||||||
| 91 | 0 | my $model = shift; # check for driver | |||||||||||||||||||||||||||
| 92 | |||||||||||||||||||||||||||||
| 93 | 0 | ▶ | if (defined $type and $types->{$type} eq 'date') { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
defined $type and $DBI::Easy::Helper::types->{$type} eq "date"
MC/DC0%0/2
defined $type and $DBI::Easy::Helper::types->{$type} eq "date"
defined $type
$DBI::Easy::Helper::types->{$type} eq "date"
Truth table0%0/3
A and B
A= defined $typeB= $DBI::Easy::Helper::types->{$type} eq "date"
| |||||||||||||||||||||||||||||
| 94 | |||||||||||||||||||||||||||||
| 95 | 0 | my $t = localtime; | |||||||||||||||||||||||||||
| 96 | 0 | my $timestamp = eval {(Time::Piece->strptime ($value, $model->_datetime_format) - $t->tzoffset)->epoch}; | |||||||||||||||||||||||||||
| 97 | 0 | ▶ | return 0 | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 98 | if $t->tzoffset->seconds + $timestamp == 0; | ||||||||||||||||||||||||||||
| 99 | |||||||||||||||||||||||||||||
| 100 | 0 | ▶ | return $timestamp | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 101 | if $timestamp; | ||||||||||||||||||||||||||||
| 102 | } | ||||||||||||||||||||||||||||
| 103 | |||||||||||||||||||||||||||||
| 104 | 0 | return $value; | |||||||||||||||||||||||||||
| 105 | |||||||||||||||||||||||||||||
| 106 | } | ||||||||||||||||||||||||||||
| 107 | |||||||||||||||||||||||||||||
| 108 | 76 | ▶ | sub value_to_type { | ||||||||||||||||||||||||||
|
Subroutine
value_to_type: called CC=4 SCAR=17.3
Pod
undocumented
| |||||||||||||||||||||||||||||
| 109 | 76 | my $pack = shift; | |||||||||||||||||||||||||||
| 110 | 76 | my $type = shift; | |||||||||||||||||||||||||||
| 111 | 76 | my $value = shift; | |||||||||||||||||||||||||||
| 112 | 76 | my $model = shift; # check for driver | |||||||||||||||||||||||||||
| 113 | |||||||||||||||||||||||||||||
| 114 | # warn "$type => $value, $types->{$type} ".$model->_datetime_format."\n"; | ||||||||||||||||||||||||||||
| 115 | |||||||||||||||||||||||||||||
| 116 | 76 | ▶ | if (defined $type and $types->{$type} eq 'date') { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
defined $type and $DBI::Easy::Helper::types->{$type} eq "date"
MC/DC0%0/2
defined $type and $DBI::Easy::Helper::types->{$type} eq "date"
defined $type
$DBI::Easy::Helper::types->{$type} eq "date"
Truth table33%1/3
A and B
A= defined $typeB= $DBI::Easy::Helper::types->{$type} eq "date"
| |||||||||||||||||||||||||||||
| 117 | 0 | my $timestamp = Time::Piece->new ([CORE::localtime ($value)])->strftime ($model->_datetime_format); | |||||||||||||||||||||||||||
| 118 | 0 | ▶ | return $timestamp | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 119 | if $timestamp; | ||||||||||||||||||||||||||||
| 120 | } | ||||||||||||||||||||||||||||
| 121 | |||||||||||||||||||||||||||||
| 122 | 76 | return $value; | |||||||||||||||||||||||||||
| 123 | |||||||||||||||||||||||||||||
| 124 | } | ||||||||||||||||||||||||||||
| 125 | |||||||||||||||||||||||||||||
| 126 | 17 | ▶ | sub table_from_package { | ||||||||||||||||||||||||||
|
Subroutine
table_from_package: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 127 | 17 | my $entity = shift; | |||||||||||||||||||||||||||
| 128 | |||||||||||||||||||||||||||||
| 129 | 17 | lc join ('_', split /(?=\p{IsUpper}\p{IsLower})/, $entity); | |||||||||||||||||||||||||||
| 130 | } | ||||||||||||||||||||||||||||
| 131 | |||||||||||||||||||||||||||||
| 132 | 21 | ▶ | sub package_from_table { | ||||||||||||||||||||||||||
|
Subroutine
package_from_table: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 133 | 21 | my $table = shift; | |||||||||||||||||||||||||||
| 134 | |||||||||||||||||||||||||||||
| 135 | 21 | join '', map {ucfirst} split /_/, $table; | |||||||||||||||||||||||||||
| 136 | } | ||||||||||||||||||||||||||||
| 137 | |||||||||||||||||||||||||||||
| 138 | 6 | 1; | |||||||||||||||||||||||||||
| 139 | |||||||||||||||||||||||||||||
| 140 | __DATA__ | ||||||||||||||||||||||||||||