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',
30 remove_column => 'delete',
32 default => sub { my %hash = (); tie %hash, 'Tie::IxHash'; return \%hash },
38 isa => HashRef[Index],
40 exists_index => 'exists',
42 get_indices => 'values',
45 remove_index => 'delete',
47 default => sub { my %hash = (); tie %hash, 'Tie::IxHash'; return \%hash },
50 has 'constraints' => (
53 isa => HashRef[Constraint],
55 exists_constraint => 'exists',
56 constraint_ids => 'keys',
57 get_constraints => 'values',
58 get_constraint => 'get',
59 add_constraint => 'set',
61 default => sub { my %hash = (); tie %hash, 'Tie::IxHash'; return \%hash },
67 isa => HashRef[Sequence],
69 exists_sequence => 'exists',
70 sequence_ids => 'keys',
71 get_sequences => 'values',
72 get_sequence => 'get',
73 add_sequence => 'set',
75 default => sub { my %hash = (); tie %hash, 'Tie::IxHash'; return \%hash },
90 method add_field(Column $column does coerce) { $self->add_column($column) }
92 around add_column(Column $column does coerce) {
93 die "Can't use column name " . $column->name if $self->exists_column($column->name) || $column->name eq '';
94 $column->table($self);
95 return $self->$orig($column->name, $column);
98 around add_constraint(Constraint $constraint does coerce) {
99 my $name = $constraint->name;
102 while ($self->exists_constraint('ANON' . $idx)) { $idx++ }
103 $name = 'ANON' . $idx;
105 $constraint->table($self);
106 $self->$orig($name, $constraint)
109 around add_index(Index $index does coerce) {
110 my $name = $index->name;
113 while ($self->exists_index('ANON' . $idx)) { $idx++ }
114 $name = 'ANON' . $idx;
116 $index->table($self);
117 $self->$orig($name, $index)
120 around add_sequence(Sequence $sequence does coerce) { $self->$orig($sequence->name, $sequence) }
122 multi method primary_key(Any $) { grep /^PRIMARY KEY$/, $_->type for $self->get_constraints }
123 multi method primary_key(Str $column) { $self->get_column($column)->is_primary_key(1) }
125 method is_valid { return $self->get_columns ? 1 : undef }
128 before name($name?) { die "Can't use table name $name, table already exists" if $name && $self->schema->exists_table($name) && $name ne $self->name }
130 multi method drop_column(Column $column, Int :$cascade = 0) {
131 die "Can't drop non-existant table " . $column->name unless $self->exists_column($column->name);
132 $self->remove_column($column->name);
136 multi method drop_column(Str $column, Int :$cascade = 0) {
137 die "Can't drop non-existant table " . $column unless $self->exists_column($column);
138 $self->remove_column($column);