Commit | Line | Data |
18360aed |
1 | package DBIx::Class::Storage::DBI::Oracle::Generic; |
e21dfd6a |
2 | # -*- mode: cperl; cperl-indent-level: 2 -*- |
18360aed |
3 | |
4 | use strict; |
5 | use warnings; |
6 | |
7137528d |
7 | =head1 NAME |
8 | |
92bc2a19 |
9 | DBIx::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 | |
21 | This class implements autoincrements for Oracle. |
22 | |
23 | =head1 METHODS |
24 | |
25 | =cut |
26 | |
18360aed |
27 | use Carp::Clan qw/^DBIx::Class/; |
c2481821 |
28 | use Scalar::Util (); |
18360aed |
29 | |
30 | use base qw/DBIx::Class::Storage::DBI::MultiDistinctEmulation/; |
31 | |
32 | # __PACKAGE__->load_components(qw/PK::Auto/); |
33 | |
34 | sub _dbh_last_insert_id { |
2e46b6eb |
35 | my ($self, $dbh, $source, @columns) = @_; |
36 | my @ids = (); |
37 | foreach my $col (@columns) { |
38 | my $seq = ($source->column_info($col)->{sequence} ||= $self->get_autoinc_seq($source,$col)); |
39 | my $id = $self->_sequence_fetch( 'currval', $seq ); |
40 | push @ids, $id; |
41 | } |
42 | return @ids; |
18360aed |
43 | } |
44 | |
45 | sub _dbh_get_autoinc_seq { |
46 | my ($self, $dbh, $source, $col) = @_; |
47 | |
48 | # look up the correct sequence automatically |
49 | my $sql = q{ |
50 | SELECT trigger_body FROM ALL_TRIGGERS t |
51 | WHERE t.table_name = ? |
52 | AND t.triggering_event = 'INSERT' |
53 | AND t.status = 'ENABLED' |
54 | }; |
55 | |
56 | # trigger_body is a LONG |
57 | $dbh->{LongReadLen} = 64 * 1024 if ($dbh->{LongReadLen} < 64 * 1024); |
58 | |
cb464582 |
59 | my $sth; |
60 | |
61 | # check for fully-qualified name (eg. SCHEMA.TABLENAME) |
62 | if ( my ( $schema, $table ) = $source->name =~ /(\w+)\.(\w+)/ ) { |
63 | $sql = q{ |
64 | SELECT trigger_body FROM ALL_TRIGGERS t |
65 | WHERE t.owner = ? AND t.table_name = ? |
66 | AND t.triggering_event = 'INSERT' |
67 | AND t.status = 'ENABLED' |
68 | }; |
69 | $sth = $dbh->prepare($sql); |
70 | $sth->execute( uc($schema), uc($table) ); |
71 | } |
72 | else { |
73 | $sth = $dbh->prepare($sql); |
74 | $sth->execute( uc( $source->name ) ); |
75 | } |
18360aed |
76 | while (my ($insert_trigger) = $sth->fetchrow_array) { |
77 | return uc($1) if $insert_trigger =~ m!(\w+)\.nextval!i; # col name goes here??? |
78 | } |
66cab05c |
79 | $self->throw_exception("Unable to find a sequence INSERT trigger on table '" . $source->name . "'."); |
18360aed |
80 | } |
81 | |
2e46b6eb |
82 | sub _sequence_fetch { |
83 | my ( $self, $type, $seq ) = @_; |
84 | my ($id) = $self->dbh->selectrow_array("SELECT ${seq}.${type} FROM DUAL"); |
85 | return $id; |
86 | } |
87 | |
c2481821 |
88 | sub connected { |
89 | my $self = shift; |
90 | |
91 | if ($self->SUPER::connected(@_)) { |
92 | my $dbh = $self->_dbh; |
93 | |
94 | my $ping_sth = $dbh->prepare_cached("select 1 from dual"); |
95 | |
96 | local $dbh->{RaiseError} = 1; |
97 | eval { |
98 | $ping_sth->execute; |
99 | $ping_sth->finish; |
100 | }; |
101 | |
102 | if ($@) { |
103 | return 0; |
104 | } else { |
105 | return 1; |
106 | } |
107 | } |
108 | |
109 | return 0; |
110 | } |
111 | |
d789fa99 |
112 | sub _dbh_execute { |
113 | my $self = shift; |
114 | my ($dbh, $op, $extra_bind, $ident, $bind_attributes, @args) = @_; |
115 | |
116 | my $wantarray = wantarray; |
117 | my @res; |
118 | my $exception; |
119 | |
120 | my $try = 2; |
121 | |
122 | while ($try--) { |
123 | eval { |
124 | if ($wantarray) { |
125 | @res = $self->SUPER::_dbh_execute(@_); |
126 | } else { |
127 | $res[0] = $self->SUPER::_dbh_execute(@_); |
128 | } |
129 | }; |
130 | $exception = $@; |
c2481821 |
131 | if ($exception =~ /ORA-01003/) { |
132 | # ORA-01003: no statement parsed (someone changed the table somehow, |
d789fa99 |
133 | # invalidating your cursor.) |
134 | my ($sql, $bind) = $self->_prep_for_execute($op, $extra_bind, $ident, \@args); |
135 | delete $dbh->{CachedKids}{$sql}; |
136 | } else { |
137 | last; |
138 | } |
139 | } |
140 | |
141 | $self->throw_exception($exception) if $exception; |
142 | |
143 | wantarray ? @res : $res[0] |
144 | } |
145 | |
7137528d |
146 | =head2 get_autoinc_seq |
147 | |
148 | Returns the sequence name for an autoincrement column |
149 | |
150 | =cut |
151 | |
18360aed |
152 | sub get_autoinc_seq { |
153 | my ($self, $source, $col) = @_; |
154 | |
373940e1 |
155 | $self->dbh_do('_dbh_get_autoinc_seq', $source, $col); |
18360aed |
156 | } |
157 | |
7137528d |
158 | =head2 columns_info_for |
159 | |
160 | This wraps the superclass version of this method to force table |
161 | names to uppercase |
162 | |
163 | =cut |
164 | |
18360aed |
165 | sub columns_info_for { |
166 | my ($self, $table) = @_; |
167 | |
168 | $self->next::method(uc($table)); |
169 | } |
170 | |
8f7e044c |
171 | =head2 datetime_parser_type |
172 | |
173 | This sets the proper DateTime::Format module for use with |
174 | L<DBIx::Class::InflateColumn::DateTime>. |
175 | |
176 | =cut |
177 | |
178 | sub datetime_parser_type { return "DateTime::Format::Oracle"; } |
179 | |
281719d2 |
180 | sub _svp_begin { |
181 | my ($self, $name) = @_; |
182 | |
183 | $self->dbh->do("SAVEPOINT $name"); |
184 | } |
185 | |
186 | # Oracle automatically releases a savepoint when you start another one with the |
187 | # same name. |
188 | sub _svp_release { 1 } |
189 | |
190 | sub _svp_rollback { |
191 | my ($self, $name) = @_; |
192 | |
193 | $self->dbh->do("ROLLBACK TO SAVEPOINT $name") |
194 | } |
195 | |
18360aed |
196 | =head1 AUTHORS |
197 | |
198 | Andy Grundman <andy@hybridized.org> |
199 | |
200 | Scott Connelly <scottsweep@yahoo.com> |
201 | |
202 | =head1 LICENSE |
203 | |
204 | You may distribute this code under the same terms as Perl itself. |
205 | |
206 | =cut |
7137528d |
207 | |
208 | 1; |