coerce Sequence and Constraint into objects
[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',
4f4fd192 31 },
32 default => sub { my %hash = (); tie %hash, 'Tie::IxHash'; return \%hash },
33 );
34
35 has 'indexes' => (
720dcdc3 36 traits => ['Hash'],
4f4fd192 37 is => 'rw',
38 isa => HashRef[Index],
720dcdc3 39 handles => {
40 exists_index => 'exists',
41 index_ids => 'keys',
42 get_indices => 'values',
43 get_index => 'get',
44 add_index => 'set',
48020fcf 45 remove_index => 'delete',
4f4fd192 46 },
2e7ac234 47 default => sub { my %hash = (); tie %hash, 'Tie::IxHash'; return \%hash },
4f4fd192 48 );
49
50 has 'constraints' => (
720dcdc3 51 traits => ['Hash'],
4f4fd192 52 is => 'rw',
53 isa => HashRef[Constraint],
720dcdc3 54 handles => {
55 exists_constraint => 'exists',
56 constraint_ids => 'keys',
57 get_constraints => 'values',
58 get_constraint => 'get',
59 add_constraint => 'set',
4f4fd192 60 },
49063029 61 default => sub { my %hash = (); tie %hash, 'Tie::IxHash'; return \%hash },
4f4fd192 62 );
63
64 has 'sequences' => (
720dcdc3 65 traits => ['Hash'],
4f4fd192 66 is => 'rw',
67 isa => HashRef[Sequence],
720dcdc3 68 handles => {
69 exists_sequence => 'exists',
70 sequence_ids => 'keys',
71 get_sequences => 'values',
72 get_sequence => 'get',
73 add_sequence => 'set',
4f4fd192 74 },
2e7ac234 75 default => sub { my %hash = (); tie %hash, 'Tie::IxHash'; return \%hash },
4f4fd192 76 );
f78a484a 77
2850baeb 78 has 'schema' => (
4f4fd192 79 is => 'rw',
2850baeb 80 isa => Schema,
81 weak_ref => 1,
4f4fd192 82 );
49063029 83
2850baeb 84 has 'temporary' => (
b750d2f1 85 is => 'rw',
2850baeb 86 isa => Bool,
87 default => 0
b750d2f1 88 );
89
de1e817e 90 method add_field(Column $column does coerce) { $self->add_column($column) }
91
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 '';
e1bef8b9 94 $column->table($self);
de1e817e 95 return $self->$orig($column->name, $column);
96 }
e1bef8b9 97
78963c00 98 around add_constraint(Constraint $constraint does coerce) {
f34b818d 99 my $name = $constraint->name;
100 if ($name eq '') {
c27fec89 101 my $idx = 0;
102 while ($self->exists_constraint('ANON' . $idx)) { $idx++ }
103 $name = 'ANON' . $idx;
f34b818d 104 }
e1bef8b9 105 $constraint->table($self);
f34b818d 106 $self->$orig($name, $constraint)
107 }
e1bef8b9 108
706009e5 109 around add_index(Index $index does coerce) {
c27fec89 110 my $name = $index->name;
111 if ($name eq '') {
112 my $idx = 0;
113 while ($self->exists_index('ANON' . $idx)) { $idx++ }
114 $name = 'ANON' . $idx;
115 }
e1bef8b9 116 $index->table($self);
c27fec89 117 $self->$orig($name, $index)
118 }
e1bef8b9 119
78963c00 120 around add_sequence(Sequence $sequence does coerce) { $self->$orig($sequence->name, $sequence) }
51700db2 121
5f184270 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) }
b750d2f1 124
079f0c78 125 method is_valid { return $self->get_columns ? 1 : undef }
b750d2f1 126 method order { }
079f0c78 127
40fb14a9 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 }
079f0c78 129
40fb14a9 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);
133
134 }
135
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);
139 }
4f4fd192 140}