line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
# This module is for saving development time and |
2
|
|
|
|
|
|
|
# programming space with the Perl DBI. |
3
|
|
|
|
|
|
|
|
4
|
|
|
|
|
|
|
package Relations::Abstract; |
5
|
|
|
|
|
|
|
|
6
|
|
|
|
|
|
|
require Exporter; |
7
|
|
|
|
|
|
|
require DBI; |
8
|
|
|
|
|
|
|
require 5.004; |
9
|
|
|
|
|
|
|
require Relations; |
10
|
|
|
|
|
|
|
require Relations::Query; |
11
|
|
|
|
|
|
|
|
12
|
1
|
|
|
1
|
|
52477
|
use Relations; |
|
1
|
|
|
|
|
3
|
|
|
1
|
|
|
|
|
131
|
|
13
|
1
|
|
|
1
|
|
6
|
use Relations::Query; |
|
1
|
|
|
|
|
1
|
|
|
1
|
|
|
|
|
119
|
|
14
|
|
|
|
|
|
|
|
15
|
|
|
|
|
|
|
# You can run this file through either pod2man or pod2html to produce pretty |
16
|
|
|
|
|
|
|
# documentation in manual or html file format (these utilities are part of the |
17
|
|
|
|
|
|
|
# Perl 5 distribution). |
18
|
|
|
|
|
|
|
|
19
|
|
|
|
|
|
|
# Copyright 2001 GAF-3 Industries, Inc. All rights reserved. |
20
|
|
|
|
|
|
|
# Written by George A. Fitch III (aka Gaffer), gaf3@gaf3.com |
21
|
|
|
|
|
|
|
|
22
|
|
|
|
|
|
|
# This program is free software, you can redistribute it and/or modify it under |
23
|
|
|
|
|
|
|
# the same terms as Perl itself |
24
|
|
|
|
|
|
|
|
25
|
|
|
|
|
|
|
$Relations::Abstract::VERSION='0.94'; |
26
|
|
|
|
|
|
|
|
27
|
|
|
|
|
|
|
@ISA = qw(Exporter); |
28
|
|
|
|
|
|
|
|
29
|
|
|
|
|
|
|
@EXPORT = qw( |
30
|
|
|
|
|
|
|
new |
31
|
|
|
|
|
|
|
); |
32
|
|
|
|
|
|
|
|
33
|
|
|
|
|
|
|
@EXPORT_OK = qw( |
34
|
|
|
|
|
|
|
new |
35
|
|
|
|
|
|
|
set_dbh |
36
|
|
|
|
|
|
|
run_query |
37
|
|
|
|
|
|
|
select_field |
38
|
|
|
|
|
|
|
select_row |
39
|
|
|
|
|
|
|
select_column |
40
|
|
|
|
|
|
|
select_matrix |
41
|
|
|
|
|
|
|
insert_row |
42
|
|
|
|
|
|
|
insert_id |
43
|
|
|
|
|
|
|
select_insert_id |
44
|
|
|
|
|
|
|
update_rows |
45
|
|
|
|
|
|
|
delete_rows |
46
|
|
|
|
|
|
|
report_error |
47
|
|
|
|
|
|
|
); |
48
|
|
|
|
|
|
|
|
49
|
|
|
|
|
|
|
%EXPORT_TAGS = (); |
50
|
|
|
|
|
|
|
|
51
|
|
|
|
|
|
|
# From here on out, be strict and clean. |
52
|
|
|
|
|
|
|
|
53
|
1
|
|
|
1
|
|
6
|
use strict; |
|
1
|
|
|
|
|
5
|
|
|
1
|
|
|
|
|
1866
|
|
54
|
|
|
|
|
|
|
|
55
|
|
|
|
|
|
|
|
56
|
|
|
|
|
|
|
|
57
|
|
|
|
|
|
|
### Creates a new Relations::Abstract object. |
58
|
|
|
|
|
|
|
|
59
|
|
|
|
|
|
|
sub new { |
60
|
|
|
|
|
|
|
|
61
|
0
|
|
|
0
|
|
|
my ($type) = shift; |
62
|
|
|
|
|
|
|
|
63
|
|
|
|
|
|
|
# Get all the arguments passed |
64
|
|
|
|
|
|
|
|
65
|
0
|
|
|
|
|
|
my ($dbh) = shift; |
66
|
|
|
|
|
|
|
|
67
|
|
|
|
|
|
|
# Create the hash to hold all the vars |
68
|
|
|
|
|
|
|
# for this object. |
69
|
|
|
|
|
|
|
|
70
|
0
|
|
|
|
|
|
my $self = {}; |
71
|
|
|
|
|
|
|
|
72
|
|
|
|
|
|
|
# Bless it with the type sent (I think this |
73
|
|
|
|
|
|
|
# makes it a full fledged object) |
74
|
|
|
|
|
|
|
|
75
|
0
|
|
|
|
|
|
bless $self, $type; |
76
|
|
|
|
|
|
|
|
77
|
|
|
|
|
|
|
# Add the info into the hash only if it was sent |
78
|
|
|
|
|
|
|
|
79
|
0
|
0
|
|
|
|
|
$self->{dbh} = $dbh if $dbh; |
80
|
|
|
|
|
|
|
|
81
|
|
|
|
|
|
|
# Give thyself |
82
|
|
|
|
|
|
|
|
83
|
0
|
|
|
|
|
|
return $self; |
84
|
|
|
|
|
|
|
|
85
|
|
|
|
|
|
|
} |
86
|
|
|
|
|
|
|
|
87
|
|
|
|
|
|
|
|
88
|
|
|
|
|
|
|
|
89
|
|
|
|
|
|
|
### Sets the default database handle to use. |
90
|
|
|
|
|
|
|
|
91
|
|
|
|
|
|
|
sub set_dbh { |
92
|
|
|
|
|
|
|
|
93
|
|
|
|
|
|
|
# Know thyself |
94
|
|
|
|
|
|
|
|
95
|
0
|
|
|
0
|
1
|
|
my $self = shift; |
96
|
|
|
|
|
|
|
|
97
|
|
|
|
|
|
|
# Get the DBH sent |
98
|
|
|
|
|
|
|
|
99
|
0
|
|
|
|
|
|
my ($dbh) = shift; |
100
|
|
|
|
|
|
|
|
101
|
|
|
|
|
|
|
# Set the database handle. |
102
|
|
|
|
|
|
|
|
103
|
0
|
|
|
|
|
|
$self->{dbh} = $dbh; |
104
|
|
|
|
|
|
|
|
105
|
|
|
|
|
|
|
} |
106
|
|
|
|
|
|
|
|
107
|
|
|
|
|
|
|
|
108
|
|
|
|
|
|
|
|
109
|
|
|
|
|
|
|
### Runs a query. |
110
|
|
|
|
|
|
|
|
111
|
|
|
|
|
|
|
sub run_query { |
112
|
|
|
|
|
|
|
|
113
|
|
|
|
|
|
|
### What we're doing here is sending the query to |
114
|
|
|
|
|
|
|
### the dbh, and reporting an error if the execute |
115
|
|
|
|
|
|
|
### failed. The query can be sent as a string, hash |
116
|
|
|
|
|
|
|
### or Relations::Query object. |
117
|
|
|
|
|
|
|
|
118
|
|
|
|
|
|
|
# Know thyself |
119
|
|
|
|
|
|
|
|
120
|
0
|
|
|
0
|
1
|
|
my $self = shift; |
121
|
|
|
|
|
|
|
|
122
|
|
|
|
|
|
|
# Get the query sent |
123
|
|
|
|
|
|
|
|
124
|
0
|
|
|
|
|
|
my ($query) = shift; |
125
|
|
|
|
|
|
|
|
126
|
|
|
|
|
|
|
# Convert whatever we were sent to a query string. |
127
|
|
|
|
|
|
|
|
128
|
0
|
|
|
|
|
|
$query = to_string($query); |
129
|
|
|
|
|
|
|
|
130
|
|
|
|
|
|
|
# Declare a statement handle and prepare the query. |
131
|
|
|
|
|
|
|
|
132
|
0
|
|
|
|
|
|
my ($sth) = $self->{dbh}->prepare($query); |
133
|
|
|
|
|
|
|
|
134
|
|
|
|
|
|
|
# Execute it. |
135
|
|
|
|
|
|
|
|
136
|
0
|
0
|
|
|
|
|
$sth->execute() or return $self->report_error("run_query failed: $query\n"); |
137
|
|
|
|
|
|
|
|
138
|
|
|
|
|
|
|
# Finish it off. |
139
|
|
|
|
|
|
|
|
140
|
0
|
|
|
|
|
|
$sth->finish(); |
141
|
|
|
|
|
|
|
|
142
|
|
|
|
|
|
|
# Return 1 cuz it worked |
143
|
|
|
|
|
|
|
|
144
|
0
|
|
|
|
|
|
return 1; |
145
|
|
|
|
|
|
|
|
146
|
|
|
|
|
|
|
} |
147
|
|
|
|
|
|
|
|
148
|
|
|
|
|
|
|
|
149
|
|
|
|
|
|
|
|
150
|
|
|
|
|
|
|
### Select a field's data. |
151
|
|
|
|
|
|
|
|
152
|
|
|
|
|
|
|
sub select_field { |
153
|
|
|
|
|
|
|
|
154
|
|
|
|
|
|
|
### What we're doing here is creating and sending the query |
155
|
|
|
|
|
|
|
### string to the dbh, retreiving the requested item and |
156
|
|
|
|
|
|
|
### reporting an error if the execute failed. We can take |
157
|
|
|
|
|
|
|
### simple info like the table and where clause, or complex |
158
|
|
|
|
|
|
|
### info like a full query. |
159
|
|
|
|
|
|
|
|
160
|
|
|
|
|
|
|
# Know thyself |
161
|
|
|
|
|
|
|
|
162
|
0
|
|
|
0
|
1
|
|
my $self = shift; |
163
|
|
|
|
|
|
|
|
164
|
|
|
|
|
|
|
# Get the field, table, where clause sent |
165
|
|
|
|
|
|
|
|
166
|
0
|
|
|
|
|
|
my ($field,$table,$where,$query) = rearrange(['FIELD','TABLE','WHERE','QUERY'],@_); |
167
|
|
|
|
|
|
|
|
168
|
|
|
|
|
|
|
# Unless we were sent query info, make some. |
169
|
|
|
|
|
|
|
|
170
|
0
|
0
|
|
|
|
|
$query = "select $field from $table where " . equals_clause($where) unless $query; |
171
|
|
|
|
|
|
|
|
172
|
|
|
|
|
|
|
# Convert whatever we were sent to a query string. |
173
|
|
|
|
|
|
|
|
174
|
0
|
|
|
|
|
|
$query = to_string($query); |
175
|
|
|
|
|
|
|
|
176
|
|
|
|
|
|
|
# Declare a statement handle and prepare the query. |
177
|
|
|
|
|
|
|
|
178
|
0
|
|
|
|
|
|
my ($sth) = $self->{dbh}->prepare($query); |
179
|
|
|
|
|
|
|
|
180
|
|
|
|
|
|
|
# Execute it. |
181
|
|
|
|
|
|
|
|
182
|
0
|
0
|
|
|
|
|
$sth->execute() or return $self->report_error("select_field failed: $query\n"); |
183
|
|
|
|
|
|
|
|
184
|
|
|
|
|
|
|
# Declare hash for retrieving value, and variable to hold value. |
185
|
|
|
|
|
|
|
|
186
|
0
|
|
|
|
|
|
my ($hash_ref,$value); |
187
|
|
|
|
|
|
|
|
188
|
|
|
|
|
|
|
# If we got something returned |
189
|
|
|
|
|
|
|
|
190
|
0
|
0
|
|
|
|
|
if ($hash_ref = $sth->fetchrow_hashref()) { |
191
|
|
|
|
|
|
|
|
192
|
|
|
|
|
|
|
# Get the value returned. |
193
|
|
|
|
|
|
|
|
194
|
0
|
|
|
|
|
|
$value = $hash_ref->{$field}; |
195
|
|
|
|
|
|
|
|
196
|
|
|
|
|
|
|
} |
197
|
|
|
|
|
|
|
|
198
|
|
|
|
|
|
|
# Finish it off. |
199
|
|
|
|
|
|
|
|
200
|
0
|
|
|
|
|
|
$sth->finish(); |
201
|
|
|
|
|
|
|
|
202
|
|
|
|
|
|
|
# Return the value. |
203
|
|
|
|
|
|
|
|
204
|
0
|
|
|
|
|
|
return $value; |
205
|
|
|
|
|
|
|
|
206
|
|
|
|
|
|
|
} |
207
|
|
|
|
|
|
|
|
208
|
|
|
|
|
|
|
|
209
|
|
|
|
|
|
|
|
210
|
|
|
|
|
|
|
### Selects a row of data. |
211
|
|
|
|
|
|
|
|
212
|
|
|
|
|
|
|
sub select_row { |
213
|
|
|
|
|
|
|
|
214
|
|
|
|
|
|
|
### What we're doing here is creating and sending the query |
215
|
|
|
|
|
|
|
### string to the dbh, retreiving the requested item and |
216
|
|
|
|
|
|
|
### reporting an error if the execute failed. We can take |
217
|
|
|
|
|
|
|
### simple info like the table and where clause, or complex |
218
|
|
|
|
|
|
|
### info like a full query. |
219
|
|
|
|
|
|
|
|
220
|
|
|
|
|
|
|
# Know thyself |
221
|
|
|
|
|
|
|
|
222
|
0
|
|
|
0
|
1
|
|
my $self = shift; |
223
|
|
|
|
|
|
|
|
224
|
|
|
|
|
|
|
# Get the table and where clause sent |
225
|
|
|
|
|
|
|
|
226
|
0
|
|
|
|
|
|
my ($table,$where,$query) = rearrange(['TABLE','WHERE','QUERY'],@_); |
227
|
|
|
|
|
|
|
|
228
|
|
|
|
|
|
|
# Unless we were sent query info, make some. |
229
|
|
|
|
|
|
|
|
230
|
0
|
0
|
|
|
|
|
$query = "select * from $table where " . equals_clause($where) unless $query; |
231
|
|
|
|
|
|
|
|
232
|
|
|
|
|
|
|
# Convert whatever we were sent to a query string. |
233
|
|
|
|
|
|
|
|
234
|
0
|
|
|
|
|
|
$query = to_string($query); |
235
|
|
|
|
|
|
|
|
236
|
|
|
|
|
|
|
# Declare a statement handle and prepare the query. |
237
|
|
|
|
|
|
|
|
238
|
0
|
|
|
|
|
|
my ($sth) = $self->{dbh}->prepare($query); |
239
|
|
|
|
|
|
|
|
240
|
|
|
|
|
|
|
# Execute it. |
241
|
|
|
|
|
|
|
|
242
|
0
|
0
|
|
|
|
|
$sth->execute() or return $self->report_error("select_row failed: $query\n"); |
243
|
|
|
|
|
|
|
|
244
|
|
|
|
|
|
|
# Get the value returned. |
245
|
|
|
|
|
|
|
|
246
|
0
|
|
|
|
|
|
my ($hash_ref) = $sth->fetchrow_hashref(); |
247
|
|
|
|
|
|
|
|
248
|
|
|
|
|
|
|
# Finish it off. |
249
|
|
|
|
|
|
|
|
250
|
0
|
|
|
|
|
|
$sth->finish(); |
251
|
|
|
|
|
|
|
|
252
|
|
|
|
|
|
|
# Return the value. |
253
|
|
|
|
|
|
|
|
254
|
0
|
|
|
|
|
|
return $hash_ref; |
255
|
|
|
|
|
|
|
|
256
|
|
|
|
|
|
|
} |
257
|
|
|
|
|
|
|
|
258
|
|
|
|
|
|
|
|
259
|
|
|
|
|
|
|
|
260
|
|
|
|
|
|
|
### Selects a column of data. |
261
|
|
|
|
|
|
|
|
262
|
|
|
|
|
|
|
sub select_column { |
263
|
|
|
|
|
|
|
|
264
|
|
|
|
|
|
|
### What we're doing here is creating and sending the query |
265
|
|
|
|
|
|
|
### string to the dbh, retreiving the requested items and |
266
|
|
|
|
|
|
|
### reporting an error if the execute failed. We can take |
267
|
|
|
|
|
|
|
### simple info like the table and where clause, or complex |
268
|
|
|
|
|
|
|
### info like a full query. |
269
|
|
|
|
|
|
|
|
270
|
|
|
|
|
|
|
# Know thyself |
271
|
|
|
|
|
|
|
|
272
|
0
|
|
|
0
|
1
|
|
my $self = shift; |
273
|
|
|
|
|
|
|
|
274
|
|
|
|
|
|
|
# Get the field table and where clause sent |
275
|
|
|
|
|
|
|
|
276
|
0
|
|
|
|
|
|
my ($field,$table,$where,$query) = rearrange(['FIELD','TABLE','WHERE','QUERY'],@_); |
277
|
|
|
|
|
|
|
|
278
|
|
|
|
|
|
|
# Unless we were sent query info, make some. |
279
|
|
|
|
|
|
|
|
280
|
0
|
0
|
|
|
|
|
$query = "select $field from $table where " . equals_clause($where) unless $query; |
281
|
|
|
|
|
|
|
|
282
|
|
|
|
|
|
|
# Convert whatever we were sent to a query string. |
283
|
|
|
|
|
|
|
|
284
|
0
|
|
|
|
|
|
$query = to_string($query); |
285
|
|
|
|
|
|
|
|
286
|
|
|
|
|
|
|
# Declare a statement handle and prepare the query. |
287
|
|
|
|
|
|
|
|
288
|
0
|
|
|
|
|
|
my ($sth) = $self->{dbh}->prepare($query); |
289
|
|
|
|
|
|
|
|
290
|
|
|
|
|
|
|
# Execute it. |
291
|
|
|
|
|
|
|
|
292
|
0
|
0
|
|
|
|
|
$sth->execute() or return $self->report_error("select_colum failed: $query\n"); |
293
|
|
|
|
|
|
|
|
294
|
|
|
|
|
|
|
# Create an array to hold the data |
295
|
|
|
|
|
|
|
|
296
|
0
|
|
|
|
|
|
my (@column) = (); |
297
|
|
|
|
|
|
|
|
298
|
|
|
|
|
|
|
# Get the values returned. |
299
|
|
|
|
|
|
|
|
300
|
0
|
|
|
|
|
|
my ($hash_ref); |
301
|
|
|
|
|
|
|
|
302
|
0
|
|
|
|
|
|
while ($hash_ref = $sth->fetchrow_hashref()) { |
303
|
|
|
|
|
|
|
|
304
|
|
|
|
|
|
|
# Add them to the column array. |
305
|
|
|
|
|
|
|
|
306
|
0
|
|
|
|
|
|
push @column,$hash_ref->{$field}; |
307
|
|
|
|
|
|
|
|
308
|
|
|
|
|
|
|
} |
309
|
|
|
|
|
|
|
|
310
|
|
|
|
|
|
|
# Finish it off. |
311
|
|
|
|
|
|
|
|
312
|
0
|
|
|
|
|
|
$sth->finish(); |
313
|
|
|
|
|
|
|
|
314
|
|
|
|
|
|
|
# Return the value. |
315
|
|
|
|
|
|
|
|
316
|
0
|
|
|
|
|
|
return \@column; |
317
|
|
|
|
|
|
|
|
318
|
|
|
|
|
|
|
} |
319
|
|
|
|
|
|
|
|
320
|
|
|
|
|
|
|
|
321
|
|
|
|
|
|
|
|
322
|
|
|
|
|
|
|
### Selects a matrix (rows of columns) of data. |
323
|
|
|
|
|
|
|
|
324
|
|
|
|
|
|
|
sub select_matrix { |
325
|
|
|
|
|
|
|
|
326
|
|
|
|
|
|
|
### What we're doing here is creating the query string |
327
|
|
|
|
|
|
|
### and sending it to the dbh, retreiving the rows of hashes, |
328
|
|
|
|
|
|
|
### returning them unless there's an error. If so we'll report |
329
|
|
|
|
|
|
|
### the error. We can take simple info like the table and where |
330
|
|
|
|
|
|
|
### clause, or complex info like a full query. |
331
|
|
|
|
|
|
|
|
332
|
|
|
|
|
|
|
# Know thyself |
333
|
|
|
|
|
|
|
|
334
|
0
|
|
|
0
|
1
|
|
my $self = shift; |
335
|
|
|
|
|
|
|
|
336
|
|
|
|
|
|
|
# Get the table and where clause sent |
337
|
|
|
|
|
|
|
|
338
|
0
|
|
|
|
|
|
my ($table,$where,$query) = rearrange(['TABLE','WHERE','QUERY'],@_); |
339
|
|
|
|
|
|
|
|
340
|
|
|
|
|
|
|
# Unless we were sent query info, make some. |
341
|
|
|
|
|
|
|
|
342
|
0
|
0
|
|
|
|
|
$query = "select * from $table where " . equals_clause($where) unless $query; |
343
|
|
|
|
|
|
|
|
344
|
|
|
|
|
|
|
# Convert whatever we were sent to a query string. |
345
|
|
|
|
|
|
|
|
346
|
0
|
|
|
|
|
|
$query = to_string($query); |
347
|
|
|
|
|
|
|
|
348
|
|
|
|
|
|
|
# Declare a statement handle and prepare the query. |
349
|
|
|
|
|
|
|
|
350
|
0
|
|
|
|
|
|
my ($sth) = $self->{dbh}->prepare($query); |
351
|
|
|
|
|
|
|
|
352
|
|
|
|
|
|
|
# Execute it. |
353
|
|
|
|
|
|
|
|
354
|
0
|
0
|
|
|
|
|
$sth->execute() or return $self->report_error("select_matrix failed: $query\n"); |
355
|
|
|
|
|
|
|
|
356
|
|
|
|
|
|
|
# Create an array to hold the data |
357
|
|
|
|
|
|
|
|
358
|
0
|
|
|
|
|
|
my (@matrix) = (); |
359
|
|
|
|
|
|
|
|
360
|
|
|
|
|
|
|
# Get the values returned. |
361
|
|
|
|
|
|
|
|
362
|
0
|
|
|
|
|
|
my ($hash_ref); |
363
|
|
|
|
|
|
|
|
364
|
0
|
|
|
|
|
|
while ($hash_ref = $sth->fetchrow_hashref()) { |
365
|
|
|
|
|
|
|
|
366
|
|
|
|
|
|
|
# Create a hash to hold the data |
367
|
|
|
|
|
|
|
|
368
|
0
|
|
|
|
|
|
my (%matrix) = %$hash_ref; |
369
|
|
|
|
|
|
|
|
370
|
|
|
|
|
|
|
# Add them to the column array. |
371
|
|
|
|
|
|
|
|
372
|
0
|
|
|
|
|
|
push @matrix,\%matrix; |
373
|
|
|
|
|
|
|
|
374
|
|
|
|
|
|
|
} |
375
|
|
|
|
|
|
|
|
376
|
|
|
|
|
|
|
# Finish it off. |
377
|
|
|
|
|
|
|
|
378
|
0
|
|
|
|
|
|
$sth->finish(); |
379
|
|
|
|
|
|
|
|
380
|
|
|
|
|
|
|
# Return the value. |
381
|
|
|
|
|
|
|
|
382
|
0
|
|
|
|
|
|
return \@matrix; |
383
|
|
|
|
|
|
|
|
384
|
|
|
|
|
|
|
} |
385
|
|
|
|
|
|
|
|
386
|
|
|
|
|
|
|
|
387
|
|
|
|
|
|
|
|
388
|
|
|
|
|
|
|
### Inserts a row of data into a table. |
389
|
|
|
|
|
|
|
|
390
|
|
|
|
|
|
|
sub insert_row { |
391
|
|
|
|
|
|
|
|
392
|
|
|
|
|
|
|
### What we're doing here is sending the query string to the dbh, and |
393
|
|
|
|
|
|
|
### returning the number of rows affected, unless there's an error. If |
394
|
|
|
|
|
|
|
### there's an error, we'll send back a 0. |
395
|
|
|
|
|
|
|
|
396
|
|
|
|
|
|
|
# Know thyself |
397
|
|
|
|
|
|
|
|
398
|
0
|
|
|
0
|
1
|
|
my $self = shift; |
399
|
|
|
|
|
|
|
|
400
|
|
|
|
|
|
|
# Get the table and set clause sent |
401
|
|
|
|
|
|
|
|
402
|
0
|
|
|
|
|
|
my ($table,$set) = rearrange(['TABLE','SET'],@_); |
403
|
|
|
|
|
|
|
|
404
|
|
|
|
|
|
|
# Get the info for the where clause; |
405
|
|
|
|
|
|
|
|
406
|
0
|
|
|
|
|
|
$set = assign_clause($set); |
407
|
|
|
|
|
|
|
|
408
|
|
|
|
|
|
|
# Form query |
409
|
|
|
|
|
|
|
|
410
|
0
|
|
|
|
|
|
my ($query) = "insert into $table set $set"; |
411
|
|
|
|
|
|
|
|
412
|
|
|
|
|
|
|
# Declare a statement handle and prepare the query. |
413
|
|
|
|
|
|
|
|
414
|
0
|
|
|
|
|
|
my ($sth) = $self->{dbh}->prepare($query); |
415
|
|
|
|
|
|
|
|
416
|
|
|
|
|
|
|
# Execute it. |
417
|
|
|
|
|
|
|
|
418
|
0
|
0
|
|
|
|
|
$sth->execute() or return $self->report_error("insert_row failed: $query\n"); |
419
|
|
|
|
|
|
|
|
420
|
|
|
|
|
|
|
# Finish it off. |
421
|
|
|
|
|
|
|
|
422
|
0
|
|
|
|
|
|
$sth->finish(); |
423
|
|
|
|
|
|
|
|
424
|
|
|
|
|
|
|
# Return the number of rows affected |
425
|
|
|
|
|
|
|
|
426
|
0
|
|
|
|
|
|
return $sth->rows(); |
427
|
|
|
|
|
|
|
|
428
|
|
|
|
|
|
|
} |
429
|
|
|
|
|
|
|
|
430
|
|
|
|
|
|
|
|
431
|
|
|
|
|
|
|
|
432
|
|
|
|
|
|
|
### Inserts a row data into a table and returns the new id. |
433
|
|
|
|
|
|
|
|
434
|
|
|
|
|
|
|
sub insert_id { |
435
|
|
|
|
|
|
|
|
436
|
|
|
|
|
|
|
### What we're doing here is sending the query string to the dbh, retreiving |
437
|
|
|
|
|
|
|
### the new id, and sending it back, unless there's an error. If there's an |
438
|
|
|
|
|
|
|
### error, we'll send back a zero. |
439
|
|
|
|
|
|
|
|
440
|
|
|
|
|
|
|
# Know thyself |
441
|
|
|
|
|
|
|
|
442
|
0
|
|
|
0
|
1
|
|
my $self = shift; |
443
|
|
|
|
|
|
|
|
444
|
|
|
|
|
|
|
# Get the table and set clause sent |
445
|
|
|
|
|
|
|
|
446
|
0
|
|
|
|
|
|
my ($table,$set) = rearrange(['TABLE','SET'],@_); |
447
|
|
|
|
|
|
|
|
448
|
|
|
|
|
|
|
# Get the info for the where clause; |
449
|
|
|
|
|
|
|
|
450
|
0
|
|
|
|
|
|
$set = assign_clause($set); |
451
|
|
|
|
|
|
|
|
452
|
|
|
|
|
|
|
# Form query |
453
|
|
|
|
|
|
|
|
454
|
0
|
|
|
|
|
|
my ($query) = "insert into $table set $set"; |
455
|
|
|
|
|
|
|
|
456
|
|
|
|
|
|
|
# Declare a statement handle and prepare the query. |
457
|
|
|
|
|
|
|
|
458
|
0
|
|
|
|
|
|
my ($sth) = $self->{dbh}->prepare($query); |
459
|
|
|
|
|
|
|
|
460
|
|
|
|
|
|
|
# Execute it. |
461
|
|
|
|
|
|
|
|
462
|
0
|
0
|
|
|
|
|
$sth->execute() or return $self->report_error("insert_id failed: $query\n"); |
463
|
|
|
|
|
|
|
|
464
|
|
|
|
|
|
|
# Finish it off. |
465
|
|
|
|
|
|
|
|
466
|
0
|
|
|
|
|
|
$sth->finish(); |
467
|
|
|
|
|
|
|
|
468
|
|
|
|
|
|
|
# Return the new id using and old friend of ours. |
469
|
|
|
|
|
|
|
|
470
|
0
|
|
|
|
|
|
return $self->select_field(-field => 'id', -query => 'select last_insert_id() as id'); |
471
|
|
|
|
|
|
|
|
472
|
|
|
|
|
|
|
} |
473
|
|
|
|
|
|
|
|
474
|
|
|
|
|
|
|
|
475
|
|
|
|
|
|
|
|
476
|
|
|
|
|
|
|
### Selects or inserts data and returns the id. |
477
|
|
|
|
|
|
|
|
478
|
|
|
|
|
|
|
sub select_insert_id { |
479
|
|
|
|
|
|
|
|
480
|
|
|
|
|
|
|
### What we're doing here trying select_item and returning the id if |
481
|
|
|
|
|
|
|
### succesful. Else, trying insert_id, returning the new if successful. |
482
|
|
|
|
|
|
|
### Else, returning zero. |
483
|
|
|
|
|
|
|
|
484
|
|
|
|
|
|
|
# Know thyself |
485
|
|
|
|
|
|
|
|
486
|
0
|
|
|
0
|
1
|
|
my $self = shift; |
487
|
|
|
|
|
|
|
|
488
|
|
|
|
|
|
|
# Get the table, where clause, and set clause sent |
489
|
|
|
|
|
|
|
|
490
|
0
|
|
|
|
|
|
my ($id,$table,$where,$set) = rearrange(['ID','TABLE','WHERE','SET'],@_); |
491
|
|
|
|
|
|
|
|
492
|
|
|
|
|
|
|
# Declare the id variables |
493
|
|
|
|
|
|
|
|
494
|
0
|
|
|
|
|
|
my ($old_id,$new_id); |
495
|
|
|
|
|
|
|
|
496
|
|
|
|
|
|
|
# If the data's already there. |
497
|
|
|
|
|
|
|
|
498
|
0
|
0
|
|
|
|
|
if ($old_id = $self->select_field($id,$table,$where)) { |
499
|
|
|
|
|
|
|
|
500
|
|
|
|
|
|
|
# Return the old id |
501
|
|
|
|
|
|
|
|
502
|
0
|
|
|
|
|
|
return $old_id; |
503
|
|
|
|
|
|
|
|
504
|
|
|
|
|
|
|
} |
505
|
|
|
|
|
|
|
|
506
|
|
|
|
|
|
|
# If we could add the data |
507
|
|
|
|
|
|
|
|
508
|
0
|
0
|
|
|
|
|
if ($new_id = $self->insert_id($table,$set)) { |
509
|
|
|
|
|
|
|
|
510
|
|
|
|
|
|
|
# Return the new id. |
511
|
|
|
|
|
|
|
|
512
|
0
|
|
|
|
|
|
return $new_id; |
513
|
|
|
|
|
|
|
|
514
|
|
|
|
|
|
|
} |
515
|
|
|
|
|
|
|
|
516
|
|
|
|
|
|
|
# If we've come this far, then neither was successful. Indicate this. |
517
|
|
|
|
|
|
|
|
518
|
0
|
|
|
|
|
|
return $self->report_error("select_insert_id failed: id: $id table: $table where: $where set: $set\n"); |
519
|
|
|
|
|
|
|
|
520
|
|
|
|
|
|
|
|
521
|
|
|
|
|
|
|
} |
522
|
|
|
|
|
|
|
|
523
|
|
|
|
|
|
|
|
524
|
|
|
|
|
|
|
|
525
|
|
|
|
|
|
|
### Updates rows of data in a table and returns the number of updated rows. |
526
|
|
|
|
|
|
|
|
527
|
|
|
|
|
|
|
sub update_rows { |
528
|
|
|
|
|
|
|
|
529
|
|
|
|
|
|
|
### What we're doing here is sending the query string to the dbh, and |
530
|
|
|
|
|
|
|
### returning the number of rows affected, unless there's an error. If |
531
|
|
|
|
|
|
|
### there's an error, we'll send back a 0. |
532
|
|
|
|
|
|
|
|
533
|
|
|
|
|
|
|
# Know thyself |
534
|
|
|
|
|
|
|
|
535
|
0
|
|
|
0
|
1
|
|
my $self = shift; |
536
|
|
|
|
|
|
|
|
537
|
|
|
|
|
|
|
# Get the table where clause, and set clause sent |
538
|
|
|
|
|
|
|
|
539
|
0
|
|
|
|
|
|
my ($table,$where,$set) = rearrange(['TABLE','WHERE','SET'],@_); |
540
|
|
|
|
|
|
|
|
541
|
|
|
|
|
|
|
# Get the info for the set and where clause; |
542
|
|
|
|
|
|
|
|
543
|
0
|
|
|
|
|
|
$set = assign_clause($set); |
544
|
0
|
|
|
|
|
|
$where = equals_clause($where); |
545
|
|
|
|
|
|
|
|
546
|
|
|
|
|
|
|
# Form query |
547
|
|
|
|
|
|
|
|
548
|
0
|
|
|
|
|
|
my ($query) = "update $table set $set where $where"; |
549
|
|
|
|
|
|
|
|
550
|
|
|
|
|
|
|
# Declare a statement handle and prepare the query. |
551
|
|
|
|
|
|
|
|
552
|
0
|
|
|
|
|
|
my ($sth) = $self->{dbh}->prepare($query); |
553
|
|
|
|
|
|
|
|
554
|
|
|
|
|
|
|
# Execute it. |
555
|
|
|
|
|
|
|
|
556
|
0
|
0
|
|
|
|
|
$sth->execute() or return $self->report_error("update_rows failed: $query\n"); |
557
|
|
|
|
|
|
|
|
558
|
|
|
|
|
|
|
# Finish it off. |
559
|
|
|
|
|
|
|
|
560
|
0
|
|
|
|
|
|
$sth->finish(); |
561
|
|
|
|
|
|
|
|
562
|
|
|
|
|
|
|
# Return the number of rows affected |
563
|
|
|
|
|
|
|
|
564
|
0
|
|
|
|
|
|
return $sth->rows(); |
565
|
|
|
|
|
|
|
|
566
|
|
|
|
|
|
|
} |
567
|
|
|
|
|
|
|
|
568
|
|
|
|
|
|
|
|
569
|
|
|
|
|
|
|
|
570
|
|
|
|
|
|
|
### Deletes rows from a table and returns the number of deleted rows. |
571
|
|
|
|
|
|
|
|
572
|
|
|
|
|
|
|
sub delete_rows { |
573
|
|
|
|
|
|
|
|
574
|
|
|
|
|
|
|
### What we're doing here is sending the query string to the dbh, and |
575
|
|
|
|
|
|
|
### returning the number of rows affected, unless there's an error. If |
576
|
|
|
|
|
|
|
### there's an error, we'll send back a 0. |
577
|
|
|
|
|
|
|
|
578
|
|
|
|
|
|
|
# Know thyself |
579
|
|
|
|
|
|
|
|
580
|
0
|
|
|
0
|
1
|
|
my $self = shift; |
581
|
|
|
|
|
|
|
|
582
|
|
|
|
|
|
|
# Get the table and where clause criteria sent |
583
|
|
|
|
|
|
|
|
584
|
0
|
|
|
|
|
|
my ($table,$where) = rearrange(['TABLE','WHERE'],@_); |
585
|
|
|
|
|
|
|
|
586
|
|
|
|
|
|
|
# Get the info where clause; |
587
|
|
|
|
|
|
|
|
588
|
0
|
|
|
|
|
|
$where = equals_clause($where); |
589
|
|
|
|
|
|
|
|
590
|
|
|
|
|
|
|
# Form query |
591
|
|
|
|
|
|
|
|
592
|
0
|
|
|
|
|
|
my ($query) = "delete from $table where $where"; |
593
|
|
|
|
|
|
|
|
594
|
|
|
|
|
|
|
# Declare a statement handle and prepare the query. |
595
|
|
|
|
|
|
|
|
596
|
0
|
|
|
|
|
|
my ($sth) = $self->{dbh}->prepare($query); |
597
|
|
|
|
|
|
|
|
598
|
|
|
|
|
|
|
# Execute it. |
599
|
|
|
|
|
|
|
|
600
|
0
|
0
|
|
|
|
|
$sth->execute() or return $self->report_error("delete_rows failed: $query\n"); |
601
|
|
|
|
|
|
|
|
602
|
|
|
|
|
|
|
# Finish it off. |
603
|
|
|
|
|
|
|
|
604
|
0
|
|
|
|
|
|
$sth->finish(); |
605
|
|
|
|
|
|
|
|
606
|
|
|
|
|
|
|
# Return the number of rows affected |
607
|
|
|
|
|
|
|
|
608
|
0
|
|
|
|
|
|
return $sth->rows(); |
609
|
|
|
|
|
|
|
|
610
|
|
|
|
|
|
|
} |
611
|
|
|
|
|
|
|
|
612
|
|
|
|
|
|
|
|
613
|
|
|
|
|
|
|
|
614
|
|
|
|
|
|
|
### Reports a failed routine if PrintError is enabled in the dbh. |
615
|
|
|
|
|
|
|
|
616
|
|
|
|
|
|
|
sub report_error { |
617
|
|
|
|
|
|
|
|
618
|
|
|
|
|
|
|
# Know thyself |
619
|
|
|
|
|
|
|
|
620
|
0
|
|
|
0
|
1
|
|
my $self = shift; |
621
|
|
|
|
|
|
|
|
622
|
|
|
|
|
|
|
# If DBI isn't printing errors neither are we. |
623
|
|
|
|
|
|
|
|
624
|
0
|
0
|
|
|
|
|
return unless $self->{dbh}->{PrintError}; |
625
|
|
|
|
|
|
|
|
626
|
|
|
|
|
|
|
# Get the failure message |
627
|
|
|
|
|
|
|
|
628
|
0
|
|
|
|
|
|
my $message = shift; |
629
|
|
|
|
|
|
|
|
630
|
|
|
|
|
|
|
# Tell the user what's up. |
631
|
|
|
|
|
|
|
|
632
|
0
|
|
|
|
|
|
print STDERR $message; |
633
|
|
|
|
|
|
|
|
634
|
0
|
|
|
|
|
|
return; |
635
|
|
|
|
|
|
|
|
636
|
|
|
|
|
|
|
} |
637
|
|
|
|
|
|
|
|
638
|
|
|
|
|
|
|
$Relations::Abstract::VERSION; |
639
|
|
|
|
|
|
|
|
640
|
|
|
|
|
|
|
__END__ |