| 1 | package SQL::Translator::Schema::Table; | ||||||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||||||
| 3 | =pod | ||||||||||||||||||||||||||||||||
| 4 | |||||||||||||||||||||||||||||||||
| 5 | =head1 NAME | ||||||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||||||
| 7 | SQL::Translator::Schema::Table - SQL::Translator table object | ||||||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||||||
| 9 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||||||
| 11 | use SQL::Translator::Schema::Table; | ||||||||||||||||||||||||||||||||
| 12 | my $table = SQL::Translator::Schema::Table->new( name => 'foo' ); | ||||||||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||||||
| 14 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||||||
| 16 | C<SQL::Translator::Schema::Table> is the table object. | ||||||||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||||||
| 18 | =head1 METHODS | ||||||||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||||||
| 20 | =cut | ||||||||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||||||
| 22 | 75 | ▶ | use Moo; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 23 | 75 | ▶ | use SQL::Translator::Utils qw(parse_list_arg ex2err throw carp_ro); | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 24 | 75 | ▶ | use SQL::Translator::Types qw(schema_obj); | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 25 | 75 | ▶ | use SQL::Translator::Role::ListAttr; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 26 | 75 | ▶ | use SQL::Translator::Schema::Constants; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 27 | 75 | ▶ | use SQL::Translator::Schema::Constraint; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 28 | 75 | ▶ | use SQL::Translator::Schema::Field; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 29 | 75 | ▶ | use SQL::Translator::Schema::Index; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 30 | |||||||||||||||||||||||||||||||||
| 31 | 75 | ▶ | use Carp::Clan '^SQL::Translator'; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 32 | 75 | ▶ | use List::Util 'max'; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 33 | 75 | ▶ | use Sub::Quote qw(quote_sub); | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 34 | |||||||||||||||||||||||||||||||||
| 35 | 75 | extends 'SQL::Translator::Schema::Object'; | |||||||||||||||||||||||||||||||
| 36 | |||||||||||||||||||||||||||||||||
| 37 | 75 | our $VERSION = '1.63'; | |||||||||||||||||||||||||||||||
| 38 | |||||||||||||||||||||||||||||||||
| 39 | # Stringify to our name, being careful not to pass any args through so we don't | ||||||||||||||||||||||||||||||||
| 40 | # accidentally set it to undef. We also have to tweak bool so the object is | ||||||||||||||||||||||||||||||||
| 41 | # still true when it doesn't have a name (which shouldn't happen!). | ||||||||||||||||||||||||||||||||
| 42 | use overload | ||||||||||||||||||||||||||||||||
| 43 | 474 | ▶ | '""' => sub { shift->name }, | ||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 44 | 7041 | ▶ | 'bool' => sub { $_[0]->name || $_[0] }, | ||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=7.6
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 45 | 75 | fallback => 1, | |||||||||||||||||||||||||||||||
| 46 | 75 | ▶ | ; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 47 | |||||||||||||||||||||||||||||||||
| 48 | =pod | ||||||||||||||||||||||||||||||||
| 49 | |||||||||||||||||||||||||||||||||
| 50 | =head2 new | ||||||||||||||||||||||||||||||||
| 51 | |||||||||||||||||||||||||||||||||
| 52 | Object constructor. | ||||||||||||||||||||||||||||||||
| 53 | |||||||||||||||||||||||||||||||||
| 54 | my $table = SQL::Translator::Schema::Table->new( | ||||||||||||||||||||||||||||||||
| 55 | schema => $schema, | ||||||||||||||||||||||||||||||||
| 56 | name => 'foo', | ||||||||||||||||||||||||||||||||
| 57 | ); | ||||||||||||||||||||||||||||||||
| 58 | |||||||||||||||||||||||||||||||||
| 59 | =head2 add_constraint | ||||||||||||||||||||||||||||||||
| 60 | |||||||||||||||||||||||||||||||||
| 61 | Add a constraint to the table. Returns the newly created | ||||||||||||||||||||||||||||||||
| 62 | C<SQL::Translator::Schema::Constraint> object. | ||||||||||||||||||||||||||||||||
| 63 | |||||||||||||||||||||||||||||||||
| 64 | my $c1 = $table->add_constraint( | ||||||||||||||||||||||||||||||||
| 65 | name => 'pk', | ||||||||||||||||||||||||||||||||
| 66 | type => PRIMARY_KEY, | ||||||||||||||||||||||||||||||||
| 67 | fields => [ 'foo_id' ], | ||||||||||||||||||||||||||||||||
| 68 | ); | ||||||||||||||||||||||||||||||||
| 69 | |||||||||||||||||||||||||||||||||
| 70 | my $c2 = SQL::Translator::Schema::Constraint->new( name => 'uniq' ); | ||||||||||||||||||||||||||||||||
| 71 | $c2 = $table->add_constraint( $constraint ); | ||||||||||||||||||||||||||||||||
| 72 | |||||||||||||||||||||||||||||||||
| 73 | =cut | ||||||||||||||||||||||||||||||||
| 74 | |||||||||||||||||||||||||||||||||
| 75 | 75 | has _constraints => ( | |||||||||||||||||||||||||||||||
| 76 | is => 'ro', | ||||||||||||||||||||||||||||||||
| 77 | init_arg => undef, | ||||||||||||||||||||||||||||||||
| 78 | default => quote_sub(q{ +[] }), | ||||||||||||||||||||||||||||||||
| 79 | predicate => 1, | ||||||||||||||||||||||||||||||||
| 80 | lazy => 1, | ||||||||||||||||||||||||||||||||
| 81 | ); | ||||||||||||||||||||||||||||||||
| 82 | |||||||||||||||||||||||||||||||||
| 83 | sub add_constraint { | ||||||||||||||||||||||||||||||||
| 84 | 608 | ▶ | my $self = shift; | ||||||||||||||||||||||||||||||
|
Subroutine
add_constraint: called CC=11 SCAR=24.0
Pod
documented
| |||||||||||||||||||||||||||||||||
| 85 | 608 | my $constraint_class = 'SQL::Translator::Schema::Constraint'; | |||||||||||||||||||||||||||||||
| 86 | 608 | my $constraint; | |||||||||||||||||||||||||||||||
| 87 | |||||||||||||||||||||||||||||||||
| 88 | 608 | ▶ | if ( UNIVERSAL::isa( $_[0], $constraint_class ) ) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 89 | 3 | $constraint = shift; | |||||||||||||||||||||||||||||||
| 90 | 3 | $constraint->table( $self ); | |||||||||||||||||||||||||||||||
| 91 | } | ||||||||||||||||||||||||||||||||
| 92 | else { | ||||||||||||||||||||||||||||||||
| 93 | 605 | my %args = @_; | |||||||||||||||||||||||||||||||
| 94 | 605 | $args{'table'} = $self; | |||||||||||||||||||||||||||||||
| 95 | 605 | ▶ | $constraint = $constraint_class->new( \%args ) or | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 96 | return $self->error( $constraint_class->error ); | ||||||||||||||||||||||||||||||||
| 97 | } | ||||||||||||||||||||||||||||||||
| 98 | |||||||||||||||||||||||||||||||||
| 99 | # | ||||||||||||||||||||||||||||||||
| 100 | # If we're trying to add a PK when one is already defined, | ||||||||||||||||||||||||||||||||
| 101 | # then just add the fields to the existing definition. | ||||||||||||||||||||||||||||||||
| 102 | # | ||||||||||||||||||||||||||||||||
| 103 | 608 | my $ok = 1; | |||||||||||||||||||||||||||||||
| 104 | 608 | my $pk = $self->primary_key; | |||||||||||||||||||||||||||||||
| 105 | 608 | ▶ | if ( $pk && $constraint->type eq PRIMARY_KEY ) { | ||||||||||||||||||||||||||||||
|
Branch: 2 branches
Condition100%3/3
$pk and $constraint->type eq "PRIMARY KEY"
MC/DC100%2/2
$pk and $constraint->type eq "PRIMARY KEY"
$pk
$constraint->type eq "PRIMARY KEY"
Truth table100%3/3
A and B
A= $pkB= $constraint->type eq "PRIMARY KEY"
| |||||||||||||||||||||||||||||||||
| 106 | 79 | $self->primary_key( $constraint->fields ); | |||||||||||||||||||||||||||||||
| 107 | 79 | ▶ | $pk->name($constraint->name) if $constraint->name; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 108 | 79 | my %extra = $constraint->extra; | |||||||||||||||||||||||||||||||
| 109 | 79 | ▶ | $pk->extra(%extra) if keys %extra; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 110 | 79 | $constraint = $pk; | |||||||||||||||||||||||||||||||
| 111 | 79 | $ok = 0; | |||||||||||||||||||||||||||||||
| 112 | } | ||||||||||||||||||||||||||||||||
| 113 | elsif ( $constraint->type eq PRIMARY_KEY ) { | ||||||||||||||||||||||||||||||||
| 114 | 234 | for my $fname ( $constraint->fields ) { | |||||||||||||||||||||||||||||||
| 115 | 266 | ▶ | if ( my $f = $self->get_field( $fname ) ) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 116 | 266 | $f->is_primary_key( 1 ); | |||||||||||||||||||||||||||||||
| 117 | } | ||||||||||||||||||||||||||||||||
| 118 | } | ||||||||||||||||||||||||||||||||
| 119 | } | ||||||||||||||||||||||||||||||||
| 120 | # | ||||||||||||||||||||||||||||||||
| 121 | # See if another constraint of the same type | ||||||||||||||||||||||||||||||||
| 122 | # covers the same fields. -- This doesn't work! ky | ||||||||||||||||||||||||||||||||
| 123 | # | ||||||||||||||||||||||||||||||||
| 124 | # elsif ( $constraint->type ne CHECK_C ) { | ||||||||||||||||||||||||||||||||
| 125 | # my @field_names = $constraint->fields; | ||||||||||||||||||||||||||||||||
| 126 | # for my $c ( | ||||||||||||||||||||||||||||||||
| 127 | # grep { $_->type eq $constraint->type } | ||||||||||||||||||||||||||||||||
| 128 | # $self->get_constraints | ||||||||||||||||||||||||||||||||
| 129 | # ) { | ||||||||||||||||||||||||||||||||
| 130 | # my %fields = map { $_, 1 } $c->fields; | ||||||||||||||||||||||||||||||||
| 131 | # for my $field_name ( @field_names ) { | ||||||||||||||||||||||||||||||||
| 132 | # if ( $fields{ $field_name } ) { | ||||||||||||||||||||||||||||||||
| 133 | # $constraint = $c; | ||||||||||||||||||||||||||||||||
| 134 | # $ok = 0; | ||||||||||||||||||||||||||||||||
| 135 | # last; | ||||||||||||||||||||||||||||||||
| 136 | # } | ||||||||||||||||||||||||||||||||
| 137 | # } | ||||||||||||||||||||||||||||||||
| 138 | # last unless $ok; | ||||||||||||||||||||||||||||||||
| 139 | # } | ||||||||||||||||||||||||||||||||
| 140 | # } | ||||||||||||||||||||||||||||||||
| 141 | |||||||||||||||||||||||||||||||||
| 142 | 608 | ▶ | if ( $ok ) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 143 | 529 | push @{ $self->_constraints }, $constraint; | |||||||||||||||||||||||||||||||
| 144 | } | ||||||||||||||||||||||||||||||||
| 145 | |||||||||||||||||||||||||||||||||
| 146 | 608 | return $constraint; | |||||||||||||||||||||||||||||||
| 147 | } | ||||||||||||||||||||||||||||||||
| 148 | |||||||||||||||||||||||||||||||||
| 149 | =head2 drop_constraint | ||||||||||||||||||||||||||||||||
| 150 | |||||||||||||||||||||||||||||||||
| 151 | Remove a constraint from the table. Returns the constraint object if the index | ||||||||||||||||||||||||||||||||
| 152 | was found and removed, an error otherwise. The single parameter can be either | ||||||||||||||||||||||||||||||||
| 153 | an index name or an C<SQL::Translator::Schema::Constraint> object. | ||||||||||||||||||||||||||||||||
| 154 | |||||||||||||||||||||||||||||||||
| 155 | $table->drop_constraint('myconstraint'); | ||||||||||||||||||||||||||||||||
| 156 | |||||||||||||||||||||||||||||||||
| 157 | =cut | ||||||||||||||||||||||||||||||||
| 158 | |||||||||||||||||||||||||||||||||
| 159 | sub drop_constraint { | ||||||||||||||||||||||||||||||||
| 160 | 3 | ▶ | my $self = shift; | ||||||||||||||||||||||||||||||
|
Subroutine
drop_constraint: called CC=4 SCAR=13.9
Pod
documented
| |||||||||||||||||||||||||||||||||
| 161 | 3 | my $constraint_class = 'SQL::Translator::Schema::Constraint'; | |||||||||||||||||||||||||||||||
| 162 | 3 | my $constraint_name; | |||||||||||||||||||||||||||||||
| 163 | |||||||||||||||||||||||||||||||||
| 164 | 3 | ▶ | if ( UNIVERSAL::isa( $_[0], $constraint_class ) ) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 165 | 1 | $constraint_name = shift->name; | |||||||||||||||||||||||||||||||
| 166 | } | ||||||||||||||||||||||||||||||||
| 167 | else { | ||||||||||||||||||||||||||||||||
| 168 | 2 | $constraint_name = shift; | |||||||||||||||||||||||||||||||
| 169 | } | ||||||||||||||||||||||||||||||||
| 170 | |||||||||||||||||||||||||||||||||
| 171 | 3 | ▶ | if ( ! ($self->_has_constraints && grep { $_->name eq $constraint_name } @ { $self->_constraints }) ) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$self->_has_constraints and grep {$_->name eq $constraint_name;} @{$self->_constraints;}
MC/DC50%1/2
$self->_has_constraints and grep {$_->name eq $constraint_name;} @{$self->_constraints;}
$self->_has_constraints
grep {$_->name eq $constraint_name;} @{$self->_constraints;}
Truth table66%2/3
A and B
A= $self->_has_constraintsB= grep {$_->name eq $constraint_name;} @{$self->_constraints;}
| |||||||||||||||||||||||||||||||||
| 172 | 1 | return $self->error(qq[Can't drop constraint: "$constraint_name" doesn't exist]); | |||||||||||||||||||||||||||||||
| 173 | } | ||||||||||||||||||||||||||||||||
| 174 | |||||||||||||||||||||||||||||||||
| 175 | 2 | my @cs = @{ $self->_constraints }; | |||||||||||||||||||||||||||||||
| 176 | 2 | my ($constraint_id) = grep { $cs[$_]->name eq $constraint_name } (0..$#cs); | |||||||||||||||||||||||||||||||
| 177 | 2 | my $constraint = splice(@{$self->_constraints}, $constraint_id, 1); | |||||||||||||||||||||||||||||||
| 178 | |||||||||||||||||||||||||||||||||
| 179 | 2 | return $constraint; | |||||||||||||||||||||||||||||||
| 180 | } | ||||||||||||||||||||||||||||||||
| 181 | |||||||||||||||||||||||||||||||||
| 182 | =head2 add_index | ||||||||||||||||||||||||||||||||
| 183 | |||||||||||||||||||||||||||||||||
| 184 | Add an index to the table. Returns the newly created | ||||||||||||||||||||||||||||||||
| 185 | C<SQL::Translator::Schema::Index> object. | ||||||||||||||||||||||||||||||||
| 186 | |||||||||||||||||||||||||||||||||
| 187 | my $i1 = $table->add_index( | ||||||||||||||||||||||||||||||||
| 188 | name => 'name', | ||||||||||||||||||||||||||||||||
| 189 | fields => [ 'name' ], | ||||||||||||||||||||||||||||||||
| 190 | type => 'normal', | ||||||||||||||||||||||||||||||||
| 191 | ); | ||||||||||||||||||||||||||||||||
| 192 | |||||||||||||||||||||||||||||||||
| 193 | my $i2 = SQL::Translator::Schema::Index->new( name => 'id' ); | ||||||||||||||||||||||||||||||||
| 194 | $i2 = $table->add_index( $index ); | ||||||||||||||||||||||||||||||||
| 195 | |||||||||||||||||||||||||||||||||
| 196 | =cut | ||||||||||||||||||||||||||||||||
| 197 | |||||||||||||||||||||||||||||||||
| 198 | 75 | has _indices => ( | |||||||||||||||||||||||||||||||
| 199 | is => 'ro', | ||||||||||||||||||||||||||||||||
| 200 | init_arg => undef, | ||||||||||||||||||||||||||||||||
| 201 | default => quote_sub(q{ [] }), | ||||||||||||||||||||||||||||||||
| 202 | predicate => 1, | ||||||||||||||||||||||||||||||||
| 203 | lazy => 1, | ||||||||||||||||||||||||||||||||
| 204 | ); | ||||||||||||||||||||||||||||||||
| 205 | |||||||||||||||||||||||||||||||||
| 206 | sub add_index { | ||||||||||||||||||||||||||||||||
| 207 | 144 | ▶ | my $self = shift; | ||||||||||||||||||||||||||||||
|
Subroutine
add_index: called CC=5 SCAR=16.1
Pod
documented
| |||||||||||||||||||||||||||||||||
| 208 | 144 | my $index_class = 'SQL::Translator::Schema::Index'; | |||||||||||||||||||||||||||||||
| 209 | 144 | my $index; | |||||||||||||||||||||||||||||||
| 210 | |||||||||||||||||||||||||||||||||
| 211 | 144 | ▶ | if ( UNIVERSAL::isa( $_[0], $index_class ) ) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 212 | 3 | $index = shift; | |||||||||||||||||||||||||||||||
| 213 | 3 | $index->table( $self ); | |||||||||||||||||||||||||||||||
| 214 | } | ||||||||||||||||||||||||||||||||
| 215 | else { | ||||||||||||||||||||||||||||||||
| 216 | 141 | my %args = @_; | |||||||||||||||||||||||||||||||
| 217 | 141 | $args{'table'} = $self; | |||||||||||||||||||||||||||||||
| 218 | 141 | ▶ | $index = $index_class->new( \%args ) or return | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 219 | $self->error( $index_class->error ); | ||||||||||||||||||||||||||||||||
| 220 | } | ||||||||||||||||||||||||||||||||
| 221 | 144 | foreach my $ex_index ($self->get_indices) { | |||||||||||||||||||||||||||||||
| 222 | 69 | ▶ | return if ($ex_index->equals($index)); | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 223 | } | ||||||||||||||||||||||||||||||||
| 224 | 144 | push @{ $self->_indices }, $index; | |||||||||||||||||||||||||||||||
| 225 | 144 | return $index; | |||||||||||||||||||||||||||||||
| 226 | } | ||||||||||||||||||||||||||||||||
| 227 | |||||||||||||||||||||||||||||||||
| 228 | =head2 drop_index | ||||||||||||||||||||||||||||||||
| 229 | |||||||||||||||||||||||||||||||||
| 230 | Remove an index from the table. Returns the index object if the index was | ||||||||||||||||||||||||||||||||
| 231 | found and removed, an error otherwise. The single parameter can be either | ||||||||||||||||||||||||||||||||
| 232 | an index name of an C<SQL::Translator::Schema::Index> object. | ||||||||||||||||||||||||||||||||
| 233 | |||||||||||||||||||||||||||||||||
| 234 | $table->drop_index('myindex'); | ||||||||||||||||||||||||||||||||
| 235 | |||||||||||||||||||||||||||||||||
| 236 | =cut | ||||||||||||||||||||||||||||||||
| 237 | |||||||||||||||||||||||||||||||||
| 238 | sub drop_index { | ||||||||||||||||||||||||||||||||
| 239 | 3 | ▶ | my $self = shift; | ||||||||||||||||||||||||||||||
|
Subroutine
drop_index: called CC=4 SCAR=13.9
Pod
documented
| |||||||||||||||||||||||||||||||||
| 240 | 3 | my $index_class = 'SQL::Translator::Schema::Index'; | |||||||||||||||||||||||||||||||
| 241 | 3 | my $index_name; | |||||||||||||||||||||||||||||||
| 242 | |||||||||||||||||||||||||||||||||
| 243 | 3 | ▶ | if ( UNIVERSAL::isa( $_[0], $index_class ) ) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 244 | 1 | $index_name = shift->name; | |||||||||||||||||||||||||||||||
| 245 | } | ||||||||||||||||||||||||||||||||
| 246 | else { | ||||||||||||||||||||||||||||||||
| 247 | 2 | $index_name = shift; | |||||||||||||||||||||||||||||||
| 248 | } | ||||||||||||||||||||||||||||||||
| 249 | |||||||||||||||||||||||||||||||||
| 250 | 3 | ▶ | if ( ! ($self->_has_indices && grep { $_->name eq $index_name } @{ $self->_indices }) ) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$self->_has_indices and grep {$_->name eq $index_name;} @{$self->_indices;}
MC/DC50%1/2
$self->_has_indices and grep {$_->name eq $index_name;} @{$self->_indices;}
$self->_has_indices
grep {$_->name eq $index_name;} @{$self->_indices;}
Truth table66%2/3
A and B
A= $self->_has_indicesB= grep {$_->name eq $index_name;} @{$self->_indices;}
| |||||||||||||||||||||||||||||||||
| 251 | 1 | return $self->error(qq[Can't drop index: "$index_name" doesn't exist]); | |||||||||||||||||||||||||||||||
| 252 | } | ||||||||||||||||||||||||||||||||
| 253 | |||||||||||||||||||||||||||||||||
| 254 | 2 | my @is = @{ $self->_indices }; | |||||||||||||||||||||||||||||||
| 255 | 2 | my ($index_id) = grep { $is[$_]->name eq $index_name } (0..$#is); | |||||||||||||||||||||||||||||||
| 256 | 2 | my $index = splice(@{$self->_indices}, $index_id, 1); | |||||||||||||||||||||||||||||||
| 257 | |||||||||||||||||||||||||||||||||
| 258 | 2 | return $index; | |||||||||||||||||||||||||||||||
| 259 | } | ||||||||||||||||||||||||||||||||
| 260 | |||||||||||||||||||||||||||||||||
| 261 | =head2 add_field | ||||||||||||||||||||||||||||||||
| 262 | |||||||||||||||||||||||||||||||||
| 263 | Add an field to the table. Returns the newly created | ||||||||||||||||||||||||||||||||
| 264 | C<SQL::Translator::Schema::Field> object. The "name" parameter is | ||||||||||||||||||||||||||||||||
| 265 | required. If you try to create a field with the same name as an | ||||||||||||||||||||||||||||||||
| 266 | existing field, you will get an error and the field will not be created. | ||||||||||||||||||||||||||||||||
| 267 | |||||||||||||||||||||||||||||||||
| 268 | my $f1 = $table->add_field( | ||||||||||||||||||||||||||||||||
| 269 | name => 'foo_id', | ||||||||||||||||||||||||||||||||
| 270 | data_type => 'integer', | ||||||||||||||||||||||||||||||||
| 271 | size => 11, | ||||||||||||||||||||||||||||||||
| 272 | ); | ||||||||||||||||||||||||||||||||
| 273 | |||||||||||||||||||||||||||||||||
| 274 | my $f2 = SQL::Translator::Schema::Field->new( | ||||||||||||||||||||||||||||||||
| 275 | name => 'name', | ||||||||||||||||||||||||||||||||
| 276 | table => $table, | ||||||||||||||||||||||||||||||||
| 277 | ); | ||||||||||||||||||||||||||||||||
| 278 | $f2 = $table->add_field( $field2 ) or die $table->error; | ||||||||||||||||||||||||||||||||
| 279 | |||||||||||||||||||||||||||||||||
| 280 | =cut | ||||||||||||||||||||||||||||||||
| 281 | |||||||||||||||||||||||||||||||||
| 282 | 75 | has _fields => ( | |||||||||||||||||||||||||||||||
| 283 | is => 'ro', | ||||||||||||||||||||||||||||||||
| 284 | init_arg => undef, | ||||||||||||||||||||||||||||||||
| 285 | default => quote_sub(q{ +{} }), | ||||||||||||||||||||||||||||||||
| 286 | predicate => 1, | ||||||||||||||||||||||||||||||||
| 287 | lazy => 1 | ||||||||||||||||||||||||||||||||
| 288 | ); | ||||||||||||||||||||||||||||||||
| 289 | |||||||||||||||||||||||||||||||||
| 290 | sub add_field { | ||||||||||||||||||||||||||||||||
| 291 | 1549 | ▶ | my $self = shift; | ||||||||||||||||||||||||||||||
|
Subroutine
add_field: called CC=8 SCAR=20.8
Pod
documented
| |||||||||||||||||||||||||||||||||
| 292 | 1549 | my $field_class = 'SQL::Translator::Schema::Field'; | |||||||||||||||||||||||||||||||
| 293 | 1549 | my $field; | |||||||||||||||||||||||||||||||
| 294 | |||||||||||||||||||||||||||||||||
| 295 | 1549 | ▶ | if ( UNIVERSAL::isa( $_[0], $field_class ) ) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 296 | 7 | $field = shift; | |||||||||||||||||||||||||||||||
| 297 | 7 | $field->table( $self ); | |||||||||||||||||||||||||||||||
| 298 | } | ||||||||||||||||||||||||||||||||
| 299 | else { | ||||||||||||||||||||||||||||||||
| 300 | 1542 | my %args = @_; | |||||||||||||||||||||||||||||||
| 301 | 1542 | $args{'table'} = $self; | |||||||||||||||||||||||||||||||
| 302 | 1542 | ▶ | $field = $field_class->new( \%args ) or return | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 303 | $self->error( $field_class->error ); | ||||||||||||||||||||||||||||||||
| 304 | } | ||||||||||||||||||||||||||||||||
| 305 | |||||||||||||||||||||||||||||||||
| 306 | 1548 | my $existing_order = { map { $_->order => $_->name } $self->get_fields }; | |||||||||||||||||||||||||||||||
| 307 | |||||||||||||||||||||||||||||||||
| 308 | # supplied order, possible unordered assembly | ||||||||||||||||||||||||||||||||
| 309 | 1548 | ▶ | if ( $field->order ) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 310 | 199 | ▶ | if($existing_order->{$field->order}) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 311 | croak sprintf | ||||||||||||||||||||||||||||||||
| 312 | "Requested order '%d' for column '%s' conflicts with already existing column '%s'", | ||||||||||||||||||||||||||||||||
| 313 | $field->order, | ||||||||||||||||||||||||||||||||
| 314 | $field->name, | ||||||||||||||||||||||||||||||||
| 315 | 1 | $existing_order->{$field->order}, | |||||||||||||||||||||||||||||||
| 316 | ; | ||||||||||||||||||||||||||||||||
| 317 | } | ||||||||||||||||||||||||||||||||
| 318 | } | ||||||||||||||||||||||||||||||||
| 319 | else { | ||||||||||||||||||||||||||||||||
| 320 | 1349 | ▶ | my $last_field_no = max(keys %$existing_order) || 0; | ||||||||||||||||||||||||||||||
|
Condition100%2/2
&max(keys %$existing_order) || 0
MC/DC100%1/1
&max(keys %$existing_order) || 0
&max(keys %$existing_order)
Truth table100%2/2
A
A= &max(keys %$existing_order)
| |||||||||||||||||||||||||||||||||
| 321 | 1349 | ▶ | if ( $last_field_no != scalar keys %$existing_order ) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 322 | 1 | croak sprintf | |||||||||||||||||||||||||||||||
| 323 | "Table '%s' field order incomplete - unable to auto-determine order for newly added field", | ||||||||||||||||||||||||||||||||
| 324 | $self->name, | ||||||||||||||||||||||||||||||||
| 325 | ; | ||||||||||||||||||||||||||||||||
| 326 | } | ||||||||||||||||||||||||||||||||
| 327 | |||||||||||||||||||||||||||||||||
| 328 | 1348 | $field->order( $last_field_no + 1 ); | |||||||||||||||||||||||||||||||
| 329 | } | ||||||||||||||||||||||||||||||||
| 330 | |||||||||||||||||||||||||||||||||
| 331 | # We know we have a name as the Field->new above errors if none given. | ||||||||||||||||||||||||||||||||
| 332 | 1546 | my $field_name = $field->name; | |||||||||||||||||||||||||||||||
| 333 | |||||||||||||||||||||||||||||||||
| 334 | 1546 | ▶ | if ( $self->get_field($field_name) ) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 335 | 2 | return $self->error(qq[Can't use field name "$field_name": field exists]); | |||||||||||||||||||||||||||||||
| 336 | } | ||||||||||||||||||||||||||||||||
| 337 | else { | ||||||||||||||||||||||||||||||||
| 338 | 1544 | $self->_fields->{ $field_name } = $field; | |||||||||||||||||||||||||||||||
| 339 | } | ||||||||||||||||||||||||||||||||
| 340 | |||||||||||||||||||||||||||||||||
| 341 | 1544 | return $field; | |||||||||||||||||||||||||||||||
| 342 | } | ||||||||||||||||||||||||||||||||
| 343 | |||||||||||||||||||||||||||||||||
| 344 | =head2 drop_field | ||||||||||||||||||||||||||||||||
| 345 | |||||||||||||||||||||||||||||||||
| 346 | Remove a field from the table. Returns the field object if the field was | ||||||||||||||||||||||||||||||||
| 347 | found and removed, an error otherwise. The single parameter can be either | ||||||||||||||||||||||||||||||||
| 348 | a field name or an C<SQL::Translator::Schema::Field> object. | ||||||||||||||||||||||||||||||||
| 349 | |||||||||||||||||||||||||||||||||
| 350 | $table->drop_field('myfield'); | ||||||||||||||||||||||||||||||||
| 351 | |||||||||||||||||||||||||||||||||
| 352 | =cut | ||||||||||||||||||||||||||||||||
| 353 | |||||||||||||||||||||||||||||||||
| 354 | sub drop_field { | ||||||||||||||||||||||||||||||||
| 355 | 3 | ▶ | my $self = shift; | ||||||||||||||||||||||||||||||
|
Subroutine
drop_field: called CC=7 SCAR=21.4
Pod
documented
| |||||||||||||||||||||||||||||||||
| 356 | 3 | my $field_class = 'SQL::Translator::Schema::Field'; | |||||||||||||||||||||||||||||||
| 357 | 3 | my $field_name; | |||||||||||||||||||||||||||||||
| 358 | |||||||||||||||||||||||||||||||||
| 359 | 3 | ▶ | if ( UNIVERSAL::isa( $_[0], $field_class ) ) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 360 | 1 | $field_name = shift->name; | |||||||||||||||||||||||||||||||
| 361 | } | ||||||||||||||||||||||||||||||||
| 362 | else { | ||||||||||||||||||||||||||||||||
| 363 | 2 | $field_name = shift; | |||||||||||||||||||||||||||||||
| 364 | } | ||||||||||||||||||||||||||||||||
| 365 | 3 | my %args = @_; | |||||||||||||||||||||||||||||||
| 366 | 3 | my $cascade = $args{'cascade'}; | |||||||||||||||||||||||||||||||
| 367 | |||||||||||||||||||||||||||||||||
| 368 | 3 | ▶ | if ( ! ($self->_has_fields && exists $self->_fields->{ $field_name } ) ) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$self->_has_fields and exists $self->_fields->{$field_name}
MC/DC50%1/2
$self->_has_fields and exists $self->_fields->{$field_name}
$self->_has_fields
exists $self->_fields->{$field_name}
Truth table66%2/3
A and B
A= $self->_has_fieldsB= exists $self->_fields->{$field_name}
| |||||||||||||||||||||||||||||||||
| 369 | 1 | return $self->error(qq[Can't drop field: "$field_name" doesn't exists]); | |||||||||||||||||||||||||||||||
| 370 | } | ||||||||||||||||||||||||||||||||
| 371 | |||||||||||||||||||||||||||||||||
| 372 | 2 | my $field = delete $self->_fields->{ $field_name }; | |||||||||||||||||||||||||||||||
| 373 | |||||||||||||||||||||||||||||||||
| 374 | 2 | ▶ | if ( $cascade ) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 375 | # Remove this field from all indices using it | ||||||||||||||||||||||||||||||||
| 376 | 2 | foreach my $i ($self->get_indices()) { | |||||||||||||||||||||||||||||||
| 377 | 0 | my @fs = $i->fields(); | |||||||||||||||||||||||||||||||
| 378 | 0 | @fs = grep { $_ ne $field->name } @fs; | |||||||||||||||||||||||||||||||
| 379 | 0 | $i->fields(@fs); | |||||||||||||||||||||||||||||||
| 380 | } | ||||||||||||||||||||||||||||||||
| 381 | |||||||||||||||||||||||||||||||||
| 382 | # Remove this field from all constraints using it | ||||||||||||||||||||||||||||||||
| 383 | 2 | foreach my $c ($self->get_constraints()) { | |||||||||||||||||||||||||||||||
| 384 | 0 | my @cs = $c->fields(); | |||||||||||||||||||||||||||||||
| 385 | 0 | @cs = grep { $_ ne $field->name } @cs; | |||||||||||||||||||||||||||||||
| 386 | 0 | $c->fields(@cs); | |||||||||||||||||||||||||||||||
| 387 | } | ||||||||||||||||||||||||||||||||
| 388 | } | ||||||||||||||||||||||||||||||||
| 389 | |||||||||||||||||||||||||||||||||
| 390 | 2 | return $field; | |||||||||||||||||||||||||||||||
| 391 | } | ||||||||||||||||||||||||||||||||
| 392 | |||||||||||||||||||||||||||||||||
| 393 | =head2 comments | ||||||||||||||||||||||||||||||||
| 394 | |||||||||||||||||||||||||||||||||
| 395 | Get or set the comments on a table. May be called several times to | ||||||||||||||||||||||||||||||||
| 396 | set and it will accumulate the comments. Called in an array context, | ||||||||||||||||||||||||||||||||
| 397 | returns each comment individually; called in a scalar context, returns | ||||||||||||||||||||||||||||||||
| 398 | all the comments joined on newlines. | ||||||||||||||||||||||||||||||||
| 399 | |||||||||||||||||||||||||||||||||
| 400 | $table->comments('foo'); | ||||||||||||||||||||||||||||||||
| 401 | $table->comments('bar'); | ||||||||||||||||||||||||||||||||
| 402 | print join( ', ', $table->comments ); # prints "foo, bar" | ||||||||||||||||||||||||||||||||
| 403 | |||||||||||||||||||||||||||||||||
| 404 | =cut | ||||||||||||||||||||||||||||||||
| 405 | |||||||||||||||||||||||||||||||||
| 406 | 75 | has comments => ( | |||||||||||||||||||||||||||||||
| 407 | is => 'rw', | ||||||||||||||||||||||||||||||||
| 408 | coerce => quote_sub(q{ ref($_[0]) eq 'ARRAY' ? $_[0] : [$_[0]] }), | ||||||||||||||||||||||||||||||||
| 409 | default => quote_sub(q{ [] }), | ||||||||||||||||||||||||||||||||
| 410 | ); | ||||||||||||||||||||||||||||||||
| 411 | |||||||||||||||||||||||||||||||||
| 412 | around comments => sub { | ||||||||||||||||||||||||||||||||
| 413 | 384 | ▶ | my $orig = shift; | ||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=8 SCAR=20.8
| |||||||||||||||||||||||||||||||||
| 414 | 384 | my $self = shift; | |||||||||||||||||||||||||||||||
| 415 | 384 | ▶ | my @comments = ref $_[0] ? @{ $_[0] } : @_; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 416 | |||||||||||||||||||||||||||||||||
| 417 | 384 | for my $arg ( @comments ) { | |||||||||||||||||||||||||||||||
| 418 | 274 | ▶ | $arg = $arg->[0] if ref $arg; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 419 | 274 | ▶ | push @{ $self->$orig }, $arg if defined $arg && $arg; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
defined $arg and $arg
MC/DC50%1/2
defined $arg and $arg
defined $arg
$arg
Truth table66%2/3
A and B
A= defined $argB= $arg
| |||||||||||||||||||||||||||||||||
| 420 | } | ||||||||||||||||||||||||||||||||
| 421 | |||||||||||||||||||||||||||||||||
| 422 | 384 | @comments = @{$self->$orig}; | |||||||||||||||||||||||||||||||
| 423 | return wantarray ? @comments | ||||||||||||||||||||||||||||||||
| 424 | 384 | ▶ | : @comments ? join( "\n", @comments ) | ||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||
| 425 | : undef; | ||||||||||||||||||||||||||||||||
| 426 | 75 | }; | |||||||||||||||||||||||||||||||
| 427 | |||||||||||||||||||||||||||||||||
| 428 | =head2 get_constraints | ||||||||||||||||||||||||||||||||
| 429 | |||||||||||||||||||||||||||||||||
| 430 | Returns all the constraint objects as an array or array reference. | ||||||||||||||||||||||||||||||||
| 431 | |||||||||||||||||||||||||||||||||
| 432 | my @constraints = $table->get_constraints; | ||||||||||||||||||||||||||||||||
| 433 | |||||||||||||||||||||||||||||||||
| 434 | =cut | ||||||||||||||||||||||||||||||||
| 435 | |||||||||||||||||||||||||||||||||
| 436 | sub get_constraints { | ||||||||||||||||||||||||||||||||
| 437 | 2748 | ▶ | my $self = shift; | ||||||||||||||||||||||||||||||
|
Subroutine
get_constraints: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||||||||||||||
| 438 | |||||||||||||||||||||||||||||||||
| 439 | 2748 | ▶ | if ( $self->_has_constraints ) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 440 | return wantarray | ||||||||||||||||||||||||||||||||
| 441 | 2082 | ▶ | ? @{ $self->_constraints } : $self->_constraints; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 442 | } | ||||||||||||||||||||||||||||||||
| 443 | else { | ||||||||||||||||||||||||||||||||
| 444 | 666 | $self->error('No constraints'); | |||||||||||||||||||||||||||||||
| 445 | 666 | return; | |||||||||||||||||||||||||||||||
| 446 | } | ||||||||||||||||||||||||||||||||
| 447 | } | ||||||||||||||||||||||||||||||||
| 448 | |||||||||||||||||||||||||||||||||
| 449 | =head2 get_indices | ||||||||||||||||||||||||||||||||
| 450 | |||||||||||||||||||||||||||||||||
| 451 | Returns all the index objects as an array or array reference. | ||||||||||||||||||||||||||||||||
| 452 | |||||||||||||||||||||||||||||||||
| 453 | my @indices = $table->get_indices; | ||||||||||||||||||||||||||||||||
| 454 | |||||||||||||||||||||||||||||||||
| 455 | =cut | ||||||||||||||||||||||||||||||||
| 456 | |||||||||||||||||||||||||||||||||
| 457 | sub get_indices { | ||||||||||||||||||||||||||||||||
| 458 | 610 | ▶ | my $self = shift; | ||||||||||||||||||||||||||||||
|
Subroutine
get_indices: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||||||||||||||
| 459 | |||||||||||||||||||||||||||||||||
| 460 | 610 | ▶ | if ( $self->_has_indices ) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 461 | return wantarray | ||||||||||||||||||||||||||||||||
| 462 | 217 | ▶ | ? @{ $self->_indices } | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 463 | : $self->_indices; | ||||||||||||||||||||||||||||||||
| 464 | } | ||||||||||||||||||||||||||||||||
| 465 | else { | ||||||||||||||||||||||||||||||||
| 466 | 393 | $self->error('No indices'); | |||||||||||||||||||||||||||||||
| 467 | 393 | return; | |||||||||||||||||||||||||||||||
| 468 | } | ||||||||||||||||||||||||||||||||
| 469 | } | ||||||||||||||||||||||||||||||||
| 470 | |||||||||||||||||||||||||||||||||
| 471 | =head2 get_field | ||||||||||||||||||||||||||||||||
| 472 | |||||||||||||||||||||||||||||||||
| 473 | Returns a field by the name provided. | ||||||||||||||||||||||||||||||||
| 474 | |||||||||||||||||||||||||||||||||
| 475 | my $field = $table->get_field('foo'); | ||||||||||||||||||||||||||||||||
| 476 | |||||||||||||||||||||||||||||||||
| 477 | =cut | ||||||||||||||||||||||||||||||||
| 478 | |||||||||||||||||||||||||||||||||
| 479 | sub get_field { | ||||||||||||||||||||||||||||||||
| 480 | 5479 | ▶ | my $self = shift; | ||||||||||||||||||||||||||||||
|
Subroutine
get_field: called CC=7 SCAR=19.5
Pod
documented
| |||||||||||||||||||||||||||||||||
| 481 | 5479 | ▶ | my $field_name = shift or return $self->error('No field name'); | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 482 | 5479 | my $case_insensitive = shift; | |||||||||||||||||||||||||||||||
| 483 | 5479 | ▶ | return $self->error(qq[Field "$field_name" does not exist]) | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 484 | unless $self->_has_fields; | ||||||||||||||||||||||||||||||||
| 485 | 5114 | ▶ | if ( $case_insensitive ) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 486 | 1 | $field_name = uc($field_name); | |||||||||||||||||||||||||||||||
| 487 | 1 | foreach my $field ( keys %{$self->_fields} ) { | |||||||||||||||||||||||||||||||
| 488 | 2 | ▶ | return $self->_fields->{$field} if $field_name eq uc($field); | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 489 | } | ||||||||||||||||||||||||||||||||
| 490 | 0 | return $self->error(qq[Field "$field_name" does not exist]); | |||||||||||||||||||||||||||||||
| 491 | } | ||||||||||||||||||||||||||||||||
| 492 | return $self->error( qq[Field "$field_name" does not exist] ) unless | ||||||||||||||||||||||||||||||||
| 493 | 5113 | ▶ | exists $self->_fields->{ $field_name }; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 494 | 3838 | return $self->_fields->{ $field_name }; | |||||||||||||||||||||||||||||||
| 495 | } | ||||||||||||||||||||||||||||||||
| 496 | |||||||||||||||||||||||||||||||||
| 497 | =head2 get_fields | ||||||||||||||||||||||||||||||||
| 498 | |||||||||||||||||||||||||||||||||
| 499 | Returns all the field objects as an array or array reference. | ||||||||||||||||||||||||||||||||
| 500 | |||||||||||||||||||||||||||||||||
| 501 | my @fields = $table->get_fields; | ||||||||||||||||||||||||||||||||
| 502 | |||||||||||||||||||||||||||||||||
| 503 | =cut | ||||||||||||||||||||||||||||||||
| 504 | |||||||||||||||||||||||||||||||||
| 505 | sub get_fields { | ||||||||||||||||||||||||||||||||
| 506 | 2448 | ▶ | my $self = shift; | ||||||||||||||||||||||||||||||
|
Subroutine
get_fields: called CC=4 SCAR=13.9
Pod
documented
| |||||||||||||||||||||||||||||||||
| 507 | my @fields = | ||||||||||||||||||||||||||||||||
| 508 | 9267 | map { $_->[1] } | |||||||||||||||||||||||||||||||
| 509 | 16363 | sort { $a->[0] <=> $b->[0] } | |||||||||||||||||||||||||||||||
| 510 | 9267 | map { [ $_->order, $_ ] } | |||||||||||||||||||||||||||||||
| 511 | 2448 | ▶ | values %{ $self->_has_fields ? $self->_fields : {} }; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 512 | |||||||||||||||||||||||||||||||||
| 513 | 2448 | ▶ | if ( @fields ) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 514 | 2100 | ▶ | return wantarray ? @fields : \@fields; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 515 | } | ||||||||||||||||||||||||||||||||
| 516 | else { | ||||||||||||||||||||||||||||||||
| 517 | 348 | $self->error('No fields'); | |||||||||||||||||||||||||||||||
| 518 | 348 | return; | |||||||||||||||||||||||||||||||
| 519 | } | ||||||||||||||||||||||||||||||||
| 520 | } | ||||||||||||||||||||||||||||||||
| 521 | |||||||||||||||||||||||||||||||||
| 522 | =head2 is_valid | ||||||||||||||||||||||||||||||||
| 523 | |||||||||||||||||||||||||||||||||
| 524 | Determine whether the view is valid or not. | ||||||||||||||||||||||||||||||||
| 525 | |||||||||||||||||||||||||||||||||
| 526 | my $ok = $view->is_valid; | ||||||||||||||||||||||||||||||||
| 527 | |||||||||||||||||||||||||||||||||
| 528 | =cut | ||||||||||||||||||||||||||||||||
| 529 | |||||||||||||||||||||||||||||||||
| 530 | sub is_valid { | ||||||||||||||||||||||||||||||||
| 531 | 59 | ▶ | my $self = shift; | ||||||||||||||||||||||||||||||
|
Subroutine
is_valid: called CC=5 SCAR=16.1
Pod
documented
| |||||||||||||||||||||||||||||||||
| 532 | 59 | ▶ | return $self->error('No name') unless $self->name; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 533 | 59 | ▶ | return $self->error('No fields') unless $self->get_fields; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 534 | |||||||||||||||||||||||||||||||||
| 535 | 57 | for my $object ( | |||||||||||||||||||||||||||||||
| 536 | $self->get_fields, $self->get_indices, $self->get_constraints | ||||||||||||||||||||||||||||||||
| 537 | ) { | ||||||||||||||||||||||||||||||||
| 538 | 327 | ▶ | return $object->error unless $object->is_valid; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 539 | } | ||||||||||||||||||||||||||||||||
| 540 | |||||||||||||||||||||||||||||||||
| 541 | 54 | return 1; | |||||||||||||||||||||||||||||||
| 542 | } | ||||||||||||||||||||||||||||||||
| 543 | |||||||||||||||||||||||||||||||||
| 544 | =head2 is_trivial_link | ||||||||||||||||||||||||||||||||
| 545 | |||||||||||||||||||||||||||||||||
| 546 | True if table has no data (non-key) fields and only uses single key joins. | ||||||||||||||||||||||||||||||||
| 547 | |||||||||||||||||||||||||||||||||
| 548 | =cut | ||||||||||||||||||||||||||||||||
| 549 | |||||||||||||||||||||||||||||||||
| 550 | 75 | has is_trivial_link => ( is => 'lazy', init_arg => undef ); | |||||||||||||||||||||||||||||||
| 551 | |||||||||||||||||||||||||||||||||
| 552 | 75 | around is_trivial_link => carp_ro('is_trivial_link'); | |||||||||||||||||||||||||||||||
| 553 | |||||||||||||||||||||||||||||||||
| 554 | sub _build_is_trivial_link { | ||||||||||||||||||||||||||||||||
| 555 | 3 | ▶ | my $self = shift; | ||||||||||||||||||||||||||||||
|
Subroutine
_build_is_trivial_link: called CC=6 SCAR=17.9
| |||||||||||||||||||||||||||||||||
| 556 | 3 | ▶ | return 0 if $self->is_data; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 557 | |||||||||||||||||||||||||||||||||
| 558 | 2 | my %fk = (); | |||||||||||||||||||||||||||||||
| 559 | |||||||||||||||||||||||||||||||||
| 560 | 2 | foreach my $field ( $self->get_fields ) { | |||||||||||||||||||||||||||||||
| 561 | 5 | ▶ | next unless $field->is_foreign_key; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 562 | 4 | $fk{$field->foreign_key_reference->reference_table}++; | |||||||||||||||||||||||||||||||
| 563 | } | ||||||||||||||||||||||||||||||||
| 564 | |||||||||||||||||||||||||||||||||
| 565 | 2 | foreach my $referenced (keys %fk){ | |||||||||||||||||||||||||||||||
| 566 | 3 | ▶ | if($fk{$referenced} > 1){ | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 567 | 1 | return 0; | |||||||||||||||||||||||||||||||
| 568 | } | ||||||||||||||||||||||||||||||||
| 569 | } | ||||||||||||||||||||||||||||||||
| 570 | |||||||||||||||||||||||||||||||||
| 571 | 1 | return 1; | |||||||||||||||||||||||||||||||
| 572 | } | ||||||||||||||||||||||||||||||||
| 573 | |||||||||||||||||||||||||||||||||
| 574 | =head2 is_data | ||||||||||||||||||||||||||||||||
| 575 | |||||||||||||||||||||||||||||||||
| 576 | Returns true if the table has some non-key fields. | ||||||||||||||||||||||||||||||||
| 577 | |||||||||||||||||||||||||||||||||
| 578 | =cut | ||||||||||||||||||||||||||||||||
| 579 | |||||||||||||||||||||||||||||||||
| 580 | 75 | has is_data => ( is => 'lazy', init_arg => undef ); | |||||||||||||||||||||||||||||||
| 581 | |||||||||||||||||||||||||||||||||
| 582 | 75 | around is_data => carp_ro('is_data'); | |||||||||||||||||||||||||||||||
| 583 | |||||||||||||||||||||||||||||||||
| 584 | sub _build_is_data { | ||||||||||||||||||||||||||||||||
| 585 | 4 | ▶ | my $self = shift; | ||||||||||||||||||||||||||||||
|
Subroutine
_build_is_data: called CC=4 SCAR=13.9
| |||||||||||||||||||||||||||||||||
| 586 | |||||||||||||||||||||||||||||||||
| 587 | 4 | foreach my $field ( $self->get_fields ) { | |||||||||||||||||||||||||||||||
| 588 | 9 | ▶ | if ( !$field->is_primary_key and !$field->is_foreign_key ) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$field->is_primary_key or $field->is_foreign_key
MC/DC100%2/2
$field->is_primary_key or $field->is_foreign_key
$field->is_primary_key
$field->is_foreign_key
Truth table100%3/3
A or B
A= $field->is_primary_keyB= $field->is_foreign_key
| |||||||||||||||||||||||||||||||||
| 589 | 2 | return 1; | |||||||||||||||||||||||||||||||
| 590 | } | ||||||||||||||||||||||||||||||||
| 591 | } | ||||||||||||||||||||||||||||||||
| 592 | |||||||||||||||||||||||||||||||||
| 593 | 2 | return 0; | |||||||||||||||||||||||||||||||
| 594 | } | ||||||||||||||||||||||||||||||||
| 595 | |||||||||||||||||||||||||||||||||
| 596 | =head2 can_link | ||||||||||||||||||||||||||||||||
| 597 | |||||||||||||||||||||||||||||||||
| 598 | Determine whether the table can link two arg tables via many-to-many. | ||||||||||||||||||||||||||||||||
| 599 | |||||||||||||||||||||||||||||||||
| 600 | my $ok = $table->can_link($table1,$table2); | ||||||||||||||||||||||||||||||||
| 601 | |||||||||||||||||||||||||||||||||
| 602 | =cut | ||||||||||||||||||||||||||||||||
| 603 | |||||||||||||||||||||||||||||||||
| 604 | 75 | has _can_link => ( is => 'ro', init_arg => undef, default => quote_sub(q{ +{} }) ); | |||||||||||||||||||||||||||||||
| 605 | |||||||||||||||||||||||||||||||||
| 606 | sub can_link { | ||||||||||||||||||||||||||||||||
| 607 | 3 | ▶ | my ( $self, $table1, $table2 ) = @_; | ||||||||||||||||||||||||||||||
|
Subroutine
can_link: called CC=15 SCAR=30.7
Pod
documented
| |||||||||||||||||||||||||||||||||
| 608 | |||||||||||||||||||||||||||||||||
| 609 | return $self->_can_link->{ $table1->name }{ $table2->name } | ||||||||||||||||||||||||||||||||
| 610 | 3 | ▶ | if defined $self->_can_link->{ $table1->name }{ $table2->name }; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 611 | |||||||||||||||||||||||||||||||||
| 612 | 3 | ▶ | if ( $self->is_data == 1 ) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 613 | 0 | $self->_can_link->{ $table1->name }{ $table2->name } = [0]; | |||||||||||||||||||||||||||||||
| 614 | 0 | $self->_can_link->{ $table2->name }{ $table1->name } = [0]; | |||||||||||||||||||||||||||||||
| 615 | 0 | return $self->_can_link->{ $table1->name }{ $table2->name }; | |||||||||||||||||||||||||||||||
| 616 | } | ||||||||||||||||||||||||||||||||
| 617 | |||||||||||||||||||||||||||||||||
| 618 | 3 | my %fk = (); | |||||||||||||||||||||||||||||||
| 619 | |||||||||||||||||||||||||||||||||
| 620 | 3 | foreach my $field ( $self->get_fields ) { | |||||||||||||||||||||||||||||||
| 621 | 7 | ▶ | if ( $field->is_foreign_key ) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 622 | 6 | push @{ $fk{ $field->foreign_key_reference->reference_table } }, | |||||||||||||||||||||||||||||||
| 623 | $field->foreign_key_reference; | ||||||||||||||||||||||||||||||||
| 624 | } | ||||||||||||||||||||||||||||||||
| 625 | } | ||||||||||||||||||||||||||||||||
| 626 | |||||||||||||||||||||||||||||||||
| 627 | 3 | ▶ | if ( !defined( $fk{ $table1->name } ) or !defined( $fk{ $table2->name } ) ) | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
defined $fk{$table1->name} and defined $fk{$table2->name}
MC/DC50%1/2
defined $fk{$table1->name} and defined $fk{$table2->name}
defined $fk{$table1->name}
defined $fk{$table2->name}
Truth table66%2/3
A and B
A= defined $fk{$table1->name}B= defined $fk{$table2->name}
| |||||||||||||||||||||||||||||||||
| 628 | { | ||||||||||||||||||||||||||||||||
| 629 | 1 | $self->_can_link->{ $table1->name }{ $table2->name } = [0]; | |||||||||||||||||||||||||||||||
| 630 | 1 | $self->_can_link->{ $table2->name }{ $table1->name } = [0]; | |||||||||||||||||||||||||||||||
| 631 | 1 | return $self->_can_link->{ $table1->name }{ $table2->name }; | |||||||||||||||||||||||||||||||
| 632 | } | ||||||||||||||||||||||||||||||||
| 633 | |||||||||||||||||||||||||||||||||
| 634 | # trivial traversal, only one way to link the two tables | ||||||||||||||||||||||||||||||||
| 635 | 2 | ▶ | if ( scalar( @{ $fk{ $table1->name } } == 1 ) | ||||||||||||||||||||||||||||||
|
Branch: 4 branches
Condition100%2/2
scalar(@{$fk{$table1->name};} == 1) and scalar(@{$fk{$table2->name};} == 1)
MC/DC100%1/1
scalar(@{$fk{$table1->name};} == 1) and scalar(@{$fk{$table2->name};} == 1)
scalar(@{$fk{$table1->name};} == 1)
Truth table100%2/2
A
A= scalar(@{$fk{$table1->name};} == 1)
| |||||||||||||||||||||||||||||||||
| 636 | 1 | and scalar( @{ $fk{ $table2->name } } == 1 ) ) | |||||||||||||||||||||||||||||||
| 637 | { | ||||||||||||||||||||||||||||||||
| 638 | $self->_can_link->{ $table1->name }{ $table2->name } = | ||||||||||||||||||||||||||||||||
| 639 | 1 | [ 'one2one', $fk{ $table1->name }, $fk{ $table2->name } ]; | |||||||||||||||||||||||||||||||
| 640 | $self->_can_link->{ $table1->name }{ $table2->name } = | ||||||||||||||||||||||||||||||||
| 641 | 1 | [ 'one2one', $fk{ $table2->name }, $fk{ $table1->name } ]; | |||||||||||||||||||||||||||||||
| 642 | |||||||||||||||||||||||||||||||||
| 643 | # non-trivial traversal. one way to link table2, | ||||||||||||||||||||||||||||||||
| 644 | # many ways to link table1 | ||||||||||||||||||||||||||||||||
| 645 | } | ||||||||||||||||||||||||||||||||
| 646 | 1 | ▶ | elsif ( scalar( @{ $fk{ $table1->name } } > 1 ) | ||||||||||||||||||||||||||||||
|
Condition50%1/2
scalar(@{$fk{$table1->name};} > 1) and scalar(@{$fk{$table2->name};} == 1)
MC/DC0%0/1
scalar(@{$fk{$table1->name};} > 1) and scalar(@{$fk{$table2->name};} == 1)
scalar(@{$fk{$table1->name};} > 1)
Truth table50%1/2
A
A= scalar(@{$fk{$table1->name};} > 1)
| |||||||||||||||||||||||||||||||||
| 647 | 1 | and scalar( @{ $fk{ $table2->name } } == 1 ) ) | |||||||||||||||||||||||||||||||
| 648 | { | ||||||||||||||||||||||||||||||||
| 649 | $self->_can_link->{ $table1->name }{ $table2->name } = | ||||||||||||||||||||||||||||||||
| 650 | 0 | [ 'many2one', $fk{ $table1->name }, $fk{ $table2->name } ]; | |||||||||||||||||||||||||||||||
| 651 | $self->_can_link->{ $table2->name }{ $table1->name } = | ||||||||||||||||||||||||||||||||
| 652 | 0 | [ 'one2many', $fk{ $table2->name }, $fk{ $table1->name } ]; | |||||||||||||||||||||||||||||||
| 653 | |||||||||||||||||||||||||||||||||
| 654 | # non-trivial traversal. one way to link table1, | ||||||||||||||||||||||||||||||||
| 655 | # many ways to link table2 | ||||||||||||||||||||||||||||||||
| 656 | } | ||||||||||||||||||||||||||||||||
| 657 | 1 | ▶ | elsif ( scalar( @{ $fk{ $table1->name } } == 1 ) | ||||||||||||||||||||||||||||||
|
Condition50%1/2
scalar(@{$fk{$table1->name};} == 1) and scalar(@{$fk{$table2->name};} > 1)
MC/DC0%0/1
scalar(@{$fk{$table1->name};} == 1) and scalar(@{$fk{$table2->name};} > 1)
scalar(@{$fk{$table1->name};} == 1)
Truth table50%1/2
A
A= scalar(@{$fk{$table1->name};} == 1)
| |||||||||||||||||||||||||||||||||
| 658 | 0 | and scalar( @{ $fk{ $table2->name } } > 1 ) ) | |||||||||||||||||||||||||||||||
| 659 | { | ||||||||||||||||||||||||||||||||
| 660 | $self->_can_link->{ $table1->name }{ $table2->name } = | ||||||||||||||||||||||||||||||||
| 661 | 0 | [ 'one2many', $fk{ $table1->name }, $fk{ $table2->name } ]; | |||||||||||||||||||||||||||||||
| 662 | $self->_can_link->{ $table2->name }{ $table1->name } = | ||||||||||||||||||||||||||||||||
| 663 | 0 | [ 'many2one', $fk{ $table2->name }, $fk{ $table1->name } ]; | |||||||||||||||||||||||||||||||
| 664 | |||||||||||||||||||||||||||||||||
| 665 | # non-trivial traversal. many ways to link table1 and table2 | ||||||||||||||||||||||||||||||||
| 666 | } | ||||||||||||||||||||||||||||||||
| 667 | 1 | ▶ | elsif ( scalar( @{ $fk{ $table1->name } } > 1 ) | ||||||||||||||||||||||||||||||
|
Condition50%1/2
scalar(@{$fk{$table1->name};} > 1) and scalar(@{$fk{$table2->name};} > 1)
MC/DC0%0/1
scalar(@{$fk{$table1->name};} > 1) and scalar(@{$fk{$table2->name};} > 1)
scalar(@{$fk{$table1->name};} > 1)
Truth table50%1/2
A
A= scalar(@{$fk{$table1->name};} > 1)
| |||||||||||||||||||||||||||||||||
| 668 | 1 | and scalar( @{ $fk{ $table2->name } } > 1 ) ) | |||||||||||||||||||||||||||||||
| 669 | { | ||||||||||||||||||||||||||||||||
| 670 | $self->_can_link->{ $table1->name }{ $table2->name } = | ||||||||||||||||||||||||||||||||
| 671 | 1 | [ 'many2many', $fk{ $table1->name }, $fk{ $table2->name } ]; | |||||||||||||||||||||||||||||||
| 672 | $self->_can_link->{ $table2->name }{ $table1->name } = | ||||||||||||||||||||||||||||||||
| 673 | 1 | [ 'many2many', $fk{ $table2->name }, $fk{ $table1->name } ]; | |||||||||||||||||||||||||||||||
| 674 | |||||||||||||||||||||||||||||||||
| 675 | # one of the tables didn't export a key | ||||||||||||||||||||||||||||||||
| 676 | # to this table, no linking possible | ||||||||||||||||||||||||||||||||
| 677 | } | ||||||||||||||||||||||||||||||||
| 678 | else { | ||||||||||||||||||||||||||||||||
| 679 | 0 | $self->_can_link->{ $table1->name }{ $table2->name } = [0]; | |||||||||||||||||||||||||||||||
| 680 | 0 | $self->_can_link->{ $table2->name }{ $table1->name } = [0]; | |||||||||||||||||||||||||||||||
| 681 | } | ||||||||||||||||||||||||||||||||
| 682 | |||||||||||||||||||||||||||||||||
| 683 | 2 | return $self->_can_link->{ $table1->name }{ $table2->name }; | |||||||||||||||||||||||||||||||
| 684 | } | ||||||||||||||||||||||||||||||||
| 685 | |||||||||||||||||||||||||||||||||
| 686 | =head2 name | ||||||||||||||||||||||||||||||||
| 687 | |||||||||||||||||||||||||||||||||
| 688 | Get or set the table's name. | ||||||||||||||||||||||||||||||||
| 689 | |||||||||||||||||||||||||||||||||
| 690 | Errors ("No table name") if you try to set a blank name. | ||||||||||||||||||||||||||||||||
| 691 | |||||||||||||||||||||||||||||||||
| 692 | If provided an argument, checks the schema object for a table of | ||||||||||||||||||||||||||||||||
| 693 | that name and disallows the change if one exists (setting the error to | ||||||||||||||||||||||||||||||||
| 694 | "Can't use table name "%s": table exists"). | ||||||||||||||||||||||||||||||||
| 695 | |||||||||||||||||||||||||||||||||
| 696 | my $table_name = $table->name('foo'); | ||||||||||||||||||||||||||||||||
| 697 | |||||||||||||||||||||||||||||||||
| 698 | =cut | ||||||||||||||||||||||||||||||||
| 699 | |||||||||||||||||||||||||||||||||
| 700 | has name => ( | ||||||||||||||||||||||||||||||||
| 701 | is => 'rw', | ||||||||||||||||||||||||||||||||
| 702 | 367 | ▶ | isa => sub { throw("No table name") unless $_[0] }, | ||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=6.9
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 703 | 75 | ); | |||||||||||||||||||||||||||||||
| 704 | |||||||||||||||||||||||||||||||||
| 705 | around name => sub { | ||||||||||||||||||||||||||||||||
| 706 | 10113 | ▶ | my $orig = shift; | ||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=4 SCAR=13.9
| |||||||||||||||||||||||||||||||||
| 707 | 10113 | my $self = shift; | |||||||||||||||||||||||||||||||
| 708 | |||||||||||||||||||||||||||||||||
| 709 | 10113 | ▶ | if ( my ($arg) = @_ ) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 710 | 5 | ▶ | if ( my $schema = $self->schema ) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 711 | 4 | ▶ | return $self->error( qq[Can't use table name "$arg": table exists] ) | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 712 | if $schema->get_table( $arg ); | ||||||||||||||||||||||||||||||||
| 713 | } | ||||||||||||||||||||||||||||||||
| 714 | } | ||||||||||||||||||||||||||||||||
| 715 | |||||||||||||||||||||||||||||||||
| 716 | 10112 | return ex2err($orig, $self, @_); | |||||||||||||||||||||||||||||||
| 717 | 75 | }; | |||||||||||||||||||||||||||||||
| 718 | |||||||||||||||||||||||||||||||||
| 719 | =head2 schema | ||||||||||||||||||||||||||||||||
| 720 | |||||||||||||||||||||||||||||||||
| 721 | Get or set the table's schema object. | ||||||||||||||||||||||||||||||||
| 722 | |||||||||||||||||||||||||||||||||
| 723 | my $schema = $table->schema; | ||||||||||||||||||||||||||||||||
| 724 | |||||||||||||||||||||||||||||||||
| 725 | =cut | ||||||||||||||||||||||||||||||||
| 726 | |||||||||||||||||||||||||||||||||
| 727 | 75 | has schema => ( is => 'rw', isa => schema_obj('Schema'), weak_ref => 1 ); | |||||||||||||||||||||||||||||||
| 728 | |||||||||||||||||||||||||||||||||
| 729 | 75 | around schema => \&ex2err; | |||||||||||||||||||||||||||||||
| 730 | |||||||||||||||||||||||||||||||||
| 731 | sub primary_key { | ||||||||||||||||||||||||||||||||
| 732 | |||||||||||||||||||||||||||||||||
| 733 | =pod | ||||||||||||||||||||||||||||||||
| 734 | |||||||||||||||||||||||||||||||||
| 735 | =head2 primary_key | ||||||||||||||||||||||||||||||||
| 736 | |||||||||||||||||||||||||||||||||
| 737 | Gets or sets the table's primary key(s). Takes one or more field | ||||||||||||||||||||||||||||||||
| 738 | names (as a string, list or array[ref]) as an argument. If the field | ||||||||||||||||||||||||||||||||
| 739 | names are present, it will create a new PK if none exists, or it will | ||||||||||||||||||||||||||||||||
| 740 | add to the fields of an existing PK (and will unique the field names). | ||||||||||||||||||||||||||||||||
| 741 | Returns the C<SQL::Translator::Schema::Constraint> object representing | ||||||||||||||||||||||||||||||||
| 742 | the primary key. | ||||||||||||||||||||||||||||||||
| 743 | |||||||||||||||||||||||||||||||||
| 744 | These are equivalent: | ||||||||||||||||||||||||||||||||
| 745 | |||||||||||||||||||||||||||||||||
| 746 | $table->primary_key('id'); | ||||||||||||||||||||||||||||||||
| 747 | $table->primary_key(['name']); | ||||||||||||||||||||||||||||||||
| 748 | $table->primary_key('id','name']); | ||||||||||||||||||||||||||||||||
| 749 | $table->primary_key(['id','name']); | ||||||||||||||||||||||||||||||||
| 750 | $table->primary_key('id,name'); | ||||||||||||||||||||||||||||||||
| 751 | $table->primary_key(qw[ id name ]); | ||||||||||||||||||||||||||||||||
| 752 | |||||||||||||||||||||||||||||||||
| 753 | my $pk = $table->primary_key; | ||||||||||||||||||||||||||||||||
| 754 | |||||||||||||||||||||||||||||||||
| 755 | =cut | ||||||||||||||||||||||||||||||||
| 756 | |||||||||||||||||||||||||||||||||
| 757 | 1654 | ▶ | my $self = shift; | ||||||||||||||||||||||||||||||
|
Subroutine
primary_key: called CC=11 SCAR=24.0
Pod
documented
| |||||||||||||||||||||||||||||||||
| 758 | 1654 | my $fields = parse_list_arg( @_ ); | |||||||||||||||||||||||||||||||
| 759 | |||||||||||||||||||||||||||||||||
| 760 | 1654 | my $constraint; | |||||||||||||||||||||||||||||||
| 761 | 1654 | ▶ | if ( @$fields ) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 762 | 211 | for my $f ( @$fields ) { | |||||||||||||||||||||||||||||||
| 763 | 220 | ▶ | return $self->error(qq[Invalid field "$f"]) unless | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 764 | $self->get_field($f); | ||||||||||||||||||||||||||||||||
| 765 | } | ||||||||||||||||||||||||||||||||
| 766 | |||||||||||||||||||||||||||||||||
| 767 | 205 | my $has_pk; | |||||||||||||||||||||||||||||||
| 768 | 205 | for my $c ( $self->get_constraints ) { | |||||||||||||||||||||||||||||||
| 769 | 98 | ▶ | if ( $c->type eq PRIMARY_KEY ) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 770 | 88 | $has_pk = 1; | |||||||||||||||||||||||||||||||
| 771 | 88 | $c->fields( @{ $c->fields }, @$fields ); | |||||||||||||||||||||||||||||||
| 772 | 88 | $constraint = $c; | |||||||||||||||||||||||||||||||
| 773 | } | ||||||||||||||||||||||||||||||||
| 774 | } | ||||||||||||||||||||||||||||||||
| 775 | |||||||||||||||||||||||||||||||||
| 776 | 205 | ▶ | unless ( $has_pk ) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 777 | 117 | ▶ | $constraint = $self->add_constraint( | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 778 | type => PRIMARY_KEY, | ||||||||||||||||||||||||||||||||
| 779 | fields => $fields, | ||||||||||||||||||||||||||||||||
| 780 | ) or return; | ||||||||||||||||||||||||||||||||
| 781 | } | ||||||||||||||||||||||||||||||||
| 782 | } | ||||||||||||||||||||||||||||||||
| 783 | |||||||||||||||||||||||||||||||||
| 784 | 1648 | ▶ | if ( $constraint ) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 785 | 205 | return $constraint; | |||||||||||||||||||||||||||||||
| 786 | } | ||||||||||||||||||||||||||||||||
| 787 | else { | ||||||||||||||||||||||||||||||||
| 788 | 1443 | for my $c ( $self->get_constraints ) { | |||||||||||||||||||||||||||||||
| 789 | 1211 | ▶ | return $c if $c->type eq PRIMARY_KEY; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 790 | } | ||||||||||||||||||||||||||||||||
| 791 | } | ||||||||||||||||||||||||||||||||
| 792 | |||||||||||||||||||||||||||||||||
| 793 | 471 | return; | |||||||||||||||||||||||||||||||
| 794 | } | ||||||||||||||||||||||||||||||||
| 795 | |||||||||||||||||||||||||||||||||
| 796 | =head2 options | ||||||||||||||||||||||||||||||||
| 797 | |||||||||||||||||||||||||||||||||
| 798 | Get or append to the table's options (e.g., table types for MySQL). | ||||||||||||||||||||||||||||||||
| 799 | Returns an array or array reference. | ||||||||||||||||||||||||||||||||
| 800 | |||||||||||||||||||||||||||||||||
| 801 | my @options = $table->options; | ||||||||||||||||||||||||||||||||
| 802 | |||||||||||||||||||||||||||||||||
| 803 | =cut | ||||||||||||||||||||||||||||||||
| 804 | |||||||||||||||||||||||||||||||||
| 805 | 75 | with ListAttr options => ( append => 1 ); | |||||||||||||||||||||||||||||||
| 806 | |||||||||||||||||||||||||||||||||
| 807 | =head2 order | ||||||||||||||||||||||||||||||||
| 808 | |||||||||||||||||||||||||||||||||
| 809 | Get or set the table's order. | ||||||||||||||||||||||||||||||||
| 810 | |||||||||||||||||||||||||||||||||
| 811 | my $order = $table->order(3); | ||||||||||||||||||||||||||||||||
| 812 | |||||||||||||||||||||||||||||||||
| 813 | =cut | ||||||||||||||||||||||||||||||||
| 814 | |||||||||||||||||||||||||||||||||
| 815 | 75 | has order => ( is => 'rw', default => quote_sub(q{ 0 }) ); | |||||||||||||||||||||||||||||||
| 816 | |||||||||||||||||||||||||||||||||
| 817 | around order => sub { | ||||||||||||||||||||||||||||||||
| 818 | 1268 | ▶ | my ( $orig, $self, $arg ) = @_; | ||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||||||
| 819 | |||||||||||||||||||||||||||||||||
| 820 | 1268 | ▶ | 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+$/
| |||||||||||||||||||||||||||||||||
| 821 | 357 | return $self->$orig($arg); | |||||||||||||||||||||||||||||||
| 822 | } | ||||||||||||||||||||||||||||||||
| 823 | |||||||||||||||||||||||||||||||||
| 824 | 911 | return $self->$orig; | |||||||||||||||||||||||||||||||
| 825 | 75 | }; | |||||||||||||||||||||||||||||||
| 826 | |||||||||||||||||||||||||||||||||
| 827 | =head2 field_names | ||||||||||||||||||||||||||||||||
| 828 | |||||||||||||||||||||||||||||||||
| 829 | Read-only method to return a list or array ref of the field names. Returns undef | ||||||||||||||||||||||||||||||||
| 830 | or an empty list if the table has no fields set. Useful if you want to | ||||||||||||||||||||||||||||||||
| 831 | avoid the overload magic of the Field objects returned by the get_fields method. | ||||||||||||||||||||||||||||||||
| 832 | |||||||||||||||||||||||||||||||||
| 833 | my @names = $constraint->field_names; | ||||||||||||||||||||||||||||||||
| 834 | |||||||||||||||||||||||||||||||||
| 835 | =cut | ||||||||||||||||||||||||||||||||
| 836 | |||||||||||||||||||||||||||||||||
| 837 | sub field_names { | ||||||||||||||||||||||||||||||||
| 838 | 3 | ▶ | my $self = shift; | ||||||||||||||||||||||||||||||
|
Subroutine
field_names: called CC=3 SCAR=12.3
Pod
documented
| |||||||||||||||||||||||||||||||||
| 839 | my @fields = | ||||||||||||||||||||||||||||||||
| 840 | 3 | map { $_->name } | |||||||||||||||||||||||||||||||
| 841 | $self->get_fields; | ||||||||||||||||||||||||||||||||
| 842 | |||||||||||||||||||||||||||||||||
| 843 | 3 | ▶ | if ( @fields ) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 844 | 3 | ▶ | return wantarray ? @fields : \@fields; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 845 | } | ||||||||||||||||||||||||||||||||
| 846 | else { | ||||||||||||||||||||||||||||||||
| 847 | 0 | $self->error('No fields'); | |||||||||||||||||||||||||||||||
| 848 | 0 | return; | |||||||||||||||||||||||||||||||
| 849 | } | ||||||||||||||||||||||||||||||||
| 850 | } | ||||||||||||||||||||||||||||||||
| 851 | |||||||||||||||||||||||||||||||||
| 852 | sub equals { | ||||||||||||||||||||||||||||||||
| 853 | |||||||||||||||||||||||||||||||||
| 854 | =pod | ||||||||||||||||||||||||||||||||
| 855 | |||||||||||||||||||||||||||||||||
| 856 | =head2 equals | ||||||||||||||||||||||||||||||||
| 857 | |||||||||||||||||||||||||||||||||
| 858 | Determines if this table is the same as another | ||||||||||||||||||||||||||||||||
| 859 | |||||||||||||||||||||||||||||||||
| 860 | my $isIdentical = $table1->equals( $table2 ); | ||||||||||||||||||||||||||||||||
| 861 | |||||||||||||||||||||||||||||||||
| 862 | =cut | ||||||||||||||||||||||||||||||||
| 863 | |||||||||||||||||||||||||||||||||
| 864 | 0 | ▶ | my $self = shift; | ||||||||||||||||||||||||||||||
|
Subroutine
equals: not called CC=24 SCAR=64.0
Pod
documented
| |||||||||||||||||||||||||||||||||
| 865 | 0 | my $other = shift; | |||||||||||||||||||||||||||||||
| 866 | 0 | my $case_insensitive = shift; | |||||||||||||||||||||||||||||||
| 867 | |||||||||||||||||||||||||||||||||
| 868 | 0 | ▶ | return 0 unless $self->SUPER::equals($other); | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 869 | 0 | ▶ | return 0 unless $case_insensitive ? uc($self->name) eq uc($other->name) : $self->name eq $other->name; | ||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||
| 870 | 0 | ▶ | return 0 unless $self->_compare_objects(scalar $self->options, scalar $other->options); | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 871 | 0 | ▶ | return 0 unless $self->_compare_objects(scalar $self->extra, scalar $other->extra); | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 872 | |||||||||||||||||||||||||||||||||
| 873 | # Fields | ||||||||||||||||||||||||||||||||
| 874 | # Go through our fields | ||||||||||||||||||||||||||||||||
| 875 | 0 | my %checkedFields; | |||||||||||||||||||||||||||||||
| 876 | 0 | foreach my $field ( $self->get_fields ) { | |||||||||||||||||||||||||||||||
| 877 | 0 | my $otherField = $other->get_field($field->name, $case_insensitive); | |||||||||||||||||||||||||||||||
| 878 | 0 | ▶ | return 0 unless $field->equals($otherField, $case_insensitive); | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 879 | 0 | $checkedFields{$field->name} = 1; | |||||||||||||||||||||||||||||||
| 880 | } | ||||||||||||||||||||||||||||||||
| 881 | # Go through the other table's fields | ||||||||||||||||||||||||||||||||
| 882 | 0 | foreach my $otherField ( $other->get_fields ) { | |||||||||||||||||||||||||||||||
| 883 | 0 | ▶ | next if $checkedFields{$otherField->name}; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 884 | 0 | return 0; | |||||||||||||||||||||||||||||||
| 885 | } | ||||||||||||||||||||||||||||||||
| 886 | |||||||||||||||||||||||||||||||||
| 887 | # Constraints | ||||||||||||||||||||||||||||||||
| 888 | # Go through our constraints | ||||||||||||||||||||||||||||||||
| 889 | 0 | my %checkedConstraints; | |||||||||||||||||||||||||||||||
| 890 | CONSTRAINT: | ||||||||||||||||||||||||||||||||
| 891 | 0 | foreach my $constraint ( $self->get_constraints ) { | |||||||||||||||||||||||||||||||
| 892 | 0 | foreach my $otherConstraint ( $other->get_constraints ) { | |||||||||||||||||||||||||||||||
| 893 | 0 | ▶ | if ( $constraint->equals($otherConstraint, $case_insensitive) ) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 894 | 0 | $checkedConstraints{$otherConstraint} = 1; | |||||||||||||||||||||||||||||||
| 895 | 0 | next CONSTRAINT; | |||||||||||||||||||||||||||||||
| 896 | } | ||||||||||||||||||||||||||||||||
| 897 | } | ||||||||||||||||||||||||||||||||
| 898 | 0 | return 0; | |||||||||||||||||||||||||||||||
| 899 | } | ||||||||||||||||||||||||||||||||
| 900 | # Go through the other table's constraints | ||||||||||||||||||||||||||||||||
| 901 | CONSTRAINT2: | ||||||||||||||||||||||||||||||||
| 902 | 0 | foreach my $otherConstraint ( $other->get_constraints ) { | |||||||||||||||||||||||||||||||
| 903 | 0 | ▶ | next if $checkedFields{$otherConstraint}; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 904 | 0 | foreach my $constraint ( $self->get_constraints ) { | |||||||||||||||||||||||||||||||
| 905 | 0 | ▶ | if ( $otherConstraint->equals($constraint, $case_insensitive) ) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 906 | 0 | next CONSTRAINT2; | |||||||||||||||||||||||||||||||
| 907 | } | ||||||||||||||||||||||||||||||||
| 908 | } | ||||||||||||||||||||||||||||||||
| 909 | 0 | return 0; | |||||||||||||||||||||||||||||||
| 910 | } | ||||||||||||||||||||||||||||||||
| 911 | |||||||||||||||||||||||||||||||||
| 912 | # Indices | ||||||||||||||||||||||||||||||||
| 913 | # Go through our indices | ||||||||||||||||||||||||||||||||
| 914 | 0 | my %checkedIndices; | |||||||||||||||||||||||||||||||
| 915 | INDEX: | ||||||||||||||||||||||||||||||||
| 916 | 0 | foreach my $index ( $self->get_indices ) { | |||||||||||||||||||||||||||||||
| 917 | 0 | foreach my $otherIndex ( $other->get_indices ) { | |||||||||||||||||||||||||||||||
| 918 | 0 | ▶ | if ( $index->equals($otherIndex, $case_insensitive) ) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 919 | 0 | $checkedIndices{$otherIndex} = 1; | |||||||||||||||||||||||||||||||
| 920 | 0 | next INDEX; | |||||||||||||||||||||||||||||||
| 921 | } | ||||||||||||||||||||||||||||||||
| 922 | } | ||||||||||||||||||||||||||||||||
| 923 | 0 | return 0; | |||||||||||||||||||||||||||||||
| 924 | } | ||||||||||||||||||||||||||||||||
| 925 | # Go through the other table's indices | ||||||||||||||||||||||||||||||||
| 926 | INDEX2: | ||||||||||||||||||||||||||||||||
| 927 | 0 | foreach my $otherIndex ( $other->get_indices ) { | |||||||||||||||||||||||||||||||
| 928 | 0 | ▶ | next if $checkedIndices{$otherIndex}; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 929 | 0 | foreach my $index ( $self->get_indices ) { | |||||||||||||||||||||||||||||||
| 930 | 0 | ▶ | if ( $otherIndex->equals($index, $case_insensitive) ) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 931 | 0 | next INDEX2; | |||||||||||||||||||||||||||||||
| 932 | } | ||||||||||||||||||||||||||||||||
| 933 | } | ||||||||||||||||||||||||||||||||
| 934 | 0 | return 0; | |||||||||||||||||||||||||||||||
| 935 | } | ||||||||||||||||||||||||||||||||
| 936 | |||||||||||||||||||||||||||||||||
| 937 | 0 | return 1; | |||||||||||||||||||||||||||||||
| 938 | } | ||||||||||||||||||||||||||||||||
| 939 | |||||||||||||||||||||||||||||||||
| 940 | =head1 LOOKUP METHODS | ||||||||||||||||||||||||||||||||
| 941 | |||||||||||||||||||||||||||||||||
| 942 | The following are a set of shortcut methods for getting commonly used lists of | ||||||||||||||||||||||||||||||||
| 943 | fields and constraints. They all return lists or array refs of Field or | ||||||||||||||||||||||||||||||||
| 944 | Constraint objects. | ||||||||||||||||||||||||||||||||
| 945 | |||||||||||||||||||||||||||||||||
| 946 | =over 4 | ||||||||||||||||||||||||||||||||
| 947 | |||||||||||||||||||||||||||||||||
| 948 | =item pkey_fields | ||||||||||||||||||||||||||||||||
| 949 | |||||||||||||||||||||||||||||||||
| 950 | The primary key fields. | ||||||||||||||||||||||||||||||||
| 951 | |||||||||||||||||||||||||||||||||
| 952 | =item fkey_fields | ||||||||||||||||||||||||||||||||
| 953 | |||||||||||||||||||||||||||||||||
| 954 | All foreign key fields. | ||||||||||||||||||||||||||||||||
| 955 | |||||||||||||||||||||||||||||||||
| 956 | =item nonpkey_fields | ||||||||||||||||||||||||||||||||
| 957 | |||||||||||||||||||||||||||||||||
| 958 | All the fields except the primary key. | ||||||||||||||||||||||||||||||||
| 959 | |||||||||||||||||||||||||||||||||
| 960 | =item data_fields | ||||||||||||||||||||||||||||||||
| 961 | |||||||||||||||||||||||||||||||||
| 962 | All non key fields. | ||||||||||||||||||||||||||||||||
| 963 | |||||||||||||||||||||||||||||||||
| 964 | =item unique_fields | ||||||||||||||||||||||||||||||||
| 965 | |||||||||||||||||||||||||||||||||
| 966 | All fields with unique constraints. | ||||||||||||||||||||||||||||||||
| 967 | |||||||||||||||||||||||||||||||||
| 968 | =item unique_constraints | ||||||||||||||||||||||||||||||||
| 969 | |||||||||||||||||||||||||||||||||
| 970 | All this tables unique constraints. | ||||||||||||||||||||||||||||||||
| 971 | |||||||||||||||||||||||||||||||||
| 972 | =item fkey_constraints | ||||||||||||||||||||||||||||||||
| 973 | |||||||||||||||||||||||||||||||||
| 974 | All this tables foreign key constraints. (See primary_key method to get the | ||||||||||||||||||||||||||||||||
| 975 | primary key constraint) | ||||||||||||||||||||||||||||||||
| 976 | |||||||||||||||||||||||||||||||||
| 977 | =back | ||||||||||||||||||||||||||||||||
| 978 | |||||||||||||||||||||||||||||||||
| 979 | =cut | ||||||||||||||||||||||||||||||||
| 980 | |||||||||||||||||||||||||||||||||
| 981 | sub pkey_fields { | ||||||||||||||||||||||||||||||||
| 982 | 3 | ▶ | my $me = shift; | ||||||||||||||||||||||||||||||
|
Subroutine
pkey_fields: called CC=2 SCAR=7.0
Pod
documented
| |||||||||||||||||||||||||||||||||
| 983 | 3 | my @fields = grep { $_->is_primary_key } $me->get_fields; | |||||||||||||||||||||||||||||||
| 984 | 3 | ▶ | return wantarray ? @fields : \@fields; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 985 | } | ||||||||||||||||||||||||||||||||
| 986 | |||||||||||||||||||||||||||||||||
| 987 | sub fkey_fields { | ||||||||||||||||||||||||||||||||
| 988 | 1 | ▶ | my $me = shift; | ||||||||||||||||||||||||||||||
|
Subroutine
fkey_fields: called CC=3 SCAR=11.1
Pod
documented
| |||||||||||||||||||||||||||||||||
| 989 | 1 | my @fields; | |||||||||||||||||||||||||||||||
| 990 | 1 | push @fields, $_->fields foreach $me->fkey_constraints; | |||||||||||||||||||||||||||||||
| 991 | 1 | ▶ | return wantarray ? @fields : \@fields; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 992 | } | ||||||||||||||||||||||||||||||||
| 993 | |||||||||||||||||||||||||||||||||
| 994 | sub nonpkey_fields { | ||||||||||||||||||||||||||||||||
| 995 | 1 | ▶ | my $me = shift; | ||||||||||||||||||||||||||||||
|
Subroutine
nonpkey_fields: called CC=2 SCAR=7.0
Pod
documented
| |||||||||||||||||||||||||||||||||
| 996 | 1 | my @fields = grep { !$_->is_primary_key } $me->get_fields; | |||||||||||||||||||||||||||||||
| 997 | 1 | ▶ | return wantarray ? @fields : \@fields; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 998 | } | ||||||||||||||||||||||||||||||||
| 999 | |||||||||||||||||||||||||||||||||
| 1000 | sub data_fields { | ||||||||||||||||||||||||||||||||
| 1001 | 3 | ▶ | my $me = shift; | ||||||||||||||||||||||||||||||
|
Subroutine
data_fields: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||||||||||||||
| 1002 | my @fields = | ||||||||||||||||||||||||||||||||
| 1003 | 3 | ▶ | grep { !$_->is_foreign_key and !$_->is_primary_key } $me->get_fields; | ||||||||||||||||||||||||||||||
|
Condition100%3/3
$_->is_foreign_key || $_->is_primary_key
MC/DC100%2/2
$_->is_foreign_key || $_->is_primary_key
$_->is_foreign_key
$_->is_primary_key
Truth table100%3/3
A || B
A= $_->is_foreign_keyB= $_->is_primary_key
| |||||||||||||||||||||||||||||||||
| 1004 | 3 | ▶ | return wantarray ? @fields : \@fields; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 1005 | } | ||||||||||||||||||||||||||||||||
| 1006 | |||||||||||||||||||||||||||||||||
| 1007 | sub unique_fields { | ||||||||||||||||||||||||||||||||
| 1008 | 1 | ▶ | my $me = shift; | ||||||||||||||||||||||||||||||
|
Subroutine
unique_fields: called CC=3 SCAR=11.1
Pod
documented
| |||||||||||||||||||||||||||||||||
| 1009 | 1 | my @fields; | |||||||||||||||||||||||||||||||
| 1010 | 1 | push @fields, $_->fields foreach $me->unique_constraints; | |||||||||||||||||||||||||||||||
| 1011 | 1 | ▶ | return wantarray ? @fields : \@fields; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 1012 | } | ||||||||||||||||||||||||||||||||
| 1013 | |||||||||||||||||||||||||||||||||
| 1014 | sub unique_constraints { | ||||||||||||||||||||||||||||||||
| 1015 | 2 | ▶ | my $me = shift; | ||||||||||||||||||||||||||||||
|
Subroutine
unique_constraints: called CC=2 SCAR=7.0
Pod
documented
| |||||||||||||||||||||||||||||||||
| 1016 | 2 | my @cons = grep { $_->type eq UNIQUE } $me->get_constraints; | |||||||||||||||||||||||||||||||
| 1017 | 2 | ▶ | return wantarray ? @cons : \@cons; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 1018 | } | ||||||||||||||||||||||||||||||||
| 1019 | |||||||||||||||||||||||||||||||||
| 1020 | sub fkey_constraints { | ||||||||||||||||||||||||||||||||
| 1021 | 2 | ▶ | my $me = shift; | ||||||||||||||||||||||||||||||
|
Subroutine
fkey_constraints: called CC=2 SCAR=7.0
Pod
documented
| |||||||||||||||||||||||||||||||||
| 1022 | 2 | my @cons = grep { $_->type eq FOREIGN_KEY } $me->get_constraints; | |||||||||||||||||||||||||||||||
| 1023 | 2 | ▶ | return wantarray ? @cons : \@cons; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 1024 | } | ||||||||||||||||||||||||||||||||
| 1025 | |||||||||||||||||||||||||||||||||
| 1026 | # Must come after all 'has' declarations | ||||||||||||||||||||||||||||||||
| 1027 | 75 | around new => \&ex2err; | |||||||||||||||||||||||||||||||
| 1028 | |||||||||||||||||||||||||||||||||
| 1029 | 75 | 1; | |||||||||||||||||||||||||||||||
| 1030 | |||||||||||||||||||||||||||||||||
| 1031 | =pod | ||||||||||||||||||||||||||||||||
| 1032 | |||||||||||||||||||||||||||||||||
| 1033 | =head1 AUTHORS | ||||||||||||||||||||||||||||||||
| 1034 | |||||||||||||||||||||||||||||||||
| 1035 | Ken Youens-Clark E<lt>kclark@cpan.orgE<gt>, | ||||||||||||||||||||||||||||||||
| 1036 | Allen Day E<lt>allenday@ucla.eduE<gt>. | ||||||||||||||||||||||||||||||||
| 1037 | |||||||||||||||||||||||||||||||||
| 1038 | =cut | ||||||||||||||||||||||||||||||||