Commit | Line | Data |
2c0b3f9f |
1 | package SQL::Translator::Schema::Procedure; |
2 | |
2c0b3f9f |
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; |
f27f9229 |
31 | use warnings; |
2c0b3f9f |
32 | use SQL::Translator::Utils 'parse_list_arg'; |
33 | |
b6a880d1 |
34 | use base 'SQL::Translator::Schema::Object'; |
35 | |
0c04c5a2 |
36 | our $VERSION = '1.59'; |
da06ac74 |
37 | |
9371be50 |
38 | __PACKAGE__->_attributes( qw/ |
39 | name sql parameters comments owner sql schema order |
40 | /); |
2c0b3f9f |
41 | |
42 | =pod |
43 | |
44 | =head2 new |
45 | |
46 | Object constructor. |
47 | |
48 | my $schema = SQL::Translator::Schema::Procedure->new; |
49 | |
50 | =cut |
51 | |
2c0b3f9f |
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 | |
4598b71c |
84 | return wantarray ? @{ $self->{'parameters'} || [] } : ($self->{'parameters'} || ''); |
2c0b3f9f |
85 | } |
86 | |
2c0b3f9f |
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 | |
2c0b3f9f |
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 | |
2c0b3f9f |
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 | |
2c0b3f9f |
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 | |
2c0b3f9f |
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'} || [] } ) { |
ea93df61 |
181 | return wantarray |
2c0b3f9f |
182 | ? @{ $self->{'comments'} || [] } |
183 | : join( "\n", @{ $self->{'comments'} || [] } ); |
184 | } |
185 | else { |
186 | return wantarray ? () : ''; |
187 | } |
188 | } |
189 | |
2c0b3f9f |
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 | |
abf315bb |
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; |
deee3ae8 |
227 | my $case_insensitive = shift; |
d1a895ce |
228 | my $ignore_sql = shift; |
ea93df61 |
229 | |
abf315bb |
230 | return 0 unless $self->SUPER::equals($other); |
deee3ae8 |
231 | return 0 unless $case_insensitive ? uc($self->name) eq uc($other->name) : $self->name eq $other->name; |
ea93df61 |
232 | |
d1a895ce |
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 | } |
ea93df61 |
244 | |
4598b71c |
245 | return 0 unless $self->_compare_objects(scalar $self->parameters, scalar $other->parameters); |
abf315bb |
246 | # return 0 unless $self->comments eq $other->comments; |
d1a895ce |
247 | # return 0 unless $case_insensitive ? uc($self->owner) eq uc($other->owner) : $self->owner eq $other->owner; |
4598b71c |
248 | return 0 unless $self->_compare_objects(scalar $self->extra, scalar $other->extra); |
abf315bb |
249 | return 1; |
250 | } |
251 | |
2c0b3f9f |
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 | |
c3b0b535 |
263 | Ken Youens-Clark E<lt>kclark@cshl.orgE<gt>, |
2c0b3f9f |
264 | Paul Harrington E<lt>Paul-Harrington@deshaw.comE<gt>. |
6606c4c6 |
265 | |
266 | =cut |