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