line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
#!/usr/bin/perl |
2
|
|
|
|
|
|
|
|
3
|
|
|
|
|
|
|
package Goo::LiteDatabase; |
4
|
|
|
|
|
|
|
|
5
|
|
|
|
|
|
|
############################################################################### |
6
|
|
|
|
|
|
|
# Nigel Hamilton |
7
|
|
|
|
|
|
|
# |
8
|
|
|
|
|
|
|
# Copyright Nigel Hamilton 2004 |
9
|
|
|
|
|
|
|
# All Rights Reserved |
10
|
|
|
|
|
|
|
# |
11
|
|
|
|
|
|
|
# Author: Nigel Hamilton |
12
|
|
|
|
|
|
|
# Filename: Goo::LiteDatabase.pm |
13
|
|
|
|
|
|
|
# Description: Drive an SQLite database |
14
|
|
|
|
|
|
|
# |
15
|
|
|
|
|
|
|
# Date Change |
16
|
|
|
|
|
|
|
# ---------------------------------------------------------------------------- |
17
|
|
|
|
|
|
|
# 18/12/2004 Version 1 |
18
|
|
|
|
|
|
|
# 19/08/2005 Added method: createDatabase |
19
|
|
|
|
|
|
|
# 18/10/2005 Added method: getPrimaryKey |
20
|
|
|
|
|
|
|
# 18/10/2005 Created test file: GooDatabaseTest.tpm |
21
|
|
|
|
|
|
|
# 18/10/2005 Added method: getTableColumns |
22
|
|
|
|
|
|
|
# |
23
|
|
|
|
|
|
|
############################################################################## |
24
|
|
|
|
|
|
|
|
25
|
1
|
|
|
1
|
|
6
|
use strict; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
29
|
|
26
|
|
|
|
|
|
|
|
27
|
1
|
|
|
1
|
|
5
|
use DBI; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
35
|
|
28
|
1
|
|
|
1
|
|
5
|
use Data::Dumper; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
41
|
|
29
|
1
|
|
|
1
|
|
5
|
use Goo::Environment; |
|
1
|
|
|
|
|
1
|
|
|
1
|
|
|
|
|
17
|
|
30
|
1
|
|
|
1
|
|
5
|
use Goo::TrailManager; |
|
1
|
|
|
|
|
1
|
|
|
1
|
|
|
|
|
1321
|
|
31
|
|
|
|
|
|
|
|
32
|
|
|
|
|
|
|
# global database handle - set by BEGIN block |
33
|
|
|
|
|
|
|
our $dbh; |
34
|
|
|
|
|
|
|
|
35
|
|
|
|
|
|
|
$dbh->{PrintError} = 0; # enable error checking via warn |
36
|
|
|
|
|
|
|
$dbh->{RaiseError} = 0; # enable error checking via die |
37
|
|
|
|
|
|
|
|
38
|
|
|
|
|
|
|
|
39
|
|
|
|
|
|
|
############################################################################### |
40
|
|
|
|
|
|
|
# |
41
|
|
|
|
|
|
|
# get_connection - open a connection to the default database |
42
|
|
|
|
|
|
|
# |
43
|
|
|
|
|
|
|
############################################################################### |
44
|
|
|
|
|
|
|
|
45
|
|
|
|
|
|
|
sub get_connection { |
46
|
|
|
|
|
|
|
|
47
|
0
|
|
|
0
|
1
|
|
my ($database_location) = @_; |
48
|
|
|
|
|
|
|
|
49
|
|
|
|
|
|
|
# store the handle in an our variable (see above) |
50
|
0
|
0
|
|
|
|
|
$dbh = DBI->connect("dbi:SQLite:dbname=$database_location", '', '') |
51
|
|
|
|
|
|
|
or handle_error('SQLite connect failed ', caller()); |
52
|
|
|
|
|
|
|
|
53
|
|
|
|
|
|
|
} |
54
|
|
|
|
|
|
|
|
55
|
|
|
|
|
|
|
|
56
|
|
|
|
|
|
|
############################################################################### |
57
|
|
|
|
|
|
|
# |
58
|
|
|
|
|
|
|
# do_sql - execute some sql |
59
|
|
|
|
|
|
|
# |
60
|
|
|
|
|
|
|
############################################################################### |
61
|
|
|
|
|
|
|
|
62
|
|
|
|
|
|
|
sub do_sql { |
63
|
|
|
|
|
|
|
|
64
|
0
|
|
|
0
|
1
|
|
my ($querystring, $testing) = @_; |
65
|
|
|
|
|
|
|
|
66
|
0
|
|
|
|
|
|
execute_sql($querystring, $testing); |
67
|
|
|
|
|
|
|
|
68
|
|
|
|
|
|
|
} |
69
|
|
|
|
|
|
|
|
70
|
|
|
|
|
|
|
|
71
|
|
|
|
|
|
|
############################################################################### |
72
|
|
|
|
|
|
|
# |
73
|
|
|
|
|
|
|
# do_query - execute sql and return the result all in one |
74
|
|
|
|
|
|
|
# |
75
|
|
|
|
|
|
|
############################################################################### |
76
|
|
|
|
|
|
|
|
77
|
|
|
|
|
|
|
sub do_query { |
78
|
|
|
|
|
|
|
|
79
|
0
|
|
|
0
|
1
|
|
my ($querystring) = @_; |
80
|
|
|
|
|
|
|
|
81
|
0
|
|
|
|
|
|
my $query = execute_sql($querystring); |
82
|
|
|
|
|
|
|
|
83
|
0
|
|
|
|
|
|
return get_result_hash($query); |
84
|
|
|
|
|
|
|
|
85
|
|
|
|
|
|
|
} |
86
|
|
|
|
|
|
|
|
87
|
|
|
|
|
|
|
|
88
|
|
|
|
|
|
|
############################################################################### |
89
|
|
|
|
|
|
|
# |
90
|
|
|
|
|
|
|
# generate_numeric_sqlin_clause - return an sql 'in' clause with numeric values |
91
|
|
|
|
|
|
|
# |
92
|
|
|
|
|
|
|
############################################################################### |
93
|
|
|
|
|
|
|
|
94
|
|
|
|
|
|
|
sub generate_numeric_sqlin_clause { |
95
|
|
|
|
|
|
|
|
96
|
0
|
|
|
0
|
1
|
|
my (@values) = @_; |
97
|
|
|
|
|
|
|
|
98
|
0
|
|
|
|
|
|
my $inclause = join(",", @values); |
99
|
|
|
|
|
|
|
|
100
|
0
|
|
|
|
|
|
return "(" . $inclause . ")"; |
101
|
|
|
|
|
|
|
|
102
|
|
|
|
|
|
|
} |
103
|
|
|
|
|
|
|
|
104
|
|
|
|
|
|
|
|
105
|
|
|
|
|
|
|
############################################################################### |
106
|
|
|
|
|
|
|
# |
107
|
|
|
|
|
|
|
# generate_string_sqlin_clause - return an sql 'in' clause with string values |
108
|
|
|
|
|
|
|
# |
109
|
|
|
|
|
|
|
############################################################################### |
110
|
|
|
|
|
|
|
|
111
|
|
|
|
|
|
|
sub generate_string_sqlin_clause { |
112
|
|
|
|
|
|
|
|
113
|
0
|
|
|
0
|
1
|
|
my (@values) = @_; |
114
|
|
|
|
|
|
|
|
115
|
0
|
|
|
|
|
|
my $inclause = join("','", @values); |
116
|
|
|
|
|
|
|
|
117
|
0
|
|
|
|
|
|
return "('" . $inclause . "')"; |
118
|
|
|
|
|
|
|
|
119
|
|
|
|
|
|
|
} |
120
|
|
|
|
|
|
|
|
121
|
|
|
|
|
|
|
|
122
|
|
|
|
|
|
|
############################################################################### |
123
|
|
|
|
|
|
|
# |
124
|
|
|
|
|
|
|
# get_number_of_rows - return the number of rows for this statement handle |
125
|
|
|
|
|
|
|
# |
126
|
|
|
|
|
|
|
############################################################################### |
127
|
|
|
|
|
|
|
|
128
|
|
|
|
|
|
|
sub get_number_of_rows { |
129
|
|
|
|
|
|
|
|
130
|
0
|
|
|
0
|
1
|
|
my ($sth) = @_; |
131
|
0
|
|
|
|
|
|
return $sth->rows(); |
132
|
|
|
|
|
|
|
|
133
|
|
|
|
|
|
|
} |
134
|
|
|
|
|
|
|
|
135
|
|
|
|
|
|
|
|
136
|
|
|
|
|
|
|
############################################################################### |
137
|
|
|
|
|
|
|
# |
138
|
|
|
|
|
|
|
# get_next_row - alias for get_result_hash |
139
|
|
|
|
|
|
|
# |
140
|
|
|
|
|
|
|
############################################################################### |
141
|
|
|
|
|
|
|
|
142
|
|
|
|
|
|
|
sub get_next_row { |
143
|
|
|
|
|
|
|
|
144
|
0
|
|
|
0
|
1
|
|
my ($sth) = @_; |
145
|
|
|
|
|
|
|
|
146
|
0
|
|
|
|
|
|
return $sth->fetchrow_hashref(); |
147
|
|
|
|
|
|
|
} |
148
|
|
|
|
|
|
|
|
149
|
|
|
|
|
|
|
|
150
|
|
|
|
|
|
|
############################################################################### |
151
|
|
|
|
|
|
|
# |
152
|
|
|
|
|
|
|
# get_result_hash - return a hash for this result |
153
|
|
|
|
|
|
|
# |
154
|
|
|
|
|
|
|
############################################################################### |
155
|
|
|
|
|
|
|
|
156
|
|
|
|
|
|
|
sub get_result_hash { |
157
|
|
|
|
|
|
|
|
158
|
0
|
|
|
0
|
1
|
|
my ($sth) = @_; |
159
|
|
|
|
|
|
|
|
160
|
0
|
|
|
|
|
|
return $sth->fetchrow_hashref(); |
161
|
|
|
|
|
|
|
} |
162
|
|
|
|
|
|
|
|
163
|
|
|
|
|
|
|
|
164
|
|
|
|
|
|
|
############################################################################### |
165
|
|
|
|
|
|
|
# |
166
|
|
|
|
|
|
|
# bind_param - bind a parameter to a value |
167
|
|
|
|
|
|
|
# |
168
|
|
|
|
|
|
|
############################################################################### |
169
|
|
|
|
|
|
|
|
170
|
|
|
|
|
|
|
sub bind_param { |
171
|
|
|
|
|
|
|
|
172
|
0
|
|
|
0
|
1
|
|
my ($sth, $param, $value) = @_; |
173
|
|
|
|
|
|
|
|
174
|
0
|
0
|
|
|
|
|
$sth->bind_param($param, $value) || |
175
|
|
|
|
|
|
|
handle_error("failed to bind parameter: $param = $value in $sth->{statement}", caller()); |
176
|
|
|
|
|
|
|
|
177
|
|
|
|
|
|
|
} |
178
|
|
|
|
|
|
|
|
179
|
|
|
|
|
|
|
|
180
|
|
|
|
|
|
|
############################################################################### |
181
|
|
|
|
|
|
|
# |
182
|
|
|
|
|
|
|
# prepare_sql(String s) - take a string and prepare the SQL |
183
|
|
|
|
|
|
|
# |
184
|
|
|
|
|
|
|
############################################################################### |
185
|
|
|
|
|
|
|
|
186
|
|
|
|
|
|
|
sub prepare_sql { |
187
|
|
|
|
|
|
|
|
188
|
0
|
|
|
0
|
1
|
|
my ($querytext, $testmode) = @_; |
189
|
|
|
|
|
|
|
|
190
|
0
|
0
|
|
|
|
|
if ($testmode) { print $querytext; } |
|
0
|
|
|
|
|
|
|
191
|
|
|
|
|
|
|
|
192
|
0
|
|
0
|
|
|
|
my $sth = $dbh->prepare($querytext) || |
193
|
|
|
|
|
|
|
handle_error("failed to prepare $querytext", caller()); |
194
|
|
|
|
|
|
|
|
195
|
0
|
|
|
|
|
|
return $sth; |
196
|
|
|
|
|
|
|
|
197
|
|
|
|
|
|
|
} |
198
|
|
|
|
|
|
|
|
199
|
|
|
|
|
|
|
|
200
|
|
|
|
|
|
|
############################################################################### |
201
|
|
|
|
|
|
|
# |
202
|
|
|
|
|
|
|
# show_sql - display sql statement useful for debugging |
203
|
|
|
|
|
|
|
# |
204
|
|
|
|
|
|
|
############################################################################### |
205
|
|
|
|
|
|
|
|
206
|
|
|
|
|
|
|
sub show_sql { |
207
|
|
|
|
|
|
|
|
208
|
0
|
|
|
0
|
1
|
|
my ($querytext) = @_; |
209
|
|
|
|
|
|
|
|
210
|
0
|
|
|
|
|
|
print $querytext. "\n"; |
211
|
|
|
|
|
|
|
|
212
|
|
|
|
|
|
|
} |
213
|
|
|
|
|
|
|
|
214
|
|
|
|
|
|
|
|
215
|
|
|
|
|
|
|
############################################################################### |
216
|
|
|
|
|
|
|
# |
217
|
|
|
|
|
|
|
# execute_sql - take a string and execute the sql return a hash of column headings and values |
218
|
|
|
|
|
|
|
# |
219
|
|
|
|
|
|
|
############################################################################### |
220
|
|
|
|
|
|
|
|
221
|
|
|
|
|
|
|
sub execute_sql { |
222
|
|
|
|
|
|
|
|
223
|
0
|
|
|
0
|
1
|
|
my ($querytext, $testmode) = @_; |
224
|
|
|
|
|
|
|
|
225
|
0
|
0
|
|
|
|
|
print "$querytext\n" if ($testmode); |
226
|
|
|
|
|
|
|
|
227
|
0
|
|
0
|
|
|
|
my $sth = prepare_sql($querytext) || |
228
|
|
|
|
|
|
|
handle_error("error preparing $querytext", caller()); |
229
|
|
|
|
|
|
|
|
230
|
|
|
|
|
|
|
# execute the query - if it fails pass to the error handler |
231
|
0
|
0
|
|
|
|
|
$sth->execute() || handle_error("error executing $querytext", caller()); |
232
|
|
|
|
|
|
|
|
233
|
0
|
|
|
|
|
|
return $sth; |
234
|
|
|
|
|
|
|
|
235
|
|
|
|
|
|
|
} |
236
|
|
|
|
|
|
|
|
237
|
|
|
|
|
|
|
|
238
|
|
|
|
|
|
|
############################################################################### |
239
|
|
|
|
|
|
|
# |
240
|
|
|
|
|
|
|
# execute - execute prepared statement |
241
|
|
|
|
|
|
|
# |
242
|
|
|
|
|
|
|
############################################################################### |
243
|
|
|
|
|
|
|
|
244
|
|
|
|
|
|
|
sub execute { |
245
|
|
|
|
|
|
|
|
246
|
0
|
|
|
0
|
1
|
|
my ($sth) = @_; |
247
|
|
|
|
|
|
|
|
248
|
|
|
|
|
|
|
# execute the query - if it fails pass to the errorHandler |
249
|
0
|
0
|
|
|
|
|
$sth->execute() || |
250
|
|
|
|
|
|
|
handle_error("error executing $sth->{statement}", caller()); |
251
|
|
|
|
|
|
|
|
252
|
0
|
|
|
|
|
|
return $sth; |
253
|
|
|
|
|
|
|
|
254
|
|
|
|
|
|
|
} |
255
|
|
|
|
|
|
|
|
256
|
|
|
|
|
|
|
|
257
|
|
|
|
|
|
|
############################################################################### |
258
|
|
|
|
|
|
|
# |
259
|
|
|
|
|
|
|
# get_max - return the maximum value of a database column |
260
|
|
|
|
|
|
|
# |
261
|
|
|
|
|
|
|
############################################################################### |
262
|
|
|
|
|
|
|
|
263
|
|
|
|
|
|
|
sub get_max { |
264
|
|
|
|
|
|
|
|
265
|
0
|
|
|
0
|
1
|
|
my ($column, $table) = @_; |
266
|
|
|
|
|
|
|
|
267
|
0
|
|
|
|
|
|
my $row = do_query( <<EOSQL); |
268
|
|
|
|
|
|
|
|
269
|
|
|
|
|
|
|
select max($column) as $column |
270
|
|
|
|
|
|
|
from $table |
271
|
|
|
|
|
|
|
|
272
|
|
|
|
|
|
|
EOSQL |
273
|
|
|
|
|
|
|
|
274
|
0
|
|
|
|
|
|
return $row->{$column}; |
275
|
|
|
|
|
|
|
|
276
|
|
|
|
|
|
|
} |
277
|
|
|
|
|
|
|
|
278
|
|
|
|
|
|
|
|
279
|
|
|
|
|
|
|
############################################################################### |
280
|
|
|
|
|
|
|
# |
281
|
|
|
|
|
|
|
# get_last_max - return the latest increment for this database handle |
282
|
|
|
|
|
|
|
# |
283
|
|
|
|
|
|
|
############################################################################### |
284
|
|
|
|
|
|
|
|
285
|
|
|
|
|
|
|
sub get_last_max { |
286
|
|
|
|
|
|
|
|
287
|
0
|
|
|
0
|
1
|
|
my $row = do_query( <<EOSQL); |
288
|
|
|
|
|
|
|
|
289
|
|
|
|
|
|
|
select last_insert_id() as lastmaxid |
290
|
|
|
|
|
|
|
|
291
|
|
|
|
|
|
|
EOSQL |
292
|
|
|
|
|
|
|
|
293
|
0
|
|
|
|
|
|
return $row->{lastmaxid}; |
294
|
|
|
|
|
|
|
|
295
|
|
|
|
|
|
|
} |
296
|
|
|
|
|
|
|
|
297
|
|
|
|
|
|
|
|
298
|
|
|
|
|
|
|
############################################################################### |
299
|
|
|
|
|
|
|
# |
300
|
|
|
|
|
|
|
# count_rows_in_table - check if a value exists in a given column and table |
301
|
|
|
|
|
|
|
# |
302
|
|
|
|
|
|
|
############################################################################### |
303
|
|
|
|
|
|
|
|
304
|
|
|
|
|
|
|
sub count_rows_in_table { |
305
|
|
|
|
|
|
|
|
306
|
0
|
|
|
0
|
1
|
|
my ($table, $column, $value) = @_; |
307
|
|
|
|
|
|
|
|
308
|
0
|
|
|
|
|
|
my $query = prepare_sql( <<EOSQL); |
309
|
|
|
|
|
|
|
|
310
|
|
|
|
|
|
|
select count(*) as rowcount |
311
|
|
|
|
|
|
|
from $table |
312
|
|
|
|
|
|
|
where $column = ? |
313
|
|
|
|
|
|
|
|
314
|
|
|
|
|
|
|
EOSQL |
315
|
|
|
|
|
|
|
|
316
|
0
|
|
|
|
|
|
bind_param($query, 1, $value); |
317
|
0
|
|
|
|
|
|
execute($query); |
318
|
|
|
|
|
|
|
|
319
|
0
|
|
|
|
|
|
my $row = get_result_hash($query); |
320
|
0
|
|
|
|
|
|
return $row->{rowcount}; |
321
|
|
|
|
|
|
|
|
322
|
|
|
|
|
|
|
} |
323
|
|
|
|
|
|
|
|
324
|
|
|
|
|
|
|
|
325
|
|
|
|
|
|
|
############################################################################### |
326
|
|
|
|
|
|
|
# |
327
|
|
|
|
|
|
|
# exists_in_table - check if a value exists in a given column and table |
328
|
|
|
|
|
|
|
# |
329
|
|
|
|
|
|
|
############################################################################### |
330
|
|
|
|
|
|
|
|
331
|
|
|
|
|
|
|
sub exists_in_table { |
332
|
|
|
|
|
|
|
|
333
|
0
|
|
|
0
|
1
|
|
my ($table, $column, $value) = @_; |
334
|
|
|
|
|
|
|
|
335
|
0
|
|
|
|
|
|
return count_rows_in_table($table, $column, $value) > 0; |
336
|
|
|
|
|
|
|
|
337
|
|
|
|
|
|
|
} |
338
|
|
|
|
|
|
|
|
339
|
|
|
|
|
|
|
|
340
|
|
|
|
|
|
|
############################################################################### |
341
|
|
|
|
|
|
|
# |
342
|
|
|
|
|
|
|
# get_row - return a row based on a key |
343
|
|
|
|
|
|
|
# |
344
|
|
|
|
|
|
|
############################################################################### |
345
|
|
|
|
|
|
|
|
346
|
|
|
|
|
|
|
sub get_row { |
347
|
|
|
|
|
|
|
|
348
|
0
|
|
|
0
|
1
|
|
my ($table, $column, $value) = @_; |
349
|
|
|
|
|
|
|
|
350
|
0
|
|
|
|
|
|
my $query = prepare_sql( <<EOSQL); |
351
|
|
|
|
|
|
|
|
352
|
|
|
|
|
|
|
select * |
353
|
|
|
|
|
|
|
from $table |
354
|
|
|
|
|
|
|
where $column = ? |
355
|
|
|
|
|
|
|
|
356
|
|
|
|
|
|
|
EOSQL |
357
|
|
|
|
|
|
|
|
358
|
0
|
|
|
|
|
|
bind_param($query, 1, $value); |
359
|
0
|
|
|
|
|
|
execute($query); |
360
|
0
|
|
|
|
|
|
return get_result_hash($query); |
361
|
|
|
|
|
|
|
|
362
|
|
|
|
|
|
|
} |
363
|
|
|
|
|
|
|
|
364
|
|
|
|
|
|
|
|
365
|
|
|
|
|
|
|
############################################################################### |
366
|
|
|
|
|
|
|
# |
367
|
|
|
|
|
|
|
# get_count - return a simple row in the table |
368
|
|
|
|
|
|
|
# |
369
|
|
|
|
|
|
|
############################################################################### |
370
|
|
|
|
|
|
|
|
371
|
|
|
|
|
|
|
sub get_count { |
372
|
|
|
|
|
|
|
|
373
|
0
|
|
|
0
|
1
|
|
my ($table) = @_; |
374
|
|
|
|
|
|
|
|
375
|
0
|
|
|
|
|
|
my $row = do_query("select count(*) as 'count' from $table"); |
376
|
|
|
|
|
|
|
|
377
|
0
|
|
|
|
|
|
return $row->{count}; |
378
|
|
|
|
|
|
|
|
379
|
|
|
|
|
|
|
} |
380
|
|
|
|
|
|
|
|
381
|
|
|
|
|
|
|
|
382
|
|
|
|
|
|
|
############################################################################### |
383
|
|
|
|
|
|
|
# |
384
|
|
|
|
|
|
|
# delete_row - delete a row based on a key |
385
|
|
|
|
|
|
|
# |
386
|
|
|
|
|
|
|
############################################################################### |
387
|
|
|
|
|
|
|
|
388
|
|
|
|
|
|
|
sub delete_row { |
389
|
|
|
|
|
|
|
|
390
|
0
|
|
|
0
|
1
|
|
my ($table, $column, $value) = @_; |
391
|
|
|
|
|
|
|
|
392
|
0
|
|
|
|
|
|
my $query = prepare_sql( <<EOSQL); |
393
|
|
|
|
|
|
|
|
394
|
|
|
|
|
|
|
delete |
395
|
|
|
|
|
|
|
from $table |
396
|
|
|
|
|
|
|
where $column = ? |
397
|
|
|
|
|
|
|
|
398
|
|
|
|
|
|
|
EOSQL |
399
|
|
|
|
|
|
|
|
400
|
0
|
|
|
|
|
|
bind_param($query, 1, $value); |
401
|
0
|
|
|
|
|
|
execute($query); |
402
|
|
|
|
|
|
|
|
403
|
|
|
|
|
|
|
} |
404
|
|
|
|
|
|
|
|
405
|
|
|
|
|
|
|
|
406
|
|
|
|
|
|
|
############################################################################### |
407
|
|
|
|
|
|
|
# |
408
|
|
|
|
|
|
|
# quote - quote a value for insertion into the database |
409
|
|
|
|
|
|
|
# |
410
|
|
|
|
|
|
|
############################################################################### |
411
|
|
|
|
|
|
|
|
412
|
|
|
|
|
|
|
sub quote { |
413
|
|
|
|
|
|
|
|
414
|
0
|
|
|
0
|
1
|
|
my ($value) = @_; |
415
|
|
|
|
|
|
|
|
416
|
0
|
|
|
|
|
|
return $dbh->quote($value); |
417
|
|
|
|
|
|
|
|
418
|
|
|
|
|
|
|
} |
419
|
|
|
|
|
|
|
|
420
|
|
|
|
|
|
|
|
421
|
|
|
|
|
|
|
############################################################################### |
422
|
|
|
|
|
|
|
# |
423
|
|
|
|
|
|
|
# handle_error - handle any error thrown by the dbi subsystem |
424
|
|
|
|
|
|
|
# |
425
|
|
|
|
|
|
|
############################################################################### |
426
|
|
|
|
|
|
|
|
427
|
|
|
|
|
|
|
sub handle_error { |
428
|
|
|
|
|
|
|
|
429
|
0
|
|
|
0
|
1
|
|
my ($message, $calledby) = @_; |
430
|
|
|
|
|
|
|
|
431
|
0
|
|
|
|
|
|
die("[$calledby] $message \n[DB says: $DBI::err $DBI::errstr $DBI::state]"); |
432
|
|
|
|
|
|
|
|
433
|
|
|
|
|
|
|
} |
434
|
|
|
|
|
|
|
|
435
|
|
|
|
|
|
|
|
436
|
|
|
|
|
|
|
############################################################################### |
437
|
|
|
|
|
|
|
# |
438
|
|
|
|
|
|
|
# get_primary_key - return the primary key for a table |
439
|
|
|
|
|
|
|
# |
440
|
|
|
|
|
|
|
############################################################################### |
441
|
|
|
|
|
|
|
|
442
|
|
|
|
|
|
|
sub get_primary_key { |
443
|
|
|
|
|
|
|
|
444
|
0
|
|
|
0
|
1
|
|
my ($table) = @_; |
445
|
|
|
|
|
|
|
|
446
|
0
|
|
|
|
|
|
my @keys = $dbh->primary_key(undef, undef, $table); |
447
|
|
|
|
|
|
|
|
448
|
|
|
|
|
|
|
# assume one column primary keys |
449
|
0
|
|
|
|
|
|
return pop(@keys); |
450
|
|
|
|
|
|
|
|
451
|
|
|
|
|
|
|
} |
452
|
|
|
|
|
|
|
|
453
|
|
|
|
|
|
|
|
454
|
|
|
|
|
|
|
############################################################################### |
455
|
|
|
|
|
|
|
# |
456
|
|
|
|
|
|
|
# get_table_columns - return a list of column names for the table |
457
|
|
|
|
|
|
|
# |
458
|
|
|
|
|
|
|
############################################################################### |
459
|
|
|
|
|
|
|
|
460
|
|
|
|
|
|
|
sub get_table_columns { |
461
|
|
|
|
|
|
|
|
462
|
0
|
|
|
0
|
1
|
|
my ($table) = @_; |
463
|
|
|
|
|
|
|
|
464
|
0
|
|
|
|
|
|
my $query = execute_sql("select * from $table"); |
465
|
|
|
|
|
|
|
|
466
|
0
|
|
|
|
|
|
my $row = get_result_hash($query); |
467
|
|
|
|
|
|
|
|
468
|
0
|
|
|
|
|
|
return sort { $a cmp $b } keys %$row; |
|
0
|
|
|
|
|
|
|
469
|
|
|
|
|
|
|
|
470
|
|
|
|
|
|
|
} |
471
|
|
|
|
|
|
|
|
472
|
|
|
|
|
|
|
|
473
|
|
|
|
|
|
|
1; |
474
|
|
|
|
|
|
|
|
475
|
|
|
|
|
|
|
|
476
|
|
|
|
|
|
|
__END__ |
477
|
|
|
|
|
|
|
|
478
|
|
|
|
|
|
|
=head1 NAME |
479
|
|
|
|
|
|
|
|
480
|
|
|
|
|
|
|
Goo::LiteDatabase - Drive an SQLite database |
481
|
|
|
|
|
|
|
|
482
|
|
|
|
|
|
|
=head1 SYNOPSIS |
483
|
|
|
|
|
|
|
|
484
|
|
|
|
|
|
|
use Goo::LiteDatabase; |
485
|
|
|
|
|
|
|
|
486
|
|
|
|
|
|
|
=head1 DESCRIPTION |
487
|
|
|
|
|
|
|
|
488
|
|
|
|
|
|
|
Interface to an SQLite database. |
489
|
|
|
|
|
|
|
|
490
|
|
|
|
|
|
|
=head1 METHODS |
491
|
|
|
|
|
|
|
|
492
|
|
|
|
|
|
|
=over |
493
|
|
|
|
|
|
|
|
494
|
|
|
|
|
|
|
=item get_connection |
495
|
|
|
|
|
|
|
|
496
|
|
|
|
|
|
|
open a connection to the default database |
497
|
|
|
|
|
|
|
|
498
|
|
|
|
|
|
|
=item do_sql |
499
|
|
|
|
|
|
|
|
500
|
|
|
|
|
|
|
execute some SQL |
501
|
|
|
|
|
|
|
|
502
|
|
|
|
|
|
|
=item do_query |
503
|
|
|
|
|
|
|
|
504
|
|
|
|
|
|
|
execute SQL and return the result all in one |
505
|
|
|
|
|
|
|
|
506
|
|
|
|
|
|
|
=item generate_numeric_sqlin_clause |
507
|
|
|
|
|
|
|
|
508
|
|
|
|
|
|
|
return an SQL 'in' clause with numeric values |
509
|
|
|
|
|
|
|
|
510
|
|
|
|
|
|
|
=item generate_string_sqlin_clause |
511
|
|
|
|
|
|
|
|
512
|
|
|
|
|
|
|
return an SQL 'in' clause with string values |
513
|
|
|
|
|
|
|
|
514
|
|
|
|
|
|
|
=item get_number_of_rows |
515
|
|
|
|
|
|
|
|
516
|
|
|
|
|
|
|
return the number of rows for this statement handle |
517
|
|
|
|
|
|
|
|
518
|
|
|
|
|
|
|
=item get_next_row |
519
|
|
|
|
|
|
|
|
520
|
|
|
|
|
|
|
alias for get_result_hash |
521
|
|
|
|
|
|
|
|
522
|
|
|
|
|
|
|
=item get_result_hash |
523
|
|
|
|
|
|
|
|
524
|
|
|
|
|
|
|
return a hash for this result |
525
|
|
|
|
|
|
|
|
526
|
|
|
|
|
|
|
=item bind_param |
527
|
|
|
|
|
|
|
|
528
|
|
|
|
|
|
|
bind a parameter to a value |
529
|
|
|
|
|
|
|
|
530
|
|
|
|
|
|
|
=item show_sql |
531
|
|
|
|
|
|
|
|
532
|
|
|
|
|
|
|
display SQL statement useful for debugging |
533
|
|
|
|
|
|
|
|
534
|
|
|
|
|
|
|
=item execute_sql |
535
|
|
|
|
|
|
|
|
536
|
|
|
|
|
|
|
take a string and execute the SQL return a hash of column headings and values |
537
|
|
|
|
|
|
|
|
538
|
|
|
|
|
|
|
=item prepare_sql |
539
|
|
|
|
|
|
|
|
540
|
|
|
|
|
|
|
take a string and prepare the SQL for later execution |
541
|
|
|
|
|
|
|
|
542
|
|
|
|
|
|
|
=item execute |
543
|
|
|
|
|
|
|
|
544
|
|
|
|
|
|
|
execute prepared statement |
545
|
|
|
|
|
|
|
|
546
|
|
|
|
|
|
|
=item get_max |
547
|
|
|
|
|
|
|
|
548
|
|
|
|
|
|
|
return the maximum value of a database column |
549
|
|
|
|
|
|
|
|
550
|
|
|
|
|
|
|
=item get_last_max |
551
|
|
|
|
|
|
|
|
552
|
|
|
|
|
|
|
return the latest increment for this database handle |
553
|
|
|
|
|
|
|
|
554
|
|
|
|
|
|
|
=item count_rows_in_table |
555
|
|
|
|
|
|
|
|
556
|
|
|
|
|
|
|
check if a value exists in a given column and table |
557
|
|
|
|
|
|
|
|
558
|
|
|
|
|
|
|
=item exists_in_table |
559
|
|
|
|
|
|
|
|
560
|
|
|
|
|
|
|
check if a value exists in a given column and table |
561
|
|
|
|
|
|
|
|
562
|
|
|
|
|
|
|
=item get_row |
563
|
|
|
|
|
|
|
|
564
|
|
|
|
|
|
|
return a row based on a key |
565
|
|
|
|
|
|
|
|
566
|
|
|
|
|
|
|
=item get_count |
567
|
|
|
|
|
|
|
|
568
|
|
|
|
|
|
|
return a simple row in the table |
569
|
|
|
|
|
|
|
|
570
|
|
|
|
|
|
|
=item delete_row |
571
|
|
|
|
|
|
|
|
572
|
|
|
|
|
|
|
delete a row based on a key |
573
|
|
|
|
|
|
|
|
574
|
|
|
|
|
|
|
=item quote |
575
|
|
|
|
|
|
|
|
576
|
|
|
|
|
|
|
quote a value for insertion into the database |
577
|
|
|
|
|
|
|
|
578
|
|
|
|
|
|
|
=item handle_error |
579
|
|
|
|
|
|
|
|
580
|
|
|
|
|
|
|
handle any error thrown by the dbi subsystem |
581
|
|
|
|
|
|
|
|
582
|
|
|
|
|
|
|
=item get_primary_key |
583
|
|
|
|
|
|
|
|
584
|
|
|
|
|
|
|
return the primary key for a table |
585
|
|
|
|
|
|
|
|
586
|
|
|
|
|
|
|
=item get_table_columns |
587
|
|
|
|
|
|
|
|
588
|
|
|
|
|
|
|
return a list of column names for the table |
589
|
|
|
|
|
|
|
|
590
|
|
|
|
|
|
|
=back |
591
|
|
|
|
|
|
|
|
592
|
|
|
|
|
|
|
=head1 AUTHOR |
593
|
|
|
|
|
|
|
|
594
|
|
|
|
|
|
|
Nigel Hamilton <nigel@trexy.com> |
595
|
|
|
|
|
|
|
|
596
|
|
|
|
|
|
|
=head1 SEE ALSO |
597
|
|
|
|
|
|
|
|