lib/DBIx/DR/PlPlaceHolders.pm

filter: Statementstmt 98.5% 135 / 137 Branchbran 84.7% 39 / 46 Conditioncond 55.5% 10 / 18 MC/DCmcdc 18.1% 2 / 11 Subroutinesub 100% 23 / 23 Podpod 25.0% 2 / 8 totaltotal 86.8% 211 / 243 SCAR 35.5
CC
34
Cov
92%
sql_transform
14.0
__ANON__
7.4
set_helper
5.2
CRAP 34.7
« lib/DBIx/DR/PerlishTemplate.pm ↑ Summary blib/lib/DBIx/DR/Util.pm »
1 2 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
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  
5 package DBIx::DR::PlPlaceHolders;
6 2 ▶use Mouse;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 2 extends 'DBIx::DR::PerlishTemplate';
8 2 ▶use DBIx::DR::ByteStream;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 2 ▶use Carp ();
Subroutine BEGIN: called CC=1 SCAR=0.0
11 2 ▶use File::Spec ();
Subroutine BEGIN: called CC=1 SCAR=0.0
12 2 ▶use Digest::MD5 ();
Subroutine BEGIN: called CC=1 SCAR=0.0
13 2 ▶use Encode qw(encode);
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 2 has sql_dir => (is => 'bare', isa => 'Str');
16 2 has file_suffix => (is => 'rw', isa => 'Str', default => '.sql.ep');
17 2 ▶has helpers => (is => 'ro', isa => 'HashRef', default => sub {{}});
Subroutine __ANON__: called CC=1 SCAR=0.0
18  
19 sub sql_dir {
20 28 ▶ my ($self, $dir) = @_;
Subroutine sql_dir: called CC=4 SCAR=14.0
Pod undocumented
21 28 ▶ if (defined $dir) {
Branch: 2 branches
BranchTrueFalseTotal
if (defined $dir) { } 2 26 28
elsif (@_ >= 2) { } 0 26 26
22 2 ▶ Carp::croak "Diectory $dir is not found or a dir" unless -d $dir;
Branch: 1 branch
BranchTrueFalseTotal
unless -d $dir 1 1 2
23 1 $self->{sql_dir} = File::Spec->rel2abs($dir);
24 } elsif(@_ >= 2) {
25 0 $self->{sql_dir} = undef;
26 }
27 27 return $self->{sql_dir};
28 }
29  
30 sub BUILD {
31 3 ▶ my ($self) = @_;
Subroutine BUILD: called CC=1 SCAR=0.0
Pod documented
32  
33 # add default helpers
34 $self->set_helper(
35 include => sub {
36 1 ▶ my ($tpl, $file, @args) = @_;
Subroutine __ANON__: called CC=1 SCAR=0.0
37  
38 1 my $res = ref($self)->new(
39 pretokens => $self->prepretokens,
40 prepretokens => $self->prepretokens,
41 helpers => $self->helpers,
42 sql_dir => $self->sql_dir,
43 file_suffix => $self->file_suffix,
44 )->sql_transform(
45 -f => $file,
46 @args
47 );
48  
49  
50 1 $tpl->immediate($res->sql);
51 1 $tpl->add_bind_value($res->bind_values);
52 1 return DBIx::DR::ByteStream->new('');
53 },
54  
55 list => sub {
56 5 ▶ my ($tpl, @args) = @_;
Subroutine __ANON__: called CC=1 SCAR=0.0
57 5 $tpl->immediate(join ',' => map '?', @args);
58 5 $tpl->add_bind_value(@args);
59 5 return DBIx::DR::ByteStream->new('');
60 },
61  
62 hlist => sub {
63 6 ▶ my ($tpl, @args) = @_;
Subroutine __ANON__: called CC=3 SCAR=11.0
64 6 ▶ if ('ARRAY' eq ref $args[0]) {
Branch: 1 branch
BranchTrueFalseTotal
if ("ARRAY" eq ref $args[0]) 5 1 6
65 5 my $filter = shift @args;
66 5 $tpl->immediate(
67 join ',' => (
68 '(' .
69 join(',' => ('?')x @$filter) .
70 ')'
71 )x @args
72 );
73 5 for my $a (@args) {
74 15 $tpl->add_bind_value( map { $a->{$_} } @$filter );
75 }
76 4 return DBIx::DR::ByteStream->new('');
77 }
78 $tpl->immediate(
79 join ',' => map {
80 1 '(' .
81 join(',' => ('?') x keys %$_) .
82 ')'
83 } @args
84 );
85 1 $tpl->add_bind_value(map { values %$_ } @args);
86 1 return DBIx::DR::ByteStream->new('');
87 },
88  
89 stacktrace => sub {
90 1 ▶ my ($tpl, $skip, $depth, $sep) = @_;
Subroutine __ANON__: called CC=7 SCAR=20.1
91  
92 1 ▶ $depth ||= 32;
Condition50%1/2
$depth ||= 32
l!l
10
MC/DC0%0/1
$depth ||= 32
$depth
Truth table50%1/2
A
A= $depth
Aresult
11
00
93 1 ▶ $skip ||= 0;
Condition50%1/2
$skip ||= 0
l!l
01
MC/DC0%0/1
$skip ||= 0
$skip
Truth table50%1/2
A
A= $skip
Aresult
11
00
94  
95 1 $skip += 7;
96 1 $depth += 6;
97 1 ▶ $sep = ", " unless defined $sep;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $sep 1 0 1
98  
99 1 my @stack;
100  
101 1 ▶ for (my $i = $skip ? $skip - 1 : 0; $i < $depth; $i++) {
Branch: 2 branches
BranchTrueFalseTotal
$skip ? : 1 0 1
if ($i < $depth) 1 1 2
102 1 my @line = caller $i;
103 1 ▶ last unless @line;
Branch: 1 branch
BranchTrueFalseTotal
unless @line 0 1 1
104 1 push @stack => sprintf '%s:%s', @line[1,2];
105 }
106 1 return DBIx::DR::ByteStream->new(join $sep, @stack);
107 },
108 3 );
109  
110 3 $self;
111 }
112  
113  
114 sub sql_transform {
115 50 ▶ my $self = shift;
Subroutine sql_transform: called CC=14 SCAR=26.4
Pod undocumented
116 50 my ($sql, %opts);
117  
118 50 my $pt;
119  
120 50 ▶ if (@_ % 2) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_ % 2) { } 36 14 50
121 36 ($sql, %opts) = @_;
122 36 delete $opts{-f};
123 } else {
124 14 %opts = @_;
125 14 ▶ Carp::croak $self->usage unless $opts{-f};
Branch: 1 branch
BranchTrueFalseTotal
unless $opts{"-f"} 1 13 14
126 13 my $file = $opts{-f};
127  
128 13 ▶ $file = File::Spec->catfile($self->sql_dir, $file)
Branch: 1 branch
BranchTrueFalseTotal
if $self->sql_dir and not $file =~ m[^/] 11 2 13
Condition66%2/3
$self->sql_dir and not $file =~ m[^/]
!ll&&!rl&&r
0211
MC/DC50%1/2
$self->sql_dir and not $file =~ m[^/]
$self->sql_dir not $file =~ m[^/]
Truth table66%2/3
A and not(B)
A= $self->sql_dirB= not $file =~ m[^/]
ABresult
0X0
100
111
129 if $self->sql_dir and $file !~ m{^/};
130 13 my $resuffix = quotemeta $self->file_suffix;
131 13 ▶ $file .= $self->file_suffix
Branch: 1 branch
BranchTrueFalseTotal
if $self->file_suffix and not $file =~ /$resuffix$/ 7 6 13
Condition66%2/3
$self->file_suffix and not $file =~ /$resuffix$/
!ll&&!rl&&r
067
MC/DC50%1/2
$self->file_suffix and not $file =~ /$resuffix$/
$self->file_suffix not $file =~ /$resuffix$/
Truth table66%2/3
A and not(B)
A= $self->file_suffixB= not $file =~ /$resuffix$/
ABresult
0X0
100
111
132 if $self->file_suffix and $file !~ /$resuffix$/;
133  
134 13 my @fstat = stat $file;
135 13 ▶ Carp::croak "Can't find file $file" unless @fstat;
Branch: 1 branch
BranchTrueFalseTotal
unless @fstat 1 12 13
136 12 $opts{-f} = $file;
137 }
138  
139  
140 48 ▶ my $namespace = $opts{-f} || $sql;
Condition66%2/3
$opts{'-f'} || $sql
l!l&&r!l&&!r
12360
MC/DC0%0/2
$opts{'-f'} || $sql
$opts{'-f'} $sql
Truth table66%2/3
A || B
A= $opts{'-f'}B= $sql
ABresult
1X1
011
000
141 48 ▶ $namespace = encode utf8 => $namespace if utf8::is_utf8($namespace);
Branch: 1 branch
BranchTrueFalseTotal
if utf8::is_utf8($namespace) 1 47 48
142 48 $namespace = Digest::MD5::md5_hex($namespace);
143 48 $self->{namespace} = __PACKAGE__ . '::Sandbox::t' . $namespace;
144  
145 48 $self -> clean_prepends
146 -> clean_preprepends
147 ;
148  
149 48 for my $name (keys %{ $self->helpers }) {
150 228 $self->preprepend(
151 'BEGIN{ ' .
152 "*" . $name . '= sub {' .
153 '$_PTPL->call_helper(q{' . $name . '}, @_)' .
154 '} ' .
155 '}'
156 );
157 }
158  
159 48 my @args;
160 48 for (keys %opts) {
161 54 ▶ next unless /^\w/;
Branch: 1 branch
BranchTrueFalseTotal
unless /^\w/ 23 31 54
162 31 $self->prepend("my \$$_ = shift");
163 31 push @args, $opts{$_};
164 }
165  
166 48 ▶ if ($sql) {
Branch: 1 branch
BranchTrueFalseTotal
if ($sql) { } 36 12 48
167 36 $self->render($sql, @args);
168 } else {
169 12 $self->render_file($opts{-f}, @args);
170 }
171  
172 42 my $res =
173 DBIx::DR::PlPlaceHolders::TransformResult->new(rtemplate => $self);
174  
175 # clean memory
176 42 $self->{sql} = '';
177 42 $self->{variables} = [];
178  
179 42 $res;
180 }
181  
182  
183 sub call_helper {
184 17 ▶ my ($self, $name, @args) = @_;
Subroutine call_helper: called CC=2 SCAR=7.1
Pod undocumented
185 Carp::croak "Helper '$name' is not found or has already been removed"
186 17 ▶ unless exists $self->helpers->{ $name };
Branch: 1 branch
BranchTrueFalseTotal
unless exists $self->helpers->{$name} 0 17 17
187 17 $self->helpers->{ $name }->($self, @args);
188 }
189  
190  
191 sub set_helper {
192 6 ▶ my ($self, %opts) = @_;
Subroutine set_helper: called CC=5 SCAR=16.5
Pod undocumented
193 6 ▶ Carp::croak $self->usage unless %opts;
Branch: 1 branch
BranchTrueFalseTotal
unless %opts 1 5 6
194 5 ▶ while (my ($n, $s) = each %opts) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %opts) 15 5 20
195 15 ▶ Carp::croak $self->usage unless 'CODE' eq ref $s and $n =~ /^\w/;
Branch: 1 branch
BranchTrueFalseTotal
unless "CODE" eq ref $s and $n =~ /^\w/ 0 15 15
Condition33%1/3
"CODE" eq ref $s and $n =~ /^\w/
!ll&&!rl&&r
0015
MC/DC0%0/2
"CODE" eq ref $s and $n =~ /^\w/
"CODE" eq ref $s $n =~ /^\w/
Truth table33%1/3
A and B
A= "CODE" eq ref $sB= $n =~ /^\w/
ABresult
0X0
100
111
196 15 $self->helpers->{ $n } = $s ;
197 }
198 5 $self;
199 }
200  
201 sub usage {
202 2 ▶ my ($self) = @_;
Subroutine usage: called CC=3 SCAR=11.3
Pod undocumented
203 2 my @caller = caller 1;
204  
205 2 ▶ return 'Usage: $ph->sql_transform($sql | -f => $sql_file, ...)'
Branch: 1 branch
BranchTrueFalseTotal
if $caller[3] =~ /sql_transform$/ 1 1 2
206 if $caller[3] =~ /sql_transform$/;
207 1 ▶ return 'Usage: $ph->set_helper($name => sub { ... })'
Branch: 1 branch
BranchTrueFalseTotal
if $caller[3] =~ /set_helper$/ 1 0 1
208 if $caller[3] =~ /set_helper$/;
209  
210 0 return $caller[3];
211 }
212  
213 package DBIx::DR::PlPlaceHolders::TransformResult;
214 2 ▶use Mouse;
Subroutine BEGIN: called CC=1 SCAR=0.0
215  
216 2 has rtemplate => (is => 'ro', isa => 'Object', weak_ref => 1);
217 2 has sql => (is => 'ro', isa => 'Str');
218  
219 sub BUILD {
220 42 ▶ my ($self) = @_;
Subroutine BUILD: called CC=1 SCAR=0.0
Pod documented
221 42 $self->{sql} = $self->rtemplate->sql;
222 42 $self->{bind_values} = $self->rtemplate->variables;
223 }
224  
225 sub bind_values {
226 43 ▶ my ($self) = @_;
Subroutine bind_values: called CC=3 SCAR=11.0
Pod undocumented
227 43 ▶ return @{ $self->{bind_values} } if wantarray;
Branch: 1 branch
BranchTrueFalseTotal
if wantarray 19 24 43
228 24 ▶ return $self->{bind_values} || [];
Condition50%1/2
$self->{'bind_values'} || []
l!l
240
MC/DC0%0/1
$self->{'bind_values'} || []
$self->{'bind_values'}
Truth table50%1/2
A
A= $self->{'bind_values'}
Aresult
11
00
229 }
230  
231 2 1;
232  
233 =head1 NAME
234  
235 DBIx::DR::PlPlaceHolders - template converter for L<DBIx::DR>.
236  
237 =head1 COPYRIGHT
238  
239 Copyright (C) 2011 Dmitry E. Oboukhov <unera@debian.org>
240 Copyright (C) 2011 Roman V. Nikolaev <rshadow@rambler.ru>
241  
242 This program is free software, you can redistribute it and/or
243 modify it under the terms of the Artistic License.
244  
245 =cut
246  
« lib/DBIx/DR/PerlishTemplate.pm ↑ Summary blib/lib/DBIx/DR/Util.pm »