Xmldoom/Schema/ForeignKey.pm untested

filter: Statementstmt 0.0% 0 / 82 Branchbran 0.0% 0 / 10 Conditioncond 0.0% 0 / 9 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 0.0% 0 / 20 Podpod 0.0% 0 / 18 totaltotal 0.0% 0 / 143 CC 6 SCAR 15.5
CC
6
Cov
0%
new
12.0
_is_list
12.0
get_foreign_pair_name
6.0
CRAP 4.7
1  
2 package Xmldoom::Schema::ForeignKey;
3  
4 use Xmldoom::Threads;
5 use strict;
6  
7 sub new
8 {
9 my $class = shift;
10 my $args = shift;
11  
12 my $parent;
13 my $reference_table;
14 my $local_columns;
15 my $foreign_columns;
16  
17 if ( ref($args) eq 'HASH' )
18 {
19 $parent = $args->{parent};
20 $reference_table = $args->{reference_table};
21 $local_columns = $args->{local_columns};
22 $foreign_columns = $args->{foreign_columns};
23 }
24  
25 # make sure that the count of the two array's is the same.
26 if ( scalar @$local_columns != scalar @$foreign_columns )
27 {
28 die "A foreign-key must have the same number of local and foreign columns";
29 }
30  
31 my $self = {
32 parent => $parent,
33 reference_table => $reference_table,
34 local_columns => $local_columns,
35 foreign_columns => $foreign_columns
36 };
37  
38 bless $self, $class;
39 return Xmldoom::Threads::make_shared($self, $args->{shared});
40 }
41  
42 sub DESTROY
43 {
44 my $self = shift;
45  
46 $self->{parent} = undef;
47 }
48  
49 sub get_table { return shift->{parent}; }
50 sub get_table_name { return shift->{parent}->get_name(); }
51 sub get_reference_table_name { return shift->{reference_table}; }
52 sub get_local_column_names { return shift->{local_columns}; }
53 sub get_foreign_column_names { return shift->{foreign_columns}; }
54  
55 sub get_reference_table
56 {
57 my $self = shift;
58 return $self->get_table()->get_schema()->get_table( $self->{reference_table} );
59 }
60  
61 sub get_local_columns
62 {
63 my $self = shift;
64 return $self->get_table()->get_columns( $self->{local_columns} );
65 }
66  
67 sub get_foreign_columns
68 {
69 my $self = shift;
70 return $self->get_reference_table()->get_columns( $self->{foreign_columns} );
71 }
72  
73 sub _is_list
74 {
75 my ($list1, $list2) = @_;
76  
77 # quickest check, the lengths!
78 if ( scalar @$list1 != scalar @$list2 )
79 {
80 return 0;
81 }
82  
83 name1: foreach my $name1 ( @$list1 )
84 {
85 foreach my $name2 ( @$list2 )
86 {
87 if ( $name1 eq $name2 )
88 {
89 next name1;
90 }
91 }
92 return 0;
93 }
94  
95 return 1;
96 }
97  
98 sub is_local_column_names
99 {
100 my ($self, $names) = @_;
101 return _is_list($self->{local_columns}, $names);
102 }
103  
104 sub is_foreign_column_names
105 {
106 my ($self, $names) = @_;
107 return _is_list($self->{foreign_columns}, $names);
108 }
109  
110 # TODO: this function was create soley for Table::get_foreign_key() which is
111 # broken anyway, so the usefulness of this function is debatable.
112 sub get_foreign_pair_name
113 {
114 my ($self, $name) = @_;
115  
116 my @pair = grep { $_->{local_column} eq $name } @{$self->get_column_names()};
117  
118 if ( scalar @pair > 0 )
119 {
120 return $pair[0]->{foreign_column};
121 }
122  
123 return undef;
124 }
125  
126 sub get_column_names
127 {
128 my $self = shift;
129  
130 my @ret;
131 my $count = scalar @{$self->{local_columns}};
132  
133 for( my $i = 0; $i < $count; $i++ )
134 {
135 push @ret, {
136 local_column => $self->{local_columns}->[$i],
137 foreign_column => $self->{foreign_columns}->[$i],
138  
139 # for compatibility:
140 local_table => $self->get_table_name(),
141 foreign_table => $self->get_reference_table_name()
142 };
143 }
144  
145 return \@ret;
146 }
147  
148 sub get_columns
149 {
150 my $self = shift;
151  
152 my $local_table = $self->get_table();
153 my $foreign_table = $self->get_reference_table();
154  
155 my @ret;
156 my $count = scalar @{$self->{local_columns}};
157  
158 for( my $i = 0; $i < $count; $i++ )
159 {
160 push @ret, {
161 local_column => $local_table->get_column( $self->{local_columns}->[$i] ),
162 foreign_column => $foreign_table->get_column( $self->{foreign_columns}->[$i] )
163 };
164 }
165  
166 return \@ret;
167 }
168  
169 sub equals
170 {
171 my ($self, $fkey) = @_;
172  
173 return ( $self->{parent} == $fkey->{parent} and
174 $self->{reference_table} eq $fkey->{reference_table} and
175 _is_list($self->{local_columns}, $fkey->{local_columns}) and
176 _is_list($self->{foreign_columns}, $fkey->{foreign_columns}) );
177  
178 }
179  
180 sub clone_reverse
181 {
182 my $self = shift;
183  
184 return Xmldoom::Schema::ForeignKey->new({
185 parent => $self->get_reference_table(),
186 reference_table => $self->get_table_name(),
187 local_columns => $self->get_foreign_column_names(),
188 foreign_columns => $self->get_local_column_names()
189 });
190 }
191  
192 1;
193