Oracle/SQLMaker.pm

filter: Statementstmt 94.7% 90 / 95 Branchbran 71.4% 30 / 42 Conditioncond 75.0% 15 / 20 MC/DCmcdc 60.0% 6 / 10 Subroutinesub 94.1% 16 / 17 Podpod 100% 3 / 3 totaltotal 85.5% 160 / 187 CC 30 SCAR 19.2
CC
30
Cov
86%
_quote
13.8
_connect_by
8.0
_insert_returning
6.2
CRAP 6.8
« Oracle/Storage.pm ↑ Summary Oracle/Util.pm »
1 package DBIO::Oracle::SQLMaker;
2 # ABSTRACT: Oracle-specific SQL generation for DBIO
3  
4 11 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 11 our $VERSION = '0.900007';
7  
8 11 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 11 ▶use base qw( DBIO::SQLMaker );
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 11 ▶use DBIO::Oracle::Identifier ();
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14  
15 15 ▶sub new {
Subroutine new: called CC=2 SCAR=7.2
Pod documented
16 15 my $self = shift;
17 0 ▶ my %opts = (ref $_[0] eq 'HASH') ? %{$_[0]} : @_;
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] eq 'HASH' ? : 0 15 15
18  
19 # PRIOR stays an old-style special_op: it is consumed inside CONNECT BY /
20 # START WITH via _recurse_where (the SQL::Abstract v1 engine), which does
21 # not route through the new expand_op mechanism -- and the base's
22 # disable_old_special_ops does not reach that path either, so it keeps
23 # working. The canonical parenthesized WHERE renderer is now provided
24 # centrally by DBIO::SQLMaker, so the previous local render_clause override
25 # is gone.
26 15 push @{$opts{special_ops}}, {
27 regex => qr/^prior$/i,
28 handler => '_where_field_PRIOR',
29 };
30  
31 15 $self->next::method(\%opts);
32 }
33  
34  
35 7 ▶sub operator_words {
Subroutine operator_words: called CC=1 SCAR=0.0
Pod documented
36 7 my $self = shift;
37 7 return ($self->next::method(@_), 'prior');
38 }
39  
40  
41 15 ▶sub apply_limit {
Subroutine apply_limit: called CC=1 SCAR=0.0
Pod documented
42 15 my ($self, $sql, $rs_attrs, $rows, $offset) = @_;
43 15 return $self->_RowNum($sql, $rs_attrs, $rows, $offset);
44 }
45  
46 76 ▶sub _assemble_binds {
Subroutine _assemble_binds: called CC=2 SCAR=6.9
47 76 my $self = shift;
48 76 ▶ return map { @{ (delete $self->{"${_}_bind"}) || [] } } (qw/pre_select select from where oracle_connect_by group having order limit/);
Condition100%2/2
delete $$self{"${_}_bind"} || []
l!l
283401
MC/DC100%1/1
delete $$self{"${_}_bind"} || []
delete $$self{"${_}_bind"}
Truth table100%2/2
A
A= delete $$self{"${_}_bind"}
Aresult
11
00
49 }
50  
51 102 ▶sub _parse_rs_attrs {
Subroutine _parse_rs_attrs: called CC=1 SCAR=0.0
52 102 my $self = shift;
53 102 my ($rs_attrs) = @_;
54  
55 102 my ($cb_sql, @cb_bind) = $self->_connect_by($rs_attrs);
56 102 push @{$self->{oracle_connect_by_bind}}, @cb_bind;
57  
58 102 my $sql = $self->next::method(@_);
59  
60 102 return "$cb_sql $sql";
61 }
62  
63 102 ▶sub _connect_by {
Subroutine _connect_by: called CC=8 SCAR=20.8
64 102 my ($self, $attrs) = @_;
65  
66 102 my $sql = '';
67 102 my @bind;
68  
69 102 ▶ if ( ref($attrs) eq 'HASH' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $attrs eq "HASH") 102 0 102
70 102 ▶ if ( $attrs->{'start_with'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($attrs->{"start_with"}) 26 76 102
71 26 my ($ws, @wb) = $self->_recurse_where( $attrs->{'start_with'} );
72 26 $sql .= $self->_sqlcase(' start with ') . $ws;
73 26 push @bind, @wb;
74 }
75 102 ▶ if ( my $connect_by = $attrs->{'connect_by'} || $attrs->{'connect_by_nocycle'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $connect_by = $attrs->{"connect_by"} || $attrs->{"connect_by_nocycle"}) 26 76 102
Condition100%3/3
$attrs->{'connect_by'} || $attrs->{'connect_by_nocycle'}
l!l&&r!l&&!r
22476
MC/DC100%2/2
$attrs->{'connect_by'} || $attrs->{'connect_by_nocycle'}
$attrs->{'connect_by'} $attrs->{'connect_by_nocycle'}
Truth table100%3/3
A || B
A= $attrs->{'connect_by'}B= $attrs->{'connect_by_nocycle'}
ABresult
1X1
011
000
76 26 my ($connect_by_sql, @connect_by_sql_bind) = $self->_recurse_where( $connect_by );
77 $sql .= sprintf(" %s %s",
78 26 ▶ ( $attrs->{'connect_by_nocycle'} ) ? $self->_sqlcase('connect by nocycle')
Branch: 1 branch
BranchTrueFalseTotal
$attrs->{'connect_by_nocycle'} ? : 4 22 26
79 : $self->_sqlcase('connect by'),
80 $connect_by_sql,
81 );
82 26 push @bind, @connect_by_sql_bind;
83 }
84 102 ▶ if ( $attrs->{'order_siblings_by'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($attrs->{"order_siblings_by"}) 2 100 102
85 2 $sql .= $self->_order_siblings_by( $attrs->{'order_siblings_by'} );
86 }
87 }
88  
89 102 ▶ return wantarray ? ($sql, @bind) : $sql;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 102 0 102
90 }
91  
92 2 ▶sub _order_siblings_by {
Subroutine _order_siblings_by: called CC=5 SCAR=17.0
93 2 my ( $self, $arg ) = @_;
94  
95 2 my ( @sql, @bind );
96 2 for my $c ( $self->_order_by_chunks($arg) ) {
97 2 ▶ if (ref $c) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $c) { } 2 0 2
98 2 push @sql, shift @$c;
99 2 push @bind, @$c;
100 }
101 else {
102 0 push @sql, $c;
103 }
104 }
105  
106 2 ▶ my $sql =
Branch: 1 branch
BranchTrueFalseTotal
@sql ? : 2 0 2
107 @sql
108 ? sprintf( '%s %s', $self->_sqlcase(' order siblings by'), join( ', ', @sql ) )
109 : '';
110  
111 2 ▶ return wantarray ? ( $sql, @bind ) : $sql;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 2 2
112 }
113  
114 29 ▶sub _where_field_PRIOR {
Subroutine _where_field_PRIOR: called CC=1 SCAR=0.0
115 29 my ($self, $lhs, $op, $rhs) = @_;
116 29 my ($sql, @bind) = $self->_recurse_where ($rhs);
117  
118 29 $sql = sprintf ('%s = %s %s ',
119 $self->_convert($self->_quote($lhs)),
120 $self->_sqlcase ($op),
121 $sql
122 );
123  
124 29 return ($sql, @bind);
125 }
126  
127  
128 11 my %PSEUDO_COLUMNS = map { $_ => 1 } qw(
129 LEVEL
130 ROWNUM
131 ROWID
132 CONNECT_BY_ISCYCLE
133 CONNECT_BY_ISLEAF
134 );
135  
136 0 ▶sub _pseudo_columns { \%PSEUDO_COLUMNS }
Subroutine _pseudo_columns: not called CC=1 SCAR=6.9
137  
138 1126 ▶sub _quote {
Subroutine _quote: called CC=12 SCAR=26.2
139 1126 my ($self, $label) = @_;
140  
141 1126 ▶ return '' unless defined $label;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $label 0 1126 1126
142 0 ▶ return ${$label} if ref($label) eq 'SCALAR';
Branch: 1 branch
BranchTrueFalseTotal
if ref $label eq "SCALAR" 0 1126 1126
143  
144 # Oracle pseudo-columns (LEVEL, ROWNUM, ...) are not real identifiers and must
145 # never be wrapped in quote_char -- "LEVEL" => ORA-00904. They arrive here both
146 # as a bare string (string render path) and as a single-segment arrayref (the
147 # SQL::Abstract v2 already-split identifier path, e.g. from order_by); catch
148 # both and emit the bare keyword.
149 1126 ▶ if (ref $label eq 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $label eq "ARRAY") 314 812 1126
150 return $label->[0]
151 if @$label == 1 and defined $label->[0] and !ref $label->[0]
152 314 ▶ and $PSEUDO_COLUMNS{ uc $label->[0] };
Branch: 1 branch
BranchTrueFalseTotal
if @$label == 1 and defined $label->[0] and not ref $label->[0] and $PSEUDO_COLUMNS{uc $label->[0]} 6 308 314
Condition77%7/9
@$label == 1 and defined $label->[0] and not ref $label->[0] and $PSEUDO_COLUMNS{uc $label->[0]}
!ll&&!rl&&r
1151936
@$label == 1 and defined $label->[0] and not ref $label->[0]
!ll&&!rl&&r
1150199
@$label == 1 and defined $label->[0]
!ll&&!rl&&r
1150199
MC/DC50%2/4
@$label == 1 and defined $label->[0] and not ref $label->[0] and $PSEUDO_COLUMNS{uc $label->[0]}
@$label == 1 defined $label->[0] not ref $label->[0] $PSEUDO_COLUMNS{uc $label->[0]}
Truth table60%3/5
A and B and not(C) and D
A= @$label == 1B= defined $label->[0]C= not ref $label->[0]D= $PSEUDO_COLUMNS{uc $label->[0]}
ABCDresult
0XXX0
10XX0
110X0
11100
11111
153  
154 # SQL::Abstract v2 hands qualified identifiers to _render_ident as an arrayref
155 # of already-split segments, so the string regex below never sees them and the
156 # WHERE/HAVING column qualifiers bypass Oracle identifier shortening while the
157 # SELECT/FROM string path shortens them. Shorten each over-long segment here
158 # too, with the same keyword-less _shorten_identifier call so both paths emit
159 # the identical short name.
160 return $self->next::method([
161 308 ▶ map { (defined and !ref and length > 30) ? $self->_shorten_identifier($_) : $_ } @$label
Branch: 1 branch
BranchTrueFalseTotal
defined $_ && !ref($_) && length $_ > 30 ? : 28 395 423
Condition50%3/6
defined $_ and not ref $_ and length $_ > 30
!ll&&!rl&&r
039528
defined $_ and not ref $_
!ll&&!rl&&r
00423
MC/DC33%1/3
defined $_ and not ref $_ and length $_ > 30
defined $_ not ref $_ length $_ > 30
Truth table50%2/4
A and not(B) and C
A= defined $_B= not ref $_C= length $_ > 30
ABCresult
0XX0
10X0
1100
1111
162 ]);
163 }
164  
165 812 ▶ return $label if $PSEUDO_COLUMNS{ uc $label };
Branch: 1 branch
BranchTrueFalseTotal
if $PSEUDO_COLUMNS{uc $label} 0 812 812
166  
167 812 $label =~ s/ ( [^\.]{31,} ) /$self->_shorten_identifier($1)/gxe;
168  
169 812 $self->next::method($label);
170 }
171  
172 104 ▶sub _shorten_identifier {
Subroutine _shorten_identifier: called CC=1 SCAR=0.0
173 104 my ($self, $to_shorten, $keywords) = @_;
174 104 return DBIO::Oracle::Identifier::shorten($to_shorten, $keywords);
175 }
176  
177 23 ▶sub _unqualify_colname {
Subroutine _unqualify_colname: called CC=1 SCAR=0.0
178 23 my ($self, $fqcn) = @_;
179  
180 23 return $self->_shorten_identifier($self->next::method($fqcn));
181 }
182  
183  
184  
185 6 ▶sub _insert_returning {
Subroutine _insert_returning: called CC=6 SCAR=18.2
186 6 my ($self, $options) = @_;
187  
188 6 my $f = $options->{returning};
189  
190 6 my ($f_list, @f_names) = do {
191 6 ▶ if (! ref $f) {
Branch: 3 branches
BranchTrueFalseTotal
if (not ref $f) { } 2 4 6
elsif (ref $f eq 'ARRAY') { } 2 2 4
elsif (ref $f eq 'SCALAR') { } 2 0 2
192 (
193 2 $self->_quote($f),
194 $f,
195 )
196 }
197 elsif (ref $f eq 'ARRAY') {
198 (
199 2 (join ', ', map { $self->_quote($_) } @$f),
200 @$f,
201 )
202 }
203 elsif (ref $f eq 'SCALAR') {
204 (
205 2 $$f,
206 $$f,
207 )
208 }
209 else {
210 0 $self->throw_exception("Unsupported INSERT RETURNING option $f");
211 }
212 };
213  
214 my $rc_ref = $options->{returning_container}
215 6 ▶ or $self->throw_exception('No returning container supplied for IR values');
Branch: 1 branch
BranchTrueFalseTotal
my $rc_ref = $options->{"returning_container"} or $self->throw_exception("No returning container supplied for IR values") 6 0 6
216  
217 6 @$rc_ref = (undef) x @f_names;
218  
219 return (
220 ( join (' ',
221 $self->_sqlcase(' returning'),
222 $f_list,
223 $self->_sqlcase('into'),
224 join (', ', ('?') x @f_names ),
225 )),
226 map {
227 6 ▶ $self->{bindtype} eq 'columns'
Branch: 1 branch
BranchTrueFalseTotal
$self->{'bindtype'} eq 'columns' ? : 10 0 10
228 ? [ $f_names[$_] => \$rc_ref->[$_] ]
229 : \$rc_ref->[$_]
230 } (0 .. $#f_names),
231 );
232 }
233  
234  
235 11 1;
236  
237 __END__
« Oracle/Storage.pm ↑ Summary Oracle/Util.pm »