ae11ce6b148b49edd4eba525d58de63784050e21
[dbsrgits/DBIx-Class.git] / lib / DBIx / Class / ResultSourceHandle.pm
1 package DBIx::Class::ResultSourceHandle;
2
3 use strict;
4 use warnings;
5 use Storable;
6
7 use base qw/DBIx::Class/;
8
9 use overload
10     # on some RH perls the following line causes serious performance problem
11     # see https://bugzilla.redhat.com/show_bug.cgi?id=196836
12     q/""/ => sub { __PACKAGE__ . ":" . shift->source_moniker; },
13     fallback => 1;
14
15 __PACKAGE__->mk_group_accessors('simple' => qw/schema source_moniker/);
16
17 =head1 NAME
18
19 DBIx::Class::ResultSourceHandle
20
21 =head1 DESCRIPTION
22
23 This module removes fixed link between Rows/ResultSets and the actual source
24 objects, which gets round the following problems
25
26 =over 4
27
28 =item *
29
30 Needing to keep C<$schema> in scope, since any objects/result_sets
31 will have a C<$schema> object through their source handle
32
33 =item *
34
35 Large output when using Data::Dump(er) since this class can be set to
36 stringify to almost nothing
37
38 =item *
39
40 Closer to being able to do a Serialize::Storable that doesn't require class-based connections
41
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
60 Resolve the moniker into the actual ResultSource object
61
62 =cut
63
64 sub resolve { return $_[0]->schema->source($_[0]->source_moniker) }
65
66 =head2 STORABLE_freeze
67
68 Freezes a handle.
69
70 =cut
71
72 sub STORABLE_freeze {
73     my ($self, $cloning) = @_;
74     my $to_serialize = { %$self };
75     delete $to_serialize->{schema};
76     return (Storable::freeze($to_serialize));
77 }
78
79 =head2 STORABLE_thaw
80
81 Thaws frozen handle.
82
83 =cut
84
85 sub STORABLE_thaw {
86     my ($self, $cloning,$ice) = @_;
87     %$self = %{ Storable::thaw($ice) };
88 }
89
90 1;