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?).
32 use SQL::Translator::Utils 'parse_list_arg';
34 use base 'SQL::Translator::Schema::Object';
36 use vars qw($VERSION);
40 __PACKAGE__->_attributes( qw/
41 name sql parameters comments owner sql schema order
50 my $schema = SQL::Translator::Schema::Procedure->new;
60 Gets and set the parameters of the stored procedure.
62 $procedure->parameters('id');
63 $procedure->parameters('id', 'name');
64 $procedure->parameters( 'id, name' );
65 $procedure->parameters( [ 'id', 'name' ] );
66 $procedure->parameters( qw[ id name ] );
68 my @parameters = $procedure->parameters;
73 my $parameters = parse_list_arg( @_ );
76 my ( %unique, @unique );
77 for my $p ( @$parameters ) {
78 next if $unique{ $p };
83 $self->{'parameters'} = \@unique;
86 return wantarray ? @{ $self->{'parameters'} || [] } : ($self->{'parameters'} || '');
95 Get or set the procedure's name.
97 $procedure->name('foo');
98 my $name = $procedure->name;
103 $self->{'name'} = shift if @_;
104 return $self->{'name'} || '';
113 Get or set the procedure's SQL.
115 $procedure->sql('select * from foo');
116 my $sql = $procedure->sql;
121 $self->{'sql'} = shift if @_;
122 return $self->{'sql'} || '';
131 Get or set the order of the procedure.
133 $procedure->order( 3 );
134 my $order = $procedure->order;
139 $self->{'order'} = shift if @_;
140 return $self->{'order'};
149 Get or set the owner of the procedure.
151 $procedure->owner('nomar');
152 my $sql = $procedure->owner;
157 $self->{'owner'} = shift if @_;
158 return $self->{'owner'} || '';
167 Get or set the comments on a procedure.
169 $procedure->comments('foo');
170 $procedure->comments('bar');
171 print join( ', ', $procedure->comments ); # prints "foo, bar"
178 $arg = $arg->[0] if ref $arg;
179 push @{ $self->{'comments'} }, $arg if $arg;
182 if ( @{ $self->{'comments'} || [] } ) {
184 ? @{ $self->{'comments'} || [] }
185 : join( "\n", @{ $self->{'comments'} || [] } );
188 return wantarray ? () : '';
198 Get or set the procedures's schema object.
200 $procedure->schema( $schema );
201 my $schema = $procedure->schema;
206 if ( my $arg = shift ) {
207 return $self->error('Not a schema object') unless
208 UNIVERSAL::isa( $arg, 'SQL::Translator::Schema' );
209 $self->{'schema'} = $arg;
212 return $self->{'schema'};
221 Determines if this procedure is the same as another
223 my $isIdentical = $procedure1->equals( $procedure2 );
229 my $case_insensitive = shift;
230 my $ignore_sql = shift;
232 return 0 unless $self->SUPER::equals($other);
233 return 0 unless $case_insensitive ? uc($self->name) eq uc($other->name) : $self->name eq $other->name;
235 unless ($ignore_sql) {
236 my $selfSql = $self->sql;
237 my $otherSql = $other->sql;
239 $selfSql =~ s/--.*$//mg;
240 $otherSql =~ s/--.*$//mg;
241 # Collapse whitespace to space to avoid whitespace comparison issues
242 $selfSql =~ s/\s+/ /sg;
243 $otherSql =~ s/\s+/ /sg;
244 return 0 unless $selfSql eq $otherSql;
247 return 0 unless $self->_compare_objects(scalar $self->parameters, scalar $other->parameters);
248 # return 0 unless $self->comments eq $other->comments;
249 # return 0 unless $case_insensitive ? uc($self->owner) eq uc($other->owner) : $self->owner eq $other->owner;
250 return 0 unless $self->_compare_objects(scalar $self->extra, scalar $other->extra);
256 undef $self->{'schema'}; # destroy cyclical reference
265 Ken Youens-Clark E<lt>kclark@cshl.orgE<gt>,
266 Paul Harrington E<lt>Paul-Harrington@deshaw.comE<gt>.