Fey/Table.pm

filter: Statementstmt 100% 156 / 156 Branchbran 95.2% 40 / 42 Conditioncond 100% 2 / 2 MC/DCmcdc 100% 1 / 1 Subroutinesub 100% 37 / 37 Podpod 100% 10 / 10 totaltotal 99.1% 246 / 248 CC 31 SCAR 12.9
CC
31
Cov
99%
add_candidate_key
7.0
remove_candidate_key
7.0
has_candidate_key
7.0
CRAP 3.6
« Fey/SQL/Insert.pm ↑ Summary Fey/SQL/Fragment/Join.pm »
1 package Fey::Table;
2  
3 26 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 26 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 26 ▶use namespace::autoclean;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 26 our $VERSION = '0.44';
8  
9 26 ▶use Fey::Column;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 26 ▶use Fey::Exceptions qw( param_error );
Subroutine BEGIN: called CC=1 SCAR=0.0
11 26 ▶use Fey::NamedObjectSet;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 26 ▶use Fey::Schema;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 26 ▶use Fey::Table::Alias;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 26 ▶use Fey::Types qw(
Subroutine BEGIN: called CC=1 SCAR=0.0
15 ArrayRef Bool HashRef Str Undef Column ColumnOrName NamedObjectSet Schema
16 26 );
17 26 ▶use List::AllUtils qw( any all first_index );
Subroutine BEGIN: called CC=1 SCAR=0.0
18 26 ▶use Scalar::Util qw( blessed weaken );
Subroutine BEGIN: called CC=1 SCAR=0.0
19  
20 26 ▶use Moose 2.1200;
Subroutine BEGIN: called CC=1 SCAR=0.0
21 26 ▶use MooseX::Params::Validate 0.21 qw( pos_validated_list );
Subroutine BEGIN: called CC=1 SCAR=0.0
22 26 ▶use MooseX::SemiAffordanceAccessor 0.03;
Subroutine BEGIN: called CC=1 SCAR=0.0
23 26 ▶use MooseX::StrictConstructor 0.13;
Subroutine BEGIN: called CC=1 SCAR=0.0
24 26 ▶use Moose::Util::TypeConstraints;
Subroutine BEGIN: called CC=1 SCAR=0.0
25  
26 26 with 'Fey::Role::TableLike';
27  
28 26 with 'Fey::Role::MakesAliasObjects' => {
29 self_param => 'table',
30 alias_class => 'Fey::Table::Alias',
31 };
32  
33 26 has 'id' => (
34 is => 'ro',
35 lazy_build => 1,
36 init_arg => undef,
37 );
38  
39 26 has 'name' => (
40 is => 'ro',
41 isa => Str,
42 required => 1,
43 );
44  
45 26 has 'is_view' => (
46 is => 'ro',
47 isa => Bool,
48 default => 0,
49 );
50  
51 has '_keys' => (
52 traits => ['Array'],
53 is => 'bare',
54 isa => ArrayRef [NamedObjectSet],
55 209 ▶ default => sub { [] },
Subroutine __ANON__: called CC=1 SCAR=0.0
56 26 handles => {
57 _keys => 'elements',
58 _add_key => 'push',
59 _delete_key => 'splice',
60 },
61  
62 );
63  
64 has '_columns' => (
65 is => 'ro',
66 isa => NamedObjectSet,
67 209 ▶ default => sub { return Fey::NamedObjectSet->new() },
Subroutine __ANON__: called CC=1 SCAR=0.0
68 26 handles => {
69 columns => 'objects',
70 column => 'object',
71 },
72 );
73  
74 26 has 'schema' => (
75 is => 'rw',
76 isa => Undef | Schema,
77 weak_ref => 1,
78 writer => '_set_schema',
79 clearer => '_clear_schema',
80 predicate => 'has_schema',
81 );
82  
83 26 has 'candidate_keys' => (
84 is => 'ro',
85 isa => ArrayRef [ ArrayRef [Column] ],
86 clearer => '_clear_candidate_keys',
87 lazy_build => 1,
88 init_arg => undef,
89 );
90  
91 309 ▶after '_add_key', '_delete_key' => sub { $_[0]->_clear_candidate_keys() };
Subroutine __ANON__: called CC=1 SCAR=0.0
92  
93 26 has 'primary_key' => (
94 is => 'ro',
95 isa => ArrayRef [Column],
96 clearer => '_clear_primary_key',
97 lazy_build => 1,
98 init_arg => undef,
99 );
100  
101 309 ▶after '_clear_candidate_keys' => sub { $_[0]->_clear_primary_key() };
Subroutine __ANON__: called CC=1 SCAR=0.0
102  
103 has '_aliased_tables' => (
104 traits => ['Hash'],
105 is => 'bare',
106 isa => HashRef,
107 lazy => 1,
108 4 ▶ default => sub { {} },
Subroutine __ANON__: called CC=1 SCAR=0.0
109 26 handles => {
110 _aliased_table => 'get',
111 _store_aliased_table => 'set',
112 _has_aliased_table => 'exists',
113 },
114 );
115  
116 26 with 'Fey::Role::Named';
117  
118 664 ▶sub add_column {
Subroutine add_column: called CC=2 SCAR=6.9
Pod documented
119 664 my $self = shift;
120 664 my ($col) = pos_validated_list( \@_, { isa => Column } );
121  
122 664 my $name = $col->name();
123 664 ▶ param_error "The table already has a column named $name."
Branch: 1 branch
BranchTrueFalseTotal
if $self->column($name) 1 663 664
124 if $self->column($name);
125  
126 663 $self->_columns()->add($col);
127  
128 663 $col->_set_table($self);
129  
130 663 return $self;
131 }
132  
133 5 ▶sub remove_column {
Subroutine remove_column: called CC=6 SCAR=17.9
Pod documented
134 5 my $self = shift;
135 5 my ($col)
136 = pos_validated_list( \@_, { isa => ColumnOrName } );
137  
138 5 ▶ $col = $self->column($col)
Branch: 1 branch
BranchTrueFalseTotal
unless builtin::blessed($col) 3 2 5
139 unless blessed $col;
140  
141 5 ▶ if ( my $schema = $self->schema() ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $schema = $self->schema) 1 4 5
142 1 for my $fk ( grep { $_->has_column($col) }
143 $schema->foreign_keys_for_table($self) ) {
144 1 $schema->remove_foreign_key($fk);
145 }
146 }
147  
148 5 my $name = $col->name();
149  
150 5 for my $k ( $self->_keys() ) {
151 4 ▶ $self->remove_candidate_key( $k->objects() )
Branch: 1 branch
BranchTrueFalseTotal
if $k->object($name) 2 2 4
152 if $k->object($name);
153 }
154  
155 5 $self->_columns()->delete($col);
156  
157 5 $col->_clear_table();
158  
159 5 return $self;
160 }
161  
162 7 ▶sub _build_candidate_keys {
Subroutine _build_candidate_keys: called CC=1 SCAR=0.0
163 7 my $self = shift;
164  
165 7 return [ map { [ $_->objects() ] } $self->_keys() ];
166 }
167  
168 5 ▶sub _build_primary_key {
Subroutine _build_primary_key: called CC=2 SCAR=6.9
169 5 my $self = shift;
170  
171 5 my $keys = $self->candidate_keys();
172  
173 5 ▶ return $keys->[0] || [];
Condition100%2/2
$keys->[0] || []
l!l
41
MC/DC100%1/1
$keys->[0] || []
$keys->[0]
Truth table100%2/2
A
A= $keys->[0]
Aresult
11
00
174 }
175  
176 307 ▶sub add_candidate_key {
Subroutine add_candidate_key: called CC=7 SCAR=19.5
Pod documented
177 307 my $self = shift;
178  
179 307 ▶ my $count = @_ ? @_ : 1;
Branch: 1 branch
BranchTrueFalseTotal
@_ ? : 306 1 307
180 307 my (@cols) = pos_validated_list(
181 \@_,
182 ( ( { isa => ColumnOrName } ) x $count ),
183 MX_PARAMS_VALIDATE_NO_CACHE => 1,
184 );
185  
186 306 ▶ for my $name ( map { blessed $_ ? $_->name() : $_ } @cols ) {
Branch: 1 branch
BranchTrueFalseTotal
builtin::blessed($_) ? : 350 7 357
187 357 ▶ param_error "The column $name is not part of the "
Branch: 1 branch
BranchTrueFalseTotal
unless $self->column($name) 1 356 357
188 . $self->name()
189 . ' table.'
190 unless $self->column($name);
191 }
192  
193 305 $_ = $self->column($_) for grep { !blessed $_ } @cols;
194  
195 305 ▶ return if $self->has_candidate_key(@cols);
Branch: 1 branch
BranchTrueFalseTotal
if $self->has_candidate_key(@cols) 1 304 305
196  
197 304 $self->_add_key( Fey::NamedObjectSet->new(@cols) );
198  
199 304 return;
200 }
201  
202 8 ▶sub remove_candidate_key {
Subroutine remove_candidate_key: called CC=7 SCAR=19.5
Pod documented
203 8 my $self = shift;
204  
205 8 ▶ my $count = @_ ? @_ : 1;
Branch: 1 branch
BranchTrueFalseTotal
@_ ? : 7 1 8
206 8 my (@cols) = pos_validated_list(
207 \@_,
208 ( ( { isa => ColumnOrName } ) x $count ),
209 MX_PARAMS_VALIDATE_NO_CACHE => 1,
210 );
211  
212 7 ▶ for my $name ( map { blessed $_ ? $_->name() : $_ } @cols ) {
Branch: 1 branch
BranchTrueFalseTotal
builtin::blessed($_) ? : 4 3 7
213 7 ▶ param_error "The column $name is not part of the "
Branch: 1 branch
BranchTrueFalseTotal
unless $self->column($name) 1 6 7
214 . $self->name()
215 . ' table.'
216 unless $self->column($name);
217 }
218  
219 6 $_ = $self->column($_) for grep { !blessed $_ } @cols;
220  
221 6 my $set = Fey::NamedObjectSet->new(@cols);
222  
223 6 ▶ my $idx = first_index { $_->is_same_as($set) } $self->_keys();
Subroutine __ANON__: called CC=1 SCAR=0.0
224  
225 6 ▶ $self->_delete_key( $idx, 1 )
Branch: 1 branch
BranchTrueFalseTotal
if $idx >= 0 5 1 6
226 if $idx >= 0;
227  
228 6 return;
229 }
230  
231 310 ▶sub has_candidate_key {
Subroutine has_candidate_key: called CC=7 SCAR=19.5
Pod documented
232 310 my $self = shift;
233  
234 310 ▶ my $count = @_ ? @_ : 1;
Branch: 1 branch
BranchTrueFalseTotal
@_ ? : 309 1 310
235 310 my (@cols) = pos_validated_list(
236 \@_,
237 ( ( { isa => ColumnOrName } ) x $count ),
238 MX_PARAMS_VALIDATE_NO_CACHE => 1,
239 );
240  
241 309 ▶ for my $name ( map { blessed $_ ? $_->name() : $_ } @cols ) {
Branch: 1 branch
BranchTrueFalseTotal
builtin::blessed($_) ? : 356 5 361
242 361 ▶ param_error "The column $name is not part of the "
Branch: 1 branch
BranchTrueFalseTotal
unless $self->column($name) 1 360 361
243 . $self->name()
244 . ' table.'
245 unless $self->column($name);
246 }
247  
248 308 $_ = $self->column($_) for grep { !blessed $_ } @cols;
249  
250 308 my $set = Fey::NamedObjectSet->new(@cols);
251  
252 return 1
253 308 ▶ if any { $_->is_same_as($set) } $self->_keys();
Subroutine __ANON__: called CC=1 SCAR=0.0
Branch: 1 branch
BranchTrueFalseTotal
if &any(sub { $_->is_same_as($set); } , $self->_keys) 3 305 308
254  
255 305 return 0;
256 }
257  
258 # Caching the objects by name prevents a weird bug where we have two
259 # aliases of the same name, and one disappears because of weak
260 # references, causing weird errors.
261 7 ▶around 'alias' => sub {
Subroutine __ANON__: called CC=4 SCAR=13.9
262 7 my $orig = shift;
263 7 my $self = shift;
264  
265 # bleh, duplicating code from Aliasable
266 7 ▶ my %p = @_ == 1 ? ( alias_name => $_[0] ) : @_;
Branch: 1 branch
BranchTrueFalseTotal
@_ == 1 ? : 1 6 7
267  
268 7 ▶ if ( defined $p{alias_name} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $p{"alias_name"}) 4 3 7
269 return $self->_aliased_table( $p{alias_name} )
270 4 ▶ if $self->_has_aliased_table( $p{alias_name} );
Branch: 1 branch
BranchTrueFalseTotal
if $self->_has_aliased_table($p{"alias_name"}) 0 4 4
271 }
272  
273 7 my $alias = $orig->( $self, %p );
274  
275 7 $self->_store_aliased_table( $alias->alias_name() => $alias );
276  
277 7 return $alias;
278 26 };
279  
280 202 ▶sub is_alias {0}
Subroutine is_alias: called CC=1 SCAR=0.0
Pod documented
281  
282 6 ▶sub aliased_column {
Subroutine aliased_column: called CC=2 SCAR=7.0
Pod documented
283 6 my $self = shift;
284 6 my $prefix = shift;
285 6 my $name = shift;
286  
287 6 ▶ my $col = $self->column($name)
Branch: 1 branch
BranchTrueFalseTotal
my $col = $self->column($name) or return 6 0 6
288 or return;
289  
290 6 return $col->alias( alias_name => $prefix . $col->name() );
291 }
292  
293 2 ▶sub aliased_columns {
Subroutine aliased_columns: called CC=2 SCAR=6.9
Pod documented
294 2 my $self = shift;
295 2 my $prefix = shift;
296  
297 2 ▶ my @names = @_ ? @_ : map { $_->name() } $self->columns();
Branch: 1 branch
BranchTrueFalseTotal
@_ ? : 1 1 2
298  
299 2 return map { $self->aliased_column( $prefix, $_ ) } @names;
300 }
301  
302 71 ▶sub sql {
Subroutine sql: called CC=1 SCAR=0.0
Pod documented
303 71 return $_[1]->quote_identifier( $_[0]->name() );
304 }
305  
306 71 ▶sub sql_with_alias { goto &sql }
Subroutine sql_with_alias: called CC=1 SCAR=0.0
Pod documented
307  
308 12 ▶sub _build_id { $_[0]->name() }
Subroutine _build_id: called CC=1 SCAR=0.0
309  
310 26 __PACKAGE__->meta()->make_immutable();
311  
312 26 1;
313  
314 # ABSTRACT: Represents a table (or view)
315  
316 __END__
« Fey/SQL/Insert.pm ↑ Summary Fey/SQL/Fragment/Join.pm »