1 package SQL::Translator::Schema::Procedure;
7 SQL::Translator::Schema::Procedure - SQL::Translator procedure object
11 use SQL::Translator::Schema::Procedure;
12 my $procedure = SQL::Translator::Schema::Procedure->new(
14 sql => 'CREATE PROC foo AS SELECT * FROM bar',
15 parameters => 'foo,bar',
17 comments => 'blah blah blah',
23 C<SQL::Translator::Schema::Procedure> is a class for dealing with
24 stored procedures (and possibly other pieces of nameable SQL code?).
31 use SQL::Translator::Utils qw(ex2err);
32 use SQL::Translator::Role::ListAttr;
33 use SQL::Translator::Types qw(schema_obj);
35 extends 'SQL::Translator::Schema::Object';
37 our $VERSION = '1.59';
43 my $schema = SQL::Translator::Schema::Procedure->new;
49 Gets and set the parameters of the stored procedure.
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 ] );
57 my @parameters = $procedure->parameters;
61 with ListAttr parameters => ( uniq => 1 );
65 Get or set the procedure's name.
67 $procedure->name('foo');
68 my $name = $procedure->name;
72 has name => ( is => 'rw', default => sub { '' } );
76 Get or set the procedure's SQL.
78 $procedure->sql('select * from foo');
79 my $sql = $procedure->sql;
83 has sql => ( is => 'rw', default => sub { '' } );
87 Get or set the order of the procedure.
89 $procedure->order( 3 );
90 my $order = $procedure->order;
94 has order => ( is => 'rw' );
99 Get or set the owner of the procedure.
101 $procedure->owner('nomar');
102 my $sql = $procedure->owner;
106 has owner => ( is => 'rw', default => sub { '' } );
110 Get or set the comments on a procedure.
112 $procedure->comments('foo');
113 $procedure->comments('bar');
114 print join( ', ', $procedure->comments ); # prints "foo, bar"
120 coerce => sub { ref($_[0]) eq 'ARRAY' ? $_[0] : [$_[0]] },
121 default => sub { [] },
124 around comments => sub {
127 my @comments = ref $_[0] ? @{ $_[0] } : @_;
129 for my $arg ( @comments ) {
130 $arg = $arg->[0] if ref $arg;
131 push @{ $self->$orig }, $arg if defined $arg && $arg;
134 return wantarray ? @{ $self->$orig } : join( "\n", @{ $self->$orig } );
139 Get or set the procedures's schema object.
141 $procedure->schema( $schema );
142 my $schema = $procedure->schema;
146 has schema => ( is => 'rw', isa => schema_obj('Schema'), weak_ref => 1 );
148 around schema => \&ex2err;
152 Determines if this procedure is the same as another
154 my $isIdentical = $procedure1->equals( $procedure2 );
158 around equals => sub {
162 my $case_insensitive = shift;
163 my $ignore_sql = shift;
165 return 0 unless $self->$orig($other);
166 return 0 unless $case_insensitive ? uc($self->name) eq uc($other->name) : $self->name eq $other->name;
168 unless ($ignore_sql) {
169 my $selfSql = $self->sql;
170 my $otherSql = $other->sql;
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;
180 return 0 unless $self->_compare_objects(scalar $self->parameters, scalar $other->parameters);
181 # return 0 unless $self->comments eq $other->comments;
182 # return 0 unless $case_insensitive ? uc($self->owner) eq uc($other->owner) : $self->owner eq $other->owner;
183 return 0 unless $self->_compare_objects(scalar $self->extra, scalar $other->extra);
187 # Must come after all 'has' declarations
188 around new => \&ex2err;
196 Ken Youens-Clark E<lt>kclark@cshl.orgE<gt>,
197 Paul Harrington E<lt>Paul-Harrington@deshaw.comE<gt>.