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 | |
dee11153 |
30 | use Moo; |
31 | use SQL::Translator::Utils qw(parse_list_arg ex2err); |
32 | use SQL::Translator::Types qw(schema_obj); |
33 | use List::MoreUtils qw(uniq); |
2c0b3f9f |
34 | |
954ed12e |
35 | extends 'SQL::Translator::Schema::Object'; |
b6a880d1 |
36 | |
0c04c5a2 |
37 | our $VERSION = '1.59'; |
da06ac74 |
38 | |
2c0b3f9f |
39 | =head2 new |
40 | |
41 | Object constructor. |
42 | |
43 | my $schema = SQL::Translator::Schema::Procedure->new; |
44 | |
45 | =cut |
46 | |
2c0b3f9f |
47 | =head2 parameters |
48 | |
49 | Gets and set the parameters of the stored procedure. |
50 | |
51 | $procedure->parameters('id'); |
52 | $procedure->parameters('id', 'name'); |
53 | $procedure->parameters( 'id, name' ); |
54 | $procedure->parameters( [ 'id', 'name' ] ); |
55 | $procedure->parameters( qw[ id name ] ); |
56 | |
57 | my @parameters = $procedure->parameters; |
58 | |
59 | =cut |
60 | |
dee11153 |
61 | has parameters => ( |
62 | is => 'rw', |
63 | default => sub { [] }, |
64 | coerce => sub { [uniq @{parse_list_arg($_[0])}] }, |
65 | ); |
2c0b3f9f |
66 | |
dee11153 |
67 | around parameters => sub { |
68 | my $orig = shift; |
69 | my $self = shift; |
70 | my $fields = parse_list_arg( @_ ); |
71 | $self->$orig($fields) if @$fields; |
2c0b3f9f |
72 | |
dee11153 |
73 | return wantarray ? @{ $self->$orig } : $self->$orig; |
74 | }; |
2c0b3f9f |
75 | |
76 | =head2 name |
77 | |
78 | Get or set the procedure's name. |
79 | |
80 | $procedure->name('foo'); |
81 | my $name = $procedure->name; |
82 | |
83 | =cut |
84 | |
dee11153 |
85 | has name => ( is => 'rw', default => sub { '' } ); |
2c0b3f9f |
86 | |
87 | =head2 sql |
88 | |
89 | Get or set the procedure's SQL. |
90 | |
91 | $procedure->sql('select * from foo'); |
92 | my $sql = $procedure->sql; |
93 | |
94 | =cut |
95 | |
dee11153 |
96 | has sql => ( is => 'rw', default => sub { '' } ); |
2c0b3f9f |
97 | |
98 | =head2 order |
99 | |
100 | Get or set the order of the procedure. |
101 | |
102 | $procedure->order( 3 ); |
103 | my $order = $procedure->order; |
104 | |
105 | =cut |
106 | |
dee11153 |
107 | has order => ( is => 'rw' ); |
2c0b3f9f |
108 | |
2c0b3f9f |
109 | |
110 | =head2 owner |
111 | |
112 | Get or set the owner of the procedure. |
113 | |
114 | $procedure->owner('nomar'); |
115 | my $sql = $procedure->owner; |
116 | |
117 | =cut |
118 | |
dee11153 |
119 | has owner => ( is => 'rw', default => sub { '' } ); |
2c0b3f9f |
120 | |
121 | =head2 comments |
122 | |
123 | Get or set the comments on a procedure. |
124 | |
125 | $procedure->comments('foo'); |
126 | $procedure->comments('bar'); |
127 | print join( ', ', $procedure->comments ); # prints "foo, bar" |
128 | |
129 | =cut |
130 | |
dee11153 |
131 | has comments => ( |
132 | is => 'rw', |
133 | coerce => sub { ref($_[0]) eq 'ARRAY' ? $_[0] : [$_[0]] }, |
134 | default => sub { [] }, |
135 | ); |
2c0b3f9f |
136 | |
dee11153 |
137 | around comments => sub { |
138 | my $orig = shift; |
139 | my $self = shift; |
140 | my @comments = ref $_[0] ? @{ $_[0] } : @_; |
2c0b3f9f |
141 | |
dee11153 |
142 | for my $arg ( @comments ) { |
143 | $arg = $arg->[0] if ref $arg; |
144 | push @{ $self->$orig }, $arg if defined $arg && $arg; |
2c0b3f9f |
145 | } |
2c0b3f9f |
146 | |
dee11153 |
147 | return wantarray ? @{ $self->$orig } : join( "\n", @{ $self->$orig } ); |
148 | }; |
2c0b3f9f |
149 | |
150 | =head2 schema |
151 | |
152 | Get or set the procedures's schema object. |
153 | |
154 | $procedure->schema( $schema ); |
155 | my $schema = $procedure->schema; |
156 | |
157 | =cut |
158 | |
a5bfeba8 |
159 | has schema => ( is => 'rw', isa => schema_obj('Schema'), weak_ref => 1 ); |
2c0b3f9f |
160 | |
dee11153 |
161 | around schema => \&ex2err; |
abf315bb |
162 | |
163 | =head2 equals |
164 | |
165 | Determines if this procedure is the same as another |
166 | |
167 | my $isIdentical = $procedure1->equals( $procedure2 ); |
168 | |
169 | =cut |
170 | |
dee11153 |
171 | around equals => sub { |
172 | my $orig = shift; |
abf315bb |
173 | my $self = shift; |
174 | my $other = shift; |
deee3ae8 |
175 | my $case_insensitive = shift; |
d1a895ce |
176 | my $ignore_sql = shift; |
ea93df61 |
177 | |
dee11153 |
178 | return 0 unless $self->$orig($other); |
deee3ae8 |
179 | return 0 unless $case_insensitive ? uc($self->name) eq uc($other->name) : $self->name eq $other->name; |
ea93df61 |
180 | |
d1a895ce |
181 | unless ($ignore_sql) { |
182 | my $selfSql = $self->sql; |
183 | my $otherSql = $other->sql; |
184 | # Remove comments |
185 | $selfSql =~ s/--.*$//mg; |
186 | $otherSql =~ s/--.*$//mg; |
187 | # Collapse whitespace to space to avoid whitespace comparison issues |
188 | $selfSql =~ s/\s+/ /sg; |
189 | $otherSql =~ s/\s+/ /sg; |
190 | return 0 unless $selfSql eq $otherSql; |
191 | } |
ea93df61 |
192 | |
4598b71c |
193 | return 0 unless $self->_compare_objects(scalar $self->parameters, scalar $other->parameters); |
abf315bb |
194 | # return 0 unless $self->comments eq $other->comments; |
d1a895ce |
195 | # return 0 unless $case_insensitive ? uc($self->owner) eq uc($other->owner) : $self->owner eq $other->owner; |
4598b71c |
196 | return 0 unless $self->_compare_objects(scalar $self->extra, scalar $other->extra); |
abf315bb |
197 | return 1; |
dee11153 |
198 | }; |
abf315bb |
199 | |
dee11153 |
200 | # Must come after all 'has' declarations |
201 | around new => \&ex2err; |
202 | |
2c0b3f9f |
203 | 1; |
204 | |
205 | =pod |
206 | |
207 | =head1 AUTHORS |
208 | |
c3b0b535 |
209 | Ken Youens-Clark E<lt>kclark@cshl.orgE<gt>, |
2c0b3f9f |
210 | Paul Harrington E<lt>Paul-Harrington@deshaw.comE<gt>. |
6606c4c6 |
211 | |
212 | =cut |