1 package SQL::Translator::Producer::PostgreSQL;
3 # -------------------------------------------------------------------
4 # $Id: PostgreSQL.pm,v 1.2 2002-11-22 03:03:40 kycl4rk Exp $
5 # -------------------------------------------------------------------
6 # Copyright (C) 2002 Ken Y. Clark <kclark@cpan.org>,
7 # darren chamberlain <darren@cpan.org>
9 # This program is free software; you can redistribute it and/or
10 # modify it under the terms of the GNU General Public License as
11 # published by the Free Software Foundation; version 2.
13 # This program is distributed in the hope that it will be useful, but
14 # WITHOUT ANY WARRANTY; without even the implied warranty of
15 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 # General Public License for more details.
18 # You should have received a copy of the GNU General Public License
19 # along with this program; if not, write to the Free Software
20 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
22 # -------------------------------------------------------------------
25 use vars qw($VERSION $DEBUG);
26 $VERSION = sprintf "%d.%02d", q$Revision: 1.2 $ =~ /(\d+)\.(\d+)/;
27 $DEBUG = 1 unless defined $DEBUG;
36 double => 'double precision',
38 float => 'double precision',
40 mediumint => 'integer',
41 smallint => 'smallint',
42 tinyint => 'smallint',
51 mediumblob => 'bytea',
56 datetime => 'timestamp',
58 timestamp => 'timestamp',
68 warn "loading " . __PACKAGE__ . "...\n";
72 my ( $translator, $data ) = @_;
73 debug("Beginning production\n");
74 my $create = sprintf "--\n-- Created by %s\n-- Created on %s\n-- \n\n",
75 __PACKAGE__, scalar localtime;
77 for my $table ( keys %{ $data } ) {
78 debug( "Looking at table '$table'\n" );
79 my $table_data = $data->{$table};
81 $table_data->{'fields'}->{$a}->{'order'}
83 $table_data->{'fields'}->{$b}->{'order'}
84 } keys %{ $table_data->{'fields'} };
86 $create .= "--\n-- Table: $table\n--\n";
87 $create .= "CREATE TABLE $table (\n";
93 for my $field ( @fields ) {
94 debug("Looking at field '$field'\n");
95 my $field_data = $table_data->{'fields'}->{ $field };
96 my @fdata = ("", $field);
99 push @fdata, sprintf "%s%s",
100 $field_data->{'data_type'},
101 ( defined $field_data->{'size'} )
102 ? "($field_data->{'size'})" : '';
105 push @fdata, "NOT NULL" unless $field_data->{'null'};
107 # Default? XXX Need better quoting!
108 my $default = $field_data->{'default'};
109 if ( defined $default ) {
110 push @fdata, "DEFAULT '$default'";
111 # if (int $default eq "$default") {
112 # push @fdata, "DEFAULT $default";
114 # push @fdata, "DEFAULT '$default'";
119 push @fdata, "auto_increment" if $field_data->{'is_auto_inc'};
122 push @fdata, "PRIMARY KEY" if $field_data->{'is_primary_key'};
124 push @field_statements, join( " ", @fdata );
127 $create .= join( ",\n", @field_statements );
132 my @indices = @{ $table_data->{'indices'} || [] };
133 for ( my $i = 0; $i <= $#indices; $i++ ) {
135 my $key = $indices[$i];
136 my ( $name, $type, $fields ) = @{ $key }{ qw( name type fields ) };
137 if ( $type eq 'primary_key' ) {
138 $create .= " PRIMARY KEY (@{$fields})"
142 $create .= " KEY $name (@{$fields})"
149 $create .= "\n);\n\n";
158 map { carp "[" . __PACKAGE__ . "] $_" } @_;
167 SQL::Translator::Producer::PostgreSQL - PostgreSQL-specific producer for SQL::Translator
171 Ken Y. Clark E<lt>kclark@cpan.orgE<gt>