Commit | Line | Data |
4f4fd192 |
1 | use MooseX::Declare; |
ebf2721d |
2 | class SQL::Translator::Object::Index extends SQL::Translator::Object { |
1c607f61 |
3 | use MooseX::Types::Moose qw(HashRef Str); |
52137c8d |
4 | use MooseX::MultiMethods; |
f49a2a49 |
5 | use SQL::Translator::Types qw(Column Table); |
f49a2a49 |
6 | |
7 | has 'table' => ( |
8 | is => 'rw', |
9 | isa => Table, |
f49a2a49 |
10 | weak_ref => 1, |
11 | ); |
4f4fd192 |
12 | |
13 | has 'name' => ( |
14 | is => 'rw', |
15 | isa => Str, |
16 | required => 1 |
17 | ); |
2850baeb |
18 | |
4f4fd192 |
19 | has 'columns' => ( |
28bd628e |
20 | traits => ['Hash'], |
4f4fd192 |
21 | is => 'rw', |
22 | isa => HashRef[Column], |
28bd628e |
23 | handles => { |
24 | exists_column => 'exists', |
25 | column_ids => 'keys', |
26 | get_columns => 'values', |
27 | get_column => 'get', |
28 | add_column => 'set', |
db2e467f |
29 | |
30 | ## compat |
31 | get_fields => 'values', |
4f4fd192 |
32 | }, |
33 | default => sub { my %hash = (); tie %hash, 'Tie::IxHash'; return \%hash }, |
34 | ); |
2850baeb |
35 | |
4f4fd192 |
36 | has 'type' => ( |
37 | is => 'rw', |
38 | isa => Str, |
7e6d96f8 |
39 | required => 1, |
40 | default => 'NORMAL', |
4f4fd192 |
41 | ); |
65061e1b |
42 | |
51700db2 |
43 | around add_column(Column $column) { $self->$orig($column->name, $column) } |
7e6d96f8 |
44 | |
52137c8d |
45 | multi method fields(Str $columns) { |
46 | for (split /,/, $columns) { die "Column $_ does not exist!" unless $self->table->exists_column($_) } |
47 | $self->add_column($self->table->get_column($_)) for split /,/, $columns; |
48 | $self->column_ids; |
49 | } |
50 | |
51 | multi method fields(@columns) { |
52 | for (@columns) { die "Column $_ does not exist!" unless $self->table->exists_column($_) } |
53 | $self->add_column($self->table->get_column($_)) for @columns; |
54 | $self->column_ids; |
55 | } |
56 | |
57 | multi method fields(Any $) { $self->column_ids } |
58 | |
7e6d96f8 |
59 | method is_valid { $self->table && scalar $self->get_columns ? 1 : undef } |
4f4fd192 |
60 | } |