Made debugging work and it now exports its parse method.
[dbsrgits/SQL-Translator.git] / lib / SQL / Translator / Schema / View.pm
CommitLineData
3c5de62a 1package SQL::Translator::Schema::View;
2
3# ----------------------------------------------------------------------
aa3a9517 4# $Id: View.pm,v 1.4 2003-06-27 16:47:40 kycl4rk Exp $
3c5de62a 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
27SQL::Translator::Schema::View - SQL::Translator view object
28
29=head1 SYNOPSIS
30
31 use SQL::Translator::Schema::View;
32 my $view = SQL::Translator::Schema::View->new(
33 name => 'foo',
34 sql => 'select * from foo',
35 );
36
37=head1 DESCRIPTION
38
39C<SQL::Translator::Schema::View> is the view object.
40
41=head1 METHODS
42
43=cut
44
45use strict;
46use Class::Base;
25a02fa7 47use SQL::Translator::Utils 'parse_list_arg';
3c5de62a 48
49use base 'Class::Base';
50use vars qw($VERSION $TABLE_COUNT $VIEW_COUNT);
51
aa3a9517 52$VERSION = sprintf "%d.%02d", q$Revision: 1.4 $ =~ /(\d+)\.(\d+)/;
3c5de62a 53
54# ----------------------------------------------------------------------
55sub init {
56
57=pod
58
59=head2 new
60
61Object constructor.
62
63 my $schema = SQL::Translator::Schema::View->new;
64
65=cut
66
67 my ( $self, $config ) = @_;
25a02fa7 68
69 for my $arg ( qw[ name sql fields ] ) {
70 next unless $config->{ $arg };
71 $self->$arg( $config->{ $arg } ) or return;
72 }
73
3c5de62a 74 return $self;
75}
76
77# ----------------------------------------------------------------------
25a02fa7 78sub fields {
79
80=pod
81
82=head2 fields
83
84Gets and set the fields the constraint is on. Accepts a string, list or
85arrayref; returns an array or array reference. Will unique the field
86names and keep them in order by the first occurrence of a field name.
87
88 $view->fields('id');
89 $view->fields('id', 'name');
90 $view->fields( 'id, name' );
91 $view->fields( [ 'id', 'name' ] );
92 $view->fields( qw[ id name ] );
93
94 my @fields = $view->fields;
95
96=cut
97
98 my $self = shift;
99 my $fields = parse_list_arg( @_ );
100
101 if ( @$fields ) {
102 my ( %unique, @unique );
103 for my $f ( @$fields ) {
104 next if $unique{ $f };
105 $unique{ $f } = 1;
106 push @unique, $f;
107 }
108
109 $self->{'fields'} = \@unique;
110 }
111
112 return wantarray ? @{ $self->{'fields'} || [] } : $self->{'fields'};
113}
114
115# ----------------------------------------------------------------------
116sub is_valid {
117
118=pod
119
120=head2 is_valid
121
122Determine whether the view is valid or not.
123
124 my $ok = $view->is_valid;
125
126=cut
127
128 my $self = shift;
129
130 return $self->error('No name') unless $self->name;
131 return $self->error('No sql') unless $self->sql;
132
133 return 1;
134}
135
136# ----------------------------------------------------------------------
3c5de62a 137sub name {
138
139=pod
140
141=head2 name
142
143Get or set the view's name.
144
145 my $name = $view->name('foo');
146
147=cut
148
149 my $self = shift;
150 $self->{'name'} = shift if @_;
151 return $self->{'name'} || '';
152}
153
154# ----------------------------------------------------------------------
25a02fa7 155sub order {
3c5de62a 156
157=pod
158
25a02fa7 159=head2 order
3c5de62a 160
25a02fa7 161Get or set the view's order.
3c5de62a 162
25a02fa7 163 my $order = $view->order(3);
3c5de62a 164
165=cut
166
25a02fa7 167 my ( $self, $arg ) = @_;
168
169 if ( defined $arg && $arg =~ /^\d+$/ ) {
170 $self->{'order'} = $arg;
171 }
172
173 return $self->{'order'} || 0;
3c5de62a 174}
175
176# ----------------------------------------------------------------------
25a02fa7 177sub sql {
3c5de62a 178
179=pod
180
25a02fa7 181=head2 sql
3c5de62a 182
25a02fa7 183Get or set the view's SQL.
3c5de62a 184
25a02fa7 185 my $sql = $view->sql('select * from foo');
3c5de62a 186
187=cut
188
25a02fa7 189 my $self = shift;
190 $self->{'sql'} = shift if @_;
191 return $self->{'sql'} || '';
192}
193
194# ----------------------------------------------------------------------
195sub DESTROY {
3c5de62a 196 my $self = shift;
25a02fa7 197 undef $self->{'table'}; # destroy cyclical reference
3c5de62a 198}
199
2001;
201
202# ----------------------------------------------------------------------
203
204=pod
205
206=head1 AUTHOR
207
208Ken Y. Clark E<lt>kclark@cpan.orgE<gt>
209
210=cut