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