line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package DBIx::Class::InflateColumn::Serializer::Storable; |
2
|
|
|
|
|
|
|
$DBIx::Class::InflateColumn::Serializer::Storable::VERSION = '0.09'; |
3
|
|
|
|
|
|
|
=head1 NAME |
4
|
|
|
|
|
|
|
|
5
|
|
|
|
|
|
|
DBIx::Class::InflateColumn::Serializer::Storable - Storable Inflator |
6
|
|
|
|
|
|
|
|
7
|
|
|
|
|
|
|
=head1 SYNOPSIS |
8
|
|
|
|
|
|
|
|
9
|
|
|
|
|
|
|
package MySchema::Table; |
10
|
|
|
|
|
|
|
use base 'DBIx::Class'; |
11
|
|
|
|
|
|
|
|
12
|
|
|
|
|
|
|
__PACKAGE__->load_components('InflateColumn::Serializer', 'Core'); |
13
|
|
|
|
|
|
|
__PACKAGE__->add_columns( |
14
|
|
|
|
|
|
|
'data_column' => { |
15
|
|
|
|
|
|
|
'data_type' => 'VARCHAR', |
16
|
|
|
|
|
|
|
'size' => 255, |
17
|
|
|
|
|
|
|
'serializer_class' => 'Storable' |
18
|
|
|
|
|
|
|
} |
19
|
|
|
|
|
|
|
); |
20
|
|
|
|
|
|
|
|
21
|
|
|
|
|
|
|
Then in your code... |
22
|
|
|
|
|
|
|
|
23
|
|
|
|
|
|
|
my $struct = { 'I' => { 'am' => 'a struct' }; |
24
|
|
|
|
|
|
|
$obj->data_column($struct); |
25
|
|
|
|
|
|
|
$obj->update; |
26
|
|
|
|
|
|
|
|
27
|
|
|
|
|
|
|
And you can recover your data structure with: |
28
|
|
|
|
|
|
|
|
29
|
|
|
|
|
|
|
my $obj = ...->find(...); |
30
|
|
|
|
|
|
|
my $struct = $obj->data_column; |
31
|
|
|
|
|
|
|
|
32
|
|
|
|
|
|
|
The data structures you assign to "data_column" will be saved in the database in Storable format. |
33
|
|
|
|
|
|
|
|
34
|
|
|
|
|
|
|
=cut |
35
|
|
|
|
|
|
|
|
36
|
3
|
|
|
3
|
|
12
|
use strict; |
|
3
|
|
|
|
|
5
|
|
|
3
|
|
|
|
|
79
|
|
37
|
3
|
|
|
3
|
|
10
|
use warnings; |
|
3
|
|
|
|
|
3
|
|
|
3
|
|
|
|
|
72
|
|
38
|
3
|
|
|
3
|
|
13
|
use Storable qw//; |
|
3
|
|
|
|
|
3
|
|
|
3
|
|
|
|
|
49
|
|
39
|
3
|
|
|
3
|
|
11
|
use Carp; |
|
3
|
|
|
|
|
4
|
|
|
3
|
|
|
|
|
764
|
|
40
|
|
|
|
|
|
|
|
41
|
|
|
|
|
|
|
=over 4 |
42
|
|
|
|
|
|
|
|
43
|
|
|
|
|
|
|
=item get_freezer |
44
|
|
|
|
|
|
|
|
45
|
|
|
|
|
|
|
Called by DBIx::Class::InflateColumn::Serializer to get the routine that serializes |
46
|
|
|
|
|
|
|
the data passed to it. Returns a coderef. |
47
|
|
|
|
|
|
|
|
48
|
|
|
|
|
|
|
=cut |
49
|
|
|
|
|
|
|
|
50
|
|
|
|
|
|
|
sub get_freezer{ |
51
|
6
|
|
|
6
|
1
|
10
|
my ($class, $column, $info, $args) = @_; |
52
|
|
|
|
|
|
|
|
53
|
6
|
100
|
|
|
|
16
|
if (defined $info->{'size'}){ |
54
|
3
|
|
|
|
|
4
|
my $size = $info->{'size'}; |
55
|
|
|
|
|
|
|
return sub { |
56
|
2
|
|
|
2
|
|
345
|
my $s = Storable::nfreeze(shift); |
57
|
2
|
100
|
|
|
|
100
|
croak "serialization too big" if (length($s) > $size); |
58
|
1
|
|
|
|
|
3
|
return $s; |
59
|
3
|
|
|
|
|
25
|
}; |
60
|
|
|
|
|
|
|
} else { |
61
|
|
|
|
|
|
|
return sub { |
62
|
2
|
|
|
2
|
|
123628
|
return Storable::nfreeze(shift); |
63
|
3
|
|
|
|
|
19
|
}; |
64
|
|
|
|
|
|
|
} |
65
|
|
|
|
|
|
|
} |
66
|
|
|
|
|
|
|
|
67
|
|
|
|
|
|
|
=item get_unfreezer |
68
|
|
|
|
|
|
|
|
69
|
|
|
|
|
|
|
Called by DBIx::Class::InflateColumn::Serializer to get the routine that deserializes |
70
|
|
|
|
|
|
|
the data stored in the column. Returns a coderef. |
71
|
|
|
|
|
|
|
|
72
|
|
|
|
|
|
|
=back |
73
|
|
|
|
|
|
|
|
74
|
|
|
|
|
|
|
=cut |
75
|
|
|
|
|
|
|
|
76
|
|
|
|
|
|
|
|
77
|
|
|
|
|
|
|
sub get_unfreezer { |
78
|
|
|
|
|
|
|
return sub { |
79
|
3
|
|
|
3
|
|
10881
|
my $value = shift; |
80
|
|
|
|
|
|
|
# Storable returns undef if the datastructure couldn't be thawed. |
81
|
|
|
|
|
|
|
# Other deserializers throw exceptions, so we'll do the same. |
82
|
|
|
|
|
|
|
# If the column had a NULL value, then we return it (don't want to die) |
83
|
3
|
50
|
|
|
|
9
|
return undef if (not defined $value); |
84
|
3
|
|
|
|
|
10
|
my $s = Storable::thaw($value); |
85
|
3
|
50
|
|
|
|
68
|
croak "Storable couldn't thaw the value" if (not defined $s); |
86
|
3
|
|
|
|
|
16
|
return $s; |
87
|
6
|
|
|
6
|
1
|
27
|
}; |
88
|
|
|
|
|
|
|
} |
89
|
|
|
|
|
|
|
|
90
|
|
|
|
|
|
|
|
91
|
|
|
|
|
|
|
1; |