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