1 package DBIx::Class::Storage::DBI::Oracle::Generic;
6 use Context::Preserve 'preserve_context';
10 __PACKAGE__->sql_limit_dialect ('RowNum');
14 DBIx::Class::Storage::DBI::Oracle::Generic - Oracle Support for DBIx::Class
18 # In your result (table) classes
19 use base 'DBIx::Class::Core';
20 __PACKAGE__->add_columns({ id => { sequence => 'mysequence', auto_nextval => 1 } });
21 __PACKAGE__->set_primary_key('id');
23 # Somewhere in your Code
24 # add some data to a table with a hierarchical relationship
25 $schema->resultset('Person')->create ({
30 firstname => 'child1',
34 firstname => 'grandchild',
40 firstname => 'child2',
46 # select from the hierarchical relationship
47 my $rs = $schema->resultset('Person')->search({},
49 'start_with' => { 'firstname' => 'foo', 'lastname' => 'bar' },
50 'connect_by' => { 'parentid' => { '-prior' => { -ident => 'personid' } },
51 'order_siblings_by' => { -asc => 'name' },
55 # this will select the whole tree starting from person "foo bar", creating
58 # me.persionid me.firstname, me.lastname, me.parentid
62 # firstname = 'foo' and lastname = 'bar'
64 # parentid = prior personid
70 This class implements base Oracle support. The subclass
71 L<DBIx::Class::Storage::DBI::Oracle::WhereJoins> is for C<(+)> joins in Oracle
78 use base qw/DBIx::Class::Storage::DBI/;
81 __PACKAGE__->sql_maker_class('DBIx::Class::SQLMaker::Oracle');
83 sub _determine_supports_insert_returning {
86 # TODO find out which version supports the RETURNING syntax
87 # 8i has it and earlier docs are a 404 on oracle.com
90 if $self->_server_info->{normalized_dbms_version} >= 8.001;
95 __PACKAGE__->_use_insert_returning_bound (1);
97 sub deployment_statements {
99 my ($schema, $type, $version, $dir, $sqltargs, @rest) = @_;
102 my $quote_char = $self->schema->storage->sql_maker->quote_char;
103 $sqltargs->{quote_table_names} = $quote_char ? 1 : 0;
104 $sqltargs->{quote_field_names} = $quote_char ? 1 : 0;
107 ! exists $sqltargs->{producer_args}{oracle_version}
109 my $dver = $self->_server_info->{dbms_version}
111 $sqltargs->{producer_args}{oracle_version} = $dver;
114 $self->next::method($schema, $type, $version, $dir, $sqltargs, @rest);
117 sub _dbh_last_insert_id {
118 my ($self, $dbh, $source, @columns) = @_;
120 foreach my $col (@columns) {
121 my $seq = ($source->column_info($col)->{sequence} ||= $self->get_autoinc_seq($source,$col));
122 my $id = $self->_sequence_fetch( 'CURRVAL', $seq );
128 sub _dbh_get_autoinc_seq {
129 my ($self, $dbh, $source, $col) = @_;
131 my $sql_maker = $self->sql_maker;
132 my ($ql, $qr) = map { $_ ? (quotemeta $_) : '' } $sql_maker->_quote_chars;
135 if ( ref $source->name eq 'SCALAR' ) {
136 $source_name = ${$source->name};
138 # the ALL_TRIGGERS match further on is case sensitive - thus uppercase
139 # stuff unless it is already quoted
140 $source_name = uc ($source_name) if $source_name !~ /\"/;
143 $source_name = $source->name;
144 $source_name = uc($source_name) unless $ql;
147 # trigger_body is a LONG
148 local $dbh->{LongReadLen} = 64 * 1024 if ($dbh->{LongReadLen} < 64 * 1024);
150 # disable default bindtype
151 local $sql_maker->{bindtype} = 'normal';
154 # look up the correct sequence automatically
155 my ( $schema, $table ) = $source_name =~ /( (?:${ql})? \w+ (?:${qr})? ) \. ( (?:${ql})? \w+ (?:${qr})? )/x;
156 my ($sql, @bind) = $sql_maker->select (
158 [qw/TRIGGER_BODY TABLE_OWNER TRIGGER_NAME/],
160 $schema ? (OWNER => $schema) : (),
161 TABLE_NAME => $table || $source_name,
162 TRIGGERING_EVENT => { -like => '%INSERT%' }, # this will also catch insert_or_update
163 TRIGGER_TYPE => { -like => '%BEFORE%' }, # we care only about 'before' triggers
168 # to find all the triggers that mention the column in question a simple
169 # regex grep since the trigger_body above is a LONG and hence not searchable
171 { my %inf; @inf{qw/body schema name/} = @$_; \%inf }
173 { $_->[0] =~ /\:new\.${ql}${col}${qr} | \:new\.$col/xi }
174 @{ $dbh->selectall_arrayref( $sql, {}, @bind ) }
178 # extract all sequence names mentioned in each trigger
180 $_->{sequences} = [ $_->{body} =~ / ( "? [\.\w\"\-]+ "? ) \. nextval /xig ];
185 # if only one trigger matched things are easy
186 if (@triggers == 1) {
188 if ( @{$triggers[0]{sequences}} == 1 ) {
189 $chosen_trigger = $triggers[0];
192 $self->throw_exception( sprintf (
193 "Unable to introspect trigger '%s' for column %s.%s (references multiple sequences). "
194 . "You need to specify the correct 'sequence' explicitly in '%s's column_info.",
202 # got more than one matching trigger - see if we can narrow it down
203 elsif (@triggers > 1) {
205 my @candidates = grep
206 { $_->{body} =~ / into \s+ \:new\.$col /xi }
210 if (@candidates == 1 && @{$candidates[0]{sequences}} == 1) {
211 $chosen_trigger = $candidates[0];
214 $self->throw_exception( sprintf (
215 "Unable to reliably select a BEFORE INSERT trigger for column %s.%s (possibilities: %s). "
216 . "You need to specify the correct 'sequence' explicitly in '%s's column_info.",
219 ( join ', ', map { "'$_->{name}'" } @triggers ),
225 if ($chosen_trigger) {
226 my $seq_name = $chosen_trigger->{sequences}[0];
228 $seq_name = "$chosen_trigger->{schema}.$seq_name"
229 unless $seq_name =~ /\./;
231 return \$seq_name if $seq_name =~ /\"/; # may already be quoted in-trigger
235 $self->throw_exception( sprintf (
236 "No suitable BEFORE INSERT triggers found for column %s.%s. "
237 . "You need to specify the correct 'sequence' explicitly in '%s's column_info.",
244 sub _sequence_fetch {
245 my ( $self, $type, $seq ) = @_;
247 # use the maker to leverage quoting settings
248 my $sql_maker = $self->sql_maker;
249 my ($id) = $self->_get_dbh->selectrow_array ($sql_maker->select('DUAL', [ ref $seq ? \"$$seq.$type" : "$seq.$type" ] ) );
256 my $dbh = $self->_dbh or return 0;
258 local $dbh->{RaiseError} = 1;
259 local $dbh->{PrintError} = 0;
262 $dbh->do('select 1 from dual');
271 my ($dbh, $op, $extra_bind, $ident, $bind_attributes, @args) = @_;
274 my $want = wantarray;
275 my $next = $self->next::can;
278 my $exec = sub { $self->$next($dbh, $op, $extra_bind, $ident, $bind_attributes, @args) };
280 if (!defined $want) {
293 if (! $tried and $_ =~ /ORA-01003/) {
294 # ORA-01003: no statement parsed (someone changed the table somehow,
295 # invalidating your cursor.)
296 my ($sql, $bind) = $self->_prep_for_execute($op, $extra_bind, $ident, \@args);
297 delete $dbh->{CachedKids}{$sql};
300 $self->throw_exception($_);
303 } while (! $tried++);
305 return wantarray ? @res : $res[0];
308 =head2 get_autoinc_seq
310 Returns the sequence name for an autoincrement column
314 sub get_autoinc_seq {
315 my ($self, $source, $col) = @_;
317 $self->dbh_do('_dbh_get_autoinc_seq', $source, $col);
320 =head2 datetime_parser_type
322 This sets the proper DateTime::Format module for use with
323 L<DBIx::Class::InflateColumn::DateTime>.
327 sub datetime_parser_type { return "DateTime::Format::Oracle"; }
329 =head2 connect_call_datetime_setup
333 on_connect_call => 'datetime_setup'
335 In L<connect_info|DBIx::Class::Storage::DBI/connect_info> to set the session nls
336 date, and timestamp values for use with L<DBIx::Class::InflateColumn::DateTime>
337 and the necessary environment variables for L<DateTime::Format::Oracle>, which
340 Maximum allowable precision is used, unless the environment variables have
343 These are the defaults used:
345 $ENV{NLS_DATE_FORMAT} ||= 'YYYY-MM-DD HH24:MI:SS';
346 $ENV{NLS_TIMESTAMP_FORMAT} ||= 'YYYY-MM-DD HH24:MI:SS.FF';
347 $ENV{NLS_TIMESTAMP_TZ_FORMAT} ||= 'YYYY-MM-DD HH24:MI:SS.FF TZHTZM';
349 To get more than second precision with L<DBIx::Class::InflateColumn::DateTime>
350 for your timestamps, use something like this:
352 use Time::HiRes 'time';
353 my $ts = DateTime->from_epoch(epoch => time);
357 sub connect_call_datetime_setup {
360 my $date_format = $ENV{NLS_DATE_FORMAT} ||= 'YYYY-MM-DD HH24:MI:SS';
361 my $timestamp_format = $ENV{NLS_TIMESTAMP_FORMAT} ||=
362 'YYYY-MM-DD HH24:MI:SS.FF';
363 my $timestamp_tz_format = $ENV{NLS_TIMESTAMP_TZ_FORMAT} ||=
364 'YYYY-MM-DD HH24:MI:SS.FF TZHTZM';
367 "alter session set nls_date_format = '$date_format'"
370 "alter session set nls_timestamp_format = '$timestamp_format'"
373 "alter session set nls_timestamp_tz_format='$timestamp_tz_format'"
377 =head2 source_bind_attributes
379 Handle LOB types in Oracle. Under a certain size (4k?), you can get away
380 with the driver assuming your input is the deprecated LONG type if you
381 encode it as a hex string. That ain't gonna fly at larger values, where
382 you'll discover you have to do what this does.
384 This method had to be overridden because we need to set ora_field to the
385 actual column, and that isn't passed to the call (provided by Storage) to
386 bind_attribute_by_data_type.
388 According to L<DBD::Oracle>, the ora_field isn't always necessary, but
389 adding it doesn't hurt, and will save your bacon if you're modifying a
390 table with more than one LOB column.
394 sub source_bind_attributes
402 foreach my $column ($source->columns) {
403 my $data_type = $source->column_info($column)->{data_type}
406 my %column_bind_attrs = $self->bind_attribute_by_data_type($data_type);
408 if ($data_type =~ /^[BC]LOB$/i) {
409 if ($DBD::Oracle::VERSION eq '1.23') {
410 $self->throw_exception(
411 "BLOB/CLOB support in DBD::Oracle == 1.23 is broken, use an earlier or later ".
412 "version.\n\nSee: https://rt.cpan.org/Public/Bug/Display.html?id=46016\n"
416 $column_bind_attrs{'ora_type'} = uc($data_type) eq 'CLOB'
417 ? DBD::Oracle::ORA_CLOB()
418 : DBD::Oracle::ORA_BLOB()
420 $column_bind_attrs{'ora_field'} = $column;
423 $bind_attributes{$column} = \%column_bind_attrs;
426 return \%bind_attributes;
430 my ($self, $name) = @_;
431 $self->_get_dbh->do("SAVEPOINT $name");
434 # Oracle automatically releases a savepoint when you start another one with the
436 sub _svp_release { 1 }
439 my ($self, $name) = @_;
440 $self->_get_dbh->do("ROLLBACK TO SAVEPOINT $name")
443 =head2 relname_to_table_alias
445 L<DBIx::Class> uses L<DBIx::Class::Relationship> names as table aliases in
448 Unfortunately, Oracle doesn't support identifiers over 30 chars in length, so
449 the L<DBIx::Class::Relationship> name is shortened and appended with half of an
452 See L<DBIx::Class::Storage/"relname_to_table_alias">.
456 sub relname_to_table_alias {
458 my ($relname, $join_count) = @_;
460 my $alias = $self->next::method(@_);
462 return $self->sql_maker->_shorten_identifier($alias, [$relname]);
465 =head2 with_deferred_fk_checks
467 Runs a coderef between:
469 alter session set constraints = deferred
471 alter session set constraints = immediate
473 to defer foreign key checks.
475 Constraints must be declared C<DEFERRABLE> for this to work.
479 sub with_deferred_fk_checks {
480 my ($self, $sub) = @_;
482 my $txn_scope_guard = $self->txn_scope_guard;
484 $self->_do_query('alter session set constraints = deferred');
486 my $sg = Scope::Guard->new(sub {
487 $self->_do_query('alter session set constraints = immediate');
491 preserve_context { $sub->() } after => sub { $txn_scope_guard->commit };
496 Following additional attributes can be used in resultsets.
498 =head2 connect_by or connect_by_nocycle
502 =item Value: \%connect_by
506 A hashref of conditions used to specify the relationship between parent rows
507 and child rows of the hierarchy.
510 connect_by => { parentid => 'prior personid' }
512 # adds a connect by statement to the query:
514 # me.persionid me.firstname, me.lastname, me.parentid
518 # parentid = prior persionid
521 connect_by_nocycle => { parentid => 'prior personid' }
523 # adds a connect by statement to the query:
525 # me.persionid me.firstname, me.lastname, me.parentid
529 # parentid = prior persionid
536 =item Value: \%condition
540 A hashref of conditions which specify the root row(s) of the hierarchy.
542 It uses the same syntax as L<DBIx::Class::ResultSet/search>
544 start_with => { firstname => 'Foo', lastname => 'Bar' }
547 # me.persionid me.firstname, me.lastname, me.parentid
551 # firstname = 'foo' and lastname = 'bar'
553 # parentid = prior persionid
555 =head2 order_siblings_by
559 =item Value: ($order_siblings_by | \@order_siblings_by)
563 Which column(s) to order the siblings by.
565 It uses the same syntax as L<DBIx::Class::ResultSet/order_by>
567 'order_siblings_by' => 'firstname ASC'
570 # me.persionid me.firstname, me.lastname, me.parentid
574 # parentid = prior persionid
580 See L<DBIx::Class/CONTRIBUTORS>.
584 You may distribute this code under the same terms as Perl itself.