Alzabo/Table.pm

filter: Statementstmt 26.2% 36 / 137 Branchbran 0.0% 0 / 42 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 36.6% 11 / 30 Podpod 100% 18 / 18 totaltotal 28.6% 65 / 227 CC 29 SCAR 28.5
CC
29
Cov
20%
foreign_keys_by_column
42.0
all_foreign_keys
42.0
foreign_keys
30.0
CRAP 17.2
« Alzabo/Runtime/Row.pm ↑ Summary Alzabo/SQLMaker/MySQL.pm »
1 package Alzabo::Table;
2  
3 11 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 11 ▶use vars qw($VERSION);
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 11 ▶use Alzabo;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 11 ▶use Params::Validate qw( :all );
Subroutine BEGIN: called CC=1 SCAR=0.0
9 0 ▶Params::Validate::validation_options( on_fail => sub { Alzabo::Exception::Params->throw( error => join '', @_ ) } );
Subroutine __ANON__: not called CC=1 SCAR=1.2
10  
11 11 ▶use Tie::IxHash;
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 11 $VERSION = 2.0;
14  
15 11 1;
16  
17 sub schema
18 0 ▶{
Subroutine schema: not called CC=1 SCAR=6.9
Pod documented
19 0 my $self = shift;
20  
21 0 return $self->{schema};
22 }
23  
24 sub name
25 0 ▶{
Subroutine name: not called CC=1 SCAR=6.9
Pod documented
26 0 my $self = shift;
27  
28 0 return $self->{name};
29 }
30  
31 11 ▶use constant HAS_COLUMN_SPEC => { type => SCALAR };
Subroutine BEGIN: called CC=1 SCAR=0.0
32  
33 sub has_column
34 0 ▶{
Subroutine has_column: not called CC=1 SCAR=6.9
Pod documented
35 0 my $self = shift;
36  
37 0 validate_pos( @_, HAS_COLUMN_SPEC );
38  
39 0 return $self->{columns}->FETCH(shift);
40 }
41  
42 sub column
43 0 ▶{
Subroutine column: not called CC=2 SCAR=17.9
Pod documented
44 0 my $self = shift;
45 0 my $name = shift;
46  
47 0 ▶ if ( my $col = $self->{columns}->FETCH($name) )
Branch: 1 branch
BranchTrueFalseTotal
if (my $col = $self->{'columns'}->FETCH($name)) { } 0 0 0
48 {
49 0 return $col;
50 }
51 else
52 {
53 0 Alzabo::Exception::Params->throw
54 ( error => "Column $name doesn't exist in $self->{name}" );
55 }
56 }
57  
58 sub columns
59 0 ▶{
Subroutine columns: not called CC=3 SCAR=24.8
Pod documented
60 0 my $self = shift;
61  
62 0 ▶ return $self->column(@_) if @_ ==1 ;
Branch: 1 branch
BranchTrueFalseTotal
if @_ == 1 0 0 0
63 0 ▶ return map { $self->column($_) } @_ if @_ > 1;
Branch: 1 branch
BranchTrueFalseTotal
if @_ > 1 0 0 0
64 0 return $self->{columns}->Values;
65 }
66  
67 sub primary_key
68 0 ▶{
Subroutine primary_key: not called CC=3 SCAR=24.8
Pod documented
69 0 my $self = shift;
70  
71 0 ▶ return unless @{ $self->{pk} };
Branch: 1 branch
BranchTrueFalseTotal
unless @{$self->{"pk"};} 0 0 0
72  
73 return ( wantarray ?
74 0 $self->{columns}->Values( @{ $self->{pk} } ) :
75 0 ▶ $self->{columns}->Values( $self->{pk}[0] )
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 0 0
76 );
77 }
78  
79 sub primary_key_size
80 0 ▶{
Subroutine primary_key_size: not called CC=1 SCAR=6.9
Pod documented
81 0 my $self = shift;
82  
83 0 return scalar @{ $self->{pk} };
84 }
85  
86 11 ▶use constant COLUMN_IS_PRIMARY_KEY_SPEC => { isa => 'Alzabo::Column' };
Subroutine BEGIN: called CC=1 SCAR=0.0
87  
88 sub column_is_primary_key
89 0 ▶{
Subroutine column_is_primary_key: not called CC=3 SCAR=24.8
Pod documented
90 0 my $self = shift;
91  
92 0 validate_pos( @_, COLUMN_IS_PRIMARY_KEY_SPEC );
93  
94 0 my $name = shift->name;
95  
96 Alzabo::Exception::Params->throw( error => "Column $name doesn't exist in $self->{name}" )
97 0 ▶ unless $self->{columns}->EXISTS($name);
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"columns"}->EXISTS($name) 0 0 0
98  
99 0 my $idx = $self->{columns}->Indices($name);
100 0 ▶ return 1 if grep { $idx == $_ } @{ $self->{pk} };
Branch: 1 branch
BranchTrueFalseTotal
if grep {$idx == $_;} @{$$self{"pk"};} 0 0 0
101  
102 0 return 0;
103 }
104  
105 sub attributes
106 0 ▶{
Subroutine attributes: not called CC=1 SCAR=6.9
Pod documented
107 0 return keys %{ $_[0]->{attributes} };
108 }
109  
110 11 ▶use constant HAS_ATTRIBUTE_SPEC => { attribute => { type => SCALAR },
Subroutine BEGIN: called CC=1 SCAR=0.0
111 case_sensitive => { type => SCALAR,
112 default => 0 },
113 11 };
114  
115 sub has_attribute
116 0 ▶{
Subroutine has_attribute: not called CC=3 SCAR=24.8
Pod documented
117 0 my $self = shift;
118 0 my %p = validate( @_, HAS_ATTRIBUTE_SPEC );
119  
120 0 ▶ if ( $p{case_sensitive} )
Branch: 1 branch
BranchTrueFalseTotal
if ($p{'case_sensitive'}) { } 0 0 0
121 {
122 0 return exists $self->{attributes}{ $p{attribute} };
123 }
124 else
125 {
126 0 ▶ return 1 if grep { lc $p{attribute} eq lc $_ } keys %{ $self->{attributes} };
Branch: 1 branch
BranchTrueFalseTotal
if grep {lc $p{"attribute"} eq lc $_;} keys %{$$self{"attributes"};} 0 0 0
127 }
128 }
129  
130 11 ▶use constant FOREIGN_KEYS_SPEC => { column => { isa => 'Alzabo::Column' },
Subroutine BEGIN: called CC=1 SCAR=0.0
131 table => { isa => 'Alzabo::Table' },
132 11 };
133  
134 sub foreign_keys
135 0 ▶{
Subroutine foreign_keys: not called CC=5 SCAR=34.0
Pod documented
136 0 my $self = shift;
137  
138 0 validate( @_, FOREIGN_KEYS_SPEC );
139 0 my %p = @_;
140  
141 0 my $c_name = $p{column}->name;
142 0 my $t_name = $p{table}->name;
143  
144 Alzabo::Exception::Params->throw( error => "Column $c_name doesn't exist in $self->{name}" )
145 0 ▶ unless $self->{columns}->EXISTS($c_name);
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"columns"}->EXISTS($c_name) 0 0 0
146  
147 Alzabo::Exception::Params->throw( error => "No foreign keys to $t_name exist in $self->{name}" )
148 0 ▶ unless exists $self->{fk}{$t_name};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $self->{"fk"}{$t_name} 0 0 0
149  
150 Alzabo::Exception::Params->throw( error => "Column $c_name is not a foreign key to $t_name in $self->{name}" )
151 0 ▶ unless exists $self->{fk}{$t_name}{$c_name};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $self->{"fk"}{$t_name}{$c_name} 0 0 0
152  
153 0 ▶ return wantarray ? @{ $self->{fk}{$t_name}{$c_name} } : $self->{fk}{$t_name}{$c_name}[0];
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 0 0
154 }
155  
156 11 ▶use constant FOREIGN_KEYS_BY_TABLE_SPEC => { isa => 'Alzabo::Table' };
Subroutine BEGIN: called CC=1 SCAR=0.0
157  
158 sub foreign_keys_by_table
159 0 ▶{
Subroutine foreign_keys_by_table: not called CC=5 SCAR=34.0
Pod documented
160 0 my $self = shift;
161  
162 0 validate_pos( @_, FOREIGN_KEYS_BY_TABLE_SPEC );
163 0 my $name = shift->name;
164  
165 0 my $fk = $self->{fk};
166  
167 0 my %fk;
168 0 ▶ if ( exists $fk->{$name} )
Branch: 1 branch
BranchTrueFalseTotal
if (exists $fk->{$name}) 0 0 0
169 {
170 0 foreach my $c ( keys %{ $fk->{$name} } )
171 {
172 0 ▶ return $fk->{$name}{$c}[0] unless wantarray;
Branch: 1 branch
BranchTrueFalseTotal
unless wantarray 0 0 0
173  
174 0 $fk{$_} = $_ for @{ $fk->{$name}{$c} };
175 }
176 }
177  
178 0 return values %fk;
179 }
180  
181 11 ▶use constant FOREIGN_KEYS_BY_COLUMN_SPEC => { isa => 'Alzabo::Column' };
Subroutine BEGIN: called CC=1 SCAR=0.0
182  
183 sub foreign_keys_by_column
184 0 ▶{
Subroutine foreign_keys_by_column: not called CC=6 SCAR=37.4
Pod documented
185 0 my $self = shift;
186  
187 0 my ($col) = validate_pos( @_, FOREIGN_KEYS_BY_COLUMN_SPEC );
188  
189 Alzabo::Exception::Params->throw( error => "Column " . $col->name . " doesn't exist in $self->{name}" )
190 0 ▶ unless $self->{columns}->EXISTS( $col->name );
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"columns"}->EXISTS($col->name) 0 0 0
191  
192 0 my $fk = $self->{fk};
193  
194 0 my %fk;
195 0 foreach my $t (keys %$fk)
196 {
197 0 ▶ if ( exists $fk->{$t}{ $col->name } )
Branch: 1 branch
BranchTrueFalseTotal
if (exists $fk->{$t}{$col->name}) 0 0 0
198 {
199 0 ▶ return $fk->{$t}{ $col->name }[0] unless wantarray;
Branch: 1 branch
BranchTrueFalseTotal
unless wantarray 0 0 0
200  
201 0 $fk{$_} = $_ for @{ $fk->{$t}{ $col->name } };
202 }
203 }
204  
205 0 return values %fk;
206 }
207  
208 sub all_foreign_keys
209 0 ▶{
Subroutine all_foreign_keys: not called CC=6 SCAR=37.4
Pod documented
210 0 my $self = shift;
211  
212 0 my %seen;
213 my @fk;
214 0 my $fk = $self->{fk};
215 0 foreach my $t (keys %$fk)
216 {
217 0 foreach my $c ( keys %{ $fk->{$t} } )
218 {
219 0 foreach my $key ( @{ $fk->{$t}{$c} } )
220 {
221 0 ▶ next if $seen{$key};
Branch: 1 branch
BranchTrueFalseTotal
if $seen{$key} 0 0 0
222 0 push @fk, $key;
223 0 $seen{$key} = 1;
224 }
225 }
226 }
227  
228 0 ▶ return wantarray ? @fk : $fk[0];
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 0 0
229 }
230  
231 sub index
232 0 ▶{
Subroutine index: not called CC=2 SCAR=17.9
Pod documented
233 0 my $self = shift;
234  
235 0 validate_pos( @_, { type => SCALAR } );
236 0 my $id = shift;
237  
238 Alzabo::Exception::Params->throw( error => "Index $id doesn't exist in $self->{name}" )
239 0 ▶ unless $self->{indexes}->EXISTS($id);
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"indexes"}->EXISTS($id) 0 0 0
240  
241 0 return $self->{indexes}->FETCH($id);
242 }
243  
244 sub has_index
245 0 ▶{
Subroutine has_index: not called CC=1 SCAR=6.9
Pod documented
246 0 my $self = shift;
247  
248 0 validate_pos( @_, { type => SCALAR } );
249 0 my $id = shift;
250  
251 0 return $self->{indexes}->EXISTS($id);
252 }
253  
254 sub indexes
255 0 ▶{
Subroutine indexes: not called CC=1 SCAR=6.9
Pod documented
256 0 my $self = shift;
257  
258 0 return $self->{indexes}->Values;
259 }
260  
261 0 ▶sub comment { $_[0]->{comment} }
Subroutine comment: not called CC=1 SCAR=6.9
Pod documented
262  
263 __END__
« Alzabo/Runtime/Row.pm ↑ Summary Alzabo/SQLMaker/MySQL.pm »