1b57cba485f0d9d01570f33159ae1e6dd168583a
[dbsrgits/DBIx-Class-Schema-Loader.git] / lib / DBIx / Class / Schema / Loader / DBI / Oracle.pm
1 package # hide from pause/cpan for now, as there's a permissions
2         # issue and it's screwing the rest of the package
3   DBIx::Class::Schema::Loader::DBI::Oracle;
4
5 use strict;
6 use warnings;
7 use base 'DBIx::Class::Schema::Loader::DBI';
8 use Carp::Clan qw/^DBIx::Class/;
9 use Class::C3;
10
11 our $VERSION = '0.04003';
12
13 =head1 NAME
14
15 DBIx::Class::Schema::Loader::DBI::Oracle - DBIx::Class::Schema::Loader::DBI 
16 Oracle Implementation.
17
18 =head1 SYNOPSIS
19
20   package My::Schema;
21   use base qw/DBIx::Class::Schema::Loader/;
22
23   __PACKAGE__->loader_options( debug => 1 );
24
25   1;
26
27 =head1 DESCRIPTION
28
29 See L<DBIx::Class::Schema::Loader::Base>.
30
31 This module is considered experimental and not well tested yet.
32
33 =cut
34
35 sub _setup {
36     my $self = shift;
37
38     $self->next::method(@_);
39
40     my $dbh = $self->schema->storage->dbh;
41     $self->{db_schema} ||= $dbh->selectrow_array('SELECT USER FROM DUAL', {});
42 }
43
44
45 sub _table_columns {
46     my ($self, $table) = @_;
47
48     my $dbh = $self->schema->storage->dbh;
49
50     my $sth = $dbh->prepare($self->schema->storage->sql_maker->select($table, undef, \'1 = 0'));
51     $sth->execute;
52     return \@{$sth->{NAME_lc}};
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 @tables;
74 }
75
76 sub _table_uniq_info {
77     my ($self, $table) = @_;
78
79     my @uniqs;
80     my $dbh = $self->schema->storage->dbh;
81
82     my $sth = $dbh->prepare_cached(
83         qq{SELECT constraint_name, ucc.column_name FROM user_constraints JOIN user_cons_columns ucc USING (constraint_name) WHERE ucc.table_name=? AND constraint_type='U'}
84     ,{}, 1);
85
86     $sth->execute(uc $table);
87     my %constr_names;
88     while(my $constr = $sth->fetchrow_arrayref) {
89         my $constr_name = $constr->[0];
90         my $constr_def  = $constr->[1];
91         $constr_name =~ s/\Q$self->{_quoter}\E//;
92         $constr_def =~ s/\Q$self->{_quoter}\E//;
93         push @{$constr_names{$constr_name}}, lc $constr_def;
94     }
95     map {
96         push(@uniqs, [ lc $_ => $constr_names{$_} ]);
97     } keys %constr_names;
98
99     return \@uniqs;
100 }
101
102 sub _table_pk_info {
103     my ( $self, $table ) = @_;
104     return $self->SUPER::_table_pk_info(uc $table);
105 }
106
107 sub _table_fk_info {
108     my ($self, $table) = @_;
109
110     my $dbh = $self->schema->storage->dbh;
111     my $sth = $dbh->foreign_key_info( '', '', '', '',
112         $self->db_schema, uc $table );
113     return [] if !$sth;
114
115     my %rels;
116
117     my $i = 1; # for unnamed rels, which hopefully have only 1 column ...
118     while(my $raw_rel = $sth->fetchrow_arrayref) {
119         my $uk_tbl  = lc $raw_rel->[2];
120         my $uk_col  = lc $raw_rel->[3];
121         my $fk_col  = lc $raw_rel->[7];
122         my $relid   = ($raw_rel->[11] || ( "__dcsld__" . $i++ ));
123         $uk_tbl =~ s/\Q$self->{_quoter}\E//g;
124         $uk_col =~ s/\Q$self->{_quoter}\E//g;
125         $fk_col =~ s/\Q$self->{_quoter}\E//g;
126         $relid  =~ s/\Q$self->{_quoter}\E//g;
127         $rels{$relid}->{tbl} = $uk_tbl;
128         $rels{$relid}->{cols}->{$uk_col} = $fk_col;
129     }
130
131     my @rels;
132     foreach my $relid (keys %rels) {
133         push(@rels, {
134             remote_columns => [ keys   %{$rels{$relid}->{cols}} ],
135             local_columns  => [ values %{$rels{$relid}->{cols}} ],
136             remote_table   => $rels{$relid}->{tbl},
137         });
138     }
139
140     return \@rels;
141 }
142
143 =head1 SEE ALSO
144
145 L<DBIx::Class::Schema::Loader>, L<DBIx::Class::Schema::Loader::Base>,
146 L<DBIx::Class::Schema::Loader::DBI>
147
148 =head1 AUTHOR
149
150 TSUNODA Kazuya C<drk@drk7.jp>
151
152 =cut
153
154 1;