oracle_version
[dbsrgits/DBIx-Class.git] / lib / DBIx / Class / Storage / DBI / Oracle / Generic.pm
CommitLineData
18360aed 1package DBIx::Class::Storage::DBI::Oracle::Generic;
2
3use strict;
4use warnings;
5
7137528d 6=head1 NAME
7
7a84c41b 8DBIx::Class::Storage::DBI::Oracle::Generic - Oracle Support for DBIx::Class
7137528d 9
10=head1 SYNOPSIS
11
d88ecca6 12 # In your result (table) classes
13 use base 'DBIx::Class::Core';
2e46b6eb 14 __PACKAGE__->add_columns({ id => { sequence => 'mysequence', auto_nextval => 1 } });
7137528d 15 __PACKAGE__->set_primary_key('id');
16 __PACKAGE__->sequence('mysequence');
17
18=head1 DESCRIPTION
19
6c0230de 20This class implements base Oracle support. The subclass
21L<DBIx::Class::Storage::DBI::Oracle::WhereJoins> is for C<(+)> joins in Oracle
22versions before 9.
7137528d 23
24=head1 METHODS
25
26=cut
27
db56cf3d 28use base qw/DBIx::Class::Storage::DBI/;
2ad62d97 29use mro 'c3';
18360aed 30
dd2600c6 31sub deployment_statements {
32 my $self = shift;;
33 my ($schema, $type, $version, $dir, $sqltargs, @rest) = @_;
34
35 $sqltargs ||= {};
36 my $quote_char = $self->schema->storage->{'_sql_maker_opts'}->{'quote_char'};
37 $sqltargs->{quote_table_names} = 0 unless $quote_char;
38 $sqltargs->{quote_field_names} = 0 unless $quote_char;
39
a4433d8e 40 my $oracle_version = eval { $self->_get_dbh->get_info(18) };
41
42 $sqltargs->{producer_args}{oracle_version} = $oracle_version;
43
dd2600c6 44 $self->next::method($schema, $type, $version, $dir, $sqltargs, @rest);
45}
46
18360aed 47sub _dbh_last_insert_id {
2e46b6eb 48 my ($self, $dbh, $source, @columns) = @_;
49 my @ids = ();
50 foreach my $col (@columns) {
51 my $seq = ($source->column_info($col)->{sequence} ||= $self->get_autoinc_seq($source,$col));
52 my $id = $self->_sequence_fetch( 'currval', $seq );
53 push @ids, $id;
54 }
55 return @ids;
18360aed 56}
57
58sub _dbh_get_autoinc_seq {
59 my ($self, $dbh, $source, $col) = @_;
60
dd2600c6 61 # check if quoting is on
852a66f6 62 my $quote_char = $self->schema->storage->{'_sql_maker_opts'}->{'quote_char'};
852a66f6 63
18360aed 64 # look up the correct sequence automatically
65 my $sql = q{
66 SELECT trigger_body FROM ALL_TRIGGERS t
67 WHERE t.table_name = ?
68 AND t.triggering_event = 'INSERT'
69 AND t.status = 'ENABLED'
70 };
71
72 # trigger_body is a LONG
7a84c41b 73 local $dbh->{LongReadLen} = 64 * 1024 if ($dbh->{LongReadLen} < 64 * 1024);
18360aed 74
cb464582 75 my $sth;
76
e6dd7b42 77 my $source_name;
78 if ( ref $source->name ne 'SCALAR' ) {
79 $source_name = $source->name;
80 }
81 else {
82 $source_name = ${$source->name};
83 }
dd2600c6 84 $source_name = uc($source_name) unless $quote_char;
e6dd7b42 85
cb464582 86 # check for fully-qualified name (eg. SCHEMA.TABLENAME)
e6dd7b42 87 if ( my ( $schema, $table ) = $source_name =~ /(\w+)\.(\w+)/ ) {
cb464582 88 $sql = q{
89 SELECT trigger_body FROM ALL_TRIGGERS t
90 WHERE t.owner = ? AND t.table_name = ?
91 AND t.triggering_event = 'INSERT'
92 AND t.status = 'ENABLED'
93 };
94 $sth = $dbh->prepare($sql);
dd2600c6 95 my $table_name = $self -> sql_maker -> _quote($table);
96 #my $schema_name = $self -> sql_maker -> _quote($schema);
97 my $schema_name = uc($schema);
98
99 $sth->execute( $schema_name, $table_name );
cb464582 100 }
101 else {
102 $sth = $dbh->prepare($sql);
852a66f6 103 $sth->execute( $source_name );
cb464582 104 }
18360aed 105 while (my ($insert_trigger) = $sth->fetchrow_array) {
852a66f6 106 return $1 if $insert_trigger =~ m!("?\w+"?)\.nextval!i; # col name goes here???
18360aed 107 }
66cab05c 108 $self->throw_exception("Unable to find a sequence INSERT trigger on table '" . $source->name . "'.");
18360aed 109}
110
2e46b6eb 111sub _sequence_fetch {
112 my ( $self, $type, $seq ) = @_;
9ae966b9 113 my ($id) = $self->_get_dbh->selectrow_array("SELECT ${seq}.${type} FROM DUAL");
2e46b6eb 114 return $id;
115}
116
6dc4be0f 117sub _ping {
c2481821 118 my $self = shift;
7ba7a57d 119
6dc4be0f 120 my $dbh = $self->_dbh or return 0;
7ba7a57d 121
6dc4be0f 122 local $dbh->{RaiseError} = 1;
c2d7baef 123
6dc4be0f 124 eval {
125 $dbh->do("select 1 from dual");
126 };
7ba7a57d 127
6dc4be0f 128 return $@ ? 0 : 1;
c2481821 129}
130
d789fa99 131sub _dbh_execute {
132 my $self = shift;
133 my ($dbh, $op, $extra_bind, $ident, $bind_attributes, @args) = @_;
134
135 my $wantarray = wantarray;
d789fa99 136
c2d7baef 137 my (@res, $exception, $retried);
138
0f0abc97 139 RETRY: {
140 do {
141 eval {
142 if ($wantarray) {
c3515436 143 @res = $self->next::method(@_);
0f0abc97 144 } else {
c3515436 145 $res[0] = $self->next::method(@_);
0f0abc97 146 }
147 };
148 $exception = $@;
149 if ($exception =~ /ORA-01003/) {
150 # ORA-01003: no statement parsed (someone changed the table somehow,
151 # invalidating your cursor.)
152 my ($sql, $bind) = $self->_prep_for_execute($op, $extra_bind, $ident, \@args);
153 delete $dbh->{CachedKids}{$sql};
d789fa99 154 } else {
0f0abc97 155 last RETRY;
d789fa99 156 }
0f0abc97 157 } while (not $retried++);
158 }
d789fa99 159
160 $self->throw_exception($exception) if $exception;
161
162 wantarray ? @res : $res[0]
163}
164
7137528d 165=head2 get_autoinc_seq
166
167Returns the sequence name for an autoincrement column
168
169=cut
170
18360aed 171sub get_autoinc_seq {
172 my ($self, $source, $col) = @_;
d4daee7b 173
373940e1 174 $self->dbh_do('_dbh_get_autoinc_seq', $source, $col);
18360aed 175}
176
7137528d 177=head2 columns_info_for
178
179This wraps the superclass version of this method to force table
180names to uppercase
181
182=cut
183
18360aed 184sub columns_info_for {
185 my ($self, $table) = @_;
186
dd2600c6 187 $self->next::method($table);
18360aed 188}
189
8f7e044c 190=head2 datetime_parser_type
191
192This sets the proper DateTime::Format module for use with
193L<DBIx::Class::InflateColumn::DateTime>.
194
195=cut
196
197sub datetime_parser_type { return "DateTime::Format::Oracle"; }
198
9900b569 199=head2 connect_call_datetime_setup
d2a3958e 200
201Used as:
202
9900b569 203 on_connect_call => 'datetime_setup'
d2a3958e 204
82f6f45f 205In L<DBIx::Class::Storage::DBI/connect_info> to set the session nls date, and
206timestamp values for use with L<DBIx::Class::InflateColumn::DateTime> and the
207necessary environment variables for L<DateTime::Format::Oracle>, which is used
208by it.
d2a3958e 209
82f6f45f 210Maximum allowable precision is used, unless the environment variables have
211already been set.
d2a3958e 212
9900b569 213These are the defaults used:
214
215 $ENV{NLS_DATE_FORMAT} ||= 'YYYY-MM-DD HH24:MI:SS';
216 $ENV{NLS_TIMESTAMP_FORMAT} ||= 'YYYY-MM-DD HH24:MI:SS.FF';
217 $ENV{NLS_TIMESTAMP_TZ_FORMAT} ||= 'YYYY-MM-DD HH24:MI:SS.FF TZHTZM';
218
d9e53b85 219To get more than second precision with L<DBIx::Class::InflateColumn::DateTime>
220for your timestamps, use something like this:
221
222 use Time::HiRes 'time';
223 my $ts = DateTime->from_epoch(epoch => time);
224
d2a3958e 225=cut
226
9900b569 227sub connect_call_datetime_setup {
d2a3958e 228 my $self = shift;
d2a3958e 229
230 my $date_format = $ENV{NLS_DATE_FORMAT} ||= 'YYYY-MM-DD HH24:MI:SS';
231 my $timestamp_format = $ENV{NLS_TIMESTAMP_FORMAT} ||=
232 'YYYY-MM-DD HH24:MI:SS.FF';
233 my $timestamp_tz_format = $ENV{NLS_TIMESTAMP_TZ_FORMAT} ||=
234 'YYYY-MM-DD HH24:MI:SS.FF TZHTZM';
235
7a84c41b 236 $self->_do_query(
d7a58a29 237 "alter session set nls_date_format = '$date_format'"
238 );
7a84c41b 239 $self->_do_query(
d7a58a29 240 "alter session set nls_timestamp_format = '$timestamp_format'"
241 );
242 $self->_do_query(
243 "alter session set nls_timestamp_tz_format='$timestamp_tz_format'"
244 );
d2a3958e 245}
246
5db2758d 247=head2 source_bind_attributes
248
249Handle LOB types in Oracle. Under a certain size (4k?), you can get away
250with the driver assuming your input is the deprecated LONG type if you
251encode it as a hex string. That ain't gonna fly at larger values, where
252you'll discover you have to do what this does.
253
254This method had to be overridden because we need to set ora_field to the
255actual column, and that isn't passed to the call (provided by Storage) to
256bind_attribute_by_data_type.
257
258According to L<DBD::Oracle>, the ora_field isn't always necessary, but
259adding it doesn't hurt, and will save your bacon if you're modifying a
260table with more than one LOB column.
261
262=cut
263
e6dd7b42 264sub source_bind_attributes
5db2758d 265{
d7a58a29 266 require DBD::Oracle;
267 my $self = shift;
268 my($source) = @_;
5db2758d 269
d7a58a29 270 my %bind_attributes;
5db2758d 271
d7a58a29 272 foreach my $column ($source->columns) {
273 my $data_type = $source->column_info($column)->{data_type} || '';
274 next unless $data_type;
5db2758d 275
d7a58a29 276 my %column_bind_attrs = $self->bind_attribute_by_data_type($data_type);
5db2758d 277
d7a58a29 278 if ($data_type =~ /^[BC]LOB$/i) {
931e5d43 279 if ($DBD::Oracle::VERSION eq '1.23') {
280 $self->throw_exception(
281"BLOB/CLOB support in DBD::Oracle == 1.23 is broken, use an earlier or later ".
555cc3f4 282"version.\n\nSee: https://rt.cpan.org/Public/Bug/Display.html?id=46016\n"
931e5d43 283 );
284 }
285
d7a58a29 286 $column_bind_attrs{'ora_type'} = uc($data_type) eq 'CLOB'
287 ? DBD::Oracle::ORA_CLOB()
288 : DBD::Oracle::ORA_BLOB()
289 ;
290 $column_bind_attrs{'ora_field'} = $column;
291 }
5db2758d 292
d7a58a29 293 $bind_attributes{$column} = \%column_bind_attrs;
294 }
5db2758d 295
d7a58a29 296 return \%bind_attributes;
5db2758d 297}
298
1816be4f 299sub _svp_begin {
d7a58a29 300 my ($self, $name) = @_;
301 $self->_get_dbh->do("SAVEPOINT $name");
1816be4f 302}
303
281719d2 304# Oracle automatically releases a savepoint when you start another one with the
305# same name.
306sub _svp_release { 1 }
307
308sub _svp_rollback {
d7a58a29 309 my ($self, $name) = @_;
310 $self->_get_dbh->do("ROLLBACK TO SAVEPOINT $name")
281719d2 311}
312
6c0230de 313=head2 relname_to_table_alias
314
315L<DBIx::Class> uses L<DBIx::Class::Relationship> names as table aliases in
316queries.
317
318Unfortunately, Oracle doesn't support identifiers over 30 chars in length, so
af0edca1 319the L<DBIx::Class::Relationship> name is shortened and appended with half of an
320MD5 hash.
6c0230de 321
322See L<DBIx::Class::Storage/"relname_to_table_alias">.
323
324=cut
325
326sub relname_to_table_alias {
327 my $self = shift;
328 my ($relname, $join_count) = @_;
329
330 my $alias = $self->next::method(@_);
331
332 return $alias if length($alias) <= 30;
333
af0edca1 334 # get a base64 md5 of the alias with join_count
335 require Digest::MD5;
336 my $ctx = Digest::MD5->new;
337 $ctx->add($alias);
338 my $md5 = $ctx->b64digest;
6c0230de 339
f098ade6 340 # remove alignment mark just in case
341 $md5 =~ s/=*\z//;
342
af0edca1 343 # truncate and prepend to truncated relname without vowels
344 (my $devoweled = $relname) =~ s/[aeiou]//g;
909668fe 345 my $shortened = substr($devoweled, 0, 18);
6c0230de 346
909668fe 347 my $new_alias =
348 $shortened . '_' . substr($md5, 0, 30 - length($shortened) - 1);
349
350 return $new_alias;
6c0230de 351}
352
7a84c41b 353=head1 AUTHOR
18360aed 354
7a84c41b 355See L<DBIx::Class/CONTRIBUTORS>.
18360aed 356
357=head1 LICENSE
358
359You may distribute this code under the same terms as Perl itself.
360
361=cut
7137528d 362
3631;