line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Gantry::Utils::DB; |
2
|
|
|
|
|
|
|
require Exporter; |
3
|
|
|
|
|
|
|
|
4
|
3
|
|
|
3
|
|
1264
|
use strict; |
|
3
|
|
|
|
|
9
|
|
|
3
|
|
|
|
|
108
|
|
5
|
3
|
|
|
3
|
|
17
|
use Carp qw( croak confess ); |
|
3
|
|
|
|
|
6
|
|
|
3
|
|
|
|
|
169
|
|
6
|
3
|
|
|
3
|
|
8024
|
use DBI; |
|
3
|
|
|
|
|
61643
|
|
|
3
|
|
|
|
|
230
|
|
7
|
3
|
|
|
3
|
|
37
|
use vars qw( @ISA @EXPORT ); |
|
3
|
|
|
|
|
5
|
|
|
3
|
|
|
|
|
5137
|
|
8
|
|
|
|
|
|
|
|
9
|
|
|
|
|
|
|
############################################################ |
10
|
|
|
|
|
|
|
# Variables # |
11
|
|
|
|
|
|
|
############################################################ |
12
|
|
|
|
|
|
|
@ISA = qw( Exporter ); |
13
|
|
|
|
|
|
|
@EXPORT = qw( db_commit |
14
|
|
|
|
|
|
|
db_connect |
15
|
|
|
|
|
|
|
db_disconnect |
16
|
|
|
|
|
|
|
db_finish |
17
|
|
|
|
|
|
|
db_lastseq |
18
|
|
|
|
|
|
|
db_next |
19
|
|
|
|
|
|
|
db_nextvals |
20
|
|
|
|
|
|
|
db_query |
21
|
|
|
|
|
|
|
db_rollback |
22
|
|
|
|
|
|
|
db_rowcount |
23
|
|
|
|
|
|
|
db_run ); |
24
|
|
|
|
|
|
|
|
25
|
|
|
|
|
|
|
############################################################ |
26
|
|
|
|
|
|
|
# Functions # |
27
|
|
|
|
|
|
|
############################################################ |
28
|
|
|
|
|
|
|
############################################################ |
29
|
|
|
|
|
|
|
# Functions # |
30
|
|
|
|
|
|
|
############################################################ |
31
|
|
|
|
|
|
|
sub new { |
32
|
0
|
|
|
0
|
1
|
|
my ( $class, $dbh ) = @_; |
33
|
|
|
|
|
|
|
|
34
|
0
|
|
|
|
|
|
my $self = { }; |
35
|
0
|
|
|
|
|
|
bless( $self, $class ); |
36
|
|
|
|
|
|
|
|
37
|
|
|
|
|
|
|
# populate self with data from site |
38
|
0
|
|
|
|
|
|
return( $self ); |
39
|
|
|
|
|
|
|
|
40
|
|
|
|
|
|
|
} # end new |
41
|
|
|
|
|
|
|
|
42
|
|
|
|
|
|
|
#------------------------------------------------- |
43
|
|
|
|
|
|
|
# db_commit( $dbh ) |
44
|
|
|
|
|
|
|
#------------------------------------------------- |
45
|
|
|
|
|
|
|
sub db_commit { |
46
|
0
|
|
|
0
|
1
|
|
my $handle = shift; |
47
|
|
|
|
|
|
|
|
48
|
0
|
0
|
|
|
|
|
$handle->commit if ( $handle->{AutoCommit} == 0 ); |
49
|
|
|
|
|
|
|
|
50
|
0
|
|
|
|
|
|
return(); |
51
|
|
|
|
|
|
|
} # END db_commit |
52
|
|
|
|
|
|
|
|
53
|
|
|
|
|
|
|
#------------------------------------------------- |
54
|
|
|
|
|
|
|
# db_connect() |
55
|
|
|
|
|
|
|
#------------------------------------------------- |
56
|
|
|
|
|
|
|
sub db_connect { |
57
|
0
|
|
|
0
|
1
|
|
my ( $db_type, $user, $pass, $server, $db, $commit ); |
58
|
|
|
|
|
|
|
|
59
|
|
|
|
|
|
|
# Setup the variables, sanity check it too. |
60
|
0
|
0
|
|
|
|
|
if ( $_[0] =~ /^(dbtype|usr|pwd|db|srv|commit)$/ ) { # It's a hash |
61
|
0
|
|
|
|
|
|
my %settings = @_; |
62
|
|
|
|
|
|
|
|
63
|
0
|
|
0
|
|
|
|
$db_type = $settings{dbtype} || ''; |
64
|
0
|
|
0
|
|
|
|
$user = $settings{usr} || ''; |
65
|
0
|
|
0
|
|
|
|
$pass = $settings{pwd} || ''; |
66
|
0
|
|
0
|
|
|
|
$db = $settings{db} || ''; |
67
|
0
|
|
0
|
|
|
|
$server = $settings{srv} || ''; |
68
|
0
|
|
0
|
|
|
|
$commit = $settings{commit} || ''; |
69
|
|
|
|
|
|
|
|
70
|
0
|
0
|
|
|
|
|
$commit = ( $commit =~ /off/i ) ? 0 : 1 ; |
71
|
|
|
|
|
|
|
} |
72
|
|
|
|
|
|
|
else { |
73
|
0
|
|
|
|
|
|
( $db_type, $user, $pass, $server, $db, $commit ) = @_; |
74
|
|
|
|
|
|
|
|
75
|
0
|
0
|
|
|
|
|
$db_type = '' if ( ! defined ( $db_type ) ); |
76
|
0
|
0
|
|
|
|
|
$user = '' if ( ! defined ( $user ) ); |
77
|
0
|
0
|
|
|
|
|
$pass = '' if ( ! defined ( $pass ) ); |
78
|
0
|
0
|
|
|
|
|
$server = '' if ( ! defined ( $server ) ); |
79
|
0
|
0
|
|
|
|
|
$db = '' if ( ! defined ( $db ) ); |
80
|
0
|
0
|
|
|
|
|
$commit = '1' if ( ! defined ( $commit ) ); |
81
|
0
|
0
|
|
|
|
|
$commit = ( $commit =~ /off/i ) ? 0 : 1 ; |
82
|
|
|
|
|
|
|
} |
83
|
|
|
|
|
|
|
|
84
|
0
|
0
|
|
|
|
|
croak 'No Database Type defined' if ( length( $db_type ) < 1 ); |
85
|
|
|
|
|
|
|
|
86
|
0
|
|
|
|
|
|
my $dsn = "dbi:$db_type"; |
87
|
|
|
|
|
|
|
|
88
|
0
|
0
|
|
|
|
|
$dsn .= ":dbname=$db" if $db; |
89
|
0
|
0
|
|
|
|
|
$dsn .= ( $server eq '' ) ? '' : ";host=$server"; |
90
|
|
|
|
|
|
|
|
91
|
0
|
|
|
|
|
|
warn( $dsn ); |
92
|
|
|
|
|
|
|
|
93
|
0
|
0
|
|
|
|
|
my $dbh = DBI->connect( $dsn, "$user", "$pass", |
94
|
|
|
|
|
|
|
{ RaiseError => 0, |
95
|
|
|
|
|
|
|
PrintError => 1, |
96
|
|
|
|
|
|
|
AutoCommit => $commit } ) or |
97
|
|
|
|
|
|
|
confess( $DBI::errstr ); |
98
|
|
|
|
|
|
|
|
99
|
0
|
|
|
|
|
|
return( $dbh ); |
100
|
|
|
|
|
|
|
} # END db_connect |
101
|
|
|
|
|
|
|
|
102
|
|
|
|
|
|
|
#------------------------------------------------- |
103
|
|
|
|
|
|
|
# db_disconnect( $dbh ) |
104
|
|
|
|
|
|
|
#------------------------------------------------- |
105
|
|
|
|
|
|
|
sub db_disconnect { |
106
|
0
|
|
|
0
|
1
|
|
my $handle = shift; |
107
|
|
|
|
|
|
|
|
108
|
0
|
0
|
|
|
|
|
$handle->rollback if ( $handle->{AutoCommit} == 0 ); |
109
|
|
|
|
|
|
|
|
110
|
0
|
|
|
|
|
|
$handle->disconnect; |
111
|
|
|
|
|
|
|
|
112
|
0
|
|
|
|
|
|
return; |
113
|
|
|
|
|
|
|
} # END db_disconnect |
114
|
|
|
|
|
|
|
|
115
|
|
|
|
|
|
|
#------------------------------------------------- |
116
|
|
|
|
|
|
|
# db_finish( $sth ) |
117
|
|
|
|
|
|
|
#------------------------------------------------- |
118
|
|
|
|
|
|
|
sub db_finish { |
119
|
0
|
|
|
0
|
1
|
|
my $handle = shift; |
120
|
|
|
|
|
|
|
|
121
|
0
|
|
|
|
|
|
$handle->finish; |
122
|
|
|
|
|
|
|
|
123
|
0
|
|
|
|
|
|
return(); |
124
|
|
|
|
|
|
|
} # END db_finish |
125
|
|
|
|
|
|
|
|
126
|
|
|
|
|
|
|
#------------------------------------------------- |
127
|
|
|
|
|
|
|
# db_lastseq( $dbh, $sequence_name ) |
128
|
|
|
|
|
|
|
#------------------------------------------------- |
129
|
|
|
|
|
|
|
sub db_lastseq { |
130
|
0
|
|
|
0
|
1
|
|
my ( $handle, $seq ) = @_; |
131
|
|
|
|
|
|
|
|
132
|
0
|
0
|
|
|
|
|
croak "No database handle for db_lastseq: $!\n" unless ( defined $handle ); |
133
|
|
|
|
|
|
|
|
134
|
0
|
0
|
|
|
|
|
if ( ! defined $seq ) { |
135
|
0
|
0
|
|
|
|
|
$handle->rollback if ( $handle->{AutoCommit} ); |
136
|
0
|
|
|
|
|
|
croak "No sequence for db_lastseq: $!\n"; |
137
|
|
|
|
|
|
|
} |
138
|
|
|
|
|
|
|
|
139
|
0
|
|
|
|
|
|
my $sth = db_query ( $handle, "db_lastseq getting last value", |
140
|
|
|
|
|
|
|
"SELECT last_value FROM $seq;" ); |
141
|
|
|
|
|
|
|
|
142
|
0
|
|
|
|
|
|
my ( $last_value ) = db_next ( $sth ); |
143
|
|
|
|
|
|
|
|
144
|
0
|
|
|
|
|
|
db_finish ( $sth ); |
145
|
|
|
|
|
|
|
|
146
|
0
|
|
|
|
|
|
return ( $last_value ); |
147
|
|
|
|
|
|
|
|
148
|
|
|
|
|
|
|
} # END db_lastseq |
149
|
|
|
|
|
|
|
|
150
|
|
|
|
|
|
|
#------------------------------------------------- |
151
|
|
|
|
|
|
|
# db_next( $sth ) |
152
|
|
|
|
|
|
|
#------------------------------------------------- |
153
|
|
|
|
|
|
|
sub db_next { |
154
|
0
|
|
|
0
|
1
|
|
my $handle = shift; |
155
|
|
|
|
|
|
|
|
156
|
0
|
0
|
|
|
|
|
croak "Error: db_next() not given a handle, $!\n" if ( ! $handle ); |
157
|
|
|
|
|
|
|
|
158
|
0
|
|
|
|
|
|
return( $handle->fetchrow ); |
159
|
|
|
|
|
|
|
} # END db_next |
160
|
|
|
|
|
|
|
|
161
|
|
|
|
|
|
|
#------------------------------------------------- |
162
|
|
|
|
|
|
|
# db_nextvals( $sth ) |
163
|
|
|
|
|
|
|
#------------------------------------------------- |
164
|
|
|
|
|
|
|
sub db_nextvals { |
165
|
0
|
|
|
0
|
1
|
|
my $handle = shift; |
166
|
|
|
|
|
|
|
|
167
|
0
|
0
|
|
|
|
|
if( ! $handle ) { |
168
|
0
|
|
|
|
|
|
croak "Query error db_nextvals() not given a statement: $!\n"; |
169
|
|
|
|
|
|
|
} |
170
|
|
|
|
|
|
|
|
171
|
0
|
|
|
|
|
|
return( $handle->fetchrow_hashref ); |
172
|
|
|
|
|
|
|
|
173
|
|
|
|
|
|
|
} # END db_nextvals |
174
|
|
|
|
|
|
|
|
175
|
|
|
|
|
|
|
#------------------------------------------------- |
176
|
|
|
|
|
|
|
# db_query( $dbh, $description, @query ) |
177
|
|
|
|
|
|
|
#------------------------------------------------- |
178
|
|
|
|
|
|
|
sub db_query { |
179
|
0
|
|
|
0
|
1
|
|
my ( $handle, $description ) = ( shift, shift ); |
180
|
0
|
|
|
|
|
|
my ( $query, $params ); |
181
|
0
|
|
|
|
|
|
my ( $sql, $sth ); |
182
|
|
|
|
|
|
|
|
183
|
0
|
0
|
|
|
|
|
if ( ! defined ( $handle ) ) { |
184
|
0
|
|
|
|
|
|
croak "Error $description: db_query not given a connection: $!\n"; |
185
|
|
|
|
|
|
|
} |
186
|
|
|
|
|
|
|
|
187
|
|
|
|
|
|
|
# Determine which version of the function is being called. |
188
|
0
|
0
|
|
|
|
|
if ( ref( $_[0] ) eq 'ARRAY' ) { |
189
|
0
|
|
|
|
|
|
( $query, $params ) = @_; |
190
|
|
|
|
|
|
|
|
191
|
0
|
|
|
|
|
|
$sql = join ( "\n", @$query ); |
192
|
|
|
|
|
|
|
} |
193
|
|
|
|
|
|
|
else { |
194
|
|
|
|
|
|
|
# Warn users not to call db_query this way. |
195
|
0
|
|
|
|
|
|
warn( |
196
|
|
|
|
|
|
|
'db_query called without bound parameters is unsafe. ' . |
197
|
|
|
|
|
|
|
'Please update your code to use bound parameters.' |
198
|
|
|
|
|
|
|
); |
199
|
|
|
|
|
|
|
|
200
|
0
|
|
|
|
|
|
$sql = join ( "\n", @_ ); |
201
|
|
|
|
|
|
|
} |
202
|
|
|
|
|
|
|
|
203
|
0
|
0
|
|
|
|
|
if ( length ( $sql ) == 0 ) { |
204
|
0
|
0
|
|
|
|
|
$handle->rollback if ( $handle->{AutoCommit} == 0 ); |
205
|
0
|
|
|
|
|
|
croak "Error $description: db_query not given any SQL: $!\n"; |
206
|
|
|
|
|
|
|
} |
207
|
|
|
|
|
|
|
|
208
|
0
|
|
|
|
|
|
$sth = $handle->prepare( $sql ); |
209
|
|
|
|
|
|
|
$sth->execute( @$params ) or do |
210
|
0
|
0
|
|
|
|
|
{ |
211
|
0
|
0
|
|
|
|
|
$handle->rollback if ( $handle->{AutoCommit} == 0 ); |
212
|
0
|
|
|
|
|
|
croak "SQL Query Error ( $description ): $sql\n"; |
213
|
|
|
|
|
|
|
}; |
214
|
|
|
|
|
|
|
|
215
|
0
|
|
|
|
|
|
return $sth; |
216
|
|
|
|
|
|
|
} # END db_query |
217
|
|
|
|
|
|
|
|
218
|
|
|
|
|
|
|
#------------------------------------------------- |
219
|
|
|
|
|
|
|
# db_rollback( $sth ) |
220
|
|
|
|
|
|
|
#------------------------------------------------- |
221
|
|
|
|
|
|
|
sub db_rollback { |
222
|
0
|
|
|
0
|
1
|
|
my $handle = shift; |
223
|
|
|
|
|
|
|
|
224
|
0
|
|
|
|
|
|
$handle->rollback; |
225
|
|
|
|
|
|
|
|
226
|
0
|
|
|
|
|
|
return; |
227
|
|
|
|
|
|
|
} # END db_rollback |
228
|
|
|
|
|
|
|
|
229
|
|
|
|
|
|
|
#------------------------------------------------- |
230
|
|
|
|
|
|
|
# db_rowcount( $sth ) |
231
|
|
|
|
|
|
|
#------------------------------------------------- |
232
|
|
|
|
|
|
|
sub db_rowcount { |
233
|
0
|
|
|
0
|
1
|
|
my $handle = shift; |
234
|
|
|
|
|
|
|
|
235
|
0
|
|
|
|
|
|
return( $handle->rows ); |
236
|
|
|
|
|
|
|
} # END db_rowcount |
237
|
|
|
|
|
|
|
|
238
|
|
|
|
|
|
|
#------------------------------------------------- |
239
|
|
|
|
|
|
|
# db_run( $dbh, $description, @sql ) |
240
|
|
|
|
|
|
|
#------------------------------------------------- |
241
|
|
|
|
|
|
|
sub db_run { |
242
|
0
|
|
|
0
|
1
|
|
my ( $handle, $description, @sql ) = @_; |
243
|
|
|
|
|
|
|
|
244
|
0
|
0
|
|
|
|
|
if ( ! defined ( $handle ) ) { |
245
|
0
|
|
|
|
|
|
croak "Error $description: db_run() not given a connection: $!\n"; |
246
|
|
|
|
|
|
|
} |
247
|
|
|
|
|
|
|
|
248
|
0
|
0
|
|
|
|
|
if ( length ( @sql ) == 0 ) { |
249
|
0
|
0
|
|
|
|
|
$handle->rollback if ( $handle->{AutoCommit} == 0 ); |
250
|
0
|
|
|
|
|
|
croak "Error $description: db_run() was not given any SQL: $!\n"; |
251
|
|
|
|
|
|
|
} |
252
|
|
|
|
|
|
|
|
253
|
0
|
|
|
|
|
|
my $command = join ( "\n", @sql ); |
254
|
|
|
|
|
|
|
|
255
|
|
|
|
|
|
|
$handle->do( $command ) or do |
256
|
0
|
0
|
|
|
|
|
{ |
257
|
0
|
0
|
|
|
|
|
$handle->rollback if ( $handle->{AutoCommit} == 0 ); |
258
|
0
|
|
|
|
|
|
croak "SQL Query Error ($description): $command\n". |
259
|
|
|
|
|
|
|
$handle->errstr. "\n"; |
260
|
|
|
|
|
|
|
}; |
261
|
|
|
|
|
|
|
|
262
|
0
|
|
|
|
|
|
return; |
263
|
|
|
|
|
|
|
} # END db_run |
264
|
|
|
|
|
|
|
|
265
|
|
|
|
|
|
|
# EOF |
266
|
|
|
|
|
|
|
1; |
267
|
|
|
|
|
|
|
|
268
|
|
|
|
|
|
|
__END__ |