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 __PACKAGE__->_attributes( qw/
58 name sql parameters comments owner sql schema order
67 my $schema = SQL::Translator::Schema::Procedure->new;
77 Gets and set the parameters of the stored procedure.
79 $procedure->parameters('id');
80 $procedure->parameters('id', 'name');
81 $procedure->parameters( 'id, name' );
82 $procedure->parameters( [ 'id', 'name' ] );
83 $procedure->parameters( qw[ id name ] );
85 my @parameters = $procedure->parameters;
90 my $parameters = parse_list_arg( @_ );
93 my ( %unique, @unique );
94 for my $p ( @$parameters ) {
95 next if $unique{ $p };
100 $self->{'parameters'} = \@unique;
103 return wantarray ? @{ $self->{'parameters'} || [] } : ($self->{'parameters'} || '');
112 Get or set the procedure's name.
114 $procedure->name('foo');
115 my $name = $procedure->name;
120 $self->{'name'} = shift if @_;
121 return $self->{'name'} || '';
130 Get or set the procedure's SQL.
132 $procedure->sql('select * from foo');
133 my $sql = $procedure->sql;
138 $self->{'sql'} = shift if @_;
139 return $self->{'sql'} || '';
148 Get or set the order of the procedure.
150 $procedure->order( 3 );
151 my $order = $procedure->order;
156 $self->{'order'} = shift if @_;
157 return $self->{'order'};
166 Get or set the owner of the procedure.
168 $procedure->owner('nomar');
169 my $sql = $procedure->owner;
174 $self->{'owner'} = shift if @_;
175 return $self->{'owner'} || '';
184 Get or set the comments on a procedure.
186 $procedure->comments('foo');
187 $procedure->comments('bar');
188 print join( ', ', $procedure->comments ); # prints "foo, bar"
195 $arg = $arg->[0] if ref $arg;
196 push @{ $self->{'comments'} }, $arg if $arg;
199 if ( @{ $self->{'comments'} || [] } ) {
201 ? @{ $self->{'comments'} || [] }
202 : join( "\n", @{ $self->{'comments'} || [] } );
205 return wantarray ? () : '';
215 Get or set the procedures's schema object.
217 $procedure->schema( $schema );
218 my $schema = $procedure->schema;
223 if ( my $arg = shift ) {
224 return $self->error('Not a schema object') unless
225 UNIVERSAL::isa( $arg, 'SQL::Translator::Schema' );
226 $self->{'schema'} = $arg;
229 return $self->{'schema'};
238 Determines if this procedure is the same as another
240 my $isIdentical = $procedure1->equals( $procedure2 );
246 my $case_insensitive = shift;
247 my $ignore_sql = shift;
249 return 0 unless $self->SUPER::equals($other);
250 return 0 unless $case_insensitive ? uc($self->name) eq uc($other->name) : $self->name eq $other->name;
252 unless ($ignore_sql) {
253 my $selfSql = $self->sql;
254 my $otherSql = $other->sql;
256 $selfSql =~ s/--.*$//mg;
257 $otherSql =~ s/--.*$//mg;
258 # Collapse whitespace to space to avoid whitespace comparison issues
259 $selfSql =~ s/\s+/ /sg;
260 $otherSql =~ s/\s+/ /sg;
261 return 0 unless $selfSql eq $otherSql;
264 return 0 unless $self->_compare_objects(scalar $self->parameters, scalar $other->parameters);
265 # return 0 unless $self->comments eq $other->comments;
266 # return 0 unless $case_insensitive ? uc($self->owner) eq uc($other->owner) : $self->owner eq $other->owner;
267 return 0 unless $self->_compare_objects(scalar $self->extra, scalar $other->extra);
273 undef $self->{'schema'}; # destroy cyclical reference
282 Ken Youens-Clark E<lt>kclark@cshl.orgE<gt>,
283 Paul Harrington E<lt>Paul-Harrington@deshaw.comE<gt>.