X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=blobdiff_plain;f=lib%2FSQL%2FTranslator%2FObject%2FTable.pm;h=773fc3bce945768b90543172c8d0b7c3898e9e18;hb=00e289412e615f4822630691f17044a18c8ae127;hp=918cd1478d844f48c40e4d5229501aadec70510d;hpb=857ab1c2fe0f14539cc17291384da15abee0be54;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 918cd14..773fc3b 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,72 +21,71 @@ 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', - - ## compat - get_fields => 'values', - get_field => 'get', - fields => 'keys', + 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', + 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', + 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' => ( is => 'rw', isa => Schema, weak_ref => 1, - required => 1, ); has 'temporary' => ( @@ -92,48 +94,111 @@ class SQL::Translator::Object::Table extends SQL::Translator::Object is dirty { default => 0 ); - method add_field(Column $column does coerce) { $self->add_column($column) } + has '_order' => ( + is => 'rw', + isa => Int, + ); around add_column(Column $column does coerce) { die "Can't use column name " . $column->name if $self->exists_column($column->name) || $column->name eq ''; - return $self->$orig($column->name, $column); + $column->table($self); + $self->$orig($column->name, $column); + return $self->get_column($column->name); } - around add_constraint(Constraint $constraint) { + + around add_constraint(Constraint $constraint does coerce) { my $name = $constraint->name; if ($name eq '') { my $idx = 0; while ($self->exists_constraint('ANON' . $idx)) { $idx++ } $name = 'ANON' . $idx; } - $self->$orig($name, $constraint) + $constraint->table($self); + 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) { + + around add_index(Index $index does coerce) { my $name = $index->name; if ($name eq '') { my $idx = 0; while ($self->exists_index('ANON' . $idx)) { $idx++ } $name = 'ANON' . $idx; } - $self->$orig($name, $index) + $index->table($self); + $self->$orig($name, $index); + return $self->get_index($name); + } + + around add_sequence(Sequence $sequence does coerce) { + $self->$orig($sequence->name, $sequence); + return $self->get_sequence($sequence->name); + } + + multi method primary_key { + my $constraints = $self->constraints; + for my $key (keys %$constraints) { + return $constraints->{$key} if $constraints->{$key}{type} eq 'PRIMARY KEY'; + } + return undef; + } + + 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_field({ name => $column }) unless $primary_key->exists_column($column); ## FIX ME, change back to add_column once around add_column(coerce .. ) works + return $primary_key; } - around add_sequence(Sequence $sequence) { $self->$orig($sequence->name, $sequence) } - 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 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->get_columns ? 1 : undef } - method order { } before name($name?) { die "Can't use table name $name, table already exists" if $name && $self->schema->exists_table($name) && $name ne $self->name } - multi method drop_column(Column $column, Int :$cascade = 0) { - die "Can't drop non-existant table " . $column->name unless $self->exists_column($column->name); - $self->remove_column($column->name); + around remove_column(Column|Str $column, Int :$cascade = 0) { + my $name = is_Column($column) ? $column->name : $column; + die "Can't drop non-existant column " . $name unless $self->exists_column($name); + $self->$orig($name); + } + around remove_index(Index|Str $index) { + my $name = is_Index($index) ? $index->name : $index; + die "Can't drop non-existant index " . $name unless $self->exists_index($name); + $self->$orig($name); } - multi method drop_column(Str $column, Int :$cascade = 0) { - die "Can't drop non-existant table " . $column unless $self->exists_column($column); - $self->remove_column($column); + around remove_constraint(Constraint|Str $constraint) { + my $name = is_Constraint($constraint) ? $constraint->name : $constraint; + 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}; + + $args->{columns}{$_} = SQL::Translator::Object::Column->new( name => $_ ) for @$fields; + + return $args; } }