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