1 package DBIx::Class::Storage::DBI::Oracle::Generic;
2 # -*- mode: cperl; cperl-indent-level: 2 -*-
9 DBIx::Class::Storage::DBI::Oracle::Generic - Automatic primary key class for Oracle
13 # In your table classes
14 __PACKAGE__->load_components(qw/PK::Auto Core/);
15 __PACKAGE__->add_columns({ id => { sequence => 'mysequence', auto_nextval => 1 } });
16 __PACKAGE__->set_primary_key('id');
17 __PACKAGE__->sequence('mysequence');
21 This class implements autoincrements for Oracle.
27 use Carp::Clan qw/^DBIx::Class/;
29 use base qw/DBIx::Class::Storage::DBI::MultiDistinctEmulation/;
31 # __PACKAGE__->load_components(qw/PK::Auto/);
33 sub _dbh_last_insert_id {
34 my ($self, $dbh, $source, @columns) = @_;
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 );
44 sub _dbh_get_autoinc_seq {
45 my ($self, $dbh, $source, $col) = @_;
47 # look up the correct sequence automatically
49 SELECT trigger_body FROM ALL_TRIGGERS t
50 WHERE t.table_name = ?
51 AND t.triggering_event = 'INSERT'
52 AND t.status = 'ENABLED'
55 # trigger_body is a LONG
56 $dbh->{LongReadLen} = 64 * 1024 if ($dbh->{LongReadLen} < 64 * 1024);
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???
63 $self->throw_exception("Unable to find a sequence INSERT trigger on table '" . $source->name . "'.");
67 my ( $self, $type, $seq ) = @_;
68 my ($id) = $self->dbh->selectrow_array("SELECT ${seq}.${type} FROM DUAL");
74 my ($dbh, $op, $extra_bind, $ident, $bind_attributes, @args) = @_;
76 my $wantarray = wantarray;
85 @res = $self->SUPER::_dbh_execute(@_);
87 $res[0] = $self->SUPER::_dbh_execute(@_);
91 if ($exception =~ /ORA-(?:00028|01012)/) {
92 # ORA-00028: your session has been killed
93 # ORA-01012: not logged on
96 $self->throw_exception($exception) if $self->{_in_dbh_do};
98 $self->ensure_connected;
99 } elsif ($exception =~ /ORA-01003/) { # invalid cursor
100 # ORA-01003: no statement parsed (someone renamed a column or something,
101 # invalidating your cursor.)
102 my ($sql, $bind) = $self->_prep_for_execute($op, $extra_bind, $ident, \@args);
103 delete $dbh->{CachedKids}{$sql};
109 $self->throw_exception($exception) if $exception;
111 wantarray ? @res : $res[0]
114 =head2 get_autoinc_seq
116 Returns the sequence name for an autoincrement column
120 sub get_autoinc_seq {
121 my ($self, $source, $col) = @_;
123 $self->dbh_do('_dbh_get_autoinc_seq', $source, $col);
126 =head2 columns_info_for
128 This wraps the superclass version of this method to force table
133 sub columns_info_for {
134 my ($self, $table) = @_;
136 $self->next::method(uc($table));
139 =head2 datetime_parser_type
141 This sets the proper DateTime::Format module for use with
142 L<DBIx::Class::InflateColumn::DateTime>.
146 sub datetime_parser_type { return "DateTime::Format::Oracle"; }
149 my ($self, $name) = @_;
151 $self->dbh->do("SAVEPOINT $name");
154 # Oracle automatically releases a savepoint when you start another one with the
156 sub _svp_release { 1 }
159 my ($self, $name) = @_;
161 $self->dbh->do("ROLLBACK TO SAVEPOINT $name")
166 Andy Grundman <andy@hybridized.org>
168 Scott Connelly <scottsweep@yahoo.com>
172 You may distribute this code under the same terms as Perl itself.