Reduce $Id to its normal form
[dbsrgits/SQL-Translator.git] / lib / SQL / Translator / Schema / Procedure.pm
CommitLineData
2c0b3f9f 1package SQL::Translator::Schema::Procedure;
2
3# ----------------------------------------------------------------------
782b5a43 4# $Id$
2c0b3f9f 5# ----------------------------------------------------------------------
478f608d 6# Copyright (C) 2002-2009 SQLFairy Authors
2c0b3f9f 7#
8# This program is free software; you can redistribute it and/or
9# modify it under the terms of the GNU General Public License as
10# published by the Free Software Foundation; version 2.
11#
12# This program is distributed in the hope that it will be useful, but
13# WITHOUT ANY WARRANTY; without even the implied warranty of
14# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15# General Public License for more details.
16#
17# You should have received a copy of the GNU General Public License
18# along with this program; if not, write to the Free Software
19# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
20# 02111-1307 USA
21# -------------------------------------------------------------------
22
23=pod
24
25=head1 NAME
26
27SQL::Translator::Schema::Procedure - SQL::Translator procedure object
28
29=head1 SYNOPSIS
30
31 use SQL::Translator::Schema::Procedure;
32 my $procedure = SQL::Translator::Schema::Procedure->new(
33 name => 'foo',
34 sql => 'CREATE PROC foo AS SELECT * FROM bar',
35 parameters => 'foo,bar',
36 owner => 'nomar',
37 comments => 'blah blah blah',
38 schema => $schema,
39 );
40
41=head1 DESCRIPTION
42
43C<SQL::Translator::Schema::Procedure> is a class for dealing with
44stored procedures (and possibly other pieces of nameable SQL code?).
45
46=head1 METHODS
47
48=cut
49
50use strict;
2c0b3f9f 51use SQL::Translator::Utils 'parse_list_arg';
52
b6a880d1 53use base 'SQL::Translator::Schema::Object';
54
da06ac74 55use vars qw($VERSION);
56
57$VERSION = '1.99';
58
2c0b3f9f 59# ----------------------------------------------------------------------
9371be50 60
61__PACKAGE__->_attributes( qw/
62 name sql parameters comments owner sql schema order
63/);
2c0b3f9f 64
65=pod
66
67=head2 new
68
69Object constructor.
70
71 my $schema = SQL::Translator::Schema::Procedure->new;
72
73=cut
74
2c0b3f9f 75# ----------------------------------------------------------------------
76sub parameters {
77
78=pod
79
80=head2 parameters
81
82Gets and set the parameters of the stored procedure.
83
84 $procedure->parameters('id');
85 $procedure->parameters('id', 'name');
86 $procedure->parameters( 'id, name' );
87 $procedure->parameters( [ 'id', 'name' ] );
88 $procedure->parameters( qw[ id name ] );
89
90 my @parameters = $procedure->parameters;
91
92=cut
93
94 my $self = shift;
95 my $parameters = parse_list_arg( @_ );
96
97 if ( @$parameters ) {
98 my ( %unique, @unique );
99 for my $p ( @$parameters ) {
100 next if $unique{ $p };
101 $unique{ $p } = 1;
102 push @unique, $p;
103 }
104
105 $self->{'parameters'} = \@unique;
106 }
107
4598b71c 108 return wantarray ? @{ $self->{'parameters'} || [] } : ($self->{'parameters'} || '');
2c0b3f9f 109}
110
111# ----------------------------------------------------------------------
112sub name {
113
114=pod
115
116=head2 name
117
118Get or set the procedure's name.
119
120 $procedure->name('foo');
121 my $name = $procedure->name;
122
123=cut
124
125 my $self = shift;
126 $self->{'name'} = shift if @_;
127 return $self->{'name'} || '';
128}
129
130# ----------------------------------------------------------------------
131sub sql {
132
133=pod
134
135=head2 sql
136
137Get or set the procedure's SQL.
138
139 $procedure->sql('select * from foo');
140 my $sql = $procedure->sql;
141
142=cut
143
144 my $self = shift;
145 $self->{'sql'} = shift if @_;
146 return $self->{'sql'} || '';
147}
148
149# ----------------------------------------------------------------------
150sub order {
151
152=pod
153
154=head2 order
155
156Get or set the order of the procedure.
157
158 $procedure->order( 3 );
159 my $order = $procedure->order;
160
161=cut
162
163 my $self = shift;
164 $self->{'order'} = shift if @_;
165 return $self->{'order'};
166}
167
168# ----------------------------------------------------------------------
169sub owner {
170
171=pod
172
173=head2 owner
174
175Get or set the owner of the procedure.
176
177 $procedure->owner('nomar');
178 my $sql = $procedure->owner;
179
180=cut
181
182 my $self = shift;
183 $self->{'owner'} = shift if @_;
184 return $self->{'owner'} || '';
185}
186
187# ----------------------------------------------------------------------
188sub comments {
189
190=pod
191
192=head2 comments
193
194Get or set the comments on a procedure.
195
196 $procedure->comments('foo');
197 $procedure->comments('bar');
198 print join( ', ', $procedure->comments ); # prints "foo, bar"
199
200=cut
201
202 my $self = shift;
203
204 for my $arg ( @_ ) {
205 $arg = $arg->[0] if ref $arg;
206 push @{ $self->{'comments'} }, $arg if $arg;
207 }
208
209 if ( @{ $self->{'comments'} || [] } ) {
210 return wantarray
211 ? @{ $self->{'comments'} || [] }
212 : join( "\n", @{ $self->{'comments'} || [] } );
213 }
214 else {
215 return wantarray ? () : '';
216 }
217}
218
219# ----------------------------------------------------------------------
220sub schema {
221
222=pod
223
224=head2 schema
225
226Get or set the procedures's schema object.
227
228 $procedure->schema( $schema );
229 my $schema = $procedure->schema;
230
231=cut
232
233 my $self = shift;
234 if ( my $arg = shift ) {
235 return $self->error('Not a schema object') unless
236 UNIVERSAL::isa( $arg, 'SQL::Translator::Schema' );
237 $self->{'schema'} = $arg;
238 }
239
240 return $self->{'schema'};
241}
242
243# ----------------------------------------------------------------------
abf315bb 244sub equals {
245
246=pod
247
248=head2 equals
249
250Determines if this procedure is the same as another
251
252 my $isIdentical = $procedure1->equals( $procedure2 );
253
254=cut
255
256 my $self = shift;
257 my $other = shift;
deee3ae8 258 my $case_insensitive = shift;
d1a895ce 259 my $ignore_sql = shift;
abf315bb 260
261 return 0 unless $self->SUPER::equals($other);
deee3ae8 262 return 0 unless $case_insensitive ? uc($self->name) eq uc($other->name) : $self->name eq $other->name;
263
d1a895ce 264 unless ($ignore_sql) {
265 my $selfSql = $self->sql;
266 my $otherSql = $other->sql;
267 # Remove comments
268 $selfSql =~ s/--.*$//mg;
269 $otherSql =~ s/--.*$//mg;
270 # Collapse whitespace to space to avoid whitespace comparison issues
271 $selfSql =~ s/\s+/ /sg;
272 $otherSql =~ s/\s+/ /sg;
273 return 0 unless $selfSql eq $otherSql;
274 }
deee3ae8 275
4598b71c 276 return 0 unless $self->_compare_objects(scalar $self->parameters, scalar $other->parameters);
abf315bb 277# return 0 unless $self->comments eq $other->comments;
d1a895ce 278# return 0 unless $case_insensitive ? uc($self->owner) eq uc($other->owner) : $self->owner eq $other->owner;
4598b71c 279 return 0 unless $self->_compare_objects(scalar $self->extra, scalar $other->extra);
abf315bb 280 return 1;
281}
282
283# ----------------------------------------------------------------------
2c0b3f9f 284sub DESTROY {
285 my $self = shift;
286 undef $self->{'schema'}; # destroy cyclical reference
287}
288
2891;
290
6606c4c6 291# ----------------------------------------------------------------------
292
2c0b3f9f 293=pod
294
295=head1 AUTHORS
296
297Ken Y. Clark E<lt>kclark@cshl.orgE<gt>,
298Paul Harrington E<lt>Paul-Harrington@deshaw.comE<gt>.
6606c4c6 299
300=cut