DBIx/Class/ResultSourceHandle.pm

filter: Statementstmt 93.8% 46 / 49 Branchbran 62.5% 10 / 16 Conditioncond 33.3% 3 / 9 MC/DCmcdc 0.0% 0 / 5 Subroutinesub 100% 12 / 12 Podpod 100% 4 / 4 totaltotal 78.9% 75 / 95 CC 12 SCAR 16.1
CC
12
Cov
80%
new
13.0
STORABLE_thaw
4.0
resolve
3.0
CRAP 5.0
« DBIx/Class/CDBICompat/Iterator.pm ↑ Summary DBIx/Class/Storage/DBI/ADO/Microsoft_SQL_Server/Cursor.pm »
1 package DBIx::Class::ResultSourceHandle;
2  
3 379 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 379 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 379 ▶use base qw/DBIx::Class/;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 379 ▶use Try::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 379 ▶use namespace::clean;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 use overload
12 1 ▶ q/""/ => sub { __PACKAGE__ . ":" . shift->source_moniker; },
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine __ANON__: called CC=1 SCAR=0.0
13 379 fallback => 1;
14  
15 379 __PACKAGE__->mk_group_accessors('simple' => qw/schema source_moniker _detached_source/);
16  
17 # Schema to use when thawing.
18 379 our $thaw_schema;
19  
20 =head1 NAME
21  
22 DBIx::Class::ResultSourceHandle - Serializable pointers to ResultSource instances
23  
24 =head1 DESCRIPTION
25  
26 Currently instances of this class are used to allow proper serialization of
27 L<ResultSources|DBIx::Class::ResultSource> (which may contain unserializable
28 elements like C<CODE> references).
29  
30 Originally this module was used to remove the fixed link between
31 L<Rows|DBIx::Class::Row>/L<ResultSets|DBIx::Class::ResultSet> and the actual
32 L<result source objects|DBIx::Class::ResultSource> in order to obviate the need
33 of keeping a L<schema instance|DBIx::Class::Schema> constantly in scope, while
34 at the same time avoiding leaks due to circular dependencies. This is however
35 no longer needed after introduction of a proper mutual-assured-destruction
36 contract between a C<Schema> instance and its C<ResultSource> registrants.
37  
38 =head1 METHODS
39  
40 =head2 new
41  
42 =cut
43  
44 206 ▶sub new {
Subroutine new: called CC=6 SCAR=25.6
Pod documented
45 206 my ($class, $args) = @_;
46 206 ▶ my $self = bless $args, ref $class || $class;
Condition33%1/3
ref $class || $class
l!l&&r!l&&!r
02060
MC/DC0%0/2
ref $class || $class
ref $class $class
Truth table33%1/3
A || B
A= ref $classB= $class
ABresult
1X1
011
000
47  
48 206 ▶ unless( ($self->{schema} || $self->{_detached_source}) && $self->{source_moniker} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"schema"} || $self->{"_detached_source"} and $self->{"source_moniker"}) 0 206 206
Condition33%2/6
$self->{"schema"} || $self->{"_detached_source"} and $self->{"source_moniker"}
!ll&&!rl&&r
00206
$self->{"schema"} or $self->{"_detached_source"}
l!l&&r!l&&!r
20600
MC/DC0%0/3
$self->{"schema"} || $self->{"_detached_source"} and $self->{"source_moniker"}
$self->{"schema"} $self->{"_detached_source"} $self->{"source_moniker"}
Truth table20%1/5
A or B and C
A= $self->{"schema"}B= $self->{"_detached_source"}C= $self->{"source_moniker"}
ABCresult
00X0
1X00
0100
1X11
0111
49 0 my $err = 'Expecting a schema instance and a source moniker';
50 $self->{schema}
51 0 ▶ ? $self->{schema}->throw_exception($err)
Branch: 1 branch
BranchTrueFalseTotal
$self->{'schema'} ? : 0 0 0
52 : DBIx::Class::Exception->throw($err)
53 }
54  
55 206 $self;
56 }
57  
58 =head2 resolve
59  
60 Resolve the moniker into the actual ResultSource object
61  
62 =cut
63  
64 206 ▶sub resolve {
Subroutine resolve: called CC=3 SCAR=11.1
Pod documented
65 206 ▶ return $_[0]->{schema}->source($_[0]->source_moniker) if $_[0]->{schema};
Branch: 1 branch
BranchTrueFalseTotal
if $_[0]{"schema"} 149 57 206
66  
67 57 ▶ $_[0]->_detached_source || DBIx::Class::Exception->throw( sprintf (
Branch: 1 branch
BranchTrueFalseTotal
$_[0]->_detached_source or "DBIx::Class::Exception"->throw(sprintf("Unable to resolve moniker '%s' - please contact the dev team at %s", $_[0]->source_moniker, "http://p3rl.org/DBIx::Class#GETTING_HELP/SUPPORT"), "full_stacktrace") 57 0 57
68 # vague error message as this is never supposed to happen
69 "Unable to resolve moniker '%s' - please contact the dev team at %s",
70 $_[0]->source_moniker,
71 DBIx::Class::_ENV_::HELP_URL,
72 ), 'full_stacktrace');
73 }
74  
75 =head2 STORABLE_freeze
76  
77 Freezes a handle.
78  
79 =cut
80  
81 207 ▶sub STORABLE_freeze {
Subroutine STORABLE_freeze: called CC=2 SCAR=7.0
Pod documented
82 207 my ($self, $cloning) = @_;
83  
84 207 my $to_serialize = { %$self };
85  
86 207 delete $to_serialize->{schema};
87 207 delete $to_serialize->{_detached_source};
88 $to_serialize->{_frozen_from_class} = $self->{schema}
89 ? $self->{schema}->class($self->source_moniker)
90 : $self->{_detached_source}->result_class
91 207 ▶ ;
Branch: 1 branch
BranchTrueFalseTotal
$self->{'schema'} ? : 207 0 207
92  
93 207 Storable::nfreeze($to_serialize);
94 }
95  
96 =head2 STORABLE_thaw
97  
98 Thaws frozen handle. Resets the internal schema reference to the package
99 variable C<$thaw_schema>. The recommended way of setting this is to use
100 C<< $schema->thaw($ice) >> which handles this for you.
101  
102 =cut
103  
104 207 ▶sub STORABLE_thaw {
Subroutine STORABLE_thaw: called CC=4 SCAR=13.9
Pod documented
105 207 my ($self, $cloning, $ice) = @_;
106 207 %$self = %{ Storable::thaw($ice) };
107  
108 207 my $from_class = delete $self->{_frozen_from_class};
109  
110 207 ▶ if( $thaw_schema ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($DBIx::Class::ResultSourceHandle::thaw_schema) { } 22 185 207
elsif (my $rs = $from_class->result_source_instance) { } 185 0 185
111 22 $self->schema( $thaw_schema );
112 }
113 elsif( my $rs = $from_class->result_source_instance ) {
114 # in the off-chance we are using CDBI-compat and have leaked $schema already
115 185 ▶ if( my $s = try { $rs->schema } ) {
Subroutine __ANON__: called CC=1 SCAR=0.0
Branch: 1 branch
BranchTrueFalseTotal
if (my $s = &try(sub { $rs->schema; } )) { } 127 58 185
116 127 $self->schema( $s );
117 }
118 else {
119 58 $rs->source_name( $self->source_moniker );
120 58 $rs->{_detached_thaw} = 1;
121 58 $self->_detached_source( $rs );
122 }
123 }
124 else {
125 0 DBIx::Class::Exception->throw(
126 "Thaw failed - original result class '$from_class' does not exist on this system"
127 );
128 }
129 }
130  
131 =head1 FURTHER QUESTIONS?
132  
133 Check the list of L<additional DBIC resources|DBIx::Class/GETTING HELP/SUPPORT>.
134  
135 =head1 COPYRIGHT AND LICENSE
136  
137 This module is free software L<copyright|DBIx::Class/COPYRIGHT AND LICENSE>
138 by the L<DBIx::Class (DBIC) authors|DBIx::Class/AUTHORS>. You can
139 redistribute it and/or modify it under the same terms as the
140 L<DBIx::Class library|DBIx::Class/COPYRIGHT AND LICENSE>.
141  
142 =cut
143  
144 379 1;
« DBIx/Class/CDBICompat/Iterator.pm ↑ Summary DBIx/Class/Storage/DBI/ADO/Microsoft_SQL_Server/Cursor.pm »