This module does not actually use any DBD::Pg calls, just DBI. However, the DBI calls...
[dbsrgits/SQL-Translator.git] / lib / SQL / Translator / Parser / DBI / PostgreSQL.pm
CommitLineData
80ae061a 1package SQL::Translator::Parser::DBI::PostgreSQL;
2
3# -------------------------------------------------------------------
6e86705a 4# $Id: PostgreSQL.pm,v 1.4 2003-10-15 19:56:20 phrrngtn Exp $
80ae061a 5# -------------------------------------------------------------------
13b0b70f 6# Copyright (C) 2003 Paul Harrington <harringp@deshaw.com>.
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
13b0b70f 33Uses DBD::Pg 1.31 catalog methods to determine schema structure.
80ae061a 34
35=cut
36
37use strict;
38use DBI;
80ae061a 39use Data::Dumper;
40use SQL::Translator::Schema::Constants;
41
42use vars qw[ $DEBUG $VERSION @EXPORT_OK ];
6e86705a 43$VERSION = sprintf "%d.%02d", q$Revision: 1.4 $ =~ /(\d+)\.(\d+)/;
80ae061a 44$DEBUG = 0 unless defined $DEBUG;
45
46# -------------------------------------------------------------------
47sub parse {
48 my ( $tr, $dbh ) = @_;
49
638e82c2 50 my $schema = $tr->schema;
80ae061a 51
638e82c2 52 my ($sth, @tables, $columns);
53 my $stuff;
80ae061a 54
6e86705a 55 warn "DBD:Pg $DBD::Pg::VERSION is not likely to produce anything useful. Upgrade to 1.31 or better if available."
56 unless ($DBD::Pg::VERSION ge '1.31');
80ae061a 57
638e82c2 58 if ($dbh->{FetchHashKeyName} ne 'NAME_uc') {
638e82c2 59 $dbh->{FetchHashKeyName} = 'NAME_uc';
60 }
80ae061a 61
638e82c2 62 if ($dbh->{ChopBlanks} != 1) {
638e82c2 63 $dbh->{ChopBlanks} = 1;
64 }
80ae061a 65
638e82c2 66 $sth = $dbh->column_info();
67 foreach my $c (@{$sth->fetchall_arrayref({})}) {
68 $columns
69 ->{$c->{TABLE_SCHEM}}
70 ->{$c->{TABLE_NAME}}
71 ->{columns}
72 ->{$c->{COLUMN_NAME}}= $c;
73 }
80ae061a 74
638e82c2 75 $sth = $dbh->table_info();
80ae061a 76
638e82c2 77 @tables = @{$sth->fetchall_arrayref({})};
80ae061a 78
638e82c2 79 foreach my $table_info (@tables) {
80 next
81 unless (defined($table_info->{TABLE_TYPE}));
80ae061a 82
638e82c2 83 if ($table_info->{TABLE_TYPE} eq 'TABLE'&&
84 $table_info->{TABLE_SCHEM} eq 'public') {
85 my $table = $schema->add_table(
86 name => $table_info->{TABLE_NAME},
87 type => $table_info->{TABLE_TYPE},
88 ) || die $schema->error;
80ae061a 89
638e82c2 90
91 my $cols =
92 $columns->{$table_info->{TABLE_SCHEM}}
93 ->{$table_info->{TABLE_NAME}}
94 ->{columns};
95
96 foreach my $c (values %{$cols}) {
97 my $f = $table->add_field(
98 name => $c->{COLUMN_NAME},
99 data_type => $c->{TYPE_NAME},
100 order => $c->{ORDINAL_POSITION},
101 size => $c->{COLUMN_SIZE},
102 ) || die $table->error;
103
104 $f->is_nullable(1)
105 if ($c->{NULLABLE} == 1);
106 }
107 }
80ae061a 108 }
109
110 return 1;
111}
112
1131;
114
115# -------------------------------------------------------------------
13b0b70f 116# Time is a waste of money.
117# Oscar Wilde
80ae061a 118# -------------------------------------------------------------------
119
120=pod
121
122=head1 AUTHOR
123
13b0b70f 124Paul Harrington E<lt>harringp@deshaw.comE<gt>.
80ae061a 125
126=head1 SEE ALSO
127
13b0b70f 128SQL::Translator, DBD::Pg.
80ae061a 129
130=cut