1 package SQL::Translator::Schema::Procedure;
3 # ----------------------------------------------------------------------
5 # ----------------------------------------------------------------------
6 # Copyright (C) 2002-2009 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?).
51 use SQL::Translator::Utils 'parse_list_arg';
53 use base 'SQL::Translator::Schema::Object';
55 # ----------------------------------------------------------------------
57 __PACKAGE__->_attributes( qw/
58 name sql parameters comments owner sql schema order
67 my $schema = SQL::Translator::Schema::Procedure->new;
71 # ----------------------------------------------------------------------
78 Gets and set the parameters of the stored procedure.
80 $procedure->parameters('id');
81 $procedure->parameters('id', 'name');
82 $procedure->parameters( 'id, name' );
83 $procedure->parameters( [ 'id', 'name' ] );
84 $procedure->parameters( qw[ id name ] );
86 my @parameters = $procedure->parameters;
91 my $parameters = parse_list_arg( @_ );
94 my ( %unique, @unique );
95 for my $p ( @$parameters ) {
96 next if $unique{ $p };
101 $self->{'parameters'} = \@unique;
104 return wantarray ? @{ $self->{'parameters'} || [] } : ($self->{'parameters'} || '');
107 # ----------------------------------------------------------------------
114 Get or set the procedure's name.
116 $procedure->name('foo');
117 my $name = $procedure->name;
122 $self->{'name'} = shift if @_;
123 return $self->{'name'} || '';
126 # ----------------------------------------------------------------------
133 Get or set the procedure's SQL.
135 $procedure->sql('select * from foo');
136 my $sql = $procedure->sql;
141 $self->{'sql'} = shift if @_;
142 return $self->{'sql'} || '';
145 # ----------------------------------------------------------------------
152 Get or set the order of the procedure.
154 $procedure->order( 3 );
155 my $order = $procedure->order;
160 $self->{'order'} = shift if @_;
161 return $self->{'order'};
164 # ----------------------------------------------------------------------
171 Get or set the owner of the procedure.
173 $procedure->owner('nomar');
174 my $sql = $procedure->owner;
179 $self->{'owner'} = shift if @_;
180 return $self->{'owner'} || '';
183 # ----------------------------------------------------------------------
190 Get or set the comments on a procedure.
192 $procedure->comments('foo');
193 $procedure->comments('bar');
194 print join( ', ', $procedure->comments ); # prints "foo, bar"
201 $arg = $arg->[0] if ref $arg;
202 push @{ $self->{'comments'} }, $arg if $arg;
205 if ( @{ $self->{'comments'} || [] } ) {
207 ? @{ $self->{'comments'} || [] }
208 : join( "\n", @{ $self->{'comments'} || [] } );
211 return wantarray ? () : '';
215 # ----------------------------------------------------------------------
222 Get or set the procedures's schema object.
224 $procedure->schema( $schema );
225 my $schema = $procedure->schema;
230 if ( my $arg = shift ) {
231 return $self->error('Not a schema object') unless
232 UNIVERSAL::isa( $arg, 'SQL::Translator::Schema' );
233 $self->{'schema'} = $arg;
236 return $self->{'schema'};
239 # ----------------------------------------------------------------------
246 Determines if this procedure is the same as another
248 my $isIdentical = $procedure1->equals( $procedure2 );
254 my $case_insensitive = shift;
255 my $ignore_sql = shift;
257 return 0 unless $self->SUPER::equals($other);
258 return 0 unless $case_insensitive ? uc($self->name) eq uc($other->name) : $self->name eq $other->name;
260 unless ($ignore_sql) {
261 my $selfSql = $self->sql;
262 my $otherSql = $other->sql;
264 $selfSql =~ s/--.*$//mg;
265 $otherSql =~ s/--.*$//mg;
266 # Collapse whitespace to space to avoid whitespace comparison issues
267 $selfSql =~ s/\s+/ /sg;
268 $otherSql =~ s/\s+/ /sg;
269 return 0 unless $selfSql eq $otherSql;
272 return 0 unless $self->_compare_objects(scalar $self->parameters, scalar $other->parameters);
273 # return 0 unless $self->comments eq $other->comments;
274 # return 0 unless $case_insensitive ? uc($self->owner) eq uc($other->owner) : $self->owner eq $other->owner;
275 return 0 unless $self->_compare_objects(scalar $self->extra, scalar $other->extra);
279 # ----------------------------------------------------------------------
282 undef $self->{'schema'}; # destroy cyclical reference
287 # ----------------------------------------------------------------------
293 Ken Y. Clark E<lt>kclark@cshl.orgE<gt>,
294 Paul Harrington E<lt>Paul-Harrington@deshaw.comE<gt>.