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