Fixup Makefile.PL to not croak on pristine dev-systems
[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/;
e7262300 9use Carp::Clan qw/^DBIx::Class/;
10use Class::C3;
11
6b1d4f76 12our $VERSION = '0.07001';
e7262300 13
14=head1 NAME
15
16DBIx::Class::Schema::Loader::DBI::Oracle - DBIx::Class::Schema::Loader::DBI
17Oracle Implementation.
18
e7262300 19=head1 DESCRIPTION
20
21See L<DBIx::Class::Schema::Loader::Base>.
22
e7262300 23=cut
24
d0e184e9 25sub _setup {
26 my $self = shift;
27
28 $self->next::method(@_);
29
30 my $dbh = $self->schema->storage->dbh;
46065bcb 31
32 my ($current_schema) = $dbh->selectrow_array('SELECT USER FROM DUAL', {});
33
34 $self->{db_schema} ||= $current_schema;
35
36 if (lc($self->db_schema) ne lc($current_schema)) {
37 $dbh->do('ALTER SESSION SET current_schema=' . $self->db_schema);
38 }
bc1cb85e 39
40 if (not defined $self->preserve_case) {
41 $self->preserve_case(0);
42 }
3785bc2e 43 elsif ($self->preserve_case) {
c930f78b 44 $self->schema->storage->sql_maker->quote_char('"');
45 $self->schema->storage->sql_maker->name_sep('.');
3785bc2e 46 }
d0e184e9 47}
48
075aff97 49sub _table_as_sql {
e7262300 50 my ($self, $table) = @_;
51
075aff97 52 return $self->_quote_table_name($table);
e7262300 53}
54
55sub _tables_list {
bfb43060 56 my ($self, $opts) = @_;
e7262300 57
58 my $dbh = $self->schema->storage->dbh;
59
60 my @tables;
61 for my $table ( $dbh->tables(undef, $self->db_schema, '%', 'TABLE,VIEW') ) { #catalog, schema, table, type
62 my $quoter = $dbh->get_info(29);
63 $table =~ s/$quoter//g;
64
65 # remove "user." (schema) prefixes
66 $table =~ s/\w+\.//;
67
68 next if $table eq 'PLAN_TABLE';
3785bc2e 69 $table = $self->_lc($table);
e7262300 70 push @tables, $1
71 if $table =~ /\A(\w+)\z/;
72 }
ffb03c96 73
128f61d8 74 {
75 # silence a warning from older DBD::Oracles in tests
76 my $warn_handler = $SIG{__WARN__} || sub { warn @_ };
77 local $SIG{__WARN__} = sub {
78 $warn_handler->(@_)
79 unless $_[0] =~ /^Field \d+ has an Oracle type \(\d+\) which is not explicitly supported/;
80 };
81
82 return $self->_filter_tables(\@tables, $opts);
83 }
e7262300 84}
85
4337bddf 86sub _table_columns {
87 my ($self, $table) = @_;
88
89 my $dbh = $self->schema->storage->dbh;
90
3785bc2e 91 my $sth = $dbh->column_info(undef, $self->db_schema, $self->_uc($table), '%');
92
b511f36e 93 return [ map $self->_lc($_->{COLUMN_NAME}), @{ $sth->fetchall_arrayref({ COLUMN_NAME => 1 }) || [] } ];
4337bddf 94}
95
e7262300 96sub _table_uniq_info {
97 my ($self, $table) = @_;
98
e7262300 99 my $dbh = $self->schema->storage->dbh;
100
101 my $sth = $dbh->prepare_cached(
65ab592d 102 q{
c7bf4194 103 SELECT constraint_name, acc.column_name
104 FROM all_constraints JOIN all_cons_columns acc USING (constraint_name)
8803e4ed 105 WHERE acc.table_name=? and acc.owner = ? AND constraint_type='U'
c7bf4194 106 ORDER BY acc.position
65ab592d 107 },
108 {}, 1);
e7262300 109
3785bc2e 110 $sth->execute($self->_uc($table),$self->{db_schema} );
e7262300 111 my %constr_names;
112 while(my $constr = $sth->fetchrow_arrayref) {
f28e7eae 113 my $constr_name = $self->_lc($constr->[0]);
3785bc2e 114 my $constr_col = $self->_lc($constr->[1]);
e7262300 115 $constr_name =~ s/\Q$self->{_quoter}\E//;
3785bc2e 116 $constr_col =~ s/\Q$self->{_quoter}\E//;
117 push @{$constr_names{$constr_name}}, $constr_col;
e7262300 118 }
65ab592d 119
120 my @uniqs = map { [ $_ => $constr_names{$_} ] } keys %constr_names;
e7262300 121 return \@uniqs;
122}
123
124sub _table_pk_info {
3785bc2e 125 my ($self, $table) = (shift, shift);
126
127 return $self->next::method($self->_uc($table), @_);
e7262300 128}
129
130sub _table_fk_info {
3785bc2e 131 my ($self, $table) = (shift, shift);
e7262300 132
3785bc2e 133 my $rels = $self->next::method($self->_uc($table), @_);
e7262300 134
65ab592d 135 foreach my $rel (@$rels) {
3785bc2e 136 $rel->{remote_table} = $self->_lc($rel->{remote_table});
e7262300 137 }
138
65ab592d 139 return $rels;
140}
141
142sub _columns_info_for {
760fd65c 143 my ($self, $table) = (shift, shift);
e7262300 144
3785bc2e 145 my $result = $self->next::method($self->_uc($table), @_);
fb328d1a 146
fb328d1a 147 my $dbh = $self->schema->storage->dbh;
fb328d1a 148
5cd600fa 149 local $dbh->{LongReadLen} = 100000;
150 local $dbh->{LongTruncOk} = 1;
151
760fd65c 152 my $sth = $dbh->prepare_cached(q{
5cd600fa 153SELECT atc.column_name, ut.trigger_body
760fd65c 154FROM all_triggers ut
155JOIN all_trigger_cols atc USING (trigger_name)
156WHERE atc.table_name = ?
157AND lower(column_usage) LIKE '%new%' AND lower(column_usage) LIKE '%out%'
158AND upper(trigger_type) LIKE '%BEFORE EACH ROW%' AND lower(triggering_event) LIKE '%insert%'
159 }, {}, 1);
160
3785bc2e 161 $sth->execute($self->_uc($table));
760fd65c 162
5cd600fa 163 while (my ($col_name, $trigger_body) = $sth->fetchrow_array) {
164 $col_name = $self->_lc($col_name);
165
166 $result->{$col_name}{is_auto_increment} = 1;
167
69219349 168 if (my ($seq_schema, $seq_name) = $trigger_body =~ /(?:\."?(\w+)"?)?"?(\w+)"?\.nextval/i) {
169 $seq_schema = $self->_lc($seq_schema) || $self->db_schema;
170 $seq_name = $self->_lc($seq_name);
5cd600fa 171
69219349 172 $result->{$col_name}{sequence} = ($self->qualify_objects ? ($seq_schema . '.') : '') . $seq_name;
5cd600fa 173 }
760fd65c 174 }
175
176 while (my ($col, $info) = each %$result) {
177 no warnings 'uninitialized';
178
179 if ($info->{data_type} =~ /^(?:n?[cb]lob|long(?: raw)?|bfile|date|binary_(?:float|double)|rowid)\z/i) {
180 delete $info->{size};
181 }
182
183 if ($info->{data_type} =~ /^n(?:var)?char2?\z/i) {
184 $info->{size} = $info->{size} / 2;
185 }
186 elsif (lc($info->{data_type}) eq 'number') {
4ea15dfe 187 $info->{original}{data_type} = 'number';
188 $info->{data_type} = 'numeric';
760fd65c 189
190 if (eval { $info->{size}[0] == 38 && $info->{size}[1] == 0 }) {
4ea15dfe 191 $info->{original}{size} = $info->{size};
192
760fd65c 193 $info->{data_type} = 'integer';
194 delete $info->{size};
195 }
196 }
197 elsif (my ($precision) = $info->{data_type} =~ /^timestamp\((\d+)\)(?: with (?:local )?time zone)?\z/i) {
198 $info->{data_type} = join ' ', $info->{data_type} =~ /[a-z]+/ig;
199
200 if ($precision == 6) {
201 delete $info->{size};
202 }
203 else {
204 $info->{size} = $precision;
205 }
206 }
207 elsif (($precision) = $info->{data_type} =~ /^interval year\((\d+)\) to month\z/i) {
208 $info->{data_type} = join ' ', $info->{data_type} =~ /[a-z]+/ig;
209
210 if ($precision == 2) {
211 delete $info->{size};
212 }
213 else {
214 $info->{size} = $precision;
215 }
216 }
217 elsif (my ($day_precision, $second_precision) = $info->{data_type} =~ /^interval day\((\d+)\) to second\((\d+)\)\z/i) {
218 $info->{data_type} = join ' ', $info->{data_type} =~ /[a-z]+/ig;
219
220 if ($day_precision == 2 && $second_precision == 6) {
221 delete $info->{size};
222 }
223 else {
224 $info->{size} = [ $day_precision, $second_precision ];
225 }
226 }
1c22571b 227 elsif (lc($info->{data_type}) eq 'float') {
228 $info->{original}{data_type} = 'float';
229 $info->{original}{size} = $info->{size};
230
231 if ($info->{size} <= 63) {
232 $info->{data_type} = 'real';
233 }
234 else {
235 $info->{data_type} = 'double precision';
236 }
237 delete $info->{size};
238 }
760fd65c 239 elsif (lc($info->{data_type}) eq 'urowid' && $info->{size} == 4000) {
240 delete $info->{size};
241 }
3785bc2e 242 elsif (lc($info->{data_type}) eq 'date') {
243 $info->{data_type} = 'datetime';
244 $info->{original}{data_type} = 'date';
245 }
1c22571b 246 elsif (lc($info->{data_type}) eq 'binary_float') {
247 $info->{data_type} = 'real';
248 $info->{original}{data_type} = 'binary_float';
249 }
250 elsif (lc($info->{data_type}) eq 'binary_double') {
251 $info->{data_type} = 'double precision';
252 $info->{original}{data_type} = 'binary_double';
253 }
760fd65c 254
268cc246 255 if ((eval { lc(${ $info->{default_value} }) }||'') eq 'sysdate') {
3785bc2e 256 my $current_timestamp = 'current_timestamp';
257 $info->{default_value} = \$current_timestamp;
701cd3e3 258
259 my $sysdate = 'sysdate';
260 $info->{original}{default_value} = \$sysdate;
760fd65c 261 }
fb328d1a 262 }
263
760fd65c 264 return $result;
fb328d1a 265}
266
e7262300 267=head1 SEE ALSO
268
269L<DBIx::Class::Schema::Loader>, L<DBIx::Class::Schema::Loader::Base>,
270L<DBIx::Class::Schema::Loader::DBI>
271
272=head1 AUTHOR
273
9cc8e7e1 274See L<DBIx::Class::Schema::Loader/AUTHOR> and L<DBIx::Class::Schema::Loader/CONTRIBUTORS>.
e7262300 275
be80bba7 276=head1 LICENSE
277
278This library is free software; you can redistribute it and/or modify it under
279the same terms as Perl itself.
fb328d1a 280
e7262300 281=cut
282
2831;
760fd65c 284# vim:et sts=4 sw=4 tw=0: