line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package LEOCHARRE::Database::Base; |
2
|
3
|
|
|
3
|
|
43179
|
use strict; |
|
3
|
|
|
|
|
5
|
|
|
3
|
|
|
|
|
120
|
|
3
|
3
|
|
|
3
|
|
1618
|
use LEOCHARRE::DEBUG; |
|
3
|
|
|
|
|
3919
|
|
|
3
|
|
|
|
|
22
|
|
4
|
3
|
|
|
3
|
|
296
|
use warnings; |
|
3
|
|
|
|
|
7
|
|
|
3
|
|
|
|
|
78
|
|
5
|
3
|
|
|
3
|
|
16
|
use Carp; |
|
3
|
|
|
|
|
5
|
|
|
3
|
|
|
|
|
269
|
|
6
|
3
|
|
|
3
|
|
17
|
use Exporter; |
|
3
|
|
|
|
|
6
|
|
|
3
|
|
|
|
|
114
|
|
7
|
|
|
|
|
|
|
|
8
|
|
|
|
|
|
|
|
9
|
3
|
|
|
3
|
|
15
|
use vars qw($VERSION @ISA @EXPORT_OK %EXPORT_TAGS); |
|
3
|
|
|
|
|
4
|
|
|
3
|
|
|
|
|
5019
|
|
10
|
|
|
|
|
|
|
@ISA = qw/Exporter/; |
11
|
|
|
|
|
|
|
|
12
|
|
|
|
|
|
|
@EXPORT_OK = qw( |
13
|
|
|
|
|
|
|
rows_count |
14
|
|
|
|
|
|
|
is_sqlite |
15
|
|
|
|
|
|
|
is_mysql |
16
|
|
|
|
|
|
|
drop_table |
17
|
|
|
|
|
|
|
lid |
18
|
|
|
|
|
|
|
close_active_handles |
19
|
|
|
|
|
|
|
table_exists |
20
|
|
|
|
|
|
|
table_dump |
21
|
|
|
|
|
|
|
driver |
22
|
|
|
|
|
|
|
selectcol |
23
|
|
|
|
|
|
|
connect_sqlite |
24
|
|
|
|
|
|
|
connect_mysql |
25
|
|
|
|
|
|
|
); |
26
|
|
|
|
|
|
|
|
27
|
|
|
|
|
|
|
%EXPORT_TAGS = ( all => \@EXPORT_OK ); |
28
|
|
|
|
|
|
|
|
29
|
|
|
|
|
|
|
$VERSION = sprintf "%d.%02d", q$Revision: 1.7 $ =~ /(\d+)/g; |
30
|
|
|
|
|
|
|
|
31
|
|
|
|
|
|
|
|
32
|
|
|
|
|
|
|
|
33
|
7
|
|
|
7
|
|
23781
|
sub DBI::db::rows_count { return rows_count(@_); } |
34
|
3
|
|
|
3
|
|
273
|
sub DBI::db::is_sqlite { return is_sqlite(@_); } |
35
|
2
|
|
|
2
|
|
7
|
sub DBI::db::is_mysql { return is_mysql(@_); } |
36
|
0
|
|
|
0
|
|
0
|
sub DBI::db::drop_table { return drop_table(@_); } |
37
|
3
|
|
|
3
|
|
2473
|
sub DBI::db::lid { return lid(@_); } |
38
|
1
|
|
|
1
|
|
4
|
sub DBI::db::close_active_handles { return close_active_handles(@_); } |
39
|
4
|
|
|
4
|
|
17723
|
sub DBI::db::table_exists { return table_exists(@_); } |
40
|
2
|
|
|
2
|
|
8578
|
sub DBI::db::table_dump { return table_dump(@_); } |
41
|
3
|
|
|
3
|
|
390
|
sub DBI::db::driver { return driver(@_); } |
42
|
|
|
|
|
|
|
|
43
|
2
|
|
|
2
|
|
15
|
sub DBI::db::selectcol { return selectcol(@_); } |
44
|
|
|
|
|
|
|
|
45
|
3
|
|
|
3
|
0
|
18832
|
sub DBI::connect_sqlite { return connect_sqlite(@_); } |
46
|
0
|
|
|
0
|
0
|
0
|
sub DBI::connect_mysql { return connect_mysql(@_); } |
47
|
|
|
|
|
|
|
|
48
|
|
|
|
|
|
|
|
49
|
|
|
|
|
|
|
|
50
|
|
|
|
|
|
|
# FUNCTIONAL ORIENTED |
51
|
|
|
|
|
|
|
|
52
|
|
|
|
|
|
|
sub rows_count { |
53
|
7
|
|
|
7
|
1
|
27
|
my ($dbh,$statement,$key,$val) = @_; |
54
|
7
|
50
|
|
|
|
31
|
defined $statement or confess('missing statement argument'); |
55
|
7
|
|
|
|
|
78
|
$statement=~s/^\s+|\s+$//g; |
56
|
|
|
|
|
|
|
|
57
|
|
|
|
|
|
|
|
58
|
|
|
|
|
|
|
# is first arg a table name? |
59
|
7
|
100
|
|
|
|
54
|
if ($statement=~/^(\w+)$/){ |
60
|
1
|
|
|
|
|
6
|
my $tname = $1; |
61
|
1
|
|
|
|
|
14
|
debug("arg 1 is table name"); |
62
|
|
|
|
|
|
|
|
63
|
1
|
|
|
|
|
13
|
$statement = "SELECT COUNT(*) FROM $tname"; |
64
|
|
|
|
|
|
|
|
65
|
1
|
50
|
33
|
|
|
9
|
if (defined $key and defined $val){ |
66
|
0
|
|
|
|
|
0
|
debug("key and val arguments are defined also [$key:$val]"); |
67
|
|
|
|
|
|
|
|
68
|
0
|
|
|
|
|
0
|
$statement.=" WHERE $key='$val'"; |
69
|
0
|
|
|
|
|
0
|
debug("stmnt: [$statement]\n"); |
70
|
|
|
|
|
|
|
} |
71
|
|
|
|
|
|
|
|
72
|
|
|
|
|
|
|
} |
73
|
|
|
|
|
|
|
|
74
|
|
|
|
|
|
|
# if not, we expect no more arguments |
75
|
|
|
|
|
|
|
else { |
76
|
6
|
|
|
|
|
39
|
debug("arg 1 is statement"); |
77
|
6
|
50
|
33
|
|
|
137
|
if (defined $key or defined $val){ |
78
|
0
|
|
|
|
|
0
|
carp('first argument was a statement, no more arguments should be provided.'); |
79
|
|
|
|
|
|
|
} |
80
|
|
|
|
|
|
|
|
81
|
6
|
50
|
|
|
|
55
|
$statement=~/count\s*\(/i or confess("statement to dbh_count() must contain COUNT()"); |
82
|
|
|
|
|
|
|
|
83
|
|
|
|
|
|
|
} |
84
|
|
|
|
|
|
|
|
85
|
7
|
|
|
|
|
27
|
return _rows_count($dbh,$statement); |
86
|
|
|
|
|
|
|
} |
87
|
|
|
|
|
|
|
|
88
|
|
|
|
|
|
|
sub _rows_count { |
89
|
7
|
|
|
7
|
|
13
|
my ($dbh,$statement) = @_; |
90
|
7
|
50
|
|
|
|
160
|
defined $statement or confess('missing statement argument'); |
91
|
7
|
50
|
|
|
|
114
|
my $c = $dbh->prepare($statement) or confess($dbh->errstr); |
92
|
7
|
|
|
|
|
2525
|
$c->execute; |
93
|
7
|
|
|
|
|
107
|
my $r = $c->fetchrow_arrayref; |
94
|
7
|
|
|
|
|
19
|
my $count = $r->[0]; |
95
|
7
|
|
100
|
|
|
39
|
$count ||= 0; |
96
|
7
|
|
|
|
|
187
|
return $count; |
97
|
|
|
|
|
|
|
} |
98
|
|
|
|
|
|
|
|
99
|
|
|
|
|
|
|
sub is_sqlite { |
100
|
3
|
|
|
3
|
1
|
7
|
my $dbh = shift; |
101
|
3
|
50
|
|
|
|
17
|
my $d = driver($dbh) or return 0; |
102
|
3
|
50
|
|
|
|
25
|
$d=~/sqlite/i or return 0; |
103
|
3
|
|
|
|
|
30
|
return 1; |
104
|
|
|
|
|
|
|
} |
105
|
|
|
|
|
|
|
|
106
|
|
|
|
|
|
|
sub is_mysql { |
107
|
2
|
|
|
2
|
1
|
4
|
my $dbh = shift; |
108
|
2
|
50
|
|
|
|
6
|
my $d = driver($dbh) or return 0; |
109
|
2
|
50
|
|
|
|
15
|
$d=~/mysql/i or return 0; |
110
|
0
|
|
|
|
|
0
|
return 1; |
111
|
|
|
|
|
|
|
} |
112
|
|
|
|
|
|
|
|
113
|
|
|
|
|
|
|
sub driver { |
114
|
8
|
|
|
8
|
1
|
14
|
my $dbh = shift; |
115
|
8
|
50
|
|
|
|
33
|
defined $dbh or confess('missing dbh object as arg'); |
116
|
8
|
50
|
0
|
|
|
88
|
defined $dbh->{Driver} or debug("attribute 'Driver' not present in dbh obj passed") and return; |
117
|
8
|
50
|
|
|
|
86
|
my $n = $dbh->{Driver}->{Name} or return; |
118
|
8
|
|
|
|
|
122
|
return $n; |
119
|
|
|
|
|
|
|
} |
120
|
|
|
|
|
|
|
|
121
|
|
|
|
|
|
|
sub drop_table { |
122
|
0
|
|
|
0
|
1
|
0
|
my ($dbh,$tablename)= @_; |
123
|
0
|
0
|
0
|
|
|
0
|
defined $dbh and defined $tablename or die('missing args'); |
124
|
0
|
|
|
|
|
0
|
local $dbh->{PrintError}; |
125
|
0
|
|
|
|
|
0
|
local $dbh->{RaiseError}; |
126
|
0
|
|
|
|
|
0
|
$dbh->do("DROP TABLE IF EXISTS $tablename"); |
127
|
0
|
|
|
|
|
0
|
return 1; |
128
|
|
|
|
|
|
|
} |
129
|
|
|
|
|
|
|
|
130
|
|
|
|
|
|
|
sub lid { |
131
|
3
|
|
|
3
|
1
|
20
|
my($dbh,$tablename) = @_; |
132
|
3
|
50
|
33
|
|
|
51
|
defined $dbh and defined $tablename or die('missing args'); |
133
|
|
|
|
|
|
|
|
134
|
3
|
|
|
|
|
43
|
my $id = $dbh->last_insert_id(undef,undef,$tablename,undef); |
135
|
|
|
|
|
|
|
|
136
|
3
|
50
|
0
|
|
|
16
|
defined $id |
137
|
|
|
|
|
|
|
or warn("last insert id on table $tablename returns undef, does table exists?") |
138
|
|
|
|
|
|
|
and return; |
139
|
3
|
|
|
|
|
24
|
return $id; |
140
|
|
|
|
|
|
|
} |
141
|
|
|
|
|
|
|
|
142
|
|
|
|
|
|
|
sub close_active_handles { |
143
|
1
|
|
|
1
|
1
|
2
|
my $dbh = shift; |
144
|
1
|
50
|
|
|
|
3
|
defined $dbh or die('missing arg'); |
145
|
1
|
|
|
|
|
2
|
my $x = 1; |
146
|
|
|
|
|
|
|
|
147
|
1
|
|
|
|
|
3
|
debug("closing active handles:"); |
148
|
|
|
|
|
|
|
|
149
|
1
|
50
|
|
|
|
14
|
if ( defined $dbh->{ChildHandles} ){ |
150
|
|
|
|
|
|
|
|
151
|
1
|
|
|
|
|
3
|
HANDLE: for (@{$dbh->{ChildHandles}}){ |
|
1
|
|
|
|
|
6
|
|
152
|
6
|
|
|
|
|
16
|
my $handle = $_; |
153
|
6
|
100
|
|
|
|
18
|
defined $handle or next HANDLE; |
154
|
2
|
50
|
|
|
|
13
|
$handle or next HANDLE; |
155
|
|
|
|
|
|
|
|
156
|
|
|
|
|
|
|
|
157
|
2
|
50
|
|
|
|
17
|
if ($handle->{Active}){ |
158
|
0
|
|
|
|
|
0
|
$handle->finish; |
159
|
0
|
|
|
|
|
0
|
$x++; |
160
|
|
|
|
|
|
|
} |
161
|
|
|
|
|
|
|
|
162
|
2
|
|
|
|
|
8
|
undef $handle; # was $_ |
163
|
|
|
|
|
|
|
} |
164
|
|
|
|
|
|
|
|
165
|
|
|
|
|
|
|
} |
166
|
1
|
|
|
|
|
7
|
debug("$x, done.\n"); |
167
|
|
|
|
|
|
|
|
168
|
1
|
|
|
|
|
9
|
return $x; |
169
|
|
|
|
|
|
|
} |
170
|
|
|
|
|
|
|
|
171
|
|
|
|
|
|
|
sub table_exists { |
172
|
4
|
|
|
4
|
1
|
14
|
my($dbh,$tablename) = @_; |
173
|
4
|
50
|
|
|
|
14
|
defined $dbh or confess('missing dbh'); |
174
|
4
|
50
|
|
|
|
10
|
defined $tablename or confess('missing tablename arg'); |
175
|
|
|
|
|
|
|
|
176
|
4
|
|
|
|
|
96
|
local $dbh->{RaiseError}; |
177
|
4
|
|
|
|
|
43
|
local $dbh->{PrintError}; |
178
|
|
|
|
|
|
|
|
179
|
4
|
100
|
|
|
|
42
|
my $t = $dbh->prepare("select * from $tablename") or return 0; |
180
|
3
|
50
|
|
|
|
776
|
$t->execute or return 0; |
181
|
3
|
|
|
|
|
166
|
return 1; |
182
|
|
|
|
|
|
|
} |
183
|
|
|
|
|
|
|
|
184
|
|
|
|
|
|
|
sub table_dump { |
185
|
2
|
|
|
2
|
1
|
8
|
my ($dbh,$tablename) = @_; |
186
|
2
|
50
|
33
|
|
|
30
|
defined $dbh and defined $tablename or die('missing args'); |
187
|
|
|
|
|
|
|
|
188
|
2
|
50
|
0
|
|
|
8
|
$dbh->table_exists($tablename) |
189
|
|
|
|
|
|
|
or warn("table $tablename does not exist") |
190
|
|
|
|
|
|
|
and return; |
191
|
|
|
|
|
|
|
|
192
|
2
|
|
|
|
|
10
|
my $dump = " # dump table '$tablename':\n"; |
193
|
|
|
|
|
|
|
|
194
|
2
|
|
|
|
|
21
|
my $d = $dbh->selectall_arrayref("SELECT * FROM $tablename"); |
195
|
|
|
|
|
|
|
|
196
|
2
|
50
|
0
|
|
|
405
|
defined $d and scalar @$d or carp("table $tablename had no entries") and return ''; |
|
|
|
50
|
|
|
|
|
197
|
|
|
|
|
|
|
|
198
|
|
|
|
|
|
|
|
199
|
|
|
|
|
|
|
|
200
|
|
|
|
|
|
|
|
201
|
3
|
|
|
3
|
|
261
|
no warnings; |
|
3
|
|
|
|
|
6
|
|
|
3
|
|
|
|
|
1762
|
|
202
|
2
|
|
|
|
|
7
|
for (@$d){ |
203
|
5
|
|
|
|
|
20
|
$dump.= ' ['.join(':',@$_)."]\n"; |
204
|
|
|
|
|
|
|
} |
205
|
|
|
|
|
|
|
|
206
|
|
|
|
|
|
|
#require Data::Dumper; |
207
|
|
|
|
|
|
|
#my $dump = " # dump table '$tablename':\n".Data::Dumper::Dumper($d); |
208
|
|
|
|
|
|
|
|
209
|
2
|
|
|
|
|
11
|
return $dump; |
210
|
|
|
|
|
|
|
|
211
|
|
|
|
|
|
|
} |
212
|
|
|
|
|
|
|
|
213
|
|
|
|
|
|
|
|
214
|
|
|
|
|
|
|
|
215
|
|
|
|
|
|
|
|
216
|
|
|
|
|
|
|
|
217
|
|
|
|
|
|
|
|
218
|
|
|
|
|
|
|
sub selectcol { |
219
|
2
|
|
|
2
|
1
|
6
|
my ($dbh, $statement) = @_; |
220
|
2
|
50
|
|
|
|
9
|
defined $statement or confess('missing statement arg'); |
221
|
|
|
|
|
|
|
|
222
|
2
|
|
|
|
|
6
|
my $return = []; |
223
|
|
|
|
|
|
|
|
224
|
2
|
50
|
|
|
|
17
|
my $q = $dbh->prepare($statement) or confess("prepare [$statement] fails.. ".$dbh->errstr); |
225
|
2
|
|
|
|
|
1034
|
$q->execute; |
226
|
|
|
|
|
|
|
|
227
|
2
|
|
|
|
|
29
|
while(my $row = $q->fetchrow_arrayref ){ |
228
|
8
|
|
|
|
|
70
|
push @$return, $row->[0]; |
229
|
|
|
|
|
|
|
} |
230
|
|
|
|
|
|
|
|
231
|
2
|
|
|
|
|
34
|
return $return; |
232
|
|
|
|
|
|
|
} |
233
|
|
|
|
|
|
|
|
234
|
|
|
|
|
|
|
|
235
|
|
|
|
|
|
|
|
236
|
|
|
|
|
|
|
|
237
|
|
|
|
|
|
|
|
238
|
|
|
|
|
|
|
|
239
|
|
|
|
|
|
|
|
240
|
|
|
|
|
|
|
|
241
|
|
|
|
|
|
|
sub connect_mysql { |
242
|
0
|
|
|
0
|
1
|
0
|
my($dbname,$user,$pass,$host)= @_; |
243
|
0
|
0
|
|
|
|
0
|
defined $dbname or confess('missing dbname'); |
244
|
0
|
0
|
|
|
|
0
|
defined $user or confess('missing user'); |
245
|
0
|
0
|
|
|
|
0
|
defined $pass or confess('missing pass'); |
246
|
0
|
|
0
|
|
|
0
|
$host||='localhost'; |
247
|
|
|
|
|
|
|
|
248
|
0
|
|
|
|
|
0
|
debug("[host:$host,dbname:$dbname,user:$user,pass:$pass]\n"); |
249
|
0
|
|
|
|
|
0
|
require DBI; |
250
|
0
|
0
|
0
|
|
|
0
|
my $dbh = DBI->connect( "DBI:mysql:database=$dbname;host=$host",$user, $pass ) |
251
|
|
|
|
|
|
|
or carp(" -ERROR=[$DBI::errstr]\n -make sure mysqld is running\n -wrong credentials?[$dbname,$user,$host]") |
252
|
|
|
|
|
|
|
and return; |
253
|
0
|
|
|
|
|
0
|
return $dbh; |
254
|
|
|
|
|
|
|
} |
255
|
|
|
|
|
|
|
|
256
|
|
|
|
|
|
|
sub connect_sqlite { |
257
|
3
|
|
|
3
|
1
|
18
|
my $abs_db = shift; |
258
|
3
|
50
|
|
|
|
38
|
defined $abs_db or die; |
259
|
3
|
|
|
|
|
54
|
debug("abs db [$abs_db]"); |
260
|
|
|
|
|
|
|
|
261
|
3
|
|
|
|
|
296
|
require DBI; |
262
|
3
|
50
|
0
|
|
|
86
|
my $dbh = DBI->connect( "dbi:SQLite:$abs_db", '', '', ) |
263
|
|
|
|
|
|
|
or carp("$DBI::errstr, cant open sqlite connect. []") |
264
|
|
|
|
|
|
|
and return; |
265
|
3
|
|
|
|
|
48207
|
return $dbh; |
266
|
|
|
|
|
|
|
} |
267
|
|
|
|
|
|
|
|
268
|
|
|
|
|
|
|
|
269
|
|
|
|
|
|
|
1; |
270
|
|
|
|
|
|
|
|
271
|
|
|
|
|
|
|
__END__ |