Added "tables" and "options" methods to Schema::View
[dbsrgits/SQL-Translator.git] / lib / SQL / Translator / Schema / Procedure.pm
1 package SQL::Translator::Schema::Procedure;
2
3 =pod
4
5 =head1 NAME
6
7 SQL::Translator::Schema::Procedure - SQL::Translator procedure object
8
9 =head1 SYNOPSIS
10
11   use SQL::Translator::Schema::Procedure;
12   my $procedure  = SQL::Translator::Schema::Procedure->new(
13       name       => 'foo',
14       sql        => 'CREATE PROC foo AS SELECT * FROM bar',
15       parameters => 'foo,bar',
16       owner      => 'nomar',
17       comments   => 'blah blah blah',
18       schema     => $schema,
19   );
20
21 =head1 DESCRIPTION
22
23 C<SQL::Translator::Schema::Procedure> is a class for dealing with
24 stored procedures (and possibly other pieces of nameable SQL code?).
25
26 =head1 METHODS
27
28 =cut
29
30 use strict;
31 use warnings;
32 use SQL::Translator::Utils 'parse_list_arg';
33
34 use base 'SQL::Translator::Schema::Object';
35
36 our $VERSION = '1.59';
37
38 __PACKAGE__->_attributes( qw/
39     name sql parameters comments owner sql schema order
40 /);
41
42 =pod
43
44 =head2 new
45
46 Object constructor.
47
48   my $schema = SQL::Translator::Schema::Procedure->new;
49
50 =cut
51
52 sub parameters {
53
54 =pod
55
56 =head2 parameters
57
58 Gets and set the parameters of the stored procedure.
59
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 ] );
65
66   my @parameters = $procedure->parameters;
67
68 =cut
69
70     my $self   = shift;
71     my $parameters = parse_list_arg( @_ );
72
73     if ( @$parameters ) {
74         my ( %unique, @unique );
75         for my $p ( @$parameters ) {
76             next if $unique{ $p };
77             $unique{ $p } = 1;
78             push @unique, $p;
79         }
80
81         $self->{'parameters'} = \@unique;
82     }
83
84     return wantarray ? @{ $self->{'parameters'} || [] } : ($self->{'parameters'} || '');
85 }
86
87 sub name {
88
89 =pod
90
91 =head2 name
92
93 Get or set the procedure's name.
94
95   $procedure->name('foo');
96   my $name = $procedure->name;
97
98 =cut
99
100     my $self        = shift;
101     $self->{'name'} = shift if @_;
102     return $self->{'name'} || '';
103 }
104
105 sub sql {
106
107 =pod
108
109 =head2 sql
110
111 Get or set the procedure's SQL.
112
113   $procedure->sql('select * from foo');
114   my $sql = $procedure->sql;
115
116 =cut
117
118     my $self       = shift;
119     $self->{'sql'} = shift if @_;
120     return $self->{'sql'} || '';
121 }
122
123 sub order {
124
125 =pod
126
127 =head2 order
128
129 Get or set the order of the procedure.
130
131   $procedure->order( 3 );
132   my $order = $procedure->order;
133
134 =cut
135
136     my $self         = shift;
137     $self->{'order'} = shift if @_;
138     return $self->{'order'};
139 }
140
141 sub owner {
142
143 =pod
144
145 =head2 owner
146
147 Get or set the owner of the procedure.
148
149   $procedure->owner('nomar');
150   my $sql = $procedure->owner;
151
152 =cut
153
154     my $self         = shift;
155     $self->{'owner'} = shift if @_;
156     return $self->{'owner'} || '';
157 }
158
159 sub comments {
160
161 =pod
162
163 =head2 comments
164
165 Get or set the comments on a procedure.
166
167   $procedure->comments('foo');
168   $procedure->comments('bar');
169   print join( ', ', $procedure->comments ); # prints "foo, bar"
170
171 =cut
172
173     my $self = shift;
174
175     for my $arg ( @_ ) {
176         $arg = $arg->[0] if ref $arg;
177         push @{ $self->{'comments'} }, $arg if $arg;
178     }
179
180     if ( @{ $self->{'comments'} || [] } ) {
181         return wantarray
182             ? @{ $self->{'comments'} || [] }
183             : join( "\n", @{ $self->{'comments'} || [] } );
184     }
185     else {
186         return wantarray ? () : '';
187     }
188 }
189
190 sub schema {
191
192 =pod
193
194 =head2 schema
195
196 Get or set the procedures's schema object.
197
198   $procedure->schema( $schema );
199   my $schema = $procedure->schema;
200
201 =cut
202
203     my $self = shift;
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;
208     }
209
210     return $self->{'schema'};
211 }
212
213 sub equals {
214
215 =pod
216
217 =head2 equals
218
219 Determines if this procedure is the same as another
220
221   my $isIdentical = $procedure1->equals( $procedure2 );
222
223 =cut
224
225     my $self = shift;
226     my $other = shift;
227     my $case_insensitive = shift;
228     my $ignore_sql = shift;
229
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;
232
233     unless ($ignore_sql) {
234         my $selfSql = $self->sql;
235         my $otherSql = $other->sql;
236         # Remove comments
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;
243     }
244
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);
249     return 1;
250 }
251
252 sub DESTROY {
253     my $self = shift;
254     undef $self->{'schema'}; # destroy cyclical reference
255 }
256
257 1;
258
259 =pod
260
261 =head1 AUTHORS
262
263 Ken Youens-Clark E<lt>kclark@cshl.orgE<gt>,
264 Paul Harrington E<lt>Paul-Harrington@deshaw.comE<gt>.
265
266 =cut