| 1 | package SQL::Translator::Schema::View; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | =pod | ||||||||||||||||||||||||||||
| 4 | |||||||||||||||||||||||||||||
| 5 | =head1 NAME | ||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||
| 7 | SQL::Translator::Schema::View - SQL::Translator view object | ||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||
| 9 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||
| 11 | use SQL::Translator::Schema::View; | ||||||||||||||||||||||||||||
| 12 | my $view = SQL::Translator::Schema::View->new( | ||||||||||||||||||||||||||||
| 13 | name => 'foo', # name, required | ||||||||||||||||||||||||||||
| 14 | sql => 'select id, name from foo', # SQL for view | ||||||||||||||||||||||||||||
| 15 | fields => 'id, name', # field names in view | ||||||||||||||||||||||||||||
| 16 | ); | ||||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||
| 18 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||
| 20 | C<SQL::Translator::Schema::View> is the view object. | ||||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||
| 22 | =head1 METHODS | ||||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||
| 24 | =cut | ||||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||
| 26 | 75 | ▶ | use Moo; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 27 | 75 | ▶ | use SQL::Translator::Utils qw(ex2err); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 28 | 75 | ▶ | use SQL::Translator::Types qw(schema_obj); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 29 | 75 | ▶ | use SQL::Translator::Role::ListAttr; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 30 | 75 | ▶ | use Sub::Quote qw(quote_sub); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||||||
| 32 | 75 | extends 'SQL::Translator::Schema::Object'; | |||||||||||||||||||||||||||
| 33 | |||||||||||||||||||||||||||||
| 34 | 75 | our $VERSION = '1.66'; | |||||||||||||||||||||||||||
| 35 | |||||||||||||||||||||||||||||
| 36 | =head2 new | ||||||||||||||||||||||||||||
| 37 | |||||||||||||||||||||||||||||
| 38 | Object constructor. | ||||||||||||||||||||||||||||
| 39 | |||||||||||||||||||||||||||||
| 40 | my $view = SQL::Translator::Schema::View->new; | ||||||||||||||||||||||||||||
| 41 | |||||||||||||||||||||||||||||
| 42 | =head2 fields | ||||||||||||||||||||||||||||
| 43 | |||||||||||||||||||||||||||||
| 44 | Gets and set the fields the constraint is on. Accepts a string, list or | ||||||||||||||||||||||||||||
| 45 | arrayref; returns an array or array reference. Will unique the field | ||||||||||||||||||||||||||||
| 46 | names and keep them in order by the first occurrence of a field name. | ||||||||||||||||||||||||||||
| 47 | |||||||||||||||||||||||||||||
| 48 | $view->fields('id'); | ||||||||||||||||||||||||||||
| 49 | $view->fields('id', 'name'); | ||||||||||||||||||||||||||||
| 50 | $view->fields( 'id, name' ); | ||||||||||||||||||||||||||||
| 51 | $view->fields( [ 'id', 'name' ] ); | ||||||||||||||||||||||||||||
| 52 | $view->fields( qw[ id name ] ); | ||||||||||||||||||||||||||||
| 53 | |||||||||||||||||||||||||||||
| 54 | my @fields = $view->fields; | ||||||||||||||||||||||||||||
| 55 | |||||||||||||||||||||||||||||
| 56 | =cut | ||||||||||||||||||||||||||||
| 57 | |||||||||||||||||||||||||||||
| 58 | 75 | with ListAttr fields => (uniq => 1); | |||||||||||||||||||||||||||
| 59 | |||||||||||||||||||||||||||||
| 60 | =head2 tables | ||||||||||||||||||||||||||||
| 61 | |||||||||||||||||||||||||||||
| 62 | Gets and set the tables the SELECT mentions. Accepts a string, list or | ||||||||||||||||||||||||||||
| 63 | arrayref; returns an array or array reference. Will unique the table | ||||||||||||||||||||||||||||
| 64 | names and keep them in order by the first occurrence of a field name. | ||||||||||||||||||||||||||||
| 65 | |||||||||||||||||||||||||||||
| 66 | $view->tables('foo'); | ||||||||||||||||||||||||||||
| 67 | $view->tables('foo', 'bar'); | ||||||||||||||||||||||||||||
| 68 | $view->tables( 'foo, bar' ); | ||||||||||||||||||||||||||||
| 69 | $view->tables( [ 'foo', 'bar' ] ); | ||||||||||||||||||||||||||||
| 70 | $view->tables( qw[ foo bar ] ); | ||||||||||||||||||||||||||||
| 71 | |||||||||||||||||||||||||||||
| 72 | my @tables = $view->tables; | ||||||||||||||||||||||||||||
| 73 | |||||||||||||||||||||||||||||
| 74 | =cut | ||||||||||||||||||||||||||||
| 75 | |||||||||||||||||||||||||||||
| 76 | 75 | with ListAttr tables => (uniq => 1); | |||||||||||||||||||||||||||
| 77 | |||||||||||||||||||||||||||||
| 78 | =head2 options | ||||||||||||||||||||||||||||
| 79 | |||||||||||||||||||||||||||||
| 80 | Gets or appends a list of options on the view. | ||||||||||||||||||||||||||||
| 81 | |||||||||||||||||||||||||||||
| 82 | $view->options('ALGORITHM=UNDEFINED'); | ||||||||||||||||||||||||||||
| 83 | |||||||||||||||||||||||||||||
| 84 | my @options = $view->options; | ||||||||||||||||||||||||||||
| 85 | |||||||||||||||||||||||||||||
| 86 | =cut | ||||||||||||||||||||||||||||
| 87 | |||||||||||||||||||||||||||||
| 88 | 75 | with ListAttr options => (uniq => 1, append => 1); | |||||||||||||||||||||||||||
| 89 | |||||||||||||||||||||||||||||
| 90 | 6 | ▶ | sub is_valid { | ||||||||||||||||||||||||||
|
Subroutine
is_valid: called CC=3 SCAR=11.4
Pod
documented
| |||||||||||||||||||||||||||||
| 91 | |||||||||||||||||||||||||||||
| 92 | =pod | ||||||||||||||||||||||||||||
| 93 | |||||||||||||||||||||||||||||
| 94 | =head2 is_valid | ||||||||||||||||||||||||||||
| 95 | |||||||||||||||||||||||||||||
| 96 | Determine whether the view is valid or not. | ||||||||||||||||||||||||||||
| 97 | |||||||||||||||||||||||||||||
| 98 | my $ok = $view->is_valid; | ||||||||||||||||||||||||||||
| 99 | |||||||||||||||||||||||||||||
| 100 | =cut | ||||||||||||||||||||||||||||
| 101 | |||||||||||||||||||||||||||||
| 102 | 6 | my $self = shift; | |||||||||||||||||||||||||||
| 103 | |||||||||||||||||||||||||||||
| 104 | 6 | ▶ | return $self->error('No name') unless $self->name; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 105 | 6 | ▶ | return $self->error('No sql') unless $self->sql; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 106 | |||||||||||||||||||||||||||||
| 107 | 6 | return 1; | |||||||||||||||||||||||||||
| 108 | } | ||||||||||||||||||||||||||||
| 109 | |||||||||||||||||||||||||||||
| 110 | =head2 name | ||||||||||||||||||||||||||||
| 111 | |||||||||||||||||||||||||||||
| 112 | Get or set the view's name. | ||||||||||||||||||||||||||||
| 113 | |||||||||||||||||||||||||||||
| 114 | my $name = $view->name('foo'); | ||||||||||||||||||||||||||||
| 115 | |||||||||||||||||||||||||||||
| 116 | =cut | ||||||||||||||||||||||||||||
| 117 | |||||||||||||||||||||||||||||
| 118 | 75 | has name => (is => 'rw', default => quote_sub(q{ '' })); | |||||||||||||||||||||||||||
| 119 | |||||||||||||||||||||||||||||
| 120 | =head2 order | ||||||||||||||||||||||||||||
| 121 | |||||||||||||||||||||||||||||
| 122 | Get or set the view's order. | ||||||||||||||||||||||||||||
| 123 | |||||||||||||||||||||||||||||
| 124 | my $order = $view->order(3); | ||||||||||||||||||||||||||||
| 125 | |||||||||||||||||||||||||||||
| 126 | =cut | ||||||||||||||||||||||||||||
| 127 | |||||||||||||||||||||||||||||
| 128 | 75 | has order => (is => 'rw', default => quote_sub(q{ 0 })); | |||||||||||||||||||||||||||
| 129 | |||||||||||||||||||||||||||||
| 130 | 101 | ▶ | around order => sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||
| 131 | 101 | my ($orig, $self, $arg) = @_; | |||||||||||||||||||||||||||
| 132 | |||||||||||||||||||||||||||||
| 133 | 101 | ▶ | if (defined $arg && $arg =~ /^\d+$/) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
defined $arg and $arg =~ /^\d+$/
MC/DC50%1/2
defined $arg and $arg =~ /^\d+$/
defined $arg
$arg =~ /^\d+$/
Truth table66%2/3
A and B
A= defined $argB= $arg =~ /^\d+$/
| |||||||||||||||||||||||||||||
| 134 | 43 | return $self->$orig($arg); | |||||||||||||||||||||||||||
| 135 | } | ||||||||||||||||||||||||||||
| 136 | |||||||||||||||||||||||||||||
| 137 | 58 | return $self->$orig; | |||||||||||||||||||||||||||
| 138 | 75 | }; | |||||||||||||||||||||||||||
| 139 | |||||||||||||||||||||||||||||
| 140 | =head2 sql | ||||||||||||||||||||||||||||
| 141 | |||||||||||||||||||||||||||||
| 142 | Get or set the view's SQL. | ||||||||||||||||||||||||||||
| 143 | |||||||||||||||||||||||||||||
| 144 | my $sql = $view->sql('select * from foo'); | ||||||||||||||||||||||||||||
| 145 | |||||||||||||||||||||||||||||
| 146 | =cut | ||||||||||||||||||||||||||||
| 147 | |||||||||||||||||||||||||||||
| 148 | 75 | has sql => (is => 'rw', default => quote_sub(q{ '' })); | |||||||||||||||||||||||||||
| 149 | |||||||||||||||||||||||||||||
| 150 | =head2 schema | ||||||||||||||||||||||||||||
| 151 | |||||||||||||||||||||||||||||
| 152 | Get or set the view's schema object. | ||||||||||||||||||||||||||||
| 153 | |||||||||||||||||||||||||||||
| 154 | $view->schema( $schema ); | ||||||||||||||||||||||||||||
| 155 | my $schema = $view->schema; | ||||||||||||||||||||||||||||
| 156 | |||||||||||||||||||||||||||||
| 157 | =cut | ||||||||||||||||||||||||||||
| 158 | |||||||||||||||||||||||||||||
| 159 | 75 | has schema => (is => 'rw', isa => schema_obj('Schema'), weak_ref => 1); | |||||||||||||||||||||||||||
| 160 | |||||||||||||||||||||||||||||
| 161 | 75 | around schema => \&ex2err; | |||||||||||||||||||||||||||
| 162 | |||||||||||||||||||||||||||||
| 163 | =head2 equals | ||||||||||||||||||||||||||||
| 164 | |||||||||||||||||||||||||||||
| 165 | Determines if this view is the same as another | ||||||||||||||||||||||||||||
| 166 | |||||||||||||||||||||||||||||
| 167 | my $isIdentical = $view1->equals( $view2 ); | ||||||||||||||||||||||||||||
| 168 | |||||||||||||||||||||||||||||
| 169 | =cut | ||||||||||||||||||||||||||||
| 170 | |||||||||||||||||||||||||||||
| 171 | 0 | ▶ | around equals => sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=9 SCAR=45.0
| |||||||||||||||||||||||||||||
| 172 | 0 | my $orig = shift; | |||||||||||||||||||||||||||
| 173 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 174 | 0 | my $other = shift; | |||||||||||||||||||||||||||
| 175 | 0 | my $case_insensitive = shift; | |||||||||||||||||||||||||||
| 176 | 0 | my $ignore_sql = shift; | |||||||||||||||||||||||||||
| 177 | |||||||||||||||||||||||||||||
| 178 | 0 | ▶ | return 0 unless $self->$orig($other); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 179 | 0 | ▶ | return 0 | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 180 | unless $case_insensitive | ||||||||||||||||||||||||||||
| 181 | ? uc($self->name) eq uc($other->name) | ||||||||||||||||||||||||||||
| 182 | : $self->name eq $other->name; | ||||||||||||||||||||||||||||
| 183 | |||||||||||||||||||||||||||||
| 184 | #return 0 unless $self->is_valid eq $other->is_valid; | ||||||||||||||||||||||||||||
| 185 | |||||||||||||||||||||||||||||
| 186 | 0 | ▶ | unless ($ignore_sql) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 187 | 0 | my $selfSql = $self->sql; | |||||||||||||||||||||||||||
| 188 | 0 | my $otherSql = $other->sql; | |||||||||||||||||||||||||||
| 189 | |||||||||||||||||||||||||||||
| 190 | # Remove comments | ||||||||||||||||||||||||||||
| 191 | 0 | $selfSql =~ s/--.*$//mg; | |||||||||||||||||||||||||||
| 192 | 0 | $otherSql =~ s/--.*$//mg; | |||||||||||||||||||||||||||
| 193 | |||||||||||||||||||||||||||||
| 194 | # Collapse whitespace to space to avoid whitespace comparison issues | ||||||||||||||||||||||||||||
| 195 | 0 | $selfSql =~ s/\s+/ /sg; | |||||||||||||||||||||||||||
| 196 | 0 | $otherSql =~ s/\s+/ /sg; | |||||||||||||||||||||||||||
| 197 | 0 | ▶ | return 0 unless $selfSql eq $otherSql; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 198 | } | ||||||||||||||||||||||||||||
| 199 | |||||||||||||||||||||||||||||
| 200 | 0 | my $selfFields = join(":", $self->fields); | |||||||||||||||||||||||||||
| 201 | 0 | my $otherFields = join(":", $other->fields); | |||||||||||||||||||||||||||
| 202 | 0 | ▶ | return 0 | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 203 | unless $case_insensitive | ||||||||||||||||||||||||||||
| 204 | ? uc($selfFields) eq uc($otherFields) | ||||||||||||||||||||||||||||
| 205 | : $selfFields eq $otherFields; | ||||||||||||||||||||||||||||
| 206 | 0 | ▶ | return 0 | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 207 | unless $self->_compare_objects(scalar $self->extra, scalar $other->extra); | ||||||||||||||||||||||||||||
| 208 | 0 | return 1; | |||||||||||||||||||||||||||
| 209 | 75 | }; | |||||||||||||||||||||||||||
| 210 | |||||||||||||||||||||||||||||
| 211 | # Must come after all 'has' declarations | ||||||||||||||||||||||||||||
| 212 | 75 | around new => \&ex2err; | |||||||||||||||||||||||||||
| 213 | |||||||||||||||||||||||||||||
| 214 | 75 | 1; | |||||||||||||||||||||||||||
| 215 | |||||||||||||||||||||||||||||
| 216 | =pod | ||||||||||||||||||||||||||||
| 217 | |||||||||||||||||||||||||||||
| 218 | =head1 AUTHOR | ||||||||||||||||||||||||||||
| 219 | |||||||||||||||||||||||||||||
| 220 | Ken Youens-Clark E<lt>kclark@cpan.orgE<gt>. | ||||||||||||||||||||||||||||
| 221 | |||||||||||||||||||||||||||||
| 222 | =cut | ||||||||||||||||||||||||||||