take out duplicate docs
[dbsrgits/SQL-Translator.git] / lib / SQL / Translator / Schema / Procedure.pm
CommitLineData
2c0b3f9f 1package SQL::Translator::Schema::Procedure;
2
44659089 3# ----------------------------------------------------------------------
4# Copyright (C) 2002-2009 SQLFairy Authors
5#
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.
9#
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.
14#
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
18# 02111-1307 USA
19# -------------------------------------------------------------------
20
2c0b3f9f 21=pod
22
23=head1 NAME
24
25SQL::Translator::Schema::Procedure - SQL::Translator procedure object
26
27=head1 SYNOPSIS
28
29 use SQL::Translator::Schema::Procedure;
30 my $procedure = SQL::Translator::Schema::Procedure->new(
31 name => 'foo',
32 sql => 'CREATE PROC foo AS SELECT * FROM bar',
33 parameters => 'foo,bar',
34 owner => 'nomar',
35 comments => 'blah blah blah',
36 schema => $schema,
37 );
38
39=head1 DESCRIPTION
40
41C<SQL::Translator::Schema::Procedure> is a class for dealing with
42stored procedures (and possibly other pieces of nameable SQL code?).
43
44=head1 METHODS
45
46=cut
47
48use strict;
2c0b3f9f 49use SQL::Translator::Utils 'parse_list_arg';
50
b6a880d1 51use base 'SQL::Translator::Schema::Object';
52
da06ac74 53use vars qw($VERSION);
54
11ad2df9 55$VERSION = '1.59';
da06ac74 56
9371be50 57__PACKAGE__->_attributes( qw/
58 name sql parameters comments owner sql schema order
59/);
2c0b3f9f 60
61=pod
62
63=head2 new
64
65Object constructor.
66
67 my $schema = SQL::Translator::Schema::Procedure->new;
68
69=cut
70
2c0b3f9f 71sub parameters {
72
73=pod
74
75=head2 parameters
76
77Gets and set the parameters of the stored procedure.
78
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 ] );
84
85 my @parameters = $procedure->parameters;
86
87=cut
88
89 my $self = shift;
90 my $parameters = parse_list_arg( @_ );
91
92 if ( @$parameters ) {
93 my ( %unique, @unique );
94 for my $p ( @$parameters ) {
95 next if $unique{ $p };
96 $unique{ $p } = 1;
97 push @unique, $p;
98 }
99
100 $self->{'parameters'} = \@unique;
101 }
102
4598b71c 103 return wantarray ? @{ $self->{'parameters'} || [] } : ($self->{'parameters'} || '');
2c0b3f9f 104}
105
2c0b3f9f 106sub name {
107
108=pod
109
110=head2 name
111
112Get or set the procedure's name.
113
114 $procedure->name('foo');
115 my $name = $procedure->name;
116
117=cut
118
119 my $self = shift;
120 $self->{'name'} = shift if @_;
121 return $self->{'name'} || '';
122}
123
2c0b3f9f 124sub sql {
125
126=pod
127
128=head2 sql
129
130Get or set the procedure's SQL.
131
132 $procedure->sql('select * from foo');
133 my $sql = $procedure->sql;
134
135=cut
136
137 my $self = shift;
138 $self->{'sql'} = shift if @_;
139 return $self->{'sql'} || '';
140}
141
2c0b3f9f 142sub order {
143
144=pod
145
146=head2 order
147
148Get or set the order of the procedure.
149
150 $procedure->order( 3 );
151 my $order = $procedure->order;
152
153=cut
154
155 my $self = shift;
156 $self->{'order'} = shift if @_;
157 return $self->{'order'};
158}
159
2c0b3f9f 160sub owner {
161
162=pod
163
164=head2 owner
165
166Get or set the owner of the procedure.
167
168 $procedure->owner('nomar');
169 my $sql = $procedure->owner;
170
171=cut
172
173 my $self = shift;
174 $self->{'owner'} = shift if @_;
175 return $self->{'owner'} || '';
176}
177
2c0b3f9f 178sub comments {
179
180=pod
181
182=head2 comments
183
184Get or set the comments on a procedure.
185
186 $procedure->comments('foo');
187 $procedure->comments('bar');
188 print join( ', ', $procedure->comments ); # prints "foo, bar"
189
190=cut
191
192 my $self = shift;
193
194 for my $arg ( @_ ) {
195 $arg = $arg->[0] if ref $arg;
196 push @{ $self->{'comments'} }, $arg if $arg;
197 }
198
199 if ( @{ $self->{'comments'} || [] } ) {
ea93df61 200 return wantarray
2c0b3f9f 201 ? @{ $self->{'comments'} || [] }
202 : join( "\n", @{ $self->{'comments'} || [] } );
203 }
204 else {
205 return wantarray ? () : '';
206 }
207}
208
2c0b3f9f 209sub schema {
210
211=pod
212
213=head2 schema
214
215Get or set the procedures's schema object.
216
217 $procedure->schema( $schema );
218 my $schema = $procedure->schema;
219
220=cut
221
222 my $self = shift;
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;
227 }
228
229 return $self->{'schema'};
230}
231
abf315bb 232sub equals {
233
234=pod
235
236=head2 equals
237
238Determines if this procedure is the same as another
239
240 my $isIdentical = $procedure1->equals( $procedure2 );
241
242=cut
243
244 my $self = shift;
245 my $other = shift;
deee3ae8 246 my $case_insensitive = shift;
d1a895ce 247 my $ignore_sql = shift;
ea93df61 248
abf315bb 249 return 0 unless $self->SUPER::equals($other);
deee3ae8 250 return 0 unless $case_insensitive ? uc($self->name) eq uc($other->name) : $self->name eq $other->name;
ea93df61 251
d1a895ce 252 unless ($ignore_sql) {
253 my $selfSql = $self->sql;
254 my $otherSql = $other->sql;
255 # Remove comments
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;
262 }
ea93df61 263
4598b71c 264 return 0 unless $self->_compare_objects(scalar $self->parameters, scalar $other->parameters);
abf315bb 265# return 0 unless $self->comments eq $other->comments;
d1a895ce 266# return 0 unless $case_insensitive ? uc($self->owner) eq uc($other->owner) : $self->owner eq $other->owner;
4598b71c 267 return 0 unless $self->_compare_objects(scalar $self->extra, scalar $other->extra);
abf315bb 268 return 1;
269}
270
2c0b3f9f 271sub DESTROY {
272 my $self = shift;
273 undef $self->{'schema'}; # destroy cyclical reference
274}
275
2761;
277
278=pod
279
280=head1 AUTHORS
281
c3b0b535 282Ken Youens-Clark E<lt>kclark@cshl.orgE<gt>,
2c0b3f9f 283Paul Harrington E<lt>Paul-Harrington@deshaw.comE<gt>.
6606c4c6 284
285=cut