set table/schema when added to table/schema, rather then on object construction
[dbsrgits/SQL-Translator-2.0-ish.git] / lib / SQL / Translator / Object / Table.pm
CommitLineData
4f4fd192 1use MooseX::Declare;
0c4b09d0 2class SQL::Translator::Object::Table extends SQL::Translator::Object is dirty {
1c607f61 3 use MooseX::Types::Moose qw(Any Bool HashRef Str);
5f184270 4 use MooseX::MultiMethods;
4f4fd192 5 use SQL::Translator::Types qw(Column Constraint Index Schema Sequence);
0c4b09d0 6 clean;
7
8 use overload
9 '""' => sub { shift->name },
10 'bool' => sub { $_[0]->name || $_[0] },
11 fallback => 1,
12 ;
13
4f4fd192 14 has 'name' => (
15 is => 'rw',
16 isa => Str,
17 required => 1
18 );
19
20 has 'columns' => (
720dcdc3 21 traits => ['Hash'],
4f4fd192 22 is => 'rw',
23 isa => HashRef[Column],
720dcdc3 24 handles => {
25 exists_column => 'exists',
26 column_ids => 'keys',
27 get_columns => 'values',
28 get_column => 'get',
29 add_column => 'set',
40fb14a9 30 remove_column => 'delete',
db2e467f 31
32 ## compat
33 get_fields => 'values',
857ab1c2 34 get_field => 'get',
db2e467f 35 fields => 'keys',
4f4fd192 36 },
37 default => sub { my %hash = (); tie %hash, 'Tie::IxHash'; return \%hash },
38 );
39
40 has 'indexes' => (
720dcdc3 41 traits => ['Hash'],
4f4fd192 42 is => 'rw',
43 isa => HashRef[Index],
720dcdc3 44 handles => {
45 exists_index => 'exists',
46 index_ids => 'keys',
47 get_indices => 'values',
48 get_index => 'get',
49 add_index => 'set',
4f4fd192 50 },
2e7ac234 51 default => sub { my %hash = (); tie %hash, 'Tie::IxHash'; return \%hash },
4f4fd192 52 );
53
54 has 'constraints' => (
720dcdc3 55 traits => ['Hash'],
4f4fd192 56 is => 'rw',
57 isa => HashRef[Constraint],
720dcdc3 58 handles => {
59 exists_constraint => 'exists',
60 constraint_ids => 'keys',
61 get_constraints => 'values',
62 get_constraint => 'get',
63 add_constraint => 'set',
4f4fd192 64 },
49063029 65 default => sub { my %hash = (); tie %hash, 'Tie::IxHash'; return \%hash },
4f4fd192 66 );
67
68 has 'sequences' => (
720dcdc3 69 traits => ['Hash'],
4f4fd192 70 is => 'rw',
71 isa => HashRef[Sequence],
720dcdc3 72 handles => {
73 exists_sequence => 'exists',
74 sequence_ids => 'keys',
75 get_sequences => 'values',
76 get_sequence => 'get',
77 add_sequence => 'set',
4f4fd192 78 },
2e7ac234 79 default => sub { my %hash = (); tie %hash, 'Tie::IxHash'; return \%hash },
4f4fd192 80 );
f78a484a 81
2850baeb 82 has 'schema' => (
4f4fd192 83 is => 'rw',
2850baeb 84 isa => Schema,
85 weak_ref => 1,
4f4fd192 86 );
49063029 87
2850baeb 88 has 'temporary' => (
b750d2f1 89 is => 'rw',
2850baeb 90 isa => Bool,
91 default => 0
b750d2f1 92 );
93
de1e817e 94 method add_field(Column $column does coerce) { $self->add_column($column) }
95
96 around add_column(Column $column does coerce) {
97 die "Can't use column name " . $column->name if $self->exists_column($column->name) || $column->name eq '';
e1bef8b9 98 $column->table($self);
de1e817e 99 return $self->$orig($column->name, $column);
100 }
e1bef8b9 101
f34b818d 102 around add_constraint(Constraint $constraint) {
103 my $name = $constraint->name;
104 if ($name eq '') {
c27fec89 105 my $idx = 0;
106 while ($self->exists_constraint('ANON' . $idx)) { $idx++ }
107 $name = 'ANON' . $idx;
f34b818d 108 }
e1bef8b9 109 $constraint->table($self);
f34b818d 110 $self->$orig($name, $constraint)
111 }
e1bef8b9 112
706009e5 113 around add_index(Index $index does coerce) {
c27fec89 114 my $name = $index->name;
115 if ($name eq '') {
116 my $idx = 0;
117 while ($self->exists_index('ANON' . $idx)) { $idx++ }
118 $name = 'ANON' . $idx;
119 }
e1bef8b9 120 $index->table($self);
c27fec89 121 $self->$orig($name, $index)
122 }
e1bef8b9 123
c27fec89 124 around add_sequence(Sequence $sequence) { $self->$orig($sequence->name, $sequence) }
51700db2 125
5f184270 126 multi method primary_key(Any $) { grep /^PRIMARY KEY$/, $_->type for $self->get_constraints }
127 multi method primary_key(Str $column) { $self->get_column($column)->is_primary_key(1) }
b750d2f1 128
079f0c78 129 method is_valid { return $self->get_columns ? 1 : undef }
b750d2f1 130 method order { }
079f0c78 131
40fb14a9 132 before name($name?) { die "Can't use table name $name, table already exists" if $name && $self->schema->exists_table($name) && $name ne $self->name }
079f0c78 133
40fb14a9 134 multi method drop_column(Column $column, Int :$cascade = 0) {
135 die "Can't drop non-existant table " . $column->name unless $self->exists_column($column->name);
136 $self->remove_column($column->name);
137
138 }
139
140 multi method drop_column(Str $column, Int :$cascade = 0) {
141 die "Can't drop non-existant table " . $column unless $self->exists_column($column);
142 $self->remove_column($column);
143 }
4f4fd192 144}