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); |
4f4fd192 |
4 | use MooseX::AttributeHelpers; |
5f184270 |
5 | use MooseX::MultiMethods; |
4f4fd192 |
6 | use SQL::Translator::Types qw(Column Constraint Index Schema Sequence); |
4f4fd192 |
7 | |
8 | has 'name' => ( |
9 | is => 'rw', |
10 | isa => Str, |
11 | required => 1 |
12 | ); |
13 | |
14 | has 'columns' => ( |
15 | metaclass => 'Collection::Hash', |
16 | is => 'rw', |
17 | isa => HashRef[Column], |
18 | provides => { |
19 | exists => 'exists_column', |
20 | keys => 'column_ids', |
49063029 |
21 | values => 'get_columns', |
4f4fd192 |
22 | get => 'get_column', |
51700db2 |
23 | set => 'add_column', |
4f4fd192 |
24 | }, |
25 | default => sub { my %hash = (); tie %hash, 'Tie::IxHash'; return \%hash }, |
26 | ); |
27 | |
28 | has 'indexes' => ( |
29 | metaclass => 'Collection::Hash', |
30 | is => 'rw', |
31 | isa => HashRef[Index], |
32 | provides => { |
33 | exists => 'exists_index', |
34 | keys => 'index_ids', |
49063029 |
35 | values => 'get_indices', |
4f4fd192 |
36 | get => 'get_index', |
51700db2 |
37 | set => 'add_index', |
4f4fd192 |
38 | }, |
2e7ac234 |
39 | default => sub { my %hash = (); tie %hash, 'Tie::IxHash'; return \%hash }, |
4f4fd192 |
40 | ); |
41 | |
42 | has 'constraints' => ( |
43 | metaclass => 'Collection::Hash', |
44 | is => 'rw', |
45 | isa => HashRef[Constraint], |
46 | provides => { |
47 | exists => 'exists_constraint', |
48 | keys => 'constraint_ids', |
49063029 |
49 | values => 'get_constraints', |
4f4fd192 |
50 | get => 'get_constraint', |
51700db2 |
51 | set => 'add_constraint', |
4f4fd192 |
52 | }, |
49063029 |
53 | default => sub { my %hash = (); tie %hash, 'Tie::IxHash'; return \%hash }, |
4f4fd192 |
54 | ); |
55 | |
56 | has 'sequences' => ( |
57 | metaclass => 'Collection::Hash', |
58 | is => 'rw', |
59 | isa => HashRef[Sequence], |
60 | provides => { |
61 | exists => 'exists_sequence', |
62 | keys => 'sequence_ids', |
49063029 |
63 | values => 'get_sequences', |
4f4fd192 |
64 | get => 'get_sequence', |
51700db2 |
65 | set => 'add_sequence', |
4f4fd192 |
66 | }, |
2e7ac234 |
67 | default => sub { my %hash = (); tie %hash, 'Tie::IxHash'; return \%hash }, |
4f4fd192 |
68 | ); |
f78a484a |
69 | |
2850baeb |
70 | has 'schema' => ( |
4f4fd192 |
71 | is => 'rw', |
2850baeb |
72 | isa => Schema, |
73 | weak_ref => 1, |
74 | required => 1, |
4f4fd192 |
75 | ); |
49063029 |
76 | |
2850baeb |
77 | has 'temporary' => ( |
b750d2f1 |
78 | is => 'rw', |
2850baeb |
79 | isa => Bool, |
80 | default => 0 |
b750d2f1 |
81 | ); |
82 | |
51700db2 |
83 | around add_column(Column $column) { $self->$orig($column->name, $column) } |
84 | around add_index(Index $index) { $self->$orig($index->name, $index) } |
85 | around add_constraint(Constraint $constraint) { $self->$orig($constraint->name, $constraint) } |
86 | around add_sequence(Sequence $sequence) { $self->$orig($sequence->name, $sequence) } |
87 | |
5f184270 |
88 | method get_fields { $self->get_columns } |
89 | method fields { $self->column_ids } |
90 | |
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 | } |