Smooth/Fields.pm

filter: Statementstmt 86.7% 98 / 113 Branchbran 61.1% 22 / 36 Conditioncond 27.7% 5 / 18 MC/DCmcdc 9.0% 1 / 11 Subroutinesub 97.7% 43 / 44 Podpod 2.9% 1 / 34 totaltotal 66.4% 170 / 256 CC 30 SCAR 23.1
CC
30
Cov
75%
merge
30.6
do_merge
14.3
EnumField
4.8
CRAP 10.1
« Smooth/Schema.pm ↑ Summary Smooth/Lookup/substring.pm »
1 3 ▶use 5.20.0;
Subroutine BEGIN: called CC=1 SCAR=0.0
2 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 package DBIx::Class::Smooth::Fields;
6  
7 # ABSTRACT: Specify columns
8 3 our $AUTHORITY = 'cpan:CSSON'; # AUTHORITY
9 3 our $VERSION = '0.0108';
10  
11 3 ▶use Carp qw/croak/;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 3 ▶use List::Util qw/uniq/;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 3 ▶use List::SomeUtils qw/any/;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 3 ▶use boolean;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 3 ▶use Sub::Exporter::Progressive -setup => {
Subroutine BEGIN: called CC=1 SCAR=0.0
16 exports => [qw/
17 true
18 false
19 ForeignKey
20 BitField
21 TinyIntField
22 SmallIntField
23 MediumIntField
24 IntegerField
25 BigIntField
26 SerialField
27 BooleanField
28 NumericField
29 NonNumericField
30 DecimalField
31 FloatField
32 DoubleField
33 VarcharField
34 CharField
35 VarbinaryField
36 BinaryField
37 TinyTextField
38 TextField
39 MediumTextField
40 LongTextField
41 TinyBlobField
42 BlobField
43 MediumBlobField
44 LongBlobField
45 EnumField
46 DateField
47 DateTimeField
48 TimestampField
49 TimeField
50 YearField
51 /],
52 groups => {
53 fields => [qw/
54 ForeignKey
55 BitField
56 TinyIntField
57 SmallIntField
58 MediumIntField
59 IntegerField
60 BigIntField
61 SerialField
62 BooleanField
63 NumericField
64 NonNumericField
65 DecimalField
66 FloatField
67 DoubleField
68 VarcharField
69 CharField
70 VarbinaryField
71 BinaryField
72 TinyTextField
73 TextField
74 MediumTextField
75 LongTextField
76 TinyBlobField
77 BlobField
78 MediumBlobField
79 LongBlobField
80 EnumField
81 DateField
82 DateTimeField
83 TimestampField
84 TimeField
85 YearField
86 /],
87 },
88 3 };
89  
90 3 ▶use experimental qw/postderef signatures/;
Subroutine BEGIN: called CC=1 SCAR=0.0
91  
92 67 ▶sub merge($first, $second) {
Subroutine merge: called CC=18 SCAR=34.2
Pod undocumented
93 67 my $merged = do_merge($first, $second);
94  
95 67 ▶ if(!exists $merged->{'extra'}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $merged->{"extra"}) 64 3 67
96 64 $merged->{'extra'} = {};
97 }
98 67 $merged->{'_smooth'} = {};
99  
100 67 for my $key (keys $merged->%*) {
101 320 ▶ if($key =~ m{^-(.*)}) {
Branch: 5 branches
BranchTrueFalseTotal
if ($key =~ /^-(.*)/u) { } 12 308 320
elsif ($key eq 'many') { } 0 308 308
elsif ($key eq 'might') { } 0 308 308
elsif ($key eq 'one') { } 0 308 308
elsif ($key eq 'across') { } 0 308 308
102 12 my $clean_key = $1;
103 12 $merged->{'extra'}{ $clean_key } = delete $merged->{ $key };
104 }
105 elsif($key eq 'many') {
106 0 ▶ $merged->{'_smooth'}{'has_many'} = delete $merged->{'many'} || [];
Condition0%0/2
delete $merged->{'many'} || []
l!l
00
MC/DC0%0/1
delete $merged->{'many'} || []
delete $merged->{'many'}
Truth table0%0/2
A
A= delete $merged->{'many'}
Aresult
11
00
107 }
108 elsif($key eq 'might') {
109 0 ▶ $merged->{'_smooth'}{'might_have'} = delete $merged->{'might'} || [];
Condition0%0/2
delete $merged->{'might'} || []
l!l
00
MC/DC0%0/1
delete $merged->{'might'} || []
delete $merged->{'might'}
Truth table0%0/2
A
A= delete $merged->{'might'}
Aresult
11
00
110 }
111 elsif($key eq 'one') {
112 0 ▶ $merged->{'_smooth'}{'has_one'} = delete $merged->{'one'} || [];
Condition0%0/2
delete $merged->{'one'} || []
l!l
00
MC/DC0%0/1
delete $merged->{'one'} || []
delete $merged->{'one'}
Truth table0%0/2
A
A= delete $merged->{'one'}
Aresult
11
00
113 }
114 elsif($key eq 'across') {
115 0 my $acrosses = delete $merged->{'across'};
116 0 ▶ for (my $i = 0; $i < scalar $acrosses->@*; ++$i) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < scalar @$acrosses) 0 0 0
117 0 my $from = $acrosses->[$i];
118 0 my $to = $acrosses->[$i + 1];
119 0 $merged->{'_smooth'}{'across'}{ $from }{ $to } = 1;
120 }
121 }
122 }
123  
124 67 my %alias = (
125 nullable => 'is_nullable',
126 auto_increment => 'is_auto_increment',
127 foreign_key => 'is_foreign_key',
128 default => 'default_value',
129 );
130  
131 67 for my $alias (keys %alias) {
132 268 ▶ if(exists $merged->{ $alias }) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $merged->{$alias}) 13 255 268
133 13 my $actual = $alias{ $alias };
134 13 $merged->{ $actual } = delete $merged->{ $alias };
135 }
136 }
137  
138 67 ▶ if(exists $merged->{'default_sql'}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $merged->{"default_sql"}) 4 63 67
139 4 ▶ if(!defined $merged->{'default_sql'}) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $merged->{'default_sql'}) { } 1 3 4
140 1 delete $merged->{'default_sql'};
141 1 $merged->{'default_value'} = \'NULL';
142 }
143 else {
144 3 my $default_sql = delete $merged->{'default_sql'};
145 3 $merged->{'default_value'} = \$default_sql;
146 }
147 }
148  
149 67 ▶ if(!scalar keys $merged->{'_smooth'}->%*) {
Branch: 1 branch
BranchTrueFalseTotal
unless (scalar keys %{$$merged{"_smooth"}}) 67 0 67
150 67 delete $merged->{'_smooth'};
151 }
152 67 ▶ if(!scalar keys $merged->{'extra'}->%*) {
Branch: 1 branch
BranchTrueFalseTotal
unless (scalar keys %{$$merged{"extra"}}) 57 10 67
153 57 delete $merged->{'extra'};
154 }
155 67 return $merged;
156 }
157  
158 67 ▶sub do_merge($first, $second) {
Subroutine do_merge: called CC=8 SCAR=26.6
Pod undocumented
159 67 my $merged = {};
160 67 for my $key (uniq (keys %{ $first }, keys %{ $second })) {
161 189 ▶ if(exists $first->{ $key } && !exists $second->{ $key }) {
Branch: 2 branches
BranchTrueFalseTotal
if (exists $first->{$key} and not exists $second->{$key}) { } 70 119 189
elsif (not exists $first->{$key} and exists $second->{$key}) { } 119 0 119
Condition66%2/3
exists $first->{$key} and not exists $second->{$key}
!ll&&!rl&&r
119070
MC/DC50%1/2
exists $first->{$key} and not exists $second->{$key}
exists $first->{$key} not exists $second->{$key}
Truth table66%2/3
A and not(B)
A= exists $first->{$key}B= not exists $second->{$key}
ABresult
0X0
100
111
162 70 $merged->{ $key } = $first->{ $key };
163 }
164 ▶ elsif(!exists $first->{ $key } && exists $second->{ $key }) {
Condition33%1/3
not exists $first->{$key} and exists $second->{$key}
!ll&&!rl&&r
00119
MC/DC0%0/2
not exists $first->{$key} and exists $second->{$key}
not exists $first->{$key} exists $second->{$key}
Truth table33%1/3
not(A) and B
A= not exists $first->{$key}B= exists $second->{$key}
ABresult
0X0
100
111
165 119 $merged->{ $key } = $second->{ $key };
166 }
167 else {
168 0 ▶ if(ref $first->{ $key } ne 'HASH' && $second->{ $key } ne 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $first->{$key} ne 'HASH' and $second->{$key} ne 'HASH') { } 0 0 0
Condition0%0/3
ref $first->{$key} ne "HASH" and $second->{$key} ne "HASH"
!ll&&!rl&&r
000
MC/DC0%0/2
ref $first->{$key} ne "HASH" and $second->{$key} ne "HASH"
ref $first->{$key} ne "HASH" $second->{$key} ne "HASH"
Truth table0%0/3
A and B
A= ref $first->{$key} ne "HASH"B= $second->{$key} ne "HASH"
ABresult
0X0
100
111
169 0 $merged->{ $key } = $first->{ $key };
170 }
171 else {
172 0 $merged->{ $key } = do_merge($first->{ $key }, $second->{ $key });
173 }
174 }
175 }
176  
177 67 return $merged;
178 }
179  
180 # this can only be used in the best case, where we can lift the definition from the primary key it points to
181 # and also does belongs_to<->has_many relationships
182 3 ▶sub ForeignKey(%settings) {
Subroutine ForeignKey: called CC=3 SCAR=15.1
Pod documented
183 # 'sql' is the attr to the relationship
184 # 'related_name' is the name of the inverse relationship, set to undef to skip creation
185 # 'related_sql' is the attr to the inverse relationship
186 3 my @approved_keys = qw/nullable indexed sql related_name related_sql/;
187 3 my @keys_in_settings = keys %settings;
188  
189 KEY:
190 3 for my $key (@keys_in_settings) {
191 0 ▶ next KEY if any { $key eq $_ } @approved_keys;
Subroutine __ANON__: not called CC=1 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
if &any(sub { $key eq $_; } , @approved_keys) 0 0 0
192 0 delete $settings{ $key };
193 }
194  
195 3 return merge { _smooth_foreign_key => 1 }, \%settings;
196 }
197  
198 # base fields
199 28 ▶sub NumericField(%settings) {
Subroutine NumericField: called CC=1 SCAR=0.0
Pod undocumented
200 28 return merge { is_numeric => 1 }, \%settings;
201 }
202 33 ▶sub NonNumericField(%settings) {
Subroutine NonNumericField: called CC=1 SCAR=0.0
Pod undocumented
203 33 return merge { is_numeric => 0 }, \%settings;
204 }
205  
206 # data types - integers
207 1 ▶sub BitField(%settings) {
Subroutine BitField: called CC=1 SCAR=0.0
Pod undocumented
208 1 return NumericField(data_type => 'bit', %settings);
209 }
210 1 ▶sub TinyIntField(%settings) {
Subroutine TinyIntField: called CC=1 SCAR=0.0
Pod undocumented
211 1 return NumericField(data_type => 'tinyint', %settings);
212 }
213 1 ▶sub SmallIntField(%settings) {
Subroutine SmallIntField: called CC=1 SCAR=0.0
Pod undocumented
214 1 return NumericField(data_type => 'smallint', %settings);
215 }
216 1 ▶sub MediumIntField(%settings) {
Subroutine MediumIntField: called CC=1 SCAR=0.0
Pod undocumented
217 1 return NumericField(data_type => 'mediumint', %settings);
218 }
219 10 ▶sub IntegerField(%settings) {
Subroutine IntegerField: called CC=1 SCAR=0.0
Pod undocumented
220 10 return NumericField(data_type => 'integer', %settings);
221 }
222 1 ▶sub BigIntField(%settings) {
Subroutine BigIntField: called CC=1 SCAR=0.0
Pod undocumented
223 1 return NumericField(data_type => 'bigint', %settings);
224 }
225 2 ▶sub SerialField(%settings) {
Subroutine SerialField: called CC=1 SCAR=0.0
Pod undocumented
226 2 return NumericField(data_type => 'serial', %settings);
227 }
228 1 ▶sub BooleanField(%settings) {
Subroutine BooleanField: called CC=1 SCAR=0.0
Pod undocumented
229 1 return NumericField(data_type => 'boolean', %settings);
230 }
231  
232 # data types - other numericals
233 6 ▶sub DecimalField(%settings) {
Subroutine DecimalField: called CC=1 SCAR=0.0
Pod undocumented
234 6 return NumericField(data_type => 'decimal', %settings);
235 }
236 2 ▶sub FloatField(%settings) {
Subroutine FloatField: called CC=1 SCAR=0.0
Pod undocumented
237 2 return NumericField(data_type => 'float', %settings);
238 }
239 1 ▶sub DoubleField(%settings) {
Subroutine DoubleField: called CC=1 SCAR=0.0
Pod undocumented
240 1 return NumericField(data_type => 'double', %settings);
241 }
242  
243 # data types - strings
244 8 ▶sub VarcharField(%settings) {
Subroutine VarcharField: called CC=1 SCAR=0.0
Pod undocumented
245 8 return NonNumericField(data_type => 'varchar', %settings);
246 }
247 1 ▶sub CharField(%settings) {
Subroutine CharField: called CC=1 SCAR=0.0
Pod undocumented
248 1 return NonNumericField(data_type => 'char', %settings);
249 }
250 1 ▶sub VarbinaryField(%settings) {
Subroutine VarbinaryField: called CC=1 SCAR=0.0
Pod undocumented
251 1 return NonNumericField(data_type => 'varbinary', %settings);
252 }
253 1 ▶sub BinaryField(%settings) {
Subroutine BinaryField: called CC=1 SCAR=0.0
Pod undocumented
254 1 return NonNumericField(data_type => 'binary', %settings);
255 }
256  
257 1 ▶sub TinyTextField(%settings) {
Subroutine TinyTextField: called CC=1 SCAR=0.0
Pod undocumented
258 1 return NonNumericField(data_type => 'tinytext', %settings);
259 }
260 1 ▶sub TextField(%settings) {
Subroutine TextField: called CC=1 SCAR=0.0
Pod undocumented
261 1 return NonNumericField(data_type => 'text', %settings);
262 }
263 1 ▶sub MediumTextField(%settings) {
Subroutine MediumTextField: called CC=1 SCAR=0.0
Pod undocumented
264 1 return NonNumericField(data_type => 'mediumtext', %settings);
265 }
266 1 ▶sub LongTextField(%settings) {
Subroutine LongTextField: called CC=1 SCAR=0.0
Pod undocumented
267 1 return NonNumericField(data_type => 'longtext', %settings);
268 }
269 1 ▶sub TinyBlobField(%settings) {
Subroutine TinyBlobField: called CC=1 SCAR=0.0
Pod undocumented
270 1 return NonNumericField(data_type => 'tinyblob', %settings);
271 }
272 1 ▶sub BlobField(%settings) {
Subroutine BlobField: called CC=1 SCAR=0.0
Pod undocumented
273 1 return NonNumericField(data_type => 'blob', %settings);
274 }
275 1 ▶sub MediumBlobField(%settings) {
Subroutine MediumBlobField: called CC=1 SCAR=0.0
Pod undocumented
276 1 return NonNumericField(data_type => 'mediumblob', %settings);
277 }
278 1 ▶sub LongBlobField(%settings) {
Subroutine LongBlobField: called CC=1 SCAR=0.0
Pod undocumented
279 1 return NonNumericField(data_type => 'longblob', %settings);
280 }
281  
282 3 ▶sub EnumField(%settings) {
Subroutine EnumField: called CC=4 SCAR=15.6
Pod undocumented
283 3 ▶ if(exists $settings{'extra'} && exists $settings{'extra'}{'list'}) {
Branch: 2 branches
BranchTrueFalseTotal
if (exists $settings{'extra'} and exists $settings{'extra'}{'list'}) { } 0 3 3
elsif (exists $settings{'-list'}) { } 3 0 3
Condition66%2/3
exists $settings{"extra"} and exists $settings{"extra"}{"list"}
!ll&&!rl&&r
210
MC/DC0%0/2
exists $settings{"extra"} and exists $settings{"extra"}{"list"}
exists $settings{"extra"} exists $settings{"extra"}{"list"}
Truth table66%2/3
A and B
A= exists $settings{"extra"}B= exists $settings{"extra"}{"list"}
ABresult
0X0
100
111
284 # all good
285 }
286 elsif(exists $settings{'-list'}) {
287 3 $settings{'extra'}{'list'} = delete $settings{'list'};
288 }
289 else {
290 0 croak qq{'enum' expects '-list => [qw/the possible values/]' or 'extra => { list => [qw/the possible values/] }'};
291 }
292 3 return merge { data_type => 'enum', is_numeric => 0 }, \%settings;
293 }
294  
295 # data types - dates and times
296 2 ▶sub DateField(%settings) {
Subroutine DateField: called CC=1 SCAR=0.0
Pod undocumented
297 2 return NonNumericField(data_type => 'date', %settings);
298 }
299 4 ▶sub DateTimeField(%settings) {
Subroutine DateTimeField: called CC=1 SCAR=0.0
Pod undocumented
300 4 return NonNumericField(data_type => 'datetime', %settings);
301 }
302 1 ▶sub TimestampField(%settings) {
Subroutine TimestampField: called CC=1 SCAR=0.0
Pod undocumented
303 1 return NonNumericField(data_type => 'timestamp', %settings);
304 }
305 1 ▶sub TimeField(%settings) {
Subroutine TimeField: called CC=1 SCAR=0.0
Pod undocumented
306 1 return NonNumericField(data_type => 'time', %settings);
307 }
308 2 ▶sub YearField(%settings) {
Subroutine YearField: called CC=1 SCAR=0.0
Pod undocumented
309 2 return NonNumericField(data_type => 'year', %settings);
310 }
311  
312 3 1;
313  
314 __END__
« Smooth/Schema.pm ↑ Summary Smooth/Lookup/substring.pm »