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