use Tie::IxHash
[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 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         get    => 'get_column',
24     },
25     curries => {
26         set => {
27             add_column => sub {
28                 my ($self, $body, $column) = @_;
29                 $self->$body($column->name, $column);
30             }
31         }
32     },
33     default => sub { my %hash = (); tie %hash, 'Tie::IxHash'; return \%hash },
34 );
35
36 has 'indexes' => (
37     metaclass => 'Collection::Hash',
38     is => 'rw',
39     isa => HashRef[Index],
40     provides => {
41         exists => 'exists_index',
42         keys   => 'index_ids',
43         get    => 'get_index',
44     },
45     curries => {
46         set => {
47             add_index => sub {
48                 my ($self, $body, $index) = @_;
49                 $self->$body($index->name, $index);
50             }
51         }
52     },
53     default => sub { {} },
54 );
55
56 has 'constraints' => (
57     metaclass => 'Collection::Hash',
58     is => 'rw',
59     isa => HashRef[Constraint],
60     provides => {
61         exists => 'exists_constraint',
62         keys   => 'constraint_ids',
63         get    => 'get_constraint',
64     },
65     curries => {
66         set => {
67             add_constraint => sub {
68                 my ($self, $body, $constraint) = @_;
69                 $self->$body($constraint->name, $constraint);
70             }
71         }
72     },
73     default => sub { {} },
74 );
75
76 has 'sequences' => (
77     metaclass => 'Collection::Hash',
78     is => 'rw',
79     isa => HashRef[Sequence],
80     provides => {
81         exists => 'exists_sequence',
82         keys   => 'sequence_ids',
83         get    => 'get_sequence',
84     },
85     curries => {
86         set => {
87             add_sequence => sub {
88                 my ($self, $body, $sequence) = @_;
89                 $self->$body($sequence->name, $sequence);
90             }
91         }
92     },
93     default => sub { {} },
94 );
95
96 __PACKAGE__->meta->make_immutable;
97
98 1;