some changes to match current functionality
[dbsrgits/SQL-Translator-2.0-ish.git] / lib / SQL / Translator / Object / Table.pm
CommitLineData
4f4fd192 1use MooseX::Declare;
2class SQL::Translator::Object::Table {
5f184270 3 use MooseX::Types::Moose qw(Any ArrayRef Bool HashRef Maybe Str);
4f4fd192 4 use MooseX::AttributeHelpers;
5f184270 5 use MooseX::MultiMethods;
4f4fd192 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',
49063029 23 values => 'get_columns',
4f4fd192 24 get => 'get_column',
51700db2 25 set => 'add_column',
4f4fd192 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',
49063029 37 values => 'get_indices',
4f4fd192 38 get => 'get_index',
51700db2 39 set => 'add_index',
4f4fd192 40 },
2e7ac234 41 default => sub { my %hash = (); tie %hash, 'Tie::IxHash'; return \%hash },
4f4fd192 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',
49063029 51 values => 'get_constraints',
4f4fd192 52 get => 'get_constraint',
51700db2 53 set => 'add_constraint',
4f4fd192 54 },
49063029 55 default => sub { my %hash = (); tie %hash, 'Tie::IxHash'; return \%hash },
4f4fd192 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',
49063029 65 values => 'get_sequences',
4f4fd192 66 get => 'get_sequence',
51700db2 67 set => 'add_sequence',
4f4fd192 68 },
2e7ac234 69 default => sub { my %hash = (); tie %hash, 'Tie::IxHash'; return \%hash },
4f4fd192 70 );
f78a484a 71
4f4fd192 72 has 'temporary' => (
73 is => 'rw',
74 isa => Bool,
75 default => 0
76 );
49063029 77
b750d2f1 78 has 'options' => (
79 is => 'rw',
80 isa => ArrayRef,
81 auto_deref => 1
82 );
83
5f184270 84 has 'extra' => (
85 is => 'rw',
86 isa => HashRef,
87 auto_deref => 1,
88 );
89
51700db2 90 around add_column(Column $column) { $self->$orig($column->name, $column) }
91 around add_index(Index $index) { $self->$orig($index->name, $index) }
92 around add_constraint(Constraint $constraint) { $self->$orig($constraint->name, $constraint) }
93 around add_sequence(Sequence $sequence) { $self->$orig($sequence->name, $sequence) }
94
5f184270 95 method get_fields { $self->get_columns }
96 method fields { $self->column_ids }
97
98 multi method primary_key(Any $) { grep /^PRIMARY KEY$/, $_->type for $self->get_constraints }
99 multi method primary_key(Str $column) { $self->get_column($column)->is_primary_key(1) }
b750d2f1 100
101 method order { }
4f4fd192 102}