1 package DBIx::Class::Storage::DBI::Oracle::Generic;
6 use Context::Preserve ();
11 DBIx::Class::Storage::DBI::Oracle::Generic - Oracle Support for DBIx::Class
15 # In your result (table) classes
16 use base 'DBIx::Class::Core';
17 __PACKAGE__->add_columns({ id => { sequence => 'mysequence', auto_nextval => 1 } });
18 __PACKAGE__->set_primary_key('id');
19 __PACKAGE__->sequence('mysequence');
23 This class implements base Oracle support. The subclass
24 L<DBIx::Class::Storage::DBI::Oracle::WhereJoins> is for C<(+)> joins in Oracle
31 use base qw/DBIx::Class::Storage::DBI/;
34 sub deployment_statements {
36 my ($schema, $type, $version, $dir, $sqltargs, @rest) = @_;
39 my $quote_char = $self->schema->storage->sql_maker->quote_char;
40 $sqltargs->{quote_table_names} = $quote_char ? 1 : 0;
41 $sqltargs->{quote_field_names} = $quote_char ? 1 : 0;
43 my $oracle_version = try { $self->_get_dbh->get_info(18) };
45 $sqltargs->{producer_args}{oracle_version} = $oracle_version;
47 $self->next::method($schema, $type, $version, $dir, $sqltargs, @rest);
50 sub _dbh_last_insert_id {
51 my ($self, $dbh, $source, @columns) = @_;
53 foreach my $col (@columns) {
54 my $seq = ($source->column_info($col)->{sequence} ||= $self->get_autoinc_seq($source,$col));
55 my $id = $self->_sequence_fetch( 'currval', $seq );
61 sub _dbh_get_autoinc_seq {
62 my ($self, $dbh, $source, $col) = @_;
64 my $sql_maker = $self->sql_maker;
67 if ( ref $source->name eq 'SCALAR' ) {
68 $source_name = ${$source->name};
71 $source_name = $source->name;
73 $source_name = uc($source_name) unless $sql_maker->quote_char;
75 # trigger_body is a LONG
76 local $dbh->{LongReadLen} = 64 * 1024 if ($dbh->{LongReadLen} < 64 * 1024);
78 # disable default bindtype
79 local $sql_maker->{bindtype} = 'normal';
81 # look up the correct sequence automatically
82 my ( $schema, $table ) = $source_name =~ /(\w+)\.(\w+)/;
83 my ($sql, @bind) = $sql_maker->select (
87 $schema ? (owner => $schema) : (),
88 table_name => $table || $source_name,
89 triggering_event => { -like => '%INSERT%' },
93 my $sth = $dbh->prepare($sql);
94 $sth->execute (@bind);
96 while (my ($insert_trigger) = $sth->fetchrow_array) {
97 return $1 if $insert_trigger =~ m!("?\w+"?)\.nextval!i; # col name goes here???
99 $self->throw_exception("Unable to find a sequence INSERT trigger on table '$source_name'.");
102 sub _sequence_fetch {
103 my ( $self, $type, $seq ) = @_;
104 my ($id) = $self->_get_dbh->selectrow_array("SELECT ${seq}.${type} FROM DUAL");
111 my $dbh = $self->_dbh or return 0;
113 local $dbh->{RaiseError} = 1;
114 local $dbh->{PrintError} = 0;
117 $dbh->do('select 1 from dual');
126 my ($dbh, $op, $extra_bind, $ident, $bind_attributes, @args) = @_;
128 my $wantarray = wantarray;
130 my $next = $self->next::can;
134 @res = $self->$next($dbh, $op, $extra_bind, $ident, $bind_attributes, @args);
137 $res[0] = $self->$next($dbh, $op, $extra_bind, $ident, $bind_attributes, @args);
143 # ORA-01003: no statement parsed (someone changed the table somehow,
144 # invalidating your cursor.)
145 my ($sql, $bind) = $self->_prep_for_execute($op, $extra_bind, $ident, \@args);
146 delete $dbh->{CachedKids}{$sql};
149 $self->throw_exception($_);
152 } while (not $retried++);
154 return $wantarray ? @res : $res[0];
157 =head2 get_autoinc_seq
159 Returns the sequence name for an autoincrement column
163 sub get_autoinc_seq {
164 my ($self, $source, $col) = @_;
166 $self->dbh_do('_dbh_get_autoinc_seq', $source, $col);
169 =head2 datetime_parser_type
171 This sets the proper DateTime::Format module for use with
172 L<DBIx::Class::InflateColumn::DateTime>.
176 sub datetime_parser_type { return "DateTime::Format::Oracle"; }
178 =head2 connect_call_datetime_setup
182 on_connect_call => 'datetime_setup'
184 In L<connect_info|DBIx::Class::Storage::DBI/connect_info> to set the session nls
185 date, and timestamp values for use with L<DBIx::Class::InflateColumn::DateTime>
186 and the necessary environment variables for L<DateTime::Format::Oracle>, which
189 Maximum allowable precision is used, unless the environment variables have
192 These are the defaults used:
194 $ENV{NLS_DATE_FORMAT} ||= 'YYYY-MM-DD HH24:MI:SS';
195 $ENV{NLS_TIMESTAMP_FORMAT} ||= 'YYYY-MM-DD HH24:MI:SS.FF';
196 $ENV{NLS_TIMESTAMP_TZ_FORMAT} ||= 'YYYY-MM-DD HH24:MI:SS.FF TZHTZM';
198 To get more than second precision with L<DBIx::Class::InflateColumn::DateTime>
199 for your timestamps, use something like this:
201 use Time::HiRes 'time';
202 my $ts = DateTime->from_epoch(epoch => time);
206 sub connect_call_datetime_setup {
209 my $date_format = $ENV{NLS_DATE_FORMAT} ||= 'YYYY-MM-DD HH24:MI:SS';
210 my $timestamp_format = $ENV{NLS_TIMESTAMP_FORMAT} ||=
211 'YYYY-MM-DD HH24:MI:SS.FF';
212 my $timestamp_tz_format = $ENV{NLS_TIMESTAMP_TZ_FORMAT} ||=
213 'YYYY-MM-DD HH24:MI:SS.FF TZHTZM';
216 "alter session set nls_date_format = '$date_format'"
219 "alter session set nls_timestamp_format = '$timestamp_format'"
222 "alter session set nls_timestamp_tz_format='$timestamp_tz_format'"
226 =head2 source_bind_attributes
228 Handle LOB types in Oracle. Under a certain size (4k?), you can get away
229 with the driver assuming your input is the deprecated LONG type if you
230 encode it as a hex string. That ain't gonna fly at larger values, where
231 you'll discover you have to do what this does.
233 This method had to be overridden because we need to set ora_field to the
234 actual column, and that isn't passed to the call (provided by Storage) to
235 bind_attribute_by_data_type.
237 According to L<DBD::Oracle>, the ora_field isn't always necessary, but
238 adding it doesn't hurt, and will save your bacon if you're modifying a
239 table with more than one LOB column.
243 sub source_bind_attributes
251 foreach my $column ($source->columns) {
252 my $data_type = $source->column_info($column)->{data_type} || '';
253 next unless $data_type;
255 my %column_bind_attrs = $self->bind_attribute_by_data_type($data_type);
257 if ($data_type =~ /^[BC]LOB$/i) {
258 if ($DBD::Oracle::VERSION eq '1.23') {
259 $self->throw_exception(
260 "BLOB/CLOB support in DBD::Oracle == 1.23 is broken, use an earlier or later ".
261 "version.\n\nSee: https://rt.cpan.org/Public/Bug/Display.html?id=46016\n"
265 $column_bind_attrs{'ora_type'} = uc($data_type) eq 'CLOB'
266 ? DBD::Oracle::ORA_CLOB()
267 : DBD::Oracle::ORA_BLOB()
269 $column_bind_attrs{'ora_field'} = $column;
272 $bind_attributes{$column} = \%column_bind_attrs;
275 return \%bind_attributes;
279 my ($self, $name) = @_;
280 $self->_get_dbh->do("SAVEPOINT $name");
283 # Oracle automatically releases a savepoint when you start another one with the
285 sub _svp_release { 1 }
288 my ($self, $name) = @_;
289 $self->_get_dbh->do("ROLLBACK TO SAVEPOINT $name")
292 =head2 relname_to_table_alias
294 L<DBIx::Class> uses L<DBIx::Class::Relationship> names as table aliases in
297 Unfortunately, Oracle doesn't support identifiers over 30 chars in length, so
298 the L<DBIx::Class::Relationship> name is shortened and appended with half of an
301 See L<DBIx::Class::Storage/"relname_to_table_alias">.
305 sub relname_to_table_alias {
307 my ($relname, $join_count) = @_;
309 my $alias = $self->next::method(@_);
311 return $alias if length($alias) <= 30;
313 # get a base64 md5 of the alias with join_count
315 my $ctx = Digest::MD5->new;
317 my $md5 = $ctx->b64digest;
319 # remove alignment mark just in case
322 # truncate and prepend to truncated relname without vowels
323 (my $devoweled = $relname) =~ s/[aeiou]//g;
324 my $shortened = substr($devoweled, 0, 18);
327 $shortened . '_' . substr($md5, 0, 30 - length($shortened) - 1);
332 =head2 with_deferred_fk_checks
334 Runs a coderef between:
336 alter session set constraints = deferred
338 alter session set constraints = immediate
340 to defer foreign key checks.
342 Constraints must be declared C<DEFERRABLE> for this to work.
346 sub with_deferred_fk_checks {
347 my ($self, $sub) = @_;
349 my $txn_scope_guard = $self->txn_scope_guard;
351 $self->_do_query('alter session set constraints = deferred');
353 my $sg = Scope::Guard->new(sub {
354 $self->_do_query('alter session set constraints = immediate');
357 return Context::Preserve::preserve_context(sub { $sub->() },
358 after => sub { $txn_scope_guard->commit });
363 See L<DBIx::Class/CONTRIBUTORS>.
367 You may distribute this code under the same terms as Perl itself.