Shovelling PK::Auto stuff where it belongs..
[dbsrgits/DBIx-Class.git] / lib / DBIx / Class / Storage / DBI / Oracle.pm
1 package DBIx::Class::Storage::DBI::Oracle;
2
3 use strict;
4 use warnings;
5
6 use Carp qw/croak/;
7
8 use base qw/DBIx::Class::Storage::DBI/;
9
10 # __PACKAGE__->load_components(qw/PK::Auto/);
11
12 sub last_insert_id {
13   my ($self, $source) = shift;
14   $self->get_autoinc_seq($source) unless $self->{_autoinc_seq};
15   my $sql = "SELECT " . $self->{_autoinc_seq} . ".currval FROM DUAL";
16   my ($id) = $self->_dbh->selectrow_array($sql);
17   return $id;  
18 }
19
20 sub get_autoinc_seq {
21   my ($self, $source) = shift;
22   
23   # return the user-defined sequence if known
24   if ($source->sequence) {
25     return $self->{_autoinc_seq} = $source->sequence;
26   }
27   
28   # look up the correct sequence automatically
29   my $dbh = $self->_dbh;
30   my $sql = qq{
31     SELECT trigger_body FROM ALL_TRIGGERS t
32     WHERE t.table_name = ?
33     AND t.triggering_event = 'INSERT'
34     AND t.status = 'ENABLED'
35   };
36   # trigger_body is a LONG
37   $dbh->{LongReadLen} = 64 * 1024 if ($dbh->{LongReadLen} < 64 * 1024);
38   my $sth = $dbh->prepare($sql);
39   $sth->execute( uc($source->name) );
40   while (my ($insert_trigger) = $sth->fetchrow_array) {
41     if ($insert_trigger =~ m!(\w+)\.nextval!i ) {
42       $self->{_autoinc_seq} = uc($1);
43     }
44   }
45   unless ($self->{_autoinc_seq}) {
46     croak "Unable to find a sequence INSERT trigger on table '" . $self->_table_name . "'.";
47   }
48 }
49
50 1;
51
52 =head1 NAME 
53
54 DBIx::Class::Storage::DBI::Oracle - Automatic primary key class for Oracle
55
56 =head1 SYNOPSIS
57
58   # In your table classes
59   __PACKAGE__->load_components(qw/PK::Auto Core/);
60   __PACKAGE__->set_primary_key('id');
61   __PACKAGE__->sequence('mysequence');
62
63 =head1 DESCRIPTION
64
65 This class implements autoincrements for Oracle.
66
67 =head1 AUTHORS
68
69 Andy Grundman <andy@hybridized.org>
70
71 Scott Connelly <scottsweep@yahoo.com>
72
73 =head1 LICENSE
74
75 You may distribute this code under the same terms as Perl itself.
76
77 =cut