Fey/Schema.pm

filter: Statementstmt 96.8% 122 / 126 Branchbran 81.8% 18 / 22 Conditioncond 100% 7 / 7 MC/DCmcdc 100% 4 / 4 Subroutinesub 100% 21 / 21 Podpod 100% 6 / 6 totaltotal 95.7% 178 / 186 CC 21 SCAR 16.2
CC
21
Cov
95%
foreign_keys_between_tables
12.7
remove_table
3.0
add_foreign_key
3.0
CRAP 5.0
« Fey/Types/Internal.pm ↑ Summary Fey/SQL/Update.pm »
1 package Fey::Schema;
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::Exceptions qw( param_error );
Subroutine BEGIN: called CC=1 SCAR=0.0
10 26 ▶use Fey::NamedObjectSet;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 26 ▶use Fey::SQL;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 26 ▶use Fey::Table;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 use Fey::Types
14 26 ▶ qw( FK HashRef NamedObjectSet Str Table TableLikeOrName TableOrName );
Subroutine BEGIN: called CC=1 SCAR=0.0
15 26 ▶use Scalar::Util qw( blessed );
Subroutine BEGIN: called CC=1 SCAR=0.0
16  
17 26 ▶use Moose 2.1200;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 26 ▶use MooseX::Params::Validate 0.21 qw( pos_validated_list );
Subroutine BEGIN: called CC=1 SCAR=0.0
19 26 ▶use MooseX::SemiAffordanceAccessor 0.03;
Subroutine BEGIN: called CC=1 SCAR=0.0
20 26 ▶use MooseX::StrictConstructor 0.13;
Subroutine BEGIN: called CC=1 SCAR=0.0
21  
22 26 has 'name' => (
23 is => 'rw',
24 isa => Str,
25 required => 1,
26 );
27  
28 has '_tables' => (
29 is => 'ro',
30 isa => NamedObjectSet,
31 52 ▶ default => sub { return Fey::NamedObjectSet->new() },
Subroutine __ANON__: called CC=1 SCAR=0.0
32 26 handles => {
33 tables => 'objects',
34 table => 'object',
35 },
36 init_arg => undef,
37 );
38  
39 has '_fks' => (
40 is => 'ro',
41 isa => HashRef,
42 52 ▶ default => sub { {} },
Subroutine __ANON__: called CC=1 SCAR=0.0
43 26 init_arg => undef,
44 );
45  
46 205 ▶sub add_table {
Subroutine add_table: called CC=2 SCAR=6.9
Pod documented
47 205 my $self = shift;
48 205 my ($table) = pos_validated_list( \@_, { isa => Table } );
49  
50 205 my $name = $table->name();
51 205 ▶ param_error "The schema already contains a table named $name."
Branch: 1 branch
BranchTrueFalseTotal
if $self->table($name) 1 204 205
52 if $self->table($name);
53  
54 204 $self->_tables->add($table);
55  
56 204 $table->_set_schema($self);
57  
58 204 return $self;
59 }
60  
61 3 ▶sub remove_table {
Subroutine remove_table: called CC=3 SCAR=11.0
Pod documented
62 3 my $self = shift;
63 3 my ($table)
64 = pos_validated_list( \@_, { isa => TableOrName } );
65  
66 3 ▶ $table = $self->table($table)
Branch: 1 branch
BranchTrueFalseTotal
unless builtin::blessed($table) 2 1 3
67 unless blessed $table;
68  
69 3 for my $fk ( $self->foreign_keys_for_table($table) ) {
70 1 $self->remove_foreign_key($fk);
71 }
72  
73 3 $self->_tables()->delete($table);
74  
75 3 $table->_set_schema(undef);
76  
77 3 return $self;
78 }
79  
80 125 ▶sub add_foreign_key {
Subroutine add_foreign_key: called CC=3 SCAR=11.0
Pod documented
81 125 my $self = shift;
82 125 my ($fk) = pos_validated_list( \@_, { isa => FK } );
83  
84 125 my $fk_id = $fk->id();
85  
86 125 my $source_table_name = $fk->source_table()->name();
87  
88 125 for my $col_name ( map { $_->name() } @{ $fk->source_columns() } ) {
89 125 $self->_fks()->{$source_table_name}{$col_name}{$fk_id} = $fk;
90 }
91  
92 125 my $target_table_name = $fk->target_table()->name();
93  
94 125 for my $col_name ( map { $_->name() } @{ $fk->target_columns() } ) {
95 125 $self->_fks()->{$target_table_name}{$col_name}{$fk_id} = $fk;
96 }
97  
98 125 return $self;
99 }
100  
101 3 ▶sub remove_foreign_key {
Subroutine remove_foreign_key: called CC=3 SCAR=11.0
Pod documented
102 3 my $self = shift;
103 3 my ($fk) = pos_validated_list( \@_, { isa => FK } );
104  
105 3 my $fk_id = $fk->id();
106  
107 3 my $source_table_name = $fk->source_table()->name();
108 3 for my $col_name ( map { $_->name() } @{ $fk->source_columns() } ) {
109 3 delete $self->_fks()->{$source_table_name}{$col_name}{$fk_id};
110 }
111  
112 3 my $target_table_name = $fk->target_table()->name();
113 3 for my $col_name ( map { $_->name() } @{ $fk->target_columns() } ) {
114 3 delete $self->_fks()->{$target_table_name}{$col_name}{$fk_id};
115 }
116  
117 3 return $self;
118 }
119  
120 12 ▶sub foreign_keys_for_table {
Subroutine foreign_keys_for_table: called CC=3 SCAR=11.0
Pod documented
121 12 my $self = shift;
122 12 my ($table)
123 = pos_validated_list( \@_, { isa => TableOrName } );
124  
125 12 ▶ my $name = blessed $table ? $table->name() : $table;
Branch: 1 branch
BranchTrueFalseTotal
builtin::blessed($table) ? : 5 7 12
126  
127 my %fks = (
128 8 map { $_->id() => $_ }
129 11 map { values %{ $self->_fks()->{$name}{$_} } }
130 12 ▶ keys %{ $self->_fks()->{$name} || {} }
Condition100%2/2
$self->_fks->{$name} || {}
l!l
102
MC/DC100%1/1
$self->_fks->{$name} || {}
$self->_fks->{$name}
Truth table100%2/2
A
A= $self->_fks->{$name}
Aresult
11
00
131 );
132  
133 12 return values %fks;
134 }
135  
136 10 ▶sub foreign_keys_between_tables {
Subroutine foreign_keys_between_tables: called CC=12 SCAR=25.4
Pod documented
137 10 my $self = shift;
138 10 my ( $table1, $table2 ) = pos_validated_list(
139 \@_,
140 { isa => TableLikeOrName },
141 { isa => TableLikeOrName }
142 );
143  
144 10 ▶ my $name1
Branch: 2 branches
BranchTrueFalseTotal
!builtin::blessed($table1) ? : 4 6 10
$table1->isa('Fey::Table') ? : 5 1 6
145 = !blessed $table1 ? $table1
146 : $table1->isa('Fey::Table') ? $table1->name()
147 : $table1->table()->name();
148  
149 10 ▶ my $name2
Branch: 2 branches
BranchTrueFalseTotal
!builtin::blessed($table2) ? : 5 5 10
$table2->isa('Fey::Table') ? : 5 0 5
150 = !blessed $table2 ? $table2
151 : $table2->isa('Fey::Table') ? $table2->name()
152 : $table2->table()->name();
153  
154 my %fks = (
155 7 map { $_->id() => $_ }
156 9 grep { $_->has_tables( $name1, $name2 ) }
157 10 map { values %{ $self->_fks()->{$name1}{$_} } }
158 10 ▶ keys %{ $self->_fks()->{$name1} || {} }
Condition100%2/2
$self->_fks->{$name1} || {}
l!l
91
MC/DC100%1/1
$self->_fks->{$name1} || {}
$self->_fks->{$name1}
Truth table100%2/2
A
A= $self->_fks->{$name1}
Aresult
11
00
159 );
160  
161 return values %fks
162 10 ▶ unless grep { blessed $_ && $_->is_alias() } $table1, $table2;
Branch: 1 branch
BranchTrueFalseTotal
unless grep {builtin::blessed($_) and $_->is_alias;} $table1, $table2 9 1 10
Condition100%3/3
builtin::blessed($_) && $_->is_alias
!ll&&!rl&&r
9101
MC/DC100%2/2
builtin::blessed($_) && $_->is_alias
builtin::blessed($_) $_->is_alias
Truth table100%3/3
A && B
A= builtin::blessed($_)B= $_->is_alias
ABresult
0X0
100
111
163  
164 1 ▶ $table1 = $self->table($name1)
Branch: 1 branch
BranchTrueFalseTotal
unless builtin::blessed($table1) 0 1 1
165 unless blessed $table1;
166  
167 1 ▶ $table2 = $self->table($name2)
Branch: 1 branch
BranchTrueFalseTotal
unless builtin::blessed($table2) 1 0 1
168 unless blessed $table2;
169  
170 1 my @fks;
171  
172 1 for my $fk ( values %fks ) {
173 my %p = $table1->name() eq $fk->source_table()->name()
174 ? (
175 source_columns => [
176 $table1->columns(
177 1 map { $_->name() } @{ $fk->source_columns() }
178 )
179 ],
180 target_columns => [
181 $table2->columns(
182 1 map { $_->name() } @{ $fk->target_columns() }
183 )
184 ],
185 )
186 : (
187 source_columns => [
188 $table2->columns(
189 0 map { $_->name() } @{ $fk->source_columns() }
190 )
191 ],
192 target_columns => [
193 $table1->columns(
194 0 ▶ map { $_->name() } @{ $fk->target_columns() }
Branch: 1 branch
BranchTrueFalseTotal
$table1->name eq $fk->source_table->name ? : 1 0 1
195 )
196 ],
197 );
198  
199 1 push @fks, Fey::FK->new(%p);
200 }
201  
202 1 return @fks;
203 }
204  
205 26 __PACKAGE__->meta()->make_immutable();
206  
207 26 1;
208  
209 # ABSTRACT: Represents a schema and contains tables and foreign keys
210  
211 __END__
« Fey/Types/Internal.pm ↑ Summary Fey/SQL/Update.pm »