1 package SQL::Translator::Schema::Procedure;
3 # ----------------------------------------------------------------------
4 # $Id: Procedure.pm,v 1.2 2004-02-09 22:15:15 kycl4rk Exp $
5 # ----------------------------------------------------------------------
6 # Copyright (C) 2002-4 SQLFairy Authors
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.
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.
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
21 # -------------------------------------------------------------------
27 SQL::Translator::Schema::Procedure - SQL::Translator procedure object
31 use SQL::Translator::Schema::Procedure;
32 my $procedure = SQL::Translator::Schema::Procedure->new(
34 sql => 'CREATE PROC foo AS SELECT * FROM bar',
35 parameters => 'foo,bar',
37 comments => 'blah blah blah',
43 C<SQL::Translator::Schema::Procedure> is a class for dealing with
44 stored procedures (and possibly other pieces of nameable SQL code?).
52 use SQL::Translator::Utils 'parse_list_arg';
54 use base 'Class::Base';
55 use vars qw($VERSION);
57 $VERSION = sprintf "%d.%02d", q$Revision: 1.2 $ =~ /(\d+)\.(\d+)/;
59 # ----------------------------------------------------------------------
68 my $schema = SQL::Translator::Schema::Procedure->new;
72 my ( $self, $config ) = @_;
74 for my $arg ( qw[ name sql parameters comments owner sql schema ] ) {
75 next unless $config->{ $arg };
76 $self->$arg( $config->{ $arg } ) or return;
82 # ----------------------------------------------------------------------
89 Gets and set the parameters of the stored procedure.
91 $procedure->parameters('id');
92 $procedure->parameters('id', 'name');
93 $procedure->parameters( 'id, name' );
94 $procedure->parameters( [ 'id', 'name' ] );
95 $procedure->parameters( qw[ id name ] );
97 my @parameters = $procedure->parameters;
102 my $parameters = parse_list_arg( @_ );
104 if ( @$parameters ) {
105 my ( %unique, @unique );
106 for my $p ( @$parameters ) {
107 next if $unique{ $p };
112 $self->{'parameters'} = \@unique;
115 return wantarray ? @{ $self->{'parameters'} || [] } : $self->{'parameters'};
118 # ----------------------------------------------------------------------
125 Get or set the procedure's name.
127 $procedure->name('foo');
128 my $name = $procedure->name;
133 $self->{'name'} = shift if @_;
134 return $self->{'name'} || '';
137 # ----------------------------------------------------------------------
144 Get or set the procedure's SQL.
146 $procedure->sql('select * from foo');
147 my $sql = $procedure->sql;
152 $self->{'sql'} = shift if @_;
153 return $self->{'sql'} || '';
156 # ----------------------------------------------------------------------
163 Get or set the order of the procedure.
165 $procedure->order( 3 );
166 my $order = $procedure->order;
171 $self->{'order'} = shift if @_;
172 return $self->{'order'};
175 # ----------------------------------------------------------------------
182 Get or set the owner of the procedure.
184 $procedure->owner('nomar');
185 my $sql = $procedure->owner;
190 $self->{'owner'} = shift if @_;
191 return $self->{'owner'} || '';
194 # ----------------------------------------------------------------------
201 Get or set the comments on a procedure.
203 $procedure->comments('foo');
204 $procedure->comments('bar');
205 print join( ', ', $procedure->comments ); # prints "foo, bar"
212 $arg = $arg->[0] if ref $arg;
213 push @{ $self->{'comments'} }, $arg if $arg;
216 if ( @{ $self->{'comments'} || [] } ) {
218 ? @{ $self->{'comments'} || [] }
219 : join( "\n", @{ $self->{'comments'} || [] } );
222 return wantarray ? () : '';
226 # ----------------------------------------------------------------------
233 Get or set the procedures's schema object.
235 $procedure->schema( $schema );
236 my $schema = $procedure->schema;
241 if ( my $arg = shift ) {
242 return $self->error('Not a schema object') unless
243 UNIVERSAL::isa( $arg, 'SQL::Translator::Schema' );
244 $self->{'schema'} = $arg;
247 return $self->{'schema'};
250 # ----------------------------------------------------------------------
253 undef $self->{'schema'}; # destroy cyclical reference
258 # ----------------------------------------------------------------------
264 Ken Y. Clark E<lt>kclark@cshl.orgE<gt>,
265 Paul Harrington E<lt>Paul-Harrington@deshaw.comE<gt>.