1 package SQL::Translator::Schema::Procedure;
7 SQL::Translator::Schema::Procedure - SQL::Translator procedure object
11 use SQL::Translator::Schema::Procedure;
12 my $procedure = SQL::Translator::Schema::Procedure->new(
14 sql => 'CREATE PROC foo AS SELECT * FROM bar',
15 parameters => 'foo,bar',
17 comments => 'blah blah blah',
23 C<SQL::Translator::Schema::Procedure> is a class for dealing with
24 stored procedures (and possibly other pieces of nameable SQL code?).
31 use SQL::Translator::Utils 'parse_list_arg';
33 use base 'SQL::Translator::Schema::Object';
35 use vars qw($VERSION);
39 # ----------------------------------------------------------------------
41 __PACKAGE__->_attributes( qw/
42 name sql parameters comments owner sql schema order
51 my $schema = SQL::Translator::Schema::Procedure->new;
55 # ----------------------------------------------------------------------
62 Gets and set the parameters of the stored procedure.
64 $procedure->parameters('id');
65 $procedure->parameters('id', 'name');
66 $procedure->parameters( 'id, name' );
67 $procedure->parameters( [ 'id', 'name' ] );
68 $procedure->parameters( qw[ id name ] );
70 my @parameters = $procedure->parameters;
75 my $parameters = parse_list_arg( @_ );
78 my ( %unique, @unique );
79 for my $p ( @$parameters ) {
80 next if $unique{ $p };
85 $self->{'parameters'} = \@unique;
88 return wantarray ? @{ $self->{'parameters'} || [] } : ($self->{'parameters'} || '');
91 # ----------------------------------------------------------------------
98 Get or set the procedure's name.
100 $procedure->name('foo');
101 my $name = $procedure->name;
106 $self->{'name'} = shift if @_;
107 return $self->{'name'} || '';
110 # ----------------------------------------------------------------------
117 Get or set the procedure's SQL.
119 $procedure->sql('select * from foo');
120 my $sql = $procedure->sql;
125 $self->{'sql'} = shift if @_;
126 return $self->{'sql'} || '';
129 # ----------------------------------------------------------------------
136 Get or set the order of the procedure.
138 $procedure->order( 3 );
139 my $order = $procedure->order;
144 $self->{'order'} = shift if @_;
145 return $self->{'order'};
148 # ----------------------------------------------------------------------
155 Get or set the owner of the procedure.
157 $procedure->owner('nomar');
158 my $sql = $procedure->owner;
163 $self->{'owner'} = shift if @_;
164 return $self->{'owner'} || '';
167 # ----------------------------------------------------------------------
174 Get or set the comments on a procedure.
176 $procedure->comments('foo');
177 $procedure->comments('bar');
178 print join( ', ', $procedure->comments ); # prints "foo, bar"
185 $arg = $arg->[0] if ref $arg;
186 push @{ $self->{'comments'} }, $arg if $arg;
189 if ( @{ $self->{'comments'} || [] } ) {
191 ? @{ $self->{'comments'} || [] }
192 : join( "\n", @{ $self->{'comments'} || [] } );
195 return wantarray ? () : '';
199 # ----------------------------------------------------------------------
206 Get or set the procedures's schema object.
208 $procedure->schema( $schema );
209 my $schema = $procedure->schema;
214 if ( my $arg = shift ) {
215 return $self->error('Not a schema object') unless
216 UNIVERSAL::isa( $arg, 'SQL::Translator::Schema' );
217 $self->{'schema'} = $arg;
220 return $self->{'schema'};
223 # ----------------------------------------------------------------------
230 Determines if this procedure is the same as another
232 my $isIdentical = $procedure1->equals( $procedure2 );
238 my $case_insensitive = shift;
239 my $ignore_sql = shift;
241 return 0 unless $self->SUPER::equals($other);
242 return 0 unless $case_insensitive ? uc($self->name) eq uc($other->name) : $self->name eq $other->name;
244 unless ($ignore_sql) {
245 my $selfSql = $self->sql;
246 my $otherSql = $other->sql;
248 $selfSql =~ s/--.*$//mg;
249 $otherSql =~ s/--.*$//mg;
250 # Collapse whitespace to space to avoid whitespace comparison issues
251 $selfSql =~ s/\s+/ /sg;
252 $otherSql =~ s/\s+/ /sg;
253 return 0 unless $selfSql eq $otherSql;
256 return 0 unless $self->_compare_objects(scalar $self->parameters, scalar $other->parameters);
257 # return 0 unless $self->comments eq $other->comments;
258 # return 0 unless $case_insensitive ? uc($self->owner) eq uc($other->owner) : $self->owner eq $other->owner;
259 return 0 unless $self->_compare_objects(scalar $self->extra, scalar $other->extra);
263 # ----------------------------------------------------------------------
266 undef $self->{'schema'}; # destroy cyclical reference
271 # ----------------------------------------------------------------------
277 Ken Youens-Clark E<lt>kclark@cshl.orgE<gt>,
278 Paul Harrington E<lt>Paul-Harrington@deshaw.comE<gt>.