| 1 | package DBIO::Generate::Util; | ||||||||||||||||||||||||||||||||||||||||||
| 2 | # ABSTRACT: Safe Perl-literal emission helpers for DBIO::Generate styles | ||||||||||||||||||||||||||||||||||||||||||
| 3 | |||||||||||||||||||||||||||||||||||||||||||
| 4 | 14 | ▶ | use strict; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 5 | 14 | ▶ | use warnings; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||||||||||||||||
| 7 | 14 | our $VERSION = '0.900007'; | |||||||||||||||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||||||||||||||||
| 9 | 14 | ▶ | use B (); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 10 | 14 | ▶ | use Carp::Clan qw/^DBIO/; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 11 | 14 | ▶ | use Data::Dumper (); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 12 | 14 | ▶ | use DBIO::Exception (); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 13 | 14 | ▶ | use namespace::clean; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||||||||||||||||
| 15 | # SECURITY: every DB-derived identifier (column / table / constraint / | ||||||||||||||||||||||||||||||||||||||||||
| 16 | # relationship / moniker name, default values, view definitions, ...) reaches | ||||||||||||||||||||||||||||||||||||||||||
| 17 | # generated Perl source through this module. The escaping logic lives here, in | ||||||||||||||||||||||||||||||||||||||||||
| 18 | # ONE place, on purpose: a hostile DB identifier must never become executable | ||||||||||||||||||||||||||||||||||||||||||
| 19 | # Perl in a generated Result class. Do not inline / duplicate this — see | ||||||||||||||||||||||||||||||||||||||||||
| 20 | # t/generate/08-codegen-injection.t. | ||||||||||||||||||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||||||||||||||||
| 23 | # Render an arbitrary string as a safe, single-token Perl string literal. | ||||||||||||||||||||||||||||||||||||||||||
| 24 | # B::perlstring escapes quotes, backslashes, newlines and all other special | ||||||||||||||||||||||||||||||||||||||||||
| 25 | # characters, so the result is a complete literal that can be spliced into | ||||||||||||||||||||||||||||||||||||||||||
| 26 | # emitted source wherever a string is wanted (hash key, call argument, ...). | ||||||||||||||||||||||||||||||||||||||||||
| 27 | # undef-safe: undef becomes the literal undef. | ||||||||||||||||||||||||||||||||||||||||||
| 28 | 1143 | ▶ | sub pl_str { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
pl_str: called CC=2 SCAR=7.1
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 29 | 1143 | my ($s) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 30 | 1143 | ▶ | return 'undef' unless defined $s; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 31 | 1143 | return B::perlstring($s); | |||||||||||||||||||||||||||||||||||||||||
| 32 | } | ||||||||||||||||||||||||||||||||||||||||||
| 33 | |||||||||||||||||||||||||||||||||||||||||||
| 34 | |||||||||||||||||||||||||||||||||||||||||||
| 35 | 25 | ▶ | sub pl_val { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
pl_val: called CC=4 SCAR=13.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 36 | 25 | my ($v) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 37 | 25 | ▶ | return pl_str($v) unless defined $v; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 38 | 25 | ▶ | if (ref $v) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 39 | 5 | local $Data::Dumper::Indent = 0; | |||||||||||||||||||||||||||||||||||||||||
| 40 | 5 | local $Data::Dumper::Terse = 1; | |||||||||||||||||||||||||||||||||||||||||
| 41 | 5 | local $Data::Dumper::Sortkeys = 1; | |||||||||||||||||||||||||||||||||||||||||
| 42 | 5 | return Data::Dumper::Dumper($v); | |||||||||||||||||||||||||||||||||||||||||
| 43 | } | ||||||||||||||||||||||||||||||||||||||||||
| 44 | 20 | ▶ | return $v =~ /\A-?[0-9]+\z/ ? $v : pl_str($v); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 45 | } | ||||||||||||||||||||||||||||||||||||||||||
| 46 | |||||||||||||||||||||||||||||||||||||||||||
| 47 | |||||||||||||||||||||||||||||||||||||||||||
| 48 | # Render a value safe for emission on a `# ABSTRACT: ...` comment line. | ||||||||||||||||||||||||||||||||||||||||||
| 49 | # A newline in the source string would otherwise break out of the comment and | ||||||||||||||||||||||||||||||||||||||||||
| 50 | # inject a fresh line of (executable) code, so all vertical whitespace is | ||||||||||||||||||||||||||||||||||||||||||
| 51 | # collapsed to single spaces and the result trimmed. undef -> empty string. | ||||||||||||||||||||||||||||||||||||||||||
| 52 | 164 | ▶ | sub abstract_comment { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
abstract_comment: called CC=2 SCAR=7.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 53 | 164 | my ($s) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 54 | 164 | ▶ | return '' unless defined $s; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 55 | 164 | $s =~ s/\s+/ /g; | |||||||||||||||||||||||||||||||||||||||||
| 56 | 164 | $s =~ s/\A\s+//; | |||||||||||||||||||||||||||||||||||||||||
| 57 | 164 | $s =~ s/\s+\z//; | |||||||||||||||||||||||||||||||||||||||||
| 58 | 164 | return $s; | |||||||||||||||||||||||||||||||||||||||||
| 59 | } | ||||||||||||||||||||||||||||||||||||||||||
| 60 | |||||||||||||||||||||||||||||||||||||||||||
| 61 | |||||||||||||||||||||||||||||||||||||||||||
| 62 | # Validate a string that is about to be emitted as a BAREWORD package / | ||||||||||||||||||||||||||||||||||||||||||
| 63 | # class name (e.g. `package $name;`), where a string literal is not an | ||||||||||||||||||||||||||||||||||||||||||
| 64 | # option. A bareword can carry arbitrary code, so anything that is not a | ||||||||||||||||||||||||||||||||||||||||||
| 65 | # strict, well-formed Perl package name is rejected loudly. | ||||||||||||||||||||||||||||||||||||||||||
| 66 | 219 | ▶ | sub assert_pkg { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
assert_pkg: called CC=4 SCAR=14.2
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 67 | 219 | my ($name) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 68 | 219 | ▶ | croak "refusing to emit invalid/unsafe package name: " . (defined $name ? "'$name'" : 'undef') | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
Condition66%2/3
defined $name and $name =~ /\A[A-Za-z_][A-Za-z0-9_]*(?:::[A-Za-z_][A-Za-z0-9_]*)*\z/
MC/DC50%1/2
defined $name and $name =~ /\A[A-Za-z_][A-Za-z0-9_]*(?:::[A-Za-z_][A-Za-z0-9_]*)*\z/
defined $name
$name =~ /\A[A-Za-z_][A-Za-z0-9_]*(?:::[A-Za-z_][A-Za-z0-9_]*)*\z/
Truth table66%2/3
A and B
A= defined $nameB= $name =~ /\A[A-Za-z_][A-Za-z0-9_]*(?:::[A-Za-z_][A-Za-z0-9_]*)*\z/
| |||||||||||||||||||||||||||||||||||||||||||
| 69 | unless defined $name | ||||||||||||||||||||||||||||||||||||||||||
| 70 | && $name =~ /\A[A-Za-z_][A-Za-z0-9_]*(?:::[A-Za-z_][A-Za-z0-9_]*)*\z/; | ||||||||||||||||||||||||||||||||||||||||||
| 71 | 213 | return $name; | |||||||||||||||||||||||||||||||||||||||||
| 72 | } | ||||||||||||||||||||||||||||||||||||||||||
| 73 | |||||||||||||||||||||||||||||||||||||||||||
| 74 | |||||||||||||||||||||||||||||||||||||||||||
| 75 | 129 | ▶ | sub assert_method_name { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
assert_method_name: called CC=4 SCAR=14.6
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 76 | 129 | my ($method, $what) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 77 | 129 | ▶ | DBIO::Exception->throw("$what is not a Perl identifier") | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition50%3/6
defined $method and not ref $method and $method =~ /\A[A-Za-z_]\w*\z/
defined $method and not ref $method
MC/DC33%1/3
defined $method and not ref $method and $method =~ /\A[A-Za-z_]\w*\z/
defined $method
not ref $method
$method =~ /\A[A-Za-z_]\w*\z/
Truth table50%2/4
A and not(B) and C
A= defined $methodB= not ref $methodC= $method =~ /\A[A-Za-z_]\w*\z/
| |||||||||||||||||||||||||||||||||||||||||||
| 78 | unless defined $method && !ref $method && $method =~ /\A[A-Za-z_]\w*\z/; | ||||||||||||||||||||||||||||||||||||||||||
| 79 | 49 | return $method; | |||||||||||||||||||||||||||||||||||||||||
| 80 | } | ||||||||||||||||||||||||||||||||||||||||||
| 81 | |||||||||||||||||||||||||||||||||||||||||||
| 82 | |||||||||||||||||||||||||||||||||||||||||||
| 83 | 14 | my %RELATIONSHIP_METHOD = map { $_ => 1 } | |||||||||||||||||||||||||||||||||||||||||
| 84 | qw(belongs_to has_one has_many might_have many_to_many); | ||||||||||||||||||||||||||||||||||||||||||
| 85 | |||||||||||||||||||||||||||||||||||||||||||
| 86 | 79 | ▶ | sub assert_relationship_method { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
assert_relationship_method: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 87 | 79 | my ($method) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 88 | 79 | assert_method_name($method, 'relationship method'); | |||||||||||||||||||||||||||||||||||||||||
| 89 | DBIO::Exception->throw('relationship method is not a known relationship method') | ||||||||||||||||||||||||||||||||||||||||||
| 90 | 39 | ▶ | unless $RELATIONSHIP_METHOD{$method}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 91 | 34 | return $method; | |||||||||||||||||||||||||||||||||||||||||
| 92 | } | ||||||||||||||||||||||||||||||||||||||||||
| 93 | |||||||||||||||||||||||||||||||||||||||||||
| 94 | 14 | 1; | |||||||||||||||||||||||||||||||||||||||||
| 95 | |||||||||||||||||||||||||||||||||||||||||||
| 96 | __END__ | ||||||||||||||||||||||||||||||||||||||||||