line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package CPAN::Testers::Common::DBUtils; |
2
|
|
|
|
|
|
|
|
3
|
3
|
|
|
3
|
|
74143
|
use warnings; |
|
3
|
|
|
|
|
14
|
|
|
3
|
|
|
|
|
78
|
|
4
|
3
|
|
|
3
|
|
11
|
use strict; |
|
3
|
|
|
|
|
5
|
|
|
3
|
|
|
|
|
53
|
|
5
|
|
|
|
|
|
|
|
6
|
3
|
|
|
3
|
|
10
|
use vars qw($VERSION); |
|
3
|
|
|
|
|
4
|
|
|
3
|
|
|
|
|
179
|
|
7
|
|
|
|
|
|
|
$VERSION = '0.13'; |
8
|
|
|
|
|
|
|
|
9
|
|
|
|
|
|
|
=head1 NAME |
10
|
|
|
|
|
|
|
|
11
|
|
|
|
|
|
|
CPAN::Testers::Common::DBUtils - Basic Database Wrapper |
12
|
|
|
|
|
|
|
|
13
|
|
|
|
|
|
|
=head1 SYNOPSIS |
14
|
|
|
|
|
|
|
|
15
|
|
|
|
|
|
|
use CPAN::Testers::Common::DBUtils; |
16
|
|
|
|
|
|
|
|
17
|
|
|
|
|
|
|
my $dbx = CPAN::Testers::Common::DBUtils->new( |
18
|
|
|
|
|
|
|
driver => 'mysql', |
19
|
|
|
|
|
|
|
database => 'testdb'); |
20
|
|
|
|
|
|
|
|
21
|
|
|
|
|
|
|
sub errors { print STDERR "Error: $_[0], sql=$_[1]\n" } |
22
|
|
|
|
|
|
|
my $dbi = CPAN::Testers::Common::DBUtils->new( |
23
|
|
|
|
|
|
|
driver => 'CSV', |
24
|
|
|
|
|
|
|
dbfile => '/var/www/mysite/db |
25
|
|
|
|
|
|
|
errsub => \&errors); |
26
|
|
|
|
|
|
|
|
27
|
|
|
|
|
|
|
my @arr = $dbi->get_query('array',$sql); |
28
|
|
|
|
|
|
|
my @arr = $dbi->get_query('array',$sql,$id); |
29
|
|
|
|
|
|
|
my @arr = $dbi->get_query('hash', $sql,$id); |
30
|
|
|
|
|
|
|
|
31
|
|
|
|
|
|
|
my $id = $dbi->id_query($sql,$id,$name); |
32
|
|
|
|
|
|
|
$dbi->do_query($sql,$id); |
33
|
|
|
|
|
|
|
|
34
|
|
|
|
|
|
|
$dbi->do_rollback(); # where AutoCommit is disabled |
35
|
|
|
|
|
|
|
$dbi->do_commit(); # where AutoCommit is disabled |
36
|
|
|
|
|
|
|
|
37
|
|
|
|
|
|
|
# array iterator |
38
|
|
|
|
|
|
|
my $next = $dbi->iterator('array',$sql); |
39
|
|
|
|
|
|
|
my $row = $next->(); |
40
|
|
|
|
|
|
|
my $id = $row->[0]; |
41
|
|
|
|
|
|
|
|
42
|
|
|
|
|
|
|
# hash iterator |
43
|
|
|
|
|
|
|
my $next = $dbi->iterator('hash',$sql); |
44
|
|
|
|
|
|
|
my $row = $next->(); |
45
|
|
|
|
|
|
|
my $id = $row->{id}; |
46
|
|
|
|
|
|
|
|
47
|
|
|
|
|
|
|
$value = $dbi->quote($value); |
48
|
|
|
|
|
|
|
|
49
|
|
|
|
|
|
|
=head1 DESCRIPTION |
50
|
|
|
|
|
|
|
|
51
|
|
|
|
|
|
|
The DBUtils package is a wrapper around the database interface layer, providing |
52
|
|
|
|
|
|
|
a collection of methods to access and alter the data within the database, which |
53
|
|
|
|
|
|
|
handle any errors and abstracts these commonly called routines away from the |
54
|
|
|
|
|
|
|
calling program. |
55
|
|
|
|
|
|
|
|
56
|
|
|
|
|
|
|
Known supported drivers: |
57
|
|
|
|
|
|
|
|
58
|
|
|
|
|
|
|
MySQL (database) |
59
|
|
|
|
|
|
|
SQLite (database) |
60
|
|
|
|
|
|
|
CSV (dbfile) |
61
|
|
|
|
|
|
|
ODBC (driver) |
62
|
|
|
|
|
|
|
|
63
|
|
|
|
|
|
|
The keys in braces above, indicate how the name/location of the data store is |
64
|
|
|
|
|
|
|
passed to the wrapper and thus added to the connection string. |
65
|
|
|
|
|
|
|
|
66
|
|
|
|
|
|
|
=cut |
67
|
|
|
|
|
|
|
|
68
|
|
|
|
|
|
|
# ------------------------------------- |
69
|
|
|
|
|
|
|
# Library Modules |
70
|
|
|
|
|
|
|
|
71
|
3
|
|
|
3
|
|
14
|
use Carp; |
|
3
|
|
|
|
|
4
|
|
|
3
|
|
|
|
|
202
|
|
72
|
3
|
|
|
3
|
|
3403
|
use DBI; |
|
3
|
|
|
|
|
43181
|
|
|
3
|
|
|
|
|
170
|
|
73
|
|
|
|
|
|
|
|
74
|
3
|
|
|
3
|
|
23
|
use base qw(Class::Accessor::Fast); |
|
3
|
|
|
|
|
4
|
|
|
3
|
|
|
|
|
1509
|
|
75
|
|
|
|
|
|
|
|
76
|
|
|
|
|
|
|
# ------------------------------------- |
77
|
|
|
|
|
|
|
# The Public Interface Subs |
78
|
|
|
|
|
|
|
|
79
|
|
|
|
|
|
|
=head2 CONSTRUCTOR |
80
|
|
|
|
|
|
|
|
81
|
|
|
|
|
|
|
=over 4 |
82
|
|
|
|
|
|
|
|
83
|
|
|
|
|
|
|
=item new() |
84
|
|
|
|
|
|
|
|
85
|
|
|
|
|
|
|
The Constructor method can be called with an anonymous hash, |
86
|
|
|
|
|
|
|
listing the values to be used to connect to and handle the database. |
87
|
|
|
|
|
|
|
|
88
|
|
|
|
|
|
|
Values in the hash can be |
89
|
|
|
|
|
|
|
|
90
|
|
|
|
|
|
|
driver (*) |
91
|
|
|
|
|
|
|
database (+) |
92
|
|
|
|
|
|
|
dbfile (+) |
93
|
|
|
|
|
|
|
dbhost |
94
|
|
|
|
|
|
|
dbport |
95
|
|
|
|
|
|
|
dbuser |
96
|
|
|
|
|
|
|
dbpass |
97
|
|
|
|
|
|
|
errsub |
98
|
|
|
|
|
|
|
AutoCommit |
99
|
|
|
|
|
|
|
|
100
|
|
|
|
|
|
|
(*) These entries MUST exist in the hash. |
101
|
|
|
|
|
|
|
(+) At least ONE of these must exist in the hash, and depend upon the driver. |
102
|
|
|
|
|
|
|
|
103
|
|
|
|
|
|
|
Note that 'dbfile' is for use with a flat file database, such as DBD::CSV. |
104
|
|
|
|
|
|
|
|
105
|
|
|
|
|
|
|
By default the errors are handle via croak(), however if you pass a subroutine |
106
|
|
|
|
|
|
|
reference that will be called instead. Parameters passed to the error |
107
|
|
|
|
|
|
|
subroutine are the error string, the SQL string and the list of arguments given. |
108
|
|
|
|
|
|
|
|
109
|
|
|
|
|
|
|
AutoCommit is on by default, unless you explicitly pass 'AutoCommit => 0'. |
110
|
|
|
|
|
|
|
|
111
|
|
|
|
|
|
|
=back |
112
|
|
|
|
|
|
|
|
113
|
|
|
|
|
|
|
=cut |
114
|
|
|
|
|
|
|
|
115
|
|
|
|
|
|
|
sub new { |
116
|
5
|
|
|
5
|
1
|
247902
|
my ($self, %hash) = @_; |
117
|
|
|
|
|
|
|
|
118
|
|
|
|
|
|
|
# check we've got our mandatory fields |
119
|
5
|
100
|
|
|
|
218
|
croak("$self needs a driver!") unless($hash{driver}); |
120
|
|
|
|
|
|
|
croak("$self needs a database/file!") |
121
|
4
|
100
|
100
|
|
|
103
|
unless($hash{database} || $hash{dbfile}); |
122
|
|
|
|
|
|
|
|
123
|
|
|
|
|
|
|
# create an attributes hash |
124
|
|
|
|
|
|
|
my $dbv = { |
125
|
|
|
|
|
|
|
'driver' => $hash{driver}, |
126
|
|
|
|
|
|
|
'database' => $hash{database}, |
127
|
|
|
|
|
|
|
'dbfile' => $hash{dbfile}, |
128
|
|
|
|
|
|
|
'dbhost' => $hash{dbhost}, |
129
|
|
|
|
|
|
|
'dbport' => $hash{dbport}, |
130
|
|
|
|
|
|
|
'dbuser' => $hash{dbuser}, |
131
|
|
|
|
|
|
|
'dbpass' => $hash{dbpass}, |
132
|
|
|
|
|
|
|
'errsub' => $hash{errsub} || \&_errsub, |
133
|
3
|
100
|
50
|
|
|
66
|
'AutoCommit' => defined $hash{AutoCommit} ? $hash{AutoCommit} : 1, |
134
|
|
|
|
|
|
|
}; |
135
|
|
|
|
|
|
|
|
136
|
|
|
|
|
|
|
# create the object |
137
|
3
|
|
|
|
|
15
|
bless $dbv, $self; |
138
|
3
|
|
|
|
|
22
|
return $dbv; |
139
|
|
|
|
|
|
|
} |
140
|
|
|
|
|
|
|
|
141
|
|
|
|
|
|
|
=head2 PUBLIC INTERFACE METHODS |
142
|
|
|
|
|
|
|
|
143
|
|
|
|
|
|
|
=over 4 |
144
|
|
|
|
|
|
|
|
145
|
|
|
|
|
|
|
=item get_query(type,sql,) |
146
|
|
|
|
|
|
|
|
147
|
|
|
|
|
|
|
type - 'array' or 'hash' |
148
|
|
|
|
|
|
|
sql - SQL statement |
149
|
|
|
|
|
|
|
- optional additional values to be inserted into SQL placeholders |
150
|
|
|
|
|
|
|
|
151
|
|
|
|
|
|
|
This method performs a SELECT statement and returns an array of the returned |
152
|
|
|
|
|
|
|
rows. Each column within the row is then accessed as an array or hash as |
153
|
|
|
|
|
|
|
specified by 'type'. |
154
|
|
|
|
|
|
|
|
155
|
|
|
|
|
|
|
=cut |
156
|
|
|
|
|
|
|
|
157
|
|
|
|
|
|
|
sub get_query { |
158
|
14
|
|
|
14
|
1
|
3324
|
my ($dbv,$type,$sql,@args) = @_; |
159
|
14
|
100
|
|
|
|
41
|
return () unless($sql); |
160
|
|
|
|
|
|
|
|
161
|
|
|
|
|
|
|
# if the object doesn't contain a reference to a dbh |
162
|
|
|
|
|
|
|
# object then we need to connect to the database |
163
|
13
|
100
|
|
|
|
36
|
$dbv = _db_connect($dbv) if not $dbv->{dbh}; |
164
|
|
|
|
|
|
|
|
165
|
|
|
|
|
|
|
# prepare the sql statement for executing |
166
|
13
|
|
|
|
|
15
|
my $sth; |
167
|
13
|
|
|
|
|
15
|
eval { $sth = $dbv->{dbh}->prepare($sql) }; |
|
13
|
|
|
|
|
58
|
|
168
|
13
|
100
|
66
|
|
|
997
|
if($@ || !$sth) { |
169
|
1
|
|
|
|
|
35
|
$dbv->{errsub}->($dbv->{dbh}->errstr,$sql,@args); |
170
|
0
|
|
|
|
|
0
|
return (); |
171
|
|
|
|
|
|
|
} |
172
|
|
|
|
|
|
|
|
173
|
|
|
|
|
|
|
# execute the SQL using any values sent to the function |
174
|
|
|
|
|
|
|
# to be placed in the sql |
175
|
12
|
|
|
|
|
16
|
my $res; |
176
|
12
|
|
|
|
|
15
|
eval { $res = $sth->execute(@args); }; |
|
12
|
|
|
|
|
431
|
|
177
|
12
|
50
|
33
|
|
|
57
|
if($@ || !$res) { |
178
|
0
|
|
|
|
|
0
|
$dbv->{errsub}->($sth->errstr,$sql,@args); |
179
|
0
|
|
|
|
|
0
|
return (); |
180
|
|
|
|
|
|
|
} |
181
|
|
|
|
|
|
|
|
182
|
12
|
|
|
|
|
16
|
my @result; |
183
|
|
|
|
|
|
|
# grab the data in the right way |
184
|
12
|
100
|
|
|
|
28
|
if ( $type eq 'array' ) { |
185
|
11
|
|
|
|
|
102
|
while ( my $row = $sth->fetchrow_arrayref() ) { |
186
|
18
|
|
|
|
|
38
|
push @result, [@{$row}]; |
|
18
|
|
|
|
|
113
|
|
187
|
|
|
|
|
|
|
} |
188
|
|
|
|
|
|
|
} else { |
189
|
1
|
|
|
|
|
52
|
while ( my $row = $sth->fetchrow_hashref() ) { |
190
|
1
|
|
|
|
|
12
|
push @result, $row; |
191
|
|
|
|
|
|
|
} |
192
|
|
|
|
|
|
|
} |
193
|
|
|
|
|
|
|
|
194
|
|
|
|
|
|
|
# finish with our statement handle |
195
|
12
|
|
|
|
|
41
|
$sth->finish; |
196
|
|
|
|
|
|
|
# return the found datastructure |
197
|
12
|
|
|
|
|
143
|
return @result; |
198
|
|
|
|
|
|
|
} |
199
|
|
|
|
|
|
|
|
200
|
|
|
|
|
|
|
=item iterator(type,sql,) |
201
|
|
|
|
|
|
|
|
202
|
|
|
|
|
|
|
type - 'array' or 'hash' |
203
|
|
|
|
|
|
|
sql - SQL statement |
204
|
|
|
|
|
|
|
- optional additional values to be inserted into SQL placeholders |
205
|
|
|
|
|
|
|
|
206
|
|
|
|
|
|
|
This method is used to call a SELECT statement a row at a time, via a closure. |
207
|
|
|
|
|
|
|
Returns a subroutine reference which can then be used to obtain each row as a |
208
|
|
|
|
|
|
|
array reference or hash reference. Finally returns 'undef' when no more rows |
209
|
|
|
|
|
|
|
can be returned. |
210
|
|
|
|
|
|
|
|
211
|
|
|
|
|
|
|
=cut |
212
|
|
|
|
|
|
|
|
213
|
|
|
|
|
|
|
sub iterator { |
214
|
5
|
|
|
5
|
1
|
2063
|
my ($dbv,$type,$sql,@args) = @_; |
215
|
5
|
100
|
|
|
|
15
|
return unless($sql); |
216
|
|
|
|
|
|
|
|
217
|
|
|
|
|
|
|
# if the object doesn't contain a reference to a dbh |
218
|
|
|
|
|
|
|
# object then we need to connect to the database |
219
|
4
|
50
|
|
|
|
11
|
$dbv = _db_connect($dbv) if not $dbv->{dbh}; |
220
|
|
|
|
|
|
|
|
221
|
|
|
|
|
|
|
# prepare the sql statement for executing |
222
|
4
|
|
|
|
|
5
|
my $sth; |
223
|
4
|
|
|
|
|
6
|
eval { $sth = $dbv->{dbh}->prepare($sql); }; |
|
4
|
|
|
|
|
20
|
|
224
|
4
|
100
|
66
|
|
|
261
|
if($@ || !$sth) { |
225
|
1
|
|
|
|
|
9
|
$dbv->{errsub}->($dbv->{dbh}->errstr,$sql,@args); |
226
|
0
|
|
|
|
|
0
|
return; |
227
|
|
|
|
|
|
|
} |
228
|
|
|
|
|
|
|
|
229
|
|
|
|
|
|
|
# execute the SQL using any values sent to the function |
230
|
|
|
|
|
|
|
# to be placed in the sql |
231
|
3
|
|
|
|
|
6
|
my $res; |
232
|
3
|
|
|
|
|
7
|
eval { $res = $sth->execute(@args); }; |
|
3
|
|
|
|
|
93
|
|
233
|
3
|
50
|
33
|
|
|
24
|
if($@ || !$res) { |
234
|
0
|
|
|
|
|
0
|
$dbv->{errsub}->($sth->errstr,$sql,@args); |
235
|
0
|
|
|
|
|
0
|
return; |
236
|
|
|
|
|
|
|
} |
237
|
|
|
|
|
|
|
|
238
|
|
|
|
|
|
|
# grab the data in the right way |
239
|
3
|
100
|
|
|
|
10
|
if ( $type eq 'array' ) { |
240
|
|
|
|
|
|
|
return sub { |
241
|
9
|
100
|
|
9
|
|
3408
|
if ( my $row = $sth->fetchrow_arrayref() ) { return $row; } |
|
8
|
|
|
|
|
21
|
|
242
|
1
|
|
|
|
|
4
|
else { $sth->finish; return; } |
|
1
|
|
|
|
|
2
|
|
243
|
|
|
|
|
|
|
} |
244
|
2
|
|
|
|
|
24
|
} else { |
245
|
|
|
|
|
|
|
return sub { |
246
|
9
|
100
|
|
9
|
|
3600
|
if ( my $row = $sth->fetchrow_hashref() ) { return $row; } |
|
8
|
|
|
|
|
27
|
|
247
|
1
|
|
|
|
|
5
|
else { $sth->finish; return; } |
|
1
|
|
|
|
|
2
|
|
248
|
|
|
|
|
|
|
} |
249
|
1
|
|
|
|
|
16
|
} |
250
|
|
|
|
|
|
|
} |
251
|
|
|
|
|
|
|
|
252
|
|
|
|
|
|
|
=item do_query(sql,) |
253
|
|
|
|
|
|
|
|
254
|
|
|
|
|
|
|
sql - SQL statement |
255
|
|
|
|
|
|
|
- optional additional values to be inserted into SQL placeholders |
256
|
|
|
|
|
|
|
|
257
|
|
|
|
|
|
|
This method is used to perform an SQL action statement. |
258
|
|
|
|
|
|
|
|
259
|
|
|
|
|
|
|
=cut |
260
|
|
|
|
|
|
|
|
261
|
|
|
|
|
|
|
sub do_query { |
262
|
11
|
|
|
11
|
1
|
2945
|
my ($dbv,$sql,@args) = @_; |
263
|
11
|
|
|
|
|
49
|
$dbv->_do_query($sql,0,@args); |
264
|
|
|
|
|
|
|
} |
265
|
|
|
|
|
|
|
|
266
|
|
|
|
|
|
|
=item id_query(sql,) |
267
|
|
|
|
|
|
|
|
268
|
|
|
|
|
|
|
sql - SQL statement |
269
|
|
|
|
|
|
|
- optional additional values to be inserted into SQL placeholders |
270
|
|
|
|
|
|
|
|
271
|
|
|
|
|
|
|
This method is used to perform an SQL action statement. Commonly used when |
272
|
|
|
|
|
|
|
performing an INSERT statement, so that it returns the inserted record id. |
273
|
|
|
|
|
|
|
|
274
|
|
|
|
|
|
|
=cut |
275
|
|
|
|
|
|
|
|
276
|
|
|
|
|
|
|
sub id_query { |
277
|
4
|
|
|
4
|
1
|
1666
|
my ($dbv,$sql,@args) = @_; |
278
|
4
|
|
|
|
|
12
|
return $dbv->_do_query($sql,1,@args); |
279
|
|
|
|
|
|
|
} |
280
|
|
|
|
|
|
|
|
281
|
|
|
|
|
|
|
# _do_query(sql,idrequired,) |
282
|
|
|
|
|
|
|
# |
283
|
|
|
|
|
|
|
# sql - SQL statement |
284
|
|
|
|
|
|
|
# idrequired - true if an ID value is required on return |
285
|
|
|
|
|
|
|
# - optional additional values to be inserted into SQL placeholders |
286
|
|
|
|
|
|
|
# |
287
|
|
|
|
|
|
|
# This method is used to perform an SQL action statement. Commonly used when |
288
|
|
|
|
|
|
|
# performing an INSERT statement, so that it returns the inserted record id. |
289
|
|
|
|
|
|
|
|
290
|
|
|
|
|
|
|
sub _do_query { |
291
|
15
|
|
|
15
|
|
38
|
my ($dbv,$sql,$idrequired,@args) = @_; |
292
|
15
|
|
|
|
|
23
|
my $rowid; |
293
|
|
|
|
|
|
|
|
294
|
15
|
100
|
|
|
|
78
|
return unless($sql); |
295
|
|
|
|
|
|
|
|
296
|
|
|
|
|
|
|
# if the object doesn't contain a reference to a dbh |
297
|
|
|
|
|
|
|
# object then we need to connect to the database |
298
|
13
|
100
|
|
|
|
73
|
$dbv = _db_connect($dbv) if not $dbv->{dbh}; |
299
|
|
|
|
|
|
|
|
300
|
13
|
100
|
|
|
|
23
|
if($idrequired) { |
301
|
|
|
|
|
|
|
# prepare the sql statement for executing |
302
|
3
|
|
|
|
|
4
|
my $sth; |
303
|
3
|
|
|
|
|
5
|
eval { $sth = $dbv->{dbh}->prepare($sql); }; |
|
3
|
|
|
|
|
14
|
|
304
|
3
|
50
|
33
|
|
|
180
|
if($@ || !$sth) { |
305
|
0
|
|
|
|
|
0
|
$dbv->{errsub}->($dbv->{dbh}->errstr,$sql,@args); |
306
|
0
|
|
|
|
|
0
|
return; |
307
|
|
|
|
|
|
|
} |
308
|
|
|
|
|
|
|
|
309
|
|
|
|
|
|
|
# execute the SQL using any values sent to the function |
310
|
|
|
|
|
|
|
# to be placed in the sql |
311
|
3
|
|
|
|
|
5
|
my $res; |
312
|
3
|
|
|
|
|
6
|
eval { $res = $sth->execute(@args); }; |
|
3
|
|
|
|
|
487
|
|
313
|
3
|
50
|
33
|
|
|
28
|
if($@ || !$res) { |
314
|
0
|
|
|
|
|
0
|
$dbv->{errsub}->($sth->errstr,$sql,@args); |
315
|
0
|
|
|
|
|
0
|
return; |
316
|
|
|
|
|
|
|
} |
317
|
|
|
|
|
|
|
|
318
|
3
|
50
|
|
|
|
27
|
if($dbv->{driver} =~ /mysql/i) { |
|
|
50
|
|
|
|
|
|
|
|
50
|
|
|
|
|
|
319
|
0
|
|
|
|
|
0
|
$rowid = $dbv->{dbh}->{mysql_insertid}; |
320
|
|
|
|
|
|
|
} elsif($dbv->{driver} =~ /pg/i) { |
321
|
0
|
|
|
|
|
0
|
my ($table) = $sql =~ /INTO\s+(\S+)/; |
322
|
0
|
|
|
|
|
0
|
$rowid = $dbv->{dbh}->last_insert_id(undef,undef,$table,undef); |
323
|
|
|
|
|
|
|
} elsif($dbv->{driver} =~ /sqlite/i) { |
324
|
3
|
|
|
|
|
18
|
$sth = $dbv->{dbh}->prepare('SELECT last_insert_rowid()'); |
325
|
3
|
|
|
|
|
170
|
$res = $sth->execute(); |
326
|
3
|
|
|
|
|
5
|
my $row; |
327
|
3
|
50
|
|
|
|
30
|
$rowid = $row->[0] if( $row = $sth->fetchrow_arrayref() ); |
328
|
|
|
|
|
|
|
} else { |
329
|
0
|
|
|
|
|
0
|
my $row; |
330
|
0
|
0
|
|
|
|
0
|
$rowid = $row->[0] if( $row = $sth->fetchrow_arrayref() ); |
331
|
|
|
|
|
|
|
} |
332
|
|
|
|
|
|
|
|
333
|
|
|
|
|
|
|
} else { |
334
|
10
|
|
|
|
|
13
|
eval { $dbv->{dbh}->do($sql, undef, @args) }; |
|
10
|
|
|
|
|
53
|
|
335
|
10
|
100
|
|
|
|
67638
|
if ( $@ ) { |
336
|
1
|
|
|
|
|
10
|
$dbv->{errsub}->($dbv->{dbh}->errstr,$sql,@args); |
337
|
0
|
|
|
|
|
0
|
return -1; |
338
|
|
|
|
|
|
|
} |
339
|
|
|
|
|
|
|
|
340
|
9
|
|
|
|
|
27
|
$rowid = 1; # technically this should be the number of succesful rows |
341
|
|
|
|
|
|
|
} |
342
|
|
|
|
|
|
|
|
343
|
|
|
|
|
|
|
## Return the rowid we just used |
344
|
12
|
|
|
|
|
44
|
return $rowid; |
345
|
|
|
|
|
|
|
} |
346
|
|
|
|
|
|
|
|
347
|
|
|
|
|
|
|
=item repeat_query(sql,,[(), ... ()]) |
348
|
|
|
|
|
|
|
|
349
|
|
|
|
|
|
|
sql - SQL statement |
350
|
|
|
|
|
|
|
- values to be inserted into SQL placeholders |
351
|
|
|
|
|
|
|
- arguments to be inserted into placeholders |
352
|
|
|
|
|
|
|
|
353
|
|
|
|
|
|
|
This method is used to store an SQL action statement, together withe the |
354
|
|
|
|
|
|
|
associated arguments. Commonly used with statements where multiple arguments |
355
|
|
|
|
|
|
|
sets are applied to the same statement. |
356
|
|
|
|
|
|
|
|
357
|
|
|
|
|
|
|
=item repeat_queries() |
358
|
|
|
|
|
|
|
|
359
|
|
|
|
|
|
|
This method performs all stored SQL action statements. |
360
|
|
|
|
|
|
|
|
361
|
|
|
|
|
|
|
=item repeater(sql,) |
362
|
|
|
|
|
|
|
|
363
|
|
|
|
|
|
|
sql - SQL statement |
364
|
|
|
|
|
|
|
- list of values to be inserted into SQL placeholders |
365
|
|
|
|
|
|
|
|
366
|
|
|
|
|
|
|
This method performs an single SQL action statement, using all the associated |
367
|
|
|
|
|
|
|
arguments within the given list reference. |
368
|
|
|
|
|
|
|
|
369
|
|
|
|
|
|
|
=cut |
370
|
|
|
|
|
|
|
|
371
|
|
|
|
|
|
|
sub repeat_query { |
372
|
5
|
|
|
5
|
1
|
423
|
my ($dbv,$sql,@args) = @_; |
373
|
5
|
100
|
100
|
|
|
21
|
return unless($sql && @args); |
374
|
|
|
|
|
|
|
|
375
|
|
|
|
|
|
|
# if the object doesn't contain a reference to a dbh |
376
|
|
|
|
|
|
|
# object then we need to connect to the database |
377
|
3
|
50
|
|
|
|
8
|
$dbv = _db_connect($dbv) if not $dbv->{dbh}; |
378
|
|
|
|
|
|
|
|
379
|
3
|
|
|
|
|
5
|
push @{ $dbv->{repeat}{$sql} }, \@args; |
|
3
|
|
|
|
|
16
|
|
380
|
|
|
|
|
|
|
} |
381
|
|
|
|
|
|
|
|
382
|
|
|
|
|
|
|
sub repeat_queries { |
383
|
2
|
|
|
2
|
1
|
913
|
my $dbv = shift; |
384
|
2
|
100
|
66
|
|
|
16
|
return 0 unless($dbv && $dbv->{repeat}); |
385
|
|
|
|
|
|
|
|
386
|
1
|
|
|
|
|
2
|
my $rows = 0; |
387
|
1
|
|
|
|
|
2
|
for my $sql (keys %{ $dbv->{repeat} }) { |
|
1
|
|
|
|
|
7
|
|
388
|
1
|
|
|
|
|
8
|
$rows += $dbv->repeater($sql,$dbv->{repeat}{$sql}); |
389
|
|
|
|
|
|
|
} |
390
|
|
|
|
|
|
|
|
391
|
1
|
|
|
|
|
6
|
$dbv->{repeat} = undef; |
392
|
1
|
|
|
|
|
8
|
return $rows; |
393
|
|
|
|
|
|
|
} |
394
|
|
|
|
|
|
|
|
395
|
|
|
|
|
|
|
sub repeater { |
396
|
1
|
|
|
1
|
1
|
3
|
my ($dbv,$sql,$args) = @_; |
397
|
1
|
|
|
|
|
2
|
my $rows = 0; |
398
|
|
|
|
|
|
|
|
399
|
1
|
50
|
|
|
|
3
|
return $rows unless($sql); |
400
|
|
|
|
|
|
|
|
401
|
|
|
|
|
|
|
# if the object doesn't contain a reference to a dbh |
402
|
|
|
|
|
|
|
# object then we need to connect to the database |
403
|
1
|
50
|
|
|
|
3
|
$dbv = _db_connect($dbv) if not $dbv->{dbh}; |
404
|
|
|
|
|
|
|
|
405
|
|
|
|
|
|
|
# prepare the sql statement for executing |
406
|
1
|
|
|
|
|
3
|
my $sth; |
407
|
1
|
|
|
|
|
3
|
eval { $sth = $dbv->{dbh}->prepare($sql); }; |
|
1
|
|
|
|
|
6
|
|
408
|
1
|
50
|
33
|
|
|
60
|
if($@ || !$sth) { |
409
|
0
|
|
|
|
|
0
|
$dbv->{errsub}->($dbv->{dbh}->errstr,$sql,@{$args->[0]}); |
|
0
|
|
|
|
|
0
|
|
410
|
0
|
|
|
|
|
0
|
return $rows; |
411
|
|
|
|
|
|
|
} |
412
|
|
|
|
|
|
|
|
413
|
1
|
|
|
|
|
7
|
for my $arg (@$args) { |
414
|
|
|
|
|
|
|
# execute the SQL using any values sent to the function |
415
|
|
|
|
|
|
|
# to be placed in the sql |
416
|
3
|
|
|
|
|
6
|
my $res; |
417
|
3
|
|
|
|
|
4
|
eval { $res = $sth->execute(@$arg); }; |
|
3
|
|
|
|
|
27504
|
|
418
|
3
|
50
|
33
|
|
|
27
|
if($@ || !$res) { |
419
|
0
|
|
|
|
|
0
|
$dbv->{errsub}->($sth->errstr,$sql,@$args); |
420
|
0
|
|
|
|
|
0
|
next; |
421
|
|
|
|
|
|
|
} |
422
|
|
|
|
|
|
|
|
423
|
3
|
|
|
|
|
8
|
$rows++; |
424
|
|
|
|
|
|
|
} |
425
|
|
|
|
|
|
|
|
426
|
1
|
|
|
|
|
17
|
return $rows; |
427
|
|
|
|
|
|
|
} |
428
|
|
|
|
|
|
|
|
429
|
|
|
|
|
|
|
=item do_commit() |
430
|
|
|
|
|
|
|
|
431
|
|
|
|
|
|
|
Performs a commit on the transaction where AutoCommit is disabled. |
432
|
|
|
|
|
|
|
|
433
|
|
|
|
|
|
|
=cut |
434
|
|
|
|
|
|
|
|
435
|
|
|
|
|
|
|
sub do_commit { |
436
|
1
|
|
|
1
|
1
|
463
|
my $dbv = shift; |
437
|
1
|
50
|
|
|
|
9088
|
$dbv->{dbh}->commit if($dbv->{dbh}); |
438
|
|
|
|
|
|
|
} |
439
|
|
|
|
|
|
|
|
440
|
|
|
|
|
|
|
=item do_rollback() |
441
|
|
|
|
|
|
|
|
442
|
|
|
|
|
|
|
Performs a rollback on the transaction where AutoCommit is disabled. |
443
|
|
|
|
|
|
|
|
444
|
|
|
|
|
|
|
=cut |
445
|
|
|
|
|
|
|
|
446
|
|
|
|
|
|
|
sub do_rollback { |
447
|
1
|
|
|
1
|
1
|
502
|
my $dbv = shift; |
448
|
1
|
50
|
|
|
|
176
|
$dbv->{dbh}->rollback if($dbv->{dbh}); |
449
|
|
|
|
|
|
|
} |
450
|
|
|
|
|
|
|
|
451
|
|
|
|
|
|
|
=item quote(string) |
452
|
|
|
|
|
|
|
|
453
|
|
|
|
|
|
|
string - string to be quoted |
454
|
|
|
|
|
|
|
|
455
|
|
|
|
|
|
|
This method performs a DBI quote operation, which will quote a string |
456
|
|
|
|
|
|
|
according to the SQL rules. |
457
|
|
|
|
|
|
|
|
458
|
|
|
|
|
|
|
=cut |
459
|
|
|
|
|
|
|
|
460
|
|
|
|
|
|
|
sub quote { |
461
|
1
|
|
|
1
|
1
|
2122
|
my $dbv = shift; |
462
|
1
|
50
|
|
|
|
3
|
return unless($_[0]); |
463
|
|
|
|
|
|
|
|
464
|
|
|
|
|
|
|
# Cant quote with DBD::CSV |
465
|
1
|
50
|
|
|
|
7
|
return $_[0] if($dbv->{driver} =~ /csv/i); |
466
|
|
|
|
|
|
|
|
467
|
|
|
|
|
|
|
# if the object doesnt contain a reference to a dbh object |
468
|
|
|
|
|
|
|
# then we need to connect to the database |
469
|
1
|
50
|
|
|
|
4
|
$dbv = _db_connect($dbv) if not $dbv->{dbh}; |
470
|
|
|
|
|
|
|
|
471
|
1
|
|
|
|
|
26
|
$dbv->{dbh}->quote($_[0]); |
472
|
|
|
|
|
|
|
} |
473
|
|
|
|
|
|
|
|
474
|
|
|
|
|
|
|
# ------------------------------------- |
475
|
|
|
|
|
|
|
# The Accessors |
476
|
|
|
|
|
|
|
|
477
|
|
|
|
|
|
|
=item Accessor Methods |
478
|
|
|
|
|
|
|
|
479
|
|
|
|
|
|
|
The following accessor methods are available: |
480
|
|
|
|
|
|
|
|
481
|
|
|
|
|
|
|
=over 4 |
482
|
|
|
|
|
|
|
|
483
|
|
|
|
|
|
|
=item * driver |
484
|
|
|
|
|
|
|
|
485
|
|
|
|
|
|
|
=item * database |
486
|
|
|
|
|
|
|
|
487
|
|
|
|
|
|
|
=item * dbfile |
488
|
|
|
|
|
|
|
|
489
|
|
|
|
|
|
|
=item * dbhost |
490
|
|
|
|
|
|
|
|
491
|
|
|
|
|
|
|
=item * dbport |
492
|
|
|
|
|
|
|
|
493
|
|
|
|
|
|
|
=item * dbuser |
494
|
|
|
|
|
|
|
|
495
|
|
|
|
|
|
|
=item * dbpass |
496
|
|
|
|
|
|
|
|
497
|
|
|
|
|
|
|
=back |
498
|
|
|
|
|
|
|
|
499
|
|
|
|
|
|
|
All methods can be called to return the current value of the associated |
500
|
|
|
|
|
|
|
object variable. Note that these are only meant to be used as read-only |
501
|
|
|
|
|
|
|
methods. |
502
|
|
|
|
|
|
|
|
503
|
|
|
|
|
|
|
=cut |
504
|
|
|
|
|
|
|
|
505
|
|
|
|
|
|
|
__PACKAGE__->mk_accessors(qw(driver database dbfile dbhost dbport dbuser dbpass)); |
506
|
|
|
|
|
|
|
|
507
|
|
|
|
|
|
|
# ------------------------------------- |
508
|
|
|
|
|
|
|
# The Private Subs |
509
|
|
|
|
|
|
|
# These modules should not have to be called from outside this module |
510
|
|
|
|
|
|
|
|
511
|
|
|
|
|
|
|
sub _db_connect { |
512
|
2
|
|
|
2
|
|
6
|
my $dbv = shift; |
513
|
|
|
|
|
|
|
|
514
|
2
|
|
|
|
|
5
|
my $dsn = 'dbi:' . $dbv->{driver}; |
515
|
|
|
|
|
|
|
my %options = ( |
516
|
|
|
|
|
|
|
RaiseError => 1, |
517
|
|
|
|
|
|
|
AutoCommit => $dbv->{AutoCommit}, |
518
|
2
|
|
|
|
|
11
|
); |
519
|
|
|
|
|
|
|
|
520
|
2
|
50
|
|
|
|
29
|
if($dbv->{driver} =~ /ODBC/) { |
|
|
50
|
|
|
|
|
|
521
|
|
|
|
|
|
|
# all the info is in the Data Source repository |
522
|
|
|
|
|
|
|
|
523
|
|
|
|
|
|
|
} elsif($dbv->{driver} =~ /SQLite/i) { |
524
|
2
|
50
|
|
|
|
23
|
$dsn .= ':dbname=' . $dbv->{database} if $dbv->{database}; |
525
|
2
|
50
|
|
|
|
11
|
$dsn .= ';host=' . $dbv->{dbhost} if $dbv->{dbhost}; |
526
|
2
|
50
|
|
|
|
7
|
$dsn .= ';port=' . $dbv->{dbport} if $dbv->{dbport}; |
527
|
|
|
|
|
|
|
|
528
|
2
|
|
|
|
|
9
|
$options{sqlite_handle_binary_nulls} = 1; |
529
|
|
|
|
|
|
|
|
530
|
|
|
|
|
|
|
} else { |
531
|
0
|
0
|
|
|
|
0
|
$dsn .= ':f_dir=' . $dbv->{dbfile} if $dbv->{dbfile}; |
532
|
0
|
0
|
|
|
|
0
|
$dsn .= ':database=' . $dbv->{database} if $dbv->{database}; |
533
|
0
|
0
|
|
|
|
0
|
$dsn .= ';host=' . $dbv->{dbhost} if $dbv->{dbhost}; |
534
|
0
|
0
|
|
|
|
0
|
$dsn .= ';port=' . $dbv->{dbport} if $dbv->{dbport}; |
535
|
|
|
|
|
|
|
} |
536
|
|
|
|
|
|
|
|
537
|
2
|
|
|
|
|
5
|
eval { |
538
|
2
|
|
|
|
|
16
|
$dbv->{dbh} = DBI->connect($dsn, $dbv->{dbuser}, $dbv->{dbpass}, \%options); |
539
|
|
|
|
|
|
|
}; |
540
|
|
|
|
|
|
|
|
541
|
2
|
50
|
|
|
|
857
|
croak("Cannot connect to DB [$dsn]: $@") if($@); |
542
|
2
|
|
|
|
|
6
|
return $dbv; |
543
|
|
|
|
|
|
|
} |
544
|
|
|
|
|
|
|
|
545
|
|
|
|
|
|
|
sub DESTROY { |
546
|
4
|
|
|
4
|
|
1091
|
my $dbv = shift; |
547
|
|
|
|
|
|
|
# $dbv->{dbh}->commit if defined $dbv->{dbh}; |
548
|
4
|
100
|
|
|
|
424
|
$dbv->{dbh}->disconnect if defined $dbv->{dbh}; |
549
|
|
|
|
|
|
|
} |
550
|
|
|
|
|
|
|
|
551
|
|
|
|
|
|
|
sub _errsub { |
552
|
3
|
|
|
3
|
|
9
|
my ($err,$sql,@args) = @_; |
553
|
3
|
50
|
|
|
|
408
|
croak("err=$err, sql=[$sql], args[".join(",",map{$_ || ''} @args)."]"); |
|
1
|
|
|
|
|
94
|
|
554
|
|
|
|
|
|
|
} |
555
|
|
|
|
|
|
|
|
556
|
|
|
|
|
|
|
1; |
557
|
|
|
|
|
|
|
|
558
|
|
|
|
|
|
|
__END__ |