Revert my previous changes (rev 1722 reverted back to rev 1721)
[dbsrgits/SQL-Translator.git] / lib / SQL / Translator / Parser / DBI / Oracle.pm
CommitLineData
bef2169c 1package SQL::Translator::Parser::DBI::Oracle;
2
44659089 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
bef2169c 21=head1 NAME
22
23SQL::Translator::Parser::DBI::Oracle - parser for DBD::Oracle
24
25=head1 SYNOPSIS
26
27See SQL::Translator::Parser::DBI.
28
29=head1 DESCRIPTION
30
31Uses DBI introspection methods to determine schema details.
32
33=cut
34
35use strict;
36use warnings;
37use DBI;
38use SQL::Translator::Schema::Constants;
39use SQL::Translator::Schema::Table;
40use SQL::Translator::Schema::Field;
41use SQL::Translator::Schema::Constraint;
42
11ad2df9 43our $VERSION = '1.59';
da06ac74 44
bef2169c 45# -------------------------------------------------------------------
46sub parse {
47 my ( $tr, $dbh ) = @_;
48
49 my $schema = $tr->schema;
50
1f5b2625 51 my $db_user = uc $tr->parser_args()->{db_user};
52 my $sth = $dbh->table_info(undef, $db_user, '%', 'TABLE');
bef2169c 53
54 while(my $table_info = $sth->fetchrow_hashref('NAME_uc')) {
bef2169c 55 next if ($table_info->{TABLE_NAME} =~ /\$/);
56
57 # create the table
58
59 my $table = $schema->add_table(
60 name => $table_info->{TABLE_NAME},
61 type => $table_info->{TABLE_TYPE},
62 );
63
64 # add the fields (columns) for this table
65
66 my $sth;
67
68 $sth = $dbh->column_info(
69 undef,
70 $table_info->{TABLE_SCHEM},
71 $table_info->{TABLE_NAME},
72 '%'
73 );
74
75 while(my $column = $sth->fetchrow_hashref('NAME_uc')) {
76 my $f = $table->add_field(
77 name => $column->{COLUMN_NAME},
78 default_value => $column->{COLUMN_DEF},
79 data_type => $column->{TYPE_NAME},
80 order => $column->{ORDINAL_POSITION},
81 size => $column->{COLUMN_SIZE},
82 ) || die $table->error;
83
84 $f->is_nullable( $column->{NULLABLE} == 1 );
85 }
86
87 # add the primary key info
88
89 $sth = $dbh->primary_key_info(
90 undef,
91 $table_info->{TABLE_SCHEM},
92 $table_info->{TABLE_NAME},
93 );
94
95 while(my $primary_key = $sth->fetchrow_hashref('NAME_uc')) {
96 my $f = $table->get_field( $primary_key->{COLUMN_NAME} );
97 $f->is_primary_key(1);
98 }
99
100 # add the foreign key info (constraints)
101
102 $sth = $dbh->foreign_key_info(
103 undef,
104 undef,
105 undef,
106 undef,
107 $table_info->{TABLE_SCHEM},
108 $table_info->{TABLE_NAME},
109 );
110
111 my $cons = {};
112 while(my $foreign_key = $sth->fetchrow_hashref('NAME_uc')) {
113 my $name = $foreign_key->{FK_NAME};
114 $cons->{$name}->{reference_table} = $foreign_key->{UK_TABLE_NAME};
115 push @{ $cons->{$name}->{fields} },
116 $foreign_key->{FK_COLUMN_NAME};
117 push @{ $cons->{$name}->{reference_fields} },
118 $foreign_key->{UK_COLUMN_NAME};
119 }
120
121 for my $name ( keys %$cons ) {
122 my $c = $table->add_constraint(
123 type => FOREIGN_KEY,
124 name => $name,
125 fields => $cons->{$name}->{fields},
126 reference_fields => $cons->{$name}->{reference_fields},
127 reference_table => $cons->{$name}->{reference_table},
128 ) || die $table->error;
129 }
130 }
131
132 return 1;
133}
134
1351;
136
137=pod
138
139=head1 AUTHOR
140
141Earl Cahill E<lt>cpan@spack.netE<gt>.
142
143=head1 ACKNOWLEDGEMENT
144
145Initial revision of this module came almost entirely from work done by
146Todd Hepler E<lt>thepler@freeshell.orgE<gt>. My changes were
147quite minor (ensuring NAME_uc, changing a couple variable names,
148skipping tables with a $ in them).
149
150Todd claimed his work to be an almost verbatim copy of
151SQL::Translator::Parser::DBI::PostgreSQL revision 1.7
152
153For me, the real work happens in DBD::Oracle and DBI, which, also
154for me, that is the beauty of having introspection methods in DBI.
155
156=head1 SEE ALSO
157
158SQL::Translator, DBD::Oracle.
159
160=cut