Persistent/StructTemplate.pm untested

filter: Statementstmt 0.0% 0 / 167 Branchbran 0.0% 0 / 62 Conditioncond 0.0% 0 / 35 MC/DCmcdc 0.0% 0 / 21 Subroutinesub 0.0% 0 / 13 Podpod 0.0% 0 / 9 totaltotal 0.0% 0 / 307 CC 32 SCAR 33.1
CC
32
Cov
0%
load_into
56.0
save
30.0
delete
30.0
CRAP 27.5
1 #
2 # Class::Persistent::StructTemplate - Persistent implementation of Class::StructTemplate. Uses a plugin to enable persistence through various interfaces.
3 # $Id$
4 #
5 # Copyright (C) 2000 by Heiko Wundram.
6 # All rights reserved.
7 #
8 # This program is free software; you can redistribute and/or modify it under the same terms as Perl itself.
9 #
10 # $Log$
11 #
12  
13 package Class::Persistent::StructTemplate;
14 $Class::Persistent::StructTemplate::VERSION = '0.01';
15  
16 require Exporter;
17 @Class::Persistent::StructTemplate::ISA = qw(Exporter Class::StructTemplate);
18 @Class::Persistent::StructTemplate::EXPORT = qw(attributes);
19  
20 use Class::StructTemplate qw();
21  
22 use Carp;
23  
24 use Data::Dumper;
25  
26 sub attributes
27 {
28 my ($pkg) = ref $_[0] ? (${ shift() }) : caller();
29  
30 if( @_ < 2 )
31 {
32 confess "Need at least one attribute to assign to new class!";
33 }
34  
35 my $plugin = shift();
36 my $plugin_parms = shift();
37  
38 eval "use $plugin;";
39 confess "Couldn't load storage plugin $plugin (error: $@)!" if $@;
40 (${"${pkg}::_PLUGIN"} = new $plugin (@$plugin_parms)) or confess "Couldn't create storage plugin $plugin!";
41  
42 Class::StructTemplate::attributes(\$pkg,@_) or confess "Couldn't create class $pkg!";
43  
44 ${"${pkg}::_max_id"} = ${"${pkg}::_PLUGIN"}->get_max_id($pkg);
45  
46 _define_load($pkg) or confess "Couldn't create load-constructor for class $pkg!";
47  
48 return 1;
49 }
50  
51 sub _define_load
52 {
53 if( @_ != 1 )
54 {
55 confess "_define_load can only be called with one argument!";
56 }
57  
58 my ($pkg) = @_;
59  
60 my $accs = qq|
61 package $pkg;
62  
63 sub load
64 {
65 my (\$class,\$type) = \@_;
66 \$class = ref \$class ? ref \$class : \$class;
67 my (\@self);
68  
69 \@self = \$class->load_into(\$type);
70  
71 return \@self;
72 }|;
73  
74 eval $accs;
75  
76 croak $@ if $@;
77 return !$@;
78 }
79  
80 sub load_into
81 {
82 if( @_ != 2 )
83 {
84 confess "load_into can only be called with one arguments!";
85 }
86  
87 my ($class,$type) = @_;
88 my $done = 0;
89 my $pkg = ref $class ? ref $class : $class;
90 my $self;
91 my @ret_val = ();
92  
93 if( ref $class )
94 {
95 $done = ${"${pkg}::_PLUGIN"}->load($class,$pkg,$type);
96 if( $done != -1 )
97 {
98 $class->{"_created"} = 1;
99 $class->{"_changed"} = 0;
100 }
101  
102 return $done!=-1?$class:undef;
103 }
104 else
105 {
106 while( !$done && $done != -1 )
107 {
108 $self = new $pkg;
109 $done = ${"${pkg}::_PLUGIN"}->load($self,$pkg,$type);
110 $self->{"_created"} = 1;
111 $self->{"_changed"} = 0;
112  
113 if( !$done && $done != -1 )
114 {
115 $self->{"_created"} = 1;
116 $self->{"_changed"} = 0;
117 push @ret_val, $self;
118 }
119 }
120  
121 return @ret_val;
122 }
123 }
124  
125 sub save
126 {
127 if( @_ != 1 )
128 {
129 confess "save isn't called with any arguments!";
130 }
131  
132 my ($class) = @_;
133 ref $class or confess "Can only save an instance of class ".ref($class)."!";
134 my $pkg = ref $class;
135 my $done;
136  
137 if( !$class->{"_changed"} )
138 {
139 return 1;
140 }
141  
142 if( $class->{"_created"} )
143 {
144 $done = ${"${pkg}::_PLUGIN"}->save($class,$pkg);
145 }
146 else
147 {
148 $done = ${"${pkg}::_PLUGIN"}->store($class,$pkg);
149 }
150  
151 if( $done )
152 {
153 $class->{"_changed"} = 0;
154 $class->{"_created"} = 1;
155 }
156  
157 return $done;
158 }
159  
160 sub delete
161 {
162 if( @_ != 1 )
163 {
164 confess "delete isn't called with any arguments!";
165 }
166  
167 my ($class) = @_;
168 ref $class or confess "Can only delete an instance of class ".ref($class)."!";
169 my $pkg = ref $class;
170 my $done = 1;
171 my $is_a;
172  
173 if( $class->{"_changed"} || !$class->{"_created"} )
174 {
175 return 0;
176 }
177  
178 foreach $attrib (@${"${pkg}::_ATTRIBUTES"},"_id")
179 {
180 eval "\$is_a = \$class->{\$attrib}->isa('Class::Persistent::StructTemplate')";
181  
182 if( !$@ && $is_a )
183 {
184 $done &= $class->{$attrib}->delete;
185 }
186 }
187  
188 if( ${"${pkg}::_PLUGIN"}->calc_refs($class,$pkg) <= 1 )
189 {
190 ${"${pkg}::_PLUGIN"}->delete($class,$pkg);
191 $class->{"_created"} = 0;
192 $class->{"_changed"} = 1;
193 }
194  
195 ${"${pkg}::_PLUGIN"}->check_tables;
196  
197 return $done;
198 }
199  
200  
201 sub set_attributes_type
202 {
203 if( @_ != 3 )
204 {
205 confess "set_attributes_type can only be called with two arguments!";
206 }
207  
208 my ($class,$attribs,$types) = @_;
209 ref $class or confess "Can only set attributes to an instance of this class!";
210 my $pkg = ref $class;
211 my ($attrib);
212  
213 foreach $attrib (@${"${pkg}::_ATTRIBUTES"},"_id")
214 {
215 if( exists $attribs->{$attrib} )
216 {
217 $class->$attrib(restore_val($attribs->{$attrib},$types->{$attrib}));
218 }
219 elsif( !$class->{"_allset"} )
220 {
221 $class->$attrib(undef);
222 }
223 }
224  
225 $class->{"_allset"} = 1;
226  
227 return $class;
228 }
229  
230 sub get_attributes_type
231 {
232 if( @_ != 1 )
233 {
234 confess "get_attributes is never called with arguments!";
235 }
236  
237 my ($class) = @_;
238 ref $class or confess "Can only get attributes of an instance of this class!";
239 my $pkg = ref $class;
240 my ($attrib);
241 my ($ret_val1,$ret_val2) = ({},{});
242  
243 foreach $attrib (@${"${pkg}::_ATTRIBUTES"},"_id")
244 {
245 ($ret_val1->{$attrib},$ret_val2->{$attrib}) = store_val($class->{$attrib});
246 }
247  
248 return ($ret_val1,$ret_val2);
249 }
250  
251 sub restore_val
252 {
253 if( @_ != 2 )
254 {
255 confess "restore_val can only be called with two parameters!";
256 }
257  
258 my ($val,$type) = @_;
259 my ($id,$class);
260 my $ret_val;
261  
262 if( $type eq 'n' || $type eq 's' )
263 {
264 $ret_val = $val;
265 }
266 elsif( $type eq 'c' )
267 {
268 $val =~ /^(.*?)\|(.*)$/;
269 $id = $1;
270 $class = $2;
271  
272 eval "use $class;";
273 confess "Could not load class $class (error: $@)!" if $@;
274  
275 ($ret_val) = $class->load("_id = $id");
276 }
277 else
278 {
279 eval $val;
280 }
281  
282 return $ret_val;
283 }
284  
285 sub store_val
286 {
287 if( @_ != 1 )
288 {
289 confess "store_val can only be called with one parameter!";
290 }
291  
292 my ($val) = @_;
293 my ($ret_val1,$ret_val2);
294 my $is_a;
295  
296 if( ref $val )
297 {
298 eval "\$is_a = \$val->isa('Class::Persistent::StructTemplate');";
299 if( !$@ && $is_a )
300 {
301 $ret_val2 = 'c';
302 $ret_val1 = $val->_id()."|".ref($val);
303  
304 $val->save();
305 }
306 else
307 {
308 local $Data::Dumper::Purity = 1;
309 local $Data::Dumper::Useqq = 1;
310 local $Data::Dumper::Indent = 0;
311  
312 $ret_val2 = 'd';
313 $ret_val1 = Data::Dumper->Dump([$val],[qw(ret_val)]);
314 }
315 }
316 elsif( $val == 0 && $val ne '0' )
317 {
318 $ret_val2 = 's';
319 $ret_val1 = $val;
320 }
321 else
322 {
323 $ret_val2 = 'n';
324 $ret_val1 = $val;
325 }
326  
327 return ($ret_val1,$ret_val2);
328 }
329  
330 1;