line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package PGObject::Type::Composite; |
2
|
|
|
|
|
|
|
|
3
|
1
|
|
|
1
|
|
20429
|
use 5.008; |
|
1
|
|
|
|
|
3
|
|
|
1
|
|
|
|
|
42
|
|
4
|
1
|
|
|
1
|
|
5
|
use Scalar::Util; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
56
|
|
5
|
1
|
|
|
1
|
|
897
|
use PGObject::Util::Catalog::Types qw(get_attributes); |
|
1
|
|
|
|
|
4328
|
|
|
1
|
|
|
|
|
58
|
|
6
|
1
|
|
|
1
|
|
414
|
use PGObject::Util::PseudoCSV; |
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
7
|
|
|
|
|
|
|
use Carp; |
8
|
|
|
|
|
|
|
|
9
|
|
|
|
|
|
|
=head1 NAME |
10
|
|
|
|
|
|
|
|
11
|
|
|
|
|
|
|
PGObject::Type::Composite - Composite Type handler for PGObject |
12
|
|
|
|
|
|
|
|
13
|
|
|
|
|
|
|
=head1 VERSION |
14
|
|
|
|
|
|
|
|
15
|
|
|
|
|
|
|
Version 0.04 |
16
|
|
|
|
|
|
|
|
17
|
|
|
|
|
|
|
=cut |
18
|
|
|
|
|
|
|
|
19
|
|
|
|
|
|
|
our $VERSION = '0.04'; |
20
|
|
|
|
|
|
|
|
21
|
|
|
|
|
|
|
|
22
|
|
|
|
|
|
|
=head1 SYNOPSIS |
23
|
|
|
|
|
|
|
|
24
|
|
|
|
|
|
|
package MyObject; |
25
|
|
|
|
|
|
|
use Moo; |
26
|
|
|
|
|
|
|
with 'PGObject::Type::Composite'; |
27
|
|
|
|
|
|
|
|
28
|
|
|
|
|
|
|
Then |
29
|
|
|
|
|
|
|
|
30
|
|
|
|
|
|
|
use MyObject; |
31
|
|
|
|
|
|
|
my $dbh = DBI->connect; |
32
|
|
|
|
|
|
|
MyObject->initialize(dbh => $dbh); |
33
|
|
|
|
|
|
|
MyObject->register(registry => 'default', type => 'foo'); |
34
|
|
|
|
|
|
|
|
35
|
|
|
|
|
|
|
And now every column of type foo (which must be a composite type) will get |
36
|
|
|
|
|
|
|
deserialized into MyObject. |
37
|
|
|
|
|
|
|
|
38
|
|
|
|
|
|
|
=head1 EXPORTS |
39
|
|
|
|
|
|
|
|
40
|
|
|
|
|
|
|
=over |
41
|
|
|
|
|
|
|
|
42
|
|
|
|
|
|
|
=item initialize |
43
|
|
|
|
|
|
|
|
44
|
|
|
|
|
|
|
=item from_db |
45
|
|
|
|
|
|
|
|
46
|
|
|
|
|
|
|
=item to_db |
47
|
|
|
|
|
|
|
|
48
|
|
|
|
|
|
|
=back |
49
|
|
|
|
|
|
|
|
50
|
|
|
|
|
|
|
=cut |
51
|
|
|
|
|
|
|
|
52
|
|
|
|
|
|
|
=head1 SUBROUTINES/METHODS |
53
|
|
|
|
|
|
|
|
54
|
|
|
|
|
|
|
=head2 initialize |
55
|
|
|
|
|
|
|
|
56
|
|
|
|
|
|
|
=head2 register |
57
|
|
|
|
|
|
|
|
58
|
|
|
|
|
|
|
=head2 from_db |
59
|
|
|
|
|
|
|
|
60
|
|
|
|
|
|
|
=head2 to_db |
61
|
|
|
|
|
|
|
|
62
|
|
|
|
|
|
|
=cut |
63
|
|
|
|
|
|
|
|
64
|
|
|
|
|
|
|
sub import { |
65
|
|
|
|
|
|
|
my ($importer) = caller; |
66
|
|
|
|
|
|
|
my @cols; |
67
|
|
|
|
|
|
|
my $can_has if *{ "${importer}::has" }; # moo/moose lolmoose? |
68
|
|
|
|
|
|
|
|
69
|
|
|
|
|
|
|
my $initialize = sub { |
70
|
|
|
|
|
|
|
my ($pkg, %args) = @_; |
71
|
|
|
|
|
|
|
croak 'first argument must be a package name' if ref $pkg; |
72
|
|
|
|
|
|
|
croak 'Must supply a dbh or columns argument' |
73
|
|
|
|
|
|
|
unless $args{dbh} or scalar @{$args{columns}}; |
74
|
|
|
|
|
|
|
|
75
|
|
|
|
|
|
|
@cols = @{$args{columns}} if @{$args{columns}}; |
76
|
|
|
|
|
|
|
if ($args{dbh} and !@cols){ |
77
|
|
|
|
|
|
|
@cols = get_attributes( |
78
|
|
|
|
|
|
|
typeschema => "$pkg"->_get_schema, |
79
|
|
|
|
|
|
|
typename => "$pkg"->_get_typename, |
80
|
|
|
|
|
|
|
dbh => $args{dbh} |
81
|
|
|
|
|
|
|
); |
82
|
|
|
|
|
|
|
} |
83
|
|
|
|
|
|
|
return @cols; |
84
|
|
|
|
|
|
|
}; |
85
|
|
|
|
|
|
|
|
86
|
|
|
|
|
|
|
my $from_db = sub { |
87
|
|
|
|
|
|
|
my ($to_pkg, $string) = @_; |
88
|
|
|
|
|
|
|
my $hashref = pcsv2hash($string, map { $_->{attname}} @cols); |
89
|
|
|
|
|
|
|
$hashref = { |
90
|
|
|
|
|
|
|
map { $_->{attname} => PGObject::process_type( |
91
|
|
|
|
|
|
|
$hashref->{$_->{attname}}, |
92
|
|
|
|
|
|
|
$_->{atttype}, |
93
|
|
|
|
|
|
|
(eval {$to_pkg->can('_get_registry')} ? |
94
|
|
|
|
|
|
|
"$to_pkg"->_get_registry : |
95
|
|
|
|
|
|
|
'default')) |
96
|
|
|
|
|
|
|
} @cols |
97
|
|
|
|
|
|
|
}; |
98
|
|
|
|
|
|
|
if ($can_has){ # moo/moose |
99
|
|
|
|
|
|
|
return "$pkg"->new(%$hashref); |
100
|
|
|
|
|
|
|
} else { |
101
|
|
|
|
|
|
|
return bless($hashref, $to_pkg); |
102
|
|
|
|
|
|
|
} |
103
|
|
|
|
|
|
|
}; |
104
|
|
|
|
|
|
|
|
105
|
|
|
|
|
|
|
my $to_db = sub { |
106
|
|
|
|
|
|
|
my ($self) = @_; |
107
|
|
|
|
|
|
|
my $hashref = { map { |
108
|
|
|
|
|
|
|
my $att = $_->{attname}; |
109
|
|
|
|
|
|
|
my $val = eval { $self->$att } || $self->{$att}; |
110
|
|
|
|
|
|
|
$att => $val; |
111
|
|
|
|
|
|
|
} @cols }; |
112
|
|
|
|
|
|
|
return { |
113
|
|
|
|
|
|
|
type => $typename, |
114
|
|
|
|
|
|
|
value => hash2pcsv($hashref, map {$_->{attname}} @cols), |
115
|
|
|
|
|
|
|
}; |
116
|
|
|
|
|
|
|
}; |
117
|
|
|
|
|
|
|
|
118
|
|
|
|
|
|
|
my $register = sub { # easier here than also doing export |
119
|
|
|
|
|
|
|
my $self = shift @_; |
120
|
|
|
|
|
|
|
croak "Can't pass reference to register \n". |
121
|
|
|
|
|
|
|
"Hint: use the class instead of the object" if ref $self; |
122
|
|
|
|
|
|
|
my %args = @_; |
123
|
|
|
|
|
|
|
my $registry = $args{registry}; |
124
|
|
|
|
|
|
|
$registry ||= 'default'; |
125
|
|
|
|
|
|
|
my $types = $args{types}; |
126
|
|
|
|
|
|
|
croak 'Must supply types as a hashref' |
127
|
|
|
|
|
|
|
unless defined $types and @$types; |
128
|
|
|
|
|
|
|
for my $type (@$types){ |
129
|
|
|
|
|
|
|
my $ret = |
130
|
|
|
|
|
|
|
PGObject->register_type(registry => $registry, |
131
|
|
|
|
|
|
|
pg_type => $type, |
132
|
|
|
|
|
|
|
perl_class => "$self"); |
133
|
|
|
|
|
|
|
return $ret unless $ret; |
134
|
|
|
|
|
|
|
} |
135
|
|
|
|
|
|
|
return 1; |
136
|
|
|
|
|
|
|
}; |
137
|
|
|
|
|
|
|
my $_get_cols = sub { |
138
|
|
|
|
|
|
|
return @cols; |
139
|
|
|
|
|
|
|
}; |
140
|
|
|
|
|
|
|
|
141
|
|
|
|
|
|
|
no strict 'refs'; |
142
|
|
|
|
|
|
|
*{ "${importer}::initialize" } = $initialize; |
143
|
|
|
|
|
|
|
*{ "${importer}::register" } = $register; |
144
|
|
|
|
|
|
|
*{ "${importer}::from_db" } = $from_db; |
145
|
|
|
|
|
|
|
*{ "${importer}::to_db" } = $to_db; |
146
|
|
|
|
|
|
|
*{ "${importer}::_get_cols" } = $_get_cols; |
147
|
|
|
|
|
|
|
} |
148
|
|
|
|
|
|
|
|
149
|
|
|
|
|
|
|
=head1 AUTHOR |
150
|
|
|
|
|
|
|
|
151
|
|
|
|
|
|
|
Chris Travers, C<< >> |
152
|
|
|
|
|
|
|
|
153
|
|
|
|
|
|
|
=head1 BUGS |
154
|
|
|
|
|
|
|
|
155
|
|
|
|
|
|
|
Please report any bugs or feature requests to C, or through |
156
|
|
|
|
|
|
|
the web interface at L. I will be notified, and then you'll |
157
|
|
|
|
|
|
|
automatically be notified of progress on your bug as I make changes. |
158
|
|
|
|
|
|
|
|
159
|
|
|
|
|
|
|
|
160
|
|
|
|
|
|
|
|
161
|
|
|
|
|
|
|
|
162
|
|
|
|
|
|
|
=head1 SUPPORT |
163
|
|
|
|
|
|
|
|
164
|
|
|
|
|
|
|
You can find documentation for this module with the perldoc command. |
165
|
|
|
|
|
|
|
|
166
|
|
|
|
|
|
|
perldoc PGObject::Type::Composite |
167
|
|
|
|
|
|
|
|
168
|
|
|
|
|
|
|
|
169
|
|
|
|
|
|
|
You can also look for information at: |
170
|
|
|
|
|
|
|
|
171
|
|
|
|
|
|
|
=over 4 |
172
|
|
|
|
|
|
|
|
173
|
|
|
|
|
|
|
=item * RT: CPAN's request tracker (report bugs here) |
174
|
|
|
|
|
|
|
|
175
|
|
|
|
|
|
|
L |
176
|
|
|
|
|
|
|
|
177
|
|
|
|
|
|
|
=item * AnnoCPAN: Annotated CPAN documentation |
178
|
|
|
|
|
|
|
|
179
|
|
|
|
|
|
|
L |
180
|
|
|
|
|
|
|
|
181
|
|
|
|
|
|
|
=item * CPAN Ratings |
182
|
|
|
|
|
|
|
|
183
|
|
|
|
|
|
|
L |
184
|
|
|
|
|
|
|
|
185
|
|
|
|
|
|
|
=item * Search CPAN |
186
|
|
|
|
|
|
|
|
187
|
|
|
|
|
|
|
L |
188
|
|
|
|
|
|
|
|
189
|
|
|
|
|
|
|
=back |
190
|
|
|
|
|
|
|
|
191
|
|
|
|
|
|
|
|
192
|
|
|
|
|
|
|
=head1 ACKNOWLEDGEMENTS |
193
|
|
|
|
|
|
|
|
194
|
|
|
|
|
|
|
|
195
|
|
|
|
|
|
|
=head1 LICENSE AND COPYRIGHT |
196
|
|
|
|
|
|
|
|
197
|
|
|
|
|
|
|
Copyright 2014 Chris Travers. |
198
|
|
|
|
|
|
|
|
199
|
|
|
|
|
|
|
This program is distributed under the (Revised) BSD License: |
200
|
|
|
|
|
|
|
L |
201
|
|
|
|
|
|
|
|
202
|
|
|
|
|
|
|
Redistribution and use in source and binary forms, with or without |
203
|
|
|
|
|
|
|
modification, are permitted provided that the following conditions |
204
|
|
|
|
|
|
|
are met: |
205
|
|
|
|
|
|
|
|
206
|
|
|
|
|
|
|
* Redistributions of source code must retain the above copyright |
207
|
|
|
|
|
|
|
notice, this list of conditions and the following disclaimer. |
208
|
|
|
|
|
|
|
|
209
|
|
|
|
|
|
|
* Redistributions in binary form must reproduce the above copyright |
210
|
|
|
|
|
|
|
notice, this list of conditions and the following disclaimer in the |
211
|
|
|
|
|
|
|
documentation and/or other materials provided with the distribution. |
212
|
|
|
|
|
|
|
|
213
|
|
|
|
|
|
|
* Neither the name of Chris Travers's Organization |
214
|
|
|
|
|
|
|
nor the names of its contributors may be used to endorse or promote |
215
|
|
|
|
|
|
|
products derived from this software without specific prior written |
216
|
|
|
|
|
|
|
permission. |
217
|
|
|
|
|
|
|
|
218
|
|
|
|
|
|
|
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
219
|
|
|
|
|
|
|
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
220
|
|
|
|
|
|
|
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
221
|
|
|
|
|
|
|
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
222
|
|
|
|
|
|
|
OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
223
|
|
|
|
|
|
|
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
224
|
|
|
|
|
|
|
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
225
|
|
|
|
|
|
|
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
226
|
|
|
|
|
|
|
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
227
|
|
|
|
|
|
|
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
228
|
|
|
|
|
|
|
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
229
|
|
|
|
|
|
|
|
230
|
|
|
|
|
|
|
|
231
|
|
|
|
|
|
|
=cut |
232
|
|
|
|
|
|
|
|
233
|
|
|
|
|
|
|
1; # End of PGObject::Type::Composite |