New format of changelog (easier to read)
[dbsrgits/DBIx-Class.git] / lib / DBIx / Class / ResultSourceHandle.pm
CommitLineData
aec3eff1 1package DBIx::Class::ResultSourceHandle;
2
3use strict;
4use warnings;
5use Storable;
3a81f59b 6use Carp;
aec3eff1 7
8use base qw/DBIx::Class/;
9
10use 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.
19our $thaw_schema;
20
aec3eff1 21=head1 NAME
22
b24d86a1 23DBIx::Class::ResultSourceHandle - Decouple Rows/ResultSets objects from their Source objects
aec3eff1 24
25=head1 DESCRIPTION
26
27This module removes fixed link between Rows/ResultSets and the actual source
28objects, which gets round the following problems
29
30=over 4
31
32=item *
33
34Needing to keep C<$schema> in scope, since any objects/result_sets
35will have a C<$schema> object through their source handle
36
37=item *
38
39Large output when using Data::Dump(er) since this class can be set to
40stringify to almost nothing
41
42=item *
43
7137528d 44Closer to being able to do a Serialize::Storable that doesn't require class-based connections
aec3eff1 45
46=back
47
48=head1 METHODS
49
50=head2 new
51
52=cut
53
54sub new {
55 my ($class, $data) = @_;
56
57 $class = ref $class if ref $class;
58
59 bless $data, $class;
60}
61
62=head2 resolve
63
3441fd57 64Resolve the moniker into the actual ResultSource object
aec3eff1 65
66=cut
67
3441fd57 68sub resolve { return $_[0]->schema->source($_[0]->source_moniker) }
aec3eff1 69
7137528d 70=head2 STORABLE_freeze
71
72Freezes a handle.
73
74=cut
75
aec3eff1 76sub STORABLE_freeze {
77 my ($self, $cloning) = @_;
4146e3da 78
aec3eff1 79 my $to_serialize = { %$self };
d4daee7b 80
7cfda9a6 81 delete $to_serialize->{schema};
82 $to_serialize->{_frozen_from_class} = $self->schema->class($self->source_moniker);
83
aec3eff1 84 return (Storable::freeze($to_serialize));
85}
86
7137528d 87=head2 STORABLE_thaw
88
4146e3da 89Thaws frozen handle. Resets the internal schema reference to the package
48580715 90variable C<$thaw_schema>. The recommended way of setting this is to use
323e0bd0 91C<< $schema->thaw($ice) >> which handles this for you.
7137528d 92
93=cut
94
4146e3da 95
aec3eff1 96sub STORABLE_thaw {
7cfda9a6 97 my ($self, $cloning, $ice) = @_;
aec3eff1 98 %$self = %{ Storable::thaw($ice) };
3a81f59b 99
7cfda9a6 100 my $class = delete $self->{_frozen_from_class};
3a81f59b 101 if( $thaw_schema ) {
102 $self->{schema} = $thaw_schema;
103 }
104 else {
105 my $rs = $class->result_source_instance;
106 $self->{schema} = $rs->schema if $rs;
107 }
108
4e8205a8 109 carp "Unable to restore schema. Look at 'freeze' and 'thaw' methods in DBIx::Class::Schema."
110 unless $self->{schema};
aec3eff1 111}
112
4146e3da 113=head1 AUTHOR
114
115Ash Berlin C<< <ash@cpan.org> >>
116
117=cut
118
aec3eff1 1191;