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