| 1 | package DBIO::Generate::Style::Candy; | ||||||||||||||||||||
| 2 | # ABSTRACT: DBIO::Candy DSL emitter for DBIO::Generate | ||||||||||||||||||||
| 3 | |||||||||||||||||||||
| 4 | 7 | ▶ | use strict; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 5 | 7 | ▶ | use warnings; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 6 | |||||||||||||||||||||
| 7 | 7 | our $VERSION = '0.900007'; | |||||||||||||||||||
| 8 | |||||||||||||||||||||
| 9 | 7 | ▶ | use Data::Dumper (); | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 10 | 7 | ▶ | use DBIO::Generate::Util (); | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 11 | 7 | ▶ | use namespace::clean; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 12 | |||||||||||||||||||||
| 13 | |||||||||||||||||||||
| 14 | 43 | ▶ | sub emit { | ||||||||||||||||||
|
Subroutine
emit: called CC=27 SCAR=33.5
Pod
documented
| |||||||||||||||||||||
| 15 | 43 | my ($class, $spec) = @_; | |||||||||||||||||||
| 16 | |||||||||||||||||||||
| 17 | 43 | my @lines; | |||||||||||||||||||
| 18 | |||||||||||||||||||||
| 19 | 43 | push @lines, "package " . DBIO::Generate::Util::assert_pkg($spec->{class}) . ";"; | |||||||||||||||||||
| 20 | 42 | push @lines, "# ABSTRACT: " . DBIO::Generate::Util::abstract_comment($spec->{moniker}); | |||||||||||||||||||
| 21 | 42 | push @lines, ""; | |||||||||||||||||||
| 22 | # DBIO::Candy loads components through its import, not a bareword | ||||||||||||||||||||
| 23 | # load_components() call (which it does not export). | ||||||||||||||||||||
| 24 | 42 | ▶ | if (my @c = @{ $spec->{components} // [] }) { | ||||||||||||||||||
|
Branch: 1 branch
Condition50%1/2
$spec->{"components"} // []
MC/DC0%0/1
$spec->{"components"} // []
$spec->{"components"}
Truth table50%1/2
A
A= $spec->{"components"}
| |||||||||||||||||||||
| 25 | 1 | push @lines, "use DBIO::Candy -components => [" . join(', ', map { DBIO::Generate::Util::pl_str($_) } @c) . "];"; | |||||||||||||||||||
| 26 | } | ||||||||||||||||||||
| 27 | else { | ||||||||||||||||||||
| 28 | 41 | push @lines, "use DBIO::Candy;"; | |||||||||||||||||||
| 29 | } | ||||||||||||||||||||
| 30 | 42 | push @lines, ""; | |||||||||||||||||||
| 31 | |||||||||||||||||||||
| 32 | 42 | ▶ | if ($spec->{is_view}) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 33 | 1 | push @lines, "table_class 'DBIO::ResultSource::View';"; | |||||||||||||||||||
| 34 | 1 | push @lines, "table " . DBIO::Generate::Util::pl_str($spec->{table}) . ";"; | |||||||||||||||||||
| 35 | push @lines, "view_definition " . DBIO::Generate::Util::pl_str($spec->{view_definition}) . ";" | ||||||||||||||||||||
| 36 | 1 | ▶ | if defined $spec->{view_definition}; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 37 | } | ||||||||||||||||||||
| 38 | else { | ||||||||||||||||||||
| 39 | 41 | push @lines, "table " . DBIO::Generate::Util::pl_str($spec->{table}) . ";"; | |||||||||||||||||||
| 40 | } | ||||||||||||||||||||
| 41 | 42 | push @lines, ""; | |||||||||||||||||||
| 42 | |||||||||||||||||||||
| 43 | 42 | ▶ | for my $col (@{ $spec->{column_order} // [] }) { | ||||||||||||||||||
|
Condition50%1/2
$spec->{"column_order"} // []
MC/DC0%0/1
$spec->{"column_order"} // []
$spec->{"column_order"}
Truth table50%1/2
A
A= $spec->{"column_order"}
| |||||||||||||||||||||
| 44 | 65 | ▶ | my $info = $spec->{columns}{$col} // {}; | ||||||||||||||||||
|
Condition50%1/2
$spec->{"columns"}{$col} // {}
MC/DC0%0/1
$spec->{"columns"}{$col} // {}
$spec->{"columns"}{$col}
Truth table50%1/2
A
A= $spec->{"columns"}{$col}
| |||||||||||||||||||||
| 45 | 65 | ▶ | my $dt = $info->{data_type} // 'varchar'; | ||||||||||||||||||
|
Condition50%1/2
$info->{"data_type"} // "varchar"
MC/DC0%0/1
$info->{"data_type"} // "varchar"
$info->{"data_type"}
Truth table50%1/2
A
A= $info->{"data_type"}
| |||||||||||||||||||||
| 46 | 65 | ▶ | my $size = defined $info->{size} ? $info->{size} : undef; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 47 | # has_column wraps its trailing args into the column-info hashref | ||||||||||||||||||||
| 48 | # (add_columns($col => { @_ })), so the type and every attribute must be | ||||||||||||||||||||
| 49 | # emitted as a FLAT key/value list -- `data_type => '...', size => N, ...`. | ||||||||||||||||||||
| 50 | # A bare `'type', { ... }` would store { 'type' => {...} } with no data_type. | ||||||||||||||||||||
| 51 | 65 | my @info_pairs = ("data_type => " . DBIO::Generate::Util::pl_str($dt)); | |||||||||||||||||||
| 52 | 65 | ▶ | if (defined $size) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 53 | 7 | push @info_pairs, "size => " . DBIO::Generate::Util::pl_val($size); | |||||||||||||||||||
| 54 | } | ||||||||||||||||||||
| 55 | 65 | ▶ | push @info_pairs, "is_auto_increment => 1" if $info->{is_auto_increment}; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 56 | 65 | ▶ | push @info_pairs, "is_nullable => 1" if $info->{is_nullable}; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 57 | 65 | push @lines, "has_column " . DBIO::Generate::Util::pl_str($col) . " => " . join(', ', @info_pairs) . ";"; | |||||||||||||||||||
| 58 | } | ||||||||||||||||||||
| 59 | 42 | push @lines, ""; | |||||||||||||||||||
| 60 | |||||||||||||||||||||
| 61 | 42 | ▶ | if (my @pk = @{ $spec->{pk} // [] }) { | ||||||||||||||||||
|
Branch: 1 branch
Condition50%1/2
$spec->{"pk"} // []
MC/DC0%0/1
$spec->{"pk"} // []
$spec->{"pk"}
Truth table50%1/2
A
A= $spec->{"pk"}
| |||||||||||||||||||||
| 62 | 42 | push @lines, "primary_key " . join(', ', map { DBIO::Generate::Util::pl_str($_) } @pk) . ";"; | |||||||||||||||||||
| 63 | 42 | push @lines, ""; | |||||||||||||||||||
| 64 | } | ||||||||||||||||||||
| 65 | |||||||||||||||||||||
| 66 | 42 | ▶ | for my $uq (@{ $spec->{uniq} // [] }) { | ||||||||||||||||||
|
Condition50%1/2
$spec->{"uniq"} // []
MC/DC0%0/1
$spec->{"uniq"} // []
$spec->{"uniq"}
Truth table50%1/2
A
A= $spec->{"uniq"}
| |||||||||||||||||||||
| 67 | 1 | my ($name, $cols) = @$uq; | |||||||||||||||||||
| 68 | 1 | push @lines, "unique_constraint " . DBIO::Generate::Util::pl_str($name) . " => [" . join(', ', map { DBIO::Generate::Util::pl_str($_) } @$cols) . "];"; | |||||||||||||||||||
| 69 | } | ||||||||||||||||||||
| 70 | 42 | ▶ | push @lines, "" if @{ $spec->{uniq} // [] }; | ||||||||||||||||||
|
Branch: 1 branch
Condition50%1/2
$spec->{"uniq"} // []
MC/DC0%0/1
$spec->{"uniq"} // []
$spec->{"uniq"}
Truth table50%1/2
A
A= $spec->{"uniq"}
| |||||||||||||||||||||
| 71 | |||||||||||||||||||||
| 72 | 42 | ▶ | for my $stmt (@{ $spec->{extra_statements} // [] }) { | ||||||||||||||||||
|
Condition50%1/2
$spec->{"extra_statements"} // []
MC/DC0%0/1
$spec->{"extra_statements"} // []
$spec->{"extra_statements"}
Truth table50%1/2
A
A= $spec->{"extra_statements"}
| |||||||||||||||||||||
| 73 | 10 | my ($method, @args) = @$stmt; | |||||||||||||||||||
| 74 | 10 | DBIO::Generate::Util::assert_method_name($method, 'extra_statements method'); | |||||||||||||||||||
| 75 | 2 | push @lines, "$method(" . join(', ', map { _dump_val($_) } @args) . ");"; | |||||||||||||||||||
| 76 | } | ||||||||||||||||||||
| 77 | 34 | ▶ | push @lines, "" if @{ $spec->{extra_statements} // [] }; | ||||||||||||||||||
|
Branch: 1 branch
Condition50%1/2
$spec->{"extra_statements"} // []
MC/DC0%0/1
$spec->{"extra_statements"} // []
$spec->{"extra_statements"}
Truth table50%1/2
A
A= $spec->{"extra_statements"}
| |||||||||||||||||||||
| 78 | |||||||||||||||||||||
| 79 | 34 | ▶ | for my $rel (@{ $spec->{relationships} // [] }) { | ||||||||||||||||||
|
Condition50%1/2
$spec->{"relationships"} // []
MC/DC0%0/1
$spec->{"relationships"} // []
$spec->{"relationships"}
Truth table50%1/2
A
A= $spec->{"relationships"}
| |||||||||||||||||||||
| 80 | 15 | DBIO::Generate::Util::assert_relationship_method($rel->{method}); | |||||||||||||||||||
| 81 | 6 | my ($rel_name, $remote_class, $cond, @rest) = @{ $rel->{args} }; | |||||||||||||||||||
| 82 | 6 | push @lines, "$rel->{method} " . DBIO::Generate::Util::pl_str($rel_name) . ", " . DBIO::Generate::Util::pl_str($remote_class) . ", " . _dump_inline($cond) . ";"; | |||||||||||||||||||
| 83 | } | ||||||||||||||||||||
| 84 | 25 | ▶ | push @lines, "" if @{ $spec->{relationships} // [] }; | ||||||||||||||||||
|
Branch: 1 branch
Condition50%1/2
$spec->{"relationships"} // []
MC/DC0%0/1
$spec->{"relationships"} // []
$spec->{"relationships"}
Truth table50%1/2
A
A= $spec->{"relationships"}
| |||||||||||||||||||||
| 85 | |||||||||||||||||||||
| 86 | 25 | push @lines, "1;"; | |||||||||||||||||||
| 87 | |||||||||||||||||||||
| 88 | 25 | return join("\n", @lines); | |||||||||||||||||||
| 89 | } | ||||||||||||||||||||
| 90 | |||||||||||||||||||||
| 91 | 10 | ▶ | sub _dump_inline { | ||||||||||||||||||
|
Subroutine
_dump_inline: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 92 | 10 | my ($val) = @_; | |||||||||||||||||||
| 93 | 10 | local $Data::Dumper::Indent = 0; | |||||||||||||||||||
| 94 | 10 | local $Data::Dumper::Terse = 1; | |||||||||||||||||||
| 95 | 10 | local $Data::Dumper::Sortkeys = 1; | |||||||||||||||||||
| 96 | 10 | return Data::Dumper::Dumper($val); | |||||||||||||||||||
| 97 | } | ||||||||||||||||||||
| 98 | |||||||||||||||||||||
| 99 | 4 | ▶ | sub _dump_val { _dump_inline($_[0]) } | ||||||||||||||||||
|
Subroutine
_dump_val: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 100 | |||||||||||||||||||||
| 101 | 7 | 1; | |||||||||||||||||||
| 102 | |||||||||||||||||||||
| 103 | __END__ | ||||||||||||||||||||