Merge 'trunk' into 'replication_dedux'
[dbsrgits/DBIx-Class.git] / lib / DBIx / Class / Storage / DBI / Oracle / Generic.pm
CommitLineData
18360aed 1package DBIx::Class::Storage::DBI::Oracle::Generic;
e21dfd6a 2# -*- mode: cperl; cperl-indent-level: 2 -*-
18360aed 3
4use strict;
5use warnings;
6
7137528d 7=head1 NAME
8
9DBIx::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/);
2e46b6eb 15 __PACKAGE__->add_columns({ id => { sequence => 'mysequence', auto_nextval => 1 } });
7137528d 16 __PACKAGE__->set_primary_key('id');
17 __PACKAGE__->sequence('mysequence');
18
19=head1 DESCRIPTION
20
21This class implements autoincrements for Oracle.
22
23=head1 METHODS
24
25=cut
26
18360aed 27use Carp::Clan qw/^DBIx::Class/;
28
29use base qw/DBIx::Class::Storage::DBI::MultiDistinctEmulation/;
30
31# __PACKAGE__->load_components(qw/PK::Auto/);
32
33sub _dbh_last_insert_id {
2e46b6eb 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;
18360aed 42}
43
44sub _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 }
66cab05c 63 $self->throw_exception("Unable to find a sequence INSERT trigger on table '" . $source->name . "'.");
18360aed 64}
65
2e46b6eb 66sub _sequence_fetch {
67 my ( $self, $type, $seq ) = @_;
68 my ($id) = $self->dbh->selectrow_array("SELECT ${seq}.${type} FROM DUAL");
69 return $id;
70}
71
7137528d 72=head2 get_autoinc_seq
73
74Returns the sequence name for an autoincrement column
75
76=cut
77
18360aed 78sub get_autoinc_seq {
79 my ($self, $source, $col) = @_;
80
373940e1 81 $self->dbh_do('_dbh_get_autoinc_seq', $source, $col);
18360aed 82}
83
7137528d 84=head2 columns_info_for
85
86This wraps the superclass version of this method to force table
87names to uppercase
88
89=cut
90
18360aed 91sub columns_info_for {
92 my ($self, $table) = @_;
93
94 $self->next::method(uc($table));
95}
96
8f7e044c 97=head2 datetime_parser_type
98
99This sets the proper DateTime::Format module for use with
100L<DBIx::Class::InflateColumn::DateTime>.
101
102=cut
103
104sub datetime_parser_type { return "DateTime::Format::Oracle"; }
105
18360aed 106=head1 AUTHORS
107
108Andy Grundman <andy@hybridized.org>
109
110Scott Connelly <scottsweep@yahoo.com>
111
112=head1 LICENSE
113
114You may distribute this code under the same terms as Perl itself.
115
116=cut
7137528d 117
1181;