Commit | Line | Data |
3c5de62a |
1 | package SQL::Translator::Schema::Index; |
2 | |
3 | # ---------------------------------------------------------------------- |
aa3a9517 |
4 | # $Id: Index.pm,v 1.7 2003-06-27 16:47:40 kycl4rk Exp $ |
3c5de62a |
5 | # ---------------------------------------------------------------------- |
6 | # Copyright (C) 2003 Ken Y. Clark <kclark@cpan.org> |
7 | # |
8 | # This program is free software; you can redistribute it and/or |
9 | # modify it under the terms of the GNU General Public License as |
10 | # published by the Free Software Foundation; version 2. |
11 | # |
12 | # This program is distributed in the hope that it will be useful, but |
13 | # WITHOUT ANY WARRANTY; without even the implied warranty of |
14 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
15 | # General Public License for more details. |
16 | # |
17 | # You should have received a copy of the GNU General Public License |
18 | # along with this program; if not, write to the Free Software |
19 | # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA |
20 | # 02111-1307 USA |
21 | # ------------------------------------------------------------------- |
22 | |
23 | =pod |
24 | |
25 | =head1 NAME |
26 | |
27 | SQL::Translator::Schema::Index - SQL::Translator index object |
28 | |
29 | =head1 SYNOPSIS |
30 | |
31 | use SQL::Translator::Schema::Index; |
32 | my $index = SQL::Translator::Schema::Index->new( |
33 | name => 'foo', |
34 | fields => [ id ], |
35 | type => 'unique', |
36 | ); |
37 | |
38 | =head1 DESCRIPTION |
39 | |
40 | C<SQL::Translator::Schema::Index> is the index object. |
41 | |
b54199ae |
42 | Primary and unique keys are table constraints, not indices. |
3c5de62a |
43 | |
44 | =head1 METHODS |
45 | |
46 | =cut |
47 | |
48 | use strict; |
49 | use Class::Base; |
25868dc9 |
50 | use SQL::Translator::Schema::Constants; |
51 | use SQL::Translator::Utils 'parse_list_arg'; |
3c5de62a |
52 | |
53 | use base 'Class::Base'; |
54 | use vars qw($VERSION $TABLE_COUNT $VIEW_COUNT); |
55 | |
aa3a9517 |
56 | $VERSION = sprintf "%d.%02d", q$Revision: 1.7 $ =~ /(\d+)\.(\d+)/; |
3c5de62a |
57 | |
c8efc003 |
58 | my %VALID_INDEX_TYPE = ( |
25868dc9 |
59 | UNIQUE, 1, |
60 | NORMAL, 1, |
61 | FULL_TEXT, 1, # MySQL only (?) |
c8efc003 |
62 | ); |
3c5de62a |
63 | |
64 | # ---------------------------------------------------------------------- |
65 | sub init { |
66 | |
67 | =pod |
68 | |
69 | =head2 new |
70 | |
71 | Object constructor. |
72 | |
73 | my $schema = SQL::Translator::Schema::Index->new; |
74 | |
75 | =cut |
76 | |
77 | my ( $self, $config ) = @_; |
25868dc9 |
78 | |
79 | for my $arg ( qw[ name type fields table ] ) { |
3c5de62a |
80 | next unless $config->{ $arg }; |
b54199ae |
81 | defined $self->$arg( $config->{ $arg } ) or return; |
3c5de62a |
82 | } |
25868dc9 |
83 | |
3c5de62a |
84 | return $self; |
85 | } |
86 | |
87 | # ---------------------------------------------------------------------- |
88 | sub fields { |
89 | |
90 | =pod |
91 | |
92 | =head2 fields |
93 | |
b54199ae |
94 | Gets and set the fields the index is on. Accepts a string, list or |
25868dc9 |
95 | arrayref; returns an array or array reference. Will unique the field |
96 | names and keep them in order by the first occurrence of a field name. |
3c5de62a |
97 | |
b54199ae |
98 | $index->fields('id'); |
99 | $index->fields('id', 'name'); |
100 | $index->fields( 'id, name' ); |
101 | $index->fields( [ 'id', 'name' ] ); |
102 | $index->fields( qw[ id name ] ); |
25868dc9 |
103 | |
b54199ae |
104 | my @fields = $index->fields; |
3c5de62a |
105 | |
106 | =cut |
107 | |
108 | my $self = shift; |
25868dc9 |
109 | my $fields = parse_list_arg( @_ ); |
3c5de62a |
110 | |
111 | if ( @$fields ) { |
25868dc9 |
112 | my ( %unique, @unique ); |
113 | for my $f ( @$fields ) { |
114 | next if $unique{ $f }; |
115 | $unique{ $f } = 1; |
116 | push @unique, $f; |
117 | } |
118 | |
119 | $self->{'fields'} = \@unique; |
3c5de62a |
120 | } |
121 | |
122 | return wantarray ? @{ $self->{'fields'} || [] } : $self->{'fields'}; |
123 | } |
124 | |
125 | # ---------------------------------------------------------------------- |
b54199ae |
126 | sub is_valid { |
127 | |
128 | =pod |
129 | |
130 | =head2 is_valid |
131 | |
132 | Determine whether the index is valid or not. |
133 | |
134 | my $ok = $index->is_valid; |
135 | |
136 | =cut |
137 | |
138 | my $self = shift; |
139 | my $table = $self->table or return $self->error('No table'); |
140 | my @fields = $self->fields or return $self->error('No fields'); |
141 | |
142 | for my $field ( @fields ) { |
143 | return $self->error( |
144 | "Field '$field' does not exist in table '", $table->name, "'" |
145 | ) unless $table->get_field( $field ); |
146 | } |
147 | |
148 | return 1; |
149 | } |
150 | |
151 | # ---------------------------------------------------------------------- |
3c5de62a |
152 | sub name { |
153 | |
154 | =pod |
155 | |
156 | =head2 name |
157 | |
158 | Get or set the index's name. |
159 | |
160 | my $name = $index->name('foo'); |
161 | |
162 | =cut |
163 | |
164 | my $self = shift; |
165 | $self->{'name'} = shift if @_; |
166 | return $self->{'name'} || ''; |
167 | } |
168 | |
169 | # ---------------------------------------------------------------------- |
25868dc9 |
170 | sub options { |
171 | |
172 | =pod |
173 | |
174 | =head2 options |
175 | |
176 | Get or set the index's options (e.g., "using" or "where" for PG). Returns |
177 | an array or array reference. |
178 | |
179 | my @options = $index->options; |
180 | |
181 | =cut |
182 | |
183 | my $self = shift; |
184 | my $options = parse_list_arg( @_ ); |
185 | |
186 | push @{ $self->{'options'} }, @$options; |
187 | |
188 | if ( ref $self->{'options'} ) { |
189 | return wantarray ? @{ $self->{'options'} || [] } : $self->{'options'}; |
190 | } |
191 | else { |
192 | return wantarray ? () : []; |
193 | } |
194 | } |
195 | |
196 | # ---------------------------------------------------------------------- |
43b9dc7a |
197 | sub table { |
198 | |
199 | =pod |
200 | |
201 | =head2 table |
202 | |
203 | Get or set the index's table object. |
204 | |
205 | my $table = $index->table; |
206 | |
207 | =cut |
208 | |
209 | my $self = shift; |
210 | if ( my $arg = shift ) { |
211 | return $self->error('Not a table object') unless |
212 | UNIVERSAL::isa( $arg, 'SQL::Translator::Schema::Table' ); |
213 | $self->{'table'} = $arg; |
214 | } |
215 | |
216 | return $self->{'table'}; |
217 | } |
218 | |
219 | # ---------------------------------------------------------------------- |
3c5de62a |
220 | sub type { |
221 | |
222 | =pod |
223 | |
224 | =head2 type |
225 | |
226 | Get or set the index's type. |
227 | |
228 | my $type = $index->type('unique'); |
229 | |
230 | =cut |
231 | |
232 | my $self = shift; |
233 | |
234 | if ( my $type = shift ) { |
235 | return $self->error("Invalid index type: $type") |
c8efc003 |
236 | unless $VALID_INDEX_TYPE{ $type }; |
3c5de62a |
237 | $self->{'type'} = $type; |
238 | } |
239 | |
25868dc9 |
240 | return $self->{'type'} || NORMAL; |
3c5de62a |
241 | } |
242 | |
243 | |
244 | # ---------------------------------------------------------------------- |
25868dc9 |
245 | sub DESTROY { |
3c5de62a |
246 | my $self = shift; |
25868dc9 |
247 | undef $self->{'table'}; # destroy cyclical reference |
3c5de62a |
248 | } |
249 | |
250 | 1; |
251 | |
252 | # ---------------------------------------------------------------------- |
253 | |
254 | =pod |
255 | |
256 | =head1 AUTHOR |
257 | |
258 | Ken Y. Clark E<lt>kclark@cpan.orgE<gt> |
259 | |
260 | =cut |