new dev release
[dbsrgits/DBIx-Class-Schema-Loader.git] / lib / DBIx / Class / Schema / Loader / DBI / Oracle.pm
1 package DBIx::Class::Schema::Loader::DBI::Oracle;
2
3 use strict;
4 use warnings;
5 use base 'DBIx::Class::Schema::Loader::DBI';
6 use Carp::Clan qw/^DBIx::Class/;
7 use Class::C3;
8
9 our $VERSION = '0.04999_14';
10
11 =head1 NAME
12
13 DBIx::Class::Schema::Loader::DBI::Oracle - DBIx::Class::Schema::Loader::DBI 
14 Oracle Implementation.
15
16 =head1 SYNOPSIS
17
18   package My::Schema;
19   use base qw/DBIx::Class::Schema::Loader/;
20
21   __PACKAGE__->loader_options( debug => 1 );
22
23   1;
24
25 =head1 DESCRIPTION
26
27 See L<DBIx::Class::Schema::Loader::Base>.
28
29 This module is considered experimental and not well tested yet.
30
31 =cut
32
33 sub _setup {
34     my $self = shift;
35
36     $self->next::method(@_);
37
38     my $dbh = $self->schema->storage->dbh;
39
40     my ($current_schema) = $dbh->selectrow_array('SELECT USER FROM DUAL', {});
41
42     $self->{db_schema} ||= $current_schema;
43
44     if (lc($self->db_schema) ne lc($current_schema)) {
45         $dbh->do('ALTER SESSION SET current_schema=' . $self->db_schema);
46     }
47 }
48
49 sub _table_as_sql {
50     my ($self, $table) = @_;
51
52     return $self->_quote_table_name($table);
53 }
54
55 sub _tables_list { 
56     my $self = shift;
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';
69         $table = lc $table;
70         push @tables, $1
71           if $table =~ /\A(\w+)\z/;
72     }
73     return $self->_filter_tables(@tables);
74 }
75
76 sub _table_uniq_info {
77     my ($self, $table) = @_;
78
79     my $dbh = $self->schema->storage->dbh;
80
81     my $sth = $dbh->prepare_cached(
82         q{
83             SELECT constraint_name, acc.column_name
84             FROM all_constraints JOIN all_cons_columns acc USING (constraint_name)
85             WHERE acc.table_name=? and acc.owner = ? AND constraint_type='U'
86             ORDER BY acc.position
87         },
88         {}, 1);
89
90     $sth->execute(uc $table,$self->{db_schema} );
91     my %constr_names;
92     while(my $constr = $sth->fetchrow_arrayref) {
93         my $constr_name = lc $constr->[0];
94         my $constr_def  = lc $constr->[1];
95         $constr_name =~ s/\Q$self->{_quoter}\E//;
96         $constr_def =~ s/\Q$self->{_quoter}\E//;
97         push @{$constr_names{$constr_name}}, $constr_def;
98     }
99     
100     my @uniqs = map { [ $_ => $constr_names{$_} ] } keys %constr_names;
101     return \@uniqs;
102 }
103
104 sub _table_pk_info {
105     my ($self, $table) = @_;
106     return $self->next::method(uc $table);
107 }
108
109 sub _table_fk_info {
110     my ($self, $table) = @_;
111
112     my $rels = $self->next::method(uc $table);
113
114     foreach my $rel (@$rels) {
115         $rel->{remote_table} = lc $rel->{remote_table};
116     }
117
118     return $rels;
119 }
120
121 sub _columns_info_for {
122     my ($self, $table) = @_;
123     return $self->next::method(uc $table);
124 }
125
126 sub _extra_column_info {
127     my ($self, $info) = @_;
128     my %extra_info;
129
130     my ($table, $column) = @$info{qw/TABLE_NAME COLUMN_NAME/};
131
132     my $dbh = $self->schema->storage->dbh;
133     my $sth = $dbh->prepare_cached(
134         q{
135             SELECT COUNT(*)
136             FROM all_triggers ut JOIN all_trigger_cols atc USING (trigger_name)
137             WHERE atc.table_name = ? AND atc.column_name = ?
138             AND column_usage LIKE '%NEW%' AND column_usage LIKE '%OUT%'
139             AND trigger_type = 'BEFORE EACH ROW' AND triggering_event LIKE '%INSERT%'
140         },
141         {}, 1);
142
143     $sth->execute($table, $column);
144     if ($sth->fetchrow_array) {
145         $extra_info{is_auto_increment} = 1;
146     }
147
148     return \%extra_info;
149 }
150
151 =head1 SEE ALSO
152
153 L<DBIx::Class::Schema::Loader>, L<DBIx::Class::Schema::Loader::Base>,
154 L<DBIx::Class::Schema::Loader::DBI>
155
156 =head1 AUTHOR
157
158 See L<DBIx::Class::Schema::Loader/AUTHOR> and L<DBIx::Class::Schema::Loader/CONTRIBUTORS>.
159
160 =head1 LICENSE
161
162 This library is free software; you can redistribute it and/or modify it under
163 the same terms as Perl itself.
164
165 =cut
166
167 1;