take out duplicate docs
[dbsrgits/SQL-Translator.git] / lib / SQL / Translator / Parser / DBI / Oracle.pm
1 package SQL::Translator::Parser::DBI::Oracle;
2
3 # -------------------------------------------------------------------
4 # Copyright (C) 2006-2009 SQLFairy Authors
5 #
6 # This program is free software; you can redistribute it and/or
7 # modify it under the terms of the GNU General Public License as
8 # published by the Free Software Foundation; version 2.
9 #
10 # This program is distributed in the hope that it will be useful, but
11 # WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13 # General Public License for more details.
14 #
15 # You should have received a copy of the GNU General Public License
16 # along with this program; if not, write to the Free Software
17 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
18 # 02111-1307  USA
19 # -------------------------------------------------------------------
20
21 =head1 NAME
22
23 SQL::Translator::Parser::DBI::Oracle - parser for DBD::Oracle
24
25 =head1 SYNOPSIS
26
27 See SQL::Translator::Parser::DBI.
28
29 =head1 DESCRIPTION
30
31 Uses DBI introspection methods to determine schema details.
32
33 =cut
34
35 use strict;
36 use warnings;
37 use DBI;
38 use SQL::Translator::Schema::Constants;
39 use SQL::Translator::Schema::Table;
40 use SQL::Translator::Schema::Field;
41 use SQL::Translator::Schema::Constraint;
42
43 our $VERSION = '1.59';
44
45 sub parse {
46     my ( $tr, $dbh ) = @_;
47
48     my $schema = $tr->schema;
49
50     my $db_user = uc $tr->parser_args()->{db_user};
51     my $sth = $dbh->table_info(undef, $db_user, '%', 'TABLE');
52
53     while(my $table_info = $sth->fetchrow_hashref('NAME_uc')) {
54         next if ($table_info->{TABLE_NAME} =~ /\$/);
55
56         # create the table
57
58         my $table = $schema->add_table(
59             name => $table_info->{TABLE_NAME},
60             type => $table_info->{TABLE_TYPE},
61         );
62
63         # add the fields (columns) for this table
64
65         my $sth;
66
67         $sth = $dbh->column_info(
68             undef,
69             $table_info->{TABLE_SCHEM},
70             $table_info->{TABLE_NAME},
71             '%'
72         );
73
74         while(my $column = $sth->fetchrow_hashref('NAME_uc')) {
75             my $f = $table->add_field(
76                 name          => $column->{COLUMN_NAME},
77                 default_value => $column->{COLUMN_DEF},
78                 data_type     => $column->{TYPE_NAME},
79                 order         => $column->{ORDINAL_POSITION},
80                 size          => $column->{COLUMN_SIZE},
81             ) || die $table->error;
82
83             $f->is_nullable( $column->{NULLABLE} == 1 );
84         }
85
86         # add the primary key info
87
88         $sth = $dbh->primary_key_info(
89             undef,
90             $table_info->{TABLE_SCHEM},
91             $table_info->{TABLE_NAME},
92         );
93
94         while(my $primary_key = $sth->fetchrow_hashref('NAME_uc')) {
95             my $f = $table->get_field( $primary_key->{COLUMN_NAME} );
96             $f->is_primary_key(1);
97         }
98
99         # add the foreign key info (constraints)
100
101         $sth = $dbh->foreign_key_info(
102             undef,
103             undef,
104             undef,
105             undef,
106             $table_info->{TABLE_SCHEM},
107             $table_info->{TABLE_NAME},
108         );
109
110         my $cons = {};
111         while(my $foreign_key = $sth->fetchrow_hashref('NAME_uc')) {
112             my $name = $foreign_key->{FK_NAME};
113             $cons->{$name}->{reference_table} = $foreign_key->{UK_TABLE_NAME};
114             push @{ $cons->{$name}->{fields} },
115                 $foreign_key->{FK_COLUMN_NAME};
116             push @{ $cons->{$name}->{reference_fields} },
117                 $foreign_key->{UK_COLUMN_NAME};
118         }
119
120         for my $name ( keys %$cons ) {
121             my $c = $table->add_constraint(
122                 type             => FOREIGN_KEY,
123                 name             => $name,
124                 fields           => $cons->{$name}->{fields},
125                 reference_fields => $cons->{$name}->{reference_fields},
126                 reference_table  => $cons->{$name}->{reference_table},
127             ) || die $table->error;
128         }
129     }
130
131     return 1;
132 }
133
134 1;
135
136 =pod
137
138 =head1 AUTHOR
139
140 Earl Cahill E<lt>cpan@spack.netE<gt>.
141
142 =head1 ACKNOWLEDGEMENT
143
144 Initial revision of this module came almost entirely from work done by
145 Todd Hepler E<lt>thepler@freeshell.orgE<gt>.  My changes were
146 quite minor (ensuring NAME_uc, changing a couple variable names,
147 skipping tables with a $ in them).
148
149 Todd claimed his work to be an almost verbatim copy of
150 SQL::Translator::Parser::DBI::PostgreSQL revision 1.7
151
152 For me, the real work happens in DBD::Oracle and DBI, which, also
153 for me, that is the beauty of having introspection methods in DBI.
154
155 =head1 SEE ALSO
156
157 SQL::Translator, DBD::Oracle.
158
159 =cut