initial xml test
[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,
86 required => 1,
4f4fd192 87 );
49063029 88
2850baeb 89 has 'temporary' => (
b750d2f1 90 is => 'rw',
2850baeb 91 isa => Bool,
92 default => 0
b750d2f1 93 );
94
de1e817e 95 method add_field(Column $column does coerce) { $self->add_column($column) }
96
97 around add_column(Column $column does coerce) {
98 die "Can't use column name " . $column->name if $self->exists_column($column->name) || $column->name eq '';
99 return $self->$orig($column->name, $column);
100 }
f34b818d 101 around add_constraint(Constraint $constraint) {
102 my $name = $constraint->name;
103 if ($name eq '') {
c27fec89 104 my $idx = 0;
105 while ($self->exists_constraint('ANON' . $idx)) { $idx++ }
106 $name = 'ANON' . $idx;
f34b818d 107 }
108 $self->$orig($name, $constraint)
109 }
706009e5 110 around add_index(Index $index does coerce) {
c27fec89 111 my $name = $index->name;
112 if ($name eq '') {
113 my $idx = 0;
114 while ($self->exists_index('ANON' . $idx)) { $idx++ }
115 $name = 'ANON' . $idx;
116 }
117 $self->$orig($name, $index)
118 }
119 around add_sequence(Sequence $sequence) { $self->$orig($sequence->name, $sequence) }
51700db2 120
5f184270 121 multi method primary_key(Any $) { grep /^PRIMARY KEY$/, $_->type for $self->get_constraints }
122 multi method primary_key(Str $column) { $self->get_column($column)->is_primary_key(1) }
b750d2f1 123
079f0c78 124 method is_valid { return $self->get_columns ? 1 : undef }
b750d2f1 125 method order { }
079f0c78 126
40fb14a9 127 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 128
40fb14a9 129 multi method drop_column(Column $column, Int :$cascade = 0) {
130 die "Can't drop non-existant table " . $column->name unless $self->exists_column($column->name);
131 $self->remove_column($column->name);
132
133 }
134
135 multi method drop_column(Str $column, Int :$cascade = 0) {
136 die "Can't drop non-existant table " . $column unless $self->exists_column($column);
137 $self->remove_column($column);
138 }
4f4fd192 139}