Repository navigation
Expand file tree
/
Copy pathDBSQLArray.pm
More file actions
344 lines (222 loc) · 7.95 KB
/
Copy pathDBSQLArray.pm
File metadata and controls
344 lines (222 loc) · 7.95 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
package DBSQLArray;
# DBSQLArray - ties persistent perl object arrays to the database backend
# $Id: DBSQLArray.pm,v 1.7 2008-04-10 14:42:56 paarmann Exp $
use strict;
use warnings;
use base qw( Tie::Array );
use DBObject qw( DB_ARRAY_OF_SCALARS DB_ARRAY_OF_OBJECTS );
1;
=pod
=head1 NAME
DBSQLArray - ties persistent perl object arrays to the database backend
=head1 DESCRIPTION
This module provides methods for tying array attributes of DBObjects to the
database backend. It supplies all required methods to tie an array to a package
based on the Tie::Array package.
Even though named DB/SQL/Array it will make use of the database methods provided
by the backend of the DBMaster and thus be portable to other database implementations.
It provides DELETE and EXISTS methods, and implementations of PUSH, POP, SHIFT,
UNSHIFT, SPLICE and CLEAR in terms of basic FETCH, STORE, FETCHSIZE, STORESIZE.
=head1 METHODS
=over 4
=item * B<TIEARRAY> I<parent_object>, I<attribute_key>
This method is invoked by the command tie @array, DBSQLArray, I<parent_object>,
I<attribute_key> and associates an array attribute with it's database backend.
=cut
sub TIEARRAY {
my ($class, $parent, $key) = @_;
my $self = { '_parent' => $parent,
'_attribute' => $key,
'_array' => [],
};
bless $self, $class;
my $id = $parent->_id;
# load array from database if applicable
if ($parent->attributes->{$key}->[0] == DB_ARRAY_OF_SCALARS) {
# get array content
my $data = $self->backend->get_rows( $self->table, [ '_value' ], "_source_id=$id",
{ 'sort_by' => [ '_array_index' ] } );
foreach (@$data) {
push(@{$self->{'_array'}}, $_->[0]);
}
}
elsif ($parent->attributes->{$key}->[0] == DB_ARRAY_OF_OBJECTS) {
# get object array class
my ($refclass) = ($parent->attributes->{$key}->[1] =~ /\w+::(\w+)/);
# get array content
my $data = $self->backend->get_rows( $self->table, [ '_target_db', '_target_id' ], "_source_id=$id",
{ 'sort_by' => [ '_array_index' ] } );
foreach (@$data) {
push(@{$self->{'_array'}}, [ $_->[0], $refclass, $_->[1] ]);
}
}
else {
die "Unknown attribute type '".$parent->attributes->{$key}->[0]."'.";
}
return $self;
}
=pod
=item * B<FETCH> I<this>, I<index>
Retrieve the datum in I<index> for the tied array associated with object I<this>.
=cut
sub FETCH {
my ($self, $index) = @_;
# in the case of object arrays check if object has to be retrieved first
if ($self->type == DB_ARRAY_OF_OBJECTS and
ref($self->{'_array'}->[$index]) ne $self->ref_type) {
# fetch object and store it in the array
my $object = $self->{'_parent'}->_master->fetch_by_ref( @{$self->{'_array'}->[$index]} );
unless (ref $object) {
$self->SPLICE($index,1);
warn "Invalid object reference in array: ".join(', ',@{$self->{'_array'}->[$index]}).
", deleted entry _source_id=".$self->{'_parent'}->_id." and _array_index=$index.";
}
$self->{'_array'}->[$index] = $object;
}
return $self->{'_array'}->[$index];
}
=pod
=item * B<STORE> I<this>, I<index>, I<value>
Store datum I<value> into I<index> for the tied array associated with object I<this>.
If this makes the array larger then class's mapping of undef should be returned for
new positions. Storing a value in the array automatically updates the corresponding
entries in the database.
=cut
sub STORE {
my( $self, $index, $value ) = @_;
my $parent = $self->{'_parent'};
my $key = $self->{'_attribute'};
my $id = $parent->_id;
# delete entry ($value = undef)
unless (defined $value) {
$self->backend->delete_rows( $self->table, "_source_id=$id and _array_index=$index" );
return undef;
}
# set entry - check value first
if ($self->type == DB_ARRAY_OF_OBJECTS and ref($value) ne $self->ref_type) {
die "Mismatched object class at position $index for array attribute '$key': '".$value."'.";
}
elsif ($self->type == DB_ARRAY_OF_SCALARS and ref($value)) {
die "Mismatched value at position $index for array attribute '$key': '$value'.";
}
# update existing entry?
if (exists ($self->{'_array'}->[$index])) {
# array of objects
if ($self->type == DB_ARRAY_OF_OBJECTS) {
my ($db_id, $obj_id) = $parent->_master->translate_ref_to_ids($value);
$self->backend->update_row( $self->table, { '_target_db' => $db_id, '_target_id' => $obj_id },
"_source_id=$id and _array_index=$index" );
}
# array of scalars
elsif ($self->type == DB_ARRAY_OF_SCALARS) {
$self->backend->update_row( $self->table, { '_value' => $value },
"_source_id=$id and _array_index=$index" );
}
}
# new entry
else {
# array of objects
if ($parent->attributes->{$key}->[0] == DB_ARRAY_OF_OBJECTS) {
my ($db_id, $obj_id) = $parent->_master->translate_ref_to_ids($value);
$self->backend->insert_row( $self->table, { '_array_index' => $index, '_source_id' => $id,
'_target_id' => $obj_id, '_target_db' => $db_id, } );
}
# array of scalars
elsif ($parent->attributes->{$key}->[0] == DB_ARRAY_OF_SCALARS) {
$self->backend->insert_row( $self->table, { '_array_index' => $index,
'_source_id' => $id,
'_value' => $value,
});
}
}
$self->{'_array'}->[$index] = $value;
}
=pod
=item * B<FETCHSIZE> I<this>
Returns the total number of items in the tied array associated with object I<this>.
(Equivalent to scalar(@array)).
=cut
sub FETCHSIZE {
return scalar @{$_[0]->{'_array'}};
}
=pod
=item * B<STORESIZE> I<this>, I<count>
Sets the total number of items in the tied array associated with object I<this> to be
I<count>. If this makes the array larger then class's mapping of undef should be returned
for new positions. If the array becomes smaller then entries beyond I<count> are removed
and their rows delete from the database table.
=cut
sub STORESIZE {
my ($self, $size) = @_;
# is the array becoming smaller?
if ($size < $self->FETCHSIZE) {
# delete from array
foreach ( 1 .. $self->FETCHSIZE() - $size ) {
pop @{$self->{'_array'}};
}
# delete from database
my $id = $self->{'_parent'}->_id;
$self->backend->delete_rows( $self->table, "_source_id=$id and _array_index>=$size" );
}
}
=pod
=item * B<EXISTS> I<this>, I<key>
Return true if the element at index I<key> exists in the tied array I<this>.
=cut
sub EXISTS {
my ($self, $index) = @_;
return exists $self->{'_array'}->[$index];
}
=pod
=item * B<DELETE> I<this>, I<key>
Delete the element at index I<key> exists in the tied array I<this>.
This method will delete the corresponding row from the database table.
=cut
sub DELETE {
my ($self, $index) = @_;
return $self->STORE( $index, undef );
}
=pod
=item * B<CLEAR> I<this>
Clear (remove, delete, ...) all values from the tied array I<this>.
This method will delete all entries from the array database table.
=cut
sub CLEAR {
$_[0]->backend->delete_rows( $_[0]->table, '_source_id='.$_[0]->{'_parent'}->_id );
@{$_[0]->{_array}} = ();
}
=pod
=item * B<backend>
Helper method to get the backend from the DBMaster stored in the parent object
=cut
sub backend {
return $_[0]->{'_parent'}->_master->backend;
}
=pod
=item * B<database>
Helper method to get the name of the database from the DBMaster stored in the parent object
=cut
sub database {
return $_[0]->{'_parent'}->_master->database;
}
=pod
=item * B<table>
Helper method to get the name of the array table from the parent object
=cut
sub table {
return $_[0]->{'_parent'}->_table().'_'.$_[0]->{'_attribute'};
}
=pod
=item * B<type>
Helper method to get the type of the array attribute from the parent object
=cut
sub type {
return $_[0]->{'_parent'}->attributes->{ $_[0]->{'_attribute'} }->[0];
}
=pod
=item * B<ref_type>
Helper method to get the perl package name of the array attribute from the parent object
=cut
sub ref_type {
return $_[0]->{'_parent'}->attributes->{ $_[0]->{'_attribute'} }->[1];
}