| 1 | package DBIx::POS::Template; | ||||||||||||||||||||||||||||||
| 2 | 2 | ▶ | use strict; | ||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||
| 3 | 2 | ▶ | use warnings; | ||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||
| 4 | 2 | ▶ | use base qw{Pod::Parser}; | ||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||
| 5 | 2 | ▶ | use Hash::Merge qw(merge); | ||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||
| 6 | 2 | ▶ | use Encode; | ||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||||
| 8 | 2 | our $VERSION = '0.067'; | |||||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||||
| 10 | # Hold data for our pending statement | ||||||||||||||||||||||||||||||
| 11 | 2 | my $info = {}; | |||||||||||||||||||||||||||||
| 12 | # SQL parse statements | ||||||||||||||||||||||||||||||
| 13 | 2 | my %sql = (); | |||||||||||||||||||||||||||||
| 14 | # What command we're looking at | ||||||||||||||||||||||||||||||
| 15 | 2 | my $cmd; | |||||||||||||||||||||||||||||
| 16 | # PODs enc | ||||||||||||||||||||||||||||||
| 17 | my $enc; | ||||||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||||
| 19 | # Text::Template->new(%TT, %tt) | ||||||||||||||||||||||||||||||
| 20 | 2 | our %TT = ( | |||||||||||||||||||||||||||||
| 21 | DELIMITERS => ['{%', '%}'], | ||||||||||||||||||||||||||||||
| 22 | #~ BROKEN => sub { die @_;}, | ||||||||||||||||||||||||||||||
| 23 | ); | ||||||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||||
| 25 | 2 | my %tt = (); # instance scope: Text::Template->new(..., tt=>{%TT, %tt}, ) | |||||||||||||||||||||||||||||
| 26 | 2 | my $tt = {}; # new scope: Text::Template->new(..., tt=>{%TT, %$tt}, ) | |||||||||||||||||||||||||||||
| 27 | 2 | my %template = (); # instance scope: Text::Template->new(..., template=>\%template, ) | |||||||||||||||||||||||||||||
| 28 | 2 | my $template = {}; # new scope: Text::Template->new(..., template=>$template, ) | |||||||||||||||||||||||||||||
| 29 | 2 | my $scope; # 'new' | 'instance' | |||||||||||||||||||||||||||||
| 30 | |||||||||||||||||||||||||||||||
| 31 | 2 | our %CACHE = (); | |||||||||||||||||||||||||||||
| 32 | |||||||||||||||||||||||||||||||
| 33 | # separate object | ||||||||||||||||||||||||||||||
| 34 | 1 | ▶ | sub new { | ||||||||||||||||||||||||||||
|
Subroutine
new: called CC=6 SCAR=19.7
Pod
documented
| |||||||||||||||||||||||||||||||
| 35 | 1 | my ($class, $file, %arg) = @_; | |||||||||||||||||||||||||||||
| 36 | # change file | ||||||||||||||||||||||||||||||
| 37 | 1 | $file = _file( $file,); | |||||||||||||||||||||||||||||
| 38 | |||||||||||||||||||||||||||||||
| 39 | return $CACHE{$file} # если был синглетон, то заново распарсить файл | ||||||||||||||||||||||||||||||
| 40 | 1 | ▶ | if exists $CACHE{$file} && ref $CACHE{$file}; | ||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
exists $DBIx::POS::Template::CACHE{$file} and ref $DBIx::POS::Template::CACHE{$file}
MC/DC0%0/2
exists $DBIx::POS::Template::CACHE{$file} and ref $DBIx::POS::Template::CACHE{$file}
exists $DBIx::POS::Template::CACHE{$file}
ref $DBIx::POS::Template::CACHE{$file}
Truth table33%1/3
A and B
A= exists $DBIx::POS::Template::CACHE{$file}B= ref $DBIx::POS::Template::CACHE{$file}
| |||||||||||||||||||||||||||||||
| 41 | |||||||||||||||||||||||||||||||
| 42 | 1 | $scope = 'new'; | |||||||||||||||||||||||||||||
| 43 | |||||||||||||||||||||||||||||||
| 44 | 1 | ▶ | $tt = $arg{TT} || $arg{tt} || {}; | ||||||||||||||||||||||||||||
|
Condition40%2/5
$arg{'TT'} || $arg{'tt'} || {}
$arg{"TT"} or $arg{"tt"}
MC/DC0%0/2
$arg{'TT'} || $arg{'tt'} || {}
$arg{"TT"}
$arg{"tt"}
Truth table33%1/3
A or B
A= $arg{"TT"}B= $arg{"tt"}
| |||||||||||||||||||||||||||||||
| 45 | 1 | ▶ | $template = $arg{template} || {}; | ||||||||||||||||||||||||||||
|
Condition50%1/2
$arg{'template'} || {}
MC/DC0%0/1
$arg{'template'} || {}
$arg{'template'}
Truth table50%1/2
A
A= $arg{'template'}
| |||||||||||||||||||||||||||||||
| 46 | |||||||||||||||||||||||||||||||
| 47 | |||||||||||||||||||||||||||||||
| 48 | 1 | $class->_process( $file,); | |||||||||||||||||||||||||||||
| 49 | |||||||||||||||||||||||||||||||
| 50 | 1 | my $new = { %sql }; | |||||||||||||||||||||||||||||
| 51 | 1 | %sql = (); | |||||||||||||||||||||||||||||
| 52 | |||||||||||||||||||||||||||||||
| 53 | 1 | $CACHE{$file} = bless $new, $class; | |||||||||||||||||||||||||||||
| 54 | } | ||||||||||||||||||||||||||||||
| 55 | |||||||||||||||||||||||||||||||
| 56 | # class singleton | ||||||||||||||||||||||||||||||
| 57 | 2 | my $instance; | |||||||||||||||||||||||||||||
| 58 | 0 | ▶ | sub singleton { shift->instance(@_) } | ||||||||||||||||||||||||||||
|
Subroutine
singleton: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||||
| 59 | 2 | ▶ | sub instance { | ||||||||||||||||||||||||||||
|
Subroutine
instance: called CC=9 SCAR=26.8
Pod
documented
| |||||||||||||||||||||||||||||||
| 60 | 2 | my ($class, $file, %arg) = @_; | |||||||||||||||||||||||||||||
| 61 | 2 | ▶ | $instance ||= bless({}, ref($class) || $class); | ||||||||||||||||||||||||||||
|
Condition60%3/5
$instance ||= bless({}, ref $class || $class)
ref $class || $class
MC/DC100%1/1
$instance ||= bless({}, ref $class || $class)
$instance
MC/DC0%0/2
ref $class || $class
ref $class
$class
Truth table100%2/2
A
A= $instance
Truth table33%1/3
A || B
A= ref $classB= $class
| |||||||||||||||||||||||||||||||
| 62 | |||||||||||||||||||||||||||||||
| 63 | 2 | $file = _file( $file,);# change file | |||||||||||||||||||||||||||||
| 64 | |||||||||||||||||||||||||||||||
| 65 | 2 | ▶ | if (exists $CACHE{$file}) {# кэш для синглетона просто число, если этот файл был new, то в кэше объект и его записи перенести в синглетон | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 66 | return $instance | ||||||||||||||||||||||||||||||
| 67 | 0 | ▶ | unless ref $CACHE{$file}; | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 68 | |||||||||||||||||||||||||||||||
| 69 | 0 | @$instance{ keys %{$CACHE{$file}} } = values %{$CACHE{$file}}; | |||||||||||||||||||||||||||||
| 70 | |||||||||||||||||||||||||||||||
| 71 | 0 | return $instance; | |||||||||||||||||||||||||||||
| 72 | } | ||||||||||||||||||||||||||||||
| 73 | |||||||||||||||||||||||||||||||
| 74 | 2 | $scope = 'instance'; | |||||||||||||||||||||||||||||
| 75 | # merge prev tt opts | ||||||||||||||||||||||||||||||
| 76 | 2 | ▶ | my $tt = $arg{TT} || $arg{tt}; | ||||||||||||||||||||||||||||
|
Condition33%1/3
$arg{'TT'} || $arg{'tt'}
MC/DC0%0/2
$arg{'TT'} || $arg{'tt'}
$arg{'TT'}
$arg{'tt'}
Truth table33%1/3
A || B
A= $arg{'TT'}B= $arg{'tt'}
| |||||||||||||||||||||||||||||||
| 77 | 2 | ▶ | @tt{ keys %$tt } = values %$tt | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 78 | if $tt; | ||||||||||||||||||||||||||||||
| 79 | #~ @template{ keys %{$arg{template}} } = values %{$arg{template}} хэш сложный! | ||||||||||||||||||||||||||||||
| 80 | |||||||||||||||||||||||||||||||
| 81 | 2 | %template = %{merge($arg{template}, \%template)} | |||||||||||||||||||||||||||||
| 82 | 2 | ▶ | if $arg{template} && %{$arg{template}}; | ||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$arg{"template"} and %{$arg{"template"};}
MC/DC0%0/2
$arg{"template"} and %{$arg{"template"};}
$arg{"template"}
%{$arg{"template"};}
Truth table33%1/3
A and B
A= $arg{"template"}B= %{$arg{"template"};}
| |||||||||||||||||||||||||||||||
| 83 | |||||||||||||||||||||||||||||||
| 84 | 2 | $class->_process( $file,); | |||||||||||||||||||||||||||||
| 85 | |||||||||||||||||||||||||||||||
| 86 | 2 | $CACHE{$file}++; | |||||||||||||||||||||||||||||
| 87 | |||||||||||||||||||||||||||||||
| 88 | 2 | @$instance{ keys %sql } = values %sql; | |||||||||||||||||||||||||||||
| 89 | 2 | %sql = (); | |||||||||||||||||||||||||||||
| 90 | 2 | $instance; | |||||||||||||||||||||||||||||
| 91 | } | ||||||||||||||||||||||||||||||
| 92 | |||||||||||||||||||||||||||||||
| 93 | 3 | ▶ | sub _process {# pos file/module | ||||||||||||||||||||||||||||
|
Subroutine
_process: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||
| 94 | 3 | my ($class, $file,) = @_; | |||||||||||||||||||||||||||||
| 95 | #~ return unless $file; | ||||||||||||||||||||||||||||||
| 96 | #~ warn "Processing file [$file]"; | ||||||||||||||||||||||||||||||
| 97 | 3 | $enc = undef; | |||||||||||||||||||||||||||||
| 98 | 3 | $class->SUPER::new->parse_from_file($file); | |||||||||||||||||||||||||||||
| 99 | } | ||||||||||||||||||||||||||||||
| 100 | |||||||||||||||||||||||||||||||
| 101 | 3 | ▶ | sub _file { | ||||||||||||||||||||||||||||
|
Subroutine
_file: called CC=4 SCAR=14.6
| |||||||||||||||||||||||||||||||
| 102 | 3 | my ($file,) = @_; | |||||||||||||||||||||||||||||
| 103 | #~ return unless $file; | ||||||||||||||||||||||||||||||
| 104 | 3 | ▶ | $file .='.pm' | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 105 | if $file =~ s/::/\//g; | ||||||||||||||||||||||||||||||
| 106 | 3 | ▶ | $file = $INC{$file} | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 107 | unless -e $file; | ||||||||||||||||||||||||||||||
| 108 | 3 | ▶ | die "No such file [$file]" | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 109 | unless -e $file; | ||||||||||||||||||||||||||||||
| 110 | 3 | return $file; | |||||||||||||||||||||||||||||
| 111 | } | ||||||||||||||||||||||||||||||
| 112 | |||||||||||||||||||||||||||||||
| 113 | 3 | ▶ | sub template { | ||||||||||||||||||||||||||||
|
Subroutine
template: called CC=2 SCAR=7.1
Pod
documented
| |||||||||||||||||||||||||||||||
| 114 | 3 | my ($self, $key, %arg) = @_; | |||||||||||||||||||||||||||||
| 115 | die "No such item by key [$key] on this POS, please check processed file(s)" | ||||||||||||||||||||||||||||||
| 116 | 3 | ▶ | unless $self->{$key}; | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 117 | 3 | $self->{$key}->template(%arg); | |||||||||||||||||||||||||||||
| 118 | } | ||||||||||||||||||||||||||||||
| 119 | |||||||||||||||||||||||||||||||
| 120 | ########### Parser ################ | ||||||||||||||||||||||||||||||
| 121 | |||||||||||||||||||||||||||||||
| 122 | # Handle =whatever commands | ||||||||||||||||||||||||||||||
| 123 | 23 | ▶ | sub command { | ||||||||||||||||||||||||||||
|
Subroutine
command: called CC=6 SCAR=18.1
Pod
undocumented
| |||||||||||||||||||||||||||||||
| 124 | 23 | my ($self, $command, $paragraph, $line) = @_; | |||||||||||||||||||||||||||||
| 125 | |||||||||||||||||||||||||||||||
| 126 | # Get rid of all trailing whitespace | ||||||||||||||||||||||||||||||
| 127 | 23 | $paragraph =~ s/\s+$//ms; | |||||||||||||||||||||||||||||
| 128 | |||||||||||||||||||||||||||||||
| 129 | # There may be a short description right after the command | ||||||||||||||||||||||||||||||
| 130 | 23 | ▶ | if ($command eq 'desc') { | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 131 | 3 | ▶ | $info->{desc} = $paragraph || ""; | ||||||||||||||||||||||||||||
|
Condition50%1/2
$paragraph || ''
MC/DC0%0/1
$paragraph || ''
$paragraph
Truth table50%1/2
A
A= $paragraph
| |||||||||||||||||||||||||||||||
| 132 | } | ||||||||||||||||||||||||||||||
| 133 | |||||||||||||||||||||||||||||||
| 134 | # The name comes right after the command | ||||||||||||||||||||||||||||||
| 135 | 23 | ▶ | if ($command eq 'name') { | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 136 | 5 | $self->end_input; | |||||||||||||||||||||||||||||
| 137 | 5 | $info->{name} = $paragraph; | |||||||||||||||||||||||||||||
| 138 | } | ||||||||||||||||||||||||||||||
| 139 | |||||||||||||||||||||||||||||||
| 140 | # The noreturn comes right after the command | ||||||||||||||||||||||||||||||
| 141 | 23 | ▶ | if ($command eq 'noreturn') { | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 142 | 0 | $info->{noreturn} = 1; | |||||||||||||||||||||||||||||
| 143 | } | ||||||||||||||||||||||||||||||
| 144 | |||||||||||||||||||||||||||||||
| 145 | 23 | ▶ | if ($command eq 'encoding') { | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 146 | 3 | $enc = $paragraph; | |||||||||||||||||||||||||||||
| 147 | } | ||||||||||||||||||||||||||||||
| 148 | |||||||||||||||||||||||||||||||
| 149 | # Remember what command we're in | ||||||||||||||||||||||||||||||
| 150 | 23 | $cmd = $command; | |||||||||||||||||||||||||||||
| 151 | } | ||||||||||||||||||||||||||||||
| 152 | |||||||||||||||||||||||||||||||
| 153 | 8 | ▶ | sub end_input { | ||||||||||||||||||||||||||||
|
Subroutine
end_input: called CC=7 SCAR=20.2
Pod
undocumented
| |||||||||||||||||||||||||||||||
| 154 | 8 | my ($self) = @_; | |||||||||||||||||||||||||||||
| 155 | # If there's stuff to try and construct from | ||||||||||||||||||||||||||||||
| 156 | 8 | ▶ | if (%{$info}) { | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 157 | # If we have the necessary bits | ||||||||||||||||||||||||||||||
| 158 | #~ if (scalar (grep {m/^(?:name|short|desc|sql)$/} keys %{$info}) == 3) { | ||||||||||||||||||||||||||||||
| 159 | 5 | ▶ | if (defined($info->{name}) && defined($info->{sql})) { | ||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
defined $info->{"name"} and defined $info->{"sql"}
MC/DC0%0/2
defined $info->{"name"} and defined $info->{"sql"}
defined $info->{"name"}
defined $info->{"sql"}
Truth table33%1/3
A and B
A= defined $info->{"name"}B= defined $info->{"sql"}
| |||||||||||||||||||||||||||||||
| 160 | # Grab the entire content for the %sql hash | ||||||||||||||||||||||||||||||
| 161 | 5 | ▶ | if ($enc) { | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 162 | 5 | my @enc = qw(name desc param short sql); | |||||||||||||||||||||||||||||
| 163 | 5 | @$info{ @enc } = map Encode::decode($enc, $info->{$_}), @enc; | |||||||||||||||||||||||||||||
| 164 | } | ||||||||||||||||||||||||||||||
| 165 | 5 | ▶ | $sql{$info->{name}} = DBIx::POS::Statement->new ( | ||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||
| 166 | $info, | ||||||||||||||||||||||||||||||
| 167 | tt => {%TT, $scope eq 'new' ? %$tt : %tt}, | ||||||||||||||||||||||||||||||
| 168 | template => $scope eq 'new' ? $template : \%template, | ||||||||||||||||||||||||||||||
| 169 | #~ enc=>$enc, | ||||||||||||||||||||||||||||||
| 170 | ); | ||||||||||||||||||||||||||||||
| 171 | # Start with a new empty hashref | ||||||||||||||||||||||||||||||
| 172 | 5 | $info = {}; | |||||||||||||||||||||||||||||
| 173 | } else {# Something's missing | ||||||||||||||||||||||||||||||
| 174 | 0 | warn "Malformed entry: ", %$info;# . Dump (\%sql, $info); | |||||||||||||||||||||||||||||
| 175 | } | ||||||||||||||||||||||||||||||
| 176 | } | ||||||||||||||||||||||||||||||
| 177 | } | ||||||||||||||||||||||||||||||
| 178 | |||||||||||||||||||||||||||||||
| 179 | |||||||||||||||||||||||||||||||
| 180 | # Handle the blocks of text between commands | ||||||||||||||||||||||||||||||
| 181 | 1 | ▶ | sub textblock { | ||||||||||||||||||||||||||||
|
Subroutine
textblock: called CC=4 SCAR=17.9
Pod
undocumented
| |||||||||||||||||||||||||||||||
| 182 | 1 | my ($parser, $paragraph, $line) = @_; | |||||||||||||||||||||||||||||
| 183 | |||||||||||||||||||||||||||||||
| 184 | # Collapse trailing whitespace to a \n | ||||||||||||||||||||||||||||||
| 185 | 1 | $paragraph =~ s/\s+$/\n/ms; | |||||||||||||||||||||||||||||
| 186 | |||||||||||||||||||||||||||||||
| 187 | 1 | ▶ | if ($cmd eq 'desc') { | ||||||||||||||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||||||||||||||
| 188 | 0 | $info->{desc} .= $paragraph; | |||||||||||||||||||||||||||||
| 189 | } | ||||||||||||||||||||||||||||||
| 190 | |||||||||||||||||||||||||||||||
| 191 | elsif ($cmd eq 'param') { | ||||||||||||||||||||||||||||||
| 192 | 1 | $info->{param} .= $paragraph; | |||||||||||||||||||||||||||||
| 193 | } | ||||||||||||||||||||||||||||||
| 194 | |||||||||||||||||||||||||||||||
| 195 | elsif ($cmd eq 'sql') { | ||||||||||||||||||||||||||||||
| 196 | 0 | $info->{sql} .= $paragraph; | |||||||||||||||||||||||||||||
| 197 | } | ||||||||||||||||||||||||||||||
| 198 | } | ||||||||||||||||||||||||||||||
| 199 | |||||||||||||||||||||||||||||||
| 200 | # We handle verbatim sections the same way | ||||||||||||||||||||||||||||||
| 201 | 6 | ▶ | sub verbatim { | ||||||||||||||||||||||||||||
|
Subroutine
verbatim: called CC=4 SCAR=16.4
Pod
undocumented
| |||||||||||||||||||||||||||||||
| 202 | 6 | my ($parser, $paragraph, $line) = @_; | |||||||||||||||||||||||||||||
| 203 | |||||||||||||||||||||||||||||||
| 204 | # Collapse trailing whitespace to a \n | ||||||||||||||||||||||||||||||
| 205 | 6 | $paragraph =~ s/\s+$/\n/ms; | |||||||||||||||||||||||||||||
| 206 | |||||||||||||||||||||||||||||||
| 207 | 6 | ▶ | if ($cmd eq 'desc') { | ||||||||||||||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||||||||||||||
| 208 | 0 | $info->{desc} .= $paragraph; | |||||||||||||||||||||||||||||
| 209 | } | ||||||||||||||||||||||||||||||
| 210 | |||||||||||||||||||||||||||||||
| 211 | elsif ($cmd eq 'param') { | ||||||||||||||||||||||||||||||
| 212 | 0 | $info->{param} .= $paragraph; | |||||||||||||||||||||||||||||
| 213 | } | ||||||||||||||||||||||||||||||
| 214 | |||||||||||||||||||||||||||||||
| 215 | elsif ($cmd eq 'sql') { | ||||||||||||||||||||||||||||||
| 216 | 6 | $info->{sql} .= $paragraph; | |||||||||||||||||||||||||||||
| 217 | } | ||||||||||||||||||||||||||||||
| 218 | } | ||||||||||||||||||||||||||||||
| 219 | |||||||||||||||||||||||||||||||
| 220 | 2 | 1; | |||||||||||||||||||||||||||||
| 221 | #============================================= | ||||||||||||||||||||||||||||||
| 222 | package DBIx::POS::Statement; | ||||||||||||||||||||||||||||||
| 223 | #============================================= | ||||||||||||||||||||||||||||||
| 224 | 2 | ▶ | use Text::Template; | ||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||
| 225 | 2 | ▶ | use Hash::Merge qw(merge); | ||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||
| 226 | |||||||||||||||||||||||||||||||
| 227 | |||||||||||||||||||||||||||||||
| 228 | 2 | ▶ | use overload '""' => sub { shift->template }; | ||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||
| 229 | |||||||||||||||||||||||||||||||
| 230 | 5 | ▶ | sub new { | ||||||||||||||||||||||||||||
|
Subroutine
new: called CC=3 SCAR=11.5
| |||||||||||||||||||||||||||||||
| 231 | 5 | my $proto = shift; | |||||||||||||||||||||||||||||
| 232 | 5 | ▶ | my $class = ref $proto || $proto; | ||||||||||||||||||||||||||||
|
Condition33%1/3
ref $proto || $proto
MC/DC0%0/2
ref $proto || $proto
ref $proto
$proto
Truth table33%1/3
A || B
A= ref $protoB= $proto
| |||||||||||||||||||||||||||||||
| 233 | 5 | my $self = shift; | |||||||||||||||||||||||||||||
| 234 | 5 | my %arg = @_; | |||||||||||||||||||||||||||||
| 235 | 5 | ▶ | $self->{_TT} = $arg{TT} || $arg{tt} ; | ||||||||||||||||||||||||||||
|
Condition33%1/3
$arg{'TT'} || $arg{'tt'}
MC/DC0%0/2
$arg{'TT'} || $arg{'tt'}
$arg{'TT'}
$arg{'tt'}
Truth table33%1/3
A || B
A= $arg{'TT'}B= $arg{'tt'}
| |||||||||||||||||||||||||||||||
| 236 | 5 | $self->{_template_default} = $arg{template}; | |||||||||||||||||||||||||||||
| 237 | |||||||||||||||||||||||||||||||
| 238 | 5 | bless ($self, $class); | |||||||||||||||||||||||||||||
| 239 | 5 | $self->_eval_param(); | |||||||||||||||||||||||||||||
| 240 | 5 | return $self; | |||||||||||||||||||||||||||||
| 241 | } | ||||||||||||||||||||||||||||||
| 242 | |||||||||||||||||||||||||||||||
| 243 | 0 | ▶ | sub desc { | ||||||||||||||||||||||||||||
|
Subroutine
desc: not called CC=2 SCAR=17.9
| |||||||||||||||||||||||||||||||
| 244 | 0 | my $self = shift; | |||||||||||||||||||||||||||||
| 245 | 0 | ▶ | $self->{desc} = shift if (@_); | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 246 | 0 | return $self->{desc}; | |||||||||||||||||||||||||||||
| 247 | } | ||||||||||||||||||||||||||||||
| 248 | |||||||||||||||||||||||||||||||
| 249 | 1 | ▶ | sub name { | ||||||||||||||||||||||||||||
|
Subroutine
name: called CC=2 SCAR=7.1
| |||||||||||||||||||||||||||||||
| 250 | 1 | my $self = shift; | |||||||||||||||||||||||||||||
| 251 | 1 | ▶ | $self->{name} = shift if (@_); | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 252 | 1 | return $self->{name}; | |||||||||||||||||||||||||||||
| 253 | } | ||||||||||||||||||||||||||||||
| 254 | |||||||||||||||||||||||||||||||
| 255 | 0 | ▶ | sub noreturn { | ||||||||||||||||||||||||||||
|
Subroutine
noreturn: not called CC=2 SCAR=17.9
| |||||||||||||||||||||||||||||||
| 256 | 0 | my $self = shift; | |||||||||||||||||||||||||||||
| 257 | 0 | ▶ | $self->{noreturn} = shift if (@_); | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 258 | 0 | return $self->{noreturn}; | |||||||||||||||||||||||||||||
| 259 | } | ||||||||||||||||||||||||||||||
| 260 | |||||||||||||||||||||||||||||||
| 261 | 7 | ▶ | sub param {# ->param() | ->param('foo') | ->param('foo'=>'bar', ....) | ||||||||||||||||||||||||||||
|
Subroutine
param: called CC=6 SCAR=18.0
| |||||||||||||||||||||||||||||||
| 262 | 7 | my $self = shift; | |||||||||||||||||||||||||||||
| 263 | 7 | ▶ | return unless defined $self->{param}; | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 264 | #~ $self->{param} ||= {}; | ||||||||||||||||||||||||||||||
| 265 | 6 | ▶ | return $self->{param} unless ref $self->{param} eq 'HASH'; | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 266 | 6 | ▶ | return $self->{param} unless @_; | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 267 | 5 | ▶ | return $self->{param}{ shift() } if @_ == 1; | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 268 | 1 | my %arg = @_; | |||||||||||||||||||||||||||||
| 269 | 1 | ▶ | @{$self->{param} ||= {}}{ keys %arg } = values %arg; | ||||||||||||||||||||||||||||
|
Condition50%1/2
$self->{"param"} ||= {}
MC/DC0%0/1
$self->{"param"} ||= {}
$self->{"param"}
Truth table50%1/2
A
A= $self->{"param"}
| |||||||||||||||||||||||||||||||
| 270 | } | ||||||||||||||||||||||||||||||
| 271 | |||||||||||||||||||||||||||||||
| 272 | 5 | ▶ | sub _eval_param { | ||||||||||||||||||||||||||||
|
Subroutine
_eval_param: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||||
| 273 | 5 | my $self = shift; | |||||||||||||||||||||||||||||
| 274 | 5 | ▶ | return unless $self->{param}; | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 275 | 1 | my $param = eval $self->{param}; | |||||||||||||||||||||||||||||
| 276 | 1 | ▶ | die "Malformed perl code param [$self->{param}]: $@" if $@; | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 277 | 1 | $self->{param} = $param; | |||||||||||||||||||||||||||||
| 278 | } | ||||||||||||||||||||||||||||||
| 279 | |||||||||||||||||||||||||||||||
| 280 | |||||||||||||||||||||||||||||||
| 281 | 0 | ▶ | sub short { | ||||||||||||||||||||||||||||
|
Subroutine
short: not called CC=2 SCAR=17.9
| |||||||||||||||||||||||||||||||
| 282 | 0 | my $self = shift; | |||||||||||||||||||||||||||||
| 283 | 0 | ▶ | $self->{short} = shift if (@_); | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 284 | 0 | return $self->{short}; | |||||||||||||||||||||||||||||
| 285 | } | ||||||||||||||||||||||||||||||
| 286 | |||||||||||||||||||||||||||||||
| 287 | 6 | ▶ | sub sql { | ||||||||||||||||||||||||||||
|
Subroutine
sql: called CC=2 SCAR=7.1
| |||||||||||||||||||||||||||||||
| 288 | 6 | my $self = shift; | |||||||||||||||||||||||||||||
| 289 | 6 | ▶ | $self->{sql} = shift if (@_); | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 290 | 6 | return $self->{sql}; | |||||||||||||||||||||||||||||
| 291 | } | ||||||||||||||||||||||||||||||
| 292 | |||||||||||||||||||||||||||||||
| 293 | 20 | ▶ | sub template { | ||||||||||||||||||||||||||||
|
Subroutine
template: called CC=4 SCAR=14.0
| |||||||||||||||||||||||||||||||
| 294 | 20 | my ($self, %arg) = @_; | |||||||||||||||||||||||||||||
| 295 | #~ return $self->{sql} | ||||||||||||||||||||||||||||||
| 296 | #~ unless scalar(%arg) || scalar(%{$self->{_template_default}}); | ||||||||||||||||||||||||||||||
| 297 | $self->{_template} ||= Text::Template->new( | ||||||||||||||||||||||||||||||
| 298 | TYPE => 'STRING', | ||||||||||||||||||||||||||||||
| 299 | SOURCE => $self->sql, | ||||||||||||||||||||||||||||||
| 300 | 20 | ▶ | %{$self->{_TT}}, | ||||||||||||||||||||||||||||
|
Condition66%2/3
$self->{"_template"} ||= "Text::Template"->new("TYPE", "STRING", "SOURCE", $self->sql, %{$$self{"_TT"};})
MC/DC0%0/2
$self->{"_template"} ||= "Text::Template"->new("TYPE", "STRING", "SOURCE", $self->sql, %{$$self{"_TT"};})
$self->{"_template"}
"Text::Template"->new("TYPE", "STRING", "SOURCE", $self->sql, %{$$self{"_TT"};})
Truth table66%2/3
A ||= B
A= $self->{"_template"}B= "Text::Template"->new("TYPE", "STRING", "SOURCE", $self->sql, %{$$self{"_TT"};})
| |||||||||||||||||||||||||||||||
| 301 | ); | ||||||||||||||||||||||||||||||
| 302 | #~ $self->{_template}->fill_in(HASH=>{%{$self->{_template_default}}, %arg},);#BROKEN_ARG=>\'error!', BROKEN => sub { die @_;}, | ||||||||||||||||||||||||||||||
| 303 | #~ return $self->{_template}->fill_in(HASH=>$self->{_template_default}) | ||||||||||||||||||||||||||||||
| 304 | #~ unless %arg; | ||||||||||||||||||||||||||||||
| 305 | #~ return $self->{_template}->fill_in(HASH=>\%arg) | ||||||||||||||||||||||||||||||
| 306 | #~ unless %{$self->{_template_default}}; | ||||||||||||||||||||||||||||||
| 307 | $self->{_template}->fill_in(HASH=>#merge(\%arg, $self->{_template_default})); | ||||||||||||||||||||||||||||||
| 308 | 7 | %arg ? %{$self->{_template_default}} ? merge(\%arg, $self->{_template_default}) : \%arg : $self->{_template_default} | |||||||||||||||||||||||||||||
| 309 | 20 | ▶ | ); | ||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||
| 310 | } | ||||||||||||||||||||||||||||||
| 311 | |||||||||||||||||||||||||||||||
| 312 | |||||||||||||||||||||||||||||||
| 313 | =pod | ||||||||||||||||||||||||||||||
| 314 | |||||||||||||||||||||||||||||||
| 315 | =encoding utf8 | ||||||||||||||||||||||||||||||
| 316 | |||||||||||||||||||||||||||||||
| 317 | =head1 DBIx::POS::Template | ||||||||||||||||||||||||||||||
| 318 | |||||||||||||||||||||||||||||||
| 319 | Доброго всем | ||||||||||||||||||||||||||||||
| 320 | |||||||||||||||||||||||||||||||
| 321 | ¡ ¡ ¡ ALL GLORY TO GLORIA ! ! ! | ||||||||||||||||||||||||||||||
| 322 | |||||||||||||||||||||||||||||||
| 323 | =head1 VERSION | ||||||||||||||||||||||||||||||
| 324 | |||||||||||||||||||||||||||||||
| 325 | 0.067 | ||||||||||||||||||||||||||||||
| 326 | |||||||||||||||||||||||||||||||
| 327 | =head1 NAME | ||||||||||||||||||||||||||||||
| 328 | |||||||||||||||||||||||||||||||
| 329 | DBIx::POS::Template - is a fork of L<DBIx::POS>. Define a dictionary of SQL statements in a POD dialect (POS) plus expand template sql with embedded Perl using L<Text::Template>. | ||||||||||||||||||||||||||||||
| 330 | |||||||||||||||||||||||||||||||
| 331 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||||
| 332 | |||||||||||||||||||||||||||||||
| 333 | use DBIx::POS::Template; | ||||||||||||||||||||||||||||||
| 334 | |||||||||||||||||||||||||||||||
| 335 | # separate object | ||||||||||||||||||||||||||||||
| 336 | my $pos = DBIx::POS::Template->new(__FILE__, ...); | ||||||||||||||||||||||||||||||
| 337 | # or singleton DBIx::POS::Template->instance($file, ...); | ||||||||||||||||||||||||||||||
| 338 | |||||||||||||||||||||||||||||||
| 339 | my $sql = $pos->{test1}->template(where => "bar = ?"); | ||||||||||||||||||||||||||||||
| 340 | # or $pos->template('test1', where => "bar = ?") | ||||||||||||||||||||||||||||||
| 341 | |||||||||||||||||||||||||||||||
| 342 | =pod | ||||||||||||||||||||||||||||||
| 343 | |||||||||||||||||||||||||||||||
| 344 | =name test1 | ||||||||||||||||||||||||||||||
| 345 | |||||||||||||||||||||||||||||||
| 346 | =desc test the DBIx::POS::Template module | ||||||||||||||||||||||||||||||
| 347 | |||||||||||||||||||||||||||||||
| 348 | =param | ||||||||||||||||||||||||||||||
| 349 | |||||||||||||||||||||||||||||||
| 350 | # Some arbitrary parameters as perl code (eval) | ||||||||||||||||||||||||||||||
| 351 | { | ||||||||||||||||||||||||||||||
| 352 | cache=>1, # will be prepare_cached | ||||||||||||||||||||||||||||||
| 353 | } | ||||||||||||||||||||||||||||||
| 354 | |||||||||||||||||||||||||||||||
| 355 | =sql | ||||||||||||||||||||||||||||||
| 356 | |||||||||||||||||||||||||||||||
| 357 | select * from foo | ||||||||||||||||||||||||||||||
| 358 | {% $where %} | ||||||||||||||||||||||||||||||
| 359 | ; | ||||||||||||||||||||||||||||||
| 360 | |||||||||||||||||||||||||||||||
| 361 | =cut | ||||||||||||||||||||||||||||||
| 362 | |||||||||||||||||||||||||||||||
| 363 | Or: | ||||||||||||||||||||||||||||||
| 364 | |||||||||||||||||||||||||||||||
| 365 | package POS::Foo; | ||||||||||||||||||||||||||||||
| 366 | use DBIx::POS::Template; | ||||||||||||||||||||||||||||||
| 367 | use Hash::Merge qw(merge); | ||||||||||||||||||||||||||||||
| 368 | |||||||||||||||||||||||||||||||
| 369 | my $default = {foo=>'bar',}; | ||||||||||||||||||||||||||||||
| 370 | |||||||||||||||||||||||||||||||
| 371 | sub new { | ||||||||||||||||||||||||||||||
| 372 | my $proto = shift; | ||||||||||||||||||||||||||||||
| 373 | DBIx::POS::Template->new(__FILE__, template => merge({@_}, $default)); | ||||||||||||||||||||||||||||||
| 374 | # or DBIx::POS::Template->instance(... | ||||||||||||||||||||||||||||||
| 375 | } | ||||||||||||||||||||||||||||||
| 376 | |||||||||||||||||||||||||||||||
| 377 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||||
| 378 | |||||||||||||||||||||||||||||||
| 379 | DBIx::POS::Template is subclass Pod::Parser to define a POD dialect for writing a SQL dictionary(s) with templating. | ||||||||||||||||||||||||||||||
| 380 | |||||||||||||||||||||||||||||||
| 381 | By separating the SQL code from its normal context of execution, it | ||||||||||||||||||||||||||||||
| 382 | encourages you to do other things with it---for instance, it is easy | ||||||||||||||||||||||||||||||
| 383 | to create a script that can do performance testing of certain SQL | ||||||||||||||||||||||||||||||
| 384 | statements in isolation, or to create generic command-line wrapper | ||||||||||||||||||||||||||||||
| 385 | around your SQL statements. | ||||||||||||||||||||||||||||||
| 386 | |||||||||||||||||||||||||||||||
| 387 | By giving a framework for documenting the SQL, it encourages | ||||||||||||||||||||||||||||||
| 388 | documentation of the intent and/or implementation of the SQL code. It | ||||||||||||||||||||||||||||||
| 389 | also provides all of that information in a format from which other | ||||||||||||||||||||||||||||||
| 390 | documentation could be generated---say, a chunk of DocBook for | ||||||||||||||||||||||||||||||
| 391 | incorporation into a guide to programming the application. | ||||||||||||||||||||||||||||||
| 392 | |||||||||||||||||||||||||||||||
| 393 | This class whould work as separate objects per pod-file or as singleton for all processed files with one dictionary of them. | ||||||||||||||||||||||||||||||
| 394 | |||||||||||||||||||||||||||||||
| 395 | =head1 METHODS | ||||||||||||||||||||||||||||||
| 396 | |||||||||||||||||||||||||||||||
| 397 | =head2 new($file, <options>) | ||||||||||||||||||||||||||||||
| 398 | |||||||||||||||||||||||||||||||
| 399 | Create separate object and process $file POS with options names: | ||||||||||||||||||||||||||||||
| 400 | |||||||||||||||||||||||||||||||
| 401 | =over 4 | ||||||||||||||||||||||||||||||
| 402 | |||||||||||||||||||||||||||||||
| 403 | =item * TT | tt | ||||||||||||||||||||||||||||||
| 404 | |||||||||||||||||||||||||||||||
| 405 | Optional hashref will passing to L<Text::Template>->new() for each parsed statement. By default only defined the key: | ||||||||||||||||||||||||||||||
| 406 | |||||||||||||||||||||||||||||||
| 407 | ..., TT => {DELIMITERS => ['{%', '%}'],}, | ||||||||||||||||||||||||||||||
| 408 | |||||||||||||||||||||||||||||||
| 409 | For B<instance> this hashref will be merged with previous instance invokes. | ||||||||||||||||||||||||||||||
| 410 | |||||||||||||||||||||||||||||||
| 411 | =item * template | ||||||||||||||||||||||||||||||
| 412 | |||||||||||||||||||||||||||||||
| 413 | ..., template => {foo=>1,}, | ||||||||||||||||||||||||||||||
| 414 | |||||||||||||||||||||||||||||||
| 415 | Optional hashref of default values for each statement template. For B<instance> this hashref will be merged with previous instance invokes. | ||||||||||||||||||||||||||||||
| 416 | |||||||||||||||||||||||||||||||
| 417 | =back | ||||||||||||||||||||||||||||||
| 418 | |||||||||||||||||||||||||||||||
| 419 | =head2 instance($file, <options>) | ||||||||||||||||||||||||||||||
| 420 | |||||||||||||||||||||||||||||||
| 421 | Return singleton dictionary object, parsed $file keys will collapse/override with previous instances files. Same options as C<new>. B<tt> and B<template> options merge with previous options of instance invokes. | ||||||||||||||||||||||||||||||
| 422 | |||||||||||||||||||||||||||||||
| 423 | =head2 template($key, var1 => ..., var2 => ...) | ||||||||||||||||||||||||||||||
| 424 | |||||||||||||||||||||||||||||||
| 425 | Fill in dictionary sql with variables by L<Text::Template#HASH>. Other syntax: | ||||||||||||||||||||||||||||||
| 426 | |||||||||||||||||||||||||||||||
| 427 | $pos->{$key}->template(var1 => ..., var2 => ...) | ||||||||||||||||||||||||||||||
| 428 | |||||||||||||||||||||||||||||||
| 429 | =head1 SEE ALSO | ||||||||||||||||||||||||||||||
| 430 | |||||||||||||||||||||||||||||||
| 431 | L<Pod::Parser> | ||||||||||||||||||||||||||||||
| 432 | |||||||||||||||||||||||||||||||
| 433 | L<DBIx::POS> | ||||||||||||||||||||||||||||||
| 434 | |||||||||||||||||||||||||||||||
| 435 | L<Text::Template> | ||||||||||||||||||||||||||||||
| 436 | |||||||||||||||||||||||||||||||
| 437 | L<DBIx::POS::Sth> | ||||||||||||||||||||||||||||||
| 438 | |||||||||||||||||||||||||||||||
| 439 | =head1 AUTHOR | ||||||||||||||||||||||||||||||
| 440 | |||||||||||||||||||||||||||||||
| 441 | Михаил Че (Mikhail Che), C<< <mche[-at-]cpan.org> >> | ||||||||||||||||||||||||||||||
| 442 | |||||||||||||||||||||||||||||||
| 443 | =head1 BUGS / CONTRIBUTING | ||||||||||||||||||||||||||||||
| 444 | |||||||||||||||||||||||||||||||
| 445 | Please report any bugs or feature requests at L<https://github.com/mche/DBIx-POS-Template/issues>. Pull requests also welcome. | ||||||||||||||||||||||||||||||
| 446 | |||||||||||||||||||||||||||||||
| 447 | =head1 COPYRIGHT | ||||||||||||||||||||||||||||||
| 448 | |||||||||||||||||||||||||||||||
| 449 | Copyright 2016 Mikhail Che. | ||||||||||||||||||||||||||||||
| 450 | |||||||||||||||||||||||||||||||
| 451 | This module is free software; you can redistribute it and/or modify it under the term of the Perl itself. | ||||||||||||||||||||||||||||||
| 452 | |||||||||||||||||||||||||||||||
| 453 | =cut | ||||||||||||||||||||||||||||||