2e9a8c1faa7ed0dee92eaa264eb985f00a45ca8a
[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::Generic - 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;
59
60   # check for fully-qualified name (eg. SCHEMA.TABLENAME)
61   if ( my ( $schema, $table ) = $source->name =~ /(\w+)\.(\w+)/ ) {
62     $sql = q{
63       SELECT trigger_body FROM ALL_TRIGGERS t
64       WHERE t.owner = ? AND t.table_name = ?
65       AND t.triggering_event = 'INSERT'
66       AND t.status = 'ENABLED'
67     };
68     $sth = $dbh->prepare($sql);
69     $sth->execute( uc($schema), uc($table) );
70   }
71   else {
72     $sth = $dbh->prepare($sql);
73     $sth->execute( uc( $source->name ) );
74   }
75   while (my ($insert_trigger) = $sth->fetchrow_array) {
76     return uc($1) if $insert_trigger =~ m!(\w+)\.nextval!i; # col name goes here???
77   }
78   $self->throw_exception("Unable to find a sequence INSERT trigger on table '" . $source->name . "'.");
79 }
80
81 sub _sequence_fetch {
82   my ( $self, $type, $seq ) = @_;
83   my ($id) = $self->dbh->selectrow_array("SELECT ${seq}.${type} FROM DUAL");
84   return $id;
85 }
86
87 =head2 connected
88
89 Returns true if we have an open (and working) database connection, false if it is not (yet)
90 open (or does not work). (Executes a simple SELECT to make sure it works.)
91
92 The reason this is needed is that L<DBD::Oracle>'s ping() does not do a real
93 OCIPing but just gets the server version, which doesn't help if someone killed
94 your session.
95
96 =cut
97
98 sub connected {
99   my $self = shift;
100
101   if (not $self->SUPER::connected(@_)) {
102     return 0;
103   }
104   else {
105     my $dbh = $self->_dbh;
106
107     local $dbh->{RaiseError} = 1;
108
109     eval {
110       my $ping_sth = $dbh->prepare_cached("select 1 from dual");
111       $ping_sth->execute;
112       $ping_sth->finish;
113     };
114
115     return $@ ? 0 : 1;
116   }
117 }
118
119 sub _dbh_execute {
120   my $self = shift;
121   my ($dbh, $op, $extra_bind, $ident, $bind_attributes, @args) = @_;
122
123   my $wantarray = wantarray;
124
125   my (@res, $exception, $retried);
126
127   RETRY: {
128     do {
129       eval {
130         if ($wantarray) {
131           @res    = $self->SUPER::_dbh_execute(@_);
132         } else {
133           $res[0] = $self->SUPER::_dbh_execute(@_);
134         }
135       };
136       $exception = $@;
137       if ($exception =~ /ORA-01003/) {
138         # ORA-01003: no statement parsed (someone changed the table somehow,
139         # invalidating your cursor.)
140         my ($sql, $bind) = $self->_prep_for_execute($op, $extra_bind, $ident, \@args);
141         delete $dbh->{CachedKids}{$sql};
142       } else {
143         last RETRY;
144       }
145     } while (not $retried++);
146   }
147
148   $self->throw_exception($exception) if $exception;
149
150   wantarray ? @res : $res[0]
151 }
152
153 =head2 get_autoinc_seq
154
155 Returns the sequence name for an autoincrement column
156
157 =cut
158
159 sub get_autoinc_seq {
160   my ($self, $source, $col) = @_;
161     
162   $self->dbh_do('_dbh_get_autoinc_seq', $source, $col);
163 }
164
165 =head2 columns_info_for
166
167 This wraps the superclass version of this method to force table
168 names to uppercase
169
170 =cut
171
172 sub columns_info_for {
173   my ($self, $table) = @_;
174
175   $self->next::method(uc($table));
176 }
177
178 =head2 datetime_parser_type
179
180 This sets the proper DateTime::Format module for use with
181 L<DBIx::Class::InflateColumn::DateTime>.
182
183 =cut
184
185 sub datetime_parser_type { return "DateTime::Format::Oracle"; }
186
187 sub _svp_begin {
188     my ($self, $name) = @_;
189  
190     $self->dbh->do("SAVEPOINT $name");
191 }
192
193 # Oracle automatically releases a savepoint when you start another one with the
194 # same name.
195 sub _svp_release { 1 }
196
197 sub _svp_rollback {
198     my ($self, $name) = @_;
199
200     $self->dbh->do("ROLLBACK TO SAVEPOINT $name")
201 }
202
203 =head1 AUTHORS
204
205 Andy Grundman <andy@hybridized.org>
206
207 Scott Connelly <scottsweep@yahoo.com>
208
209 =head1 LICENSE
210
211 You may distribute this code under the same terms as Perl itself.
212
213 =cut
214
215 1;