package SQL::Translator::Producer::PostgreSQL;
# -------------------------------------------------------------------
-# $Id: PostgreSQL.pm,v 1.24 2006-07-23 14:03:52 schiffbruechige Exp $
+# $Id: PostgreSQL.pm,v 1.25 2006-08-04 21:38:20 schiffbruechige Exp $
# -------------------------------------------------------------------
# Copyright (C) 2002-4 SQLFairy Authors
#
use strict;
use warnings;
use vars qw[ $DEBUG $WARN $VERSION ];
-$VERSION = sprintf "%d.%02d", q$Revision: 1.24 $ =~ /(\d+)\.(\d+)/;
+$VERSION = sprintf "%d.%02d", q$Revision: 1.25 $ =~ /(\d+)\.(\d+)/;
$DEBUG = 1 unless defined $DEBUG;
use SQL::Translator::Schema::Constants;
$output .= header_comment unless ($no_comments);
# my %used_index_names;
- my @table_defs;
+ my (@table_defs, @fks);
for my $table ( $schema->get_tables ) {
-
- push @table_defs, create_table($table, { quote_table_names => $qt,
- quote_field_names => $qf,
- no_comments => $no_comments,
- add_drop_table => $add_drop_table,});
-
-# $output .= join( "\n\n",
-# @comments,
-# @sequence_defs,
-# $create_statement,
-# @index_defs,
-# ''
- # );
+
+ my ($table_def, $fks) = create_table($table,
+ { quote_table_names => $qt,
+ quote_field_names => $qf,
+ no_comments => $no_comments,
+ add_drop_table => $add_drop_table,});
+ push @table_defs, $table_def;
+ push @fks, @$fks;
+
}
$output = join("\n\n", @table_defs);
+ if ( @fks ) {
+ $output .= "--\n-- Foreign Key Definitions\n--\n\n" unless $no_comments;
+ $output .= join( "\n\n", @fks );
+ }
if ( $WARN ) {
if ( %truncated ) {
my $table_name = $table->name or next;
$table_name = mk_name( $table_name, '', undef, 1 );
- my $table_name_ur = $qt ? unreserve($table_name) : $table_name;
+ my $table_name_ur = $qt ? $table_name : unreserve($table_name);
$table->name($table_name_ur);
# print STDERR "$table_name table_name\n";
join( ",\n", map { " $_" } @field_defs, @constraint_defs ).
"\n);"
;
- if ( @fks ) {
- $create_statement .= "--\n-- Foreign Key Definitions\n--\n\n" unless $no_comments;
- $create_statement .= join( "\n\n", @fks );
- }
$create_statement .= "\n" . join(";\n", @index_defs) . "\n";
- return $create_statement;
+ return $create_statement, \@fks;
}
{
my $field_name = mk_name(
$field->name, '', $field_name_scope{$table_name}, 1
);
- my $field_name_ur = $qf ? unreserve($field_name, $table_name ) : $field_name;
+ my $field_name_ur = $qf ? $field_name : unreserve($field_name, $table_name );
$field->name($field_name_ur);
my $field_comments = $field->comments
? "-- " . $field->comments . "\n "
my $qt = $options->{quote_table_names} ||'';
my $qf = $options->{quote_field_names} ||'';
my $table_name = $index->table->name;
- my $table_name_ur = $qt ? unreserve($table_name) : $table_name;
+# my $table_name_ur = $qt ? unreserve($table_name) : $table_name;
my ($index_def, @constraint_defs);
my $type = $index->type || NORMAL;
my @fields =
map { $_ =~ s/\(.+\)//; $_ }
- map { $qt ? unreserve($_, $table_name ) : $_ }
+ map { $qt ? $_ : unreserve($_, $table_name ) }
$index->fields;
next unless @fields;
}
elsif ( $type eq NORMAL ) {
$index_def =
- "CREATE INDEX ${qf}${name}${qf} on ${qt}${table_name_ur}${qt} (".
+ "CREATE INDEX ${qf}${name}${qf} on ${qt}${table_name}${qt} (".
join( ', ', map { qq[$qf$_$qf] } @fields ).
');'
;
my @fields =
map { $_ =~ s/\(.+\)//; $_ }
- map { $qt ? unreserve( $_, $table_name ) : $_}
+ map { $qt ? $_ : unreserve( $_, $table_name )}
$c->fields;
my @rfields =
map { $_ =~ s/\(.+\)//; $_ }
- map { $qt ? unreserve( $_, $table_name ) : $_}
+ map { $qt ? $_ : unreserve( $_, $table_name )}
$c->reference_fields;
next if !@fields && $c->type ne CHECK_C;
if ( defined $size[0] && $size[0] > 0 ) {
$data_type .= '(' . join( ',', @size ) . ')';
}
+ elsif (defined $size[0] && $data_type eq 'timestamp' ) {
+ $data_type .= '(' . join( ',', @size ) . ')';
+ }
return $data_type;
use Test::SQL::Translator qw(maybe_plan);
BEGIN {
- maybe_plan(119, 'SQL::Translator::Parser::PostgreSQL');
+ maybe_plan(117, 'SQL::Translator::Parser::PostgreSQL');
SQL::Translator::Parser::PostgreSQL->import('parse');
}
f_tz timestamp,
f_text text,
f_fk1 integer not null references t_test2 (f_id),
- f_dropped text
+ f_dropped text,
+ f_timestamp timestamp(0) with time zone,
+ f_timestamp2 timestamp without time zone
);
create table t_test2 (
alter table t_test1 alter column f_char drop default;
-- The following are allowed by the grammar
- -- but won't do anything... - ky
+ -- but won\'t do anything... - ky
alter table t_text1 alter column f_char set not null;
is( $t1->comments, 'comment on t_test1', 'Table comment exists' );
my @t1_fields = $t1->get_fields;
-is( scalar @t1_fields, 11, '11 fields in t_test1' );
+is( scalar @t1_fields, 13, '13 fields in t_test1' );
my $f1 = shift @t1_fields;
is( $f1->name, 'f_serial', 'First field is "f_serial"' );
is( $fk_ref1->reference_table, 't_test2', 'FK is to "t_test2" table' );
my $f11 = shift @t1_fields;
-is( $f11->name, 'f_fk2', 'Eleventh field is "f_fk2"' );
-is( $f11->data_type, 'integer', 'Field is an integer' );
+is( $f11->name, 'f_timestamp', 'Eleventh field is "f_timestamp"' );
+is( $f11->data_type, 'timestamp', 'Field is a timestamp' );
is( $f11->is_nullable, 1, 'Field can be null' );
-is( $f11->size, 10, 'Size is "10"' );
-is( $f11->default_value, 'FOO', 'Default value is "FOO"' );
+is( $f11->size, 0, 'Size is "0"' );
+is( $f11->default_value, undef, 'Default value is "undef"' );
is( $f11->is_primary_key, 0, 'Field is not PK' );
-is( $f11->is_foreign_key, 1, 'Field is a FK' );
-my $fk_ref2 = $f11->foreign_key_reference;
-isa_ok( $fk_ref2, 'SQL::Translator::Schema::Constraint', 'FK' );
-is( $fk_ref2->reference_table, 't_test2', 'FK is to "t_test2" table' );
+is( $f11->is_foreign_key, 0, 'Field is not FK' );
+# my $fk_ref2 = $f11->foreign_key_reference;
+# isa_ok( $fk_ref2, 'SQL::Translator::Schema::Constraint', 'FK' );
+# is( $fk_ref2->reference_table, 't_test2', 'FK is to "t_test2" table' );
my @t1_constraints = $t1->get_constraints;
is( scalar @t1_constraints, 8, '8 constraints on t_test1' );
#=============================================================================
BEGIN {
- maybe_plan(150, 'SQL::Translator::Parser::XML::SQLFairy');
+ maybe_plan(162, 'SQL::Translator::Parser::XML::SQLFairy');
}
my $testschema = "$Bin/data/xml/schema.xml";
is_nullable => 1,
comments => "Hello emptytagdef",
},
+ {
+ name => "timest",
+ data_type => "timestamp",
+ size => "0",
+ is_nullable => 1,
+ },
],
constraints => [
{
order: 7
table: Basic
+ timest
+ data_type: timestamp
+ size: 0
+ is_nullable: 1
+ default_value:
+ is_primary_key: 0
+ is_unique: 0
+ is_auto_increment: 0
+ is_foreign_key: 0
+ foreign_key_reference:
+ is_valid: 1
+ order: 8
+ table: Basic
+
Indices
titleindex
Basic
------
-Fields: id title description email explicitnulldef explicitemptystring emptytagdef
+Fields: id title description email explicitnulldef explicitemptystring emptytagdef timest
explicitnulldef VARCHAR(0),
explicitemptystring VARCHAR(0) DEFAULT '',
emptytagdef VARCHAR(0) DEFAULT '',
+timest TIMESTAMP,
CONSTRAINT emailuniqueindex UNIQUE (email) ,
PRIMARY KEY(id)
);
explicitnulldef VARCHAR(0),
explicitemptystring VARCHAR(0) DEFAULT '',
emptytagdef VARCHAR(0) DEFAULT '',
+timest TIMESTAMP,
CONSTRAINT emailuniqueindex UNIQUE (email) ,
PRIMARY KEY(id)
);|,
'CREATE TRIGGER foo_trigger after insert ON Basic REFERENCING OLD AS oldrow NEW AS newrow FOR EACH ROW MODE DB2SQL update modified=timestamp();'
];
-is_deeply(\@sql, $want, 'Got correct DB2 statements in list context');
\ No newline at end of file
+is_deeply(\@sql, $want, 'Got correct DB2 statements in list context');
"explicitemptystring" character varying DEFAULT '',
-- Hello emptytagdef
"emptytagdef" character varying DEFAULT '',
+ "timest" timestamp(0),
PRIMARY KEY ("id"),
Constraint "emailuniqueindex" UNIQUE ("email")
);
explicitnulldef varchar,
explicitemptystring varchar DEFAULT '',
-- Hello emptytagdef
- emptytagdef varchar DEFAULT ''
+ emptytagdef varchar DEFAULT '',
+ timest timestamp
);
CREATE INDEX titleindex_Basic on Basic (title);
data_type="varchar" order="7" default_value="" >
<comments>Hello emptytagdef</comments>
</field>
+ <field name="timest" size="0"
+ data_type="timestamp" order="7" >
+ </field>
</fields>
<indices>