| 1 | package DBIx::Class::KiokuDB; | ||||||||||||||||||||||||||||
| 2 | 3 | ▶ | BEGIN { | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 3 | 3 | $DBIx::Class::KiokuDB::AUTHORITY = 'cpan:NUFFIN'; | |||||||||||||||||||||||||||
| 4 | } | ||||||||||||||||||||||||||||
| 5 | # ABSTRACT: Refer to KiokuDB objects from DBIx::Class tables. | ||||||||||||||||||||||||||||
| 6 | 3 | $DBIx::Class::KiokuDB::VERSION = '1.23'; | |||||||||||||||||||||||||||
| 7 | 3 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | 3 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||
| 10 | 3 | ▶ | use Carp; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 11 | 3 | ▶ | use Scalar::Util qw(weaken); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||
| 13 | 3 | ▶ | use namespace::clean; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||
| 15 | 3 | ▶ | use base qw(DBIx::Class::Core); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||
| 17 | 4 | ▶ | sub new { | ||||||||||||||||||||||||||
|
Subroutine
new: called CC=4 SCAR=13.9
Pod
documented
| |||||||||||||||||||||||||||||
| 18 | 4 | my $self = shift->next::method(@_); | |||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||
| 20 | 4 | foreach my $key ( $self->result_source->columns ) { | |||||||||||||||||||||||||||
| 21 | 12 | my $col_info = $self->column_info($key); | |||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||
| 23 | 12 | ▶ | if ( $col_info->{_kiokudb_info} and ref( my $obj = $self->get_column($key) ) ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$col_info->{"_kiokudb_info"} and ref(my $obj = $self->get_column($key))
MC/DC100%2/2
$col_info->{"_kiokudb_info"} and ref(my $obj = $self->get_column($key))
$col_info->{"_kiokudb_info"}
ref(my $obj = $self->get_column($key))
Truth table100%3/3
A and B
A= $col_info->{"_kiokudb_info"}B= ref(my $obj = $self->get_column($key))
| |||||||||||||||||||||||||||||
| 24 | 3 | $self->store_kiokudb_column( $key => $obj ); | |||||||||||||||||||||||||||
| 25 | } | ||||||||||||||||||||||||||||
| 26 | } | ||||||||||||||||||||||||||||
| 27 | |||||||||||||||||||||||||||||
| 28 | 4 | return $self; | |||||||||||||||||||||||||||
| 29 | } | ||||||||||||||||||||||||||||
| 30 | |||||||||||||||||||||||||||||
| 31 | 4 | ▶ | sub insert { | ||||||||||||||||||||||||||
|
Subroutine
insert: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||||||||||
| 32 | 4 | my ( $self, @args ) = @_; | |||||||||||||||||||||||||||
| 33 | |||||||||||||||||||||||||||||
| 34 | 4 | my $schema = $self->result_source->schema; | |||||||||||||||||||||||||||
| 35 | |||||||||||||||||||||||||||||
| 36 | 4 | my $g = $schema->txn_scope_guard; | |||||||||||||||||||||||||||
| 37 | |||||||||||||||||||||||||||||
| 38 | 4 | my $dir = $schema->kiokudb_handle; | |||||||||||||||||||||||||||
| 39 | 4 | my $lo = $dir->live_objects; | |||||||||||||||||||||||||||
| 40 | |||||||||||||||||||||||||||||
| 41 | 4 | ▶ | if ( my @insert = grep { ref and not $lo->object_to_entry($_) } values %{ $self->{_kiokudb_column} } ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
ref $_ && !$lo->object_to_entry($_)
MC/DC50%1/2
ref $_ && !$lo->object_to_entry($_)
ref $_
!$lo->object_to_entry($_)
Truth table66%2/3
A && not(B)
A= ref $_B= !$lo->object_to_entry($_)
| |||||||||||||||||||||||||||||
| 42 | 2 | $dir->insert(@insert); | |||||||||||||||||||||||||||
| 43 | } | ||||||||||||||||||||||||||||
| 44 | |||||||||||||||||||||||||||||
| 45 | 4 | my $ret = $self->next::method(@args); | |||||||||||||||||||||||||||
| 46 | |||||||||||||||||||||||||||||
| 47 | 4 | $g->commit; | |||||||||||||||||||||||||||
| 48 | |||||||||||||||||||||||||||||
| 49 | 4 | return $ret; | |||||||||||||||||||||||||||
| 50 | } | ||||||||||||||||||||||||||||
| 51 | |||||||||||||||||||||||||||||
| 52 | 4 | ▶ | sub update { | ||||||||||||||||||||||||||
|
Subroutine
update: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||||||||||
| 53 | 4 | my ( $self, @args ) = @_; | |||||||||||||||||||||||||||
| 54 | |||||||||||||||||||||||||||||
| 55 | 4 | my $dir = $self->result_source->schema->kiokudb_handle; | |||||||||||||||||||||||||||
| 56 | 4 | my $lo = $dir->live_objects; | |||||||||||||||||||||||||||
| 57 | |||||||||||||||||||||||||||||
| 58 | 4 | ▶ | if ( my @insert = grep { ref and not $lo->object_to_entry($_) } values %{ $self->{_kiokudb_column} } ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
ref $_ && !$lo->object_to_entry($_)
MC/DC50%1/2
ref $_ && !$lo->object_to_entry($_)
ref $_
!$lo->object_to_entry($_)
Truth table66%2/3
A && not(B)
A= ref $_B= !$lo->object_to_entry($_)
| |||||||||||||||||||||||||||||
| 59 | 1 | croak("Can't update object, related KiokuDB objects are not in storage"); | |||||||||||||||||||||||||||
| 60 | } | ||||||||||||||||||||||||||||
| 61 | |||||||||||||||||||||||||||||
| 62 | 3 | $self->next::method(@args); | |||||||||||||||||||||||||||
| 63 | } | ||||||||||||||||||||||||||||
| 64 | |||||||||||||||||||||||||||||
| 65 | 1 | ▶ | sub store { | ||||||||||||||||||||||||||
|
Subroutine
store: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 66 | 1 | my ( $self, @args ) = @_; | |||||||||||||||||||||||||||
| 67 | |||||||||||||||||||||||||||||
| 68 | 1 | my $schema = $self->result_source->schema; | |||||||||||||||||||||||||||
| 69 | |||||||||||||||||||||||||||||
| 70 | 1 | my $g = $schema->txn_scope_guard; | |||||||||||||||||||||||||||
| 71 | |||||||||||||||||||||||||||||
| 72 | 1 | ▶ | if ( my @objects = grep { ref } values %{ $self->{_kiokudb_column} } ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 73 | 1 | $schema->kiokudb_handle->store(@objects); | |||||||||||||||||||||||||||
| 74 | } | ||||||||||||||||||||||||||||
| 75 | |||||||||||||||||||||||||||||
| 76 | 1 | my $ret = $self->insert_or_update; | |||||||||||||||||||||||||||
| 77 | |||||||||||||||||||||||||||||
| 78 | 1 | $g->commit; | |||||||||||||||||||||||||||
| 79 | |||||||||||||||||||||||||||||
| 80 | 1 | return $ret; | |||||||||||||||||||||||||||
| 81 | } | ||||||||||||||||||||||||||||
| 82 | |||||||||||||||||||||||||||||
| 83 | 2 | ▶ | sub kiokudb_column { | ||||||||||||||||||||||||||
|
Subroutine
kiokudb_column: called CC=4 SCAR=14.2
Pod
documented
| |||||||||||||||||||||||||||||
| 84 | 2 | my ($self, $rel, $cond, $attrs) = @_; | |||||||||||||||||||||||||||
| 85 | |||||||||||||||||||||||||||||
| 86 | # assume a foreign key contraint unless defined otherwise | ||||||||||||||||||||||||||||
| 87 | $attrs->{is_foreign_key_constraint} = 1 | ||||||||||||||||||||||||||||
| 88 | 2 | ▶ | if not exists $attrs->{is_foreign_key_constraint}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 89 | |||||||||||||||||||||||||||||
| 90 | 2 | ▶ | my $fk = defined $cond ? $cond : $rel; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 91 | |||||||||||||||||||||||||||||
| 92 | 2 | $self->add_relationship( $rel, 'entries', { 'foreign.id' => "self.$fk" }, $attrs ); # FIXME hardcoded 'entries' | |||||||||||||||||||||||||||
| 93 | |||||||||||||||||||||||||||||
| 94 | 2 | my $col_info = $self->column_info($fk); | |||||||||||||||||||||||||||
| 95 | |||||||||||||||||||||||||||||
| 96 | 2 | $col_info->{_kiokudb_info} = {}; | |||||||||||||||||||||||||||
| 97 | |||||||||||||||||||||||||||||
| 98 | 2 | my $accessor = $col_info->{accessor}; | |||||||||||||||||||||||||||
| 99 | 2 | ▶ | $accessor = $rel unless defined $accessor; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 100 | |||||||||||||||||||||||||||||
| 101 | 2 | $self->mk_group_accessors('kiokudb_column' => [ $accessor, $fk]); | |||||||||||||||||||||||||||
| 102 | } | ||||||||||||||||||||||||||||
| 103 | |||||||||||||||||||||||||||||
| 104 | 5 | ▶ | sub _kiokudb_id_to_object { | ||||||||||||||||||||||||||
|
Subroutine
_kiokudb_id_to_object: called CC=3 SCAR=14.2
| |||||||||||||||||||||||||||||
| 105 | 5 | my ( $self, $id ) = @_; | |||||||||||||||||||||||||||
| 106 | |||||||||||||||||||||||||||||
| 107 | 5 | ▶ | if ( ref( my $obj = $self->result_source->schema->kiokudb_handle->lookup($id) ) ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 108 | 5 | return $obj; | |||||||||||||||||||||||||||
| 109 | } else { | ||||||||||||||||||||||||||||
| 110 | 0 | ▶ | croak("No object with ID '$id' found") unless ref $obj; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 111 | } | ||||||||||||||||||||||||||||
| 112 | } | ||||||||||||||||||||||||||||
| 113 | |||||||||||||||||||||||||||||
| 114 | 4 | ▶ | sub _kiokudb_object_to_id { | ||||||||||||||||||||||||||
|
Subroutine
_kiokudb_object_to_id: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||
| 115 | 4 | my ( $self, $object ) = @_; | |||||||||||||||||||||||||||
| 116 | |||||||||||||||||||||||||||||
| 117 | 4 | ▶ | confess unless ref $object; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 118 | |||||||||||||||||||||||||||||
| 119 | 4 | my $dir = $self->result_source->schema->kiokudb_handle; | |||||||||||||||||||||||||||
| 120 | |||||||||||||||||||||||||||||
| 121 | 4 | ▶ | if ( my $id = $dir->object_to_id($object) ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 122 | 1 | return $id; | |||||||||||||||||||||||||||
| 123 | } else { | ||||||||||||||||||||||||||||
| 124 | # generate an ID | ||||||||||||||||||||||||||||
| 125 | 3 | my $collapser = $dir->collapser; | |||||||||||||||||||||||||||
| 126 | 3 | my $id_method = $collapser->id_method(ref $object); | |||||||||||||||||||||||||||
| 127 | 3 | my $id = $id = $collapser->$id_method($object); | |||||||||||||||||||||||||||
| 128 | |||||||||||||||||||||||||||||
| 129 | # register the ID | ||||||||||||||||||||||||||||
| 130 | 3 | $dir->live_objects->insert( $id => $object ); | |||||||||||||||||||||||||||
| 131 | |||||||||||||||||||||||||||||
| 132 | 3 | return $id; | |||||||||||||||||||||||||||
| 133 | } | ||||||||||||||||||||||||||||
| 134 | } | ||||||||||||||||||||||||||||
| 135 | |||||||||||||||||||||||||||||
| 136 | 9 | ▶ | sub get_kiokudb_column { | ||||||||||||||||||||||||||
|
Subroutine
get_kiokudb_column: called CC=5 SCAR=16.8
Pod
documented
| |||||||||||||||||||||||||||||
| 137 | 9 | my ( $self, $col ) = @_; | |||||||||||||||||||||||||||
| 138 | |||||||||||||||||||||||||||||
| 139 | $self->throw_exception("$col is not a KiokuDB column") | ||||||||||||||||||||||||||||
| 140 | 9 | ▶ | unless exists $self->column_info($col)->{_kiokudb_info}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 141 | |||||||||||||||||||||||||||||
| 142 | return $self->{_kiokudb_column}{$col} | ||||||||||||||||||||||||||||
| 143 | 9 | ▶ | if defined $self->{_kiokudb_column}{$col}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 144 | |||||||||||||||||||||||||||||
| 145 | 5 | ▶ | if ( defined( my $val = $self->get_column($col) ) ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 146 | 5 | ▶ | my $obj = ref $val ? $val : $self->_kiokudb_id_to_object($val); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 147 | |||||||||||||||||||||||||||||
| 148 | # weaken by default, in case there are cycles, the live object scope will | ||||||||||||||||||||||||||||
| 149 | # take care of this | ||||||||||||||||||||||||||||
| 150 | 5 | weaken( $self->{_kiokudb_column}{$col} = $obj ); | |||||||||||||||||||||||||||
| 151 | |||||||||||||||||||||||||||||
| 152 | 5 | return $obj; | |||||||||||||||||||||||||||
| 153 | } else { | ||||||||||||||||||||||||||||
| 154 | 0 | return; | |||||||||||||||||||||||||||
| 155 | } | ||||||||||||||||||||||||||||
| 156 | } | ||||||||||||||||||||||||||||
| 157 | |||||||||||||||||||||||||||||
| 158 | 4 | ▶ | sub _set_kiokudb_column { | ||||||||||||||||||||||||||
|
Subroutine
_set_kiokudb_column: called CC=2 SCAR=7.6
| |||||||||||||||||||||||||||||
| 159 | 4 | my ( $self, $method, $col, $obj ) = @_; | |||||||||||||||||||||||||||
| 160 | |||||||||||||||||||||||||||||
| 161 | 4 | ▶ | if ( ref $obj ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 162 | 4 | $self->$method( $col, $self->_kiokudb_object_to_id($obj) ); | |||||||||||||||||||||||||||
| 163 | 4 | $self->{_kiokudb_column}{$col} = $obj; | |||||||||||||||||||||||||||
| 164 | } else { | ||||||||||||||||||||||||||||
| 165 | 0 | $self->$method( $col, undef ); | |||||||||||||||||||||||||||
| 166 | 0 | delete $self->{_kiokudb_column}{$col}; | |||||||||||||||||||||||||||
| 167 | } | ||||||||||||||||||||||||||||
| 168 | |||||||||||||||||||||||||||||
| 169 | 4 | return $obj; | |||||||||||||||||||||||||||
| 170 | } | ||||||||||||||||||||||||||||
| 171 | |||||||||||||||||||||||||||||
| 172 | 1 | ▶ | sub set_kiokudb_column { | ||||||||||||||||||||||||||
|
Subroutine
set_kiokudb_column: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 173 | 1 | my ( $self, @args ) = @_; | |||||||||||||||||||||||||||
| 174 | 1 | $self->_set_kiokudb_column( set_column => @args ); | |||||||||||||||||||||||||||
| 175 | } | ||||||||||||||||||||||||||||
| 176 | |||||||||||||||||||||||||||||
| 177 | 3 | ▶ | sub store_kiokudb_column { | ||||||||||||||||||||||||||
|
Subroutine
store_kiokudb_column: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 178 | 3 | my ( $self, @args ) = @_; | |||||||||||||||||||||||||||
| 179 | 3 | $self->_set_kiokudb_column( store_column => @args ); | |||||||||||||||||||||||||||
| 180 | } | ||||||||||||||||||||||||||||
| 181 | |||||||||||||||||||||||||||||
| 182 | # ex: set sw=4 et: | ||||||||||||||||||||||||||||
| 183 | |||||||||||||||||||||||||||||
| 184 | 3 | __PACKAGE__ | |||||||||||||||||||||||||||
| 185 | |||||||||||||||||||||||||||||
| 186 | __END__ | ||||||||||||||||||||||||||||