| 1 | # the contents of this file are Copyright (c) 2009 Daniel Norman | ||||||||||||||||||||
| 2 | # This program is free software; you can redistribute it and/or | ||||||||||||||||||||
| 3 | # modify it under the terms of the GNU General Public License as | ||||||||||||||||||||
| 4 | # published by the Free Software Foundation. | ||||||||||||||||||||
| 5 | |||||||||||||||||||||
| 6 | package DBR::Config::Relation; | ||||||||||||||||||||
| 7 | |||||||||||||||||||||
| 8 | 18 | ▶ | use strict; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 9 | 18 | ▶ | use base 'DBR::Common'; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 10 | 18 | ▶ | use DBR::Config::Table; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 11 | 18 | ▶ | use DBR::Config::Field; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 12 | 18 | ▶ | use Carp; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 13 | 18 | ▶ | use Clone 'clone'; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 14 | |||||||||||||||||||||
| 15 | 18 | my %TYPES = ( | |||||||||||||||||||
| 16 | 1 => { name => 'parentof', mode => '1toM', opposite => 2 }, #reciprocal | ||||||||||||||||||||
| 17 | 2 => { name => 'childof', mode => 'Mto1', opposite => 1 }, | ||||||||||||||||||||
| 18 | 3 => { name => 'assoc', mode => 'MtoM' }, | ||||||||||||||||||||
| 19 | 4 => { name => 'other', mode => 'MtoM' }, | ||||||||||||||||||||
| 20 | ); | ||||||||||||||||||||
| 21 | |||||||||||||||||||||
| 22 | 18 | map { $TYPES{$_}{type_id} = $_ } keys %TYPES; | |||||||||||||||||||
| 23 | |||||||||||||||||||||
| 24 | 18 | ▶ | sub list_types{ | ||||||||||||||||||
|
Subroutine
list_types: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||
| 25 | 18 | return clone( [ sort {$a->{type_id} <=> $b->{type_id} } values %TYPES ] ); | |||||||||||||||||||
| 26 | } | ||||||||||||||||||||
| 27 | |||||||||||||||||||||
| 28 | |||||||||||||||||||||
| 29 | 18 | my %RELATIONS_BY_ID; | |||||||||||||||||||
| 30 | 34 | ▶ | sub load{ | ||||||||||||||||||
|
Subroutine
load: called CC=10 SCAR=24.6
Pod
undocumented
| |||||||||||||||||||||
| 31 | 34 | my( $package ) = shift; | |||||||||||||||||||
| 32 | 34 | my %params = @_; | |||||||||||||||||||
| 33 | |||||||||||||||||||||
| 34 | 34 | my $self = { session => $params{session} }; | |||||||||||||||||||
| 35 | 34 | bless( $self, $package ); # Dummy object | |||||||||||||||||||
| 36 | |||||||||||||||||||||
| 37 | 34 | ▶ | my $instance = $params{instance} || return $self->_error('instance is required'); | ||||||||||||||||||
|
Condition50%1/2
$params{'instance'} || (return $self->_error('instance is required'))
MC/DC0%0/1
$params{'instance'} || (return $self->_error('instance is required'))
$params{'instance'}
Truth table50%1/2
A
A= $params{'instance'}
| |||||||||||||||||||||
| 38 | |||||||||||||||||||||
| 39 | 34 | ▶ | my $table_ids = $params{table_id} || return $self->_error('table_id is required'); | ||||||||||||||||||
|
Condition50%1/2
$params{'table_id'} || (return $self->_error('table_id is required'))
MC/DC0%0/1
$params{'table_id'} || (return $self->_error('table_id is required'))
$params{'table_id'}
Truth table50%1/2
A
A= $params{'table_id'}
| |||||||||||||||||||||
| 40 | 34 | ▶ | $table_ids = [$table_ids] unless ref($table_ids) eq 'ARRAY'; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 41 | |||||||||||||||||||||
| 42 | 34 | ▶ | return 1 unless @$table_ids; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 43 | |||||||||||||||||||||
| 44 | 0 | ▶ | my $dbrh = $instance->connect || return $self->_error("Failed to connect to ${\$instance->name}"); | ||||||||||||||||||
|
Condition50%1/2
$instance->connect || (return $self->_error("Failed to connect to ${\$instance->name;}"))
MC/DC0%0/1
$instance->connect || (return $self->_error("Failed to connect to ${\$instance->name;}"))
$instance->connect
Truth table50%1/2
A
A= $instance->connect
| |||||||||||||||||||||
| 45 | |||||||||||||||||||||
| 46 | 34 | ▶ | return $self->_error('Failed to select from dbr_relationships') unless | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 47 | my $relations = $dbrh->select( | ||||||||||||||||||||
| 48 | -table => 'dbr_relationships', | ||||||||||||||||||||
| 49 | -fields => 'relationship_id from_name from_table_id from_field_id to_name to_table_id to_field_id type', | ||||||||||||||||||||
| 50 | -where => { from_table_id => ['d in',@$table_ids] }, | ||||||||||||||||||||
| 51 | ); | ||||||||||||||||||||
| 52 | |||||||||||||||||||||
| 53 | 34 | my @rel_ids; | |||||||||||||||||||
| 54 | 34 | foreach my $relation (@$relations){ | |||||||||||||||||||
| 55 | |||||||||||||||||||||
| 56 | my $table1 = DBR::Config::Table->_register_relation( | ||||||||||||||||||||
| 57 | table_id => $relation->{to_table_id}, | ||||||||||||||||||||
| 58 | name => $relation->{from_name}, #yes, this is kinda confusing | ||||||||||||||||||||
| 59 | relation_id => $relation->{relationship_id}, | ||||||||||||||||||||
| 60 | 24 | ▶ | ) or return $self->_error('failed to register to relationship'); | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 61 | |||||||||||||||||||||
| 62 | my $table2 = DBR::Config::Table->_register_relation( | ||||||||||||||||||||
| 63 | table_id => $relation->{from_table_id}, | ||||||||||||||||||||
| 64 | name => $relation->{to_name}, #yes, this is kinda confusing | ||||||||||||||||||||
| 65 | relation_id => $relation->{relationship_id}, | ||||||||||||||||||||
| 66 | 24 | ▶ | ) or return $self->_error('failed to register from relationship'); | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 67 | |||||||||||||||||||||
| 68 | |||||||||||||||||||||
| 69 | 24 | $relation->{same_schema} = ( $table1->{schema_id} == $table2->{schema_id} ); | |||||||||||||||||||
| 70 | |||||||||||||||||||||
| 71 | 24 | $RELATIONS_BY_ID{ $relation->{relationship_id} } = $relation; | |||||||||||||||||||
| 72 | 24 | push @rel_ids, $relation->{relationship_id}; | |||||||||||||||||||
| 73 | |||||||||||||||||||||
| 74 | } | ||||||||||||||||||||
| 75 | |||||||||||||||||||||
| 76 | 34 | return 1; | |||||||||||||||||||
| 77 | } | ||||||||||||||||||||
| 78 | |||||||||||||||||||||
| 79 | |||||||||||||||||||||
| 80 | 44 | ▶ | sub new { | ||||||||||||||||||
|
Subroutine
new: called CC=8 SCAR=22.0
Pod
undocumented
| |||||||||||||||||||||
| 81 | 44 | my $package = shift; | |||||||||||||||||||
| 82 | 44 | my %params = @_; | |||||||||||||||||||
| 83 | my $self = { | ||||||||||||||||||||
| 84 | session => $params{session}, | ||||||||||||||||||||
| 85 | relation_id => $params{relation_id}, | ||||||||||||||||||||
| 86 | table_id => $params{table_id}, | ||||||||||||||||||||
| 87 | 44 | }; | |||||||||||||||||||
| 88 | |||||||||||||||||||||
| 89 | 44 | bless( $self, $package ); | |||||||||||||||||||
| 90 | |||||||||||||||||||||
| 91 | 44 | ▶ | return $self->_error('relation_id is required') unless $self->{relation_id}; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 92 | 44 | ▶ | return $self->_error('table_id is required') unless $self->{table_id}; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 93 | |||||||||||||||||||||
| 94 | |||||||||||||||||||||
| 95 | 44 | ▶ | my $ref = $RELATIONS_BY_ID{ $self->{relation_id} } or return $self->_error('invalid relation_id'); | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 96 | 44 | ▶ | return $self->_error("Invalid type_id $ref->{type}") unless $TYPES{ $ref->{type} }; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 97 | |||||||||||||||||||||
| 98 | 44 | ▶ | if($ref->{from_table_id} == $self->{table_id}){ | ||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||
| 99 | |||||||||||||||||||||
| 100 | 25 | $self->{forward} = 'from'; | |||||||||||||||||||
| 101 | 25 | $self->{reverse} = 'to'; | |||||||||||||||||||
| 102 | 25 | $self->{type_id} = $ref->{type}; | |||||||||||||||||||
| 103 | }elsif($ref->{to_table_id} == $self->{table_id}){ | ||||||||||||||||||||
| 104 | |||||||||||||||||||||
| 105 | 19 | $self->{forward} = 'to'; | |||||||||||||||||||
| 106 | 19 | $self->{reverse} = 'from'; | |||||||||||||||||||
| 107 | 19 | ▶ | $self->{type_id} = $TYPES{ $ref->{type} }->{opposite} || $ref->{type}; | ||||||||||||||||||
|
Condition33%1/3
$TYPES{$ref->{'type'}}{'opposite'} || $ref->{'type'}
MC/DC0%0/2
$TYPES{$ref->{'type'}}{'opposite'} || $ref->{'type'}
$TYPES{$ref->{'type'}}{'opposite'}
$ref->{'type'}
Truth table33%1/3
A || B
A= $TYPES{$ref->{'type'}}{'opposite'}B= $ref->{'type'}
| |||||||||||||||||||||
| 108 | |||||||||||||||||||||
| 109 | }else{ | ||||||||||||||||||||
| 110 | 0 | return $self->_error("table_id $self->{table_id} is invalid for this relationship"); | |||||||||||||||||||
| 111 | } | ||||||||||||||||||||
| 112 | |||||||||||||||||||||
| 113 | 44 | return( $self ); | |||||||||||||||||||
| 114 | } | ||||||||||||||||||||
| 115 | |||||||||||||||||||||
| 116 | 0 | ▶ | sub relation_id { $_[0]->{relation_id} } | ||||||||||||||||||
|
Subroutine
relation_id: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||
| 117 | 35 | ▶ | sub name { $RELATIONS_BY_ID{ $_[0]->{relation_id} }->{ $_[0]->{reverse} . '_name' } } # Name is always the opposite of everything else | ||||||||||||||||||
|
Subroutine
name: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||
| 118 | |||||||||||||||||||||
| 119 | 35 | ▶ | sub field_id { | ||||||||||||||||||
|
Subroutine
field_id: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||
| 120 | 35 | my $self = shift; | |||||||||||||||||||
| 121 | |||||||||||||||||||||
| 122 | 35 | return $RELATIONS_BY_ID{ $self->{relation_id} }->{ $self->{forward} . '_field_id' }; | |||||||||||||||||||
| 123 | } | ||||||||||||||||||||
| 124 | |||||||||||||||||||||
| 125 | 9 | ▶ | sub field { | ||||||||||||||||||
|
Subroutine
field: called CC=2 SCAR=7.0
Pod
undocumented
| |||||||||||||||||||||
| 126 | 9 | my $self = shift; | |||||||||||||||||||
| 127 | 9 | my $field_id = $RELATIONS_BY_ID{ $self->{relation_id} }->{ $self->{forward} . '_field_id' }; | |||||||||||||||||||
| 128 | |||||||||||||||||||||
| 129 | my $field = DBR::Config::Field->new( | ||||||||||||||||||||
| 130 | session => $self->{session}, | ||||||||||||||||||||
| 131 | 9 | ▶ | field_id => $field_id, | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 132 | ) or return $self->_error('failed to create field object'); | ||||||||||||||||||||
| 133 | |||||||||||||||||||||
| 134 | 9 | return $field; | |||||||||||||||||||
| 135 | } | ||||||||||||||||||||
| 136 | |||||||||||||||||||||
| 137 | 21 | ▶ | sub mapfield { | ||||||||||||||||||
|
Subroutine
mapfield: called CC=2 SCAR=7.0
Pod
undocumented
| |||||||||||||||||||||
| 138 | 21 | my $self = shift; | |||||||||||||||||||
| 139 | 21 | my $mapfield_id = $RELATIONS_BY_ID{ $self->{relation_id} }->{ $self->{reverse} . '_field_id' }; | |||||||||||||||||||
| 140 | |||||||||||||||||||||
| 141 | my $field = DBR::Config::Field->new( | ||||||||||||||||||||
| 142 | session => $self->{session}, | ||||||||||||||||||||
| 143 | 21 | ▶ | field_id => $mapfield_id, | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 144 | ) or return $self->_error('failed to create field object'); | ||||||||||||||||||||
| 145 | |||||||||||||||||||||
| 146 | 21 | return $field; | |||||||||||||||||||
| 147 | } | ||||||||||||||||||||
| 148 | |||||||||||||||||||||
| 149 | 12 | ▶ | sub table { | ||||||||||||||||||
|
Subroutine
table: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||
| 150 | 12 | my $self = shift; | |||||||||||||||||||
| 151 | |||||||||||||||||||||
| 152 | return DBR::Config::Table->new( | ||||||||||||||||||||
| 153 | session => $self->{session}, | ||||||||||||||||||||
| 154 | 12 | table_id => $RELATIONS_BY_ID{ $self->{relation_id} }->{$self->{forward} . '_table_id'} | |||||||||||||||||||
| 155 | ); | ||||||||||||||||||||
| 156 | } | ||||||||||||||||||||
| 157 | |||||||||||||||||||||
| 158 | 21 | ▶ | sub maptable { | ||||||||||||||||||
|
Subroutine
maptable: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||
| 159 | 21 | my $self = shift; | |||||||||||||||||||
| 160 | |||||||||||||||||||||
| 161 | return DBR::Config::Table->new( | ||||||||||||||||||||
| 162 | session => $self->{session}, | ||||||||||||||||||||
| 163 | 21 | table_id => $RELATIONS_BY_ID{ $self->{relation_id} }->{$self->{reverse} . '_table_id'} | |||||||||||||||||||
| 164 | ); | ||||||||||||||||||||
| 165 | } | ||||||||||||||||||||
| 166 | |||||||||||||||||||||
| 167 | 30 | ▶ | sub is_to_one{ | ||||||||||||||||||
|
Subroutine
is_to_one: called CC=3 SCAR=11.0
Pod
undocumented
| |||||||||||||||||||||
| 168 | 30 | my $mode = $TYPES{ $_[0]->{type_id} }->{mode}; | |||||||||||||||||||
| 169 | |||||||||||||||||||||
| 170 | 30 | ▶ | return 1 if $mode eq 'Mto1'; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 171 | 4 | ▶ | return 1 if $mode eq '1to1'; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 172 | |||||||||||||||||||||
| 173 | 4 | return 0; | |||||||||||||||||||
| 174 | } | ||||||||||||||||||||
| 175 | |||||||||||||||||||||
| 176 | 28 | ▶ | sub is_same_schema{ $RELATIONS_BY_ID{ shift->{relation_id} }->{same_schema} } | ||||||||||||||||||
|
Subroutine
is_same_schema: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||
| 177 | |||||||||||||||||||||
| 178 | |||||||||||||||||||||
| 179 | 66 | ▶ | sub index{ | ||||||||||||||||||
|
Subroutine
index: called CC=3 SCAR=11.0
Pod
undocumented
| |||||||||||||||||||||
| 180 | 66 | my $self = shift; | |||||||||||||||||||
| 181 | 66 | my $set = shift; | |||||||||||||||||||
| 182 | |||||||||||||||||||||
| 183 | 66 | ▶ | if(defined($set)){ | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 184 | 8 | ▶ | croak "Cannot set the index on a relation object twice" if defined($self->{index}); # I want this to fail obnoxiously | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 185 | 8 | $self->{index} = $set; | |||||||||||||||||||
| 186 | 8 | return 1; | |||||||||||||||||||
| 187 | } | ||||||||||||||||||||
| 188 | |||||||||||||||||||||
| 189 | 58 | return $self->{index}; | |||||||||||||||||||
| 190 | } | ||||||||||||||||||||
| 191 | |||||||||||||||||||||
| 192 | 18 | 1; | |||||||||||||||||||