migrate from MXAH to Native::Trait
[dbsrgits/SQL-Translator-2.0-ish.git] / lib / SQL / Translator / Object / Table.pm
CommitLineData
4f4fd192 1use MooseX::Declare;
ebf2721d 2class SQL::Translator::Object::Table extends SQL::Translator::Object {
1c607f61 3 use MooseX::Types::Moose qw(Any Bool HashRef Str);
5f184270 4 use MooseX::MultiMethods;
4f4fd192 5 use SQL::Translator::Types qw(Column Constraint Index Schema Sequence);
4f4fd192 6
7 has 'name' => (
8 is => 'rw',
9 isa => Str,
10 required => 1
11 );
12
13 has 'columns' => (
720dcdc3 14 traits => ['Hash'],
4f4fd192 15 is => 'rw',
16 isa => HashRef[Column],
720dcdc3 17 handles => {
18 exists_column => 'exists',
19 column_ids => 'keys',
20 get_columns => 'values',
21 get_column => 'get',
22 add_column => 'set',
4f4fd192 23 },
24 default => sub { my %hash = (); tie %hash, 'Tie::IxHash'; return \%hash },
25 );
26
27 has 'indexes' => (
720dcdc3 28 traits => ['Hash'],
4f4fd192 29 is => 'rw',
30 isa => HashRef[Index],
720dcdc3 31 handles => {
32 exists_index => 'exists',
33 index_ids => 'keys',
34 get_indices => 'values',
35 get_index => 'get',
36 add_index => 'set',
4f4fd192 37 },
2e7ac234 38 default => sub { my %hash = (); tie %hash, 'Tie::IxHash'; return \%hash },
4f4fd192 39 );
40
41 has 'constraints' => (
720dcdc3 42 traits => ['Hash'],
4f4fd192 43 is => 'rw',
44 isa => HashRef[Constraint],
720dcdc3 45 handles => {
46 exists_constraint => 'exists',
47 constraint_ids => 'keys',
48 get_constraints => 'values',
49 get_constraint => 'get',
50 add_constraint => 'set',
4f4fd192 51 },
49063029 52 default => sub { my %hash = (); tie %hash, 'Tie::IxHash'; return \%hash },
4f4fd192 53 );
54
55 has 'sequences' => (
720dcdc3 56 traits => ['Hash'],
4f4fd192 57 is => 'rw',
58 isa => HashRef[Sequence],
720dcdc3 59 handles => {
60 exists_sequence => 'exists',
61 sequence_ids => 'keys',
62 get_sequences => 'values',
63 get_sequence => 'get',
64 add_sequence => 'set',
4f4fd192 65 },
2e7ac234 66 default => sub { my %hash = (); tie %hash, 'Tie::IxHash'; return \%hash },
4f4fd192 67 );
f78a484a 68
2850baeb 69 has 'schema' => (
4f4fd192 70 is => 'rw',
2850baeb 71 isa => Schema,
72 weak_ref => 1,
73 required => 1,
4f4fd192 74 );
49063029 75
2850baeb 76 has 'temporary' => (
b750d2f1 77 is => 'rw',
2850baeb 78 isa => Bool,
79 default => 0
b750d2f1 80 );
81
51700db2 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
5f184270 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) }
b750d2f1 92
93 method order { }
4f4fd192 94}