release 0.07017
[dbsrgits/DBIx-Class-Schema-Loader.git] / lib / DBIx / Class / Schema / Loader / DBI / Oracle.pm
CommitLineData
d87d939a 1package DBIx::Class::Schema::Loader::DBI::Oracle;
e7262300 2
3use strict;
4use warnings;
6b0e47fc 5use base qw/
6 DBIx::Class::Schema::Loader::DBI::Component::QuotedDefault
7 DBIx::Class::Schema::Loader::DBI
8/;
942bd5e0 9use mro 'c3';
e7262300 10
b45a0999 11our $VERSION = '0.07017';
e7262300 12
13=head1 NAME
14
15DBIx::Class::Schema::Loader::DBI::Oracle - DBIx::Class::Schema::Loader::DBI
16Oracle Implementation.
17
e7262300 18=head1 DESCRIPTION
19
c4a69b87 20See L<DBIx::Class::Schema::Loader> and L<DBIx::Class::Schema::Loader::Base>.
e7262300 21
e7262300 22=cut
23
d0e184e9 24sub _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 47sub _build_name_sep { '.' }
48
49sub _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 57sub _system_tables {
58 my $self = shift;
e7262300 59
c4a69b87 60 return ($self->next::method(@_), 'PLAN_TABLE');
61}
e7262300 62
c4a69b87 63sub _dbh_tables {
64 my ($self, $schema) = @_;
e7262300 65
c4a69b87 66 return $self->dbh->tables(undef, $schema, '%', 'TABLE,VIEW');
67}
e7262300 68
c4a69b87 69sub _filter_tables {
70 my $self = shift;
ffb03c96 71
c4a69b87 72 # silence a warning from older DBD::Oracles in tests
73 my $warn_handler = $SIG{__WARN__} || sub { warn @_ };
74 local $SIG{__WARN__} = sub {
75 $warn_handler->(@_)
76 unless $_[0] =~ /^Field \d+ has an Oracle type \(\d+\) which is not explicitly supported/;
77 };
128f61d8 78
c4a69b87 79 return $self->next::method(@_);
e7262300 80}
81
4337bddf 82sub _table_columns {
83 my ($self, $table) = @_;
84
c4a69b87 85 my $sth = $self->dbh->column_info(undef, $table->schema, $table, '%');
3785bc2e 86
b511f36e 87 return [ map $self->_lc($_->{COLUMN_NAME}), @{ $sth->fetchall_arrayref({ COLUMN_NAME => 1 }) || [] } ];
4337bddf 88}
89
e7262300 90sub _table_uniq_info {
91 my ($self, $table) = @_;
92
c4a69b87 93 my $sth = $self->dbh->prepare_cached(<<'EOF', {}, 1);
5975bbe6 94SELECT ac.constraint_name, acc.column_name
95FROM all_constraints ac, all_cons_columns acc
96WHERE acc.table_name=? AND acc.owner = ?
97 AND ac.table_name = acc.table_name AND ac.owner = acc.owner
98 AND acc.constraint_name = ac.constraint_name
99 AND ac.constraint_type='U'
c4a69b87 100ORDER BY acc.position
101EOF
e7262300 102
c4a69b87 103 $sth->execute($table->name, $table->schema);
e7262300 104
e7262300 105 my %constr_names;
c4a69b87 106
e7262300 107 while(my $constr = $sth->fetchrow_arrayref) {
f28e7eae 108 my $constr_name = $self->_lc($constr->[0]);
3785bc2e 109 my $constr_col = $self->_lc($constr->[1]);
3785bc2e 110 push @{$constr_names{$constr_name}}, $constr_col;
e7262300 111 }
65ab592d 112
113 my @uniqs = map { [ $_ => $constr_names{$_} ] } keys %constr_names;
e7262300 114 return \@uniqs;
115}
116
4cd5155b 117sub _table_comment {
ea998e8e 118 my $self = shift;
119 my ($table) = @_;
120
121 my $table_comment = $self->next::method(@_);
122
123 return $table_comment if $table_comment;
124
c4a69b87 125 ($table_comment) = $self->dbh->selectrow_array(<<'EOF', {}, $table->schema, $table->name);
126SELECT comments FROM all_tab_comments
127WHERE owner = ?
128 AND table_name = ?
129 AND (table_type = 'TABLE' OR table_type = 'VIEW')
130EOF
4cd5155b 131
132 return $table_comment
133}
134
135sub _column_comment {
ea998e8e 136 my $self = shift;
137 my ($table, $column_number, $column_name) = @_;
138
139 my $column_comment = $self->next::method(@_);
140
141 return $column_comment if $column_comment;
142
c4a69b87 143 ($column_comment) = $self->dbh->selectrow_array(<<'EOF', {}, $table->schema, $table->name, $self->_uc($column_name));
144SELECT comments FROM all_col_comments
145WHERE owner = ?
146 AND table_name = ?
147 AND column_name = ?
148EOF
e7262300 149
c4a69b87 150 return $column_comment
65ab592d 151}
152
153sub _columns_info_for {
c4a69b87 154 my $self = shift;
155 my ($table) = @_;
fb328d1a 156
c4a69b87 157 my $result = $self->next::method(@_);
fb328d1a 158
c4a69b87 159 local $self->dbh->{LongReadLen} = 100000;
160 local $self->dbh->{LongTruncOk} = 1;
5cd600fa 161
c4a69b87 162 my $sth = $self->dbh->prepare_cached(<<'EOF', {}, 1);
5975bbe6 163SELECT trigger_body
164FROM all_triggers
165WHERE table_name = ? AND table_owner = ?
760fd65c 166AND upper(trigger_type) LIKE '%BEFORE EACH ROW%' AND lower(triggering_event) LIKE '%insert%'
c4a69b87 167EOF
760fd65c 168
5975bbe6 169 $sth->execute($table->name, $table->schema);
760fd65c 170
5975bbe6 171 while (my ($trigger_body) = $sth->fetchrow_array) {
172 if (my ($seq_schema, $seq_name) = $trigger_body =~ /(?:\."?(\w+)"?)?"?(\w+)"?\.nextval/i) {
173 if (my ($col_name) = $trigger_body =~ /:new\.(\w+)/i) {
174 $col_name = $self->_lc($col_name);
5cd600fa 175
5975bbe6 176 $result->{$col_name}{is_auto_increment} = 1;
5cd600fa 177
5975bbe6 178 $seq_schema = $self->_lc($seq_schema || $table->schema);
179 $seq_name = $self->_lc($seq_name);
5cd600fa 180
5975bbe6 181 $result->{$col_name}{sequence} = ($self->qualify_objects ? ($seq_schema . '.') : '') . $seq_name;
182 }
5cd600fa 183 }
760fd65c 184 }
185
186 while (my ($col, $info) = each %$result) {
187 no warnings 'uninitialized';
188
189 if ($info->{data_type} =~ /^(?:n?[cb]lob|long(?: raw)?|bfile|date|binary_(?:float|double)|rowid)\z/i) {
190 delete $info->{size};
191 }
192
193 if ($info->{data_type} =~ /^n(?:var)?char2?\z/i) {
194 $info->{size} = $info->{size} / 2;
195 }
196 elsif (lc($info->{data_type}) eq 'number') {
4ea15dfe 197 $info->{original}{data_type} = 'number';
198 $info->{data_type} = 'numeric';
760fd65c 199
200 if (eval { $info->{size}[0] == 38 && $info->{size}[1] == 0 }) {
4ea15dfe 201 $info->{original}{size} = $info->{size};
202
760fd65c 203 $info->{data_type} = 'integer';
204 delete $info->{size};
205 }
206 }
207 elsif (my ($precision) = $info->{data_type} =~ /^timestamp\((\d+)\)(?: with (?:local )?time zone)?\z/i) {
208 $info->{data_type} = join ' ', $info->{data_type} =~ /[a-z]+/ig;
209
210 if ($precision == 6) {
211 delete $info->{size};
212 }
213 else {
214 $info->{size} = $precision;
215 }
216 }
217 elsif (($precision) = $info->{data_type} =~ /^interval year\((\d+)\) to month\z/i) {
218 $info->{data_type} = join ' ', $info->{data_type} =~ /[a-z]+/ig;
219
220 if ($precision == 2) {
221 delete $info->{size};
222 }
223 else {
224 $info->{size} = $precision;
225 }
226 }
227 elsif (my ($day_precision, $second_precision) = $info->{data_type} =~ /^interval day\((\d+)\) to second\((\d+)\)\z/i) {
228 $info->{data_type} = join ' ', $info->{data_type} =~ /[a-z]+/ig;
229
230 if ($day_precision == 2 && $second_precision == 6) {
231 delete $info->{size};
232 }
233 else {
234 $info->{size} = [ $day_precision, $second_precision ];
235 }
236 }
1c22571b 237 elsif (lc($info->{data_type}) eq 'float') {
238 $info->{original}{data_type} = 'float';
239 $info->{original}{size} = $info->{size};
240
241 if ($info->{size} <= 63) {
242 $info->{data_type} = 'real';
243 }
244 else {
245 $info->{data_type} = 'double precision';
246 }
247 delete $info->{size};
248 }
760fd65c 249 elsif (lc($info->{data_type}) eq 'urowid' && $info->{size} == 4000) {
250 delete $info->{size};
251 }
3785bc2e 252 elsif (lc($info->{data_type}) eq 'date') {
253 $info->{data_type} = 'datetime';
254 $info->{original}{data_type} = 'date';
255 }
1c22571b 256 elsif (lc($info->{data_type}) eq 'binary_float') {
257 $info->{data_type} = 'real';
258 $info->{original}{data_type} = 'binary_float';
259 }
260 elsif (lc($info->{data_type}) eq 'binary_double') {
261 $info->{data_type} = 'double precision';
262 $info->{original}{data_type} = 'binary_double';
263 }
760fd65c 264
268cc246 265 if ((eval { lc(${ $info->{default_value} }) }||'') eq 'sysdate') {
3785bc2e 266 my $current_timestamp = 'current_timestamp';
267 $info->{default_value} = \$current_timestamp;
701cd3e3 268
269 my $sysdate = 'sysdate';
270 $info->{original}{default_value} = \$sysdate;
760fd65c 271 }
fb328d1a 272 }
273
760fd65c 274 return $result;
fb328d1a 275}
276
e7262300 277=head1 SEE ALSO
278
279L<DBIx::Class::Schema::Loader>, L<DBIx::Class::Schema::Loader::Base>,
280L<DBIx::Class::Schema::Loader::DBI>
281
282=head1 AUTHOR
283
9cc8e7e1 284See L<DBIx::Class::Schema::Loader/AUTHOR> and L<DBIx::Class::Schema::Loader/CONTRIBUTORS>.
e7262300 285
be80bba7 286=head1 LICENSE
287
288This library is free software; you can redistribute it and/or modify it under
289the same terms as Perl itself.
fb328d1a 290
e7262300 291=cut
292
2931;
760fd65c 294# vim:et sts=4 sw=4 tw=0: