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 4 ▶use 5.20.0;
Subroutine BEGIN: called CC=1 SCAR=0.0
2 4 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 4 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 package DBIx::Class::Smooth::Fields;
6  
7 # ABSTRACT: Specify columns
8 4 our $AUTHORITY = 'cpan:CSSON'; # AUTHORITY
9 4 our $VERSION = '0.0104';
10  
11 4 ▶use Carp qw/croak/;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 4 ▶use List::Util qw/uniq/;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 4 ▶use List::SomeUtils qw/any/;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 4 ▶use boolean;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 4 use Sub::Exporter::Progressive -setup => {
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 4 ▶};
Subroutine BEGIN: called CC=1 SCAR=0.0
89  
90 4 ▶use experimental qw/postderef signatures/;
Subroutine BEGIN: called CC=1 SCAR=0.0
91  
92 ▶sub merge($first, $second) {
Subroutine merge: called CC=18 SCAR=34.2
Pod undocumented
93 70 my $merged = do_merge($first, $second);
94  
95 70 ▶ if(!exists $merged->{'extra'}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $merged->{"extra"}) 67 3 70
96 67 $merged->{'extra'} = {};
97 }
98 70 $merged->{'_smooth'} = {};
99  
100 70 for my $key (keys $merged->%*) {
101 339 ▶ if($key =~ m{^-(.*)}) {
Branch: 5 branches
BranchTrueFalseTotal
if ($key =~ /^-(.*)/u) { } 12 327 339
elsif ($key eq 'many') { } 0 327 327
elsif ($key eq 'might') { } 0 327 327
elsif ($key eq 'one') { } 0 327 327
elsif ($key eq 'across') { } 0 327 327
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 70 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 70 for my $alias (keys %alias) {
132 280 ▶ if(exists $merged->{ $alias }) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $merged->{$alias}) 13 267 280
133 13 my $actual = $alias{ $alias };
134 13 $merged->{ $actual } = delete $merged->{ $alias };
135 }
136 }
137  
138 70 ▶ if(exists $merged->{'default_sql'}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $merged->{"default_sql"}) 4 66 70
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 70 ▶ if(!scalar keys $merged->{'_smooth'}->%*) {
Branch: 1 branch
BranchTrueFalseTotal
unless (scalar keys %{$$merged{"_smooth"}}) 70 0 70
150 70 delete $merged->{'_smooth'};
151 }
152 70 ▶ if(!scalar keys $merged->{'extra'}->%*) {
Branch: 1 branch
BranchTrueFalseTotal
unless (scalar keys %{$$merged{"extra"}}) 60 10 70
153 60 delete $merged->{'extra'};
154 }
155 70 return $merged;
156 }
157  
158 ▶sub do_merge($first, $second) {
Subroutine do_merge: called CC=8 SCAR=26.6
Pod undocumented
159 70 my $merged = {};
160 70 for my $key (uniq (keys %{ $first }, keys %{ $second })) {
161 202 ▶ if(exists $first->{ $key } && !exists $second->{ $key }) {
Branch: 2 branches
BranchTrueFalseTotal
if (exists $first->{$key} and not exists $second->{$key}) { } 73 129 202
elsif (not exists $first->{$key} and exists $second->{$key}) { } 129 0 129
Condition66%2/3
exists $first->{$key} and not exists $second->{$key}
!ll&&!rl&&r
129073
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 73 $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
00129
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 129 $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 70 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 ▶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 2 my @approved_keys = qw/nullable indexed sql related_name related_sql/;
187 2 my @keys_in_settings = keys %settings;
188  
189 KEY:
190 2 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 2 return merge { _smooth_foreign_key => 1 }, \%settings;
196 }
197  
198 # base fields
199 ▶sub NumericField(%settings) {
Subroutine NumericField: called CC=1 SCAR=0.0
Pod undocumented
200 29 return merge { is_numeric => 1 }, \%settings;
201 }
202 ▶sub NonNumericField(%settings) {
Subroutine NonNumericField: called CC=1 SCAR=0.0
Pod undocumented
203 36 return merge { is_numeric => 0 }, \%settings;
204 }
205  
206 # data types - integers
207 ▶sub BitField(%settings) {
Subroutine BitField: called CC=1 SCAR=0.0
Pod undocumented
208 1 return NumericField(data_type => 'bit', %settings);
209 }
210 ▶sub TinyIntField(%settings) {
Subroutine TinyIntField: called CC=1 SCAR=0.0
Pod undocumented
211 1 return NumericField(data_type => 'tinyint', %settings);
212 }
213 ▶sub SmallIntField(%settings) {
Subroutine SmallIntField: called CC=1 SCAR=0.0
Pod undocumented
214 1 return NumericField(data_type => 'smallint', %settings);
215 }
216 ▶sub MediumIntField(%settings) {
Subroutine MediumIntField: called CC=1 SCAR=0.0
Pod undocumented
217 1 return NumericField(data_type => 'mediumint', %settings);
218 }
219 ▶sub IntegerField(%settings) {
Subroutine IntegerField: called CC=1 SCAR=0.0
Pod undocumented
220 11 return NumericField(data_type => 'integer', %settings);
221 }
222 ▶sub BigIntField(%settings) {
Subroutine BigIntField: called CC=1 SCAR=0.0
Pod undocumented
223 1 return NumericField(data_type => 'bigint', %settings);
224 }
225 ▶sub SerialField(%settings) {
Subroutine SerialField: called CC=1 SCAR=0.0
Pod undocumented
226 2 return NumericField(data_type => 'serial', %settings);
227 }
228 ▶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 ▶sub DecimalField(%settings) {
Subroutine DecimalField: called CC=1 SCAR=0.0
Pod undocumented
234 6 return NumericField(data_type => 'decimal', %settings);
235 }
236 ▶sub FloatField(%settings) {
Subroutine FloatField: called CC=1 SCAR=0.0
Pod undocumented
237 2 return NumericField(data_type => 'float', %settings);
238 }
239 ▶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 ▶sub VarcharField(%settings) {
Subroutine VarcharField: called CC=1 SCAR=0.0
Pod undocumented
245 11 return NonNumericField(data_type => 'varchar', %settings);
246 }
247 ▶sub CharField(%settings) {
Subroutine CharField: called CC=1 SCAR=0.0
Pod undocumented
248 1 return NonNumericField(data_type => 'char', %settings);
249 }
250 ▶sub VarbinaryField(%settings) {
Subroutine VarbinaryField: called CC=1 SCAR=0.0
Pod undocumented
251 1 return NonNumericField(data_type => 'varbinary', %settings);
252 }
253 ▶sub BinaryField(%settings) {
Subroutine BinaryField: called CC=1 SCAR=0.0
Pod undocumented
254 1 return NonNumericField(data_type => 'binary', %settings);
255 }
256  
257 ▶sub TinyTextField(%settings) {
Subroutine TinyTextField: called CC=1 SCAR=0.0
Pod undocumented
258 1 return NonNumericField(data_type => 'tinytext', %settings);
259 }
260 ▶sub TextField(%settings) {
Subroutine TextField: called CC=1 SCAR=0.0
Pod undocumented
261 1 return NonNumericField(data_type => 'text', %settings);
262 }
263 ▶sub MediumTextField(%settings) {
Subroutine MediumTextField: called CC=1 SCAR=0.0
Pod undocumented
264 1 return NonNumericField(data_type => 'mediumtext', %settings);
265 }
266 ▶sub LongTextField(%settings) {
Subroutine LongTextField: called CC=1 SCAR=0.0
Pod undocumented
267 1 return NonNumericField(data_type => 'longtext', %settings);
268 }
269 ▶sub TinyBlobField(%settings) {
Subroutine TinyBlobField: called CC=1 SCAR=0.0
Pod undocumented
270 1 return NonNumericField(data_type => 'tinyblob', %settings);
271 }
272 ▶sub BlobField(%settings) {
Subroutine BlobField: called CC=1 SCAR=0.0
Pod undocumented
273 1 return NonNumericField(data_type => 'blob', %settings);
274 }
275 ▶sub MediumBlobField(%settings) {
Subroutine MediumBlobField: called CC=1 SCAR=0.0
Pod undocumented
276 1 return NonNumericField(data_type => 'mediumblob', %settings);
277 }
278 ▶sub LongBlobField(%settings) {
Subroutine LongBlobField: called CC=1 SCAR=0.0
Pod undocumented
279 1 return NonNumericField(data_type => 'longblob', %settings);
280 }
281  
282 ▶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 ▶sub DateField(%settings) {
Subroutine DateField: called CC=1 SCAR=0.0
Pod undocumented
297 3 return NonNumericField(data_type => 'date', %settings);
298 }
299 ▶sub DateTimeField(%settings) {
Subroutine DateTimeField: called CC=1 SCAR=0.0
Pod undocumented
300 3 return NonNumericField(data_type => 'datetime', %settings);
301 }
302 ▶sub TimestampField(%settings) {
Subroutine TimestampField: called CC=1 SCAR=0.0
Pod undocumented
303 1 return NonNumericField(data_type => 'timestamp', %settings);
304 }
305 ▶sub TimeField(%settings) {
Subroutine TimeField: called CC=1 SCAR=0.0
Pod undocumented
306 1 return NonNumericField(data_type => 'time', %settings);
307 }
308 ▶sub YearField(%settings) {
Subroutine YearField: called CC=1 SCAR=0.0
Pod undocumented
309 3 return NonNumericField(data_type => 'year', %settings);
310 }
311  
312 4 1;
313  
314 __END__
315  
316 =pod
317  
318 =encoding UTF-8
319  
320 =head1 NAME
321  
322 DBIx::Class::Smooth::Fields - Specify columns
323  
324 =head1 VERSION
325  
326 Version 0.0104, released 2020-08-30.
327  
328 =head1 SYNOPSIS
329  
330 package Your::Schema::Result::Book;
331  
332 use Your::Schema::Result -components => [qw//];
333 use DBIx::Class::Smooth::Fields -all;
334  
335 primary id => IntegerField(auto_increment => true);
336 belongs Publisher => ForeignKey();
337 col isbn => VarcharField(size => 13);
338 col title => VarcharField(size => 150);
339 col published_date => DateField();
340 col language => EnumField(indexed => 1, -list => [qw/english french german spanish/]);
341  
342 =head1 DESCRIPTION
343  
344 DBIx::Class::Smooth::Fields defines an alternative way to specify columns in DBIx::Class result sources. They make most sense when used together with the functions exported by L<Smooth::Helper::Row::Creation>.
345  
346 These are just functions that return the hashes that you normally use to configure DBIx::Class columns. With a couple of exceptions, they only set C<data_type> and C<is_numeric>.
347  
348 Any key-value pairs passed will be included in the returned hash. If you need to use other data types, you can use C<NumericalField> or C<NonNumericalField> which only sets C<is_numeric> to the expected value.
349  
350 =head2 Relational fields
351  
352 =head3 ForeignKey()
353  
354 belongs Publisher => ForeignKey();
355  
356 This is not a field type at all, but helps define the relationship with another result source. The heavy lifting is done by C<belongs>, but in short there will be a field named C<publisher_id> with the C<size> and C<data_type> of the C<id> field in C<::Publisher>.
357  
358 =head2 Numerical fields
359  
360 These will all have C<is_numeric> set to C<1>, in addition to their respective C<data_type>:
361  
362 BitField bit
363 TinyIntField tinyint
364 SmallIntField smallint
365 MediumIntField mediumint
366 IntegerField integer
367 BigIntField bigint
368 SerialField serial
369 BooleanField boolean
370 DecimalField decimal
371 FloatField float
372 DoubleField double
373  
374 =head2 Non-numerical fields
375  
376 These will all have C<is_numeric> set to C<0>, in addition to their respective C<data_type>:
377  
378 VarcharField varchar
379 CharField char
380 VarbinaryField varbinary
381 BinaryField binary
382 TinyTextField tinytext
383 TextField text
384 MediumTextField mediumtext
385 LongTextField longtext
386 TinyBlobField tinyblob
387 BlobField blob
388 MediumBlobField mediumblob
389 LongBlobField longblod
390 EnumField enum
391 DateField date
392 DateTimeField datetime
393 TimestampField timestamp
394 TimeField time
395 YearField year
396  
397 For C<EnumField>, you can do C<EnumField(-list => [qw/one to three/])> instead of C<EnumField(extra => { list => [qw/one two three/] })>.
398  
399 =head1 SEE ALSO
400  
401 =head1 SOURCE
402  
403 L<https://github.com/Csson/p5-DBIx-Class-Smooth>
404  
405 =head1 HOMEPAGE
406  
407 L<https://metacpan.org/release/DBIx-Class-Smooth>
408  
409 =head1 AUTHOR
410  
411 Erik Carlsson <info@code301.com>
412  
413 =head1 COPYRIGHT AND LICENSE
414  
415 This software is copyright (c) 2018 by Erik Carlsson.
416  
417 This is free software; you can redistribute it and/or modify it under
418 the same terms as the Perl 5 programming language system itself.
419  
420 =cut
« Smooth/Schema.pm ↑ Summary Smooth/Lookup/substring.pm »