handle constraints with no name
[dbsrgits/SQL-Translator-2.0-ish.git] / lib / SQL / Translator / Object / Table.pm
1 use MooseX::Declare;
2 class SQL::Translator::Object::Table extends SQL::Translator::Object {
3     use MooseX::Types::Moose qw(Any Bool HashRef Str);
4     use MooseX::MultiMethods;
5     use SQL::Translator::Types qw(Column Constraint Index Schema Sequence);
6     
7     has 'name' => (
8         is => 'rw',
9         isa => Str,
10         required => 1
11     );
12     
13     has 'columns' => (
14         traits => ['Hash'],
15         is => 'rw',
16         isa => HashRef[Column],
17         handles => {
18             exists_column => 'exists',
19             column_ids    => 'keys',
20             get_columns   => 'values',
21             get_column    => 'get',
22             add_column    => 'set',
23
24             ## compat
25             get_fields    => 'values',
26             fields        => 'keys',
27         },
28         default => sub { my %hash = (); tie %hash, 'Tie::IxHash'; return \%hash },
29     );
30     
31     has 'indexes' => (
32         traits => ['Hash'],
33         is => 'rw',
34         isa => HashRef[Index],
35         handles => {
36             exists_index => 'exists',
37             index_ids    => 'keys',
38             get_indices  => 'values',
39             get_index    => 'get',
40             add_index    => 'set',
41         },
42         default => sub { my %hash = (); tie %hash, 'Tie::IxHash'; return \%hash },
43     );
44     
45     has 'constraints' => (
46         traits => ['Hash'],
47         is => 'rw',
48         isa => HashRef[Constraint],
49         handles => {
50             exists_constraint => 'exists',
51             constraint_ids    => 'keys',
52             get_constraints   => 'values',
53             get_constraint    => 'get',
54             add_constraint    => 'set',
55         },
56         default => sub { my %hash = (); tie %hash, 'Tie::IxHash'; return \%hash },
57     );
58     
59     has 'sequences' => (
60         traits => ['Hash'],
61         is => 'rw',
62         isa => HashRef[Sequence],
63         handles => {
64             exists_sequence => 'exists',
65             sequence_ids    => 'keys',
66             get_sequences   => 'values',
67             get_sequence    => 'get',
68             add_sequence    => 'set',
69         },
70         default => sub { my %hash = (); tie %hash, 'Tie::IxHash'; return \%hash },
71     );
72
73     has 'schema' => (
74         is => 'rw',
75         isa => Schema,
76         weak_ref => 1,
77         required => 1,
78     );
79
80     has 'temporary' => (
81         is => 'rw',
82         isa => Bool,
83         default => 0
84     );
85
86     around add_column(Column $column) { $self->$orig($column->name, $column) }
87     around add_index(Index $index) { $self->$orig($index->name, $index) }
88     around add_sequence(Sequence $sequence) { $self->$orig($sequence->name, $sequence) }
89     around add_constraint(Constraint $constraint) {
90         my $name = $constraint->name;
91         if ($name eq '') {
92             my $index = 0;
93             while ($self->exists_constraint('ANON' . $index)) { $index++ }
94             $name = 'ANON' . $index;
95         }
96         $self->$orig($name, $constraint)
97     }
98
99     multi method primary_key(Any $) { grep /^PRIMARY KEY$/, $_->type for $self->get_constraints }
100     multi method primary_key(Str $column) { $self->get_column($column)->is_primary_key(1) }
101
102     method order { }
103 }