migrate from MXAH to Native::Trait
[dbsrgits/SQL-Translator-2.0-ish.git] / lib / SQL / Translator / Object / Table.pm
1 use MooseX::Declare;
2 class SQL::Translator::Object::Table extends SQL::Translator::Object {
3     use MooseX::Types::Moose qw(Any Bool HashRef Str);
4     use MooseX::MultiMethods;
5     use SQL::Translator::Types qw(Column Constraint Index Schema Sequence);
6     
7     has 'name' => (
8         is => 'rw',
9         isa => Str,
10         required => 1
11     );
12     
13     has 'columns' => (
14         traits => ['Hash'],
15         is => 'rw',
16         isa => HashRef[Column],
17         handles => {
18             exists_column => 'exists',
19             column_ids    => 'keys',
20             get_columns   => 'values',
21             get_column    => 'get',
22             add_column    => 'set',
23         },
24         default => sub { my %hash = (); tie %hash, 'Tie::IxHash'; return \%hash },
25     );
26     
27     has 'indexes' => (
28         traits => ['Hash'],
29         is => 'rw',
30         isa => HashRef[Index],
31         handles => {
32             exists_index => 'exists',
33             index_ids    => 'keys',
34             get_indices  => 'values',
35             get_index    => 'get',
36             add_index    => 'set',
37         },
38         default => sub { my %hash = (); tie %hash, 'Tie::IxHash'; return \%hash },
39     );
40     
41     has 'constraints' => (
42         traits => ['Hash'],
43         is => 'rw',
44         isa => HashRef[Constraint],
45         handles => {
46             exists_constraint => 'exists',
47             constraint_ids    => 'keys',
48             get_constraints   => 'values',
49             get_constraint    => 'get',
50             add_constraint    => 'set',
51         },
52         default => sub { my %hash = (); tie %hash, 'Tie::IxHash'; return \%hash },
53     );
54     
55     has 'sequences' => (
56         traits => ['Hash'],
57         is => 'rw',
58         isa => HashRef[Sequence],
59         handles => {
60             exists_sequence => 'exists',
61             sequence_ids    => 'keys',
62             get_sequences   => 'values',
63             get_sequence    => 'get',
64             add_sequence    => 'set',
65         },
66         default => sub { my %hash = (); tie %hash, 'Tie::IxHash'; return \%hash },
67     );
68
69     has 'schema' => (
70         is => 'rw',
71         isa => Schema,
72         weak_ref => 1,
73         required => 1,
74     );
75
76     has 'temporary' => (
77         is => 'rw',
78         isa => Bool,
79         default => 0
80     );
81
82     around add_column(Column $column) { $self->$orig($column->name, $column) }
83     around add_index(Index $index) { $self->$orig($index->name, $index) }
84     around add_constraint(Constraint $constraint) { $self->$orig($constraint->name, $constraint) }
85     around add_sequence(Sequence $sequence) { $self->$orig($sequence->name, $sequence) }
86
87     method get_fields { $self->get_columns }
88     method fields { $self->column_ids }
89
90     multi method primary_key(Any $) { grep /^PRIMARY KEY$/, $_->type for $self->get_constraints }
91     multi method primary_key(Str $column) { $self->get_column($column)->is_primary_key(1) }
92
93     method order { }
94 }