get PostgreSQL passing roundtrip
Justin Hunter [Thu, 3 Sep 2009 18:05:17 +0000 (11:05 -0700)]
lib/SQL/Translator/Parser/DDL/PostgreSQL.pm
lib/SQL/Translator/Producer/SQL/PostgreSQL.pm

index d73412c..1942ac7 100644 (file)
@@ -25,10 +25,10 @@ role SQL::Translator::Parser::DDL::PostgreSQL {
     
         my $result = $parser->startrule($data);
         die "Parse failed.\n" unless defined $result;
-    
+
         my $schema = $translator->schema;
         my @tables = 
-sort { ( $result->{tables}{ $a }{'order'} || 0 ) <=> ( $result->{tables}{ $b }{'order'} || 0 ) }
+         sort { ( $result->{tables}{ $a }{'order'} || 0 ) <=> ( $result->{tables}{ $b }{'order'} || 0 ) }
          keys %{ $result->{tables} };
     
         for my $table_name ( @tables ) {
@@ -55,9 +55,9 @@ sort { ( $result->{tables}{ $a }{'order'} || 0 ) <=> ( $result->{tables}{ $b }{'
                     comments          => $fdata->{'comments'},
                     table             => $table,
                 });
+
                 $table->add_column($field);
-    
-                $table->primary_key( $field->name ) if $fdata->{is_primary_key};
+                $table->primary_key($field->name) if $fdata->{is_primary_key};
     
                 for my $cdata ( @{ $fdata->{constraints} } ) {
                     next unless $cdata->{type} eq 'foreign_key';
@@ -77,18 +77,23 @@ sort { ( $result->{tables}{ $a }{'order'} || 0 ) <=> ( $result->{tables}{ $b }{'
             }
     
             for my $cdata ( @{ $tdata->{'constraints'} || [] } ) {
-                my $constraint = Constraint->new({
-                    name             => $cdata->{name},
-                    type             => $cdata->{type},
-                    fields           => $cdata->{fields},
-                    reference_table  => $cdata->{reference_table},
-                    reference_fields => $cdata->{reference_fields},
-                    match_type       => $cdata->{match_type} || '',
-                    on_delete        => $cdata->{on_delete} || $cdata->{on_delete_do},
-                    on_update        => $cdata->{on_update} || $cdata->{on_update_do},
-                    expression       => $cdata->{expression},
-                    table            => $table,
-                });
+                my $constraint;
+                if (uc $cdata->{type} eq 'PRIMARY_KEY') {
+                    $constraint = PrimaryKey->new({ name => $cdata->{name} || '', table => $table });
+                    $table->get_column($_)->is_primary_key(1) for @{$cdata->{fields}};
+                } elsif (uc $cdata->{type} eq 'FOREIGN_KEY') {
+                    $constraint = ForeignKey->new({ name => $cdata->{name} || '',
+                                                    table => $table,
+                                                    reference_table => $cdata->{reference_table},
+                                                    reference_columns => $cdata->{reference_fields},
+                                                    on_delete => $cdata->{on_delete} || $cdata->{on_delete_do},
+                                                    on_update => $cdata->{on_update} || $cdata->{on_update_do} });
+                    $table->get_column($_)->is_foreign_key(1) for @{$cdata->{fields}};
+                    $table->get_column($_)->foreign_key_reference($constraint) for @{$cdata->{fields}};
+                } else {
+                    $constraint = Constraint->new({ name => $cdata->{name} || '', type => uc $cdata->{type}, table => $table });
+                }
+                $constraint->add_column($table->get_column($_)) for @{$cdata->{fields}};
                 $table->add_constraint($constraint);
             }
         }
index f3c6272..4a4530e 100644 (file)
@@ -296,7 +296,6 @@ method create_table(Table $table, $options?) {
         push @fks, @$fks;
     }
 
-
     my $temporary = "";
 
     if(exists $table->{extra}{temporary}) {
@@ -317,9 +316,9 @@ method create_table(Table $table, $options?) {
     $create_statement .= join(";\n", @type_defs) . ";\n"
         if $postgres_version >= 8.3 && scalar @type_defs;
     $create_statement .= qq[CREATE ${temporary}TABLE $qt$table_name_ur$qt (\n].
-                            join( ",\n", map { "  $_" } @field_defs, @constraint_defs ).
-                            "\n)"
-                            ;
+                            join( ",\n", map { "  $_" } @field_defs, @constraint_defs ) .
+                            "\n)" ;
+
     $create_statement .= @index_defs ? ';' : q{};
     $create_statement .= ( $create_statement =~ /;$/ ? "\n" : q{} )
         . join(";\n", @index_defs);
@@ -489,13 +488,12 @@ method create_constraint(Constraint $c, $options?) {
         map { $_ =~ s/\(.+\)//; $_ }
     map { $qt ? $_ : $self->unreserve( $_, $table_name )}
     $c->fields;
-
     my @rfields     = 
         map { $_ =~ s/\(.+\)//; $_ }
     map { $qt ? $_ : $self->unreserve( $_, $table_name )}
     $c->reference_fields;
-
     return ([], []) if !@fields && $c->type ne CHECK_C;
+
     my $def_start = $name ? qq[CONSTRAINT "$name" ] : '';
     if ( $c->type eq PRIMARY_KEY ) {
         push @constraint_defs, "${def_start}PRIMARY KEY ".
@@ -520,10 +518,10 @@ method create_constraint(Constraint $c, $options?) {
         }
 
         if ( $c->match_type ) {
-            $def .= ' MATCH ' . 
-                ( $c->match_type =~ /full/i ) ? 'FULL' : 'PARTIAL';
+            $def .= ' MATCH ' . ( $c->match_type =~ /full/i ) ? 'FULL' : 'PARTIAL';
         }
 
+=cut
         if ( $c->on_delete ) {
             $def .= ' ON DELETE '.join( ' ', $c->on_delete );
         }
@@ -531,7 +529,7 @@ method create_constraint(Constraint $c, $options?) {
         if ( $c->on_update ) {
             $def .= ' ON UPDATE '.join( ' ', $c->on_update );
         }
-
+=cut
         if ( $c->deferrable ) {
             $def .= ' DEFERRABLE';
         }