Commit | Line | Data |
4f4fd192 |
1 | use MooseX::Declare; |
2 | class 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 | |
2850baeb |
72 | has 'schema' => ( |
4f4fd192 |
73 | is => 'rw', |
2850baeb |
74 | isa => Schema, |
75 | weak_ref => 1, |
76 | required => 1, |
4f4fd192 |
77 | ); |
49063029 |
78 | |
2850baeb |
79 | has 'temporary' => ( |
b750d2f1 |
80 | is => 'rw', |
2850baeb |
81 | isa => Bool, |
82 | default => 0 |
b750d2f1 |
83 | ); |
84 | |
51700db2 |
85 | around add_column(Column $column) { $self->$orig($column->name, $column) } |
86 | around add_index(Index $index) { $self->$orig($index->name, $index) } |
87 | around add_constraint(Constraint $constraint) { $self->$orig($constraint->name, $constraint) } |
88 | around add_sequence(Sequence $sequence) { $self->$orig($sequence->name, $sequence) } |
89 | |
5f184270 |
90 | method get_fields { $self->get_columns } |
91 | method fields { $self->column_ids } |
92 | |
93 | multi method primary_key(Any $) { grep /^PRIMARY KEY$/, $_->type for $self->get_constraints } |
94 | multi method primary_key(Str $column) { $self->get_column($column)->is_primary_key(1) } |
b750d2f1 |
95 | |
96 | method order { } |
4f4fd192 |
97 | } |