Commit | Line | Data |
3c5de62a |
1 | package SQL::Translator::Schema::Index; |
2 | |
3 | # ---------------------------------------------------------------------- |
43b9dc7a |
4 | # $Id: Index.pm,v 1.2 2003-05-05 04:32:39 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 | |
42 | Primary keys will be considered table constraints, not indices. |
43 | |
44 | =head1 METHODS |
45 | |
46 | =cut |
47 | |
48 | use strict; |
49 | use Class::Base; |
50 | |
51 | use base 'Class::Base'; |
52 | use vars qw($VERSION $TABLE_COUNT $VIEW_COUNT); |
53 | |
54 | $VERSION = 1.00; |
55 | |
56 | use constant VALID_TYPE => { |
57 | unique => 1, |
58 | normal => 1, |
59 | full_text => 1, # MySQL only (?) |
60 | }; |
61 | |
62 | # ---------------------------------------------------------------------- |
63 | sub init { |
64 | |
65 | =pod |
66 | |
67 | =head2 new |
68 | |
69 | Object constructor. |
70 | |
71 | my $schema = SQL::Translator::Schema::Index->new; |
72 | |
73 | =cut |
74 | |
75 | my ( $self, $config ) = @_; |
76 | for my $arg ( qw[ name type fields ] ) { |
77 | next unless $config->{ $arg }; |
78 | $self->$arg( $config->{ $arg } ) or return; |
79 | } |
80 | return $self; |
81 | } |
82 | |
83 | # ---------------------------------------------------------------------- |
84 | sub fields { |
85 | |
86 | =pod |
87 | |
88 | =head2 fields |
89 | |
90 | Gets and set the fields the index is on. Accepts a list or arrayref, |
91 | return both, too. |
92 | |
93 | my @fields = $index->fields( 'id' ); |
94 | |
95 | =cut |
96 | |
97 | my $self = shift; |
98 | my $fields = ref $_[0] eq 'ARRAY' ? shift : [ @_ ]; |
99 | |
100 | if ( @$fields ) { |
101 | $self->{'fields'} = $fields; |
102 | } |
103 | |
104 | return wantarray ? @{ $self->{'fields'} || [] } : $self->{'fields'}; |
105 | } |
106 | |
107 | # ---------------------------------------------------------------------- |
108 | sub name { |
109 | |
110 | =pod |
111 | |
112 | =head2 name |
113 | |
114 | Get or set the index's name. |
115 | |
116 | my $name = $index->name('foo'); |
117 | |
118 | =cut |
119 | |
120 | my $self = shift; |
121 | $self->{'name'} = shift if @_; |
122 | return $self->{'name'} || ''; |
123 | } |
124 | |
125 | # ---------------------------------------------------------------------- |
43b9dc7a |
126 | sub table { |
127 | |
128 | =pod |
129 | |
130 | =head2 table |
131 | |
132 | Get or set the index's table object. |
133 | |
134 | my $table = $index->table; |
135 | |
136 | =cut |
137 | |
138 | my $self = shift; |
139 | if ( my $arg = shift ) { |
140 | return $self->error('Not a table object') unless |
141 | UNIVERSAL::isa( $arg, 'SQL::Translator::Schema::Table' ); |
142 | $self->{'table'} = $arg; |
143 | } |
144 | |
145 | return $self->{'table'}; |
146 | } |
147 | |
148 | # ---------------------------------------------------------------------- |
3c5de62a |
149 | sub type { |
150 | |
151 | =pod |
152 | |
153 | =head2 type |
154 | |
155 | Get or set the index's type. |
156 | |
157 | my $type = $index->type('unique'); |
158 | |
159 | =cut |
160 | |
161 | my $self = shift; |
162 | |
163 | if ( my $type = shift ) { |
164 | return $self->error("Invalid index type: $type") |
165 | unless VALID_TYPE->{ $type }; |
166 | $self->{'type'} = $type; |
167 | } |
168 | |
169 | return $self->{'type'} || ''; |
170 | } |
171 | |
172 | |
173 | # ---------------------------------------------------------------------- |
174 | sub is_valid { |
175 | |
176 | =pod |
177 | |
178 | =head2 is_valid |
179 | |
180 | Determine whether the index is valid or not. |
181 | |
182 | my $ok = $index->is_valid; |
183 | |
184 | =cut |
185 | |
186 | my $self = shift; |
187 | return ( $self->name && $self->{'type'} && @{ $self->fields } ) ? 1 : 0; |
188 | } |
189 | |
190 | 1; |
191 | |
192 | # ---------------------------------------------------------------------- |
193 | |
194 | =pod |
195 | |
196 | =head1 AUTHOR |
197 | |
198 | Ken Y. Clark E<lt>kclark@cpan.orgE<gt> |
199 | |
200 | =cut |