Commit | Line | Data |
aec3eff1 |
1 | package DBIx::Class::ResultSourceHandle; |
2 | |
3 | use strict; |
4 | use warnings; |
5 | use Storable; |
3a81f59b |
6 | use Carp; |
aec3eff1 |
7 | |
8 | use base qw/DBIx::Class/; |
9 | |
10 | use overload |
5f946071 |
11 | # on some RH perls the following line causes serious performance problem |
12 | # see https://bugzilla.redhat.com/show_bug.cgi?id=196836 |
3441fd57 |
13 | q/""/ => sub { __PACKAGE__ . ":" . shift->source_moniker; }, |
aec3eff1 |
14 | fallback => 1; |
15 | |
3441fd57 |
16 | __PACKAGE__->mk_group_accessors('simple' => qw/schema source_moniker/); |
aec3eff1 |
17 | |
4146e3da |
18 | # Schema to use when thawing. |
19 | our $thaw_schema; |
20 | |
aec3eff1 |
21 | =head1 NAME |
22 | |
b24d86a1 |
23 | DBIx::Class::ResultSourceHandle - Decouple Rows/ResultSets objects from their Source objects |
aec3eff1 |
24 | |
25 | =head1 DESCRIPTION |
26 | |
27 | This module removes fixed link between Rows/ResultSets and the actual source |
28 | objects, which gets round the following problems |
29 | |
30 | =over 4 |
31 | |
32 | =item * |
33 | |
34 | Needing to keep C<$schema> in scope, since any objects/result_sets |
35 | will have a C<$schema> object through their source handle |
36 | |
37 | =item * |
38 | |
39 | Large output when using Data::Dump(er) since this class can be set to |
40 | stringify to almost nothing |
41 | |
aec3eff1 |
42 | =back |
43 | |
44 | =head1 METHODS |
45 | |
46 | =head2 new |
47 | |
48 | =cut |
49 | |
50 | sub new { |
51 | my ($class, $data) = @_; |
52 | |
53 | $class = ref $class if ref $class; |
54 | |
55 | bless $data, $class; |
56 | } |
57 | |
58 | =head2 resolve |
59 | |
3441fd57 |
60 | Resolve the moniker into the actual ResultSource object |
aec3eff1 |
61 | |
62 | =cut |
63 | |
3441fd57 |
64 | sub resolve { return $_[0]->schema->source($_[0]->source_moniker) } |
aec3eff1 |
65 | |
7137528d |
66 | =head2 STORABLE_freeze |
67 | |
68 | Freezes a handle. |
69 | |
70 | =cut |
71 | |
aec3eff1 |
72 | sub STORABLE_freeze { |
73 | my ($self, $cloning) = @_; |
4146e3da |
74 | |
aec3eff1 |
75 | my $to_serialize = { %$self }; |
d4daee7b |
76 | |
7cfda9a6 |
77 | delete $to_serialize->{schema}; |
78 | $to_serialize->{_frozen_from_class} = $self->schema->class($self->source_moniker); |
79 | |
26148d36 |
80 | return (Storable::nfreeze($to_serialize)); |
aec3eff1 |
81 | } |
82 | |
7137528d |
83 | =head2 STORABLE_thaw |
84 | |
4146e3da |
85 | Thaws frozen handle. Resets the internal schema reference to the package |
48580715 |
86 | variable C<$thaw_schema>. The recommended way of setting this is to use |
323e0bd0 |
87 | C<< $schema->thaw($ice) >> which handles this for you. |
7137528d |
88 | |
89 | =cut |
90 | |
4146e3da |
91 | |
aec3eff1 |
92 | sub STORABLE_thaw { |
7cfda9a6 |
93 | my ($self, $cloning, $ice) = @_; |
aec3eff1 |
94 | %$self = %{ Storable::thaw($ice) }; |
3a81f59b |
95 | |
7cfda9a6 |
96 | my $class = delete $self->{_frozen_from_class}; |
3a81f59b |
97 | if( $thaw_schema ) { |
98 | $self->{schema} = $thaw_schema; |
99 | } |
100 | else { |
101 | my $rs = $class->result_source_instance; |
102 | $self->{schema} = $rs->schema if $rs; |
103 | } |
104 | |
4e8205a8 |
105 | carp "Unable to restore schema. Look at 'freeze' and 'thaw' methods in DBIx::Class::Schema." |
106 | unless $self->{schema}; |
aec3eff1 |
107 | } |
108 | |
4146e3da |
109 | =head1 AUTHOR |
110 | |
111 | Ash Berlin C<< <ash@cpan.org> >> |
112 | |
113 | =cut |
114 | |
aec3eff1 |
115 | 1; |