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