From: Ken Youens-Clark Date: Fri, 9 May 2003 17:12:15 +0000 (+0000) Subject: Use "parse_list_args," added "fields" method, changed validation, break X-Git-Tag: v0.02~121 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=25a02fa730c2f24d9d1ed36f3ded087e5bfaa47e;p=dbsrgits%2FSQL-Translator.git Use "parse_list_args," added "fields" method, changed validation, break ref to table in DESTROY. --- diff --git a/lib/SQL/Translator/Schema/View.pm b/lib/SQL/Translator/Schema/View.pm index 0f9a1bd..c41f5f0 100644 --- a/lib/SQL/Translator/Schema/View.pm +++ b/lib/SQL/Translator/Schema/View.pm @@ -1,7 +1,7 @@ package SQL::Translator::Schema::View; # ---------------------------------------------------------------------- -# $Id: View.pm,v 1.1 2003-05-01 04:25:00 kycl4rk Exp $ +# $Id: View.pm,v 1.2 2003-05-09 17:12:15 kycl4rk Exp $ # ---------------------------------------------------------------------- # Copyright (C) 2003 Ken Y. Clark # @@ -44,6 +44,7 @@ C is the view object. use strict; use Class::Base; +use SQL::Translator::Utils 'parse_list_arg'; use base 'Class::Base'; use vars qw($VERSION $TABLE_COUNT $VIEW_COUNT); @@ -64,11 +65,75 @@ Object constructor. =cut my ( $self, $config ) = @_; - $self->params( $config, qw[ name sql ] ); + + for my $arg ( qw[ name sql fields ] ) { + next unless $config->{ $arg }; + $self->$arg( $config->{ $arg } ) or return; + } + return $self; } # ---------------------------------------------------------------------- +sub fields { + +=pod + +=head2 fields + +Gets and set the fields the constraint is on. Accepts a string, list or +arrayref; returns an array or array reference. Will unique the field +names and keep them in order by the first occurrence of a field name. + + $view->fields('id'); + $view->fields('id', 'name'); + $view->fields( 'id, name' ); + $view->fields( [ 'id', 'name' ] ); + $view->fields( qw[ id name ] ); + + my @fields = $view->fields; + +=cut + + my $self = shift; + my $fields = parse_list_arg( @_ ); + + if ( @$fields ) { + my ( %unique, @unique ); + for my $f ( @$fields ) { + next if $unique{ $f }; + $unique{ $f } = 1; + push @unique, $f; + } + + $self->{'fields'} = \@unique; + } + + return wantarray ? @{ $self->{'fields'} || [] } : $self->{'fields'}; +} + +# ---------------------------------------------------------------------- +sub is_valid { + +=pod + +=head2 is_valid + +Determine whether the view is valid or not. + + my $ok = $view->is_valid; + +=cut + + my $self = shift; + + return $self->error('No name') unless $self->name; + return $self->error('No sql') unless $self->sql; + + return 1; +} + +# ---------------------------------------------------------------------- sub name { =pod @@ -87,38 +152,49 @@ Get or set the view's name. } # ---------------------------------------------------------------------- -sub sql { +sub order { =pod -=head2 sql +=head2 order -Get or set the view's SQL. +Get or set the view's order. - my $sql = $view->sql('select * from foo'); + my $order = $view->order(3); =cut - my $self = shift; - $self->{'sql'} = shift if @_; - return $self->{'sql'} || ''; + my ( $self, $arg ) = @_; + + if ( defined $arg && $arg =~ /^\d+$/ ) { + $self->{'order'} = $arg; + } + + return $self->{'order'} || 0; } # ---------------------------------------------------------------------- -sub is_valid { +sub sql { =pod -=head2 is_valid +=head2 sql -Determine whether the view is valid or not. +Get or set the view's SQL. - my $ok = $view->is_valid; + my $sql = $view->sql('select * from foo'); =cut + my $self = shift; + $self->{'sql'} = shift if @_; + return $self->{'sql'} || ''; +} + +# ---------------------------------------------------------------------- +sub DESTROY { my $self = shift; - return 1 if $self->name && $self->sql; + undef $self->{'table'}; # destroy cyclical reference } 1;