EEDB/Symbol.pm untested

filter: Statementstmt 0.0% 0 / 180 Branchbran 0.0% 0 / 42 Conditioncond 0.0% 0 / 9 MC/DCmcdc 0.0% 0 / 5 Subroutinesub 0.0% 0 / 28 Podpod 0.0% 0 / 26 totaltotal 0.0% 0 / 290 CC 22 SCAR 23.2
CC
22
Cov
0%
fetch_all_by_type_numerical_range
30.0
check_exists_db
20.0
unlink_from_feature
20.0
CRAP 10.2
1 =head1 NAME - EEDB::Symbol
2  
3 =head1 SYNOPSIS
4  
5 =head1 DESCRIPTION
6  
7 =head1 CONTACT
8  
9 Jessica Severin <severin@gsc.riken.jp>
10  
11 =head1 LICENSE
12  
13 * Software License Agreement (BSD License)
14 * EdgeExpressDB [eeDB] system
15 * copyright (c) 2007-2009 Jessica Severin RIKEN OSC
16 * All rights reserved.
17 * Redistribution and use in source and binary forms, with or without
18 * modification, are permitted provided that the following conditions are met:
19 * * Redistributions of source code must retain the above copyright
20 * notice, this list of conditions and the following disclaimer.
21 * * Redistributions in binary form must reproduce the above copyright
22 * notice, this list of conditions and the following disclaimer in the
23 * documentation and/or other materials provided with the distribution.
24 * * Neither the name of Jessica Severin RIKEN OSC nor the
25 * names of its contributors may be used to endorse or promote products
26 * derived from this software without specific prior written permission.
27 *
28 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ''AS IS'' AND ANY
29 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
30 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
31 * DISCLAIMED. IN NO EVENT SHALL COPYRIGHT HOLDERS BE LIABLE FOR ANY
32 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
33 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
34 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
35 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
36 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
37 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
38  
39 =head1 APPENDIX
40  
41 The rest of the documentation details each of the object methods. Internal methods are usually preceded with a _
42  
43 =cut
44  
45 $VERSION = 0.953;
46  
47 package EEDB::Symbol;
48  
49 use strict;
50  
51 use EEDB::Metadata;
52 our @ISA = qw(EEDB::Metadata);
53  
54 #################################################
55 # Class methods
56 #################################################
57  
58 sub class { return "Symbol"; }
59  
60 #################################################
61 # Instance methods
62 #################################################
63  
64 ##########################
65 #
66 # getter/setter methods of data which is stored in database
67 #
68 ##########################
69  
70 sub symbol {
71 my $self = shift;
72 return $self->SUPER::data(@_);
73 }
74  
75 sub display_desc {
76 #override superclass method
77 my $self = shift;
78 return sprintf("Symbol(%s) %s:%s",
79 $self->id,
80 $self->type,
81 $self->symbol);
82 }
83  
84 sub display_contents {
85 #override superclass method
86 my $self = shift;
87 return $self->display_desc;
88 }
89  
90 sub xml {
91 my $self = shift;
92 my $value = $self->symbol;
93 $value =~ s/&/&amp;/g;
94 $value =~ s/</&lt;/g;
95 $value =~ s/>/&gt;/g;
96 $value =~ s/"/&quot;/g; #"
97 my $str = sprintf("<symbol type=\"%s\" value=\"%s\" />\n", $self->type, $value);
98 return $str;
99 }
100  
101 #################################################
102 #
103 # DBObject override methods : storage methods
104 #
105 #################################################
106  
107 sub store {
108 my $self = shift;
109 my $db = shift;
110
111 unless($db) { return undef; }
112 if($self->check_exists_db($db)) { return $self; }
113
114 $db->execute_sql("INSERT ignore INTO symbol (sym_type, sym_value) VALUES(?,?)",
115 $self->type, $self->symbol);
116 return $self->check_exists_db($db); #checks the database and sets the id
117 }
118  
119  
120 sub check_exists_db {
121 my $self = shift;
122 my $db = shift;
123
124 unless($db) { return undef; }
125 if(defined($self->primary_id)) { return $self; }
126
127 #check if it is already in the database
128 my $dbID = $db->fetch_col_value("SELECT symbol_id FROM symbol where sym_type=? and sym_value=?",
129 $self->type, $self->data);
130 if($dbID) {
131 $self->primary_id($dbID);
132 $self->database($db);
133 return $self;
134 } else {
135 return undef;
136 }
137 }
138  
139  
140 sub store_link_to_feature {
141 my $self = shift;
142 my $feature = shift;
143
144 unless($feature->database) {
145 printf("ERROR:: %s has no database to link Symbol\n", $feature->simple_display_desc);
146 die();
147 }
148 $feature->database->execute_sql(
149 "INSERT ignore INTO feature_2_symbol (feature_id, symbol_id) VALUES(?,?)",
150 $feature->id, $self->id);
151 }
152  
153 sub unlink_from_feature {
154 my $self = shift;
155 my $feature = shift;
156
157 unless($self->database) { printf("ERROR:: no database\n"); die(); }
158 unless($self->primary_id) { printf("ERROR:: undef symbol_id\n"); die(); }
159 unless($feature->primary_id) { printf("ERROR:: undef feature_id\n"); die(); }
160  
161 #then link
162 my $sql = sprintf("DELETE from feature_2_symbol where feature_id=%s and symbol_id=%s", $feature->id, $self->id);
163 #printf("%s\n", $sql);
164 $feature->database->do_sql($sql);
165 }
166  
167  
168 sub store_link_to_feature_link {
169 my $self = shift;
170 my $edge = shift;
171
172 unless($edge->database) {
173 printf("ERROR:: %s has no database to link Symbol\n", $edge->display_desc);
174 die();
175 }
176 $edge->database->execute_sql(
177 "INSERT ignore INTO edge_2_symbol (edge_id, symbol_id) VALUES(?,?)",
178 $edge->id, $self->id);
179 }
180  
181  
182 sub store_link_to_experiment {
183 my $self = shift;
184 my $experiment = shift;
185
186 unless($experiment->database) {
187 printf("ERROR:: %s has no database to link Symbol\n", $experiment->display_desc);
188 die();
189 }
190 $experiment->database->execute_sql(
191 "INSERT ignore INTO experiment_2_symbol (experiment_id, symbol_id) VALUES(?,?)",
192 $experiment->id, $self->id);
193 }
194  
195  
196 sub store_link_to_feature_source {
197 my $self = shift;
198 my $fsrc = shift;
199
200 unless($fsrc->database) {
201 printf("ERROR:: %s has no database to link Symbol\n", $fsrc->display_desc);
202 die();
203 }
204 $fsrc->database->execute_sql(
205 "INSERT ignore INTO feature_source_2_symbol (feature_source_id, symbol_id) VALUES(?,?)",
206 $fsrc->id, $self->id);
207 }
208  
209  
210 sub store_link_to_edge_source {
211 my $self = shift;
212 my $edge_source = shift;
213
214 unless($edge_source->database) {
215 printf("ERROR:: %s has no database to link Symbol\n", $edge_source->display_desc);
216 die();
217 }
218 $edge_source->database->execute_sql(
219 "INSERT ignore INTO edge_source_2_symbol (edge_source_id, symbol_id) VALUES(?,?)",
220 $edge_source->id, $self->id);
221 }
222  
223  
224 ##### DBObject instance override methods #####
225  
226 sub mapRow {
227 my $self = shift;
228 my $rowHash = shift;
229 my $dbh = shift;
230  
231 $self->primary_id($rowHash->{'symbol_id'});
232 $self->type($rowHash->{'sym_type'});
233 $self->symbol($rowHash->{'sym_value'});
234  
235 return $self;
236 }
237  
238  
239 ##### public class methods for fetching by utilizing DBObject framework methods #####
240  
241 sub fetch_by_id {
242 my $class = shift;
243 my $db = shift;
244 my $id = shift;
245  
246 my $sql = "SELECT * FROM symbol WHERE symbol_id=?";
247 return $class->fetch_single($db, $sql, $id);
248 }
249  
250 sub fetch_by_type_name {
251 my $class = shift;
252 my $db = shift;
253 my $sym_type = shift;
254 my $sym_value = shift;
255
256 my $sql = "SELECT * FROM symbol WHERE sym_value = ? AND sym_type=?";
257 return $class->fetch_single($db, $sql, $sym_value, $sym_type);
258 }
259  
260 sub stream_all {
261 my $class = shift;
262 my $db = shift;
263  
264 my $sql = "SELECT * FROM symbol";
265 return $class->stream_multiple($db, $sql);
266 }
267  
268 sub fetch_all_by_feature_id {
269 my $class = shift;
270 my $db = shift;
271 my $id = shift;
272  
273 my $sql = "SELECT * FROM feature_2_symbol join symbol using(symbol_id) where feature_id = ?";
274 return $class->fetch_multiple($db, $sql, $id);
275 }
276  
277 sub fetch_all_by_edge_id {
278 my $class = shift;
279 my $db = shift;
280 my $id = shift;
281  
282 my $sql = "SELECT * FROM edge_2_symbol join symbol using(symbol_id) where edge_id = ?";
283 return $class->fetch_multiple($db, $sql, $id);
284 }
285  
286 sub fetch_all_by_experiment_id {
287 my $class = shift;
288 my $db = shift;
289 my $id = shift;
290  
291 my $sql = "SELECT * FROM experiment_2_symbol join symbol using(symbol_id) where experiment_id = ?";
292 return $class->fetch_multiple($db, $sql, $id);
293 }
294  
295 sub fetch_all_by_feature_source_id {
296 my $class = shift;
297 my $db = shift;
298 my $id = shift;
299  
300 my $sql = "SELECT s.* FROM symbol s join feature_source_2_symbol using(symbol_id) where feature_source_id = ?";
301 return $class->fetch_multiple($db, $sql, $id);
302 }
303  
304 sub fetch_all_by_edge_source_id {
305 my $class = shift;
306 my $db = shift;
307 my $id = shift;
308  
309 my $sql = "SELECT s.* FROM symbol s join edge_source_2_symbol using(symbol_id) where edge_source_id = ?";
310 return $class->fetch_multiple($db, $sql, $id);
311 }
312  
313  
314 sub fetch_all_by_name {
315 my $class = shift;
316 my $db = shift;
317 my $sym_value = shift;
318 my $sym_type = shift; #optional
319 my $response_limit = shift; #optional
320
321 my $sql = sprintf("SELECT * FROM symbol WHERE sym_value = '%s'", $sym_value);
322 if(defined($sym_type)) {
323 $sql .= sprintf(" AND sym_type='%s'", $sym_type);
324 }
325 if($response_limit) {
326 $sql .= sprintf(" LIMIT %d", $response_limit);
327 }
328  
329 #print($sql, "\n", );
330 return $class->fetch_multiple($db, $sql);
331 }
332  
333  
334 sub fetch_all_symbol_search {
335 my $class = shift;
336 my $db = shift;
337 my $sym_value = shift;
338 my $sym_type = shift; #optional
339 my $response_limit = shift; #optional
340
341 $sym_value =~ s/_/\\_/g;
342 $sym_value =~ s/\"/\\\"/g;
343 $sym_value =~ s/\'/\\\'/g; #'
344  
345 my $sql = sprintf("SELECT * FROM symbol WHERE sym_value like '%s%%'", $sym_value);
346 if(defined($sym_type)) {
347 $sql .= sprintf(" AND sym_type='%s'", $sym_type);
348 }
349 #print($sql, "\n", );
350 return $class->fetch_multiple($db, $sql);
351 }
352  
353  
354 sub fetch_all_by_type_numerical_range {
355 my $class = shift;
356 my $db = shift;
357 my $sym_type = shift;
358 my $low_value = shift;
359 my $upper_value = shift;
360 my $response_limit = shift; #optional
361
362 return [] unless(defined($low_value) and defined($sym_type));
363
364 my $sql = sprintf("SELECT * FROM symbol WHERE sym_type='%s' ", $sym_type);
365 if(defined($low_value) and defined($upper_value) and ($low_value == $upper_value)) {
366 #doing an equals
367 $sql .= sprintf("AND (sym_value+0)=%s ", $low_value);
368 } else {
369 $sql .= sprintf("AND (sym_value+0)>=%s ", $low_value);
370 $sql .= sprintf("AND (sym_value+0)<=%s ", $upper_value) if(defined($upper_value));
371 }
372  
373 if($response_limit) {
374 $sql .= sprintf(" LIMIT %d", $response_limit);
375 }
376  
377 #print($sql, "\n", );
378 return $class->fetch_multiple($db, $sql);
379 }
380  
381  
382 sub get_count_symbol_search {
383 my $class = shift;
384 my $db = shift;
385 my $sym_value = shift;
386 my $sym_type = shift; #optional
387
388 $sym_value =~ s/_/\\_/g;
389  
390 my $sql = sprintf("select count(distinct symbol_id) from symbol WHERE sym_value like '%s'", $sym_value);
391 if(defined($sym_type)) {
392 $sql .= sprintf(" AND sym_type='%s'", $sym_type);
393 }
394 return $class->fetch_col_value($db, $sql);
395 }
396  
397 1;
398