line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
#!/usr/bin/perl |
2
|
|
|
|
|
|
|
package Persistent::Hash::Storage::MySQL; |
3
|
|
|
|
|
|
|
|
4
|
1
|
|
|
1
|
|
975
|
use strict; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
47
|
|
5
|
1
|
|
|
1
|
|
6
|
use Carp qw(croak); |
|
1
|
|
|
|
|
3
|
|
|
1
|
|
|
|
|
62
|
|
6
|
|
|
|
|
|
|
|
7
|
1
|
|
|
1
|
|
1709
|
use DBI; |
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
8
|
|
|
|
|
|
|
use Data::Dumper; |
9
|
|
|
|
|
|
|
|
10
|
|
|
|
|
|
|
use vars qw($VERSION); |
11
|
|
|
|
|
|
|
|
12
|
|
|
|
|
|
|
$VERSION = '0.1'; |
13
|
|
|
|
|
|
|
|
14
|
|
|
|
|
|
|
sub LoadObjectInfo |
15
|
|
|
|
|
|
|
{ |
16
|
|
|
|
|
|
|
my $classname = shift; |
17
|
|
|
|
|
|
|
my $object_package = shift; |
18
|
|
|
|
|
|
|
my $id = shift; |
19
|
|
|
|
|
|
|
|
20
|
|
|
|
|
|
|
croak "Attempt to call LoadObjectInfo() as a function call" if not defined $classname; |
21
|
|
|
|
|
|
|
croak "No type passed to LoadObjectInfo()" if not defined $object_package; |
22
|
|
|
|
|
|
|
croak "No id passed to LoadObjectInfo()" if not defined $id; |
23
|
|
|
|
|
|
|
|
24
|
|
|
|
|
|
|
my $dbh = $object_package->DatabaseHandle(); |
25
|
|
|
|
|
|
|
croak "Could not obtain a database handle!" if not defined $dbh; |
26
|
|
|
|
|
|
|
|
27
|
|
|
|
|
|
|
my $info_table = $object_package->INFO_TABLE(); |
28
|
|
|
|
|
|
|
|
29
|
|
|
|
|
|
|
#Load the object informations |
30
|
|
|
|
|
|
|
my $load_info_query = "SELECT |
31
|
|
|
|
|
|
|
type, |
32
|
|
|
|
|
|
|
time_created, |
33
|
|
|
|
|
|
|
time_modified |
34
|
|
|
|
|
|
|
FROM |
35
|
|
|
|
|
|
|
$info_table |
36
|
|
|
|
|
|
|
WHERE |
37
|
|
|
|
|
|
|
id = ?"; |
38
|
|
|
|
|
|
|
my $load_info_sth = $dbh->prepare_cached($load_info_query) || die "Could not prepare $load_info_query: $DBI::errstr"; |
39
|
|
|
|
|
|
|
$load_info_sth->execute($id) || die "Could not execute $load_info_query: $DBI::errstr"; |
40
|
|
|
|
|
|
|
|
41
|
|
|
|
|
|
|
my ($type,$time_created,$time_modified) = $load_info_sth->fetchrow_array(); |
42
|
|
|
|
|
|
|
$load_info_sth->finish(); |
43
|
|
|
|
|
|
|
|
44
|
|
|
|
|
|
|
my $object_info = undef; |
45
|
|
|
|
|
|
|
|
46
|
|
|
|
|
|
|
if(defined $type) |
47
|
|
|
|
|
|
|
{ |
48
|
|
|
|
|
|
|
$object_info = {}; |
49
|
|
|
|
|
|
|
$object_info->{type} = $type, |
50
|
|
|
|
|
|
|
$object_info->{time_created} = $time_created; |
51
|
|
|
|
|
|
|
$object_info->{time_modified} = $time_modified; |
52
|
|
|
|
|
|
|
} |
53
|
|
|
|
|
|
|
|
54
|
|
|
|
|
|
|
return $object_info; |
55
|
|
|
|
|
|
|
} |
56
|
|
|
|
|
|
|
|
57
|
|
|
|
|
|
|
sub LoadObjectData |
58
|
|
|
|
|
|
|
{ |
59
|
|
|
|
|
|
|
my $classname = shift; |
60
|
|
|
|
|
|
|
croak "Attempt to call LoadObjectData() as a function call" if not defined $classname; |
61
|
|
|
|
|
|
|
|
62
|
|
|
|
|
|
|
my $object = shift; |
63
|
|
|
|
|
|
|
croak "No object passed to LoadObjectData()" if not defined $object; |
64
|
|
|
|
|
|
|
croak "Wrong object side!" if tied %$object; |
65
|
|
|
|
|
|
|
|
66
|
|
|
|
|
|
|
return undef if not defined $object->{_object_id}; |
67
|
|
|
|
|
|
|
|
68
|
|
|
|
|
|
|
my $data_table = $object->DATA_TABLE(); |
69
|
|
|
|
|
|
|
|
70
|
|
|
|
|
|
|
my $dbh = $object->DatabaseHandle; |
71
|
|
|
|
|
|
|
croak "Could not obtain database handle!" if not defined $dbh; |
72
|
|
|
|
|
|
|
|
73
|
|
|
|
|
|
|
#Load the object data |
74
|
|
|
|
|
|
|
my $load_data_query = "SELECT |
75
|
|
|
|
|
|
|
data |
76
|
|
|
|
|
|
|
FROM |
77
|
|
|
|
|
|
|
$data_table |
78
|
|
|
|
|
|
|
WHERE |
79
|
|
|
|
|
|
|
id = ?"; |
80
|
|
|
|
|
|
|
my $load_data_sth = $dbh->prepare_cached($load_data_query) || die "Could not prepare $load_data_query: $DBI::errstr"; |
81
|
|
|
|
|
|
|
$load_data_sth->execute($object->{_object_id}) || die "Could not execute $load_data_query: $DBI::errstr"; |
82
|
|
|
|
|
|
|
my $data = $load_data_sth->fetchrow(); |
83
|
|
|
|
|
|
|
$load_data_sth->finish(); |
84
|
|
|
|
|
|
|
|
85
|
|
|
|
|
|
|
|
86
|
|
|
|
|
|
|
$data = eval "+"."$data"; |
87
|
|
|
|
|
|
|
|
88
|
|
|
|
|
|
|
|
89
|
|
|
|
|
|
|
return $data; |
90
|
|
|
|
|
|
|
} |
91
|
|
|
|
|
|
|
|
92
|
|
|
|
|
|
|
sub LoadObjectIndex |
93
|
|
|
|
|
|
|
{ |
94
|
|
|
|
|
|
|
my $classname = shift; |
95
|
|
|
|
|
|
|
croak "Attempt to call LoadObjectIndex() as a function call" if not defined $classname; |
96
|
|
|
|
|
|
|
|
97
|
|
|
|
|
|
|
my $object = shift; |
98
|
|
|
|
|
|
|
croak "No object passed to LoadObjectIndex()" if not defined $object; |
99
|
|
|
|
|
|
|
croak "Wrong object side!" if tied %$object; |
100
|
|
|
|
|
|
|
|
101
|
|
|
|
|
|
|
my $dbh = $object->DatabaseHandle(); |
102
|
|
|
|
|
|
|
croak "Could not obtain database handle" if not defined $dbh; |
103
|
|
|
|
|
|
|
|
104
|
|
|
|
|
|
|
my $index_table = $object->INDEX_TABLE(); |
105
|
|
|
|
|
|
|
my $index_fields = $object->INDEX_FIELDS(); |
106
|
|
|
|
|
|
|
|
107
|
|
|
|
|
|
|
my $loaded_fields = {}; |
108
|
|
|
|
|
|
|
|
109
|
|
|
|
|
|
|
if(@$index_fields) |
110
|
|
|
|
|
|
|
{ |
111
|
|
|
|
|
|
|
my $load_index_query = "SELECT |
112
|
|
|
|
|
|
|
".(join ',', @$index_fields)." |
113
|
|
|
|
|
|
|
FROM |
114
|
|
|
|
|
|
|
$index_table |
115
|
|
|
|
|
|
|
WHERE |
116
|
|
|
|
|
|
|
id = ?"; |
117
|
|
|
|
|
|
|
my $load_index_sth = $dbh->prepare_cached($load_index_query) || die "Could not prepare $load_index_query: $DBI::errstr"; |
118
|
|
|
|
|
|
|
$load_index_sth->execute($object->{_object_id}) || die "Could not execute $load_index_query: $DBI::errstr"; |
119
|
|
|
|
|
|
|
$loaded_fields = $load_index_sth->fetchrow_hashref(); |
120
|
|
|
|
|
|
|
$load_index_sth->finish(); |
121
|
|
|
|
|
|
|
} |
122
|
|
|
|
|
|
|
|
123
|
|
|
|
|
|
|
return $loaded_fields; |
124
|
|
|
|
|
|
|
} |
125
|
|
|
|
|
|
|
|
126
|
|
|
|
|
|
|
sub DeleteObject |
127
|
|
|
|
|
|
|
{ |
128
|
|
|
|
|
|
|
my $classname = shift; |
129
|
|
|
|
|
|
|
my $self = shift; |
130
|
|
|
|
|
|
|
|
131
|
|
|
|
|
|
|
my $object = tied %$self if tied %$self; |
132
|
|
|
|
|
|
|
croak "Wrong object side!" if not defined $object; |
133
|
|
|
|
|
|
|
|
134
|
|
|
|
|
|
|
croak "Attempt to call DeleteObject() as a function call." if not defined $classname; |
135
|
|
|
|
|
|
|
croak "No object passed to DeleteObject()" if not defined $object; |
136
|
|
|
|
|
|
|
|
137
|
|
|
|
|
|
|
return undef if not $self->Id(); |
138
|
|
|
|
|
|
|
|
139
|
|
|
|
|
|
|
my $dbh = $self->DatabaseHandle(); |
140
|
|
|
|
|
|
|
croak "Could not obtain a database handle!" if not defined $dbh; |
141
|
|
|
|
|
|
|
|
142
|
|
|
|
|
|
|
|
143
|
|
|
|
|
|
|
my $info_table = $object->INFO_TABLE(); |
144
|
|
|
|
|
|
|
my $data_table = $object->DATA_TABLE(); |
145
|
|
|
|
|
|
|
my $index_table = $object->INDEX_TABLE(); |
146
|
|
|
|
|
|
|
my $hash_id = $object->{_object_id}; |
147
|
|
|
|
|
|
|
|
148
|
|
|
|
|
|
|
my $time = time(); |
149
|
|
|
|
|
|
|
|
150
|
|
|
|
|
|
|
my $delete_info_query = "DELETE FROM $info_table WHERE id = ?"; |
151
|
|
|
|
|
|
|
my $delete_info_sth = $dbh->prepare_cached($delete_info_query) || die "Could not prepare $delete_info_query: $DBI::errstr"; |
152
|
|
|
|
|
|
|
$delete_info_sth->execute($hash_id) || die "Could not execute $delete_info_query: $DBI::errstr"; |
153
|
|
|
|
|
|
|
|
154
|
|
|
|
|
|
|
my $delete_data_query = "DELETE FROM $data_table WHERE id = ?"; |
155
|
|
|
|
|
|
|
my $delete_data_sth = $dbh->prepare_cached($delete_data_query) || die "Could not prepare $delete_data_query: $DBI::errstr"; |
156
|
|
|
|
|
|
|
$delete_data_sth->execute($hash_id) || die "Could not execute $delete_info_query: $DBI::errstr"; |
157
|
|
|
|
|
|
|
|
158
|
|
|
|
|
|
|
my $delete_index_query = "DELETE FROM $index_table WHERE id = ?"; |
159
|
|
|
|
|
|
|
my $delete_index_sth = $dbh->prepare_cached($delete_index_query) || die "Could not prepare $delete_index_query: $DBI::errstr"; |
160
|
|
|
|
|
|
|
$delete_index_sth->execute($hash_id) || die "Could not execute $delete_index_query: $DBI::errstr"; |
161
|
|
|
|
|
|
|
|
162
|
|
|
|
|
|
|
return 1; |
163
|
|
|
|
|
|
|
} |
164
|
|
|
|
|
|
|
|
165
|
|
|
|
|
|
|
sub InsertObject |
166
|
|
|
|
|
|
|
{ |
167
|
|
|
|
|
|
|
my $classname = shift; |
168
|
|
|
|
|
|
|
my $self = shift; |
169
|
|
|
|
|
|
|
|
170
|
|
|
|
|
|
|
my $object = tied %$self if tied %$self; |
171
|
|
|
|
|
|
|
croak "Wrong object side!" if not defined $object; |
172
|
|
|
|
|
|
|
|
173
|
|
|
|
|
|
|
croak "Attempt to call InsertObject() as a function call." if not defined $classname; |
174
|
|
|
|
|
|
|
croak "No object passed to InsertObject()" if not defined $object; |
175
|
|
|
|
|
|
|
|
176
|
|
|
|
|
|
|
my $dbh = $self->DatabaseHandle(); |
177
|
|
|
|
|
|
|
croak "Could not obtain a database handle!" if not defined $dbh; |
178
|
|
|
|
|
|
|
|
179
|
|
|
|
|
|
|
my $info_table = $self->INFO_TABLE(); |
180
|
|
|
|
|
|
|
my $data_table = $self->DATA_TABLE(); |
181
|
|
|
|
|
|
|
my $index_table = $self->INDEX_TABLE(); |
182
|
|
|
|
|
|
|
my $time = time(); |
183
|
|
|
|
|
|
|
|
184
|
|
|
|
|
|
|
my $object_data = $object->{_data}; |
185
|
|
|
|
|
|
|
my $object_index_data = $object->{_index_data}; |
186
|
|
|
|
|
|
|
my $index_fields = $object->INDEX_FIELDS(); |
187
|
|
|
|
|
|
|
|
188
|
|
|
|
|
|
|
#Insert the info record |
189
|
|
|
|
|
|
|
my $insert_info_query = "INSERT INTO |
190
|
|
|
|
|
|
|
$info_table(type,time_created,time_modified) |
191
|
|
|
|
|
|
|
values(?,?,?)"; |
192
|
|
|
|
|
|
|
my $insert_info_sth = $dbh->prepare_cached($insert_info_query) || die "Could not prepare $insert_info_query: $DBI::errstr"; |
193
|
|
|
|
|
|
|
$insert_info_sth->execute($self->Type(), $time,$time) || die "Could not execute $insert_info_query: $DBI::errstr"; |
194
|
|
|
|
|
|
|
my $object_id = $insert_info_sth->{'mysql_insertid'}; |
195
|
|
|
|
|
|
|
$insert_info_sth->finish(); |
196
|
|
|
|
|
|
|
|
197
|
|
|
|
|
|
|
|
198
|
|
|
|
|
|
|
#Insert the data record |
199
|
|
|
|
|
|
|
if($object_data) |
200
|
|
|
|
|
|
|
{ |
201
|
|
|
|
|
|
|
my $insert_data_query = "INSERT INTO |
202
|
|
|
|
|
|
|
$data_table(id,data) |
203
|
|
|
|
|
|
|
values(?,?)"; |
204
|
|
|
|
|
|
|
my $insert_data_sth = $dbh->prepare_cached($insert_data_query) || die "Could not prepare $insert_data_query: $DBI::errstr"; |
205
|
|
|
|
|
|
|
$insert_data_sth->execute( |
206
|
|
|
|
|
|
|
$object_id, |
207
|
|
|
|
|
|
|
$object->_FlattenData($object_data)||"{}") || die "Could not execute $insert_data_query: $DBI::errstr"; |
208
|
|
|
|
|
|
|
$insert_data_sth->finish(); |
209
|
|
|
|
|
|
|
} |
210
|
|
|
|
|
|
|
|
211
|
|
|
|
|
|
|
#Insert the index record |
212
|
|
|
|
|
|
|
if(keys %$object_index_data) |
213
|
|
|
|
|
|
|
{ |
214
|
|
|
|
|
|
|
my $index_values = [(map $object->{_index_data}->{$_}, @$index_fields)]; |
215
|
|
|
|
|
|
|
|
216
|
|
|
|
|
|
|
my $insert_index_query = "INSERT INTO |
217
|
|
|
|
|
|
|
$index_table(id,".(join ',', @$index_fields).") |
218
|
|
|
|
|
|
|
values(?,".(join ',', map('?', @$index_fields)).")"; |
219
|
|
|
|
|
|
|
my $insert_index_sth = $dbh->prepare($insert_index_query) || die "Could not prepare $insert_index_query: $DBI::errstr"; |
220
|
|
|
|
|
|
|
$insert_index_sth->execute( |
221
|
|
|
|
|
|
|
$object_id, |
222
|
|
|
|
|
|
|
@$index_values) || die "Could not execute $insert_index_query: $DBI::errstr"; |
223
|
|
|
|
|
|
|
$insert_index_sth->finish(); |
224
|
|
|
|
|
|
|
} |
225
|
|
|
|
|
|
|
|
226
|
|
|
|
|
|
|
return $object_id; |
227
|
|
|
|
|
|
|
} |
228
|
|
|
|
|
|
|
|
229
|
|
|
|
|
|
|
sub UpdateObject |
230
|
|
|
|
|
|
|
{ |
231
|
|
|
|
|
|
|
my $classname = shift; |
232
|
|
|
|
|
|
|
my $self = shift; |
233
|
|
|
|
|
|
|
|
234
|
|
|
|
|
|
|
my $object = tied %$self if tied %$self; |
235
|
|
|
|
|
|
|
croak "Wrong object side!" if not defined $object; |
236
|
|
|
|
|
|
|
|
237
|
|
|
|
|
|
|
croak "Attempt to call UpdateObject() as a function call." if not defined $classname; |
238
|
|
|
|
|
|
|
croak "No object passed to UpdateObject()" if not defined $object; |
239
|
|
|
|
|
|
|
|
240
|
|
|
|
|
|
|
my $dbh = $self->DatabaseHandle(); |
241
|
|
|
|
|
|
|
croak "Could not obtain a database handle!" if not defined $dbh; |
242
|
|
|
|
|
|
|
|
243
|
|
|
|
|
|
|
my $info_table = $object->INFO_TABLE(); |
244
|
|
|
|
|
|
|
my $data_table = $object->DATA_TABLE(); |
245
|
|
|
|
|
|
|
my $index_table = $object->INDEX_TABLE(); |
246
|
|
|
|
|
|
|
my $time = time(); |
247
|
|
|
|
|
|
|
|
248
|
|
|
|
|
|
|
my $object_data = $object->{_data}; |
249
|
|
|
|
|
|
|
my $object_index_data = $object->{_index_data}; |
250
|
|
|
|
|
|
|
my $index_fields = $object->INDEX_FIELDS(); |
251
|
|
|
|
|
|
|
|
252
|
|
|
|
|
|
|
#Update the info record. |
253
|
|
|
|
|
|
|
my $update_info_query = "UPDATE $info_table |
254
|
|
|
|
|
|
|
SET |
255
|
|
|
|
|
|
|
type = ?, |
256
|
|
|
|
|
|
|
time_created = ?, |
257
|
|
|
|
|
|
|
time_modified = ? |
258
|
|
|
|
|
|
|
WHERE |
259
|
|
|
|
|
|
|
id = ?"; |
260
|
|
|
|
|
|
|
|
261
|
|
|
|
|
|
|
|
262
|
|
|
|
|
|
|
my $update_info_sth = $dbh->prepare_cached($update_info_query) || die "Could not prepare $update_info_query: $DBI::errstr"; |
263
|
|
|
|
|
|
|
|
264
|
|
|
|
|
|
|
$update_info_sth->execute( |
265
|
|
|
|
|
|
|
$self->Type(), |
266
|
|
|
|
|
|
|
$self->TimeCreated(), |
267
|
|
|
|
|
|
|
$time, |
268
|
|
|
|
|
|
|
$object->{_object_id}, |
269
|
|
|
|
|
|
|
) || die "Could not execute $update_info_query: $DBI::errstr"; |
270
|
|
|
|
|
|
|
$update_info_sth->finish(); |
271
|
|
|
|
|
|
|
|
272
|
|
|
|
|
|
|
#update the data record |
273
|
|
|
|
|
|
|
|
274
|
|
|
|
|
|
|
if($object_data && $object->{_data_dirty}) |
275
|
|
|
|
|
|
|
{ |
276
|
|
|
|
|
|
|
my $update_data_query = "UPDATE $data_table |
277
|
|
|
|
|
|
|
SET |
278
|
|
|
|
|
|
|
data = ? |
279
|
|
|
|
|
|
|
WHERE |
280
|
|
|
|
|
|
|
id = ?"; |
281
|
|
|
|
|
|
|
|
282
|
|
|
|
|
|
|
|
283
|
|
|
|
|
|
|
my $update_data_sth = $dbh->prepare_cached($update_data_query) || die "Could not prepare $update_data_query: $DBI::errstr"; |
284
|
|
|
|
|
|
|
$update_data_sth->execute( |
285
|
|
|
|
|
|
|
$object->_FlattenData($object_data), |
286
|
|
|
|
|
|
|
$object->{_object_id}) || die "Could not execute $update_data_query: $DBI::errstr"; |
287
|
|
|
|
|
|
|
|
288
|
|
|
|
|
|
|
$update_data_sth->finish(); |
289
|
|
|
|
|
|
|
} |
290
|
|
|
|
|
|
|
|
291
|
|
|
|
|
|
|
#update the index record |
292
|
|
|
|
|
|
|
if($object_index_data && $object->{_index_dirty}) |
293
|
|
|
|
|
|
|
{ |
294
|
|
|
|
|
|
|
my $index_values = [(map $object->{_index_data}->{$_}, @$index_fields)]; |
295
|
|
|
|
|
|
|
my $update_index_query = "UPDATE $index_table |
296
|
|
|
|
|
|
|
SET |
297
|
|
|
|
|
|
|
".(join ',', map("$_ = ?", (@$index_fields)))." |
298
|
|
|
|
|
|
|
WHERE id = ?"; |
299
|
|
|
|
|
|
|
my $update_index_sth = $dbh->prepare($update_index_query) || die "Could not prepare $update_index_query: $DBI::errstr"; |
300
|
|
|
|
|
|
|
$update_index_sth->execute( |
301
|
|
|
|
|
|
|
@$index_values, |
302
|
|
|
|
|
|
|
$object->{_object_id}) || die "Could not execute $update_index_query: $DBI::errstr"; |
303
|
|
|
|
|
|
|
$update_index_sth->finish(); |
304
|
|
|
|
|
|
|
} |
305
|
|
|
|
|
|
|
|
306
|
|
|
|
|
|
|
return $object->{_object_id}; |
307
|
|
|
|
|
|
|
} |
308
|
|
|
|
|
|
|
|
309
|
|
|
|
|
|
|
666; |