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 __PACKAGE__->_attributes( qw/
40 name sql parameters comments owner sql schema order
49 my $schema = SQL::Translator::Schema::Procedure->new;
59 Gets and set the parameters of the stored procedure.
61 $procedure->parameters('id');
62 $procedure->parameters('id', 'name');
63 $procedure->parameters( 'id, name' );
64 $procedure->parameters( [ 'id', 'name' ] );
65 $procedure->parameters( qw[ id name ] );
67 my @parameters = $procedure->parameters;
72 my $parameters = parse_list_arg( @_ );
75 my ( %unique, @unique );
76 for my $p ( @$parameters ) {
77 next if $unique{ $p };
82 $self->{'parameters'} = \@unique;
85 return wantarray ? @{ $self->{'parameters'} || [] } : ($self->{'parameters'} || '');
94 Get or set the procedure's name.
96 $procedure->name('foo');
97 my $name = $procedure->name;
102 $self->{'name'} = shift if @_;
103 return $self->{'name'} || '';
112 Get or set the procedure's SQL.
114 $procedure->sql('select * from foo');
115 my $sql = $procedure->sql;
120 $self->{'sql'} = shift if @_;
121 return $self->{'sql'} || '';
130 Get or set the order of the procedure.
132 $procedure->order( 3 );
133 my $order = $procedure->order;
138 $self->{'order'} = shift if @_;
139 return $self->{'order'};
148 Get or set the owner of the procedure.
150 $procedure->owner('nomar');
151 my $sql = $procedure->owner;
156 $self->{'owner'} = shift if @_;
157 return $self->{'owner'} || '';
166 Get or set the comments on a procedure.
168 $procedure->comments('foo');
169 $procedure->comments('bar');
170 print join( ', ', $procedure->comments ); # prints "foo, bar"
177 $arg = $arg->[0] if ref $arg;
178 push @{ $self->{'comments'} }, $arg if $arg;
181 if ( @{ $self->{'comments'} || [] } ) {
183 ? @{ $self->{'comments'} || [] }
184 : join( "\n", @{ $self->{'comments'} || [] } );
187 return wantarray ? () : '';
197 Get or set the procedures's schema object.
199 $procedure->schema( $schema );
200 my $schema = $procedure->schema;
205 if ( my $arg = shift ) {
206 return $self->error('Not a schema object') unless
207 UNIVERSAL::isa( $arg, 'SQL::Translator::Schema' );
208 $self->{'schema'} = $arg;
211 return $self->{'schema'};
220 Determines if this procedure is the same as another
222 my $isIdentical = $procedure1->equals( $procedure2 );
228 my $case_insensitive = shift;
229 my $ignore_sql = shift;
231 return 0 unless $self->SUPER::equals($other);
232 return 0 unless $case_insensitive ? uc($self->name) eq uc($other->name) : $self->name eq $other->name;
234 unless ($ignore_sql) {
235 my $selfSql = $self->sql;
236 my $otherSql = $other->sql;
238 $selfSql =~ s/--.*$//mg;
239 $otherSql =~ s/--.*$//mg;
240 # Collapse whitespace to space to avoid whitespace comparison issues
241 $selfSql =~ s/\s+/ /sg;
242 $otherSql =~ s/\s+/ /sg;
243 return 0 unless $selfSql eq $otherSql;
246 return 0 unless $self->_compare_objects(scalar $self->parameters, scalar $other->parameters);
247 # return 0 unless $self->comments eq $other->comments;
248 # return 0 unless $case_insensitive ? uc($self->owner) eq uc($other->owner) : $self->owner eq $other->owner;
249 return 0 unless $self->_compare_objects(scalar $self->extra, scalar $other->extra);
255 undef $self->{'schema'}; # destroy cyclical reference
264 Ken Youens-Clark E<lt>kclark@cshl.orgE<gt>,
265 Paul Harrington E<lt>Paul-Harrington@deshaw.comE<gt>.