Commit | Line | Data |
d87d939a |
1 | package DBIx::Class::Schema::Loader::DBI::Oracle; |
e7262300 |
2 | |
3 | use strict; |
4 | use warnings; |
383bd2a8 |
5 | use base 'DBIx::Class::Schema::Loader::DBI::Component::QuotedDefault'; |
942bd5e0 |
6 | use mro 'c3'; |
1af21646 |
7 | use Try::Tiny; |
8 | use namespace::clean; |
e7262300 |
9 | |
c34033b1 |
10 | our $VERSION = '0.07032'; |
e7262300 |
11 | |
12 | =head1 NAME |
13 | |
3b61a7ca |
14 | DBIx::Class::Schema::Loader::DBI::Oracle - DBIx::Class::Schema::Loader::DBI |
e7262300 |
15 | Oracle Implementation. |
16 | |
e7262300 |
17 | =head1 DESCRIPTION |
18 | |
c4a69b87 |
19 | See L<DBIx::Class::Schema::Loader> and L<DBIx::Class::Schema::Loader::Base>. |
e7262300 |
20 | |
e7262300 |
21 | =cut |
22 | |
d0e184e9 |
23 | sub _setup { |
24 | my $self = shift; |
25 | |
26 | $self->next::method(@_); |
27 | |
c4a69b87 |
28 | my ($current_schema) = $self->dbh->selectrow_array('SELECT USER FROM DUAL'); |
46065bcb |
29 | |
c4a69b87 |
30 | $self->db_schema([ $current_schema ]) unless $self->db_schema; |
46065bcb |
31 | |
c4a69b87 |
32 | if (@{ $self->db_schema } == 1 && $self->db_schema->[0] ne '%' |
33 | && lc($self->db_schema->[0]) ne lc($current_schema)) { |
34 | $self->dbh->do('ALTER SESSION SET current_schema=' . $self->db_schema->[0]); |
46065bcb |
35 | } |
bc1cb85e |
36 | |
37 | if (not defined $self->preserve_case) { |
38 | $self->preserve_case(0); |
39 | } |
3785bc2e |
40 | elsif ($self->preserve_case) { |
c930f78b |
41 | $self->schema->storage->sql_maker->quote_char('"'); |
42 | $self->schema->storage->sql_maker->name_sep('.'); |
3785bc2e |
43 | } |
d0e184e9 |
44 | } |
45 | |
c4a69b87 |
46 | sub _build_name_sep { '.' } |
47 | |
48 | sub _system_schemas { |
49 | my $self = shift; |
e7262300 |
50 | |
c4a69b87 |
51 | # From http://www.adp-gmbh.ch/ora/misc/known_schemas.html |
52 | |
53 | return ($self->next::method(@_), qw/ANONYMOUS APEX_PUBLIC_USER APEX_030200 APPQOSSYS CTXSYS DBSNMP DIP DMSYS EXFSYS LBACSYS MDDATA MDSYS MGMT_VIEW OLAPSYS ORACLE_OCM ORDDATA ORDPLUGINS ORDSYS OUTLN SI_INFORMTN_SCHEMA SPATIAL_CSW_ADMIN_USR SPATIAL_WFS_ADMIN_USR SYS SYSMAN SYSTEM TRACESRV MTSSYS OASPUBLIC OWBSYS OWBSYS_AUDIT WEBSYS WK_PROXY WKSYS WK_TEST WMSYS XDB OSE$HTTP$ADMIN AURORA$JIS$UTILITY$ AURORA$ORB$UNAUTHENTICATED/, qr/^FLOWS_\d\d\d\d\d\d\z/); |
e7262300 |
54 | } |
55 | |
c4a69b87 |
56 | sub _system_tables { |
57 | my $self = shift; |
e7262300 |
58 | |
c4a69b87 |
59 | return ($self->next::method(@_), 'PLAN_TABLE'); |
60 | } |
e7262300 |
61 | |
c4a69b87 |
62 | sub _dbh_tables { |
63 | my ($self, $schema) = @_; |
e7262300 |
64 | |
c4a69b87 |
65 | return $self->dbh->tables(undef, $schema, '%', 'TABLE,VIEW'); |
66 | } |
e7262300 |
67 | |
c4a69b87 |
68 | sub _filter_tables { |
69 | my $self = shift; |
ffb03c96 |
70 | |
c4a69b87 |
71 | # silence a warning from older DBD::Oracles in tests |
72 | my $warn_handler = $SIG{__WARN__} || sub { warn @_ }; |
73 | local $SIG{__WARN__} = sub { |
74 | $warn_handler->(@_) |
75 | unless $_[0] =~ /^Field \d+ has an Oracle type \(\d+\) which is not explicitly supported/; |
76 | }; |
128f61d8 |
77 | |
c4a69b87 |
78 | return $self->next::method(@_); |
e7262300 |
79 | } |
80 | |
a40434df |
81 | sub _table_fk_info { |
82 | my $self = shift; |
83 | my ($table) = @_; |
84 | |
85 | my $rels = $self->next::method(@_); |
86 | |
87 | my $deferrable_sth = $self->dbh->prepare_cached(<<'EOF'); |
88 | select deferrable from all_constraints |
89 | where owner = ? and table_name = ? and constraint_name = ? |
90 | EOF |
91 | |
92 | foreach my $rel (@$rels) { |
93 | # Oracle does not have update rules |
94 | $rel->{attrs}{on_update} = 'NO ACTION';; |
95 | |
96 | # DBD::Oracle's foreign_key_info does not return DEFERRABILITY, so we get it ourselves |
97 | my ($deferrable) = $self->dbh->selectrow_array( |
98 | $deferrable_sth, undef, $table->schema, $table->name, $rel->{_constraint_name} |
99 | ); |
100 | |
101 | $rel->{attrs}{is_deferrable} = $deferrable && $deferrable =~ /^DEFERRABLE/i ? 1 : 0; |
102 | } |
103 | |
104 | return $rels; |
105 | } |
106 | |
e7262300 |
107 | sub _table_uniq_info { |
108 | my ($self, $table) = @_; |
109 | |
c4a69b87 |
110 | my $sth = $self->dbh->prepare_cached(<<'EOF', {}, 1); |
5975bbe6 |
111 | SELECT ac.constraint_name, acc.column_name |
112 | FROM all_constraints ac, all_cons_columns acc |
113 | WHERE acc.table_name=? AND acc.owner = ? |
114 | AND ac.table_name = acc.table_name AND ac.owner = acc.owner |
115 | AND acc.constraint_name = ac.constraint_name |
116 | AND ac.constraint_type='U' |
c4a69b87 |
117 | ORDER BY acc.position |
118 | EOF |
e7262300 |
119 | |
c4a69b87 |
120 | $sth->execute($table->name, $table->schema); |
e7262300 |
121 | |
e7262300 |
122 | my %constr_names; |
c4a69b87 |
123 | |
e7262300 |
124 | while(my $constr = $sth->fetchrow_arrayref) { |
f28e7eae |
125 | my $constr_name = $self->_lc($constr->[0]); |
3785bc2e |
126 | my $constr_col = $self->_lc($constr->[1]); |
3785bc2e |
127 | push @{$constr_names{$constr_name}}, $constr_col; |
e7262300 |
128 | } |
3b61a7ca |
129 | |
65ab592d |
130 | my @uniqs = map { [ $_ => $constr_names{$_} ] } keys %constr_names; |
e7262300 |
131 | return \@uniqs; |
132 | } |
133 | |
4cd5155b |
134 | sub _table_comment { |
ea998e8e |
135 | my $self = shift; |
136 | my ($table) = @_; |
137 | |
138 | my $table_comment = $self->next::method(@_); |
139 | |
140 | return $table_comment if $table_comment; |
141 | |
c4a69b87 |
142 | ($table_comment) = $self->dbh->selectrow_array(<<'EOF', {}, $table->schema, $table->name); |
143 | SELECT comments FROM all_tab_comments |
3b61a7ca |
144 | WHERE owner = ? |
c4a69b87 |
145 | AND table_name = ? |
146 | AND (table_type = 'TABLE' OR table_type = 'VIEW') |
147 | EOF |
4cd5155b |
148 | |
149 | return $table_comment |
150 | } |
151 | |
152 | sub _column_comment { |
ea998e8e |
153 | my $self = shift; |
154 | my ($table, $column_number, $column_name) = @_; |
155 | |
156 | my $column_comment = $self->next::method(@_); |
157 | |
158 | return $column_comment if $column_comment; |
159 | |
c4a69b87 |
160 | ($column_comment) = $self->dbh->selectrow_array(<<'EOF', {}, $table->schema, $table->name, $self->_uc($column_name)); |
161 | SELECT comments FROM all_col_comments |
3b61a7ca |
162 | WHERE owner = ? |
c4a69b87 |
163 | AND table_name = ? |
164 | AND column_name = ? |
165 | EOF |
e7262300 |
166 | |
c4a69b87 |
167 | return $column_comment |
65ab592d |
168 | } |
169 | |
170 | sub _columns_info_for { |
c4a69b87 |
171 | my $self = shift; |
172 | my ($table) = @_; |
fb328d1a |
173 | |
c4a69b87 |
174 | my $result = $self->next::method(@_); |
fb328d1a |
175 | |
1af21646 |
176 | local $self->dbh->{LongReadLen} = 1_000_000; |
c4a69b87 |
177 | local $self->dbh->{LongTruncOk} = 1; |
5cd600fa |
178 | |
c4a69b87 |
179 | my $sth = $self->dbh->prepare_cached(<<'EOF', {}, 1); |
5975bbe6 |
180 | SELECT trigger_body |
181 | FROM all_triggers |
182 | WHERE table_name = ? AND table_owner = ? |
760fd65c |
183 | AND upper(trigger_type) LIKE '%BEFORE EACH ROW%' AND lower(triggering_event) LIKE '%insert%' |
c4a69b87 |
184 | EOF |
760fd65c |
185 | |
5975bbe6 |
186 | $sth->execute($table->name, $table->schema); |
760fd65c |
187 | |
5975bbe6 |
188 | while (my ($trigger_body) = $sth->fetchrow_array) { |
189 | if (my ($seq_schema, $seq_name) = $trigger_body =~ /(?:\."?(\w+)"?)?"?(\w+)"?\.nextval/i) { |
190 | if (my ($col_name) = $trigger_body =~ /:new\.(\w+)/i) { |
191 | $col_name = $self->_lc($col_name); |
5cd600fa |
192 | |
5975bbe6 |
193 | $result->{$col_name}{is_auto_increment} = 1; |
5cd600fa |
194 | |
5975bbe6 |
195 | $seq_schema = $self->_lc($seq_schema || $table->schema); |
196 | $seq_name = $self->_lc($seq_name); |
5cd600fa |
197 | |
5975bbe6 |
198 | $result->{$col_name}{sequence} = ($self->qualify_objects ? ($seq_schema . '.') : '') . $seq_name; |
199 | } |
5cd600fa |
200 | } |
760fd65c |
201 | } |
202 | |
203 | while (my ($col, $info) = each %$result) { |
204 | no warnings 'uninitialized'; |
205 | |
1af21646 |
206 | my $sth = $self->dbh->prepare_cached(<<'EOF', {}, 1); |
207 | SELECT data_type, data_length |
208 | FROM all_tab_columns |
209 | WHERE column_name = ? AND table_name = ? AND owner = ? |
210 | EOF |
211 | $sth->execute($self->_uc($col), $table->name, $table->schema); |
212 | my ($data_type, $data_length) = $sth->fetchrow_array; |
213 | $sth->finish; |
214 | $data_type = lc $data_type; |
215 | |
216 | if ($data_type =~ /^(?:n(?:var)?char2?|u?rowid|nclob|timestamp\(\d+\)(?: with(?: local)? time zone)?|binary_(?:float|double))\z/i) { |
217 | $info->{data_type} = $data_type; |
218 | |
219 | if ($data_type =~ /^u?rowid\z/i) { |
220 | $info->{size} = $data_length; |
221 | } |
222 | } |
223 | |
760fd65c |
224 | if ($info->{data_type} =~ /^(?:n?[cb]lob|long(?: raw)?|bfile|date|binary_(?:float|double)|rowid)\z/i) { |
225 | delete $info->{size}; |
226 | } |
227 | |
228 | if ($info->{data_type} =~ /^n(?:var)?char2?\z/i) { |
1af21646 |
229 | if (ref $info->{size}) { |
230 | $info->{size} = $info->{size}[0] / 8; |
231 | } |
232 | else { |
233 | $info->{size} = $info->{size} / 2; |
234 | } |
235 | } |
236 | elsif ($info->{data_type} =~ /^(?:var)?char2?\z/i) { |
237 | if (ref $info->{size}) { |
238 | $info->{size} = $info->{size}[0]; |
239 | } |
760fd65c |
240 | } |
1af21646 |
241 | elsif (lc($info->{data_type}) =~ /^(?:number|decimal)\z/i) { |
4ea15dfe |
242 | $info->{original}{data_type} = 'number'; |
243 | $info->{data_type} = 'numeric'; |
760fd65c |
244 | |
1af21646 |
245 | if (try { $info->{size}[0] == 38 && $info->{size}[1] == 0 }) { |
4ea15dfe |
246 | $info->{original}{size} = $info->{size}; |
247 | |
760fd65c |
248 | $info->{data_type} = 'integer'; |
249 | delete $info->{size}; |
250 | } |
251 | } |
252 | elsif (my ($precision) = $info->{data_type} =~ /^timestamp\((\d+)\)(?: with (?:local )?time zone)?\z/i) { |
253 | $info->{data_type} = join ' ', $info->{data_type} =~ /[a-z]+/ig; |
254 | |
255 | if ($precision == 6) { |
256 | delete $info->{size}; |
257 | } |
258 | else { |
259 | $info->{size} = $precision; |
260 | } |
261 | } |
1af21646 |
262 | elsif ($info->{data_type} =~ /timestamp/i && ref $info->{size} && $info->{size}[0] == 0) { |
263 | my $size = $info->{size}[1]; |
264 | delete $info->{size}; |
265 | $info->{size} = $size unless $size == 6; |
266 | } |
760fd65c |
267 | elsif (($precision) = $info->{data_type} =~ /^interval year\((\d+)\) to month\z/i) { |
268 | $info->{data_type} = join ' ', $info->{data_type} =~ /[a-z]+/ig; |
269 | |
270 | if ($precision == 2) { |
271 | delete $info->{size}; |
272 | } |
273 | else { |
274 | $info->{size} = $precision; |
275 | } |
276 | } |
277 | elsif (my ($day_precision, $second_precision) = $info->{data_type} =~ /^interval day\((\d+)\) to second\((\d+)\)\z/i) { |
278 | $info->{data_type} = join ' ', $info->{data_type} =~ /[a-z]+/ig; |
279 | |
280 | if ($day_precision == 2 && $second_precision == 6) { |
281 | delete $info->{size}; |
282 | } |
283 | else { |
284 | $info->{size} = [ $day_precision, $second_precision ]; |
285 | } |
286 | } |
1af21646 |
287 | elsif ($info->{data_type} =~ /^interval year to month\z/i && ref $info->{size}) { |
288 | my $precision = $info->{size}[0]; |
289 | |
290 | if ($precision == 2) { |
291 | delete $info->{size}; |
292 | } |
293 | else { |
294 | $info->{size} = $precision; |
295 | } |
296 | } |
297 | elsif ($info->{data_type} =~ /^interval day to second\z/i && ref $info->{size}) { |
298 | if ($info->{size}[0] == 2 && $info->{size}[1] == 6) { |
299 | delete $info->{size}; |
300 | } |
301 | } |
1c22571b |
302 | elsif (lc($info->{data_type}) eq 'float') { |
303 | $info->{original}{data_type} = 'float'; |
304 | $info->{original}{size} = $info->{size}; |
305 | |
306 | if ($info->{size} <= 63) { |
307 | $info->{data_type} = 'real'; |
308 | } |
309 | else { |
310 | $info->{data_type} = 'double precision'; |
311 | } |
312 | delete $info->{size}; |
313 | } |
1af21646 |
314 | elsif (lc($info->{data_type}) eq 'double precision') { |
315 | $info->{original}{data_type} = 'float'; |
316 | |
317 | my $size = try { $info->{size}[0] }; |
318 | |
319 | $info->{original}{size} = $size; |
320 | |
321 | if ($size <= 63) { |
322 | $info->{data_type} = 'real'; |
323 | } |
324 | delete $info->{size}; |
325 | } |
760fd65c |
326 | elsif (lc($info->{data_type}) eq 'urowid' && $info->{size} == 4000) { |
327 | delete $info->{size}; |
328 | } |
1af21646 |
329 | elsif ($info->{data_type} eq '-9104') { |
330 | $info->{data_type} = 'rowid'; |
331 | delete $info->{size}; |
332 | } |
333 | elsif ($info->{data_type} eq '-2') { |
334 | $info->{data_type} = 'raw'; |
335 | $info->{size} = try { $info->{size}[0] / 2 }; |
336 | } |
3785bc2e |
337 | elsif (lc($info->{data_type}) eq 'date') { |
338 | $info->{data_type} = 'datetime'; |
339 | $info->{original}{data_type} = 'date'; |
340 | } |
1c22571b |
341 | elsif (lc($info->{data_type}) eq 'binary_float') { |
342 | $info->{data_type} = 'real'; |
343 | $info->{original}{data_type} = 'binary_float'; |
3b61a7ca |
344 | } |
1c22571b |
345 | elsif (lc($info->{data_type}) eq 'binary_double') { |
346 | $info->{data_type} = 'double precision'; |
347 | $info->{original}{data_type} = 'binary_double'; |
1af21646 |
348 | } |
349 | |
350 | # DEFAULT could be missed by ::DBI because of ORA-24345 |
351 | if (not defined $info->{default_value}) { |
352 | local $self->dbh->{LongReadLen} = 1_000_000; |
353 | local $self->dbh->{LongTruncOk} = 1; |
354 | my $sth = $self->dbh->prepare_cached(<<'EOF', {}, 1); |
355 | SELECT data_default |
356 | FROM all_tab_columns |
357 | WHERE column_name = ? AND table_name = ? AND owner = ? |
358 | EOF |
359 | $sth->execute($self->_uc($col), $table->name, $table->schema); |
360 | my ($default) = $sth->fetchrow_array; |
361 | $sth->finish; |
362 | |
363 | # this is mostly copied from ::DBI::QuotedDefault |
364 | if (defined $default) { |
365 | s/^\s+//, s/\s+\z// for $default; |
366 | |
367 | if ($default =~ /^'(.*?)'\z/) { |
368 | $info->{default_value} = $1; |
369 | } |
370 | elsif ($default =~ /^(-?\d.*?)\z/) { |
371 | $info->{default_value} = $1; |
372 | } |
373 | elsif ($default =~ /^NULL\z/i) { |
374 | my $null = 'null'; |
375 | $info->{default_value} = \$null; |
376 | } |
377 | elsif ($default ne '') { |
378 | my $val = $default; |
379 | $info->{default_value} = \$val; |
380 | } |
381 | } |
382 | } |
760fd65c |
383 | |
1af21646 |
384 | if ((try { lc(${ $info->{default_value} }) }||'') eq 'sysdate') { |
3785bc2e |
385 | my $current_timestamp = 'current_timestamp'; |
386 | $info->{default_value} = \$current_timestamp; |
701cd3e3 |
387 | |
388 | my $sysdate = 'sysdate'; |
389 | $info->{original}{default_value} = \$sysdate; |
760fd65c |
390 | } |
fb328d1a |
391 | } |
392 | |
760fd65c |
393 | return $result; |
fb328d1a |
394 | } |
395 | |
1af21646 |
396 | sub _dbh_column_info { |
397 | my $self = shift; |
398 | my ($dbh) = @_; |
399 | |
400 | # try to avoid ORA-24345 |
401 | local $dbh->{LongReadLen} = 1_000_000; |
402 | local $dbh->{LongTruncOk} = 1; |
403 | |
404 | return $self->next::method(@_); |
405 | } |
406 | |
e7262300 |
407 | =head1 SEE ALSO |
408 | |
409 | L<DBIx::Class::Schema::Loader>, L<DBIx::Class::Schema::Loader::Base>, |
410 | L<DBIx::Class::Schema::Loader::DBI> |
411 | |
412 | =head1 AUTHOR |
413 | |
9cc8e7e1 |
414 | See L<DBIx::Class::Schema::Loader/AUTHOR> and L<DBIx::Class::Schema::Loader/CONTRIBUTORS>. |
e7262300 |
415 | |
be80bba7 |
416 | =head1 LICENSE |
417 | |
418 | This library is free software; you can redistribute it and/or modify it under |
419 | the same terms as Perl itself. |
fb328d1a |
420 | |
e7262300 |
421 | =cut |
422 | |
423 | 1; |
760fd65c |
424 | # vim:et sts=4 sw=4 tw=0: |