Strip evil svn:keywords
[dbsrgits/SQL-Translator.git] / lib / SQL / Translator / Parser / DBI / PostgreSQL.pm
CommitLineData
80ae061a 1package SQL::Translator::Parser::DBI::PostgreSQL;
2
3# -------------------------------------------------------------------
d4f84dd1 4# $Id: PostgreSQL.pm 1440 2009-01-17 16:31:57Z jawnsy $
80ae061a 5# -------------------------------------------------------------------
478f608d 6# Copyright (C) 2002-2009 SQLFairy Authors
80ae061a 7#
8# This program is free software; you can redistribute it and/or
9# modify it under the terms of the GNU General Public License as
10# published by the Free Software Foundation; version 2.
11#
12# This program is distributed in the hope that it will be useful, but
13# WITHOUT ANY WARRANTY; without even the implied warranty of
14# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15# General Public License for more details.
16#
17# You should have received a copy of the GNU General Public License
18# along with this program; if not, write to the Free Software
19# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
20# 02111-1307 USA
21# -------------------------------------------------------------------
22
23=head1 NAME
24
25SQL::Translator::Parser::DBI::PostgreSQL - parser for DBD::Pg
26
27=head1 SYNOPSIS
28
29See SQL::Translator::Parser::DBI.
30
31=head1 DESCRIPTION
32
d2522b19 33Uses DBI to query PostgreSQL system tables to determine schema structure.
80ae061a 34
35=cut
36
37use strict;
38use DBI;
80ae061a 39use Data::Dumper;
40use SQL::Translator::Schema::Constants;
41
478f608d 42use vars qw[ $DEBUG @EXPORT_OK ];
80ae061a 43$DEBUG = 0 unless defined $DEBUG;
44
45# -------------------------------------------------------------------
46sub parse {
47 my ( $tr, $dbh ) = @_;
48
638e82c2 49 my $schema = $tr->schema;
80ae061a 50
d2522b19 51 my $column_select = $dbh->prepare(
52 "SELECT a.attname, t.typname, a.attnum,a.atttypmod as length,
53 a.attnotnull, a.atthasdef, d.adsrc
54 FROM pg_type t,pg_attribute a
55 LEFT JOIN pg_attrdef d ON (d.adrelid = a.attrelid AND a.attnum = d.adnum)
56 WHERE a.attrelid=? AND attnum>0
57 AND a.atttypid=t.oid
58 ORDER BY a.attnum"
59 );
60
61 my $index_select = $dbh->prepare(
62 "SELECT oid, c.relname, i.indkey, i.indnatts, i.indisunique,
63 i.indisprimary, pg_get_indexdef(oid) AS create_string
64 FROM pg_class c,pg_index i
65 WHERE c.relnamespace=2200 AND c.relkind='i'
66 AND c.oid=i.indexrelid AND i.indrelid=?"
67 );
68
69 my $table_select = $dbh->prepare(
70 "SELECT oid,relname FROM pg_class WHERE relnamespace IN
71 (SELECT oid FROM pg_namespace WHERE nspname='public')
72 AND relkind='r';"
73 );
74 $table_select->execute();
75
76 while ( my $tablehash = $table_select->fetchrow_hashref ) {
77
78 my $table_name = $$tablehash{'relname'};
79 my $table_oid = $$tablehash{'oid'};
80
81 my $table = $schema->add_table(
82 name => $table_name,
83 #what is type? type => $table_info->{TABLE_TYPE},
638e82c2 84 ) || die $schema->error;
80ae061a 85
d2522b19 86 $column_select->execute($table_oid);
87
88 while (my $columnhash = $column_select->fetchrow_hashref ) {
638e82c2 89
31f10179 90 #data_type seems to not be populated; perhaps there needs to
91 #be a mapping of query output to reserved constants in sqlt?
92
93 my $col = $table->add_field(
d2522b19 94 name => $$columnhash{'attname'},
95 default_value => $$columnhash{'adsrc'},
7ed7402c 96 data_type => $$columnhash{'typname'},
d2522b19 97 order => $$columnhash{'attnum'},
d2522b19 98 ) || die $table->error;
31f10179 99
100 $col->{size} = [$$columnhash{'length'}] if $$columnhash{'length'}>0;
7ed7402c 101 $col->{is_nullable} = $$columnhash{'attnotnull'} ? 0 : 1;
d2522b19 102 }
638e82c2 103
d2522b19 104 $index_select->execute($table_oid);
105
106 my @column_names = $table->field_names();
107 while (my $indexhash = $index_select->fetchrow_hashref ) {
108 #don't deal with function indexes at the moment
109 next if ($$indexhash{'indkey'} eq ''
110 or !defined($$indexhash{'indkey'}) );
111
112 my $type;
31f10179 113 if ($$indexhash{'indisprimary'}) {
114 $type = UNIQUE; #PRIMARY_KEY;
115
116 #tell sqlt that this is the primary key:
117 my $col_name=$column_names[($$indexhash{'indkey'} - 1)];
118 $table->get_field($col_name)->{is_primary_key}=1;
119
120 } elsif ($$indexhash{'indisunique'}) {
d2522b19 121 $type = UNIQUE;
122 } else {
123 $type = NORMAL;
124 }
638e82c2 125
d2522b19 126 my @column_ids = split /\s+/, $$indexhash{'indkey'};
127 my @columns;
128 foreach my $col (@column_ids) {
129 push @columns, $column_names[($col - 1)];
638e82c2 130 }
d2522b19 131
132 $table->add_index(
133 name => $$indexhash{'relname'},
134 type => $type,
31f10179 135 fields => \@columns,
d2522b19 136 ) || die $table->error;
638e82c2 137 }
80ae061a 138 }
139
140 return 1;
141}
142
1431;
144
145# -------------------------------------------------------------------
13b0b70f 146# Time is a waste of money.
147# Oscar Wilde
80ae061a 148# -------------------------------------------------------------------
149
150=pod
151
152=head1 AUTHOR
153
d2522b19 154Scott Cain E<lt>cain@cshl.eduE<gt>, previous author:
13b0b70f 155Paul Harrington E<lt>harringp@deshaw.comE<gt>.
80ae061a 156
157=head1 SEE ALSO
158
13b0b70f 159SQL::Translator, DBD::Pg.
80ae061a 160
161=cut