blib/lib/DBIO/Generate.pm

filter: Statementstmt 92.7% 128 / 138 Branchbran 58.7% 27 / 46 Conditioncond 53.2% 33 / 62 MC/DCmcdc 26.6% 8 / 30 Subroutinesub 100% 18 / 18 Podpod 100% 2 / 2 totaltotal 72.9% 216 / 296 CC 57 SCAR 28.6
CC
57
Cov
76%
dump
33.7
_moniker_for
17.6
_write_class
16.0
CRAP 17.5
« blib/lib/DBIO/Test.pm ↑ Summary blib/lib/DBIO/Generate/Style/Cake.pm »
1 package DBIO::Generate;
2 # ABSTRACT: Driver-agnostic Result class generator for DBIO
3  
4 13 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 13 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 13 our $VERSION = '0.900007';
8  
9 13 ▶use base qw/Class::Accessor::Grouped/;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 13 ▶use mro 'c3';
Subroutine BEGIN: called CC=1 SCAR=0.0
11 13 ▶use Carp::Clan qw/^DBIO/;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 13 ▶use DBIO::Util qw(dir_path parent_dir mkpath write_file);
Subroutine BEGIN: called CC=1 SCAR=0.0
13 13 ▶use Lingua::EN::Inflect::Phrase ();
Subroutine BEGIN: called CC=1 SCAR=0.0
14 13 ▶use DBIO::Generate::Relationships ();
Subroutine BEGIN: called CC=1 SCAR=0.0
15 13 ▶use namespace::clean;
Subroutine BEGIN: called CC=1 SCAR=0.0
16  
17 13 __PACKAGE__->mk_group_accessors(simple => qw/
18 dump_directory
19 dump_overwrite
20 really_erase_my_files
21 schema_class
22 result_base_class
23 additional_classes
24 left_base_classes
25 components
26 result_roles
27 generate_pod
28 use_namespaces
29 preserve_case
30 constraint
31 exclude
32 db_schema
33 moniker_map
34 moniker_parts
35 moniker_part_separator
36 col_accessor_map
37 rel_name_map
38 col_collision_map
39 rel_collision_map
40 inflect_plural
41 inflect_singular
42 custom_column_info
43 datetime_timezone
44 datetime_locale
45 datetime_undef_if_invalid
46 uniq_to_primary
47 allow_extra_m2m_cols
48 filter_generated_code
49 style
50 quiet
51 debug
52 /);
53  
54 13 my %STYLE_CLASS = (
55 vanilla => 'DBIO::Generate::Style::Vanilla',
56 cake => 'DBIO::Generate::Style::Cake',
57 candy => 'DBIO::Generate::Style::Candy',
58 moose => 'DBIO::Generate::Style::Moose',
59 moo => 'DBIO::Generate::Style::Moo',
60 );
61  
62  
63 12 ▶sub new {
Subroutine new: called CC=12 SCAR=24.9
Pod documented
64 12 my ($class, %args) = @_;
65 12 my $self = bless {}, $class;
66 12 ▶ $self->dump_directory( $args{dump_directory} // './lib' );
Condition50%1/2
$args{"dump_directory"} // "./lib"
l!l
120
MC/DC0%0/1
$args{"dump_directory"} // "./lib"
$args{"dump_directory"}
Truth table50%1/2
A
A= $args{"dump_directory"}
Aresult
11
00
67 12 ▶ $self->result_base_class( $args{result_base_class} // 'DBIO::Core' );
Condition100%2/2
$args{"result_base_class"} // "DBIO::Core"
l!l
111
MC/DC100%1/1
$args{"result_base_class"} // "DBIO::Core"
$args{"result_base_class"}
Truth table100%2/2
A
A= $args{"result_base_class"}
Aresult
11
00
68 12 ▶ $self->use_namespaces( exists $args{use_namespaces} ? $args{use_namespaces} : 1 );
Branch: 1 branch
BranchTrueFalseTotal
exists $args{'use_namespaces'} ? : 6 6 12
69 12 ▶ $self->generate_pod( exists $args{generate_pod} ? $args{generate_pod} : 1 );
Branch: 1 branch
BranchTrueFalseTotal
exists $args{'generate_pod'} ? : 11 1 12
70 12 ▶ $self->style( $args{style} // 'vanilla' );
Condition50%1/2
$args{"style"} // "vanilla"
l!l
120
MC/DC0%0/1
$args{"style"} // "vanilla"
$args{"style"}
Truth table50%1/2
A
A= $args{"style"}
Aresult
11
00
71 12 ▶ $self->moniker_parts( $args{moniker_parts} // ['name'] );
Condition100%2/2
$args{"moniker_parts"} // ["name"]
l!l
111
MC/DC100%1/1
$args{"moniker_parts"} // ["name"]
$args{"moniker_parts"}
Truth table100%2/2
A
A= $args{"moniker_parts"}
Aresult
11
00
72 12 ▶ $self->moniker_part_separator( $args{moniker_part_separator} // '' );
Condition100%2/2
$args{"moniker_part_separator"} // ""
l!l
111
MC/DC100%1/1
$args{"moniker_part_separator"} // ""
$args{"moniker_part_separator"}
Truth table100%2/2
A
A= $args{"moniker_part_separator"}
Aresult
11
00
73 12 ▶ $self->allow_extra_m2m_cols( $args{allow_extra_m2m_cols} // 0 );
Condition100%2/2
$args{"allow_extra_m2m_cols"} // 0
l!l
111
MC/DC100%1/1
$args{"allow_extra_m2m_cols"} // 0
$args{"allow_extra_m2m_cols"}
Truth table100%2/2
A
A= $args{"allow_extra_m2m_cols"}
Aresult
11
00
74 12 ▶ $self->quiet( $args{quiet} // 0 );
Condition100%2/2
$args{"quiet"} // 0
l!l
48
MC/DC100%1/1
$args{"quiet"} // 0
$args{"quiet"}
Truth table100%2/2
A
A= $args{"quiet"}
Aresult
11
00
75 12 for my $k (keys %args) {
76 61 ▶ $self->$k($args{$k}) if $self->can($k);
Branch: 1 branch
BranchTrueFalseTotal
if $self->can($k) 60 1 61
77 }
78 12 return $self;
79 }
80  
81  
82 10 ▶sub dump {
Subroutine dump: called CC=25 SCAR=35.2
Pod documented
83 10 my ($self, $introspect) = @_;
84  
85 10 ▶ croak "dump() requires an introspect object"
Branch: 1 branch
BranchTrueFalseTotal
unless ref $introspect 0 10 10
86 unless ref $introspect;
87  
88 10 ▶ my $style_class = $STYLE_CLASS{ $self->style // 'vanilla' }
Branch: 1 branch
BranchTrueFalseTotal
my $style_class = $STYLE_CLASS{$self->style // "vanilla"} or croak(q[Unknown style '] . ($self->style // "") . q[']) 10 0 10
Condition25%1/4
$self->style // "vanilla"
l!l
100
$self->style // ""
l!l
00
MC/DC0%0/1
$self->style // "vanilla"
$self->style
MC/DC0%0/1
$self->style // ""
$self->style
Truth table50%1/2
A
A= $self->style
Aresult
11
00
Truth table0%0/2
A
A= $self->style
Aresult
11
00
89 or croak "Unknown style '" . ($self->style//'') . "'";
90  
91 10 DBIO::Util::assert_class_name($style_class);
92 10 ▶ eval "require $style_class" or croak "Cannot load $style_class: $@";
Branch: 1 branch
BranchTrueFalseTotal
eval "require $style_class" or croak("Cannot load ${style_class}: $@") 10 0 10
93  
94 10 my @keys = @{ $introspect->table_keys };
95 10 my $introspected = scalar @keys;
96  
97 10 @keys = $self->_filter_keys(@keys);
98  
99 # An empty schema is legitimate, so this is not fatal -- but staying silent
100 # is exactly how a broken introspection run reaches the user as a successful
101 # "Done." with no files written at all (Codeberg #15).
102 10 ▶ if (!@keys && !$self->quiet) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@keys or $self->quiet) 3 7 10
Condition100%3/3
@keys or $self->quiet
l!l&&r!l&&!r
613
MC/DC100%2/2
@keys or $self->quiet
@keys $self->quiet
Truth table100%3/3
A or B
A= @keysB= $self->quiet
ABresult
1X1
011
000
103 3 ▶ warn $introspected
Branch: 1 branch
BranchTrueFalseTotal
$introspected ? : 1 2 3
104 ? "DBIO::Generate: all $introspected introspected tables were filtered out by "
105 . "constraint/exclude -- no Result classes written\n"
106 : "DBIO::Generate: " . ref($introspect) . " reported no tables or views -- "
107 . "no Result classes written. Check the DSN, the connecting user's "
108 . "permissions, and whether the database is actually empty.\n";
109 }
110  
111 10 my %moniker_for = map { $_ => $self->_moniker_for($_) } @keys;
112 10 my %class_for = map { $moniker_for{$_} => $self->_class_for($moniker_for{$_}) } @keys;
113 10 ▶ my %pk_for = map { $moniker_for{$_} => ($introspect->table_pk_info($_) // []) } @keys;
Condition50%1/2
$introspect->table_pk_info($_) // []
l!l
70
MC/DC0%0/1
$introspect->table_pk_info($_) // []
$introspect->table_pk_info($_)
Truth table50%1/2
A
A= $introspect->table_pk_info($_)
Aresult
11
00
114 10 ▶ my %cols_for = map { $moniker_for{$_} => ($introspect->table_columns($_) // []) } @keys;
Condition50%1/2
$introspect->table_columns($_) // []
l!l
70
MC/DC0%0/1
$introspect->table_columns($_) // []
$introspect->table_columns($_)
Truth table50%1/2
A
A= $introspect->table_columns($_)
Aresult
11
00
115  
116 10 my @rel_tables;
117 10 for my $key (@keys) {
118 7 my $moniker = $moniker_for{$key};
119 1 my @fk_info = map { $self->_resolve_fk_remote($_, \%moniker_for) }
120 7 ▶ @{ $introspect->table_fk_info($key) // [] };
Condition50%1/2
$introspect->table_fk_info($key) // []
l!l
70
MC/DC0%0/1
$introspect->table_fk_info($key) // []
$introspect->table_fk_info($key)
Truth table50%1/2
A
A= $introspect->table_fk_info($key)
Aresult
11
00
121 7 ▶ push @rel_tables, [ $moniker, \@fk_info, ($introspect->table_uniq_info($key) // []) ];
Condition50%1/2
$introspect->table_uniq_info($key) // []
l!l
70
MC/DC0%0/1
$introspect->table_uniq_info($key) // []
$introspect->table_uniq_info($key)
Truth table50%1/2
A
A= $introspect->table_uniq_info($key)
Aresult
11
00
122 }
123  
124 10 my $rels = DBIO::Generate::Relationships->new(
125 inflect_plural => $self->inflect_plural,
126 inflect_singular => $self->inflect_singular,
127 rel_name_map => $self->rel_name_map,
128 rel_collision_map => $self->rel_collision_map,
129 allow_extra_m2m_cols => $self->allow_extra_m2m_cols,
130 quiet => $self->quiet,
131 )->generate_code(\@rel_tables, \%class_for, \%pk_for, \%cols_for);
132  
133 10 for my $key (@keys) {
134 7 my $moniker = $moniker_for{$key};
135 7 my $class = $class_for{$moniker};
136  
137 my $spec = {
138 moniker => $moniker,
139 class => $class,
140 table => $self->_table_name($key),
141 column_order => ($introspect->table_columns($key) // []),
142 columns => $self->_normalize_columns_info($introspect->table_columns_info($key) // {}),
143 pk => ($introspect->table_pk_info($key) // []),
144 uniq => ($introspect->table_uniq_info($key) // []),
145 7 ▶ relationships => ($rels->{$moniker} // []),
Condition55%10/18
$introspect->table_columns($key) // []
l!l
70
$introspect->table_columns_info($key) // {}
l!l
70
$introspect->table_pk_info($key) // []
l!l
70
$introspect->table_uniq_info($key) // []
l!l
70
$rels->{$moniker} // []
l!l
25
$introspect->table_is_view($key) // 0
l!l
70
$self->result_base_class // "DBIO::Core"
l!l
70
$self->components // []
l!l
07
$self->additional_classes // []
l!l
07
MC/DC0%0/1
$introspect->table_columns($key) // []
$introspect->table_columns($key)
MC/DC0%0/1
$introspect->table_columns_info($key) // {}
$introspect->table_columns_info($key)
MC/DC0%0/1
$introspect->table_pk_info($key) // []
$introspect->table_pk_info($key)
MC/DC0%0/1
$introspect->table_uniq_info($key) // []
$introspect->table_uniq_info($key)
MC/DC100%1/1
$rels->{$moniker} // []
$rels->{$moniker}
MC/DC0%0/1
$introspect->table_is_view($key) // 0
$introspect->table_is_view($key)
MC/DC0%0/1
$self->result_base_class // "DBIO::Core"
$self->result_base_class
MC/DC0%0/1
$self->components // []
$self->components
MC/DC0%0/1
$self->additional_classes // []
$self->additional_classes
Truth table50%1/2
A
A= $introspect->table_columns($key)
Aresult
11
00
Truth table50%1/2
A
A= $introspect->table_columns_info($key)
Aresult
11
00
Truth table50%1/2
A
A= $introspect->table_pk_info($key)
Aresult
11
00
Truth table50%1/2
A
A= $introspect->table_uniq_info($key)
Aresult
11
00
Truth table100%2/2
A
A= $rels->{$moniker}
Aresult
11
00
Truth table50%1/2
A
A= $introspect->table_is_view($key)
Aresult
11
00
Truth table50%1/2
A
A= $self->result_base_class
Aresult
11
00
Truth table50%1/2
A
A= $self->components
Aresult
11
00
Truth table50%1/2
A
A= $self->additional_classes
Aresult
11
00
146 extra_statements => [ $introspect->result_class_extra_statements($key) ],
147 is_view => ($introspect->table_is_view($key) // 0),
148 view_definition => $introspect->view_definition($key),
149 result_base_class => ($self->result_base_class // 'DBIO::Core'),
150 components => ($self->components // []),
151 additional_classes => ($self->additional_classes // []),
152 };
153  
154 7 my $code = $style_class->emit($spec);
155  
156 7 ▶ $code = $self->filter_generated_code->($code, $spec)
Branch: 1 branch
BranchTrueFalseTotal
if ref $self->filter_generated_code eq "CODE" 0 7 7
157 if ref($self->filter_generated_code) eq 'CODE';
158  
159 7 $self->_write_class($class, $code);
160 }
161 }
162  
163 # The style contract for optional column-info keys is "absent key", not
164 # "undef value" -- introspectors that hand back size => undef must not
165 # leak that into the emitters.
166 7 ▶sub _normalize_columns_info {
Subroutine _normalize_columns_info: called CC=3 SCAR=11.0
167 7 my ($self, $info) = @_;
168 7 my %normalized;
169 7 for my $col (keys %$info) {
170 21 ▶ my $ci = $info->{$col} // {};
Condition50%1/2
$info->{$col} // {}
l!l
210
MC/DC0%0/1
$info->{$col} // {}
$info->{$col}
Truth table50%1/2
A
A= $info->{$col}
Aresult
11
00
171 21 $normalized{$col} = { map { $_ => $ci->{$_} } grep { defined $ci->{$_} } keys %$ci };
172 }
173 7 return \%normalized;
174 }
175  
176 10 ▶sub _filter_keys {
Subroutine _filter_keys: called CC=3 SCAR=11.4
177 10 my ($self, @keys) = @_;
178 10 ▶ if (my $con = $self->constraint) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $con = $self->constraint) 0 10 10
179 0 @keys = grep { $_ =~ $con } @keys;
180 }
181 10 ▶ if (my $ex = $self->exclude) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $ex = $self->exclude) 1 9 10
182 1 @keys = grep { $_ !~ $ex } @keys;
183 }
184 10 return @keys;
185 }
186  
187 16 ▶sub _table_name {
Subroutine _table_name: called CC=1 SCAR=0.0
188 16 my ($self, $key) = @_;
189 16 (my $name = $key) =~ s/^[^.]+\.//;
190 16 return $name;
191 }
192  
193 7 ▶sub _moniker_for {
Subroutine _moniker_for: called CC=8 SCAR=28.7
194 7 my ($self, $key) = @_;
195 7 my $name = $self->_table_name($key);
196  
197 7 ▶ unless ($self->preserve_case) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->preserve_case) 7 0 7
198 7 my @parts = split /[_\W]+/, $name;
199 7 $name = join('', map { ucfirst lc } @parts);
200 7 ▶ if (@parts > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (@parts > 0) 7 0 7
201 7 my $last = pop @parts;
202 7 my $singular = Lingua::EN::Inflect::Phrase::to_S($last);
203 0 $name = join('', map { ucfirst lc } @parts) . ucfirst(lc $singular);
204 }
205 }
206  
207 7 ▶ if (my $map = $self->moniker_map) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $map = $self->moniker_map) 0 7 7
208 0 ▶ if (ref $map eq 'HASH' && exists $map->{$name}) {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $map eq 'HASH' and exists $map->{$name}) { } 0 0 0
elsif (ref $map eq 'CODE') { } 0 0 0
Condition0%0/3
ref $map eq "HASH" and exists $map->{$name}
!ll&&!rl&&r
000
MC/DC0%0/2
ref $map eq "HASH" and exists $map->{$name}
ref $map eq "HASH" exists $map->{$name}
Truth table0%0/3
A and B
A= ref $map eq "HASH"B= exists $map->{$name}
ABresult
0X0
100
111
209 0 return $map->{$name};
210 }
211 elsif (ref $map eq 'CODE') {
212 0 my $mapped = $map->($name);
213 0 ▶ return $mapped if defined $mapped;
Branch: 1 branch
BranchTrueFalseTotal
if defined $mapped 0 0 0
214 }
215 }
216  
217 7 return $name;
218 }
219  
220 7 ▶sub _class_for {
Subroutine _class_for: called CC=3 SCAR=12.0
221 7 my ($self, $moniker) = @_;
222 7 ▶ my $schema = $self->schema_class or croak "schema_class required";
Branch: 1 branch
BranchTrueFalseTotal
my $schema = $self->schema_class or croak("schema_class required") 7 0 7
223 7 ▶ if ($self->use_namespaces) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->use_namespaces) 7 0 7
224 7 return "${schema}::Result::${moniker}";
225 }
226 0 return "${schema}::${moniker}";
227 }
228  
229 1 ▶sub _resolve_fk_remote {
Subroutine _resolve_fk_remote: called CC=2 SCAR=6.9
230 1 my ($self, $fk, $moniker_for) = @_;
231 1 my %by_table;
232 1 ▶ while (my ($k, $m) = each %$moniker_for) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %$moniker_for) 2 1 3
233 2 my $tname = $self->_table_name($k);
234 2 $by_table{$tname} = $m;
235 }
236 1 my %resolved = %$fk;
237 1 $resolved{remote_moniker} = $by_table{ $fk->{remote_table} };
238 1 return \%resolved;
239 }
240  
241 7 ▶sub _write_class {
Subroutine _write_class: called CC=8 SCAR=27.7
242 7 my ($self, $class, $code) = @_;
243  
244 7 (my $rel_path = $class) =~ s{::}{/}g;
245 7 my $dir = dir_path($self->dump_directory, $rel_path);
246 7 my $file = "${dir}.pm";
247  
248 7 ▶ if (-e $file && !($self->dump_overwrite // 0) && !($self->really_erase_my_files // 0)) {
Branch: 1 branch
BranchTrueFalseTotal
if (-e $file and not $self->dump_overwrite // 0 and not $self->really_erase_my_files // 0) 0 7 7
Condition20%2/10
-e $file and not $self->dump_overwrite // 0 and not $self->really_erase_my_files // 0
!ll&&!rl&&r
700
-e $file and not $self->dump_overwrite // 0
!ll&&!rl&&r
700
$self->dump_overwrite // 0
l!l
00
$self->really_erase_my_files // 0
l!l
00
MC/DC0%0/3
-e $file and not $self->dump_overwrite // 0 and not $self->really_erase_my_files // 0
-e $file $self->dump_overwrite $self->really_erase_my_files
Truth table25%1/4
A and not(B) and not(C)
A= -e $fileB= $self->dump_overwriteC= $self->really_erase_my_files
ABCresult
0XX0
11X0
1010
1001
249 0 ▶ warn "Skipping existing $file (set dump_overwrite => 1 to overwrite)\n"
Branch: 1 branch
BranchTrueFalseTotal
unless $self->quiet 0 0 0
250 unless $self->quiet;
251 0 return;
252 }
253  
254 # $dir is the class path without the .pm -- only its parent is a directory;
255 # mkpath($dir) would litter an empty Result/Artist/ next to Result/Artist.pm
256 7 mkpath(parent_dir($file));
257 7 write_file($file, $code);
258 7 ▶ warn "Wrote $file\n" unless $self->quiet;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->quiet 5 2 7
259 }
260  
261 13 1;
262  
263 __END__
« blib/lib/DBIO/Test.pm ↑ Summary blib/lib/DBIO/Generate/Style/Cake.pm »