File Coverage

blib/lib/DBI/Easy/Helper.pm
Criterion Covered Total %
statement 84 103 81.5
branch 10 26 38.4
condition 3 11 27.2
subroutine 22 23 95.6
pod 0 7 0.0
total 119 170 70.0


line stmt bran cond sub pod time code
1             package DBI::Easy::Helper;
2              
3 6     6   32 use Class::Easy;
  6         9  
  6         43  
4 6     6   8412 use Time::Piece;
  6         84968  
  6         40  
5              
6             # collection constructor
7             sub _connector_maker {
8 21     21   53 my $class = shift;
9 21         39 my $type = shift;
10 21         40 my $name = shift; # actually, is entity name
11            
12 21 50       185 if ($type !~ /^(Collection|Record)$/i) {
13 0         0 warn "no correct type supplied - '$type' (expecting 'collection' or 'record')";
14 0         0 return;
15             }
16            
17 21         98 my %params = @_;
18 21   50     99 my $prefix = $params{prefix} || 'Entity';
19            
20 21         86 my @pack_chunks = ($prefix, package_from_table ($name));
21 21 100       110 push @pack_chunks, 'Collection'
22             if $type =~ /^collection$/i;
23            
24 21         65 my $pack = join '::', @pack_chunks;
25            
26 21         152 debug "creation package $pack";
27            
28             # check for existing package
29 21 50       2597 return $pack
30             if try_to_use_inc_quiet ($pack);
31            
32 21         17226 my $code;
33              
34            
35 21 50       84 if ($params{entity}) {
36 21         45 my $table_name = '';
37 21 50       549 $table_name = "has 'table_name', global => 1, is => 'rw', default => '" . $params{table_name} . "';\n"
38             if $params{table_name};
39              
40 21         338 my $column_prefix = '';
41 21 50       187 $column_prefix = "has 'column_prefix', global => 1, is => 'rw', default => '" . $params{column_prefix} . "';\n"
42             if $params{column_prefix};
43            
44 21         130 $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         0 warn "error: no entity package provided";
48 0         0 return;
49             }
50            
51 5     5   47 eval $code;
  5     5   12  
  5     4   141  
  5     4   1528  
  5     4   19  
  5     4   4702  
  4     4   45  
  4     4   8  
  4     2   80  
  4     2   734  
  4     2   9  
  4     2   1584  
  4         31  
  4         8  
  4         20  
  4         978  
  4         7  
  4         1994  
  4         36  
  4         9  
  4         34  
  4         810  
  4         12  
  4         4415  
  2         19  
  2         4  
  2         14  
  2         447  
  2         5  
  2         1244  
  2         14  
  2         5  
  2         10  
  2         573  
  2         4  
  2         1453  
  21         3318  
52            
53 21 50       160 if ($@) {
54 0         0 warn "something wrong happens: $@";
55 0         0 return;
56             } else {
57 21         253 return $pack;
58             }
59             }
60              
61             # collection constructor
62             sub c {
63 6     6 0 59 my $self = shift;
64 6         99 return $self->_connector_maker ('collection', @_);
65             }
66              
67             # record constructor
68             sub r {
69 15     15 0 2784 my $self = shift;
70 15         91 return $self->_connector_maker ('record', @_);
71             }
72              
73             our $types;
74              
75             map {
76             $types->{$_} = 'date'
77             } split (/\|/, 'DATE|TIMESTAMP(6)|DATETIME|TIMESTAMP|timestamp|timestamp without time zone');
78              
79             sub is_rich_type {
80 81     81 0 111 my $pack = shift;
81 81         102 my $type = shift;
82            
83 81 50 33     1821 return $types->{$type}
84             if defined $type and exists $types->{$type};
85             }
86              
87             sub value_from_type {
88 0     0 0 0 my $pack = shift;
89 0         0 my $type = shift;
90 0         0 my $value = shift;
91 0         0 my $model = shift; # check for driver
92            
93 0 0 0     0 if (defined $type and $types->{$type} eq 'date') {
94            
95 0         0 my $t = localtime;
96 0         0 my $timestamp = eval {(Time::Piece->strptime ($value, $model->_datetime_format) - $t->tzoffset)->epoch};
  0         0  
97 0 0       0 return 0
98             if $t->tzoffset->seconds + $timestamp == 0;
99              
100 0 0       0 return $timestamp
101             if $timestamp;
102             }
103            
104 0         0 return $value;
105            
106             }
107              
108             sub value_to_type {
109 76     76 0 200 my $pack = shift;
110 76         140 my $type = shift;
111 76         122 my $value = shift;
112 76         99 my $model = shift; # check for driver
113            
114             # warn "$type => $value, $types->{$type} ".$model->_datetime_format."\n";
115            
116 76 50 33     662 if (defined $type and $types->{$type} eq 'date') {
117 0         0 my $timestamp = Time::Piece->new ([CORE::localtime ($value)])->strftime ($model->_datetime_format);
118 0 0       0 return $timestamp
119             if $timestamp;
120             }
121            
122 76         273 return $value;
123              
124             }
125              
126             sub table_from_package {
127 17     17 0 37 my $entity = shift;
128            
129 6     6   13831 lc join ('_', split /(?=\p{IsUpper}\p{IsLower})/, $entity);
  6         65  
  6         115  
  17         166  
130             }
131              
132             sub package_from_table {
133 21     21 0 40 my $table = shift;
134            
135 21         109 join '', map {ucfirst} split /_/, $table;
  21         144  
136             }
137              
138             1;
139              
140             __DATA__