2 class SQL::Translator::Object::Table extends SQL::Translator::Object is dirty {
3 use MooseX::Types::Moose qw(Any Bool HashRef Str);
4 use MooseX::MultiMethods;
5 use SQL::Translator::Types qw(Column Constraint Index Schema Sequence);
9 '""' => sub { shift->name },
10 'bool' => sub { $_[0]->name || $_[0] },
23 isa => HashRef[Column],
25 exists_column => 'exists',
27 get_columns => 'values',
32 get_fields => 'values',
35 default => sub { my %hash = (); tie %hash, 'Tie::IxHash'; return \%hash },
41 isa => HashRef[Index],
43 exists_index => 'exists',
45 get_indices => 'values',
49 default => sub { my %hash = (); tie %hash, 'Tie::IxHash'; return \%hash },
52 has 'constraints' => (
55 isa => HashRef[Constraint],
57 exists_constraint => 'exists',
58 constraint_ids => 'keys',
59 get_constraints => 'values',
60 get_constraint => 'get',
61 add_constraint => 'set',
63 default => sub { my %hash = (); tie %hash, 'Tie::IxHash'; return \%hash },
69 isa => HashRef[Sequence],
71 exists_sequence => 'exists',
72 sequence_ids => 'keys',
73 get_sequences => 'values',
74 get_sequence => 'get',
75 add_sequence => 'set',
77 default => sub { my %hash = (); tie %hash, 'Tie::IxHash'; return \%hash },
93 around add_column(Column $column) { $self->$orig($column->name, $column) }
94 around add_constraint(Constraint $constraint) {
95 my $name = $constraint->name;
98 while ($self->exists_constraint('ANON' . $idx)) { $idx++ }
99 $name = 'ANON' . $idx;
101 $self->$orig($name, $constraint)
103 around add_index(Index $index) {
104 my $name = $index->name;
107 while ($self->exists_index('ANON' . $idx)) { $idx++ }
108 $name = 'ANON' . $idx;
110 $self->$orig($name, $index)
112 around add_sequence(Sequence $sequence) { $self->$orig($sequence->name, $sequence) }
114 multi method primary_key(Any $) { grep /^PRIMARY KEY$/, $_->type for $self->get_constraints }
115 multi method primary_key(Str $column) { $self->get_column($column)->is_primary_key(1) }
117 method is_valid { return $self->get_columns ? 1 : undef }
120 before name($name?) { die "Can't use table name $name" if $name && $self->schema->exists_table($name); }