Adding new objects for handing schema data. Not being used while I work
[dbsrgits/SQL-Translator.git] / lib / SQL / Translator / Schema / Index.pm
1 package SQL::Translator::Schema::Index;
2
3 # ----------------------------------------------------------------------
4 # $Id: Index.pm,v 1.1 2003-05-01 04:25:00 kycl4rk Exp $
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 # ----------------------------------------------------------------------
126 sub type {
127
128 =pod
129
130 =head2 type
131
132 Get or set the index's type.
133
134   my $type = $index->type('unique');
135
136 =cut
137
138     my $self = shift;
139
140     if ( my $type = shift ) {
141         return $self->error("Invalid index type: $type") 
142             unless VALID_TYPE->{ $type };
143         $self->{'type'} = $type;
144     }
145
146     return $self->{'type'} || '';
147 }
148
149
150 # ----------------------------------------------------------------------
151 sub is_valid {
152
153 =pod
154
155 =head2 is_valid
156
157 Determine whether the index is valid or not.
158
159   my $ok = $index->is_valid;
160
161 =cut
162
163     my $self = shift;
164     return ( $self->name && $self->{'type'} && @{ $self->fields } ) ? 1 : 0;
165 }
166
167 1;
168
169 # ----------------------------------------------------------------------
170
171 =pod
172
173 =head1 AUTHOR
174
175 Ken Y. Clark E<lt>kclark@cpan.orgE<gt>
176
177 =cut