1 package SQL::Translator::Schema::Constraint;
3 # ----------------------------------------------------------------------
4 # $Id: Constraint.pm,v 1.1 2003-05-01 04:24:59 kycl4rk Exp $
5 # ----------------------------------------------------------------------
6 # Copyright (C) 2003 Ken Y. Clark <kclark@cpan.org>
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.
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.
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
21 # -------------------------------------------------------------------
27 SQL::Translator::Schema::Constraint - SQL::Translator constraint object
31 use SQL::Translator::Schema::Constraint;
32 my $constraint = SQL::Translator::Schema::Constraint->new(
35 type => 'primary_key',
40 C<SQL::Translator::Schema::Constraint> is the constraint object.
49 use base 'Class::Base';
50 use vars qw($VERSION $TABLE_COUNT $VIEW_COUNT);
54 use constant VALID_TYPE => {
61 # ----------------------------------------------------------------------
70 my $schema = SQL::Translator::Schema::Constraint->new(
71 type => 'foreign_key', # type of table constraint
72 name => 'fk_phone_id', # the name of the constraint
73 fields => 'phone_id', # the field in the referring table
74 reference_fields => 'phone_id', # the referenced table
75 reference_table => 'phone', # the referenced fields
76 match_type => 'full', # how to match
77 on_delete_do => 'cascade', # what to do on deletes
78 on_update_do => '', # what to do on updates
83 my ( $self, $config ) = @_;
84 # reference_fields reference_table
85 # match_type on_delete_do on_update_do
86 my @fields = qw[ name type fields ];
88 for my $arg ( @fields ) {
89 next unless $config->{ $arg };
90 $self->$arg( $config->{ $arg } ) or return;
96 # ----------------------------------------------------------------------
103 Gets and set the fields the constraint is on. Accepts a list or arrayref,
106 my @fields = $constraint->fields( 'id' );
111 my $fields = ref $_[0] eq 'ARRAY' ? shift : [ @_ ];
114 $self->{'fields'} = $fields;
117 return wantarray ? @{ $self->{'fields'} || [] } : $self->{'fields'};
120 # ----------------------------------------------------------------------
127 Get or set the constraint's name.
129 my $name = $constraint->name('foo');
134 $self->{'name'} = shift if @_;
135 return $self->{'name'} || '';
138 # ----------------------------------------------------------------------
145 Get or set the constraint's type.
147 my $type = $constraint->type('primary_key');
153 if ( my $type = shift ) {
154 return $self->error("Invalid constraint type: $type")
155 unless VALID_TYPE->{ $type };
156 $self->{'type'} = $type;
159 return $self->{'type'} || '';
163 # ----------------------------------------------------------------------
170 Determine whether the constraint is valid or not.
172 my $ok = $constraint->is_valid;
177 return ( $self->name && $self->{'type'} && @{ $self->fields } ) ? 1 : 0;
182 # ----------------------------------------------------------------------
188 Ken Y. Clark E<lt>kclark@cpan.orgE<gt>