Commit | Line | Data |
4f4fd192 |
1 | use MooseX::Declare; |
ebf2721d |
2 | class 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', |
db2e467f |
23 | |
24 | ## compat |
25 | get_fields => 'values', |
26 | fields => 'keys', |
4f4fd192 |
27 | }, |
28 | default => sub { my %hash = (); tie %hash, 'Tie::IxHash'; return \%hash }, |
29 | ); |
30 | |
31 | has 'indexes' => ( |
720dcdc3 |
32 | traits => ['Hash'], |
4f4fd192 |
33 | is => 'rw', |
34 | isa => HashRef[Index], |
720dcdc3 |
35 | handles => { |
36 | exists_index => 'exists', |
37 | index_ids => 'keys', |
38 | get_indices => 'values', |
39 | get_index => 'get', |
40 | add_index => 'set', |
4f4fd192 |
41 | }, |
2e7ac234 |
42 | default => sub { my %hash = (); tie %hash, 'Tie::IxHash'; return \%hash }, |
4f4fd192 |
43 | ); |
44 | |
45 | has 'constraints' => ( |
720dcdc3 |
46 | traits => ['Hash'], |
4f4fd192 |
47 | is => 'rw', |
48 | isa => HashRef[Constraint], |
720dcdc3 |
49 | handles => { |
50 | exists_constraint => 'exists', |
51 | constraint_ids => 'keys', |
52 | get_constraints => 'values', |
53 | get_constraint => 'get', |
54 | add_constraint => 'set', |
4f4fd192 |
55 | }, |
49063029 |
56 | default => sub { my %hash = (); tie %hash, 'Tie::IxHash'; return \%hash }, |
4f4fd192 |
57 | ); |
58 | |
59 | has 'sequences' => ( |
720dcdc3 |
60 | traits => ['Hash'], |
4f4fd192 |
61 | is => 'rw', |
62 | isa => HashRef[Sequence], |
720dcdc3 |
63 | handles => { |
64 | exists_sequence => 'exists', |
65 | sequence_ids => 'keys', |
66 | get_sequences => 'values', |
67 | get_sequence => 'get', |
68 | add_sequence => 'set', |
4f4fd192 |
69 | }, |
2e7ac234 |
70 | default => sub { my %hash = (); tie %hash, 'Tie::IxHash'; return \%hash }, |
4f4fd192 |
71 | ); |
f78a484a |
72 | |
2850baeb |
73 | has 'schema' => ( |
4f4fd192 |
74 | is => 'rw', |
2850baeb |
75 | isa => Schema, |
76 | weak_ref => 1, |
77 | required => 1, |
4f4fd192 |
78 | ); |
49063029 |
79 | |
2850baeb |
80 | has 'temporary' => ( |
b750d2f1 |
81 | is => 'rw', |
2850baeb |
82 | isa => Bool, |
83 | default => 0 |
b750d2f1 |
84 | ); |
85 | |
51700db2 |
86 | around add_column(Column $column) { $self->$orig($column->name, $column) } |
87 | around add_index(Index $index) { $self->$orig($index->name, $index) } |
88 | around add_constraint(Constraint $constraint) { $self->$orig($constraint->name, $constraint) } |
89 | around add_sequence(Sequence $sequence) { $self->$orig($sequence->name, $sequence) } |
90 | |
5f184270 |
91 | multi method primary_key(Any $) { grep /^PRIMARY KEY$/, $_->type for $self->get_constraints } |
92 | multi method primary_key(Str $column) { $self->get_column($column)->is_primary_key(1) } |
b750d2f1 |
93 | |
94 | method order { } |
4f4fd192 |
95 | } |