1 package SQL::Translator::Schema::Procedure;
3 # ----------------------------------------------------------------------
4 # $Id: Procedure.pm,v 1.1 2003-10-08 17:31:24 kycl4rk Exp $
5 # ----------------------------------------------------------------------
6 # Copyright (C) 2003 Ken Y. Clark <kclark@cpan.org>,
7 # Paul Harrington <Paul-Harrington@deshaw.com>.
9 # This program is free software; you can redistribute it and/or
10 # modify it under the terms of the GNU General Public License as
11 # published by the Free Software Foundation; version 2.
13 # This program is distributed in the hope that it will be useful, but
14 # WITHOUT ANY WARRANTY; without even the implied warranty of
15 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 # General Public License for more details.
18 # You should have received a copy of the GNU General Public License
19 # along with this program; if not, write to the Free Software
20 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
22 # -------------------------------------------------------------------
28 SQL::Translator::Schema::Procedure - SQL::Translator procedure object
32 use SQL::Translator::Schema::Procedure;
33 my $procedure = SQL::Translator::Schema::Procedure->new(
35 sql => 'CREATE PROC foo AS SELECT * FROM bar',
36 parameters => 'foo,bar',
38 comments => 'blah blah blah',
44 C<SQL::Translator::Schema::Procedure> is a class for dealing with
45 stored procedures (and possibly other pieces of nameable SQL code?).
53 use SQL::Translator::Utils 'parse_list_arg';
55 use base 'Class::Base';
56 use vars qw($VERSION);
58 $VERSION = sprintf "%d.%02d", q$Revision: 1.1 $ =~ /(\d+)\.(\d+)/;
60 # ----------------------------------------------------------------------
69 my $schema = SQL::Translator::Schema::Procedure->new;
73 my ( $self, $config ) = @_;
75 for my $arg ( qw[ name sql parameters comments owner sql schema ] ) {
76 next unless $config->{ $arg };
77 $self->$arg( $config->{ $arg } ) or return;
83 # ----------------------------------------------------------------------
90 Gets and set the parameters of the stored procedure.
92 $procedure->parameters('id');
93 $procedure->parameters('id', 'name');
94 $procedure->parameters( 'id, name' );
95 $procedure->parameters( [ 'id', 'name' ] );
96 $procedure->parameters( qw[ id name ] );
98 my @parameters = $procedure->parameters;
103 my $parameters = parse_list_arg( @_ );
105 if ( @$parameters ) {
106 my ( %unique, @unique );
107 for my $p ( @$parameters ) {
108 next if $unique{ $p };
113 $self->{'parameters'} = \@unique;
116 return wantarray ? @{ $self->{'parameters'} || [] } : $self->{'parameters'};
119 # ----------------------------------------------------------------------
126 Get or set the procedure's name.
128 $procedure->name('foo');
129 my $name = $procedure->name;
134 $self->{'name'} = shift if @_;
135 return $self->{'name'} || '';
138 # ----------------------------------------------------------------------
145 Get or set the procedure's SQL.
147 $procedure->sql('select * from foo');
148 my $sql = $procedure->sql;
153 $self->{'sql'} = shift if @_;
154 return $self->{'sql'} || '';
157 # ----------------------------------------------------------------------
164 Get or set the order of the procedure.
166 $procedure->order( 3 );
167 my $order = $procedure->order;
172 $self->{'order'} = shift if @_;
173 return $self->{'order'};
176 # ----------------------------------------------------------------------
183 Get or set the owner of the procedure.
185 $procedure->owner('nomar');
186 my $sql = $procedure->owner;
191 $self->{'owner'} = shift if @_;
192 return $self->{'owner'} || '';
195 # ----------------------------------------------------------------------
202 Get or set the comments on a procedure.
204 $procedure->comments('foo');
205 $procedure->comments('bar');
206 print join( ', ', $procedure->comments ); # prints "foo, bar"
213 $arg = $arg->[0] if ref $arg;
214 push @{ $self->{'comments'} }, $arg if $arg;
217 if ( @{ $self->{'comments'} || [] } ) {
219 ? @{ $self->{'comments'} || [] }
220 : join( "\n", @{ $self->{'comments'} || [] } );
223 return wantarray ? () : '';
227 # ----------------------------------------------------------------------
234 Get or set the procedures's schema object.
236 $procedure->schema( $schema );
237 my $schema = $procedure->schema;
242 if ( my $arg = shift ) {
243 return $self->error('Not a schema object') unless
244 UNIVERSAL::isa( $arg, 'SQL::Translator::Schema' );
245 $self->{'schema'} = $arg;
248 return $self->{'schema'};
251 # ----------------------------------------------------------------------
254 undef $self->{'schema'}; # destroy cyclical reference
263 Ken Y. Clark E<lt>kclark@cshl.orgE<gt>,
264 Paul Harrington E<lt>Paul-Harrington@deshaw.comE<gt>.