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