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 our $VERSION = '1.59';
38 __PACKAGE__->_attributes( qw/
39 name sql parameters comments owner sql schema order
48 my $schema = SQL::Translator::Schema::Procedure->new;
58 Gets and set the parameters of the stored procedure.
60 $procedure->parameters('id');
61 $procedure->parameters('id', 'name');
62 $procedure->parameters( 'id, name' );
63 $procedure->parameters( [ 'id', 'name' ] );
64 $procedure->parameters( qw[ id name ] );
66 my @parameters = $procedure->parameters;
71 my $parameters = parse_list_arg( @_ );
74 my ( %unique, @unique );
75 for my $p ( @$parameters ) {
76 next if $unique{ $p };
81 $self->{'parameters'} = \@unique;
84 return wantarray ? @{ $self->{'parameters'} || [] } : ($self->{'parameters'} || '');
93 Get or set the procedure's name.
95 $procedure->name('foo');
96 my $name = $procedure->name;
101 $self->{'name'} = shift if @_;
102 return $self->{'name'} || '';
111 Get or set the procedure's SQL.
113 $procedure->sql('select * from foo');
114 my $sql = $procedure->sql;
119 $self->{'sql'} = shift if @_;
120 return $self->{'sql'} || '';
129 Get or set the order of the procedure.
131 $procedure->order( 3 );
132 my $order = $procedure->order;
137 $self->{'order'} = shift if @_;
138 return $self->{'order'};
147 Get or set the owner of the procedure.
149 $procedure->owner('nomar');
150 my $sql = $procedure->owner;
155 $self->{'owner'} = shift if @_;
156 return $self->{'owner'} || '';
165 Get or set the comments on a procedure.
167 $procedure->comments('foo');
168 $procedure->comments('bar');
169 print join( ', ', $procedure->comments ); # prints "foo, bar"
176 $arg = $arg->[0] if ref $arg;
177 push @{ $self->{'comments'} }, $arg if $arg;
180 if ( @{ $self->{'comments'} || [] } ) {
182 ? @{ $self->{'comments'} || [] }
183 : join( "\n", @{ $self->{'comments'} || [] } );
186 return wantarray ? () : '';
196 Get or set the procedures's schema object.
198 $procedure->schema( $schema );
199 my $schema = $procedure->schema;
204 if ( my $arg = shift ) {
205 return $self->error('Not a schema object') unless
206 UNIVERSAL::isa( $arg, 'SQL::Translator::Schema' );
207 $self->{'schema'} = $arg;
210 return $self->{'schema'};
219 Determines if this procedure is the same as another
221 my $isIdentical = $procedure1->equals( $procedure2 );
227 my $case_insensitive = shift;
228 my $ignore_sql = shift;
230 return 0 unless $self->SUPER::equals($other);
231 return 0 unless $case_insensitive ? uc($self->name) eq uc($other->name) : $self->name eq $other->name;
233 unless ($ignore_sql) {
234 my $selfSql = $self->sql;
235 my $otherSql = $other->sql;
237 $selfSql =~ s/--.*$//mg;
238 $otherSql =~ s/--.*$//mg;
239 # Collapse whitespace to space to avoid whitespace comparison issues
240 $selfSql =~ s/\s+/ /sg;
241 $otherSql =~ s/\s+/ /sg;
242 return 0 unless $selfSql eq $otherSql;
245 return 0 unless $self->_compare_objects(scalar $self->parameters, scalar $other->parameters);
246 # return 0 unless $self->comments eq $other->comments;
247 # return 0 unless $case_insensitive ? uc($self->owner) eq uc($other->owner) : $self->owner eq $other->owner;
248 return 0 unless $self->_compare_objects(scalar $self->extra, scalar $other->extra);
254 undef $self->{'schema'}; # destroy cyclical reference
263 Ken Youens-Clark E<lt>kclark@cshl.orgE<gt>,
264 Paul Harrington E<lt>Paul-Harrington@deshaw.comE<gt>.