DBIx/Class/Storage/DBI/Oracle/Generic.pm

filter: Statementstmt 16.9% 36 / 213 Branchbran 0.0% 0 / 80 Conditioncond 0.0% 0 / 42 MC/DCmcdc 0.0% 0 / 25 Subroutinesub 28.5% 10 / 35 Podpod 100% 6 / 6 totaltotal 12.9% 52 / 401 CC 63 SCAR 50.0
CC
63
Cov
11%
_prep_for_execute
420.0
_dbh_get_autoinc_seq
306.0
deployment_statements
42.0
CRAP 149.2
↑ Summary DBIx/Class/Storage/DBI/Sybase/ASE.pm »
1 package DBIx::Class::Storage::DBI::Oracle::Generic;
2  
3 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 2 ▶use base qw/DBIx::Class::Storage::DBI/;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 2 ▶use mro 'c3';
Subroutine BEGIN: called CC=1 SCAR=0.0
7 2 ▶use DBIx::Class::Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 2 ▶use Scope::Guard ();
Subroutine BEGIN: called CC=1 SCAR=0.0
9 2 ▶use Context::Preserve 'preserve_context';
Subroutine BEGIN: called CC=1 SCAR=0.0
10 2 ▶use Try::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 2 ▶use List::Util 'first';
Subroutine BEGIN: called CC=1 SCAR=0.0
12 2 ▶use namespace::clean;
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 2 __PACKAGE__->sql_limit_dialect ('RowNum');
15 2 __PACKAGE__->sql_quote_char ('"');
16 2 __PACKAGE__->sql_maker_class('DBIx::Class::SQLMaker::Oracle');
17 2 __PACKAGE__->datetime_parser_type('DateTime::Format::Oracle');
18  
19 0 ▶sub __cache_queries_with_max_lob_parts { 2 }
Subroutine __cache_queries_with_max_lob_parts: not called CC=1 SCAR=6.9
20  
21 =head1 NAME
22  
23 DBIx::Class::Storage::DBI::Oracle::Generic - Oracle Support for DBIx::Class
24  
25 =head1 SYNOPSIS
26  
27 # In your result (table) classes
28 use base 'DBIx::Class::Core';
29 __PACKAGE__->add_columns({ id => { sequence => 'mysequence', auto_nextval => 1 } });
30 __PACKAGE__->set_primary_key('id');
31  
32 # Somewhere in your Code
33 # add some data to a table with a hierarchical relationship
34 $schema->resultset('Person')->create ({
35 firstname => 'foo',
36 lastname => 'bar',
37 children => [
38 {
39 firstname => 'child1',
40 lastname => 'bar',
41 children => [
42 {
43 firstname => 'grandchild',
44 lastname => 'bar',
45 }
46 ],
47 },
48 {
49 firstname => 'child2',
50 lastname => 'bar',
51 },
52 ],
53 });
54  
55 # select from the hierarchical relationship
56 my $rs = $schema->resultset('Person')->search({},
57 {
58 'start_with' => { 'firstname' => 'foo', 'lastname' => 'bar' },
59 'connect_by' => { 'parentid' => { '-prior' => { -ident => 'personid' } },
60 'order_siblings_by' => { -asc => 'name' },
61 };
62 );
63  
64 # this will select the whole tree starting from person "foo bar", creating
65 # following query:
66 # SELECT
67 # me.persionid me.firstname, me.lastname, me.parentid
68 # FROM
69 # person me
70 # START WITH
71 # firstname = 'foo' and lastname = 'bar'
72 # CONNECT BY
73 # parentid = prior personid
74 # ORDER SIBLINGS BY
75 # firstname ASC
76  
77 =head1 DESCRIPTION
78  
79 This class implements base Oracle support. The subclass
80 L<DBIx::Class::Storage::DBI::Oracle::WhereJoins> is for C<(+)> joins in Oracle
81 versions before 9.0.
82  
83 =head1 METHODS
84  
85 =cut
86  
87 sub _determine_supports_insert_returning {
88 0 ▶ my $self = shift;
Subroutine _determine_supports_insert_returning: not called CC=2 SCAR=17.9
89  
90 # TODO find out which version supports the RETURNING syntax
91 # 8i has it and earlier docs are a 404 on oracle.com
92  
93 return 1
94 0 ▶ if $self->_server_info->{normalized_dbms_version} >= 8.001;
Branch: 1 branch
BranchTrueFalseTotal
if $self->_server_info->{"normalized_dbms_version"} >= "8.001" 0 0 0
95  
96 0 return 0;
97 }
98  
99 2 __PACKAGE__->_use_insert_returning_bound (1);
100  
101 sub deployment_statements {
102 0 ▶ my $self = shift;;
Subroutine deployment_statements: not called CC=6 SCAR=37.4
Pod documented
103 0 my ($schema, $type, $version, $dir, $sqltargs, @rest) = @_;
104  
105 0 ▶ $sqltargs ||= {};
Condition0%0/2
$sqltargs ||= {}
l!l
00
MC/DC0%0/1
$sqltargs ||= {}
$sqltargs
Truth table0%0/2
A
A= $sqltargs
Aresult
11
00
106 0 my $quote_char = $self->schema->storage->sql_maker->quote_char;
107 0 ▶ $sqltargs->{quote_table_names} = $quote_char ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$quote_char ? : 0 0 0
108 0 ▶ $sqltargs->{quote_field_names} = $quote_char ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$quote_char ? : 0 0 0
109  
110 0 ▶ if (
Branch: 1 branch
BranchTrueFalseTotal
if (not exists $sqltargs->{"producer_args"}{"oracle_version"} and my $dver = $self->_server_info->{"dbms_version"}) 0 0 0
Condition0%0/3
not exists $sqltargs->{"producer_args"}{"oracle_version"} and my $dver = $self->_server_info->{"dbms_version"}
!ll&&!rl&&r
000
MC/DC0%0/2
not exists $sqltargs->{"producer_args"}{"oracle_version"} and my $dver = $self->_server_info->{"dbms_version"}
not exists $sqltargs->{"producer_args"}{"oracle_version"} my $dver = $self->_server_info->{"dbms_version"}
Truth table0%0/3
not(A) and B
A= not exists $sqltargs->{"producer_args"}{"oracle_version"}B= my $dver = $self->_server_info->{"dbms_version"}
ABresult
0X0
100
111
111 ! exists $sqltargs->{producer_args}{oracle_version}
112 and
113 my $dver = $self->_server_info->{dbms_version}
114 ) {
115 0 $sqltargs->{producer_args}{oracle_version} = $dver;
116 }
117  
118 0 $self->next::method($schema, $type, $version, $dir, $sqltargs, @rest);
119 }
120  
121 sub _dbh_last_insert_id {
122 0 ▶ my ($self, $dbh, $source, @columns) = @_;
Subroutine _dbh_last_insert_id: not called CC=3 SCAR=24.8
123 0 my @ids = ();
124 0 foreach my $col (@columns) {
125 0 ▶ my $seq = ($source->column_info($col)->{sequence} ||= $self->get_autoinc_seq($source,$col));
Condition0%0/3
$source->column_info($col)->{"sequence"} ||= $self->get_autoinc_seq($source, $col)
l!l&&r!l&&!r
000
MC/DC0%0/2
$source->column_info($col)->{"sequence"} ||= $self->get_autoinc_seq($source, $col)
$source->column_info($col)->{"sequence"} $self->get_autoinc_seq($source, $col)
Truth table0%0/3
A ||= B
A= $source->column_info($col)->{"sequence"}B= $self->get_autoinc_seq($source, $col)
ABresult
1X1
011
000
126 0 my $id = $self->_sequence_fetch( 'CURRVAL', $seq );
127 0 push @ids, $id;
128 }
129 0 return @ids;
130 }
131  
132 sub _dbh_get_autoinc_seq {
133 0 ▶ my ($self, $dbh, $source, $col) = @_;
Subroutine _dbh_get_autoinc_seq: not called CC=17 SCAR=57.2
134  
135 0 my $sql_maker = $self->sql_maker;
136 0 ▶ my ($ql, $qr) = map { $_ ? (quotemeta $_) : '' } $sql_maker->_quote_chars;
Branch: 1 branch
BranchTrueFalseTotal
$_ ? : 0 0 0
137  
138 0 my $source_name;
139 0 ▶ if ( ref $source->name eq 'SCALAR' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $source->name eq 'SCALAR') { } 0 0 0
140 0 $source_name = ${$source->name};
141  
142 # the ALL_TRIGGERS match further on is case sensitive - thus uppercase
143 # stuff unless it is already quoted
144 0 ▶ $source_name = uc ($source_name) if $source_name !~ /\"/;
Branch: 1 branch
BranchTrueFalseTotal
unless $source_name =~ /\"/ 0 0 0
145 }
146 else {
147 0 $source_name = $source->name;
148 0 ▶ $source_name = uc($source_name) unless $ql;
Branch: 1 branch
BranchTrueFalseTotal
unless $ql 0 0 0
149 }
150  
151 # trigger_body is a LONG
152 0 ▶ local $dbh->{LongReadLen} = 64 * 1024 if ($dbh->{LongReadLen} < 64 * 1024);
Branch: 1 branch
BranchTrueFalseTotal
if $dbh->{"LongReadLen"} < 65536 0 0 0
153  
154 # disable default bindtype
155 0 local $sql_maker->{bindtype} = 'normal';
156  
157 # look up the correct sequence automatically
158 0 my ( $schema, $table ) = $source_name =~ /( (?:${ql})? \w+ (?:${qr})? ) \. ( (?:${ql})? \w+ (?:${qr})? )/x;
159  
160 # if no explicit schema was requested - use the default schema (which in the case of Oracle is the db user)
161 0 ▶ $schema ||= \'= USER';
Condition0%0/2
$schema ||= \"= USER"
l!l
00
MC/DC0%0/1
$schema ||= \"= USER"
$schema
Truth table0%0/2
A
A= $schema
Aresult
11
00
162  
163 0 ▶ my ($sql, @bind) = $sql_maker->select (
Condition0%0/3
$table || $source_name
l!l&&r!l&&!r
000
MC/DC0%0/2
$table || $source_name
$table $source_name
Truth table0%0/3
A || B
A= $tableB= $source_name
ABresult
1X1
011
000
164 'ALL_TRIGGERS',
165 [qw/TRIGGER_BODY TABLE_OWNER TRIGGER_NAME/],
166 {
167 OWNER => $schema,
168 TABLE_NAME => $table || $source_name,
169 TRIGGERING_EVENT => { -like => '%INSERT%' }, # this will also catch insert_or_update
170 TRIGGER_TYPE => { -like => '%BEFORE%' }, # we care only about 'before' triggers
171 STATUS => 'ENABLED',
172 },
173 );
174  
175 # to find all the triggers that mention the column in question a simple
176 # regex grep since the trigger_body above is a LONG and hence not searchable
177 # via -like
178 my @triggers = ( map
179 0 { my %inf; @inf{qw/body schema name/} = @$_; \%inf }
180 ( grep
181 0 { $_->[0] =~ /\:new\.${ql}${col}${qr} | \:new\.$col/xi }
182 0 @{ $dbh->selectall_arrayref( $sql, {}, @bind ) }
183 )
184 );
185  
186 # extract all sequence names mentioned in each trigger, throw away
187 # triggers without apparent sequences
188 @triggers = map {
189 0 my @seqs = $_->{body} =~ / ( [\.\w\"\-]+ ) \. nextval /xig;
190 @seqs
191 0 ▶ ? { %$_, sequences => \@seqs }
Branch: 1 branch
BranchTrueFalseTotal
@seqs ? : 0 0 0
192 : ()
193 ;
194 } @triggers;
195  
196 0 my $chosen_trigger;
197  
198 # if only one trigger matched things are easy
199 0 ▶ if (@triggers == 1) {
Branch: 2 branches
BranchTrueFalseTotal
if (@triggers == 1) { } 0 0 0
elsif (@triggers > 1) { } 0 0 0
200  
201 0 ▶ if ( @{$triggers[0]{sequences}} == 1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@{$triggers[0]{'sequences'};} == 1) { } 0 0 0
202 0 $chosen_trigger = $triggers[0];
203 }
204 else {
205 $self->throw_exception( sprintf (
206 "Unable to introspect trigger '%s' for column '%s.%s' (references multiple sequences). "
207 . "You need to specify the correct 'sequence' explicitly in '%s's column_info.",
208 $triggers[0]{name},
209 0 $source_name,
210 $col,
211 $col,
212 ) );
213 }
214 }
215 # got more than one matching trigger - see if we can narrow it down
216 elsif (@triggers > 1) {
217  
218 my @candidates = grep
219 0 { $_->{body} =~ / into \s+ \:new\.$col /xi }
220 @triggers
221 ;
222  
223 0 ▶ if (@candidates == 1 && @{$candidates[0]{sequences}} == 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (@candidates == 1 and @{$candidates[0]{'sequences'};} == 1) { } 0 0 0
Condition0%0/3
@candidates == 1 and @{$candidates[0]{"sequences"};} == 1
!ll&&!rl&&r
000
MC/DC0%0/2
@candidates == 1 and @{$candidates[0]{"sequences"};} == 1
@candidates == 1 @{$candidates[0]{"sequences"};} == 1
Truth table0%0/3
A and B
A= @candidates == 1B= @{$candidates[0]{"sequences"};} == 1
ABresult
0X0
100
111
224 0 $chosen_trigger = $candidates[0];
225 }
226 else {
227 $self->throw_exception( sprintf (
228 "Unable to reliably select a BEFORE INSERT trigger for column '%s.%s' (possibilities: %s). "
229 . "You need to specify the correct 'sequence' explicitly in '%s's column_info.",
230 $source_name,
231 $col,
232 0 ( join ', ', map { "'$_->{name}'" } @triggers ),
233 $col,
234 ) );
235 }
236 }
237  
238 0 ▶ if ($chosen_trigger) {
Branch: 1 branch
BranchTrueFalseTotal
if ($chosen_trigger) 0 0 0
239 0 my $seq_name = $chosen_trigger->{sequences}[0];
240  
241 0 ▶ $seq_name = "$chosen_trigger->{schema}.$seq_name"
Branch: 1 branch
BranchTrueFalseTotal
unless $seq_name =~ /\./ 0 0 0
242 unless $seq_name =~ /\./;
243  
244 0 ▶ return \$seq_name if $seq_name =~ /\"/; # may already be quoted in-trigger
Branch: 1 branch
BranchTrueFalseTotal
if $seq_name =~ /\"/ 0 0 0
245 0 return $seq_name;
246 }
247  
248 0 $self->throw_exception( sprintf (
249 "No suitable BEFORE INSERT triggers found for column '%s.%s'. "
250 . "You need to specify the correct 'sequence' explicitly in '%s's column_info.",
251 $source_name,
252 $col,
253 $col,
254 ));
255 }
256  
257 sub _sequence_fetch {
258 0 ▶ my ( $self, $type, $seq ) = @_;
Subroutine _sequence_fetch: not called CC=2 SCAR=17.9
259  
260 # use the maker to leverage quoting settings
261 0 ▶ my $sth = $self->_dbh->prepare_cached(
Branch: 1 branch
BranchTrueFalseTotal
ref $seq ? : 0 0 0
262 $self->sql_maker->select('DUAL', [ ref $seq ? \"$$seq.$type" : "$seq.$type" ] )
263 );
264 0 $sth->execute;
265 0 my ($id) = $sth->fetchrow_array;
266 0 $sth->finish;
267 0 return $id;
268 }
269  
270 sub _ping {
271 0 ▶ my $self = shift;
Subroutine _ping: not called CC=2 SCAR=17.9
272  
273 0 ▶ my $dbh = $self->_dbh or return 0;
Branch: 1 branch
BranchTrueFalseTotal
my $dbh = $self->_dbh or return 0 0 0 0
274  
275 0 local $dbh->{RaiseError} = 1;
276 0 local $dbh->{PrintError} = 0;
277  
278 return try {
279 0 ▶ $dbh->do('select 1 from dual');
Subroutine __ANON__: not called CC=1 SCAR=6.9
280 0 1;
281 } catch {
282 0 ▶ 0;
Subroutine __ANON__: not called CC=1 SCAR=6.9
283 0 };
284 }
285  
286 sub _dbh_execute {
287 #my ($self, $dbh, $sql, $bind, $bind_attrs) = @_;
288 0 ▶ my ($self, $sql, $bind) = @_[0,2,3];
Subroutine _dbh_execute: not called CC=3 SCAR=24.8
289  
290 # Turn off sth caching for multi-part LOBs. See _prep_for_execute below
291 local $self->{disable_sth_caching} = 1 if first {
292 0 ▶ ($_->[0]{_ora_lob_autosplit_part}||0)
Subroutine __ANON__: not called CC=2 SCAR=17.9
Condition0%0/2
$_->[0]{'_ora_lob_autosplit_part'} || 0
l!l
00
MC/DC0%0/1
$_->[0]{'_ora_lob_autosplit_part'} || 0
$_->[0]{'_ora_lob_autosplit_part'}
Truth table0%0/2
A
A= $_->[0]{'_ora_lob_autosplit_part'}
Aresult
11
00
293 >
294 (__cache_queries_with_max_lob_parts - 1)
295 0 ▶ } @$bind;
Branch: 1 branch
BranchTrueFalseTotal
if &first(sub { ($_->[0]{"_ora_lob_autosplit_part"} || 0) > __cache_queries_with_max_lob_parts(-1); } , @$bind) 0 0 0
296  
297 0 my $next = $self->next::can;
298  
299 # if we are already in a txn we can't retry anything
300 0 ▶ return shift->$next(@_)
Branch: 1 branch
BranchTrueFalseTotal
if $self->transaction_depth 0 0 0
301 if $self->transaction_depth;
302  
303 # cheat the blockrunner we are just about to create
304 # we do want to rerun things regardless of outer state
305 0 local $self->{_in_do_block};
306  
307 return DBIx::Class::Storage::BlockRunner->new(
308 storage => $self,
309 wrap_txn => 0,
310 retry_handler => sub {
311 # ORA-01003: no statement parsed (someone changed the table somehow,
312 # invalidating your cursor.)
313 0 ▶ if (
Subroutine __ANON__: not called CC=4 SCAR=30.0
Branch: 1 branch
BranchTrueFalseTotal
if ($_[0]->failed_attempt_count == 1 and $_[0]->last_exception =~ /ORA-01003/ and my $dbh = $_[0]->storage->_dbh) { } 0 0 0
Condition0%0/6
$_[0]->failed_attempt_count == 1 and $_[0]->last_exception =~ /ORA-01003/ and my $dbh = $_[0]->storage->_dbh
!ll&&!rl&&r
000
$_[0]->failed_attempt_count == 1 and $_[0]->last_exception =~ /ORA-01003/
!ll&&!rl&&r
000
MC/DC0%0/3
$_[0]->failed_attempt_count == 1 and $_[0]->last_exception =~ /ORA-01003/ and my $dbh = $_[0]->storage->_dbh
$_[0]->failed_attempt_count == 1 $_[0]->last_exception =~ /ORA-01003/ my $dbh = $_[0]->storage->_dbh
Truth table0%0/4
A and B and C
A= $_[0]->failed_attempt_count == 1B= $_[0]->last_exception =~ /ORA-01003/C= my $dbh = $_[0]->storage->_dbh
ABCresult
0XX0
10X0
1100
1111
314 $_[0]->failed_attempt_count == 1
315 and
316 $_[0]->last_exception =~ /ORA-01003/
317 and
318 my $dbh = $_[0]->storage->_dbh
319 ) {
320 0 delete $dbh->{CachedKids}{$sql};
321 0 return 1;
322 }
323 else {
324 0 return 0;
325 }
326 },
327 0 )->run( $next, @_ );
328 }
329  
330 sub _dbh_execute_for_fetch {
331 #my ($self, $sth, $tuple_status, @extra) = @_;
332  
333 # DBD::Oracle warns loudly on partial execute_for_fetch failures
334 0 ▶ local $_[1]->{PrintWarn} = 0;
Subroutine _dbh_execute_for_fetch: not called CC=1 SCAR=6.9
335  
336 0 shift->next::method(@_);
337 }
338  
339 =head2 get_autoinc_seq
340  
341 Returns the sequence name for an autoincrement column
342  
343 =cut
344  
345 sub get_autoinc_seq {
346 0 ▶ my ($self, $source, $col) = @_;
Subroutine get_autoinc_seq: not called CC=1 SCAR=6.9
Pod documented
347  
348 0 $self->dbh_do('_dbh_get_autoinc_seq', $source, $col);
349 }
350  
351 =head2 datetime_parser_type
352  
353 This sets the proper DateTime::Format module for use with
354 L<DBIx::Class::InflateColumn::DateTime>.
355  
356 =head2 connect_call_datetime_setup
357  
358 Used as:
359  
360 on_connect_call => 'datetime_setup'
361  
362 In L<connect_info|DBIx::Class::Storage::DBI/connect_info> to set the session nls
363 date, and timestamp values for use with L<DBIx::Class::InflateColumn::DateTime>
364 and the necessary environment variables for L<DateTime::Format::Oracle>, which
365 is used by it.
366  
367 Maximum allowable precision is used, unless the environment variables have
368 already been set.
369  
370 These are the defaults used:
371  
372 $ENV{NLS_DATE_FORMAT} ||= 'YYYY-MM-DD HH24:MI:SS';
373 $ENV{NLS_TIMESTAMP_FORMAT} ||= 'YYYY-MM-DD HH24:MI:SS.FF';
374 $ENV{NLS_TIMESTAMP_TZ_FORMAT} ||= 'YYYY-MM-DD HH24:MI:SS.FF TZHTZM';
375  
376 To get more than second precision with L<DBIx::Class::InflateColumn::DateTime>
377 for your timestamps, use something like this:
378  
379 use Time::HiRes 'time';
380 my $ts = DateTime->from_epoch(epoch => time);
381  
382 =cut
383  
384 sub connect_call_datetime_setup {
385 0 ▶ my $self = shift;
Subroutine connect_call_datetime_setup: not called CC=4 SCAR=30.0
Pod documented
386  
387 0 ▶ my $date_format = $ENV{NLS_DATE_FORMAT} ||= 'YYYY-MM-DD HH24:MI:SS';
Condition0%0/2
$ENV{"NLS_DATE_FORMAT"} ||= "YYYY-MM-DD HH24:MI:SS"
l!l
00
MC/DC0%0/1
$ENV{"NLS_DATE_FORMAT"} ||= "YYYY-MM-DD HH24:MI:SS"
$ENV{"NLS_DATE_FORMAT"}
Truth table0%0/2
A
A= $ENV{"NLS_DATE_FORMAT"}
Aresult
11
00
388 my $timestamp_format = $ENV{NLS_TIMESTAMP_FORMAT} ||=
389 0 ▶ 'YYYY-MM-DD HH24:MI:SS.FF';
Condition0%0/2
$ENV{"NLS_TIMESTAMP_FORMAT"} ||= "YYYY-MM-DD HH24:MI:SS.FF"
l!l
00
MC/DC0%0/1
$ENV{"NLS_TIMESTAMP_FORMAT"} ||= "YYYY-MM-DD HH24:MI:SS.FF"
$ENV{"NLS_TIMESTAMP_FORMAT"}
Truth table0%0/2
A
A= $ENV{"NLS_TIMESTAMP_FORMAT"}
Aresult
11
00
390 my $timestamp_tz_format = $ENV{NLS_TIMESTAMP_TZ_FORMAT} ||=
391 0 ▶ 'YYYY-MM-DD HH24:MI:SS.FF TZHTZM';
Condition0%0/2
$ENV{"NLS_TIMESTAMP_TZ_FORMAT"} ||= "YYYY-MM-DD HH24:MI:SS.FF TZHTZM"
l!l
00
MC/DC0%0/1
$ENV{"NLS_TIMESTAMP_TZ_FORMAT"} ||= "YYYY-MM-DD HH24:MI:SS.FF TZHTZM"
$ENV{"NLS_TIMESTAMP_TZ_FORMAT"}
Truth table0%0/2
A
A= $ENV{"NLS_TIMESTAMP_TZ_FORMAT"}
Aresult
11
00
392  
393 0 $self->_do_query(
394 "alter session set nls_date_format = '$date_format'"
395 );
396 0 $self->_do_query(
397 "alter session set nls_timestamp_format = '$timestamp_format'"
398 );
399 0 $self->_do_query(
400 "alter session set nls_timestamp_tz_format='$timestamp_tz_format'"
401 );
402 }
403  
404 ### Note originally by Ron "Quinn" Straight <quinnfazigu@gmail.org>
405 ### http://git.shadowcat.co.uk/gitweb/gitweb.cgi?p=dbsrgits/DBIx-Class.git;a=commitdiff;h=5db2758de644d53e07cd3e05f0e9037bf40116fc
406 #
407 # Handle LOB types in Oracle. Under a certain size (4k?), you can get away
408 # with the driver assuming your input is the deprecated LONG type if you
409 # encode it as a hex string. That ain't gonna fly at larger values, where
410 # you'll discover you have to do what this does.
411 #
412 # This method had to be overridden because we need to set ora_field to the
413 # actual column, and that isn't passed to the call (provided by Storage) to
414 # bind_attribute_by_data_type.
415 #
416 # According to L<DBD::Oracle>, the ora_field isn't always necessary, but
417 # adding it doesn't hurt, and will save your bacon if you're modifying a
418 # table with more than one LOB column.
419 #
420 sub _dbi_attrs_for_bind {
421 0 ▶ my ($self, $ident, $bind) = @_;
Subroutine _dbi_attrs_for_bind: not called CC=5 SCAR=34.0
422  
423 0 my $attrs = $self->next::method($ident, $bind);
424  
425 0 for my $i (0 .. $#$attrs) {
426 0 ▶ if (keys %{$attrs->[$i]||{}} and my $col = $bind->[$i][0]{dbic_colname}) {
Branch: 2 branches
BranchTrueFalseTotal
if (keys %{$attrs->[$i] or {};} and my $col = $bind->[$i][0]{"dbic_colname"}) 0 0 0
$attrs->[$i] or {} 0 0 0
Condition0%0/3
keys %{$attrs->[$i] or {};} and my $col = $bind->[$i][0]{"dbic_colname"}
!ll&&!rl&&r
000
MC/DC0%0/2
keys %{$attrs->[$i] or {};} and my $col = $bind->[$i][0]{"dbic_colname"}
keys %{$attrs->[$i] or {};} my $col = $bind->[$i][0]{"dbic_colname"}
Truth table0%0/3
A and B
A= keys %{$attrs->[$i] or {};}B= my $col = $bind->[$i][0]{"dbic_colname"}
ABresult
0X0
100
111
427 0 $attrs->[$i]{ora_field} = $col;
428 }
429 }
430  
431 0 $attrs;
432 }
433  
434 sub bind_attribute_by_data_type {
435 0 ▶ my ($self, $dt) = @_;
Subroutine bind_attribute_by_data_type: not called CC=5 SCAR=34.0
Pod documented
436  
437 0 ▶ if ($self->_is_lob_type($dt)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_is_lob_type($dt)) { } 0 0 0
438  
439 # this is a hot-ish codepath, store an escape-flag in the DBD namespace, so that
440 # things like Class::Unload work (unlikely but possible)
441 0 ▶ unless ($DBD::Oracle::__DBIC_DBD_VERSION_CHECK_OK__) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($DBD::Oracle::__DBIC_DBD_VERSION_CHECK_OK__) 0 0 0
442  
443 # no earlier - no later
444 0 ▶ if ($DBD::Oracle::VERSION eq '1.23') {
Branch: 1 branch
BranchTrueFalseTotal
if ($DBD::Oracle::VERSION eq "1.23") 0 0 0
445 0 $self->throw_exception(
446 "BLOB/CLOB support in DBD::Oracle == 1.23 is broken, use an earlier or later ".
447 "version (https://rt.cpan.org/Public/Bug/Display.html?id=46016)"
448 );
449 }
450  
451 0 $DBD::Oracle::__DBIC_DBD_VERSION_CHECK_OK__ = 1;
452 }
453  
454 return {
455 0 ▶ ora_type => $self->_is_text_lob_type($dt)
Branch: 1 branch
BranchTrueFalseTotal
$self->_is_text_lob_type($dt) ? : 0 0 0
456 ? DBD::Oracle::ORA_CLOB()
457 : DBD::Oracle::ORA_BLOB()
458 };
459 }
460 else {
461 0 return undef;
462 }
463 }
464  
465 # Handle blob columns in WHERE.
466 #
467 # For equality comparisons:
468 #
469 # We split data intended for comparing to a LOB into 2000 character chunks and
470 # compare them using dbms_lob.substr on the LOB column.
471 #
472 # We turn off DBD::Oracle LOB binds for these partial LOB comparisons by passing
473 # dbd_attrs => undef, because these are regular varchar2 comparisons and
474 # otherwise the query will fail.
475 #
476 # Since the most common comparison size is likely to be under 4000 characters
477 # (TEXT comparisons previously deployed to other RDBMSes) we disable
478 # prepare_cached for queries with more than two part comparisons to a LOB
479 # column. This is done in _dbh_execute (above) which was previously overridden
480 # to gracefully recover from an Oracle error. This is to be careful to not
481 # exhaust your application's open cursor limit.
482 #
483 # See:
484 # http://itcareershift.com/blog1/2011/02/21/oracle-max-number-of-open-cursors-complete-reference-for-the-new-oracle-dba/
485 # on the open_cursor limit.
486 #
487 # For everything else:
488 #
489 # We assume that everything that is not a LOB comparison, will most likely be a
490 # LIKE query or some sort of function invocation. This may prove to be a naive
491 # assumption in the future, but for now it should cover the two most likely
492 # things users would want to do with a BLOB or CLOB, an equality test or a LIKE
493 # query (on a CLOB.)
494 #
495 # For these expressions, the bind must NOT have the attributes of a LOB bind for
496 # DBD::Oracle, otherwise the query will fail. This is done by passing
497 # dbd_attrs => undef.
498  
499 sub _prep_for_execute {
500 0 ▶ my $self = shift;
Subroutine _prep_for_execute: not called CC=20 SCAR=60.4
501 0 my ($op) = @_;
502  
503 0 ▶ return $self->next::method(@_)
Branch: 1 branch
BranchTrueFalseTotal
if $op eq "insert" 0 0 0
504 if $op eq 'insert';
505  
506 0 my ($sql, $bind) = $self->next::method(@_);
507  
508 my $lob_bind_indices = { map {
509 0 (
510 $bind->[$_][0]{sqlt_datatype}
511 and
512 $self->_is_lob_type($bind->[$_][0]{sqlt_datatype})
513 0 ▶ ) ? ( $_ => 1 ) : ()
Branch: 1 branch
BranchTrueFalseTotal
$bind->[$_][0]{'sqlt_datatype'} && $self->_is_lob_type($bind->[$_][0]{'sqlt_datatype'}) ? : 0 0 0
Condition0%0/3
$bind->[$_][0]{"sqlt_datatype"} and $self->_is_lob_type($bind->[$_][0]{"sqlt_datatype"})
!ll&&!rl&&r
000
MC/DC0%0/2
$bind->[$_][0]{"sqlt_datatype"} and $self->_is_lob_type($bind->[$_][0]{"sqlt_datatype"})
$bind->[$_][0]{"sqlt_datatype"} $self->_is_lob_type($bind->[$_][0]{"sqlt_datatype"})
Truth table0%0/3
A and B
A= $bind->[$_][0]{"sqlt_datatype"}B= $self->_is_lob_type($bind->[$_][0]{"sqlt_datatype"})
ABresult
0X0
100
111
514 } ( 0 .. $#$bind ) };
515  
516 0 ▶ return ($sql, $bind) unless %$lob_bind_indices;
Branch: 1 branch
BranchTrueFalseTotal
unless %$lob_bind_indices 0 0 0
517  
518 0 my ($final_sql, @final_binds);
519 0 ▶ if ($op eq 'update') {
Branch: 2 branches
BranchTrueFalseTotal
if ($op eq 'update') { } 0 0 0
elsif ($op ne 'select' and $op ne 'delete') { } 0 0 0
520 0 ▶ $self->throw_exception('Update with complex WHERE clauses involving BLOB columns currently not supported')
Branch: 1 branch
BranchTrueFalseTotal
if $sql =~ /\bWHERE\b .+ \bWHERE\b/sx 0 0 0
521 if $sql =~ /\bWHERE\b .+ \bWHERE\b/xs;
522  
523 0 my $where_sql;
524 0 ($final_sql, $where_sql) = $sql =~ /^ (.+?) ( \bWHERE\b .+) /xs;
525  
526 0 ▶ if (my $set_bind_count = $final_sql =~ y/?//) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $set_bind_count = $final_sql =~ tr/?//) 0 0 0
527  
528 0 delete $lob_bind_indices->{$_} for (0 .. ($set_bind_count - 1));
529  
530 # bail if only the update part contains blobs
531 0 ▶ return ($sql, $bind) unless %$lob_bind_indices;
Branch: 1 branch
BranchTrueFalseTotal
unless %$lob_bind_indices 0 0 0
532  
533 0 @final_binds = splice @$bind, 0, $set_bind_count;
534 $lob_bind_indices = { map
535 0 { $_ - $set_bind_count => $lob_bind_indices->{$_} }
536 keys %$lob_bind_indices
537 };
538 }
539  
540 # if we got that far - assume the where SQL is all we got
541 # (the first part is already shoved into $final_sql)
542 0 $sql = $where_sql;
543 }
544 ▶ elsif ($op ne 'select' and $op ne 'delete') {
Condition0%0/3
$op ne "select" and $op ne "delete"
!ll&&!rl&&r
000
MC/DC0%0/2
$op ne "select" and $op ne "delete"
$op ne "select" $op ne "delete"
Truth table0%0/3
A and B
A= $op ne "select"B= $op ne "delete"
ABresult
0X0
100
111
545 0 $self->throw_exception("Unsupported \$op: $op");
546 }
547  
548 0 my @sql_parts = split /\?/, $sql;
549  
550 0 my $col_equality_re = qr/ (?<=\s) ([\w."]+) (\s*=\s*) $/x;
551  
552 0 for my $b_idx (0 .. $#$bind) {
553 0 my $bound = $bind->[$b_idx];
554  
555 0 ▶ if (
Branch: 1 branch
BranchTrueFalseTotal
if ($lob_bind_indices->{$b_idx} and () = $sql_parts[0] =~ /$col_equality_re/) { } 0 0 0
Condition0%0/3
$lob_bind_indices->{$b_idx} and () = $sql_parts[0] =~ /$col_equality_re/
!ll&&!rl&&r
000
MC/DC0%0/2
$lob_bind_indices->{$b_idx} and () = $sql_parts[0] =~ /$col_equality_re/
$lob_bind_indices->{$b_idx} () = $sql_parts[0] =~ /$col_equality_re/
Truth table0%0/3
A and B
A= $lob_bind_indices->{$b_idx}B= () = $sql_parts[0] =~ /$col_equality_re/
ABresult
0X0
100
111
556 $lob_bind_indices->{$b_idx}
557 and
558 my ($col, $eq) = $sql_parts[0] =~ $col_equality_re
559 ) {
560 0 my $data = $bound->[1];
561  
562 0 ▶ $data = "$data" if ref $data;
Branch: 1 branch
BranchTrueFalseTotal
if ref $data 0 0 0
563  
564 0 my @parts = unpack '(a2000)*', $data;
565  
566 0 my @sql_frag;
567  
568 0 for my $idx (0..$#parts) {
569 0 push @sql_frag, sprintf (
570 'UTL_RAW.CAST_TO_VARCHAR2(RAWTOHEX(DBMS_LOB.SUBSTR(%s, 2000, %d))) = ?',
571 $col, ($idx*2000 + 1),
572 );
573 }
574  
575 0 my $sql_frag = '( ' . (join ' AND ', @sql_frag) . ' )';
576  
577 0 $sql_parts[0] =~ s/$col_equality_re/$sql_frag/;
578  
579 0 $final_sql .= shift @sql_parts;
580  
581 0 for my $idx (0..$#parts) {
582 push @final_binds, [
583 {
584 0 %{ $bound->[0] },
585 _ora_lob_autosplit_part => $idx,
586 dbd_attrs => undef,
587 },
588 $parts[$idx]
589 ];
590 }
591 }
592 else {
593 0 $final_sql .= shift(@sql_parts) . '?';
594 push @final_binds, $lob_bind_indices->{$b_idx}
595 ? [
596 {
597 0 ▶ %{ $bound->[0] },
Branch: 1 branch
BranchTrueFalseTotal
$lob_bind_indices->{$b_idx} ? : 0 0 0
598 dbd_attrs => undef,
599 },
600 $bound->[1],
601 ] : $bound
602 ;
603 }
604 }
605  
606 0 ▶ if (@sql_parts > 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (@sql_parts > 1) 0 0 0
607 0 carp "There are more placeholders than binds, this should not happen!";
608 0 @sql_parts = join ('?', @sql_parts);
609 }
610  
611 0 $final_sql .= $sql_parts[0];
612  
613 0 return ($final_sql, \@final_binds);
614 }
615  
616 # Savepoints stuff.
617  
618 sub _exec_svp_begin {
619 0 ▶ my ($self, $name) = @_;
Subroutine _exec_svp_begin: not called CC=1 SCAR=6.9
620 0 $self->_dbh->do("SAVEPOINT $name");
621 }
622  
623 # Oracle automatically releases a savepoint when you start another one with the
624 # same name.
625 0 ▶sub _exec_svp_release { 1 }
Subroutine _exec_svp_release: not called CC=1 SCAR=6.9
626  
627 sub _exec_svp_rollback {
628 0 ▶ my ($self, $name) = @_;
Subroutine _exec_svp_rollback: not called CC=1 SCAR=6.9
629 0 $self->_dbh->do("ROLLBACK TO SAVEPOINT $name")
630 }
631  
632 =head2 relname_to_table_alias
633  
634 L<DBIx::Class> uses L<DBIx::Class::Relationship> names as table aliases in
635 queries.
636  
637 Unfortunately, Oracle doesn't support identifiers over 30 chars in length, so
638 the L<DBIx::Class::Relationship> name is shortened and appended with half of an
639 MD5 hash.
640  
641 See L<DBIx::Class::Storage/"relname_to_table_alias">.
642  
643 =cut
644  
645 sub relname_to_table_alias {
646 0 ▶ my $self = shift;
Subroutine relname_to_table_alias: not called CC=1 SCAR=6.9
Pod documented
647 0 my ($relname, $join_count) = @_;
648  
649 0 my $alias = $self->next::method(@_);
650  
651 # we need to shorten here in addition to the shortening in SQLA itself,
652 # since the final relnames are crucial for the join optimizer
653 0 return $self->sql_maker->_shorten_identifier($alias);
654 }
655  
656 =head2 with_deferred_fk_checks
657  
658 Runs a coderef between:
659  
660 alter session set constraints = deferred
661 ...
662 alter session set constraints = immediate
663  
664 to defer foreign key checks.
665  
666 Constraints must be declared C<DEFERRABLE> for this to work.
667  
668 =cut
669  
670 sub with_deferred_fk_checks {
671 0 ▶ my ($self, $sub) = @_;
Subroutine with_deferred_fk_checks: not called CC=1 SCAR=6.9
Pod documented
672  
673 0 my $txn_scope_guard = $self->txn_scope_guard;
674  
675 0 $self->_do_query('alter session set constraints = deferred');
676  
677 my $sg = Scope::Guard->new(sub {
678 0 ▶ $self->_do_query('alter session set constraints = immediate');
Subroutine __ANON__: not called CC=1 SCAR=6.9
679 0 });
680  
681 return
682 0 ▶ preserve_context { $sub->() } after => sub { $txn_scope_guard->commit };
Subroutine __ANON__: not called CC=1 SCAR=6.9
683 }
684  
685 =head1 ATTRIBUTES
686  
687 Following additional attributes can be used in resultsets.
688  
689 =head2 connect_by or connect_by_nocycle
690  
691 =over 4
692  
693 =item Value: \%connect_by
694  
695 =back
696  
697 A hashref of conditions used to specify the relationship between parent rows
698 and child rows of the hierarchy.
699  
700  
701 connect_by => { parentid => 'prior personid' }
702  
703 # adds a connect by statement to the query:
704 # SELECT
705 # me.persionid me.firstname, me.lastname, me.parentid
706 # FROM
707 # person me
708 # CONNECT BY
709 # parentid = prior persionid
710  
711  
712 connect_by_nocycle => { parentid => 'prior personid' }
713  
714 # adds a connect by statement to the query:
715 # SELECT
716 # me.persionid me.firstname, me.lastname, me.parentid
717 # FROM
718 # person me
719 # CONNECT BY NOCYCLE
720 # parentid = prior persionid
721  
722  
723 =head2 start_with
724  
725 =over 4
726  
727 =item Value: \%condition
728  
729 =back
730  
731 A hashref of conditions which specify the root row(s) of the hierarchy.
732  
733 It uses the same syntax as L<DBIx::Class::ResultSet/search>
734  
735 start_with => { firstname => 'Foo', lastname => 'Bar' }
736  
737 # SELECT
738 # me.persionid me.firstname, me.lastname, me.parentid
739 # FROM
740 # person me
741 # START WITH
742 # firstname = 'foo' and lastname = 'bar'
743 # CONNECT BY
744 # parentid = prior persionid
745  
746 =head2 order_siblings_by
747  
748 =over 4
749  
750 =item Value: ($order_siblings_by | \@order_siblings_by)
751  
752 =back
753  
754 Which column(s) to order the siblings by.
755  
756 It uses the same syntax as L<DBIx::Class::ResultSet/order_by>
757  
758 'order_siblings_by' => 'firstname ASC'
759  
760 # SELECT
761 # me.persionid me.firstname, me.lastname, me.parentid
762 # FROM
763 # person me
764 # CONNECT BY
765 # parentid = prior persionid
766 # ORDER SIBLINGS BY
767 # firstname ASC
768  
769 =head1 AUTHOR
770  
771 See L<DBIx::Class/AUTHOR> and L<DBIx::Class/CONTRIBUTORS>.
772  
773 =head1 LICENSE
774  
775 You may distribute this code under the same terms as Perl itself.
776  
777 =cut
778  
779 2 1;
780 # vim:sts=2 sw=2:
↑ Summary DBIx/Class/Storage/DBI/Sybase/ASE.pm »