* Added new test subs (is_same_sql, is_same_bind) and new predicate sub (eq_sql_bind...
[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
92bc2a19 9DBIx::Class::Storage::DBI::Oracle::Generic - Automatic primary key class for Oracle
7137528d 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
cb464582 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 }
18360aed 75 while (my ($insert_trigger) = $sth->fetchrow_array) {
76 return uc($1) if $insert_trigger =~ m!(\w+)\.nextval!i; # col name goes here???
77 }
66cab05c 78 $self->throw_exception("Unable to find a sequence INSERT trigger on table '" . $source->name . "'.");
18360aed 79}
80
2e46b6eb 81sub _sequence_fetch {
82 my ( $self, $type, $seq ) = @_;
83 my ($id) = $self->dbh->selectrow_array("SELECT ${seq}.${type} FROM DUAL");
84 return $id;
85}
86
20f27051 87=head2 connected
88
89Returns true if we have an open (and working) database connection, false if it is not (yet)
90open (or does not work). (Executes a simple SELECT to make sure it works.)
91
92=cut
93
c2481821 94sub connected {
95 my $self = shift;
7ba7a57d 96
c2d7baef 97 if (not $self->SUPER::connected(@_)) {
98 return 0;
99 }
100 else {
7ba7a57d 101 my $dbh = $self->_dbh;
102
7ba7a57d 103 local $dbh->{RaiseError} = 1;
c2d7baef 104
7ba7a57d 105 eval {
c2d7baef 106 my $ping_sth = $dbh->prepare_cached("select 1 from dual");
7ba7a57d 107 $ping_sth->execute;
108 $ping_sth->finish;
109 };
110
c2d7baef 111 return $@ ? 0 : 1;
c2481821 112 }
c2481821 113}
114
d789fa99 115sub _dbh_execute {
116 my $self = shift;
117 my ($dbh, $op, $extra_bind, $ident, $bind_attributes, @args) = @_;
118
119 my $wantarray = wantarray;
d789fa99 120
c2d7baef 121 my (@res, $exception, $retried);
122
123 do {
d789fa99 124 eval {
125 if ($wantarray) {
126 @res = $self->SUPER::_dbh_execute(@_);
127 } else {
128 $res[0] = $self->SUPER::_dbh_execute(@_);
129 }
130 };
131 $exception = $@;
c2481821 132 if ($exception =~ /ORA-01003/) {
c2d7baef 133 # ORA-01003: no statement parsed (someone changed the table somehow,
134 # invalidating your cursor.)
d789fa99 135 my ($sql, $bind) = $self->_prep_for_execute($op, $extra_bind, $ident, \@args);
136 delete $dbh->{CachedKids}{$sql};
137 } else {
138 last;
139 }
c2d7baef 140 } while (not $retried++);
d789fa99 141
142 $self->throw_exception($exception) if $exception;
143
144 wantarray ? @res : $res[0]
145}
146
7137528d 147=head2 get_autoinc_seq
148
149Returns the sequence name for an autoincrement column
150
151=cut
152
18360aed 153sub get_autoinc_seq {
154 my ($self, $source, $col) = @_;
155
373940e1 156 $self->dbh_do('_dbh_get_autoinc_seq', $source, $col);
18360aed 157}
158
7137528d 159=head2 columns_info_for
160
161This wraps the superclass version of this method to force table
162names to uppercase
163
164=cut
165
18360aed 166sub columns_info_for {
167 my ($self, $table) = @_;
168
169 $self->next::method(uc($table));
170}
171
8f7e044c 172=head2 datetime_parser_type
173
174This sets the proper DateTime::Format module for use with
175L<DBIx::Class::InflateColumn::DateTime>.
176
177=cut
178
179sub datetime_parser_type { return "DateTime::Format::Oracle"; }
180
281719d2 181sub _svp_begin {
182 my ($self, $name) = @_;
183
184 $self->dbh->do("SAVEPOINT $name");
185}
186
187# Oracle automatically releases a savepoint when you start another one with the
188# same name.
189sub _svp_release { 1 }
190
191sub _svp_rollback {
192 my ($self, $name) = @_;
193
194 $self->dbh->do("ROLLBACK TO SAVEPOINT $name")
195}
196
18360aed 197=head1 AUTHORS
198
199Andy Grundman <andy@hybridized.org>
200
201Scott Connelly <scottsweep@yahoo.com>
202
203=head1 LICENSE
204
205You may distribute this code under the same terms as Perl itself.
206
207=cut
7137528d 208
2091;