add is_valid check and make sure you can't add a table with the same 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 is dirty {
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     clean;
7
8     use overload
9         '""'     => sub { shift->name },
10         'bool'   => sub { $_[0]->name || $_[0] },
11         fallback => 1,
12     ;
13
14     has 'name' => (
15         is => 'rw',
16         isa => Str,
17         required => 1
18     );
19     
20     has 'columns' => (
21         traits => ['Hash'],
22         is => 'rw',
23         isa => HashRef[Column],
24         handles => {
25             exists_column => 'exists',
26             column_ids    => 'keys',
27             get_columns   => 'values',
28             get_column    => 'get',
29             add_column    => 'set',
30
31             ## compat
32             get_fields    => 'values',
33             fields        => 'keys',
34         },
35         default => sub { my %hash = (); tie %hash, 'Tie::IxHash'; return \%hash },
36     );
37     
38     has 'indexes' => (
39         traits => ['Hash'],
40         is => 'rw',
41         isa => HashRef[Index],
42         handles => {
43             exists_index => 'exists',
44             index_ids    => 'keys',
45             get_indices  => 'values',
46             get_index    => 'get',
47             add_index    => 'set',
48         },
49         default => sub { my %hash = (); tie %hash, 'Tie::IxHash'; return \%hash },
50     );
51     
52     has 'constraints' => (
53         traits => ['Hash'],
54         is => 'rw',
55         isa => HashRef[Constraint],
56         handles => {
57             exists_constraint => 'exists',
58             constraint_ids    => 'keys',
59             get_constraints   => 'values',
60             get_constraint    => 'get',
61             add_constraint    => 'set',
62         },
63         default => sub { my %hash = (); tie %hash, 'Tie::IxHash'; return \%hash },
64     );
65     
66     has 'sequences' => (
67         traits => ['Hash'],
68         is => 'rw',
69         isa => HashRef[Sequence],
70         handles => {
71             exists_sequence => 'exists',
72             sequence_ids    => 'keys',
73             get_sequences   => 'values',
74             get_sequence    => 'get',
75             add_sequence    => 'set',
76         },
77         default => sub { my %hash = (); tie %hash, 'Tie::IxHash'; return \%hash },
78     );
79
80     has 'schema' => (
81         is => 'rw',
82         isa => Schema,
83         weak_ref => 1,
84         required => 1,
85     );
86
87     has 'temporary' => (
88         is => 'rw',
89         isa => Bool,
90         default => 0
91     );
92
93     around add_column(Column $column) { $self->$orig($column->name, $column) }
94     around add_constraint(Constraint $constraint) {
95         my $name = $constraint->name;
96         if ($name eq '') {
97             my $idx = 0;
98             while ($self->exists_constraint('ANON' . $idx)) { $idx++ }
99             $name = 'ANON' . $idx;
100         }
101         $self->$orig($name, $constraint)
102     }
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) }
113
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) }
116
117     method is_valid { return $self->get_columns ? 1 : undef }
118     method order { }
119
120     before name($name?) { die "Can't use table name $name" if $name && $self->schema->exists_table($name); }
121
122 }