import constants
[dbsrgits/SQL-Translator-2.0-ish.git] / lib / SQL / Translator / Object / Table.pm
1 package SQL::Translator::Object::Table;
2 use namespace::autoclean;
3 use Moose;
4 use MooseX::Types::Moose qw(HashRef Str);
5 use MooseX::AttributeHelpers;
6 use SQL::Translator::Types qw(Column Constraint Index Schema Sequence);
7 use SQL::Translator::Object::Schema;
8 use Tie::IxHash;
9 extends 'SQL::Translator::Object';
10
11 has 'name' => (
12     is => 'rw',
13     isa => Str,
14     required => 1
15 );
16
17 has 'columns' => (
18     metaclass => 'Collection::Hash',
19     is => 'rw',
20     isa => HashRef[Column],
21     provides => {
22         exists => 'exists_column',
23         keys   => 'column_ids',
24         get    => 'get_column',
25     },
26     curries => {
27         set => {
28             add_column => sub {
29                 my ($self, $body, $column) = @_;
30                 $self->$body($column->name, $column);
31             }
32         }
33     },
34     default => sub { my %hash = (); tie %hash, 'Tie::IxHash'; return \%hash },
35 );
36
37 has 'indexes' => (
38     metaclass => 'Collection::Hash',
39     is => 'rw',
40     isa => HashRef[Index],
41     provides => {
42         exists => 'exists_index',
43         keys   => 'index_ids',
44         get    => 'get_index',
45     },
46     curries => {
47         set => {
48             add_index => sub {
49                 my ($self, $body, $index) = @_;
50                 $self->$body($index->name, $index);
51             }
52         }
53     },
54     default => sub { {} },
55 );
56
57 has 'constraints' => (
58     metaclass => 'Collection::Hash',
59     is => 'rw',
60     isa => HashRef[Constraint],
61     provides => {
62         exists => 'exists_constraint',
63         keys   => 'constraint_ids',
64         get    => 'get_constraint',
65     },
66     curries => {
67         set => {
68             add_constraint => sub {
69                 my ($self, $body, $constraint) = @_;
70                 $self->$body($constraint->name, $constraint);
71             }
72         }
73     },
74     default => sub { {} },
75 );
76
77 has 'sequences' => (
78     metaclass => 'Collection::Hash',
79     is => 'rw',
80     isa => HashRef[Sequence],
81     provides => {
82         exists => 'exists_sequence',
83         keys   => 'sequence_ids',
84         get    => 'get_sequence',
85     },
86     curries => {
87         set => {
88             add_sequence => sub {
89                 my ($self, $body, $sequence) = @_;
90                 $self->$body($sequence->name, $sequence);
91             }
92         }
93     },
94     default => sub { {} },
95 );
96
97 __PACKAGE__->meta->make_immutable;
98
99 1;