RT#38251: DBIx::Class::Storage::DBI::Oracle::Generic does not handle fully-qualified...
[dbsrgits/DBIx-Class-Historic.git] / lib / DBIx / Class / Storage / DBI / Oracle / Generic.pm
CommitLineData
18360aed 1package DBIx::Class::Storage::DBI::Oracle::Generic;
e21dfd6a 2# -*- mode: cperl; cperl-indent-level: 2 -*-
18360aed 3
4use strict;
5use warnings;
6
7137528d 7=head1 NAME
8
92bc2a19 9DBIx::Class::Storage::DBI::Oracle::Generic - Automatic primary key class for Oracle
7137528d 10
11=head1 SYNOPSIS
12
13 # In your table classes
14 __PACKAGE__->load_components(qw/PK::Auto Core/);
2e46b6eb 15 __PACKAGE__->add_columns({ id => { sequence => 'mysequence', auto_nextval => 1 } });
7137528d 16 __PACKAGE__->set_primary_key('id');
17 __PACKAGE__->sequence('mysequence');
18
19=head1 DESCRIPTION
20
21This class implements autoincrements for Oracle.
22
23=head1 METHODS
24
25=cut
26
18360aed 27use Carp::Clan qw/^DBIx::Class/;
28
29use base qw/DBIx::Class::Storage::DBI::MultiDistinctEmulation/;
30
31# __PACKAGE__->load_components(qw/PK::Auto/);
32
33sub _dbh_last_insert_id {
2e46b6eb 34 my ($self, $dbh, $source, @columns) = @_;
35 my @ids = ();
36 foreach my $col (@columns) {
37 my $seq = ($source->column_info($col)->{sequence} ||= $self->get_autoinc_seq($source,$col));
38 my $id = $self->_sequence_fetch( 'currval', $seq );
39 push @ids, $id;
40 }
41 return @ids;
18360aed 42}
43
44sub _dbh_get_autoinc_seq {
45 my ($self, $dbh, $source, $col) = @_;
46
47 # look up the correct sequence automatically
48 my $sql = q{
49 SELECT trigger_body FROM ALL_TRIGGERS t
50 WHERE t.table_name = ?
51 AND t.triggering_event = 'INSERT'
52 AND t.status = 'ENABLED'
53 };
54
55 # trigger_body is a LONG
56 $dbh->{LongReadLen} = 64 * 1024 if ($dbh->{LongReadLen} < 64 * 1024);
57
cb464582 58 my $sth;
59
60 # check for fully-qualified name (eg. SCHEMA.TABLENAME)
61 if ( my ( $schema, $table ) = $source->name =~ /(\w+)\.(\w+)/ ) {
62 $sql = q{
63 SELECT trigger_body FROM ALL_TRIGGERS t
64 WHERE t.owner = ? AND t.table_name = ?
65 AND t.triggering_event = 'INSERT'
66 AND t.status = 'ENABLED'
67 };
68 $sth = $dbh->prepare($sql);
69 $sth->execute( uc($schema), uc($table) );
70 }
71 else {
72 $sth = $dbh->prepare($sql);
73 $sth->execute( uc( $source->name ) );
74 }
18360aed 75 while (my ($insert_trigger) = $sth->fetchrow_array) {
76 return uc($1) if $insert_trigger =~ m!(\w+)\.nextval!i; # col name goes here???
77 }
66cab05c 78 $self->throw_exception("Unable to find a sequence INSERT trigger on table '" . $source->name . "'.");
18360aed 79}
80
2e46b6eb 81sub _sequence_fetch {
82 my ( $self, $type, $seq ) = @_;
83 my ($id) = $self->dbh->selectrow_array("SELECT ${seq}.${type} FROM DUAL");
84 return $id;
85}
86
d789fa99 87sub _dbh_execute {
88 my $self = shift;
89 my ($dbh, $op, $extra_bind, $ident, $bind_attributes, @args) = @_;
90
91 my $wantarray = wantarray;
92 my @res;
93 my $exception;
94
95 my $try = 2;
96
97 while ($try--) {
98 eval {
99 if ($wantarray) {
100 @res = $self->SUPER::_dbh_execute(@_);
101 } else {
102 $res[0] = $self->SUPER::_dbh_execute(@_);
103 }
104 };
105 $exception = $@;
106 if ($exception =~ /ORA-(?:00028|01012)/) {
107# ORA-00028: your session has been killed
108# ORA-01012: not logged on
109 $self->disconnect;
110
111 $self->throw_exception($exception) if $self->{_in_dbh_do};
7a63d445 112 $self->throw_exception($exception) if $self->transaction_depth;
d789fa99 113
114 $self->ensure_connected;
115 } elsif ($exception =~ /ORA-01003/) { # invalid cursor
116# ORA-01003: no statement parsed (someone renamed a column or something,
117# invalidating your cursor.)
118 my ($sql, $bind) = $self->_prep_for_execute($op, $extra_bind, $ident, \@args);
119 delete $dbh->{CachedKids}{$sql};
120 } else {
121 last;
122 }
123 }
124
125 $self->throw_exception($exception) if $exception;
126
127 wantarray ? @res : $res[0]
128}
129
7137528d 130=head2 get_autoinc_seq
131
132Returns the sequence name for an autoincrement column
133
134=cut
135
18360aed 136sub get_autoinc_seq {
137 my ($self, $source, $col) = @_;
138
373940e1 139 $self->dbh_do('_dbh_get_autoinc_seq', $source, $col);
18360aed 140}
141
7137528d 142=head2 columns_info_for
143
144This wraps the superclass version of this method to force table
145names to uppercase
146
147=cut
148
18360aed 149sub columns_info_for {
150 my ($self, $table) = @_;
151
152 $self->next::method(uc($table));
153}
154
8f7e044c 155=head2 datetime_parser_type
156
157This sets the proper DateTime::Format module for use with
158L<DBIx::Class::InflateColumn::DateTime>.
159
160=cut
161
162sub datetime_parser_type { return "DateTime::Format::Oracle"; }
163
281719d2 164sub _svp_begin {
165 my ($self, $name) = @_;
166
167 $self->dbh->do("SAVEPOINT $name");
168}
169
170# Oracle automatically releases a savepoint when you start another one with the
171# same name.
172sub _svp_release { 1 }
173
174sub _svp_rollback {
175 my ($self, $name) = @_;
176
177 $self->dbh->do("ROLLBACK TO SAVEPOINT $name")
178}
179
18360aed 180=head1 AUTHORS
181
182Andy Grundman <andy@hybridized.org>
183
184Scott Connelly <scottsweep@yahoo.com>
185
186=head1 LICENSE
187
188You may distribute this code under the same terms as Perl itself.
189
190=cut
7137528d 191
1921;