Oracle/SQLMaker.pm

filter: Statementstmt 56.5% 52 / 92 Branchbran 35.7% 15 / 42 Conditioncond 50.0% 10 / 20 MC/DCmcdc 10.0% 1 / 10 Subroutinesub 62.5% 10 / 16 Podpod 100% 2 / 2 totaltotal 49.4% 90 / 182 CC 30 SCAR 31.1
CC
30
Cov
50%
_connect_by
72.0
_order_siblings_by
30.0
_quote
18.7
CRAP 22.4
« Oracle/Introspect/Keys.pm ↑ Summary Oracle/SQLMaker/Joins.pm »
1 package DBIO::Oracle::SQLMaker;
2 # ABSTRACT: Oracle-specific SQL generation for DBIO
3  
4 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 3 ▶use base qw( DBIO::SQLMaker );
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 3 ▶use DBIO::Oracle::Identifier ();
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11  
12 2 ▶sub new {
Subroutine new: called CC=2 SCAR=7.2
Pod documented
13 2 my $self = shift;
14 2 ▶ my %opts = (ref $_[0] eq 'HASH') ? %{$_[0]} : @_;
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] eq 'HASH' ? : 0 2 2
15  
16 # PRIOR stays an old-style special_op: it is consumed inside CONNECT BY /
17 # START WITH via _recurse_where (the SQL::Abstract v1 engine), which does
18 # not route through the new expand_op mechanism -- and the base's
19 # disable_old_special_ops does not reach that path either, so it keeps
20 # working. The canonical parenthesized WHERE renderer is now provided
21 # centrally by DBIO::SQLMaker, so the previous local render_clause override
22 # is gone.
23 2 push @{$opts{special_ops}}, {
24 regex => qr/^prior$/i,
25 handler => '_where_field_PRIOR',
26 };
27  
28 2 $self->next::method(\%opts);
29 }
30  
31  
32 0 ▶sub apply_limit {
Subroutine apply_limit: not called CC=1 SCAR=6.9
Pod documented
33 0 my ($self, $sql, $rs_attrs, $rows, $offset) = @_;
34 0 return $self->_RowNum($sql, $rs_attrs, $rows, $offset);
35 }
36  
37 8 ▶sub _assemble_binds {
Subroutine _assemble_binds: called CC=2 SCAR=6.9
38 8 my $self = shift;
39 8 ▶ 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
1656
MC/DC100%1/1
delete $$self{"${_}_bind"} || []
delete $$self{"${_}_bind"}
Truth table100%2/2
A
A= delete $$self{"${_}_bind"}
Aresult
11
00
40 }
41  
42 0 ▶sub _parse_rs_attrs {
Subroutine _parse_rs_attrs: not called CC=1 SCAR=6.9
43 0 my $self = shift;
44 0 my ($rs_attrs) = @_;
45  
46 0 my ($cb_sql, @cb_bind) = $self->_connect_by($rs_attrs);
47 0 push @{$self->{oracle_connect_by_bind}}, @cb_bind;
48  
49 0 my $sql = $self->next::method(@_);
50  
51 0 return "$cb_sql $sql";
52 }
53  
54 0 ▶sub _connect_by {
Subroutine _connect_by: not called CC=8 SCAR=42.8
55 0 my ($self, $attrs) = @_;
56  
57 0 my $sql = '';
58 0 my @bind;
59  
60 0 ▶ if ( ref($attrs) eq 'HASH' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $attrs eq "HASH") 0 0 0
61 0 ▶ if ( $attrs->{'start_with'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($attrs->{"start_with"}) 0 0 0
62 0 my ($ws, @wb) = $self->_recurse_where( $attrs->{'start_with'} );
63 0 $sql .= $self->_sqlcase(' start with ') . $ws;
64 0 push @bind, @wb;
65 }
66 0 ▶ 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"}) 0 0 0
Condition0%0/3
$attrs->{'connect_by'} || $attrs->{'connect_by_nocycle'}
l!l&&r!l&&!r
000
MC/DC0%0/2
$attrs->{'connect_by'} || $attrs->{'connect_by_nocycle'}
$attrs->{'connect_by'} $attrs->{'connect_by_nocycle'}
Truth table0%0/3
A || B
A= $attrs->{'connect_by'}B= $attrs->{'connect_by_nocycle'}
ABresult
1X1
011
000
67 0 my ($connect_by_sql, @connect_by_sql_bind) = $self->_recurse_where( $connect_by );
68 $sql .= sprintf(" %s %s",
69 0 ▶ ( $attrs->{'connect_by_nocycle'} ) ? $self->_sqlcase('connect by nocycle')
Branch: 1 branch
BranchTrueFalseTotal
$attrs->{'connect_by_nocycle'} ? : 0 0 0
70 : $self->_sqlcase('connect by'),
71 $connect_by_sql,
72 );
73 0 push @bind, @connect_by_sql_bind;
74 }
75 0 ▶ if ( $attrs->{'order_siblings_by'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($attrs->{"order_siblings_by"}) 0 0 0
76 0 $sql .= $self->_order_siblings_by( $attrs->{'order_siblings_by'} );
77 }
78 }
79  
80 0 ▶ return wantarray ? ($sql, @bind) : $sql;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 0 0
81 }
82  
83 0 ▶sub _order_siblings_by {
Subroutine _order_siblings_by: not called CC=5 SCAR=34.0
84 0 my ( $self, $arg ) = @_;
85  
86 0 my ( @sql, @bind );
87 0 for my $c ( $self->_order_by_chunks($arg) ) {
88 0 ▶ if (ref $c) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $c) { } 0 0 0
89 0 push @sql, shift @$c;
90 0 push @bind, @$c;
91 }
92 else {
93 0 push @sql, $c;
94 }
95 }
96  
97 0 ▶ my $sql =
Branch: 1 branch
BranchTrueFalseTotal
@sql ? : 0 0 0
98 @sql
99 ? sprintf( '%s %s', $self->_sqlcase(' order siblings by'), join( ', ', @sql ) )
100 : '';
101  
102 0 ▶ return wantarray ? ( $sql, @bind ) : $sql;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 0 0
103 }
104  
105 3 ▶sub _where_field_PRIOR {
Subroutine _where_field_PRIOR: called CC=1 SCAR=0.0
106 3 my ($self, $lhs, $op, $rhs) = @_;
107 3 my ($sql, @bind) = $self->_recurse_where ($rhs);
108  
109 3 $sql = sprintf ('%s = %s %s ',
110 $self->_convert($self->_quote($lhs)),
111 $self->_sqlcase ($op),
112 $sql
113 );
114  
115 3 return ($sql, @bind);
116 }
117  
118  
119 3 my %PSEUDO_COLUMNS = map { $_ => 1 } qw(
120 LEVEL
121 ROWNUM
122 ROWID
123 CONNECT_BY_ISCYCLE
124 CONNECT_BY_ISLEAF
125 );
126  
127 0 ▶sub _pseudo_columns { \%PSEUDO_COLUMNS }
Subroutine _pseudo_columns: not called CC=1 SCAR=6.9
128  
129 123 ▶sub _quote {
Subroutine _quote: called CC=12 SCAR=29.3
130 123 my ($self, $label) = @_;
131  
132 123 ▶ return '' unless defined $label;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $label 0 123 123
133 123 ▶ return ${$label} if ref($label) eq 'SCALAR';
Branch: 1 branch
BranchTrueFalseTotal
if ref $label eq "SCALAR" 0 123 123
134  
135 # Oracle pseudo-columns (LEVEL, ROWNUM, ...) are not real identifiers and must
136 # never be wrapped in quote_char -- "LEVEL" => ORA-00904. They arrive here both
137 # as a bare string (string render path) and as a single-segment arrayref (the
138 # SQL::Abstract v2 already-split identifier path, e.g. from order_by); catch
139 # both and emit the bare keyword.
140 123 ▶ if (ref $label eq 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $label eq "ARRAY") 36 87 123
141 return $label->[0]
142 if @$label == 1 and defined $label->[0] and !ref $label->[0]
143 36 ▶ 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]} 0 36 36
Condition66%6/9
@$label == 1 and defined $label->[0] and not ref $label->[0] and $PSEUDO_COLUMNS{uc $label->[0]}
!ll&&!rl&&r
24120
@$label == 1 and defined $label->[0] and not ref $label->[0]
!ll&&!rl&&r
24012
@$label == 1 and defined $label->[0]
!ll&&!rl&&r
24012
MC/DC0%0/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 table40%2/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
144  
145 # SQL::Abstract v2 hands qualified identifiers to _render_ident as an arrayref
146 # of already-split segments, so the string regex below never sees them and the
147 # WHERE/HAVING column qualifiers bypass Oracle identifier shortening while the
148 # SELECT/FROM string path shortens them. Shorten each over-long segment here
149 # too, with the same keyword-less _shorten_identifier call so both paths emit
150 # the identical short name.
151 return $self->next::method([
152 36 ▶ map { (defined and !ref and length > 30) ? $self->_shorten_identifier($_) : $_ } @$label
Branch: 1 branch
BranchTrueFalseTotal
defined $_ && !ref($_) && length $_ > 30 ? : 0 60 60
Condition33%2/6
defined $_ and not ref $_ and length $_ > 30
!ll&&!rl&&r
0600
defined $_ and not ref $_
!ll&&!rl&&r
0060
MC/DC0%0/3
defined $_ and not ref $_ and length $_ > 30
defined $_ not ref $_ length $_ > 30
Truth table25%1/4
A and not(B) and C
A= defined $_B= not ref $_C= length $_ > 30
ABCresult
0XX0
10X0
1100
1111
153 ]);
154 }
155  
156 87 ▶ return $label if $PSEUDO_COLUMNS{ uc $label };
Branch: 1 branch
BranchTrueFalseTotal
if $PSEUDO_COLUMNS{uc $label} 0 87 87
157  
158 87 $label =~ s/ ( [^\.]{31,} ) /$self->_shorten_identifier($1)/gxe;
159  
160 87 $self->next::method($label);
161 }
162  
163 6 ▶sub _shorten_identifier {
Subroutine _shorten_identifier: called CC=1 SCAR=0.0
164 6 my ($self, $to_shorten, $keywords) = @_;
165 6 return DBIO::Oracle::Identifier::shorten($to_shorten, $keywords);
166 }
167  
168 0 ▶sub _unqualify_colname {
Subroutine _unqualify_colname: not called CC=1 SCAR=6.9
169 0 my ($self, $fqcn) = @_;
170  
171 0 return $self->_shorten_identifier($self->next::method($fqcn));
172 }
173  
174  
175  
176 3 ▶sub _insert_returning {
Subroutine _insert_returning: called CC=6 SCAR=18.2
177 3 my ($self, $options) = @_;
178  
179 3 my $f = $options->{returning};
180  
181 3 my ($f_list, @f_names) = do {
182 3 ▶ if (! ref $f) {
Branch: 3 branches
BranchTrueFalseTotal
if (not ref $f) { } 1 2 3
elsif (ref $f eq 'ARRAY') { } 1 1 2
elsif (ref $f eq 'SCALAR') { } 1 0 1
183 (
184 1 $self->_quote($f),
185 $f,
186 )
187 }
188 elsif (ref $f eq 'ARRAY') {
189 (
190 1 (join ', ', map { $self->_quote($_) } @$f),
191 @$f,
192 )
193 }
194 elsif (ref $f eq 'SCALAR') {
195 (
196 1 $$f,
197 $$f,
198 )
199 }
200 else {
201 0 $self->throw_exception("Unsupported INSERT RETURNING option $f");
202 }
203 };
204  
205 my $rc_ref = $options->{returning_container}
206 3 ▶ 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") 3 0 3
207  
208 3 @$rc_ref = (undef) x @f_names;
209  
210 return (
211 ( join (' ',
212 $self->_sqlcase(' returning'),
213 $f_list,
214 $self->_sqlcase('into'),
215 join (', ', ('?') x @f_names ),
216 )),
217 map {
218 3 ▶ $self->{bindtype} eq 'columns'
Branch: 1 branch
BranchTrueFalseTotal
$self->{'bindtype'} eq 'columns' ? : 5 0 5
219 ? [ $f_names[$_] => \$rc_ref->[$_] ]
220 : \$rc_ref->[$_]
221 } (0 .. $#f_names),
222 );
223 }
224  
225  
226 3 1;
227  
228 __END__
229  
230 =pod
231  
232 =encoding UTF-8
233  
234 =head1 NAME
235  
236 DBIO::Oracle::SQLMaker - Oracle-specific SQL generation for DBIO
237  
238 =head1 VERSION
239  
240 version 0.900001
241  
242 =head1 DESCRIPTION
243  
244 L<DBIO::SQLMaker> subclass for Oracle databases. Extends standard SQL
245 generation with:
246  
247 =over
248  
249 =item * Oracle C<CONNECT BY> / C<START WITH> / C<ORDER SIBLINGS BY> hierarchical
250 query support via the C<connect_by>, C<connect_by_nocycle>, C<start_with>,
251 and C<order_siblings_by> resultset attributes.
252  
253 =item * C<PRIOR> operator support in WHERE clauses.
254  
255 =item * Automatic identifier shortening to fit Oracle's 30-character limit,
256 using an MD5-based suffix (requires L<Digest::MD5>, L<Math::BigInt>,
257 L<Math::Base36>).
258  
259 =item * Oracle-style C<RETURNING ... INTO ?> syntax for insert-returning.
260  
261 =back
262  
263 Used automatically by L<DBIO::Oracle::Storage>.
264  
265 =head1 METHODS
266  
267 =head2 apply_limit
268  
269 Oracle has no native C<LIMIT> keyword. Wrap the statement in a C<ROWNUM>
270 subquery via the inherited C<_RowNum> dialect instead of the base
271 C<LIMIT ?> syntax.
272  
273 =head2 _pseudo_columns
274  
275 if ($sql_maker->_pseudo_columns->{ uc $name }) { ... }
276  
277 Returns a hashref whose keys are the Oracle hierarchical/row pseudo-columns
278 (C<LEVEL>, C<ROWNUM>, C<ROWID>, C<CONNECT_BY_ISCYCLE>, C<CONNECT_BY_ISLEAF>)
279 that must never be quoted. These are not real identifiers: quoting them yields
280 e.g. C<"LEVEL"> which Oracle rejects with C<ORA-00904: invalid identifier>.
281 C<_quote> consults this set and returns such a label verbatim.
282  
283 C<CONNECT_BY_ROOT> is a prefix operator, not a standalone identifier, so it
284 never reaches C<_quote> on its own and is intentionally absent.
285  
286 =head2 _shorten_identifier
287  
288 my $short = $sql_maker->_shorten_identifier($long_name, \@keywords);
289  
290 Shortens an identifier to fit Oracle's 30-character limit. Uses camelCase
291 compression of C<@keywords> (or the identifier itself if none supplied),
292 followed by a base-36 MD5 suffix to guarantee uniqueness.
293  
294 =head2 _insert_returning
295  
296 Generates Oracle C<RETURNING ... INTO ?> SQL for insert-returning operations.
297 The returned values are captured into scalar references in the
298 C<returning_container> option.
299  
300 =head1 SEE ALSO
301  
302 =over
303  
304 =item * L<DBIO::Oracle::Storage> - Oracle storage (uses this SQL maker)
305  
306 =item * L<DBIO::Oracle::SQLMaker::Joins> - WHERE-clause join syntax for Oracle E<lt> 9
307  
308 =item * L<DBIO::SQLMaker> - Base SQL maker class
309  
310 =back
311  
312 =head1 AUTHOR
313  
314 DBIO & DBIx::Class Authors
315  
316 =head1 COPYRIGHT AND LICENSE
317  
318 Copyright (C) 2026 DBIO Authors
319 Portions Copyright (C) 2005-2025 DBIx::Class Authors
320 Based on DBIx::Class, heavily modified.
321  
322 This is free software; you can redistribute it and/or modify it under
323 the same terms as the Perl 5 programming language system itself.
324  
325 =cut
« Oracle/Introspect/Keys.pm ↑ Summary Oracle/SQLMaker/Joins.pm »