Xmldoom/Definition/Link.pm untested

filter: Statementstmt 0.0% 0 / 116 Branchbran 0.0% 0 / 38 Conditioncond 0.0% 0 / 9 MC/DCmcdc 0.0% 0 / 6 Subroutinesub 0.0% 0 / 22 Podpod 0.0% 0 / 19 totaltotal 0.0% 0 / 210 CC 20 SCAR 29.5
CC
20
Cov
0%
new
56.0
contains
30.0
reduce_longest
20.0
CRAP 19.1
1  
2 package Xmldoom::Definition::Link;
3 use base qw(Exporter);
4  
5 use strict;
6  
7 use Data::Dumper;
8  
9 our @EXPORT_OK = qw(
10 ONE_TO_ONE,
11 MANY_TO_ONE,
12 ONE_TO_MANY,
13 MANY_TO_MANY
14 );
15  
16 our $ONE_TO_ONE = 'one-to-one';
17 our $MANY_TO_ONE = 'many-to-one';
18 our $ONE_TO_MANY = 'one-to-many';
19 our $MANY_TO_MANY = 'many-to-many';
20  
21 sub new
22 {
23 my $class = shift;
24 my $args = shift;
25  
26 if ( not defined $args )
27 {
28 $args = [ ];
29 }
30 elsif ( ref($args) ne 'ARRAY' )
31 {
32 $args = [ $args ];
33 }
34  
35 my $relationship;
36  
37 if ( scalar @$args > 1 )
38 {
39 # TODO: is this really that simple?
40 $relationship = $MANY_TO_MANY;
41 }
42 else
43 {
44 my $fn = $args->[0];
45  
46 my $local_key = $fn->get_table()->get_column_names({ primary_key => 1 });
47 my $foreign_key = $fn->get_reference_table()->get_column_names({ primary_key => 1 });
48  
49 # check if the local or foreign connection has the complete table key
50 my $has_local_key = $fn->is_local_column_names( $local_key );
51 my $has_foreign_key = $fn->is_foreign_column_names( $foreign_key );
52  
53 # look-up the appropriate relationship
54 if ( not $has_local_key and $has_foreign_key )
55 {
56 $relationship = $MANY_TO_ONE;
57 }
58 elsif ( $has_local_key and not $has_foreign_key )
59 {
60 $relationship = $ONE_TO_MANY;
61 }
62 elsif ( $has_local_key and $has_foreign_key )
63 {
64 $relationship = $ONE_TO_ONE;
65 }
66 else
67 {
68 $relationship = $MANY_TO_MANY;
69 }
70 }
71  
72 my $self =
73 {
74 foreign_keys => $args,
75 relationship => $relationship
76 };
77  
78 bless $self, $class;
79 return $self;
80 }
81  
82 sub get_foreign_keys { return shift->{foreign_keys}; }
83 sub get_count { return scalar @{shift->{foreign_keys}}; }
84 sub get_start { return shift->{foreign_keys}->[0]; }
85 sub get_end { return shift->{foreign_keys}->[-1]; }
86 sub get_start_column_names { return shift->get_start()->get_local_column_names(); }
87 sub get_end_column_names { return shift->get_end()->get_foreign_column_names(); }
88 sub get_start_table_name { return shift->get_start()->get_table_name(); }
89 sub get_end_table_name { return shift->get_end()->get_reference_table_name(); }
90 sub get_relationship { return shift->{relationship}; }
91  
92 sub is_start_column_names
93 {
94 my ($self, $column_names) = @_;
95 return $self->get_start()->is_local_column_names( $column_names );
96 }
97  
98 sub is_end_column_names
99 {
100 my ($self, $column_names) = @_;
101 return $self->get_end()->is_foreign_column_names( $column_names );
102 }
103  
104 # NOTE: a convenience function for when you *know* it is or can only accept a link
105 # with a single foreign in it.
106 sub get_foreign_key
107 {
108 my $self = shift;
109  
110 if ( $self->get_count() > 1 )
111 {
112 die "Link contains a chain of foreign keys rather than a single than a single foreign key which is what we expected. Could be a bug in Xmldoom!";
113 }
114  
115 return $self->get_foreign_keys()->[0];
116 }
117  
118 # convenience functions that work like get_foreign_key().
119 sub get_column_names { return shift->get_foreign_key()->get_column_names(); }
120  
121 sub equals
122 {
123 my ($self, $link) = @_;
124  
125 if ( $self->get_count() != $link->get_count() )
126 {
127 return 0;
128 }
129  
130 for( my $i = 0; $i < $self->get_count(); $i++ )
131 {
132 if ( not $self->{foreign_keys}->[$i]->equals( $link->{foreign_keys}->[$i] ) )
133 {
134 return 0;
135 }
136 }
137  
138 return 1;
139 }
140  
141 sub contains
142 {
143 my ($self, $link) = @_;
144  
145 if ( $link->get_count() > $self->get_count() )
146 {
147 return 0;
148 }
149
150 my $offset = ($self->get_count() - $self->get_count());
151  
152 # attempt to locate the correct offset
153 while ( $offset >= 0 )
154 {
155 if ( $self->{foreign_keys}->[$offset]->equals( $link->{foreign_keys}->[0] ) )
156 {
157 # this is it, yo!
158 last;
159 }
160  
161 $offset --;
162 }
163  
164 # couldn't locate the appropriate offset, so it couldn't possibly contain it.
165 if ( $offset < 0 )
166 {
167 return 0;
168 }
169  
170 # count up from the offset and make sure the rest is equal
171 for( my $i = 1; $i < $link->get_count(); $i++ )
172 {
173 if ( not $self->{foreign_keys}->[$i+$offset]->equals( $link->{foreign_keys}->[$i] ) )
174 {
175 return 0;
176 }
177 }
178  
179 return 1;
180 }
181  
182 # this is a static function.
183 sub reduce_longest
184 {
185 my $link_list = shift;
186  
187 my @longest;
188  
189 foreach my $link1 ( @$link_list )
190 {
191 my $is_contained = 0;
192  
193 foreach my $link2 ( @$link_list )
194 {
195 if ( $link1 == $link2 )
196 {
197 next;
198 }
199
200 if ( $link2->contains( $link1 ) )
201 {
202 $is_contained = 1;
203 last;
204 }
205 }
206  
207 if ( not $is_contained )
208 {
209 push @longest, $link1;
210 }
211 }
212  
213 return \@longest;
214 }
215  
216 # this is a static function.
217 sub reduce_shortest
218 {
219 my $link_list = shift;
220  
221 my @shortest;
222  
223 foreach my $link1 ( @$link_list )
224 {
225 my $is_container = 0;
226  
227 foreach my $link2 ( @$link_list )
228 {
229 if ( $link1 == $link2 )
230 {
231 next;
232 }
233
234 if ( $link1->contains( $link2 ) )
235 {
236 $is_container = 1;
237 last;
238 }
239 }
240  
241 if ( not $is_container )
242 {
243 push @shortest, $link1;
244 }
245 }
246  
247 return \@shortest;
248 }
249  
250 sub clone_reverse
251 {
252 my $self = shift;
253  
254 my @foreign_keys;
255 foreach my $fkey ( @{$self->{foreign_keys}} )
256 {
257 unshift @foreign_keys, $fkey->clone_reverse();
258 }
259  
260 return Xmldoom::Definition::Link->new( \@foreign_keys );
261 }
262  
263 1;
264