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 | |
b95e25c9 |
10 | our $VERSION = '0.07026'; |
e7262300 |
11 | |
12 | =head1 NAME |
13 | |
14 | DBIx::Class::Schema::Loader::DBI::Oracle - DBIx::Class::Schema::Loader::DBI |
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 | |
81 | sub _table_uniq_info { |
82 | my ($self, $table) = @_; |
83 | |
c4a69b87 |
84 | my $sth = $self->dbh->prepare_cached(<<'EOF', {}, 1); |
5975bbe6 |
85 | SELECT ac.constraint_name, acc.column_name |
86 | FROM all_constraints ac, all_cons_columns acc |
87 | WHERE acc.table_name=? AND acc.owner = ? |
88 | AND ac.table_name = acc.table_name AND ac.owner = acc.owner |
89 | AND acc.constraint_name = ac.constraint_name |
90 | AND ac.constraint_type='U' |
c4a69b87 |
91 | ORDER BY acc.position |
92 | EOF |
e7262300 |
93 | |
c4a69b87 |
94 | $sth->execute($table->name, $table->schema); |
e7262300 |
95 | |
e7262300 |
96 | my %constr_names; |
c4a69b87 |
97 | |
e7262300 |
98 | while(my $constr = $sth->fetchrow_arrayref) { |
f28e7eae |
99 | my $constr_name = $self->_lc($constr->[0]); |
3785bc2e |
100 | my $constr_col = $self->_lc($constr->[1]); |
3785bc2e |
101 | push @{$constr_names{$constr_name}}, $constr_col; |
e7262300 |
102 | } |
65ab592d |
103 | |
104 | my @uniqs = map { [ $_ => $constr_names{$_} ] } keys %constr_names; |
e7262300 |
105 | return \@uniqs; |
106 | } |
107 | |
4cd5155b |
108 | sub _table_comment { |
ea998e8e |
109 | my $self = shift; |
110 | my ($table) = @_; |
111 | |
112 | my $table_comment = $self->next::method(@_); |
113 | |
114 | return $table_comment if $table_comment; |
115 | |
c4a69b87 |
116 | ($table_comment) = $self->dbh->selectrow_array(<<'EOF', {}, $table->schema, $table->name); |
117 | SELECT comments FROM all_tab_comments |
118 | WHERE owner = ? |
119 | AND table_name = ? |
120 | AND (table_type = 'TABLE' OR table_type = 'VIEW') |
121 | EOF |
4cd5155b |
122 | |
123 | return $table_comment |
124 | } |
125 | |
126 | sub _column_comment { |
ea998e8e |
127 | my $self = shift; |
128 | my ($table, $column_number, $column_name) = @_; |
129 | |
130 | my $column_comment = $self->next::method(@_); |
131 | |
132 | return $column_comment if $column_comment; |
133 | |
c4a69b87 |
134 | ($column_comment) = $self->dbh->selectrow_array(<<'EOF', {}, $table->schema, $table->name, $self->_uc($column_name)); |
135 | SELECT comments FROM all_col_comments |
136 | WHERE owner = ? |
137 | AND table_name = ? |
138 | AND column_name = ? |
139 | EOF |
e7262300 |
140 | |
c4a69b87 |
141 | return $column_comment |
65ab592d |
142 | } |
143 | |
144 | sub _columns_info_for { |
c4a69b87 |
145 | my $self = shift; |
146 | my ($table) = @_; |
fb328d1a |
147 | |
c4a69b87 |
148 | my $result = $self->next::method(@_); |
fb328d1a |
149 | |
1af21646 |
150 | local $self->dbh->{LongReadLen} = 1_000_000; |
c4a69b87 |
151 | local $self->dbh->{LongTruncOk} = 1; |
5cd600fa |
152 | |
c4a69b87 |
153 | my $sth = $self->dbh->prepare_cached(<<'EOF', {}, 1); |
5975bbe6 |
154 | SELECT trigger_body |
155 | FROM all_triggers |
156 | WHERE table_name = ? AND table_owner = ? |
760fd65c |
157 | AND upper(trigger_type) LIKE '%BEFORE EACH ROW%' AND lower(triggering_event) LIKE '%insert%' |
c4a69b87 |
158 | EOF |
760fd65c |
159 | |
5975bbe6 |
160 | $sth->execute($table->name, $table->schema); |
760fd65c |
161 | |
5975bbe6 |
162 | while (my ($trigger_body) = $sth->fetchrow_array) { |
163 | if (my ($seq_schema, $seq_name) = $trigger_body =~ /(?:\."?(\w+)"?)?"?(\w+)"?\.nextval/i) { |
164 | if (my ($col_name) = $trigger_body =~ /:new\.(\w+)/i) { |
165 | $col_name = $self->_lc($col_name); |
5cd600fa |
166 | |
5975bbe6 |
167 | $result->{$col_name}{is_auto_increment} = 1; |
5cd600fa |
168 | |
5975bbe6 |
169 | $seq_schema = $self->_lc($seq_schema || $table->schema); |
170 | $seq_name = $self->_lc($seq_name); |
5cd600fa |
171 | |
5975bbe6 |
172 | $result->{$col_name}{sequence} = ($self->qualify_objects ? ($seq_schema . '.') : '') . $seq_name; |
173 | } |
5cd600fa |
174 | } |
760fd65c |
175 | } |
176 | |
177 | while (my ($col, $info) = each %$result) { |
178 | no warnings 'uninitialized'; |
179 | |
1af21646 |
180 | my $sth = $self->dbh->prepare_cached(<<'EOF', {}, 1); |
181 | SELECT data_type, data_length |
182 | FROM all_tab_columns |
183 | WHERE column_name = ? AND table_name = ? AND owner = ? |
184 | EOF |
185 | $sth->execute($self->_uc($col), $table->name, $table->schema); |
186 | my ($data_type, $data_length) = $sth->fetchrow_array; |
187 | $sth->finish; |
188 | $data_type = lc $data_type; |
189 | |
190 | if ($data_type =~ /^(?:n(?:var)?char2?|u?rowid|nclob|timestamp\(\d+\)(?: with(?: local)? time zone)?|binary_(?:float|double))\z/i) { |
191 | $info->{data_type} = $data_type; |
192 | |
193 | if ($data_type =~ /^u?rowid\z/i) { |
194 | $info->{size} = $data_length; |
195 | } |
196 | } |
197 | |
760fd65c |
198 | if ($info->{data_type} =~ /^(?:n?[cb]lob|long(?: raw)?|bfile|date|binary_(?:float|double)|rowid)\z/i) { |
199 | delete $info->{size}; |
200 | } |
201 | |
202 | if ($info->{data_type} =~ /^n(?:var)?char2?\z/i) { |
1af21646 |
203 | if (ref $info->{size}) { |
204 | $info->{size} = $info->{size}[0] / 8; |
205 | } |
206 | else { |
207 | $info->{size} = $info->{size} / 2; |
208 | } |
209 | } |
210 | elsif ($info->{data_type} =~ /^(?:var)?char2?\z/i) { |
211 | if (ref $info->{size}) { |
212 | $info->{size} = $info->{size}[0]; |
213 | } |
760fd65c |
214 | } |
1af21646 |
215 | elsif (lc($info->{data_type}) =~ /^(?:number|decimal)\z/i) { |
4ea15dfe |
216 | $info->{original}{data_type} = 'number'; |
217 | $info->{data_type} = 'numeric'; |
760fd65c |
218 | |
1af21646 |
219 | if (try { $info->{size}[0] == 38 && $info->{size}[1] == 0 }) { |
4ea15dfe |
220 | $info->{original}{size} = $info->{size}; |
221 | |
760fd65c |
222 | $info->{data_type} = 'integer'; |
223 | delete $info->{size}; |
224 | } |
225 | } |
226 | elsif (my ($precision) = $info->{data_type} =~ /^timestamp\((\d+)\)(?: with (?:local )?time zone)?\z/i) { |
227 | $info->{data_type} = join ' ', $info->{data_type} =~ /[a-z]+/ig; |
228 | |
229 | if ($precision == 6) { |
230 | delete $info->{size}; |
231 | } |
232 | else { |
233 | $info->{size} = $precision; |
234 | } |
235 | } |
1af21646 |
236 | elsif ($info->{data_type} =~ /timestamp/i && ref $info->{size} && $info->{size}[0] == 0) { |
237 | my $size = $info->{size}[1]; |
238 | delete $info->{size}; |
239 | $info->{size} = $size unless $size == 6; |
240 | } |
760fd65c |
241 | elsif (($precision) = $info->{data_type} =~ /^interval year\((\d+)\) to month\z/i) { |
242 | $info->{data_type} = join ' ', $info->{data_type} =~ /[a-z]+/ig; |
243 | |
244 | if ($precision == 2) { |
245 | delete $info->{size}; |
246 | } |
247 | else { |
248 | $info->{size} = $precision; |
249 | } |
250 | } |
251 | elsif (my ($day_precision, $second_precision) = $info->{data_type} =~ /^interval day\((\d+)\) to second\((\d+)\)\z/i) { |
252 | $info->{data_type} = join ' ', $info->{data_type} =~ /[a-z]+/ig; |
253 | |
254 | if ($day_precision == 2 && $second_precision == 6) { |
255 | delete $info->{size}; |
256 | } |
257 | else { |
258 | $info->{size} = [ $day_precision, $second_precision ]; |
259 | } |
260 | } |
1af21646 |
261 | elsif ($info->{data_type} =~ /^interval year to month\z/i && ref $info->{size}) { |
262 | my $precision = $info->{size}[0]; |
263 | |
264 | if ($precision == 2) { |
265 | delete $info->{size}; |
266 | } |
267 | else { |
268 | $info->{size} = $precision; |
269 | } |
270 | } |
271 | elsif ($info->{data_type} =~ /^interval day to second\z/i && ref $info->{size}) { |
272 | if ($info->{size}[0] == 2 && $info->{size}[1] == 6) { |
273 | delete $info->{size}; |
274 | } |
275 | } |
1c22571b |
276 | elsif (lc($info->{data_type}) eq 'float') { |
277 | $info->{original}{data_type} = 'float'; |
278 | $info->{original}{size} = $info->{size}; |
279 | |
280 | if ($info->{size} <= 63) { |
281 | $info->{data_type} = 'real'; |
282 | } |
283 | else { |
284 | $info->{data_type} = 'double precision'; |
285 | } |
286 | delete $info->{size}; |
287 | } |
1af21646 |
288 | elsif (lc($info->{data_type}) eq 'double precision') { |
289 | $info->{original}{data_type} = 'float'; |
290 | |
291 | my $size = try { $info->{size}[0] }; |
292 | |
293 | $info->{original}{size} = $size; |
294 | |
295 | if ($size <= 63) { |
296 | $info->{data_type} = 'real'; |
297 | } |
298 | delete $info->{size}; |
299 | } |
760fd65c |
300 | elsif (lc($info->{data_type}) eq 'urowid' && $info->{size} == 4000) { |
301 | delete $info->{size}; |
302 | } |
1af21646 |
303 | elsif ($info->{data_type} eq '-9104') { |
304 | $info->{data_type} = 'rowid'; |
305 | delete $info->{size}; |
306 | } |
307 | elsif ($info->{data_type} eq '-2') { |
308 | $info->{data_type} = 'raw'; |
309 | $info->{size} = try { $info->{size}[0] / 2 }; |
310 | } |
3785bc2e |
311 | elsif (lc($info->{data_type}) eq 'date') { |
312 | $info->{data_type} = 'datetime'; |
313 | $info->{original}{data_type} = 'date'; |
314 | } |
1c22571b |
315 | elsif (lc($info->{data_type}) eq 'binary_float') { |
316 | $info->{data_type} = 'real'; |
317 | $info->{original}{data_type} = 'binary_float'; |
318 | } |
319 | elsif (lc($info->{data_type}) eq 'binary_double') { |
320 | $info->{data_type} = 'double precision'; |
321 | $info->{original}{data_type} = 'binary_double'; |
1af21646 |
322 | } |
323 | |
324 | # DEFAULT could be missed by ::DBI because of ORA-24345 |
325 | if (not defined $info->{default_value}) { |
326 | local $self->dbh->{LongReadLen} = 1_000_000; |
327 | local $self->dbh->{LongTruncOk} = 1; |
328 | my $sth = $self->dbh->prepare_cached(<<'EOF', {}, 1); |
329 | SELECT data_default |
330 | FROM all_tab_columns |
331 | WHERE column_name = ? AND table_name = ? AND owner = ? |
332 | EOF |
333 | $sth->execute($self->_uc($col), $table->name, $table->schema); |
334 | my ($default) = $sth->fetchrow_array; |
335 | $sth->finish; |
336 | |
337 | # this is mostly copied from ::DBI::QuotedDefault |
338 | if (defined $default) { |
339 | s/^\s+//, s/\s+\z// for $default; |
340 | |
341 | if ($default =~ /^'(.*?)'\z/) { |
342 | $info->{default_value} = $1; |
343 | } |
344 | elsif ($default =~ /^(-?\d.*?)\z/) { |
345 | $info->{default_value} = $1; |
346 | } |
347 | elsif ($default =~ /^NULL\z/i) { |
348 | my $null = 'null'; |
349 | $info->{default_value} = \$null; |
350 | } |
351 | elsif ($default ne '') { |
352 | my $val = $default; |
353 | $info->{default_value} = \$val; |
354 | } |
355 | } |
356 | } |
760fd65c |
357 | |
1af21646 |
358 | if ((try { lc(${ $info->{default_value} }) }||'') eq 'sysdate') { |
3785bc2e |
359 | my $current_timestamp = 'current_timestamp'; |
360 | $info->{default_value} = \$current_timestamp; |
701cd3e3 |
361 | |
362 | my $sysdate = 'sysdate'; |
363 | $info->{original}{default_value} = \$sysdate; |
760fd65c |
364 | } |
fb328d1a |
365 | } |
366 | |
760fd65c |
367 | return $result; |
fb328d1a |
368 | } |
369 | |
1af21646 |
370 | sub _dbh_column_info { |
371 | my $self = shift; |
372 | my ($dbh) = @_; |
373 | |
374 | # try to avoid ORA-24345 |
375 | local $dbh->{LongReadLen} = 1_000_000; |
376 | local $dbh->{LongTruncOk} = 1; |
377 | |
378 | return $self->next::method(@_); |
379 | } |
380 | |
e7262300 |
381 | =head1 SEE ALSO |
382 | |
383 | L<DBIx::Class::Schema::Loader>, L<DBIx::Class::Schema::Loader::Base>, |
384 | L<DBIx::Class::Schema::Loader::DBI> |
385 | |
386 | =head1 AUTHOR |
387 | |
9cc8e7e1 |
388 | See L<DBIx::Class::Schema::Loader/AUTHOR> and L<DBIx::Class::Schema::Loader/CONTRIBUTORS>. |
e7262300 |
389 | |
be80bba7 |
390 | =head1 LICENSE |
391 | |
392 | This library is free software; you can redistribute it and/or modify it under |
393 | the same terms as Perl itself. |
fb328d1a |
394 | |
e7262300 |
395 | =cut |
396 | |
397 | 1; |
760fd65c |
398 | # vim:et sts=4 sw=4 tw=0: |