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