X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=blobdiff_plain;f=lib%2FSQL%2FTranslator%2FObject%2FTable.pm;h=2655c19478442746aec3a89de9fed3b6367423ca;hb=b750d2f185523d7425967b675f9dd51ea659ef83;hp=2a13e8a03abf0ac2a2de9844b7602676901c22e4;hpb=cc0972be996be6e521f94bb0a6b12b7fb82c2574;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 2a13e8a..2655c19 100644 --- a/lib/SQL/Translator/Object/Table.pm +++ b/lib/SQL/Translator/Object/Table.pm @@ -1,74 +1,123 @@ -package SQL::Translator::Object::Table; -use namespace::autoclean; -use Moose; -use MooseX::Types::Moose qw(HashRef Str); -use MooseX::AttributeHelpers; -use SQL::Translator::Types qw(Column Constraint Index Schema Sequence); -use SQL::Translator::Object::Schema; -extends 'SQL::Translator::Object'; +use MooseX::Declare; +class SQL::Translator::Object::Table { + use MooseX::Types::Moose qw(ArrayRef Bool HashRef Maybe Str); + use MooseX::AttributeHelpers; + use SQL::Translator::Types qw(Column Constraint Index Schema Sequence); + use SQL::Translator::Object::Schema; + extends 'SQL::Translator::Object'; + + has 'name' => ( + is => 'rw', + isa => Str, + required => 1 + ); + + has 'columns' => ( + metaclass => 'Collection::Hash', + is => 'rw', + isa => HashRef[Column], + provides => { + exists => 'exists_column', + keys => 'column_ids', + values => 'get_columns', + get => 'get_column', + }, + curries => { + set => { + add_column => sub { + my ($self, $body, $column) = @_; + $self->$body($column->name, $column); + } + } + }, + default => sub { my %hash = (); tie %hash, 'Tie::IxHash'; return \%hash }, + ); + + has 'indexes' => ( + metaclass => 'Collection::Hash', + is => 'rw', + isa => HashRef[Index], + provides => { + exists => 'exists_index', + keys => 'index_ids', + values => 'get_indices', + get => 'get_index', + }, + curries => { + set => { + add_index => sub { + my ($self, $body, $index) = @_; + $self->$body($index->name, $index); + } + } + }, + default => sub { my %hash = (); tie %hash, 'Tie::IxHash'; return \%hash }, + ); + + has 'constraints' => ( + metaclass => 'Collection::Hash', + is => 'rw', + isa => HashRef[Constraint], + provides => { + exists => 'exists_constraint', + keys => 'constraint_ids', + values => 'get_constraints', + get => 'get_constraint', + }, + curries => { + set => { + add_constraint => sub { + my ($self, $body, $constraint) = @_; + $self->$body($constraint->name, $constraint); + } + } + }, + default => sub { my %hash = (); tie %hash, 'Tie::IxHash'; return \%hash }, + ); + + has 'sequences' => ( + metaclass => 'Collection::Hash', + is => 'rw', + isa => HashRef[Sequence], + provides => { + exists => 'exists_sequence', + keys => 'sequence_ids', + values => 'get_sequences', + get => 'get_sequence', + }, + curries => { + set => { + add_sequence => sub { + my ($self, $body, $sequence) = @_; + $self->$body($sequence->name, $sequence); + } + } + }, + default => sub { my %hash = (); tie %hash, 'Tie::IxHash'; return \%hash }, + ); -has 'name' => ( - is => 'rw', - isa => Str, - required => 1 -); + has 'comments' => ( + is => 'rw', + isa => Maybe[Str], + ); + + has 'temporary' => ( + is => 'rw', + isa => Bool, + default => 0 + ); -has 'columns' => ( - metaclass => 'Collection::Hash', - is => 'rw', - isa => HashRef[Column], - provides => { - exists => 'exists_column', - keys => 'column_ids', - get => 'get_column', - }, - curries => { set => { add_column => sub { my ($self, $body, $column) = @_; $self->$body($column->name, $column); } } }, - default => sub { {} }, - required => 0 -); + has 'options' => ( + is => 'rw', + isa => ArrayRef, + auto_deref => 1 + ); -has 'indexes' => ( - metaclass => 'Collection::Hash', - is => 'rw', - isa => HashRef[Index], - provides => { - exists => 'exists_index', - keys => 'index_ids', - get => 'get_index', - }, - curries => { set => { add_index => sub { my ($self, $body, $index) = @_; $self->$body($index->name, $index); } } }, - default => sub { {} }, - required => 0 -); + method get_fields { return $self->get_columns } + method fields { return $self->column_ids } + method primary_key(Str $column) { + $self->get_column($column)->is_primary_key(1); + } -has 'constraints' => ( - metaclass => 'Collection::Hash', - is => 'rw', - isa => HashRef[Constraint], - provides => { - exists => 'exists_constraint', - keys => 'constraint_ids', - get => 'get_constraint', - }, - curries => { set => { add_constraint => sub { my ($self, $body, $constraint) = @_; $self->$body($constraint->name, $constraint); } } }, - default => sub { {} }, - required => 0 -); - -has 'sequences' => ( - metaclass => 'Collection::Hash', - is => 'rw', - isa => HashRef[Sequence], - provides => { - exists => 'exists_sequence', - keys => 'sequence_ids', - get => 'get_sequence', - }, - curries => { set => { add_sequence => sub { my ($self, $body, $sequence) = @_; $self->$body($sequence->name, $sequence); } } }, - default => sub { {} }, - required => 0 -); - -__PACKAGE__->meta->make_immutable; - -1; + method order { } +}