fetch nextval from sequence for insert
[dbsrgits/DBIx-Class.git] / lib / DBIx / Class / Storage / DBI / Oracle / Generic.pm
1 package DBIx::Class::Storage::DBI::Oracle::Generic;
2 # -*- mode: cperl; cperl-indent-level: 2 -*-
3
4 use strict;
5 use warnings;
6
7 =head1 NAME
8
9 DBIx::Class::Storage::DBI::Oracle - Automatic primary key class for Oracle
10
11 =head1 SYNOPSIS
12
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');
18
19 =head1 DESCRIPTION
20
21 This class implements autoincrements for Oracle.
22
23 =head1 METHODS
24
25 =cut
26
27 use Carp::Clan qw/^DBIx::Class/;
28
29 use base qw/DBIx::Class::Storage::DBI::MultiDistinctEmulation/;
30
31 # __PACKAGE__->load_components(qw/PK::Auto/);
32
33 sub _dbh_last_insert_id {
34   my ($self, $dbh, $source, @columns) = @_;
35   my @ids = ();
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 );
39     push @ids, $id;
40   }
41   return @ids;
42 }
43
44 sub _dbh_get_autoinc_seq {
45   my ($self, $dbh, $source, $col) = @_;
46
47   # look up the correct sequence automatically
48   my $sql = q{
49     SELECT trigger_body FROM ALL_TRIGGERS t
50     WHERE t.table_name = ?
51     AND t.triggering_event = 'INSERT'
52     AND t.status = 'ENABLED'
53   };
54
55   # trigger_body is a LONG
56   $dbh->{LongReadLen} = 64 * 1024 if ($dbh->{LongReadLen} < 64 * 1024);
57
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???
62   }
63   $self->throw_exception("Unable to find a sequence INSERT trigger on table '" . $source->name . "'.");
64 }
65
66 =head2 insert
67
68 Fetch nextval from sequence and handle insert statement.
69
70 =cut
71
72 sub insert {
73   my ( $self, $source, $to_insert ) = @_;
74   foreach my $col ( $source->columns ) {
75     if ( !defined $to_insert->{$col} ) {
76       my $col_info = $source->column_info($col);
77
78       if ( $col_info->{auto_nextval} ) {
79         $to_insert->{$col} = $self->_sequence_fetch( 'nextval', $col_info->{sequence} || $self->_dbh_get_autoinc_seq($self->dbh, $source) );
80       }
81     }
82   }
83   $self->next::method( $source, $to_insert );
84 }
85
86 sub _sequence_fetch {
87   my ( $self, $type, $seq ) = @_;
88   my ($id) = $self->dbh->selectrow_array("SELECT ${seq}.${type} FROM DUAL");
89   return $id;
90 }
91
92 =head2 get_autoinc_seq
93
94 Returns the sequence name for an autoincrement column
95
96 =cut
97
98 sub get_autoinc_seq {
99   my ($self, $source, $col) = @_;
100     
101   $self->dbh_do($self->can('_dbh_get_autoinc_seq'), $source, $col);
102 }
103
104 =head2 columns_info_for
105
106 This wraps the superclass version of this method to force table
107 names to uppercase
108
109 =cut
110
111 sub columns_info_for {
112   my ($self, $table) = @_;
113
114   $self->next::method(uc($table));
115 }
116
117 =head2 datetime_parser_type
118
119 This sets the proper DateTime::Format module for use with
120 L<DBIx::Class::InflateColumn::DateTime>.
121
122 =cut
123
124 sub datetime_parser_type { return "DateTime::Format::Oracle"; }
125
126 =head1 AUTHORS
127
128 Andy Grundman <andy@hybridized.org>
129
130 Scott Connelly <scottsweep@yahoo.com>
131
132 =head1 LICENSE
133
134 You may distribute this code under the same terms as Perl itself.
135
136 =cut
137
138 1;