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