Fixed VERSION to be CPAN-friendly.
[dbsrgits/SQL-Translator.git] / lib / SQL / Translator / Schema / View.pm
1 package SQL::Translator::Schema::View;
2
3 # ----------------------------------------------------------------------
4 # $Id: View.pm,v 1.4 2003-06-27 16:47:40 kycl4rk Exp $
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
27 SQL::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
39 C<SQL::Translator::Schema::View> is the view object.
40
41 =head1 METHODS
42
43 =cut
44
45 use strict;
46 use Class::Base;
47 use SQL::Translator::Utils 'parse_list_arg';
48
49 use base 'Class::Base';
50 use vars qw($VERSION $TABLE_COUNT $VIEW_COUNT);
51
52 $VERSION = sprintf "%d.%02d", q$Revision: 1.4 $ =~ /(\d+)\.(\d+)/;
53
54 # ----------------------------------------------------------------------
55 sub init {
56
57 =pod
58
59 =head2 new
60
61 Object constructor.
62
63   my $schema = SQL::Translator::Schema::View->new;
64
65 =cut
66
67     my ( $self, $config ) = @_;
68
69     for my $arg ( qw[ name sql fields ] ) {
70         next unless $config->{ $arg };
71         $self->$arg( $config->{ $arg } ) or return;
72     }
73
74     return $self;
75 }
76
77 # ----------------------------------------------------------------------
78 sub fields {
79
80 =pod
81
82 =head2 fields
83
84 Gets and set the fields the constraint is on.  Accepts a string, list or
85 arrayref; returns an array or array reference.  Will unique the field
86 names 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 # ----------------------------------------------------------------------
116 sub is_valid {
117
118 =pod
119
120 =head2 is_valid
121
122 Determine 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 # ----------------------------------------------------------------------
137 sub name {
138
139 =pod
140
141 =head2 name
142
143 Get 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 # ----------------------------------------------------------------------
155 sub order {
156
157 =pod
158
159 =head2 order
160
161 Get or set the view's order.
162
163   my $order = $view->order(3);
164
165 =cut
166
167     my ( $self, $arg ) = @_;
168
169     if ( defined $arg && $arg =~ /^\d+$/ ) {
170         $self->{'order'} = $arg;
171     }
172
173     return $self->{'order'} || 0;
174 }
175
176 # ----------------------------------------------------------------------
177 sub sql {
178
179 =pod
180
181 =head2 sql
182
183 Get or set the view's SQL.
184
185   my $sql = $view->sql('select * from foo');
186
187 =cut
188
189     my $self       = shift;
190     $self->{'sql'} = shift if @_;
191     return $self->{'sql'} || '';
192 }
193
194 # ----------------------------------------------------------------------
195 sub DESTROY {
196     my $self = shift;
197     undef $self->{'table'}; # destroy cyclical reference
198 }
199
200 1;
201
202 # ----------------------------------------------------------------------
203
204 =pod
205
206 =head1 AUTHOR
207
208 Ken Y. Clark E<lt>kclark@cpan.orgE<gt>
209
210 =cut