From: Ken Youens-Clark Date: Sat, 3 May 2003 04:07:09 +0000 (+0000) Subject: More changes to getting the schema to a working state. X-Git-Tag: v0.02~160 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=0f3cc5c0b8515724a77c0cdc7b816f41ca5f0aa0;p=dbsrgits%2FSQL-Translator.git More changes to getting the schema to a working state. --- diff --git a/lib/SQL/Translator/Schema/Constants.pm b/lib/SQL/Translator/Schema/Constants.pm new file mode 100644 index 0000000..15799df --- /dev/null +++ b/lib/SQL/Translator/Schema/Constants.pm @@ -0,0 +1,69 @@ +package SQL::Translator::Schema::Constants; + +# ---------------------------------------------------------------------- +# $Id: Constants.pm,v 1.1 2003-05-03 04:07:09 kycl4rk Exp $ +# ---------------------------------------------------------------------- +# Copyright (C) 2003 Ken Y. Clark +# +# This program is free software; you can redistribute it and/or +# modify it under the terms of the GNU General Public License as +# published by the Free Software Foundation; version 2. +# +# This program is distributed in the hope that it will be useful, but +# WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +# General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program; if not, write to the Free Software +# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA +# 02111-1307 USA +# ------------------------------------------------------------------- + +=head1 NAME + +SQL::Translator::CMap::Constants - constants module + +=head1 SYNOPSIS + + use SQL::Translator::CMap::Constants; + + $table->add_constraint( + name => 'foo', + type => PRIMARY_KEY, + ); + +=head1 DESCRIPTION + +This module exports a several constants to like "primary key," etc. + +=cut + +use strict; +use base qw( Exporter ); +use vars qw( @EXPORT $VERSION ); +require Exporter; +$VERSION = (qw$Revision: 1.1 $)[-1]; + +@EXPORT = qw[ + PRIMARY_KEY +]; + +use constant PRIMARY_KEY => 'primary_key'; + +1; + +=pod + +=head1 AUTHOR + +Ken Y. Clark Ekclark@cpan.orgE + +=head1 COPYRIGHT + +Copyright (c) 2003 + +This library is free software; you can redistribute it and/or modify +it under the same terms as Perl itself. + +=cut diff --git a/lib/SQL/Translator/Schema/Table.pm b/lib/SQL/Translator/Schema/Table.pm index 69ad4f3..889e6d7 100644 --- a/lib/SQL/Translator/Schema/Table.pm +++ b/lib/SQL/Translator/Schema/Table.pm @@ -1,7 +1,7 @@ package SQL::Translator::Schema::Table; # ---------------------------------------------------------------------- -# $Id: Table.pm,v 1.1 2003-05-01 04:25:00 kycl4rk Exp $ +# $Id: Table.pm,v 1.2 2003-05-03 04:07:09 kycl4rk Exp $ # ---------------------------------------------------------------------- # Copyright (C) 2003 Ken Y. Clark # @@ -29,25 +29,7 @@ SQL::Translator::Schema::Table - SQL::Translator table object =head1 SYNOPSIS use SQL::Translator::Schema::Table; - my $foo_table = SQL::Translator::Schema::Table->new('foo'); - - $foo_table->add_field( - name => 'foo_id', - data_type => 'integer', - size => 11, - is_primary_key => 1, - ); - - $foo_table->add_field( - name => 'foo_name', - data_type => 'char', - size => 10, - ); - - $foo_table->add_index( - name => '', - fields => [ 'foo_name' ], - ); + my $table = SQL::Translator::Schema::Table->new( name => 'foo' ); =head1 DESCSIPTION @@ -59,12 +41,13 @@ C is the table object. use strict; use Class::Base; +use SQL::Translator::Schema::Constants; use SQL::Translator::Schema::Constraint; use SQL::Translator::Schema::Field; use SQL::Translator::Schema::Index; use base 'Class::Base'; -use vars qw($VERSION); +use vars qw( $VERSION $FIELD_ORDER ); $VERSION = 1.00; @@ -111,12 +94,13 @@ sub add_constraint { =head2 add_constraint -Add a constraint to the table. +Add a constraint to the table. Returns the newly created +C object. - $table->add_constraint( + my $constraint = $table->add_constraint( name => 'pk', + type => PRIMARY_KEY, fields => [ 'foo_id' ], - type => 'primary_key', ); =cut @@ -135,9 +119,10 @@ sub add_index { =head2 add_index -Add an index to the table. +Add an index to the table. Returns the newly created +C object. - $table->add_index( + my $index = $table->add_index( name => 'name', fields => [ 'name' ], type => 'normal', @@ -159,40 +144,103 @@ sub add_field { =head2 add_field -Add an field to the table. Returns the SQL::Translator::Schema::Field -object. +Add an field to the table. Returns the newly created +C object. - my $field = $table->add_field( - name => 'foo_id', - data_type => 'integer', - size => 11, - is_primary_key => 1, + my $field = $table->add_field( + name => 'foo_id', + data_type => 'integer', + size => 11, ); =cut my $self = shift; - my $field = SQL::Translator::Schema::Field->new( @_ ) or return; + my %args = @_; + return $self->error('No name') unless $args{'name'}; + my $field = SQL::Translator::Schema::Field->new( \%args ) or return; SQL::Translator::Schema::Field->error; $self->{'fields'}{ $field->name } = $field; + $self->{'fields'}{ $field->name }{'order'} = ++$FIELD_ORDER; return $field; } # ---------------------------------------------------------------------- -sub fields { +sub get_constraints { + +=pod + +=head2 get_constraints + +Returns all the constraint objects as an array or array reference. + + my @constraints = $table->get_constraints; + +=cut + + my $self = shift; + + if ( ref $self->{'constraints'} ) { + return wantarray + ? @{ $self->{'constraints'} } : $self->{'constraints'}; + } + else { + $self->error('No constraints'); + return wantarray ? () : undef; + } +} + +# ---------------------------------------------------------------------- +sub get_indices { =pod -=head2 fields +=head2 get_indices -Returns all the fields. +Returns all the index objects as an array or array reference. - my @fields = $table->fields; + my @indices = $table->get_indices; =cut my $self = shift; - return wantarray ? %{ $self->{'fields'} || {} } : $self->{'fields'}; + + if ( ref $self->{'indices'} ) { + return wantarray + ? @{ $self->{'indices'} } + : $self->{'indices'}; + } + else { + $self->error('No indices'); + return wantarray ? () : undef; + } +} + +# ---------------------------------------------------------------------- +sub get_fields { + +=pod + +=head2 get_fields + +Returns all the field objects as an array or array reference. + + my @fields = $table->get_fields; + +=cut + + my $self = shift; + my @fields = + sort { $a->{'order'} <=> $b->{'order'} } + values %{ $self->{'fields'} || {} }; + + if ( @fields ) { + return wantarray ? @fields : \@fields; + } + else { + $self->error('No fields'); + return wantarray ? () : undef; + } } # ---------------------------------------------------------------------- @@ -209,7 +257,16 @@ Determine whether the view is valid or not. =cut my $self = shift; - return ( $self->name && $self->fields ) ? 1 : 0; + return $self->error('No name') unless $self->name; + return $self->error('No fields') unless $self->get_fields; + + for my $object ( + $self->get_fields, $self->get_indices, $self->get_constraints + ) { + return $object->error unless $object->is_valid; + } + + return 1; } 1;