add ability to drop(remove) a column
[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',
34 fields => 'keys',
4f4fd192 35 },
36 default => sub { my %hash = (); tie %hash, 'Tie::IxHash'; return \%hash },
37 );
38
39 has 'indexes' => (
720dcdc3 40 traits => ['Hash'],
4f4fd192 41 is => 'rw',
42 isa => HashRef[Index],
720dcdc3 43 handles => {
44 exists_index => 'exists',
45 index_ids => 'keys',
46 get_indices => 'values',
47 get_index => 'get',
48 add_index => 'set',
4f4fd192 49 },
2e7ac234 50 default => sub { my %hash = (); tie %hash, 'Tie::IxHash'; return \%hash },
4f4fd192 51 );
52
53 has 'constraints' => (
720dcdc3 54 traits => ['Hash'],
4f4fd192 55 is => 'rw',
56 isa => HashRef[Constraint],
720dcdc3 57 handles => {
58 exists_constraint => 'exists',
59 constraint_ids => 'keys',
60 get_constraints => 'values',
61 get_constraint => 'get',
62 add_constraint => 'set',
4f4fd192 63 },
49063029 64 default => sub { my %hash = (); tie %hash, 'Tie::IxHash'; return \%hash },
4f4fd192 65 );
66
67 has 'sequences' => (
720dcdc3 68 traits => ['Hash'],
4f4fd192 69 is => 'rw',
70 isa => HashRef[Sequence],
720dcdc3 71 handles => {
72 exists_sequence => 'exists',
73 sequence_ids => 'keys',
74 get_sequences => 'values',
75 get_sequence => 'get',
76 add_sequence => 'set',
4f4fd192 77 },
2e7ac234 78 default => sub { my %hash = (); tie %hash, 'Tie::IxHash'; return \%hash },
4f4fd192 79 );
f78a484a 80
2850baeb 81 has 'schema' => (
4f4fd192 82 is => 'rw',
2850baeb 83 isa => Schema,
84 weak_ref => 1,
85 required => 1,
4f4fd192 86 );
49063029 87
2850baeb 88 has 'temporary' => (
b750d2f1 89 is => 'rw',
2850baeb 90 isa => Bool,
91 default => 0
b750d2f1 92 );
93
51700db2 94 around add_column(Column $column) { $self->$orig($column->name, $column) }
f34b818d 95 around add_constraint(Constraint $constraint) {
96 my $name = $constraint->name;
97 if ($name eq '') {
c27fec89 98 my $idx = 0;
99 while ($self->exists_constraint('ANON' . $idx)) { $idx++ }
100 $name = 'ANON' . $idx;
f34b818d 101 }
102 $self->$orig($name, $constraint)
103 }
c27fec89 104 around add_index(Index $index) {
105 my $name = $index->name;
106 if ($name eq '') {
107 my $idx = 0;
108 while ($self->exists_index('ANON' . $idx)) { $idx++ }
109 $name = 'ANON' . $idx;
110 }
111 $self->$orig($name, $index)
112 }
113 around add_sequence(Sequence $sequence) { $self->$orig($sequence->name, $sequence) }
51700db2 114
5f184270 115 multi method primary_key(Any $) { grep /^PRIMARY KEY$/, $_->type for $self->get_constraints }
116 multi method primary_key(Str $column) { $self->get_column($column)->is_primary_key(1) }
b750d2f1 117
079f0c78 118 method is_valid { return $self->get_columns ? 1 : undef }
b750d2f1 119 method order { }
079f0c78 120
40fb14a9 121 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 122
40fb14a9 123 multi method drop_column(Column $column, Int :$cascade = 0) {
124 die "Can't drop non-existant table " . $column->name unless $self->exists_column($column->name);
125 $self->remove_column($column->name);
126
127 }
128
129 multi method drop_column(Str $column, Int :$cascade = 0) {
130 die "Can't drop non-existant table " . $column unless $self->exists_column($column);
131 $self->remove_column($column);
132 }
4f4fd192 133}