A dbh_do statement executed with bind values will confuse the hell out of DBIC runnin...
[dbsrgits/DBIx-Class.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
58 my $sth = $dbh->prepare($sql);
59 $sth->execute( uc($source->name) );
60 while (my ($insert_trigger) = $sth->fetchrow_array) {
61 return uc($1) if $insert_trigger =~ m!(\w+)\.nextval!i; # col name goes here???
62 }
66cab05c 63 $self->throw_exception("Unable to find a sequence INSERT trigger on table '" . $source->name . "'.");
18360aed 64}
65
2e46b6eb 66sub _sequence_fetch {
67 my ( $self, $type, $seq ) = @_;
68 my ($id) = $self->dbh->selectrow_array("SELECT ${seq}.${type} FROM DUAL");
69 return $id;
70}
71
d789fa99 72sub _dbh_execute {
73 my $self = shift;
74 my ($dbh, $op, $extra_bind, $ident, $bind_attributes, @args) = @_;
75
76 my $wantarray = wantarray;
77 my @res;
78 my $exception;
79
80 my $try = 2;
81
82 while ($try--) {
83 eval {
84 if ($wantarray) {
85 @res = $self->SUPER::_dbh_execute(@_);
86 } else {
87 $res[0] = $self->SUPER::_dbh_execute(@_);
88 }
89 };
90 $exception = $@;
91 if ($exception =~ /ORA-(?:00028|01012)/) {
92# ORA-00028: your session has been killed
93# ORA-01012: not logged on
94 $self->disconnect;
95
96 $self->throw_exception($exception) if $self->{_in_dbh_do};
7a63d445 97 $self->throw_exception($exception) if $self->transaction_depth;
d789fa99 98
99 $self->ensure_connected;
100 } elsif ($exception =~ /ORA-01003/) { # invalid cursor
101# ORA-01003: no statement parsed (someone renamed a column or something,
102# invalidating your cursor.)
103 my ($sql, $bind) = $self->_prep_for_execute($op, $extra_bind, $ident, \@args);
104 delete $dbh->{CachedKids}{$sql};
105 } else {
106 last;
107 }
108 }
109
110 $self->throw_exception($exception) if $exception;
111
112 wantarray ? @res : $res[0]
113}
114
7137528d 115=head2 get_autoinc_seq
116
117Returns the sequence name for an autoincrement column
118
119=cut
120
18360aed 121sub get_autoinc_seq {
122 my ($self, $source, $col) = @_;
123
373940e1 124 $self->dbh_do('_dbh_get_autoinc_seq', $source, $col);
18360aed 125}
126
7137528d 127=head2 columns_info_for
128
129This wraps the superclass version of this method to force table
130names to uppercase
131
132=cut
133
18360aed 134sub columns_info_for {
135 my ($self, $table) = @_;
136
137 $self->next::method(uc($table));
138}
139
8f7e044c 140=head2 datetime_parser_type
141
142This sets the proper DateTime::Format module for use with
143L<DBIx::Class::InflateColumn::DateTime>.
144
145=cut
146
147sub datetime_parser_type { return "DateTime::Format::Oracle"; }
148
281719d2 149sub _svp_begin {
150 my ($self, $name) = @_;
151
152 $self->dbh->do("SAVEPOINT $name");
153}
154
155# Oracle automatically releases a savepoint when you start another one with the
156# same name.
157sub _svp_release { 1 }
158
159sub _svp_rollback {
160 my ($self, $name) = @_;
161
162 $self->dbh->do("ROLLBACK TO SAVEPOINT $name")
163}
164
18360aed 165=head1 AUTHORS
166
167Andy Grundman <andy@hybridized.org>
168
169Scott Connelly <scottsweep@yahoo.com>
170
171=head1 LICENSE
172
173You may distribute this code under the same terms as Perl itself.
174
175=cut
7137528d 176
1771;