Xmldoom/Definition/Database.pm untested

filter: Statementstmt 0.0% 0 / 155 Branchbran 0.0% 0 / 36 Conditioncond 0.0% 0 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 0.0% 0 / 28 Podpod 0.0% 0 / 19 totaltotal 0.0% 0 / 243 CC 19 SCAR 25.8
CC
19
Cov
0%
_find_many_to_many_links
42.0
find_links
30.0
_find_inferred_links
20.0
CRAP 13.2
1  
2 package Xmldoom::Definition::Database;
3  
4 use Xmldoom::Definition::Object;
5 use Xmldoom::Definition::SAXHandler;
6 use Xmldoom::Definition::LinkTree;
7 use Xmldoom::Definition::Link;
8 use Xmldoom::Threads;
9 use Exception::Class::TryCatch;
10 use XML::SAX::ParserFactory;
11 use strict;
12  
13 use Data::Dumper;
14  
15 sub new
16 {
17 my $class = shift;
18 my $args = shift;
19  
20 my $schema;
21  
22 if ( ref($args) eq 'HASH' )
23 {
24 $schema = $args->{schema};
25 }
26 else
27 {
28 $schema = $args;
29 }
30  
31 my $self = {
32 schema => $schema,
33 objects => { },
34  
35 real_links => Xmldoom::Definition::LinkTree->new(),
36 inferred_links => Xmldoom::Definition::LinkTree->new(),
37 many_to_many_links => Xmldoom::Definition::LinkTree->new(),
38
39 connection_factory => undef,
40 };
41  
42 # go through and add all of the real links from the schema
43 while ( my ($table_name, $table) = each %{$self->{schema}->get_tables()} )
44 {
45 foreach my $fkey ( @{$table->get_foreign_keys()} )
46 {
47 $self->{real_links}->add_link( Xmldoom::Definition::Link->new($fkey) );
48 }
49 }
50  
51 bless $self, $class;
52 return Xmldoom::Threads::make_shared($self, $args->{shared});
53 }
54  
55 sub get_connection_factory { return shift->{connection_factory}; }
56 sub get_schema { return shift->{schema}; }
57  
58 sub get_tables { return shift->{schema}->get_tables; }
59 sub get_table
60 {
61 my ($self, $name) = @_;
62 return $self->{schema}->get_table($name);
63 }
64 sub has_table
65 {
66 my ($self, $name) = @_;
67 return $self->{schema}->has_table($name);
68 }
69  
70 sub get_objects { return shift->{objects}; }
71 sub get_object
72 {
73 my ($self, $name) = @_;
74  
75 if ( not defined $self->{objects}->{$name} )
76 {
77 die "Unknown object named '$name'";
78 }
79
80 return $self->{objects}->{$name};
81 }
82 sub has_object
83 {
84 my ($self, $name) = @_;
85 return defined $self->{objects}->{$name};
86 }
87  
88 sub set_connection_factory
89 {
90 my ($self, $factory) = @_;
91 $self->{connection_factory} = $factory;
92 }
93  
94 sub create_db_connection
95 {
96 return shift->get_connection_factory()->create();
97 }
98  
99 sub create_object
100 {
101 my ($self, $object_name, $table_name) = @_;
102  
103 if ( defined $self->{objects}->{$object_name} )
104 {
105 die "Object definition for \"$object_name\" already added.";
106 }
107  
108 # add and return the object definition
109 my $object = Xmldoom::Definition::Object->new({
110 definition => $self,
111 object_name => $object_name,
112 table_name => $table_name,
113 shared => Xmldoom::Threads::is_shared($self)
114 });
115 $self->{objects}->{$object_name} = $object;
116 return $object;
117 }
118  
119 sub find_links
120 {
121 my ($self, $table1_name, $table2_name) = @_;
122  
123 if ( not $self->has_table($table1_name) or not $self->has_table($table2_name) )
124 {
125 die "Cannot find connections between one or more non-existant tables";
126 }
127  
128 my $links;
129  
130 # NOTE: In case anyone is wondering, the links are seperated into three different
131 # trees inorder to seperate which pools of links is used for calculating the links
132 # in another pool. Specifically, when we caclulate the inferred links we want to
133 # draw *only* on real links, and not other inferred links or many to many links.
134 # Similarily, when we calculate many to many links we only want to consider real links
135 # and inferred links but not other many to many links.
136  
137 # check stored real links
138 $links = $self->{real_links}->get_links($table1_name, $table2_name);
139 if ( defined $links )
140 {
141 return $links;
142 }
143  
144 # check stored inferred links
145 $links = $self->_find_inferred_links($table1_name, $table2_name);
146 if ( defined $links )
147 {
148 return $links;
149 }
150  
151 # check stored many-to-many links
152 $links = $self->_find_many_to_many_links($table1_name, $table2_name);
153 if ( defined $links )
154 {
155 return $links;
156 }
157  
158 # attempt to find new inferred links
159 return [];
160 }
161  
162 sub _find_inferred_links
163 {
164 my ($self, $table1_name, $table2_name) = @_;
165  
166 # first check to see if there is a cached link available
167 my $cached_links = $self->{inferred_links}->get_links($table1_name, $table2_name);
168 if ( defined $cached_links )
169 {
170 return $cached_links;
171 }
172  
173 my @ret;
174  
175 # now, attempt to find an inferred link, begginning by grabing a list of all the tables
176 # that this table is links to.
177 my $link_hash = $self->{real_links}->get_links($table1_name);
178 while ( my ($inter_table, $links) = each %$link_hash )
179 {
180 for my $link ( @$links )
181 {
182 # here we check to see if there are any links and the linked table, to the desired
183 # table by way of the columns specified at the end of the original link.
184 my $other_links = $self->{real_links}->get_links($inter_table, $table2_name, $link->get_end_column_names());
185  
186 # multiple links are ok --- they just mean that there are multiple inferred links,
187 # with the associated problems handled elsewhere, just as they would have to be
188 # with the relevent real links.
189 foreach my $other_link ( @$other_links )
190 {
191 if ( defined $other_link )
192 {
193 my $inferred_link = Xmldoom::Definition::Link->new(
194 Xmldoom::Schema::ForeignKey->new({
195 parent => $self->get_schema()->get_table($table1_name),
196 reference_table => $table2_name,
197 local_columns => $link->get_start_column_names(),
198 foreign_columns => $other_link->get_end_column_names()
199 })
200 );
201  
202 # cache the result for later
203 $self->{inferred_links}->add_link( $link );
204  
205 push @ret, $inferred_link;
206 }
207 }
208 }
209 }
210  
211 if ( scalar @ret > 0 )
212 {
213 return \@ret;
214 }
215  
216 return undef;
217 }
218  
219 sub _find_many_to_many_links
220 {
221 my ($self, $table1_name, $table2_name) = @_;
222  
223 # first check to see if there is a cached link available
224 my $cached_links = $self->{many_to_many_links}->get_links($table1_name, $table2_name);
225 if ( defined $cached_links )
226 {
227 return $cached_links;
228 }
229  
230 my @ret;
231  
232 # get all the connections to other tables from the real links and the inferred links
233 # while purposely not checking the many-to-many links which would just create problems.
234 my $link_hash = { };
235 my $temp;
236 if ( defined ($temp = $self->{real_links}->get_links($table1_name)) )
237 {
238 $link_hash = { %$link_hash, %$temp };
239 }
240 if ( defined ($temp = $self->{inferred_links}->get_links($table1_name)) )
241 {
242 $link_hash = { %$link_hash, %$temp };
243 }
244  
245 # we loop through simply looking for a table we are linked to which is also linked to
246 # the desired table. We don't have to check to see if this "inferred" or truely "many
247 # to many" because we know that the inferred keys will be checked first. This has the
248 # weakness of only working for single table "jumps," but any type of recursition scares
249 # me just now.
250 while ( my ($inter_table, $links) = each %$link_hash )
251 {
252 for my $link ( @$links )
253 {
254 my $other_links = $self->{real_links}->get_links($inter_table, $table2_name);
255  
256 # return multiple links as we find them, to be dealt with by the calling code.
257 foreach my $other_link ( @$other_links )
258 {
259 if ( defined $other_link )
260 {
261 my $many_to_many_link = Xmldoom::Definition::Link->new([
262 $link->get_foreign_key(),
263 $other_link->get_foreign_key()
264 ]);
265  
266 # cache the result for later
267 $self->{many_to_many_links}->add_link( $many_to_many_link );
268  
269 push @ret, $many_to_many_link;
270 }
271 }
272 }
273 }
274  
275  
276 if ( scalar @ret > 0 )
277 {
278 return \@ret;
279 }
280  
281 return undef;
282 }
283  
284 sub parse_object_string
285 {
286 my ($self, $input) = @_;
287  
288 # build the parser
289 my $handler = Xmldoom::Definition::SAXHandler->new( $self );
290 my $parser = XML::SAX::ParserFactory->parser(Handler => $handler);
291  
292 # phase 1 -- Create the objects and attach to respective tables
293 $parser->parse_string($input);
294  
295 # phase 2 -- Actually add all the properties to the objects
296 $parser->parse_string($input);
297 }
298  
299 sub parse_object_uri
300 {
301 my ($self, $uri) = @_;
302  
303 # build the parser
304 my $handler = Xmldoom::Definition::SAXHandler->new( $self );
305 my $parser = XML::SAX::ParserFactory->parser(Handler => $handler);
306  
307 # phase 1 -- Create the objects and attach to respective tables
308 $parser->parse_uri($uri);
309  
310 # phase 2 -- Actually add all the properties to the objects
311 $parser->parse_uri($uri);
312 }
313  
314 sub SearchRS
315 {
316 my $self = shift;
317 my $criteria = shift;
318  
319 my $query = $criteria->generate_query_for_attrs( $self, @_ );
320  
321 my $conn;
322 my $rs;
323  
324 # connect and query
325 try eval
326 {
327 $conn = $self->create_db_connection();
328 #printf STDERR "Search(): %s\n", $conn->generate_sql($query);
329 $rs = $conn->prepare( $query )->execute();
330 };
331  
332 catch my $err;
333 if ( $err )
334 {
335 $conn->disconnect() if defined $conn;
336 $err->rethrow();
337 }
338  
339 return $rs;
340 }
341  
342 sub Search
343 {
344 my $class = shift;
345 my $rs = $class->SearchRS( @_ );
346
347 my @ret;
348  
349 # unravel our result set
350 while ( $rs->next() )
351 {
352 push @ret, $rs->get_row();
353 }
354  
355 return wantarray ? @ret : \@ret;
356 }
357  
358 #sub DESTROY
359 #{
360 # my $self = shift;
361 #
362 # if ( $self->get_dbh() )
363 # {
364 # $self->get_dbh()->disconnect();
365 # $self->set_dbh( undef );
366 # }
367 #}
368  
369 1;
370