MQdb/DBObject.pm untested

filter: Statementstmt 0.0% 0 / 60 Branchbran 0.0% 0 / 16 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 0.0% 0 / 17 Podpod 0.0% 0 / 15 totaltotal 0.0% 0 / 108 CC 9 SCAR 19.5
CC
9
Cov
0%
db_id
20.0
id
12.0
DESTROY
6.0
CRAP 7.0
1 # $Id: DBObject.pm,v 1.13 2009/06/03 06:47:32 severin Exp $
2 =head1 NAME
3  
4 MQdb::DBObject - DESCRIPTION of Object
5  
6 =head1 SYNOPSIS
7  
8 Root class for all objects in MappedQuery toolkit
9  
10 =head1 DESCRIPTION
11  
12 Root object for toolkit and all derived subclasses.
13 All objects in the MappedQuery structure are designed to
14 be persisted in a database. Here database is a more broad
15 term and can be considered any object persistance systems.
16 Currently the toolkit works with SQL based systems but
17 object databases or custom storage engines are possible.
18 Provides base common methods used by all objects.
19  
20 =head1 AUTHOR
21  
22 Contact Jessica Severin: jessica.severin@gmail.com
23  
24 =head1 LICENSE
25  
26 * Software License Agreement (BSD License)
27 * MappedQueryDB [MQdb] toolkit
28 * copyright (c) 2006-2009 Jessica Severin
29 * All rights reserved.
30 * Redistribution and use in source and binary forms, with or without
31 * modification, are permitted provided that the following conditions are met:
32 * * Redistributions of source code must retain the above copyright
33 * notice, this list of conditions and the following disclaimer.
34 * * Redistributions in binary form must reproduce the above copyright
35 * notice, this list of conditions and the following disclaimer in the
36 * documentation and/or other materials provided with the distribution.
37 * * Neither the name of Jessica Severin nor the
38 * names of its contributors may be used to endorse or promote products
39 * derived from this software without specific prior written permission.
40 *
41 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ''AS IS'' AND ANY
42 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
43 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
44 * DISCLAIMED. IN NO EVENT SHALL COPYRIGHT HOLDERS BE LIABLE FOR ANY
45 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
46 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
47 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
48 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
49 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
50 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
51  
52 =head1 APPENDIX
53  
54 The rest of the documentation details each of the object methods.
55 Internal methods are usually preceded with a _
56  
57 =cut
58  
59 $VERSION=0.954;
60  
61 package MQdb::DBObject;
62  
63 use strict;
64 use MQdb::Database;
65  
66 #################################################
67 # Factory methods
68 #################################################
69  
70 =head2 new
71  
72 Description: instance creation method
73 Returntype : instance of this Class (subclass)
74 Exceptions : none
75  
76 =cut
77  
78 sub new {
79 my ($class, @args) = @_;
80 my $self = {};
81 bless $self,$class;
82 $self->init(@args);
83
84 #my $idx = rindex $class, "::";
85 #$self->{'_class'} = substr($class, $idx+2);
86 return $self;
87 }
88  
89 =head2 init
90  
91 Description: initialization method which subclasses can extend
92 Returntype : $self
93 Exceptions : subclass dependent
94  
95 =cut
96  
97 sub init {
98 my $self = shift;
99 #internal variables minimal allocation
100 $self->{'_primary_db_id'} = undef;
101 $self->{'_database'} = undef;
102 return $self;
103 }
104  
105 =head2 copy
106  
107 Description: Shallow copy which copies all base attributes of object
108 to new instance of same class
109 Returntype : same as calling instance
110 Exceptions : subclass dependent
111  
112 =cut
113  
114 sub copy {
115 my $self = shift;
116 my $class = ref($self);
117 my $copy = $class->new;
118 foreach my $key (keys %{$self}) {
119 $copy->{$key} = $self->{$key};
120 }
121 #print('self = ', $self, "\n");
122 #print('copy = ', $copy, "\n");
123  
124 return $copy;
125 }
126  
127 sub DESTROY {
128 my $self = shift;
129 #If I need to do any cleanup - do it here
130 $self->SUPER::DESTROY if $self->can("SUPER::DESTROY");
131 }
132  
133 #################################################
134 # Instance methods
135 #################################################
136  
137 =head2 class
138  
139 Description: fixed string symbol for this class. Must be implemented
140 for each subclass and each subclass within toolkit
141 should return a unique name. used by global methods.
142 Returntype : string
143 Exceptions : error if subclass does not redefine
144  
145 =cut
146  
147 sub class {
148 my $self = shift;
149 printf("ERROR:: DBObject subclass needs to implement class() method\n");
150 die();
151 }
152  
153 =head2 database
154  
155 Description: the MQdb::Database where this object is permanently persisted to.
156 Here database is any object persistance system.
157 Returntype : MQdb::Database
158 Exceptions : die if invalid setter value type is provided
159  
160 =cut
161  
162 sub database {
163 my $self = shift;
164 if(@_) {
165 my $db = shift;
166 $self->{'_database'} = $db;
167 }
168 return $self->{'_database'};
169 }
170  
171 =head2 primary_id
172  
173 Description: the unique identifier for this object within database.
174 Returntype : scalar or UNDEF
175 Exceptions : none
176  
177 =cut
178  
179 sub primary_id {
180 my $self = shift;
181 $self->{'_primary_db_id'} = shift if @_;
182 return $self->{'_primary_db_id'};
183 }
184  
185 =head2 id
186  
187 Description: the unique identifier for this object within database.
188 Returns empty string if not persisted.
189 Returntype : scalar or ''
190 Exceptions : none
191  
192 =cut
193  
194 sub id {
195 my $self = shift;
196 $self->{'_primary_db_id'} = shift if @_;
197 if(!defined($self->{'_primary_db_id'})) { return ''; }
198 return $self->{'_primary_db_id'};
199 }
200  
201 =head2 db_id
202  
203 Description: the worldwide unique identifier for this object.
204 A URL-like combination of database, class, and id
205 Returntype : string or undef if database is not defined
206 Exceptions : none
207  
208 =cut
209  
210 sub db_id {
211 my $self = shift;
212 return $self->{'_db_id'} if(defined($self->{'_db_id'}));
213 return undef unless($self->database);
214 if($self->database->uuid) {
215 $self->{'_db_id'} = $self->database->uuid . "::" . $self->primary_id . ":::" . $self->class;
216 } else {
217 $self->{'_db_id'} =
218 sprintf("%s://%s:%s/%s/%s?id=%d",
219 $self->database->driver,
220 $self->database->host,
221 $self->database->port,
222 $self->database->dbname,
223 $self->class,
224 $self->primary_id);
225 }
226 return $self->{'_db_id'};
227 }
228  
229  
230 =head2 display_desc
231  
232 Description: general purpose debugging method that returns a nice
233 human readable description of the object instance contents.
234 Each subclass should implement and return a nice string.
235 Returntype : string scalar
236 Exceptions : none
237  
238 =cut
239  
240 sub display_desc {
241 my $self = shift;
242 return $self; #return object identifier for printing
243 }
244  
245 =head2 display_info
246  
247 Description: convenience method which prints the display_desc string
248 with a carriage return to STDOUT. useful for debugging.
249 Returntype : none
250 Exceptions : none
251  
252 =cut
253  
254 sub display_info {
255 my $self = shift;
256 printf("%s\n", $self->display_desc);
257 }
258  
259 =head2 xml
260  
261 Description: every object in system should be persistable in XML format.
262 returns an XML description of the object and all child objects.
263 Each subclass must implement and return a proper XML string.
264 Best if one implements xml_start() and xml_end() and use here.
265 Returntype : string scalar
266 Exceptions : none
267 Default : default is a simple xml_start + xml_end
268  
269 =cut
270  
271 sub xml {
272 my $self = shift;
273 return $self->xml_start() . $self->xml_end();
274 }
275  
276 =head2 xml_start
277  
278 Description: every object in system should be persistable in XML format.
279 returns an XML description of the object and all child objects.
280 Each subclass should OVERRIDE this method and return a proper XML string.
281 xml_start is the primary XML start tag
282 Example : return sprintf("<feature id='%d' name='%d' ..... >", $id, $name....);
283 Returntype : string scalar
284 Exceptions : none
285  
286 =cut
287  
288 sub xml_start {
289 my $self = shift;
290 return '';
291 }
292  
293  
294 =head2 xml_end
295  
296 Description: every object in system should be persistable in XML format.
297 returns an XML description of the object and all child objects.
298 Each subclass should OVERRIDE this method and return a proper XML string.
299 xml_end is the primary XML end tag
300 Example : return "</feature>";
301 Returntype : string scalar
302 Exceptions : none
303  
304 =cut
305  
306 sub xml_end {
307 my $self = shift;
308 return '';
309 }
310  
311 =head2 simple_xml
312  
313 Description: short hand for xml_start() . xml_end()
314 Can be used when only the primary XML start tag and attributes are needed
315 No need to override if xml_start() and xml_end() are implemented
316 Returntype : string scalar
317 Exceptions : none
318  
319 =cut
320  
321 sub simple_xml {
322 my $self = shift;
323 return $self->xml_start() . $self->xml_end();
324 }
325  
326 1;
327  
328  
329  
330