line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
# -*- perl -*- |
2
|
|
|
|
|
|
|
|
3
|
1
|
|
|
1
|
|
6
|
use strict; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
39
|
|
4
|
|
|
|
|
|
|
|
5
|
1
|
|
|
1
|
|
6
|
use HTML::EP (); |
|
1
|
|
|
|
|
1
|
|
|
1
|
|
|
|
|
1990
|
|
6
|
|
|
|
|
|
|
|
7
|
|
|
|
|
|
|
|
8
|
|
|
|
|
|
|
package HTML::EP::EditTable; |
9
|
|
|
|
|
|
|
|
10
|
|
|
|
|
|
|
@HTML::EP::EditTable::ISA = qw(HTML::EP); |
11
|
|
|
|
|
|
|
|
12
|
|
|
|
|
|
|
|
13
|
|
|
|
|
|
|
|
14
|
|
|
|
|
|
|
sub _ep_edittable_edit { |
15
|
0
|
|
|
0
|
|
|
my $self = shift; my $attr = shift; |
|
0
|
|
|
|
|
|
|
16
|
0
|
|
|
|
|
|
my $cgi = $self->{'cgi'}; |
17
|
0
|
|
|
|
|
|
my $action = $cgi->param('what-to-do'); |
18
|
0
|
|
0
|
|
|
|
my $table = $attr->{'table'} |
19
|
|
|
|
|
|
|
|| die "Missing attribute: table (Table name)"; |
20
|
0
|
|
0
|
|
|
|
my $id_col = $attr->{'id'} || 'ID'; |
21
|
0
|
|
|
|
|
|
my $result; |
22
|
0
|
|
|
|
|
|
my($query, $id); |
23
|
|
|
|
|
|
|
|
24
|
0
|
|
0
|
|
|
|
my $dest = ($attr->{'dest'} ||= $table); |
25
|
0
|
|
0
|
|
|
|
$attr->{'prefix'} ||= "$dest\_"; |
26
|
0
|
|
0
|
|
|
|
$attr->{'sqlquery'} ||= 1; |
27
|
|
|
|
|
|
|
|
28
|
0
|
0
|
|
|
|
|
if ($action eq 'insert') { |
|
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
29
|
0
|
|
|
|
|
|
$self->_ep_input($attr); |
30
|
0
|
|
|
|
|
|
$result = $self->{$dest}; |
31
|
0
|
|
|
|
|
|
$query = "INSERT INTO $table (" . $result->{'names'}. ") VALUES" |
32
|
|
|
|
|
|
|
. " (" . $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
|
0
|
|
|
|
|
print "Select query: $q\n" if $self->{'debug'}; |
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
|
0
|
|
|
|
|
my $row = $sth->fetchrow_arrayref() |
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++) { |
51
|
0
|
|
|
|
|
|
my $type = $types->[$i]; |
52
|
0
|
|
|
|
|
|
my $name = $names->[$i]; |
53
|
0
|
|
|
|
|
|
my $ref = { 'col' => $name, |
54
|
|
|
|
|
|
|
'val' => $row->[$i] }; |
55
|
0
|
0
|
0
|
|
|
|
if ($type == DBI::SQL_DATE()) { |
|
|
0
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
56
|
0
|
|
|
|
|
|
$ref->{'type'} = 'd'; |
57
|
0
|
0
|
|
|
|
|
if (!defined($row->[$i])) { |
|
|
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() || |
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
|
0
|
|
|
|
|
if ($action eq 'delete') { |
83
|
0
|
|
|
|
|
|
$query = "DELETE FROM $table WHERE $id_col = $id"; |
84
|
|
|
|
|
|
|
} |
85
|
|
|
|
|
|
|
} |
86
|
0
|
0
|
|
|
|
|
if ($query) { |
87
|
0
|
0
|
|
|
|
|
print "Executing query: $query\n" if $self->{'debug'}; |
88
|
0
|
|
|
|
|
|
my $dbh = $self->{'dbh'}; |
89
|
0
|
|
|
|
|
|
$dbh->do($query); |
90
|
0
|
0
|
0
|
|
|
|
if (!defined($id) && $dbh->{'Driver'}->{'Name'} eq 'mysql') { |
91
|
0
|
|
|
|
|
|
$id = $dbh->{'mysql_insertid'}; |
92
|
0
|
0
|
|
|
|
|
print "Auto-ID is $id.\n" if $self->{'debug'}; |
93
|
0
|
|
|
|
|
|
$cgi->param($id_col, $id); |
94
|
|
|
|
|
|
|
} |
95
|
|
|
|
|
|
|
} |
96
|
0
|
|
|
|
|
|
''; |
97
|
|
|
|
|
|
|
} |
98
|
|
|
|
|
|
|
|
99
|
|
|
|
|
|
|
|
100
|
|
|
|
|
|
|
sub _ep_edittable_select { |
101
|
0
|
|
|
0
|
|
|
my $self = shift; my $attr = shift; |
|
0
|
|
|
|
|
|
|
102
|
0
|
|
0
|
|
|
|
my $table = $attr->{'table'} |
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
|
0
|
|
|
|
|
if ($key =~ /^select_(\w+)_(.*)/) { |
|
|
0
|
|
|
|
|
|
111
|
0
|
|
|
|
|
|
my $col = $2; |
112
|
0
|
|
|
|
|
|
my $type = $1; |
113
|
0
|
|
|
|
|
|
my $val = $cgi->param($col); |
114
|
0
|
0
|
|
|
|
|
if ($type eq 'like') { |
|
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
115
|
0
|
0
|
|
|
|
|
push(@where, "$col LIKE " . $dbh->quote("%$val%")) if $val; |
116
|
|
|
|
|
|
|
} elsif ($type eq 'like_') { |
117
|
0
|
0
|
|
|
|
|
push(@where, "$col LIKE " . $dbh->quote("$val%")) if $val; |
118
|
|
|
|
|
|
|
} elsif ($type eq '_like') { |
119
|
0
|
0
|
|
|
|
|
push(@where, "$col LIKE " . $dbh->quote("%$val")) if $val; |
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
|
|
0
|
|
|
|
my $start = $cgi->param('start') || 0; |
130
|
0
|
|
0
|
|
|
|
my $max = $cgi->param('max') || $attr->{'max'} || 20; |
131
|
0
|
0
|
|
|
|
|
my $count_query = "SELECT COUNT(*) FROM $table" |
132
|
|
|
|
|
|
|
. (@where ? " WHERE " . join(" AND ", @where) : ""); |
133
|
0
|
0
|
|
|
|
|
print "Count query is: $count_query\n" if $debug; |
134
|
0
|
0
|
|
|
|
|
my $query = "SELECT * FROM $table" |
|
|
0
|
|
|
|
|
|
135
|
|
|
|
|
|
|
. (@where ? " WHERE " . join(" AND ", @where) : "") |
136
|
|
|
|
|
|
|
. (@order ? " ORDER BY " . join(", ", @order) : "") |
137
|
|
|
|
|
|
|
. " LIMIT $start, $max"; |
138
|
0
|
0
|
|
|
|
|
print "Query is: $query\n" if $debug; |
139
|
|
|
|
|
|
|
|
140
|
0
|
|
|
|
|
|
$self->{'start'} = $start; |
141
|
0
|
|
|
|
|
|
$self->{'max'} = $max; |
142
|
0
|
0
|
|
|
|
|
$self->{'query_url'} = @url ? ("&" . join("&", @url)) : ""; |
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
|
|
|
|
|
|
|
sub _ep_edittable_links { |
153
|
0
|
|
|
0
|
|
|
my $self = shift; my $attr = shift; |
|
0
|
|
|
|
|
|
|
154
|
0
|
|
|
|
|
|
my $max = $self->{'max'}; |
155
|
0
|
|
|
|
|
|
my $start = $self->{'start'}; |
156
|
0
|
|
0
|
|
|
|
my $page = $attr->{'path'} || $ENV{'PATH_INFO'}; |
157
|
0
|
|
0
|
|
|
|
my $max_links = $attr->{'max_links'} || 10; |
158
|
0
|
0
|
|
|
|
|
$self->{'prev'} = $start ? |
159
|
|
|
|
|
|
|
"{'query_url'} . |
160
|
|
|
|
|
|
|
">Zurück" : ""; |
161
|
0
|
|
|
|
|
|
my $links = ''; |
162
|
0
|
0
|
|
|
|
|
$self->{'next'} = ($self->{'num_rows'} > $start + $max) ? |
163
|
|
|
|
|
|
|
"{'query_url'} . |
164
|
|
|
|
|
|
|
">Weiter" : ""; |
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++) { |
168
|
0
|
|
|
|
|
|
my $num = $first + $i; |
169
|
0
|
0
|
|
|
|
|
if ($self->{'num_rows'} > $num * $max) { |
170
|
0
|
0
|
|
|
|
|
if ($num * $max == $start) { |
171
|
0
|
|
|
|
|
|
$links .= $num+1; |
172
|
|
|
|
|
|
|
} else { |
173
|
0
|
|
|
|
|
|
$links .= "
|
174
|
|
|
|
|
|
|
$self->{'query_url'} . ">" . ($num+1) . ""; |
175
|
|
|
|
|
|
|
} |
176
|
|
|
|
|
|
|
} |
177
|
|
|
|
|
|
|
} |
178
|
0
|
|
|
|
|
|
$self->{'prev'} . $links . $self->{'next'}; |
179
|
|
|
|
|
|
|
} |
180
|
|
|
|
|
|
|
|
181
|
|
|
|
|
|
|
|
182
|
|
|
|
|
|
|
1; |
183
|
|
|
|
|
|
|
|
184
|
|
|
|
|
|
|
|
185
|
|
|
|
|
|
|
__END__ |