X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=blobdiff_plain;f=lib%2FSQL%2FTranslator%2FParser%2FDDL%2FSQLite.pm;h=c5406c5b8883f74eabf54ef93e827dd797e82fd1;hb=7f897a63d37d3096c6535a2417aefe599057400f;hp=5c959b56696d8b810b3f7c6790c31beb9fd99ccb;hpb=29628de67a8459f0188af21210b31ebc758bd5ab;p=dbsrgits%2FSQL-Translator-2.0-ish.git diff --git a/lib/SQL/Translator/Parser/DDL/SQLite.pm b/lib/SQL/Translator/Parser/DDL/SQLite.pm index 5c959b5..c5406c5 100644 --- a/lib/SQL/Translator/Parser/DDL/SQLite.pm +++ b/lib/SQL/Translator/Parser/DDL/SQLite.pm @@ -42,7 +42,7 @@ role SQL::Translator::Parser::DDL::SQLite { for my $table_name ( @tables ) { my $tdata = $result->{tables}{ $table_name }; - my $table = Table->new({ name => $tdata->{name}, schema => $schema }); + my $table = Table->new({ name => $tdata->{name}, schema => $schema }); $table->comments( $tdata->{comments}->flatten ) if $tdata->{comments}; $schema->add_table($table); @@ -54,14 +54,14 @@ role SQL::Translator::Parser::DDL::SQLite { size => $fdata->{size}, default_value => $fdata->{default}, is_auto_increment => $fdata->{is_auto_inc}, - is_nullable => $fdata->{is_nullable}, + is_nullable => $fdata->{is_primary_key} ? 0 : $fdata->{is_nullable}, comments => $fdata->{comments}, table => $table, }); + $table->add_column($field); - $table->primary_key( $field->name ) if $fdata->{is_primary_key}; - + for my $cdata ( @{ $fdata->{constraints} } ) { next unless $cdata->{type} eq 'foreign_key'; $cdata->{fields} ||= [ $field->name ]; @@ -84,8 +84,12 @@ role SQL::Translator::Parser::DDL::SQLite { my $constraint; if (uc $cdata->{type} eq 'PRIMARY_KEY') { $constraint = PrimaryKey->new({ name => $cdata->{name} || 'primary_key', table => $table }); - $constraint->add_column($table->get_column($_)) for @{$cdata->{fields}}; - $table->get_column($_)->is_primary_key(1) for @{$cdata->{fields}}; + + for my $field (@{$cdata->{fields}}) { + $table->get_column($field)->is_primary_key(1); + $table->get_column($field)->is_nullable(0); + $constraint->add_column($table->get_column($field)); + } } elsif (uc $cdata->{type} eq 'FOREIGN_KEY') { $constraint = ForeignKey->new({ name => $cdata->{name} || 'foreign_key', table => $table, @@ -93,12 +97,19 @@ role SQL::Translator::Parser::DDL::SQLite { reference_columns => ref $cdata->{reference_fields} ? $cdata->{reference_fields} : [ $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}}; + for my $field (@{$cdata->{fields}}) { + $table->get_column($field)->is_foreign_key(1); + $table->get_column($field)->foreign_key_reference($constraint); + $constraint->add_column($table->get_column($field)); + } } else { $constraint = Constraint->new({ name => $cdata->{name} || 'constraint', type => uc $cdata->{type}, table => $table }); + if (uc $cdata->{type} eq 'UNIQUE') { + $table->get_column($_)->is_unique(1) for @{$cdata->{fields}}; + } $constraint->add_column($table->get_column($_)) for @{$cdata->{fields}}; } + $table->add_constraint($constraint); } }