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