| 1 | package DBIO::Generate::Style::Cake; | ||||||||||||||||||||||||||||
| 2 | # ABSTRACT: DBIO::Cake 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::Cake (); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 11 | 7 | ▶ | use DBIO::Generate::Util (); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 12 | 7 | ▶ | use namespace::clean; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||
| 15 | 45 | ▶ | sub emit { | ||||||||||||||||||||||||||
|
Subroutine
emit: called CC=25 SCAR=32.5
Pod
documented
| |||||||||||||||||||||||||||||
| 16 | 45 | my ($class, $spec) = @_; | |||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||
| 18 | 45 | my @lines; | |||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||
| 20 | 45 | push @lines, "package " . DBIO::Generate::Util::assert_pkg($spec->{class}) . ";"; | |||||||||||||||||||||||||||
| 21 | 44 | push @lines, "# ABSTRACT: " . DBIO::Generate::Util::abstract_comment($spec->{moniker}); | |||||||||||||||||||||||||||
| 22 | 44 | push @lines, ""; | |||||||||||||||||||||||||||
| 23 | 44 | push @lines, "use DBIO::Cake;"; | |||||||||||||||||||||||||||
| 24 | 44 | push @lines, ""; | |||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||
| 26 | 44 | ▶ | 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"}
| |||||||||||||||||||||||||||||
| 27 | 1 | push @lines, "__PACKAGE__->load_components(" . join(', ', map { DBIO::Generate::Util::pl_str($_) } @c) . ");"; | |||||||||||||||||||||||||||
| 28 | 1 | push @lines, ""; | |||||||||||||||||||||||||||
| 29 | } | ||||||||||||||||||||||||||||
| 30 | |||||||||||||||||||||||||||||
| 31 | 44 | ▶ | if ($spec->{is_view}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 32 | 1 | push @lines, "table_class 'DBIO::ResultSource::View';"; | |||||||||||||||||||||||||||
| 33 | 1 | push @lines, "table " . DBIO::Generate::Util::pl_str($spec->{table}) . ";"; | |||||||||||||||||||||||||||
| 34 | 1 | ▶ | if (defined $spec->{view_definition}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 35 | 1 | push @lines, "view_definition " . DBIO::Generate::Util::pl_str($spec->{view_definition}) . ";"; | |||||||||||||||||||||||||||
| 36 | } | ||||||||||||||||||||||||||||
| 37 | } | ||||||||||||||||||||||||||||
| 38 | else { | ||||||||||||||||||||||||||||
| 39 | 43 | push @lines, "table " . DBIO::Generate::Util::pl_str($spec->{table}) . ";"; | |||||||||||||||||||||||||||
| 40 | } | ||||||||||||||||||||||||||||
| 41 | 44 | push @lines, ""; | |||||||||||||||||||||||||||
| 42 | |||||||||||||||||||||||||||||
| 43 | 44 | ▶ | 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 | 68 | ▶ | 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 | 68 | my $type_str = _cake_type($info); | |||||||||||||||||||||||||||
| 46 | 68 | my $col_str = DBIO::Generate::Util::pl_str($col); | |||||||||||||||||||||||||||
| 47 | # Cake's DDL form: `col NAME => <type>[, <modifier>...];`. Modifiers follow | ||||||||||||||||||||||||||||
| 48 | # the type behind a comma (as the shipped example Results do), so the type | ||||||||||||||||||||||||||||
| 49 | # function's explicit arg list is closed before the modifier bareword. | ||||||||||||||||||||||||||||
| 50 | 68 | my @mods; | |||||||||||||||||||||||||||
| 51 | 68 | ▶ | push @mods, 'auto_inc' if $info->{is_auto_increment}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 52 | 68 | ▶ | push @mods, 'null' if $info->{is_nullable}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 53 | 68 | my $line = "col $col_str => $type_str"; | |||||||||||||||||||||||||||
| 54 | 68 | ▶ | $line .= ', ' . join(', ', @mods) if @mods; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 55 | 68 | push @lines, "$line;"; | |||||||||||||||||||||||||||
| 56 | } | ||||||||||||||||||||||||||||
| 57 | 44 | push @lines, ""; | |||||||||||||||||||||||||||
| 58 | |||||||||||||||||||||||||||||
| 59 | 44 | ▶ | 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"}
| |||||||||||||||||||||||||||||
| 60 | 42 | push @lines, "primary_key " . join(', ', map { DBIO::Generate::Util::pl_str($_) } @pk) . ";"; | |||||||||||||||||||||||||||
| 61 | 42 | push @lines, ""; | |||||||||||||||||||||||||||
| 62 | } | ||||||||||||||||||||||||||||
| 63 | |||||||||||||||||||||||||||||
| 64 | 44 | ▶ | 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"}
| |||||||||||||||||||||||||||||
| 65 | 2 | my ($name, $cols) = @$uq; | |||||||||||||||||||||||||||
| 66 | 2 | push @lines, "unique " . DBIO::Generate::Util::pl_str($name) . " => [" . join(', ', map { DBIO::Generate::Util::pl_str($_) } @$cols) . "];"; | |||||||||||||||||||||||||||
| 67 | } | ||||||||||||||||||||||||||||
| 68 | 44 | ▶ | 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"}
| |||||||||||||||||||||||||||||
| 69 | |||||||||||||||||||||||||||||
| 70 | 44 | ▶ | 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"}
| |||||||||||||||||||||||||||||
| 71 | 10 | my ($method, @args) = @$stmt; | |||||||||||||||||||||||||||
| 72 | 10 | DBIO::Generate::Util::assert_method_name($method, 'extra_statements method'); | |||||||||||||||||||||||||||
| 73 | 2 | push @lines, "$method " . join(', ', map { _dump_val($_) } @args) . ";"; | |||||||||||||||||||||||||||
| 74 | } | ||||||||||||||||||||||||||||
| 75 | 36 | ▶ | 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"}
| |||||||||||||||||||||||||||||
| 76 | |||||||||||||||||||||||||||||
| 77 | 36 | ▶ | 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"}
| |||||||||||||||||||||||||||||
| 78 | 16 | DBIO::Generate::Util::assert_relationship_method($rel->{method}); | |||||||||||||||||||||||||||
| 79 | 7 | my ($rel_name, $remote_class, $cond, @rest) = @{ $rel->{args} }; | |||||||||||||||||||||||||||
| 80 | 7 | my $cond_str = _dump_inline($cond); | |||||||||||||||||||||||||||
| 81 | 7 | push @lines, "$rel->{method} " . DBIO::Generate::Util::pl_str($rel_name) . ", " . DBIO::Generate::Util::pl_str($remote_class) . ", $cond_str;"; | |||||||||||||||||||||||||||
| 82 | } | ||||||||||||||||||||||||||||
| 83 | 27 | ▶ | 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"}
| |||||||||||||||||||||||||||||
| 84 | |||||||||||||||||||||||||||||
| 85 | 27 | push @lines, "1;"; | |||||||||||||||||||||||||||
| 86 | |||||||||||||||||||||||||||||
| 87 | 27 | return join("\n", @lines); | |||||||||||||||||||||||||||
| 88 | } | ||||||||||||||||||||||||||||
| 89 | |||||||||||||||||||||||||||||
| 90 | 7 | my %TYPE_FUNCTION = map { $_ => 1 } DBIO::Cake->type_function_names; | |||||||||||||||||||||||||||
| 91 | |||||||||||||||||||||||||||||
| 92 | 68 | ▶ | sub _cake_type { | ||||||||||||||||||||||||||
|
Subroutine
_cake_type: called CC=7 SCAR=19.5
| |||||||||||||||||||||||||||||
| 93 | 68 | my ($info) = @_; | |||||||||||||||||||||||||||
| 94 | 68 | ▶ | 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"}
| |||||||||||||||||||||||||||||
| 95 | 68 | my $size = $info->{size}; | |||||||||||||||||||||||||||
| 96 | |||||||||||||||||||||||||||||
| 97 | # A type is data. The bareword DSL form (e.g. `varchar(100)`) is written for | ||||||||||||||||||||||||||||
| 98 | # the type functions DBIO::Cake exports and an integer size; every other | ||||||||||||||||||||||||||||
| 99 | # type is a string literal in `data_type => '...'` option pairs, which | ||||||||||||||||||||||||||||
| 100 | # Cake's column() accepts identically. | ||||||||||||||||||||||||||||
| 101 | 68 | ▶ | my $safe_size = !defined $size || $size =~ /\A-?[0-9]+\z/; | ||||||||||||||||||||||||||
|
Condition100%3/3
!defined($size) || $size =~ /\A-?[0-9]+\z/
MC/DC100%2/2
!defined($size) || $size =~ /\A-?[0-9]+\z/
!defined($size)
$size =~ /\A-?[0-9]+\z/
Truth table100%3/3
not(A) || B
A= !defined($size)B= $size =~ /\A-?[0-9]+\z/
| |||||||||||||||||||||||||||||
| 102 | 68 | ▶ | if ($safe_size && $TYPE_FUNCTION{$dt}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$safe_size and $TYPE_FUNCTION{$dt}
MC/DC100%2/2
$safe_size and $TYPE_FUNCTION{$dt}
$safe_size
$TYPE_FUNCTION{$dt}
Truth table100%3/3
A and B
A= $safe_sizeB= $TYPE_FUNCTION{$dt}
| |||||||||||||||||||||||||||||
| 103 | 52 | ▶ | return defined $size ? "$dt($size)" : $dt; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 104 | } | ||||||||||||||||||||||||||||
| 105 | |||||||||||||||||||||||||||||
| 106 | 16 | my $type_str = "data_type => " . DBIO::Generate::Util::pl_str($dt); | |||||||||||||||||||||||||||
| 107 | 16 | ▶ | if (defined $size) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 108 | 4 | $type_str .= ", size => " | |||||||||||||||||||||||||||
| 109 | . DBIO::Generate::Util::pl_val($size); | ||||||||||||||||||||||||||||
| 110 | } | ||||||||||||||||||||||||||||
| 111 | 16 | return $type_str; | |||||||||||||||||||||||||||
| 112 | } | ||||||||||||||||||||||||||||
| 113 | |||||||||||||||||||||||||||||
| 114 | 11 | ▶ | sub _dump_inline { | ||||||||||||||||||||||||||
|
Subroutine
_dump_inline: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 115 | 11 | my ($val) = @_; | |||||||||||||||||||||||||||
| 116 | 11 | local $Data::Dumper::Indent = 0; | |||||||||||||||||||||||||||
| 117 | 11 | local $Data::Dumper::Terse = 1; | |||||||||||||||||||||||||||
| 118 | 11 | local $Data::Dumper::Sortkeys = 1; | |||||||||||||||||||||||||||
| 119 | 11 | return Data::Dumper::Dumper($val); | |||||||||||||||||||||||||||
| 120 | } | ||||||||||||||||||||||||||||
| 121 | |||||||||||||||||||||||||||||
| 122 | 4 | ▶ | sub _dump_val { _dump_inline($_[0]) } | ||||||||||||||||||||||||||
|
Subroutine
_dump_val: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 123 | |||||||||||||||||||||||||||||
| 124 | 7 | 1; | |||||||||||||||||||||||||||
| 125 | |||||||||||||||||||||||||||||
| 126 | __END__ | ||||||||||||||||||||||||||||