blib/lib/HTML/EP/EditTable.pm

filter: Statementstmt 6.9% 8 / 115 Branchbran 0.0% 0 / 68 Conditioncond 0.0% 0 / 52 MC/DCmcdc 0.0% 0 / 24 Subroutinesub 40.0% 2 / 5 Podpod n/a totaltotal 3.7% 10 / 264 CC 56 SCAR 64.2
CC
56
Cov
3%
_ep_edittable_edit
930.0
_ep_edittable_select
420.0
_ep_edittable_links
72.0
CRAP 614.6
↑ Summary blib/lib/HTML/EP/Shop.pm »
1 # -*- perl -*-
2  
3 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 1 ▶use HTML::EP ();
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7  
8 package HTML::EP::EditTable;
9  
10 1 @HTML::EP::EditTable::ISA = qw(HTML::EP);
11  
12  
13  
14 0 ▶sub _ep_edittable_edit {
Subroutine _ep_edittable_edit: not called CC=30 SCAR=68.4
15 0 my $self = shift; my $attr = shift;
16 0 my $cgi = $self->{'cgi'};
17 0 my $action = $cgi->param('what-to-do');
18 0 ▶ my $table = $attr->{'table'}
Condition0%0/2
$attr->{'table'} || die('Missing attribute: table (Table name)')
l!l
00
MC/DC0%0/1
$attr->{'table'} || die('Missing attribute: table (Table name)')
$attr->{'table'}
Truth table0%0/2
A
A= $attr->{'table'}
Aresult
11
00
19 || die "Missing attribute: table (Table name)";
20 0 ▶ my $id_col = $attr->{'id'} || 'ID';
Condition0%0/2
$attr->{'id'} || 'ID'
l!l
00
MC/DC0%0/1
$attr->{'id'} || 'ID'
$attr->{'id'}
Truth table0%0/2
A
A= $attr->{'id'}
Aresult
11
00
21 0 my $result;
22 0 my($query, $id);
23  
24 0 ▶ my $dest = ($attr->{'dest'} ||= $table);
Condition0%0/3
$attr->{"dest"} ||= $table
l!l&&r!l&&!r
000
MC/DC0%0/2
$attr->{"dest"} ||= $table
$attr->{"dest"} $table
Truth table0%0/3
A ||= B
A= $attr->{"dest"}B= $table
ABresult
1X1
011
000
25 0 ▶ $attr->{'prefix'} ||= "$dest\_";
Condition0%0/2
$attr->{"prefix"} ||= "${dest}_"
l!l
00
MC/DC0%0/1
$attr->{"prefix"} ||= "${dest}_"
$attr->{"prefix"}
Truth table0%0/2
A
A= $attr->{"prefix"}
Aresult
11
00
26 0 ▶ $attr->{'sqlquery'} ||= 1;
Condition0%0/2
$attr->{"sqlquery"} ||= 1
l!l
00
MC/DC0%0/1
$attr->{"sqlquery"} ||= 1
$attr->{"sqlquery"}
Truth table0%0/2
A
A= $attr->{"sqlquery"}
Aresult
11
00
27  
28 0 ▶ if ($action eq 'insert') {
Branch: 3 branches
BranchTrueFalseTotal
if ($action eq 'insert') { } 0 0 0
elsif ($action eq 'update') { } 0 0 0
elsif ($id = $cgi->param($id_col)) { } 0 0 0
29 0 $self->_ep_input($attr);
30 0 $result = $self->{$dest};
31 $query = "INSERT INTO $table (" . $result->{'names'}. ") VALUES"
32 0 . " (" . $result->{'values'}. ")";
33 } elsif ($action eq 'update') {
34 0 $self->_ep_input($attr);
35 0 $result = $self->{$dest};
36 0 $id = $cgi->param($id_col);
37 0 $query = "UPDATE $table SET " . $result->{'update'} .
38 " WHERE $id_col = $id";
39 } elsif ($id = $cgi->param($id_col)) {
40 0 my $q = "SELECT * FROM $table WHERE $id_col = $id";
41 0 ▶ print "Select query: $q\n" if $self->{'debug'};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"debug"} 0 0 0
42 0 my $sth = $self->{'dbh'}->prepare($q);
43 0 $sth->execute();
44 0 my $names = $sth->{'NAME'};
45 0 my $types = $sth->{'TYPE'};
46 0 ▶ my $row = $sth->fetchrow_arrayref()
Branch: 1 branch
BranchTrueFalseTotal
my $row = $sth->fetchrow_arrayref or die "Failed to fetch row with ID ${id}: No such row" 0 0 0
47 or die "Failed to fetch row with ID $id: No such row";
48 0 $sth->finish();
49 0 my %result;
50 0 ▶ for (my $i = 0; $i < @$row; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @$row) 0 0 0
51 0 my $type = $types->[$i];
52 0 my $name = $names->[$i];
53 0 my $ref = { 'col' => $name,
54 'val' => $row->[$i] };
55 0 ▶ if ($type == DBI::SQL_DATE()) {
Branch: 2 branches
BranchTrueFalseTotal
if ($type == DBI::SQL_DATE()) { } 0 0 0
elsif ($type == DBI::SQL_NUMERIC() or $type == DBI::SQL_DECIMAL() or $type == DBI::SQL_INTEGER() or $type == DBI::SQL_SMALLINT() or $type == DBI::SQL_FLOAT() or $type == DBI::SQL_REAL() or $type == DBI::SQL_DOUBLE() or $type == DBI::SQL_BIGINT() or $type == DBI::SQL_TINYINT()) { } 0 0 0
56 0 $ref->{'type'} = 'd';
57 0 ▶ if (!defined($row->[$i])) {
Branch: 2 branches
BranchTrueFalseTotal
if (not defined $row->[$i]) { } 0 0 0
elsif ($row->[$i] =~ /^(\d\d\d\d)\-(\d\d)\-(\d\d)/) { } 0 0 0
58 0 $ref->{'day'} = $ref->{'month'} = $ref->{'year'} = '';
59 } elsif ($row->[$i] =~ /^(\d\d\d\d)\-(\d\d)\-(\d\d)/) {
60 0 $ref->{'day'} = $3;
61 0 $ref->{'month'} = $2;
62 0 $ref->{'year'} = $1;
63 } else {
64 0 die "Cannot parse date: $row->[$i]";
65 }
66 ▶ } elsif ($type == DBI::SQL_NUMERIC() ||
Condition0%0/24
$type == DBI::SQL_NUMERIC() or $type == DBI::SQL_DECIMAL() or $type == DBI::SQL_INTEGER() or $type == DBI::SQL_SMALLINT() or $type == DBI::SQL_FLOAT() or $type == DBI::SQL_REAL() or $type == DBI::SQL_DOUBLE() or $type == DBI::SQL_BIGINT() or $type == DBI::SQL_TINYINT()
l!l&&r!l&&!r
000
$type == DBI::SQL_NUMERIC() or $type == DBI::SQL_DECIMAL() or $type == DBI::SQL_INTEGER() or $type == DBI::SQL_SMALLINT() or $type == DBI::SQL_FLOAT() or $type == DBI::SQL_REAL() or $type == DBI::SQL_DOUBLE() or $type == DBI::SQL_BIGINT()
l!l&&r!l&&!r
000
$type == DBI::SQL_NUMERIC() or $type == DBI::SQL_DECIMAL() or $type == DBI::SQL_INTEGER() or $type == DBI::SQL_SMALLINT() or $type == DBI::SQL_FLOAT() or $type == DBI::SQL_REAL() or $type == DBI::SQL_DOUBLE()
l!l&&r!l&&!r
000
$type == DBI::SQL_NUMERIC() or $type == DBI::SQL_DECIMAL() or $type == DBI::SQL_INTEGER() or $type == DBI::SQL_SMALLINT() or $type == DBI::SQL_FLOAT() or $type == DBI::SQL_REAL()
l!l&&r!l&&!r
000
$type == DBI::SQL_NUMERIC() or $type == DBI::SQL_DECIMAL() or $type == DBI::SQL_INTEGER() or $type == DBI::SQL_SMALLINT() or $type == DBI::SQL_FLOAT()
l!l&&r!l&&!r
000
$type == DBI::SQL_NUMERIC() or $type == DBI::SQL_DECIMAL() or $type == DBI::SQL_INTEGER() or $type == DBI::SQL_SMALLINT()
l!l&&r!l&&!r
000
$type == DBI::SQL_NUMERIC() or $type == DBI::SQL_DECIMAL() or $type == DBI::SQL_INTEGER()
l!l&&r!l&&!r
000
$type == DBI::SQL_NUMERIC() or $type == DBI::SQL_DECIMAL()
l!l&&r!l&&!r
000
MC/DC0%0/9
$type == DBI::SQL_NUMERIC() or $type == DBI::SQL_DECIMAL() or $type == DBI::SQL_INTEGER() or $type == DBI::SQL_SMALLINT() or $type == DBI::SQL_FLOAT() or $type == DBI::SQL_REAL() or $type == DBI::SQL_DOUBLE() or $type == DBI::SQL_BIGINT() or $type == DBI::SQL_TINYINT()
$type == DBI::SQL_NUMERIC() $type == DBI::SQL_DECIMAL() $type == DBI::SQL_INTEGER() $type == DBI::SQL_SMALLINT() $type == DBI::SQL_FLOAT() $type == DBI::SQL_REAL() $type == DBI::SQL_DOUBLE() $type == DBI::SQL_BIGINT() $type == DBI::SQL_TINYINT()
Truth table0%0/10
A or B or C or D or E or F or G or H or I
A= $type == DBI::SQL_NUMERIC()B= $type == DBI::SQL_DECIMAL()C= $type == DBI::SQL_INTEGER()D= $type == DBI::SQL_SMALLINT()E= $type == DBI::SQL_FLOAT()F= $type == DBI::SQL_REAL()G= $type == DBI::SQL_DOUBLE()H= $type == DBI::SQL_BIGINT()I= $type == DBI::SQL_TINYINT()
ABCDEFGHIresult
1XXXXXXXX1
01XXXXXXX1
001XXXXXX1
0001XXXXX1
00001XXXX1
000001XXX1
0000001XX1
00000001X1
0000000011
0000000000
67 $type == DBI::SQL_DECIMAL() ||
68 $type == DBI::SQL_INTEGER() ||
69 $type == DBI::SQL_SMALLINT() ||
70 $type == DBI::SQL_FLOAT() ||
71 $type == DBI::SQL_REAL() ||
72 $type == DBI::SQL_DOUBLE() ||
73 $type == DBI::SQL_BIGINT() ||
74 $type == DBI::SQL_TINYINT()) {
75 0 $ref->{'type'} = 'n';
76 } else {
77 0 $ref->{'type'} = 't';
78 }
79 0 $result->{$name} = $ref;
80 }
81 0 $self->{$dest} = $result;
82 0 ▶ if ($action eq 'delete') {
Branch: 1 branch
BranchTrueFalseTotal
if ($action eq "delete") 0 0 0
83 0 $query = "DELETE FROM $table WHERE $id_col = $id";
84 }
85 }
86 0 ▶ if ($query) {
Branch: 1 branch
BranchTrueFalseTotal
if ($query) 0 0 0
87 0 ▶ print "Executing query: $query\n" if $self->{'debug'};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"debug"} 0 0 0
88 0 my $dbh = $self->{'dbh'};
89 0 $dbh->do($query);
90 0 ▶ if (!defined($id) && $dbh->{'Driver'}->{'Name'} eq 'mysql') {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $id and $dbh->{"Driver"}{"Name"} eq "mysql") 0 0 0
Condition0%0/3
not defined $id and $dbh->{"Driver"}{"Name"} eq "mysql"
!ll&&!rl&&r
000
MC/DC0%0/2
not defined $id and $dbh->{"Driver"}{"Name"} eq "mysql"
not defined $id $dbh->{"Driver"}{"Name"} eq "mysql"
Truth table0%0/3
not(A) and B
A= not defined $idB= $dbh->{"Driver"}{"Name"} eq "mysql"
ABresult
0X0
100
111
91 0 $id = $dbh->{'mysql_insertid'};
92 0 ▶ print "Auto-ID is $id.\n" if $self->{'debug'};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"debug"} 0 0 0
93 0 $cgi->param($id_col, $id);
94 }
95 }
96 0 '';
97 }
98  
99  
100 0 ▶sub _ep_edittable_select {
Subroutine _ep_edittable_select: not called CC=20 SCAR=60.4
101 0 my $self = shift; my $attr = shift;
102 0 ▶ my $table = $attr->{'table'}
Condition0%0/2
$attr->{'table'} || die('Missing attribute: table (Table name)')
l!l
00
MC/DC0%0/1
$attr->{'table'} || die('Missing attribute: table (Table name)')
$attr->{'table'}
Truth table0%0/2
A
A= $attr->{'table'}
Aresult
11
00
103 || die "Missing attribute: table (Table name)";
104 0 my $dbh = $self->{'dbh'};
105 0 my $cgi = $self->{'cgi'};
106 0 my $debug = $self->{'debug'};
107  
108 0 my(@where, @order, @url);
109 0 foreach my $key ($cgi->param()) {
110 0 ▶ if ($key =~ /^select_(\w+)_(.*)/) {
Branch: 2 branches
BranchTrueFalseTotal
if ($key =~ /^select_(\w+)_(.*)/) { } 0 0 0
elsif ($key =~ /^order_(\w+)_(.*)/) { } 0 0 0
111 0 my $col = $2;
112 0 my $type = $1;
113 0 my $val = $cgi->param($col);
114 0 ▶ if ($type eq 'like') {
Branch: 3 branches
BranchTrueFalseTotal
if ($type eq 'like') { } 0 0 0
elsif ($type eq 'like_') { } 0 0 0
elsif ($type eq '_like') { } 0 0 0
115 0 ▶ push(@where, "$col LIKE " . $dbh->quote("%$val%")) if $val;
Branch: 1 branch
BranchTrueFalseTotal
if $val 0 0 0
116 } elsif ($type eq 'like_') {
117 0 ▶ push(@where, "$col LIKE " . $dbh->quote("$val%")) if $val;
Branch: 1 branch
BranchTrueFalseTotal
if $val 0 0 0
118 } elsif ($type eq '_like') {
119 0 ▶ push(@where, "$col LIKE " . $dbh->quote("%$val")) if $val;
Branch: 1 branch
BranchTrueFalseTotal
if $val 0 0 0
120 }
121 0 push(@url, "$key=" . CGI->escape($val));
122 } elsif ($key =~ /^order_(\w+)_(.*)/) {
123 0 push(@order, "$2 $1");
124 0 my $val = $cgi->param($2);
125 0 push(@url, "$key=" . CGI->escape($val));
126 }
127 }
128  
129 0 ▶ my $start = $cgi->param('start') || 0;
Condition0%0/2
$cgi->param('start') || 0
l!l
00
MC/DC0%0/1
$cgi->param('start') || 0
$cgi->param('start')
Truth table0%0/2
A
A= $cgi->param('start')
Aresult
11
00
130 0 ▶ my $max = $cgi->param('max') || $attr->{'max'} || 20;
Condition0%0/5
$cgi->param('max') || $attr->{'max'} || 20
l!l
00
$cgi->param("max") or $attr->{"max"}
l!l&&r!l&&!r
000
MC/DC0%0/2
$cgi->param('max') || $attr->{'max'} || 20
$cgi->param("max") $attr->{"max"}
Truth table0%0/3
A or B
A= $cgi->param("max")B= $attr->{"max"}
ABresult
1X1
011
000
131 0 ▶ my $count_query = "SELECT COUNT(*) FROM $table"
Branch: 1 branch
BranchTrueFalseTotal
@where ? : 0 0 0
132 . (@where ? " WHERE " . join(" AND ", @where) : "");
133 0 ▶ print "Count query is: $count_query\n" if $debug;
Branch: 1 branch
BranchTrueFalseTotal
if $debug 0 0 0
134 0 ▶ my $query = "SELECT * FROM $table"
Branch: 2 branches
BranchTrueFalseTotal
@where ? : 0 0 0
@order ? : 0 0 0
135 . (@where ? " WHERE " . join(" AND ", @where) : "")
136 . (@order ? " ORDER BY " . join(", ", @order) : "")
137 . " LIMIT $start, $max";
138 0 ▶ print "Query is: $query\n" if $debug;
Branch: 1 branch
BranchTrueFalseTotal
if $debug 0 0 0
139  
140 0 $self->{'start'} = $start;
141 0 $self->{'max'} = $max;
142 0 ▶ $self->{'query_url'} = @url ? ("&" . join("&", @url)) : "";
Branch: 1 branch
BranchTrueFalseTotal
@url ? : 0 0 0
143 0 my $sth = $dbh->prepare($count_query);
144 0 $sth->execute();
145 0 $self->{'num_rows'} = $sth->fetchrow_array(); # Array context!
146 0 $self->_ep_query({'statement' => $query,
147 'result' => $table});
148 0 '';
149 }
150  
151  
152 0 ▶sub _ep_edittable_links {
Subroutine _ep_edittable_links: not called CC=8 SCAR=42.8
153 0 my $self = shift; my $attr = shift;
154 0 my $max = $self->{'max'};
155 0 my $start = $self->{'start'};
156 0 ▶ my $page = $attr->{'path'} || $ENV{'PATH_INFO'};
Condition0%0/3
$attr->{'path'} || $ENV{'PATH_INFO'}
l!l&&r!l&&!r
000
MC/DC0%0/2
$attr->{'path'} || $ENV{'PATH_INFO'}
$attr->{'path'} $ENV{'PATH_INFO'}
Truth table0%0/3
A || B
A= $attr->{'path'}B= $ENV{'PATH_INFO'}
ABresult
1X1
011
000
157 0 ▶ my $max_links = $attr->{'max_links'} || 10;
Condition0%0/2
$attr->{'max_links'} || 10
l!l
00
MC/DC0%0/1
$attr->{'max_links'} || 10
$attr->{'max_links'}
Truth table0%0/2
A
A= $attr->{'max_links'}
Aresult
11
00
158 $self->{'prev'} = $start ?
159 0 ▶ "<a href=$page?start=" . ($start - $max) . $self->{'query_url'} .
Branch: 1 branch
BranchTrueFalseTotal
$start ? : 0 0 0
160 ">Zurück</a>" : "";
161 0 my $links = '';
162 $self->{'next'} = ($self->{'num_rows'} > $start + $max) ?
163 0 ▶ "<a href=select.ep?start=" . ($start + $max) . $self->{'query_url'} .
Branch: 1 branch
BranchTrueFalseTotal
$self->{'num_rows'} > $start + $max ? : 0 0 0
164 ">Weiter</a>" : "";
165 0 my $base = $max * $max_links;
166 0 my $first = int(($start + $base - 1) / $base);
167 0 ▶ for (my $i = 0; $i < $max_links; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $max_links) 0 0 0
168 0 my $num = $first + $i;
169 0 ▶ if ($self->{'num_rows'} > $num * $max) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"num_rows"} > $num * $max) 0 0 0
170 0 ▶ if ($num * $max == $start) {
Branch: 1 branch
BranchTrueFalseTotal
if ($num * $max == $start) { } 0 0 0
171 0 $links .= $num+1;
172 } else {
173 $links .= "<a href=$page?start=" . ($num * $max) .
174 0 $self->{'query_url'} . ">" . ($num+1) . "</a>";
175 }
176 }
177 }
178 0 $self->{'prev'} . $links . $self->{'next'};
179 }
180  
181  
182 1 1;
183  
184  
185 __END__
↑ Summary blib/lib/HTML/EP/Shop.pm »