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/;
30 use base qw/DBIx::Class::Storage::DBI::MultiDistinctEmulation/;
32 # __PACKAGE__->load_components(qw/PK::Auto/);
34 sub _dbh_last_insert_id {
35 my ($self, $dbh, $source, @columns) = @_;
37 foreach my $col (@columns) {
38 my $seq = ($source->column_info($col)->{sequence} ||= $self->get_autoinc_seq($source,$col));
39 my $id = $self->_sequence_fetch( 'currval', $seq );
45 sub _dbh_get_autoinc_seq {
46 my ($self, $dbh, $source, $col) = @_;
48 # look up the correct sequence automatically
50 SELECT trigger_body FROM ALL_TRIGGERS t
51 WHERE t.table_name = ?
52 AND t.triggering_event = 'INSERT'
53 AND t.status = 'ENABLED'
56 # trigger_body is a LONG
57 $dbh->{LongReadLen} = 64 * 1024 if ($dbh->{LongReadLen} < 64 * 1024);
61 # check for fully-qualified name (eg. SCHEMA.TABLENAME)
62 if ( my ( $schema, $table ) = $source->name =~ /(\w+)\.(\w+)/ ) {
64 SELECT trigger_body FROM ALL_TRIGGERS t
65 WHERE t.owner = ? AND t.table_name = ?
66 AND t.triggering_event = 'INSERT'
67 AND t.status = 'ENABLED'
69 $sth = $dbh->prepare($sql);
70 $sth->execute( uc($schema), uc($table) );
73 $sth = $dbh->prepare($sql);
74 $sth->execute( uc( $source->name ) );
76 while (my ($insert_trigger) = $sth->fetchrow_array) {
77 return uc($1) if $insert_trigger =~ m!(\w+)\.nextval!i; # col name goes here???
79 $self->throw_exception("Unable to find a sequence INSERT trigger on table '" . $source->name . "'.");
83 my ( $self, $type, $seq ) = @_;
84 my ($id) = $self->dbh->selectrow_array("SELECT ${seq}.${type} FROM DUAL");
91 if ($self->SUPER::connected(@_)) {
92 my $dbh = $self->_dbh;
94 my $ping_sth = $dbh->prepare_cached("select 1 from dual");
96 local $dbh->{RaiseError} = 1;
114 my ($dbh, $op, $extra_bind, $ident, $bind_attributes, @args) = @_;
116 my $wantarray = wantarray;
125 @res = $self->SUPER::_dbh_execute(@_);
127 $res[0] = $self->SUPER::_dbh_execute(@_);
131 if ($exception =~ /ORA-01003/) {
132 # ORA-01003: no statement parsed (someone changed the table somehow,
133 # invalidating your cursor.)
134 my ($sql, $bind) = $self->_prep_for_execute($op, $extra_bind, $ident, \@args);
135 delete $dbh->{CachedKids}{$sql};
141 $self->throw_exception($exception) if $exception;
143 wantarray ? @res : $res[0]
146 =head2 get_autoinc_seq
148 Returns the sequence name for an autoincrement column
152 sub get_autoinc_seq {
153 my ($self, $source, $col) = @_;
155 $self->dbh_do('_dbh_get_autoinc_seq', $source, $col);
158 =head2 columns_info_for
160 This wraps the superclass version of this method to force table
165 sub columns_info_for {
166 my ($self, $table) = @_;
168 $self->next::method(uc($table));
171 =head2 datetime_parser_type
173 This sets the proper DateTime::Format module for use with
174 L<DBIx::Class::InflateColumn::DateTime>.
178 sub datetime_parser_type { return "DateTime::Format::Oracle"; }
181 my ($self, $name) = @_;
183 $self->dbh->do("SAVEPOINT $name");
186 # Oracle automatically releases a savepoint when you start another one with the
188 sub _svp_release { 1 }
191 my ($self, $name) = @_;
193 $self->dbh->do("ROLLBACK TO SAVEPOINT $name")
198 Andy Grundman <andy@hybridized.org>
200 Scott Connelly <scottsweep@yahoo.com>
204 You may distribute this code under the same terms as Perl itself.