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 ) {
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';
}
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);
}
}
push @fks, @$fks;
}
-
my $temporary = "";
if(exists $table->{extra}{temporary}) {
$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);
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 ".
}
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 );
}
if ( $c->on_update ) {
$def .= ' ON UPDATE '.join( ' ', $c->on_update );
}
-
+=cut
if ( $c->deferrable ) {
$def .= ' DEFERRABLE';
}