0.04003 changes, version bumps
[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 _table_columns {
36     my ($self, $table) = @_;
37
38     my $dbh = $self->schema->storage->dbh;
39
40     my $sth = $dbh->prepare($self->schema->storage->sql_maker->select($table, undef, \'1 = 0'));
41     $sth->execute;
42     return \@{$sth->{NAME_lc}};
43 }
44
45 sub _tables_list { 
46     my $self = shift;
47
48     my $dbh = $self->schema->storage->dbh;
49
50     my @tables;
51     for my $table ( $dbh->tables(undef, $self->db_schema, '%', 'TABLE,VIEW') ) { #catalog, schema, table, type
52         my $quoter = $dbh->get_info(29);
53         $table =~ s/$quoter//g;
54
55         # remove "user." (schema) prefixes
56         $table =~ s/\w+\.//;
57
58         next if $table eq 'PLAN_TABLE';
59         $table = lc $table;
60         push @tables, $1
61           if $table =~ /\A(\w+)\z/;
62     }
63     return @tables;
64 }
65
66 sub _table_uniq_info {
67     my ($self, $table) = @_;
68
69     my @uniqs;
70     my $dbh = $self->schema->storage->dbh;
71
72     my $sth = $dbh->prepare_cached(
73         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'}
74     ,{}, 1);
75
76     $sth->execute(uc $table);
77     my %constr_names;
78     while(my $constr = $sth->fetchrow_arrayref) {
79         my $constr_name = $constr->[0];
80         my $constr_def  = $constr->[1];
81         $constr_name =~ s/\Q$self->{_quoter}\E//;
82         $constr_def =~ s/\Q$self->{_quoter}\E//;
83         push @{$constr_names{$constr_name}}, lc $constr_def;
84     }
85     map {
86         push(@uniqs, [ lc $_ => $constr_names{$_} ]);
87     } keys %constr_names;
88
89     return \@uniqs;
90 }
91
92 sub _table_pk_info {
93     my ( $self, $table ) = @_;
94     return $self->SUPER::_table_pk_info(uc $table);
95 }
96
97 sub _table_fk_info {
98     my ($self, $table) = @_;
99
100     my $dbh = $self->schema->storage->dbh;
101     my $sth = $dbh->foreign_key_info( '', '', '', '',
102         $self->db_schema, uc $table );
103     return [] if !$sth;
104
105     my %rels;
106
107     my $i = 1; # for unnamed rels, which hopefully have only 1 column ...
108     while(my $raw_rel = $sth->fetchrow_arrayref) {
109         my $uk_tbl  = lc $raw_rel->[2];
110         my $uk_col  = lc $raw_rel->[3];
111         my $fk_col  = lc $raw_rel->[7];
112         my $relid   = ($raw_rel->[11] || ( "__dcsld__" . $i++ ));
113         $uk_tbl =~ s/\Q$self->{_quoter}\E//g;
114         $uk_col =~ s/\Q$self->{_quoter}\E//g;
115         $fk_col =~ s/\Q$self->{_quoter}\E//g;
116         $relid  =~ s/\Q$self->{_quoter}\E//g;
117         $rels{$relid}->{tbl} = $uk_tbl;
118         $rels{$relid}->{cols}->{$uk_col} = $fk_col;
119     }
120
121     my @rels;
122     foreach my $relid (keys %rels) {
123         push(@rels, {
124             remote_columns => [ keys   %{$rels{$relid}->{cols}} ],
125             local_columns  => [ values %{$rels{$relid}->{cols}} ],
126             remote_table   => $rels{$relid}->{tbl},
127         });
128     }
129
130     return \@rels;
131 }
132
133 =head1 SEE ALSO
134
135 L<DBIx::Class::Schema::Loader>, L<DBIx::Class::Schema::Loader::Base>,
136 L<DBIx::Class::Schema::Loader::DBI>
137
138 =head1 AUTHOR
139
140 TSUNODA Kazuya C<drk@drk7.jp>
141
142 =cut
143
144 1;