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 <kclark@cpan.org>
#
use strict;
use Class::Base;
+use SQL::Translator::Utils 'parse_list_arg';
use base 'Class::Base';
use vars qw($VERSION $TABLE_COUNT $VIEW_COUNT);
=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
}
# ----------------------------------------------------------------------
-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;