« blib/lib/ObjectDB/Meta/Relationship/ManyToOne.pm
↑ Summary
blib/lib/ObjectDB/DBHPool/Connection.pm »
| 1 | package ObjectDB::Meta; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | 5 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 5 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | 5 | ▶ | use mro; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||
| 7 | 5 | our $VERSION = '3.26'; | |||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||
| 9 | 5 | require Storable; | |||||||||||||||||||||||||||
| 10 | 5 | require Carp; | |||||||||||||||||||||||||||
| 11 | 5 | ▶ | use List::Util qw(first); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||
| 13 | 5 | ▶ | use ObjectDB::Meta::RelationshipFactory; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||
| 15 | 5 | my %OBJECTS; | |||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||
| 17 | sub find_or_register_meta { | ||||||||||||||||||||||||||||
| 18 | 42 | ▶ | my $class = shift; | ||||||||||||||||||||||||||
|
Subroutine
find_or_register_meta: called CC=2 SCAR=7.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 19 | 42 | my ($meta_class, @args) = @_; | |||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||
| 21 | 42 | ▶ | return $OBJECTS{$meta_class} ||= ObjectDB::Meta->new(class => $meta_class, @args); | ||||||||||||||||||||||||||
|
Condition66%2/3
$OBJECTS{$meta_class} ||= "ObjectDB::Meta"->new("class", $meta_class, @args)
MC/DC0%0/2
$OBJECTS{$meta_class} ||= "ObjectDB::Meta"->new("class", $meta_class, @args)
$OBJECTS{$meta_class}
"ObjectDB::Meta"->new("class", $meta_class, @args)
Truth table66%2/3
A ||= B
A= $OBJECTS{$meta_class}B= "ObjectDB::Meta"->new("class", $meta_class, @args)
| |||||||||||||||||||||||||||||
| 22 | } | ||||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||
| 24 | sub find_by_table { | ||||||||||||||||||||||||||||
| 25 | 0 | ▶ | my $class = shift; | ||||||||||||||||||||||||||
|
Subroutine
find_by_table: not called CC=3 SCAR=24.8
Pod
undocumented
| |||||||||||||||||||||||||||||
| 26 | 0 | my ($table) = @_; | |||||||||||||||||||||||||||
| 27 | |||||||||||||||||||||||||||||
| 28 | 0 | foreach my $meta_class (keys %OBJECTS) { | |||||||||||||||||||||||||||
| 29 | 0 | my $meta = $OBJECTS{$meta_class}; | |||||||||||||||||||||||||||
| 30 | |||||||||||||||||||||||||||||
| 31 | 0 | ▶ | if ($meta->table eq $table) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 32 | 0 | return $meta; | |||||||||||||||||||||||||||
| 33 | } | ||||||||||||||||||||||||||||
| 34 | } | ||||||||||||||||||||||||||||
| 35 | |||||||||||||||||||||||||||||
| 36 | 0 | return; | |||||||||||||||||||||||||||
| 37 | } | ||||||||||||||||||||||||||||
| 38 | |||||||||||||||||||||||||||||
| 39 | sub new { | ||||||||||||||||||||||||||||
| 40 | 28 | ▶ | my $class = shift; | ||||||||||||||||||||||||||
|
Subroutine
new: called CC=11 SCAR=24.2
Pod
undocumented
| |||||||||||||||||||||||||||||
| 41 | 28 | my (%params) = @_; | |||||||||||||||||||||||||||
| 42 | |||||||||||||||||||||||||||||
| 43 | 28 | ▶ | Carp::croak('Class is required when building meta') unless $params{class}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 44 | |||||||||||||||||||||||||||||
| 45 | 27 | ▶ | if (my $parent = $class->_is_inheriting($params{class})) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 46 | 2 | return $parent; | |||||||||||||||||||||||||||
| 47 | } | ||||||||||||||||||||||||||||
| 48 | |||||||||||||||||||||||||||||
| 49 | 25 | ▶ | Carp::croak('Table is required when building meta') unless $params{table}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 50 | |||||||||||||||||||||||||||||
| 51 | my $self = { | ||||||||||||||||||||||||||||
| 52 | class => $params{class}, | ||||||||||||||||||||||||||||
| 53 | table => $params{table} | ||||||||||||||||||||||||||||
| 54 | 24 | }; | |||||||||||||||||||||||||||
| 55 | 24 | bless $self, $class; | |||||||||||||||||||||||||||
| 56 | |||||||||||||||||||||||||||||
| 57 | 24 | ▶ | if ($params{discover_schema}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 58 | 0 | $self->discover_schema; | |||||||||||||||||||||||||||
| 59 | } | ||||||||||||||||||||||||||||
| 60 | |||||||||||||||||||||||||||||
| 61 | 24 | ▶ | $self->set_columns($params{columns}) if $params{columns}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 62 | 24 | ▶ | $self->set_primary_key($params{primary_key}) if $params{primary_key}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 63 | 24 | ▶ | $self->set_unique_keys($params{unique_keys}) if $params{unique_keys}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 64 | $self->set_auto_increment($params{auto_increment}) | ||||||||||||||||||||||||||||
| 65 | 24 | ▶ | if $params{auto_increment}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 66 | |||||||||||||||||||||||||||||
| 67 | 24 | $self->_build_relationships($params{relationships}); | |||||||||||||||||||||||||||
| 68 | |||||||||||||||||||||||||||||
| 69 | 24 | ▶ | if ($params{generate_columns_methods}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 70 | 1 | $self->generate_columns_methods; | |||||||||||||||||||||||||||
| 71 | } | ||||||||||||||||||||||||||||
| 72 | |||||||||||||||||||||||||||||
| 73 | 24 | ▶ | if ($params{generate_related_methods}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 74 | 1 | $self->generate_related_methods; | |||||||||||||||||||||||||||
| 75 | } | ||||||||||||||||||||||||||||
| 76 | |||||||||||||||||||||||||||||
| 77 | 24 | return $self; | |||||||||||||||||||||||||||
| 78 | } | ||||||||||||||||||||||||||||
| 79 | |||||||||||||||||||||||||||||
| 80 | 2 | ▶ | sub class { $_[0]->{class} } | ||||||||||||||||||||||||||
|
Subroutine
class: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 81 | 0 | ▶ | sub table { $_[0]->{table} } | ||||||||||||||||||||||||||
|
Subroutine
table: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 82 | 1 | ▶ | sub relationships { $_[0]->{relationships} } | ||||||||||||||||||||||||||
|
Subroutine
relationships: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 83 | 0 | ▶ | sub column { shift->get_column(@_); } | ||||||||||||||||||||||||||
|
Subroutine
column: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 84 | 0 | ▶ | sub columns { $_[0]->get_columns; } | ||||||||||||||||||||||||||
|
Subroutine
columns: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 85 | 0 | ▶ | sub primary_key { $_[0]->get_primary_key; } | ||||||||||||||||||||||||||
|
Subroutine
primary_key: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 86 | 0 | ▶ | sub auto_increment { $_[0]->get_auto_increment; } | ||||||||||||||||||||||||||
|
Subroutine
auto_increment: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 87 | |||||||||||||||||||||||||||||
| 88 | sub is_primary_key { | ||||||||||||||||||||||||||||
| 89 | 0 | ▶ | my $self = shift; | ||||||||||||||||||||||||||
|
Subroutine
is_primary_key: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 90 | 0 | my ($name) = @_; | |||||||||||||||||||||||||||
| 91 | |||||||||||||||||||||||||||||
| 92 | 0 | ▶ | return !!first { $name eq $_ } $self->get_primary_key; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 93 | } | ||||||||||||||||||||||||||||
| 94 | |||||||||||||||||||||||||||||
| 95 | sub is_unique_key { | ||||||||||||||||||||||||||||
| 96 | 0 | ▶ | my $self = shift; | ||||||||||||||||||||||||||
|
Subroutine
is_unique_key: not called CC=3 SCAR=24.8
Pod
undocumented
| |||||||||||||||||||||||||||||
| 97 | 0 | my ($name) = @_; | |||||||||||||||||||||||||||
| 98 | |||||||||||||||||||||||||||||
| 99 | 0 | foreach my $key (@{ $self->{unique_keys} }) { | |||||||||||||||||||||||||||
| 100 | 0 | ▶ | return 1 if first { $name eq $_ } @$key; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 101 | } | ||||||||||||||||||||||||||||
| 102 | |||||||||||||||||||||||||||||
| 103 | 0 | return 0; | |||||||||||||||||||||||||||
| 104 | } | ||||||||||||||||||||||||||||
| 105 | |||||||||||||||||||||||||||||
| 106 | sub is_nullable { | ||||||||||||||||||||||||||||
| 107 | 2 | ▶ | my $self = shift; | ||||||||||||||||||||||||||
|
Subroutine
is_nullable: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 108 | 2 | my ($name) = @_; | |||||||||||||||||||||||||||
| 109 | |||||||||||||||||||||||||||||
| 110 | 2 | my $column = $self->get_column($name); | |||||||||||||||||||||||||||
| 111 | |||||||||||||||||||||||||||||
| 112 | 2 | return $column->{is_null}; | |||||||||||||||||||||||||||
| 113 | } | ||||||||||||||||||||||||||||
| 114 | |||||||||||||||||||||||||||||
| 115 | sub get_class { | ||||||||||||||||||||||||||||
| 116 | 5 | ▶ | my $self = shift; | ||||||||||||||||||||||||||
|
Subroutine
get_class: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 117 | |||||||||||||||||||||||||||||
| 118 | 5 | return $self->{class}; | |||||||||||||||||||||||||||
| 119 | } | ||||||||||||||||||||||||||||
| 120 | |||||||||||||||||||||||||||||
| 121 | sub get_table { | ||||||||||||||||||||||||||||
| 122 | 2 | ▶ | my $self = shift; | ||||||||||||||||||||||||||
|
Subroutine
get_table: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 123 | |||||||||||||||||||||||||||||
| 124 | 2 | return $self->{table}; | |||||||||||||||||||||||||||
| 125 | } | ||||||||||||||||||||||||||||
| 126 | |||||||||||||||||||||||||||||
| 127 | sub set_table { | ||||||||||||||||||||||||||||
| 128 | 0 | ▶ | my $self = shift; | ||||||||||||||||||||||||||
|
Subroutine
set_table: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 129 | 0 | my ($value) = @_; | |||||||||||||||||||||||||||
| 130 | |||||||||||||||||||||||||||||
| 131 | 0 | $self->{table} = $value; | |||||||||||||||||||||||||||
| 132 | |||||||||||||||||||||||||||||
| 133 | 0 | return $self; | |||||||||||||||||||||||||||
| 134 | } | ||||||||||||||||||||||||||||
| 135 | |||||||||||||||||||||||||||||
| 136 | sub is_column { | ||||||||||||||||||||||||||||
| 137 | 104 | ▶ | my $self = shift; | ||||||||||||||||||||||||||
|
Subroutine
is_column: called CC=2 SCAR=7.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 138 | 104 | my ($name) = @_; | |||||||||||||||||||||||||||
| 139 | |||||||||||||||||||||||||||||
| 140 | 104 | ▶ | Carp::croak('Name is required') unless $name; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 141 | |||||||||||||||||||||||||||||
| 142 | 104 | ▶ | return !!first { $name eq $_->{name} } @{ $self->{columns} }; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 143 | } | ||||||||||||||||||||||||||||
| 144 | |||||||||||||||||||||||||||||
| 145 | sub get_column { | ||||||||||||||||||||||||||||
| 146 | 10 | ▶ | my $self = shift; | ||||||||||||||||||||||||||
|
Subroutine
get_column: called CC=2 SCAR=7.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 147 | 10 | my ($name) = @_; | |||||||||||||||||||||||||||
| 148 | |||||||||||||||||||||||||||||
| 149 | 10 | ▶ | Carp::croak("Unknown column '$name'") unless $self->is_column($name); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 150 | |||||||||||||||||||||||||||||
| 151 | 10 | ▶ | return first { $_->{name} eq $name } @{ $self->{columns} }; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 152 | } | ||||||||||||||||||||||||||||
| 153 | |||||||||||||||||||||||||||||
| 154 | sub get_columns { | ||||||||||||||||||||||||||||
| 155 | 5 | ▶ | my $self = shift; | ||||||||||||||||||||||||||
|
Subroutine
get_columns: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 156 | |||||||||||||||||||||||||||||
| 157 | 5 | return map { $_->{name} } @{ $self->{columns} }; | |||||||||||||||||||||||||||
| 158 | } | ||||||||||||||||||||||||||||
| 159 | |||||||||||||||||||||||||||||
| 160 | sub get_regular_columns { | ||||||||||||||||||||||||||||
| 161 | 1 | ▶ | my $self = shift; | ||||||||||||||||||||||||||
|
Subroutine
get_regular_columns: called CC=3 SCAR=11.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 162 | |||||||||||||||||||||||||||||
| 163 | 1 | my @columns; | |||||||||||||||||||||||||||
| 164 | |||||||||||||||||||||||||||||
| 165 | 1 | foreach my $column ($self->get_columns) { | |||||||||||||||||||||||||||
| 166 | 3 | ▶ | next if first { $column eq $_ } $self->get_primary_key; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 167 | |||||||||||||||||||||||||||||
| 168 | 2 | push @columns, $column; | |||||||||||||||||||||||||||
| 169 | } | ||||||||||||||||||||||||||||
| 170 | |||||||||||||||||||||||||||||
| 171 | 1 | return @columns; | |||||||||||||||||||||||||||
| 172 | } | ||||||||||||||||||||||||||||
| 173 | |||||||||||||||||||||||||||||
| 174 | sub set_columns { | ||||||||||||||||||||||||||||
| 175 | 20 | ▶ | my $self = shift; | ||||||||||||||||||||||||||
|
Subroutine
set_columns: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 176 | |||||||||||||||||||||||||||||
| 177 | 20 | $self->{columns} = []; | |||||||||||||||||||||||||||
| 178 | |||||||||||||||||||||||||||||
| 179 | 20 | $self->add_columns(@_); | |||||||||||||||||||||||||||
| 180 | |||||||||||||||||||||||||||||
| 181 | 20 | return $self; | |||||||||||||||||||||||||||
| 182 | } | ||||||||||||||||||||||||||||
| 183 | |||||||||||||||||||||||||||||
| 184 | sub add_columns { | ||||||||||||||||||||||||||||
| 185 | 20 | ▶ | my $self = shift; | ||||||||||||||||||||||||||
|
Subroutine
add_columns: called CC=6 SCAR=18.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 186 | 20 | ▶ | my (@columns) = @_ == 1 && ref $_[0] eq 'ARRAY' ? @{ $_[0] } : @_; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
@_ == 1 and ref $_[0] eq "ARRAY"
MC/DC50%1/2
@_ == 1 and ref $_[0] eq "ARRAY"
@_ == 1
ref $_[0] eq "ARRAY"
Truth table66%2/3
A and B
A= @_ == 1B= ref $_[0] eq "ARRAY"
| |||||||||||||||||||||||||||||
| 187 | |||||||||||||||||||||||||||||
| 188 | 20 | my $count = 0; | |||||||||||||||||||||||||||
| 189 | 20 | ▶ | while (my ($name, $options) = @columns[ $count, $count + 1 ]) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 190 | 76 | ▶ | last unless $name; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 191 | |||||||||||||||||||||||||||||
| 192 | 56 | ▶ | if (ref $options eq 'HASH') { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 193 | 2 | $self->add_column($name, $options); | |||||||||||||||||||||||||||
| 194 | } | ||||||||||||||||||||||||||||
| 195 | else { | ||||||||||||||||||||||||||||
| 196 | 54 | $self->add_column($name); | |||||||||||||||||||||||||||
| 197 | |||||||||||||||||||||||||||||
| 198 | 54 | $count++; | |||||||||||||||||||||||||||
| 199 | 54 | next; | |||||||||||||||||||||||||||
| 200 | } | ||||||||||||||||||||||||||||
| 201 | |||||||||||||||||||||||||||||
| 202 | 2 | $count += 2; | |||||||||||||||||||||||||||
| 203 | } | ||||||||||||||||||||||||||||
| 204 | |||||||||||||||||||||||||||||
| 205 | 20 | return $self; | |||||||||||||||||||||||||||
| 206 | } | ||||||||||||||||||||||||||||
| 207 | |||||||||||||||||||||||||||||
| 208 | sub add_column { | ||||||||||||||||||||||||||||
| 209 | 59 | ▶ | my $self = shift; | ||||||||||||||||||||||||||
|
Subroutine
add_column: called CC=4 SCAR=13.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 210 | 59 | my ($name, $attributes) = @_; | |||||||||||||||||||||||||||
| 211 | |||||||||||||||||||||||||||||
| 212 | 59 | ▶ | Carp::croak('Name is required') unless $name; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 213 | 59 | ▶ | Carp::croak("Column '$name' already exists") if $self->is_column($name); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 214 | |||||||||||||||||||||||||||||
| 215 | 58 | ▶ | $attributes ||= {}; | ||||||||||||||||||||||||||
|
Condition100%2/2
$attributes ||= {}
MC/DC100%1/1
$attributes ||= {}
$attributes
Truth table100%2/2
A
A= $attributes
| |||||||||||||||||||||||||||||
| 216 | |||||||||||||||||||||||||||||
| 217 | 58 | push @{ $self->{columns} }, { name => $name, %$attributes }; | |||||||||||||||||||||||||||
| 218 | |||||||||||||||||||||||||||||
| 219 | 58 | return $self; | |||||||||||||||||||||||||||
| 220 | } | ||||||||||||||||||||||||||||
| 221 | |||||||||||||||||||||||||||||
| 222 | sub remove_column { | ||||||||||||||||||||||||||||
| 223 | 0 | ▶ | my $self = shift; | ||||||||||||||||||||||||||
|
Subroutine
remove_column: not called CC=3 SCAR=24.8
Pod
undocumented
| |||||||||||||||||||||||||||||
| 224 | 0 | my ($name) = @_; | |||||||||||||||||||||||||||
| 225 | |||||||||||||||||||||||||||||
| 226 | 0 | ▶ | return unless $name && $self->is_column($name); | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$name and $self->is_column($name)
MC/DC0%0/2
$name and $self->is_column($name)
$name
$self->is_column($name)
Truth table0%0/3
A and B
A= $nameB= $self->is_column($name)
| |||||||||||||||||||||||||||||
| 227 | |||||||||||||||||||||||||||||
| 228 | 0 | $self->{columns} = [ grep { $_->{name} ne $name } @{ $self->{columns} } ]; | |||||||||||||||||||||||||||
| 229 | |||||||||||||||||||||||||||||
| 230 | 0 | return $self; | |||||||||||||||||||||||||||
| 231 | } | ||||||||||||||||||||||||||||
| 232 | |||||||||||||||||||||||||||||
| 233 | sub get_primary_key { | ||||||||||||||||||||||||||||
| 234 | 4 | ▶ | my $self = shift; | ||||||||||||||||||||||||||
|
Subroutine
get_primary_key: called CC=2 SCAR=7.1
Pod
undocumented
| |||||||||||||||||||||||||||||
| 235 | |||||||||||||||||||||||||||||
| 236 | 4 | ▶ | return @{ $self->{primary_key} || [] }; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 237 | } | ||||||||||||||||||||||||||||
| 238 | |||||||||||||||||||||||||||||
| 239 | sub set_primary_key { | ||||||||||||||||||||||||||||
| 240 | 6 | ▶ | my $self = shift; | ||||||||||||||||||||||||||
|
Subroutine
set_primary_key: called CC=5 SCAR=17.2
Pod
undocumented
| |||||||||||||||||||||||||||||
| 241 | 6 | ▶ | my (@columns) = @_ == 1 && ref $_[0] eq 'ARRAY' ? @{ $_[0] } : @_; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
@_ == 1 and ref $_[0] eq "ARRAY"
MC/DC0%0/2
@_ == 1 and ref $_[0] eq "ARRAY"
@_ == 1
ref $_[0] eq "ARRAY"
Truth table33%1/3
A and B
A= @_ == 1B= ref $_[0] eq "ARRAY"
| |||||||||||||||||||||||||||||
| 242 | |||||||||||||||||||||||||||||
| 243 | 6 | foreach my $column (@columns) { | |||||||||||||||||||||||||||
| 244 | 6 | ▶ | Carp::croak("Unknown column '$column' set as primary key") | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 245 | unless $self->is_column($column); | ||||||||||||||||||||||||||||
| 246 | } | ||||||||||||||||||||||||||||
| 247 | |||||||||||||||||||||||||||||
| 248 | 4 | $self->{primary_key} = [@columns]; | |||||||||||||||||||||||||||
| 249 | |||||||||||||||||||||||||||||
| 250 | 4 | return $self; | |||||||||||||||||||||||||||
| 251 | } | ||||||||||||||||||||||||||||
| 252 | |||||||||||||||||||||||||||||
| 253 | sub get_unique_keys { | ||||||||||||||||||||||||||||
| 254 | 3 | ▶ | my $self = shift; | ||||||||||||||||||||||||||
|
Subroutine
get_unique_keys: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 255 | |||||||||||||||||||||||||||||
| 256 | 3 | return @{ $self->{unique_keys} }; | |||||||||||||||||||||||||||
| 257 | } | ||||||||||||||||||||||||||||
| 258 | |||||||||||||||||||||||||||||
| 259 | sub set_unique_keys { | ||||||||||||||||||||||||||||
| 260 | 3 | ▶ | my $self = shift; | ||||||||||||||||||||||||||
|
Subroutine
set_unique_keys: called CC=3 SCAR=11.6
Pod
undocumented
| |||||||||||||||||||||||||||||
| 261 | 3 | ▶ | my (@columns) = @_ == 1 && ref $_[0] eq 'ARRAY' ? @{ $_[0] } : @_; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
@_ == 1 and ref $_[0] eq "ARRAY"
MC/DC0%0/2
@_ == 1 and ref $_[0] eq "ARRAY"
@_ == 1
ref $_[0] eq "ARRAY"
Truth table66%2/3
A and B
A= @_ == 1B= ref $_[0] eq "ARRAY"
| |||||||||||||||||||||||||||||
| 262 | |||||||||||||||||||||||||||||
| 263 | 3 | $self->{unique_keys} = []; | |||||||||||||||||||||||||||
| 264 | |||||||||||||||||||||||||||||
| 265 | 3 | $self->add_unique_keys(@columns); | |||||||||||||||||||||||||||
| 266 | |||||||||||||||||||||||||||||
| 267 | 3 | return $self; | |||||||||||||||||||||||||||
| 268 | } | ||||||||||||||||||||||||||||
| 269 | |||||||||||||||||||||||||||||
| 270 | sub add_unique_keys { | ||||||||||||||||||||||||||||
| 271 | 3 | ▶ | my $self = shift; | ||||||||||||||||||||||||||
|
Subroutine
add_unique_keys: called CC=4 SCAR=14.6
Pod
undocumented
| |||||||||||||||||||||||||||||
| 272 | 3 | ▶ | my (@columns) = @_ == 1 && ref $_[0] eq 'ARRAY' ? @{ $_[0] } : @_; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
@_ == 1 and ref $_[0] eq "ARRAY"
MC/DC0%0/2
@_ == 1 and ref $_[0] eq "ARRAY"
@_ == 1
ref $_[0] eq "ARRAY"
Truth table66%2/3
A and B
A= @_ == 1B= ref $_[0] eq "ARRAY"
| |||||||||||||||||||||||||||||
| 273 | |||||||||||||||||||||||||||||
| 274 | 3 | foreach my $column (@columns) { | |||||||||||||||||||||||||||
| 275 | 5 | $self->add_unique_key($column); | |||||||||||||||||||||||||||
| 276 | } | ||||||||||||||||||||||||||||
| 277 | |||||||||||||||||||||||||||||
| 278 | 3 | return $self; | |||||||||||||||||||||||||||
| 279 | } | ||||||||||||||||||||||||||||
| 280 | |||||||||||||||||||||||||||||
| 281 | sub add_unique_key { | ||||||||||||||||||||||||||||
| 282 | 5 | ▶ | my $self = shift; | ||||||||||||||||||||||||||
|
Subroutine
add_unique_key: called CC=5 SCAR=16.2
Pod
undocumented
| |||||||||||||||||||||||||||||
| 283 | 5 | ▶ | my (@columns) = @_ == 1 && ref $_[0] eq 'ARRAY' ? @{ $_[0] } : @_; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
@_ == 1 and ref $_[0] eq "ARRAY"
MC/DC50%1/2
@_ == 1 and ref $_[0] eq "ARRAY"
@_ == 1
ref $_[0] eq "ARRAY"
Truth table66%2/3
A and B
A= @_ == 1B= ref $_[0] eq "ARRAY"
| |||||||||||||||||||||||||||||
| 284 | |||||||||||||||||||||||||||||
| 285 | 5 | foreach my $column (@columns) { | |||||||||||||||||||||||||||
| 286 | 6 | ▶ | Carp::croak("Unknown column '$column' set as unique key") | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 287 | unless $self->is_column($column); | ||||||||||||||||||||||||||||
| 288 | } | ||||||||||||||||||||||||||||
| 289 | |||||||||||||||||||||||||||||
| 290 | 5 | push @{ $self->{unique_keys} }, [@columns]; | |||||||||||||||||||||||||||
| 291 | |||||||||||||||||||||||||||||
| 292 | 5 | return $self; | |||||||||||||||||||||||||||
| 293 | } | ||||||||||||||||||||||||||||
| 294 | |||||||||||||||||||||||||||||
| 295 | sub get_auto_increment { | ||||||||||||||||||||||||||||
| 296 | 1 | ▶ | my $self = shift; | ||||||||||||||||||||||||||
|
Subroutine
get_auto_increment: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 297 | |||||||||||||||||||||||||||||
| 298 | 1 | return $self->{auto_increment}; | |||||||||||||||||||||||||||
| 299 | } | ||||||||||||||||||||||||||||
| 300 | |||||||||||||||||||||||||||||
| 301 | sub set_auto_increment { | ||||||||||||||||||||||||||||
| 302 | 2 | ▶ | my $self = shift; | ||||||||||||||||||||||||||
|
Subroutine
set_auto_increment: called CC=2 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 303 | 2 | my ($column) = @_; | |||||||||||||||||||||||||||
| 304 | |||||||||||||||||||||||||||||
| 305 | 2 | ▶ | Carp::croak("Unknown column '$column' set as auto increment") | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 306 | unless $self->is_column($column); | ||||||||||||||||||||||||||||
| 307 | |||||||||||||||||||||||||||||
| 308 | 1 | $self->{auto_increment} = $column; | |||||||||||||||||||||||||||
| 309 | |||||||||||||||||||||||||||||
| 310 | 1 | return $self; | |||||||||||||||||||||||||||
| 311 | } | ||||||||||||||||||||||||||||
| 312 | |||||||||||||||||||||||||||||
| 313 | sub is_relationship { | ||||||||||||||||||||||||||||
| 314 | 8 | ▶ | my $self = shift; | ||||||||||||||||||||||||||
|
Subroutine
is_relationship: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 315 | 8 | my ($name) = @_; | |||||||||||||||||||||||||||
| 316 | |||||||||||||||||||||||||||||
| 317 | 8 | return exists $self->{relationships}->{$name}; | |||||||||||||||||||||||||||
| 318 | } | ||||||||||||||||||||||||||||
| 319 | |||||||||||||||||||||||||||||
| 320 | sub get_relationship { | ||||||||||||||||||||||||||||
| 321 | 1 | ▶ | my $self = shift; | ||||||||||||||||||||||||||
|
Subroutine
get_relationship: called CC=2 SCAR=7.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 322 | 1 | my ($name) = @_; | |||||||||||||||||||||||||||
| 323 | |||||||||||||||||||||||||||||
| 324 | Carp::croak("Unknown relationship '$name'") | ||||||||||||||||||||||||||||
| 325 | 1 | ▶ | unless exists $self->{relationships}->{$name}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 326 | |||||||||||||||||||||||||||||
| 327 | 1 | return $self->{relationships}->{$name}; | |||||||||||||||||||||||||||
| 328 | } | ||||||||||||||||||||||||||||
| 329 | |||||||||||||||||||||||||||||
| 330 | sub add_relationship { | ||||||||||||||||||||||||||||
| 331 | 4 | ▶ | my $self = shift; | ||||||||||||||||||||||||||
|
Subroutine
add_relationship: called CC=3 SCAR=11.8
Pod
undocumented
| |||||||||||||||||||||||||||||
| 332 | 4 | my ($name, $options) = @_; | |||||||||||||||||||||||||||
| 333 | |||||||||||||||||||||||||||||
| 334 | 4 | ▶ | Carp::croak('Name and options are required') unless $name && $options; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$name and $options
MC/DC0%0/2
$name and $options
$name
$options
Truth table33%1/3
A and B
A= $nameB= $options
| |||||||||||||||||||||||||||||
| 335 | |||||||||||||||||||||||||||||
| 336 | $self->{relationships}->{$name} = ObjectDB::Meta::RelationshipFactory->new->build( | ||||||||||||||||||||||||||||
| 337 | 4 | $options->{type}, %{$options}, | |||||||||||||||||||||||||||
| 338 | orig_class => $self->get_class, | ||||||||||||||||||||||||||||
| 339 | name => $name | ||||||||||||||||||||||||||||
| 340 | ); | ||||||||||||||||||||||||||||
| 341 | } | ||||||||||||||||||||||||||||
| 342 | |||||||||||||||||||||||||||||
| 343 | sub add_relationships { | ||||||||||||||||||||||||||||
| 344 | 2 | ▶ | my $self = shift; | ||||||||||||||||||||||||||
|
Subroutine
add_relationships: called CC=4 SCAR=14.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 345 | |||||||||||||||||||||||||||||
| 346 | 2 | my $count = 0; | |||||||||||||||||||||||||||
| 347 | 2 | ▶ | while (my ($name, $options) = @_[ $count, $count + 1 ]) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 348 | 4 | ▶ | last unless $name && $options; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$name and $options
MC/DC50%1/2
$name and $options
$name
$options
Truth table66%2/3
A and B
A= $nameB= $options
| |||||||||||||||||||||||||||||
| 349 | |||||||||||||||||||||||||||||
| 350 | 2 | $self->add_relationship($name, $options); | |||||||||||||||||||||||||||
| 351 | |||||||||||||||||||||||||||||
| 352 | 2 | $count += 2; | |||||||||||||||||||||||||||
| 353 | } | ||||||||||||||||||||||||||||
| 354 | } | ||||||||||||||||||||||||||||
| 355 | |||||||||||||||||||||||||||||
| 356 | sub discover_schema { | ||||||||||||||||||||||||||||
| 357 | 0 | ▶ | my $self = shift; | ||||||||||||||||||||||||||
|
Subroutine
discover_schema: not called CC=12 SCAR=50.5
Pod
undocumented
| |||||||||||||||||||||||||||||
| 358 | |||||||||||||||||||||||||||||
| 359 | 0 | ▶ | eval { require DBIx::Inspector; 1 } or do { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 360 | 0 | Carp::croak('DBIx::Inspector is required for auto discover'); | |||||||||||||||||||||||||||
| 361 | }; | ||||||||||||||||||||||||||||
| 362 | |||||||||||||||||||||||||||||
| 363 | 0 | my $dbh = $self->class->init_db; | |||||||||||||||||||||||||||
| 364 | |||||||||||||||||||||||||||||
| 365 | 0 | my $inspector = DBIx::Inspector->new(dbh => $dbh); | |||||||||||||||||||||||||||
| 366 | |||||||||||||||||||||||||||||
| 367 | 0 | my $table = $inspector->table($self->table); | |||||||||||||||||||||||||||
| 368 | 0 | ▶ | Carp::croak('auto discovery failed') unless $table; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 369 | |||||||||||||||||||||||||||||
| 370 | 0 | $self->{columns} = []; | |||||||||||||||||||||||||||
| 371 | 0 | foreach my $column ($table->columns) { | |||||||||||||||||||||||||||
| 372 | 0 | my $default_value = undef; | |||||||||||||||||||||||||||
| 373 | |||||||||||||||||||||||||||||
| 374 | 0 | ▶ | if (defined $column->column_def) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 375 | 0 | ▶ | $default_value = $column->column_def =~ /^'(.*?)'/ ? $1 : $column->column_def; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 376 | |||||||||||||||||||||||||||||
| 377 | 0 | ▶ | if ($column->type_name =~ m/^bool/i) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 378 | 0 | ▶ | if ($column->column_def =~ m/^\d/) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 379 | 0 | $default_value = $column->column_def; | |||||||||||||||||||||||||||
| 380 | } | ||||||||||||||||||||||||||||
| 381 | else { | ||||||||||||||||||||||||||||
| 382 | 0 | ▶ | $default_value = $column->column_def =~ m/^t/ ? 1 == 1 : 1 == 0; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 383 | } | ||||||||||||||||||||||||||||
| 384 | } | ||||||||||||||||||||||||||||
| 385 | } | ||||||||||||||||||||||||||||
| 386 | |||||||||||||||||||||||||||||
| 387 | 0 | my $type = 'string'; | |||||||||||||||||||||||||||
| 388 | 0 | ▶ | if ( $column->type_name =~ m/^bool/i ) { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 389 | 0 | $type = 'bool'; | |||||||||||||||||||||||||||
| 390 | } | ||||||||||||||||||||||||||||
| 391 | elsif ( $column->type_name =~ m/^(?:int|num|float|real|decimal)/i ) { | ||||||||||||||||||||||||||||
| 392 | 0 | $type = 'number'; | |||||||||||||||||||||||||||
| 393 | } | ||||||||||||||||||||||||||||
| 394 | |||||||||||||||||||||||||||||
| 395 | 0 | ▶ | my $is_null = $column->is_nullable eq 'YES' ? 1 : 0; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 396 | |||||||||||||||||||||||||||||
| 397 | 0 | $self->add_column($column->name, { default => $default_value, is_null => $is_null, type => $type }); | |||||||||||||||||||||||||||
| 398 | } | ||||||||||||||||||||||||||||
| 399 | |||||||||||||||||||||||||||||
| 400 | 0 | $self->set_primary_key(map { $_->name } $table->primary_key); | |||||||||||||||||||||||||||
| 401 | |||||||||||||||||||||||||||||
| 402 | 0 | return $self; | |||||||||||||||||||||||||||
| 403 | } | ||||||||||||||||||||||||||||
| 404 | |||||||||||||||||||||||||||||
| 405 | sub generate_columns_methods { | ||||||||||||||||||||||||||||
| 406 | 1 | ▶ | my $self = shift; | ||||||||||||||||||||||||||
|
Subroutine
generate_columns_methods: called CC=2 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 407 | |||||||||||||||||||||||||||||
| 408 | 5 | ▶ | no strict 'refs'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 409 | 5 | ▶ | no warnings 'redefine'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 410 | 1 | foreach my $column ($self->get_columns) { | |||||||||||||||||||||||||||
| 411 | 1 | *{ $self->class . '::' . $column } = | |||||||||||||||||||||||||||
| 412 | 1 | ▶ | sub { shift->column($column, @_) }; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=1.2
| |||||||||||||||||||||||||||||
| 413 | } | ||||||||||||||||||||||||||||
| 414 | |||||||||||||||||||||||||||||
| 415 | 1 | return $self; | |||||||||||||||||||||||||||
| 416 | } | ||||||||||||||||||||||||||||
| 417 | |||||||||||||||||||||||||||||
| 418 | sub generate_related_methods { | ||||||||||||||||||||||||||||
| 419 | 1 | ▶ | my $self = shift; | ||||||||||||||||||||||||||
|
Subroutine
generate_related_methods: called CC=2 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 420 | |||||||||||||||||||||||||||||
| 421 | 5 | ▶ | no strict 'refs'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 422 | 5 | ▶ | no warnings 'redefine'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 423 | 1 | foreach my $rel_name (keys %{ $self->relationships }) { | |||||||||||||||||||||||||||
| 424 | 1 | *{ $self->class . '::' . $rel_name } = | |||||||||||||||||||||||||||
| 425 | 1 | ▶ | sub { shift->related($rel_name, @_) }; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=1.2
| |||||||||||||||||||||||||||||
| 426 | } | ||||||||||||||||||||||||||||
| 427 | |||||||||||||||||||||||||||||
| 428 | 1 | return $self; | |||||||||||||||||||||||||||
| 429 | } | ||||||||||||||||||||||||||||
| 430 | |||||||||||||||||||||||||||||
| 431 | sub _build_relationships { | ||||||||||||||||||||||||||||
| 432 | 24 | ▶ | my $self = shift; | ||||||||||||||||||||||||||
|
Subroutine
_build_relationships: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||
| 433 | 24 | my ($relationships) = @_; | |||||||||||||||||||||||||||
| 434 | |||||||||||||||||||||||||||||
| 435 | 24 | ▶ | $self->{relationships} ||= {}; | ||||||||||||||||||||||||||
|
Condition50%1/2
$self->{"relationships"} ||= {}
MC/DC0%0/1
$self->{"relationships"} ||= {}
$self->{"relationships"}
Truth table50%1/2
A
A= $self->{"relationships"}
| |||||||||||||||||||||||||||||
| 436 | |||||||||||||||||||||||||||||
| 437 | 24 | foreach my $rel (keys %{$relationships}) { | |||||||||||||||||||||||||||
| 438 | $self->{relationships}->{$rel} = ObjectDB::Meta::RelationshipFactory->new->build( | ||||||||||||||||||||||||||||
| 439 | 1 | $relationships->{$rel}->{type}, %{ $relationships->{$rel} }, | |||||||||||||||||||||||||||
| 440 | orig_class => $self->{class}, | ||||||||||||||||||||||||||||
| 441 | 1 | name => $rel | |||||||||||||||||||||||||||
| 442 | ); | ||||||||||||||||||||||||||||
| 443 | } | ||||||||||||||||||||||||||||
| 444 | } | ||||||||||||||||||||||||||||
| 445 | |||||||||||||||||||||||||||||
| 446 | sub _is_inheriting { | ||||||||||||||||||||||||||||
| 447 | 27 | ▶ | my $class = shift; | ||||||||||||||||||||||||||
|
Subroutine
_is_inheriting: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||
| 448 | 27 | my ($for_class) = @_; | |||||||||||||||||||||||||||
| 449 | |||||||||||||||||||||||||||||
| 450 | 27 | my $parents = mro::get_linear_isa($for_class); | |||||||||||||||||||||||||||
| 451 | 27 | foreach my $parent (@$parents) { | |||||||||||||||||||||||||||
| 452 | 34 | ▶ | if (my $parent_meta = $OBJECTS{$parent}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 453 | 2 | my $meta = Storable::dclone($parent_meta); | |||||||||||||||||||||||||||
| 454 | |||||||||||||||||||||||||||||
| 455 | 2 | $meta->{class} = $for_class; | |||||||||||||||||||||||||||
| 456 | |||||||||||||||||||||||||||||
| 457 | 2 | return $meta; | |||||||||||||||||||||||||||
| 458 | } | ||||||||||||||||||||||||||||
| 459 | } | ||||||||||||||||||||||||||||
| 460 | |||||||||||||||||||||||||||||
| 461 | 25 | return; | |||||||||||||||||||||||||||
| 462 | } | ||||||||||||||||||||||||||||
| 463 | |||||||||||||||||||||||||||||
| 464 | 5 | 1; | |||||||||||||||||||||||||||
| 465 | __END__ | ||||||||||||||||||||||||||||