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