add compat methods
[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(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         },
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             values => 'get_indices',
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 { my %hash = (); tie %hash, 'Tie::IxHash'; return \%hash },
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             values => 'get_constraints',
65             get    => 'get_constraint',
66         },
67         curries => {
68             set => {
69                 add_constraint => sub {
70                     my ($self, $body, $constraint) = @_;
71                     $self->$body($constraint->name, $constraint);
72                 }
73             }
74         },
75         default => sub { my %hash = (); tie %hash, 'Tie::IxHash'; return \%hash },
76     );
77     
78     has 'sequences' => (
79         metaclass => 'Collection::Hash',
80         is => 'rw',
81         isa => HashRef[Sequence],
82         provides => {
83             exists => 'exists_sequence',
84             keys   => 'sequence_ids',
85             values => 'get_sequences',
86             get    => 'get_sequence',
87         },
88         curries => {
89             set => {
90                 add_sequence => sub {
91                     my ($self, $body, $sequence) = @_;
92                     $self->$body($sequence->name, $sequence);
93                 }
94             }
95         },
96         default => sub { my %hash = (); tie %hash, 'Tie::IxHash'; return \%hash },
97     );
98
99     has 'comments' => (
100         is => 'rw',
101         isa => Maybe[Str],
102     );
103     
104     has 'temporary' => (
105         is => 'rw',
106         isa => Bool,
107         default => 0
108     );
109
110     method get_fields { return $self->get_columns }
111     method primary_key(Str $column) {
112         $self->get_column($column)->is_primary_key(1);
113     }
114 }