X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=blobdiff_plain;f=lib%2FSQL%2FTranslator%2FObject%2FTable.pm;h=13899338d0b1ed83432cf11dd047e718a2bab2d5;hb=82e209aa083cf5f796661c71a862ebeb2da67e64;hp=b6b78d6db6ee683a6f454e1fe3dcd82f9f6a3022;hpb=bebe11d511e3f3b86b6965e380a6bc8bd38e73b7;p=dbsrgits%2FSQL-Translator-2.0-ish.git diff --git a/lib/SQL/Translator/Object/Table.pm b/lib/SQL/Translator/Object/Table.pm index b6b78d6..1389933 100644 --- a/lib/SQL/Translator/Object/Table.pm +++ b/lib/SQL/Translator/Object/Table.pm @@ -1,8 +1,11 @@ use MooseX::Declare; class SQL::Translator::Object::Table extends SQL::Translator::Object is dirty { - use MooseX::Types::Moose qw(Any Bool HashRef Str); + use MooseX::Types::Moose qw(Any Bool HashRef Int Str); use MooseX::MultiMethods; - use SQL::Translator::Types qw(Column Constraint Index Schema Sequence); + use SQL::Translator::Types qw(Column Constraint Index Schema Sequence ColumnHash ConstraintHash IndexHash SequenceHash IxHash); + use SQL::Translator::Object::Column; + use SQL::Translator::Object::Constraint; + use Tie::IxHash; clean; use overload @@ -18,62 +21,65 @@ class SQL::Translator::Object::Table extends SQL::Translator::Object is dirty { ); has 'columns' => ( - traits => ['Hash'], is => 'rw', - isa => HashRef[Column], + isa => IxHash, #ColumnHash, handles => { - exists_column => 'exists', - column_ids => 'keys', - get_columns => 'values', - get_column => 'get', - add_column => 'set', - remove_column => 'delete', + exists_column => 'EXISTS', + column_ids => 'Keys', + get_columns => 'Values', + get_column => 'FETCH', + add_column => 'STORE', + remove_column => 'DELETE', + has_columns => 'Length', + clear_columns => 'CLEAR', }, - default => sub { my %hash = (); tie %hash, 'Tie::IxHash'; return \%hash }, + coerce => 1, + default => sub { Tie::IxHash->new() } ); has 'indexes' => ( - traits => ['Hash'], is => 'rw', - isa => HashRef[Index], + isa => IxHash, #IndexHash, handles => { - exists_index => 'exists', - index_ids => 'keys', - get_indices => 'values', - get_index => 'get', - add_index => 'set', - remove_index => 'delete', + exists_index => 'EXISTS', + index_ids => 'Keys', + get_indices => 'Values', + get_index => 'FETCH', + add_index => 'STORE', + remove_index => 'DELETE', }, - default => sub { my %hash = (); tie %hash, 'Tie::IxHash'; return \%hash }, + coerce => 1, + default => sub { Tie::IxHash->new() } ); has 'constraints' => ( - traits => ['Hash'], is => 'rw', - isa => HashRef[Constraint], + isa => IxHash, #ConstraintHash, handles => { - exists_constraint => 'exists', - constraint_ids => 'keys', - get_constraints => 'values', - get_constraint => 'get', - add_constraint => 'set', - remove_constraint => 'delete', + exists_constraint => 'EXISTS', + constraint_ids => 'Keys', + get_constraints => 'Values', + get_constraint => 'FETCH', + add_constraint => 'STORE', + remove_constraint => 'DELETE', }, - default => sub { my %hash = (); tie %hash, 'Tie::IxHash'; return \%hash }, + coerce => 1, + default => sub { Tie::IxHash->new() } ); has 'sequences' => ( - traits => ['Hash'], is => 'rw', - isa => HashRef[Sequence], + isa => IxHash, #SequenceHash, handles => { - exists_sequence => 'exists', - sequence_ids => 'keys', - get_sequences => 'values', - get_sequence => 'get', - add_sequence => 'set', + exists_sequence => 'EXISTS', + sequence_ids => 'Keys', + get_sequences => 'Values', + get_sequence => 'FETCH', + add_sequence => 'STORE', + remove_sequence => 'DELETE', }, - default => sub { my %hash = (); tie %hash, 'Tie::IxHash'; return \%hash }, + coerce => 1, + default => sub { Tie::IxHash->new() }, ); has 'schema' => ( @@ -88,13 +94,31 @@ class SQL::Translator::Object::Table extends SQL::Translator::Object is dirty { default => 0 ); + has '_order' => ( + is => 'rw', + isa => Int, + ); + + around get_column(Column $column does coerce) { + $self->$orig($column->name); + } + around add_column(Column $column does coerce) { die "Can't use column name " . $column->name if $self->exists_column($column->name) || $column->name eq ''; $column->table($self); - return $self->$orig($column->name, $column); + $self->$orig($column->name, $column); + return $self->get_column($column->name); } around add_constraint(Constraint $constraint does coerce) { + if ($constraint->type eq 'FOREIGN KEY') { + my @columns = $constraint->get_columns; + die "There are no columns associated with this foreign key constraint." unless scalar @columns; + for my $column (@columns) { + die "Can't use column " . $column->name . ". It doesn't exist!" unless $self->exists_column($column); + } + die "Reference table " . $constraint->reference_table . " does not exist!" unless $self->schema->exists_table($constraint->reference_table); + } my $name = $constraint->name; if ($name eq '') { my $idx = 0; @@ -102,7 +126,11 @@ class SQL::Translator::Object::Table extends SQL::Translator::Object is dirty { $name = 'ANON' . $idx; } $constraint->table($self); - $self->$orig($name, $constraint) + if ($constraint->has_type && $constraint->type eq 'PRIMARY KEY') { + $self->get_column($_)->is_primary_key(1) for $constraint->column_ids; + } + $self->$orig($name, $constraint); + return $self->get_constraint($name); } around add_index(Index $index does coerce) { @@ -113,16 +141,49 @@ class SQL::Translator::Object::Table extends SQL::Translator::Object is dirty { $name = 'ANON' . $idx; } $index->table($self); - $self->$orig($name, $index) + $self->$orig($name, $index); + return $self->get_index($name); } - around add_sequence(Sequence $sequence does coerce) { $self->$orig($sequence->name, $sequence) } + around add_sequence(Sequence $sequence does coerce) { + $self->$orig($sequence->name, $sequence); + return $self->get_sequence($sequence->name); + } - multi method primary_key(Any $) { grep /^PRIMARY KEY$/, $_->type for $self->get_constraints } - multi method primary_key(Str $column) { $self->get_column($column)->is_primary_key(1) } + multi method primary_key { + my $constraints = $self->constraints; + for my $key ($constraints->Keys) { + my $val = $constraints->FETCH($key); + return $val if $val->type eq 'PRIMARY KEY'; + } + return; + } - method is_valid { return $self->get_columns ? 1 : undef } - method order { } + multi method primary_key(Str $column) { + die "Column $column does not exist!" unless $self->exists_column($column); + $self->get_column($column)->is_primary_key(1); + + my $primary_key = $self->primary_key; + unless (defined $primary_key) { + $primary_key = SQL::Translator::Object::Constraint->new({ type => 'PRIMARY KEY' }); + $self->add_constraint($primary_key); + } + $primary_key->add_column( name => $column ) unless $primary_key->exists_column($column); + return $primary_key; + } + + multi method order(Int $order) { $self->_order($order); } + multi method order { + my $order = $self->_order; + unless (defined $order && $order) { + my $tables = Tie::IxHash->new( map { $_->name => $_ } $self->schema->get_tables ); + $order = $tables->Indices($self->name) || 0; $order++; + $self->_order($order); + } + return $order; + } + + method is_valid { return $self->has_columns ? 1 : undef } before name($name?) { die "Can't use table name $name, table already exists" if $name && $self->schema->exists_table($name) && $name ne $self->name } @@ -143,4 +204,16 @@ class SQL::Translator::Object::Table extends SQL::Translator::Object is dirty { die "Can't drop non-existant constraint " . $name unless $self->exists_constraint($name); $self->$orig($name); } + + around BUILDARGS(ClassName $self: @args) { + my $args = $self->$orig(@args); + + my $fields = delete $args->{fields} || []; + $fields = ref($fields) eq 'ARRAY' ? $fields : [ $fields ]; + my $ix_hash = Tie::IxHash->new(); + $ix_hash->STORE($_, SQL::Translator::Object::Column->new( name => $_ )) for @$fields; + $args->{columns} = $ix_hash; + + return $args; + } }