1 package SQL::Translator::Schema::Procedure;
3 # ----------------------------------------------------------------------
4 # Copyright (C) 2002-2009 SQLFairy Authors
6 # This program is free software; you can redistribute it and/or
7 # modify it under the terms of the GNU General Public License as
8 # published by the Free Software Foundation; version 2.
10 # This program is distributed in the hope that it will be useful, but
11 # WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 # General Public License for more details.
15 # You should have received a copy of the GNU General Public License
16 # along with this program; if not, write to the Free Software
17 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
19 # -------------------------------------------------------------------
25 SQL::Translator::Schema::Procedure - SQL::Translator procedure object
29 use SQL::Translator::Schema::Procedure;
30 my $procedure = SQL::Translator::Schema::Procedure->new(
32 sql => 'CREATE PROC foo AS SELECT * FROM bar',
33 parameters => 'foo,bar',
35 comments => 'blah blah blah',
41 C<SQL::Translator::Schema::Procedure> is a class for dealing with
42 stored procedures (and possibly other pieces of nameable SQL code?).
49 use SQL::Translator::Utils 'parse_list_arg';
51 use base 'SQL::Translator::Schema::Object';
53 use vars qw($VERSION);
57 # ----------------------------------------------------------------------
59 __PACKAGE__->_attributes( qw/
60 name sql parameters comments owner sql schema order
69 my $schema = SQL::Translator::Schema::Procedure->new;
73 # ----------------------------------------------------------------------
80 Gets and set the parameters of the stored procedure.
82 $procedure->parameters('id');
83 $procedure->parameters('id', 'name');
84 $procedure->parameters( 'id, name' );
85 $procedure->parameters( [ 'id', 'name' ] );
86 $procedure->parameters( qw[ id name ] );
88 my @parameters = $procedure->parameters;
93 my $parameters = parse_list_arg( @_ );
96 my ( %unique, @unique );
97 for my $p ( @$parameters ) {
98 next if $unique{ $p };
103 $self->{'parameters'} = \@unique;
106 return wantarray ? @{ $self->{'parameters'} || [] } : ($self->{'parameters'} || '');
109 # ----------------------------------------------------------------------
116 Get or set the procedure's name.
118 $procedure->name('foo');
119 my $name = $procedure->name;
124 $self->{'name'} = shift if @_;
125 return $self->{'name'} || '';
128 # ----------------------------------------------------------------------
135 Get or set the procedure's SQL.
137 $procedure->sql('select * from foo');
138 my $sql = $procedure->sql;
143 $self->{'sql'} = shift if @_;
144 return $self->{'sql'} || '';
147 # ----------------------------------------------------------------------
154 Get or set the order of the procedure.
156 $procedure->order( 3 );
157 my $order = $procedure->order;
162 $self->{'order'} = shift if @_;
163 return $self->{'order'};
166 # ----------------------------------------------------------------------
173 Get or set the owner of the procedure.
175 $procedure->owner('nomar');
176 my $sql = $procedure->owner;
181 $self->{'owner'} = shift if @_;
182 return $self->{'owner'} || '';
185 # ----------------------------------------------------------------------
192 Get or set the comments on a procedure.
194 $procedure->comments('foo');
195 $procedure->comments('bar');
196 print join( ', ', $procedure->comments ); # prints "foo, bar"
203 $arg = $arg->[0] if ref $arg;
204 push @{ $self->{'comments'} }, $arg if $arg;
207 if ( @{ $self->{'comments'} || [] } ) {
209 ? @{ $self->{'comments'} || [] }
210 : join( "\n", @{ $self->{'comments'} || [] } );
213 return wantarray ? () : '';
217 # ----------------------------------------------------------------------
224 Get or set the procedures's schema object.
226 $procedure->schema( $schema );
227 my $schema = $procedure->schema;
232 if ( my $arg = shift ) {
233 return $self->error('Not a schema object') unless
234 UNIVERSAL::isa( $arg, 'SQL::Translator::Schema' );
235 $self->{'schema'} = $arg;
238 return $self->{'schema'};
241 # ----------------------------------------------------------------------
248 Determines if this procedure is the same as another
250 my $isIdentical = $procedure1->equals( $procedure2 );
256 my $case_insensitive = shift;
257 my $ignore_sql = shift;
259 return 0 unless $self->SUPER::equals($other);
260 return 0 unless $case_insensitive ? uc($self->name) eq uc($other->name) : $self->name eq $other->name;
262 unless ($ignore_sql) {
263 my $selfSql = $self->sql;
264 my $otherSql = $other->sql;
266 $selfSql =~ s/--.*$//mg;
267 $otherSql =~ s/--.*$//mg;
268 # Collapse whitespace to space to avoid whitespace comparison issues
269 $selfSql =~ s/\s+/ /sg;
270 $otherSql =~ s/\s+/ /sg;
271 return 0 unless $selfSql eq $otherSql;
274 return 0 unless $self->_compare_objects(scalar $self->parameters, scalar $other->parameters);
275 # return 0 unless $self->comments eq $other->comments;
276 # return 0 unless $case_insensitive ? uc($self->owner) eq uc($other->owner) : $self->owner eq $other->owner;
277 return 0 unless $self->_compare_objects(scalar $self->extra, scalar $other->extra);
281 # ----------------------------------------------------------------------
284 undef $self->{'schema'}; # destroy cyclical reference
289 # ----------------------------------------------------------------------
295 Ken Y. Clark E<lt>kclark@cshl.orgE<gt>,
296 Paul Harrington E<lt>Paul-Harrington@deshaw.comE<gt>.