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(parse_list_arg ex2err);
32 use SQL::Translator::Types qw(schema_obj);
33 use List::MoreUtils qw(uniq);
36 SQL::Translator::Schema::Role::Extra
37 SQL::Translator::Schema::Role::Error
38 SQL::Translator::Schema::Role::Compare
41 our $VERSION = '1.59';
47 my $schema = SQL::Translator::Schema::Procedure->new;
53 Gets and set the parameters of the stored procedure.
55 $procedure->parameters('id');
56 $procedure->parameters('id', 'name');
57 $procedure->parameters( 'id, name' );
58 $procedure->parameters( [ 'id', 'name' ] );
59 $procedure->parameters( qw[ id name ] );
61 my @parameters = $procedure->parameters;
67 default => sub { [] },
68 coerce => sub { [uniq @{parse_list_arg($_[0])}] },
71 around parameters => sub {
74 my $fields = parse_list_arg( @_ );
75 $self->$orig($fields) if @$fields;
77 return wantarray ? @{ $self->$orig } : $self->$orig;
82 Get or set the procedure's name.
84 $procedure->name('foo');
85 my $name = $procedure->name;
89 has name => ( is => 'rw', default => sub { '' } );
93 Get or set the procedure's SQL.
95 $procedure->sql('select * from foo');
96 my $sql = $procedure->sql;
100 has sql => ( is => 'rw', default => sub { '' } );
104 Get or set the order of the procedure.
106 $procedure->order( 3 );
107 my $order = $procedure->order;
111 has order => ( is => 'rw' );
116 Get or set the owner of the procedure.
118 $procedure->owner('nomar');
119 my $sql = $procedure->owner;
123 has owner => ( is => 'rw', default => sub { '' } );
127 Get or set the comments on a procedure.
129 $procedure->comments('foo');
130 $procedure->comments('bar');
131 print join( ', ', $procedure->comments ); # prints "foo, bar"
137 coerce => sub { ref($_[0]) eq 'ARRAY' ? $_[0] : [$_[0]] },
138 default => sub { [] },
141 around comments => sub {
144 my @comments = ref $_[0] ? @{ $_[0] } : @_;
146 for my $arg ( @comments ) {
147 $arg = $arg->[0] if ref $arg;
148 push @{ $self->$orig }, $arg if defined $arg && $arg;
151 return wantarray ? @{ $self->$orig } : join( "\n", @{ $self->$orig } );
156 Get or set the procedures's schema object.
158 $procedure->schema( $schema );
159 my $schema = $procedure->schema;
163 has schema => ( is => 'rw', isa => schema_obj('Schema') );
165 around schema => \&ex2err;
169 Determines if this procedure is the same as another
171 my $isIdentical = $procedure1->equals( $procedure2 );
175 around equals => sub {
179 my $case_insensitive = shift;
180 my $ignore_sql = shift;
182 return 0 unless $self->$orig($other);
183 return 0 unless $case_insensitive ? uc($self->name) eq uc($other->name) : $self->name eq $other->name;
185 unless ($ignore_sql) {
186 my $selfSql = $self->sql;
187 my $otherSql = $other->sql;
189 $selfSql =~ s/--.*$//mg;
190 $otherSql =~ s/--.*$//mg;
191 # Collapse whitespace to space to avoid whitespace comparison issues
192 $selfSql =~ s/\s+/ /sg;
193 $otherSql =~ s/\s+/ /sg;
194 return 0 unless $selfSql eq $otherSql;
197 return 0 unless $self->_compare_objects(scalar $self->parameters, scalar $other->parameters);
198 # return 0 unless $self->comments eq $other->comments;
199 # return 0 unless $case_insensitive ? uc($self->owner) eq uc($other->owner) : $self->owner eq $other->owner;
200 return 0 unless $self->_compare_objects(scalar $self->extra, scalar $other->extra);
206 undef $self->{'schema'}; # destroy cyclical reference
209 # Must come after all 'has' declarations
210 around new => \&ex2err;
218 Ken Youens-Clark E<lt>kclark@cshl.orgE<gt>,
219 Paul Harrington E<lt>Paul-Harrington@deshaw.comE<gt>.