blib/lib/PGObject/Util/PseudoCSV.pm

filter: Statementstmt 93.4% 71 / 76 Branchbran 77.5% 31 / 40 Conditioncond 75.0% 6 / 8 MC/DCmcdc 40.0% 2 / 5 Subroutinesub 91.6% 11 / 12 Podpod 100% 5 / 5 totaltotal 86.3% 126 / 146 CC 24 SCAR 16.7
CC
24
Cov
87%
pseudocsv_parse
8.1
_val
7.1
_parse
6.0
CRAP 5.3
↑ Summary
1 package PGObject::Util::PseudoCSV;
2  
3 4 ▶use 5.008;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 4 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 4 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 4 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 =head1 NAME
9  
10 PGObject::Util::PseudoCSV - Tuple/Array parsing and serialization for PGObject
11  
12 =head1 VERSION
13  
14 Version 2
15  
16 =cut
17  
18 4 our $VERSION = 2.000000;
19  
20  
21 =head1 SYNOPSIS
22  
23 This is a parser and constructor for PostgreSQL text representations of tuples
24 and arrays.
25  
26 To parse:
27  
28 For a tuple, we'd typically:
29  
30 my @list = pseudocsv_parse($text_representation);
31  
32 We can then arrange the hash as:
33  
34 my $hashref = pseudocsv_to_hash(\@list, \@col_list);
35  
36 Which we can combine as:
37  
38 my $hashref = pseudocsv_to_hash(
39 pseudocsv_parse($text_representation),
40 \@col_list
41 );
42  
43 =head1 DESCRIPTION
44  
45 PostgreSQL can represent tuples and arrays in a text format that is almost like
46 CSV. Unfortunately this format has a number of gotchas which render existing
47 CSV-parsers useless. This module provides basic parsing functions to other
48 programs for db interface purposes. With this module you can both parse
49 pseudocsv representations of tuples and arrays and you can create them from a
50 list.
51  
52 The API's here assume you send one (and only one) pseudo-csv record to the API
53 at once. These may be nested, so a single tuple can contain arrays of tuples
54 which can contain arrays of tuples ad infinitum but the parsing only goes one
55 layer deep tuple-wise so that handling classes have an opportunity to re-parse
56 with appropriate type information. Naturally this has performance implications,
57 so depth in SQL structures passed should be reasonably limited.
58  
59 As of 2.0, we no longer automatically call deserialization functions from the
60 parser itself. At his point the calling classes MUST call the deserializer
61 themselves but this is far easier since this has been moved to a separate
62 service in PGObject 2.0. This avoids an unecessary dependency on PGObject
63 and ensures that the module is more geneally useful.
64  
65 =head1 EXPORT
66  
67 =over
68  
69 =item pseudocsv_to_hash
70  
71 =item pseudocsv_parse
72  
73 =item to_pseudocsv
74  
75 =back
76  
77 =cut
78  
79 4 ▶use parent 'Exporter';
Subroutine BEGIN: called CC=1 SCAR=0.0
80  
81 4 our @EXPORT = qw(pseudocsv_to_hash pseudocsv_parse to_pseudocsv
82 hash2pcsv pcsv2hash);
83  
84 =head1 SUBROUTINES/METHODS
85  
86 =head2 pseudocsv_parse
87  
88 This does a one-level deep parse of the pseudo-csv, with additional levels in
89 arrays. When a tuple is encountered it is instantiated as its type but a
90 subarray is parsed for more entities.
91  
92 Only one pseudocsv record can be passed in at once, but embedded newlines are properly handled.
93  
94 =cut
95  
96 11 ▶sub pseudocsv_parse {
Subroutine pseudocsv_parse: called CC=8 SCAR=20.9
Pod documented
97 11 my ($csv, $type, $registry) = @_;
98 11 ▶ if ($csv =~ /^\(/ ) { # tuple
Branch: 2 branches
BranchTrueFalseTotal
if ($csv =~ /^\(/) { } 7 4 11
elsif ($csv =~ /^\{/) { } 4 0 4
99 7 $csv =~ s/^\((.*)\)$/$1/s;
100 } elsif ($csv =~ /^\{/ ) { # array
101 4 $csv =~ s/^\{(.*)\}$/$1/s;
102 }
103 11 ▶ $registry ||= 'default';
Condition50%1/2
$registry ||= "default"
l!l
011
MC/DC0%0/1
$registry ||= "default"
$registry
Truth table50%1/2
A
A= $registry
Aresult
11
00
104 11 my @returnlist = ();
105 11 ▶ while (length($csv)) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $csv) 37 11 48
106 37 my $val = _parse(\$csv);
107 37 my $in_type = $type;
108 37 ▶ $in_type = shift @$type if ref $type eq ref [];
Branch: 1 branch
BranchTrueFalseTotal
if ref $type eq ref [] 0 37 37
109 37 ▶ $val =~ s/""/"/g if defined $val;
Branch: 1 branch
BranchTrueFalseTotal
if defined $val 34 3 37
110 37 push @returnlist, $val;
111 }
112 11 ▶ return @returnlist if wantarray;
Branch: 1 branch
BranchTrueFalseTotal
if wantarray 1 10 11
113 10 return \@returnlist;
114 }
115  
116 =head2 pcsv2hash($literal_string, @cols);
117  
118 Returns a hash from a tuple literal or array literal.
119  
120 =cut
121  
122 1 ▶sub pcsv2hash {
Subroutine pcsv2hash: called CC=3 SCAR=11.1
Pod documented
123 1 my $string = shift;
124 1 ▶ $string = shift if $string eq __PACKAGE__;
Branch: 1 branch
BranchTrueFalseTotal
if $string eq __PACKAGE__ 0 1 1
125 1 my @colnames = @_;
126  
127 1 my @colvals = pseudocsv_parse($string, undef, undef);
128
129 1 my $hash = { map{ $_ => shift @colvals } @colnames };
130 1 ▶ return %$hash if wantarray;
Branch: 1 branch
BranchTrueFalseTotal
if wantarray 0 1 1
131 1 return $hash;
132 }
133  
134 =head2 hash2pcsv($hashref, @cols)
135  
136 Takes an ordered list of columns and a hashref and returns a tuple literal
137  
138 =cut
139  
140 1 ▶sub hash2pcsv {
Subroutine hash2pcsv: called CC=1 SCAR=0.0
Pod documented
141 1 my ($hashref, @cols) = @_;
142 1 return to_pseudocsv([map { $hashref->{$_} } @cols], 1)
143 }
144  
145  
146 # _parse is the private method which does the hard work of parsing.
147  
148 39 ▶sub _parse {
Subroutine _parse: called CC=6 SCAR=17.9
149 39 my ($csvref) = @_;
150 39 my $retval;
151 39 ▶ if ($$csvref =~ /^"/){ # quoted string
Branch: 1 branch
BranchTrueFalseTotal
if ($$csvref =~ /^"/) { } 14 25 39
152 14 $$csvref =~ s/^"(([^"]|"")*)",?//s;
153 14 $retval = $1;
154 14 $retval =~ s/""/"/g;
155 } else {
156 25 $$csvref =~ s/^([^,]*)(,|$)//s;
157 25 $retval = $1;
158 25 ▶ $retval = undef if $retval =~ /^null$/i;
Branch: 1 branch
BranchTrueFalseTotal
if $retval =~ /^null$/i 3 22 25
159 }
160 39 ▶ if (defined $retval and $retval =~ s/^\{(.*)\}$/$1/){
Branch: 1 branch
BranchTrueFalseTotal
if (defined $retval and $retval =~ s/^\{(.*)\}$/$1/) 1 38 39
Condition100%3/3
defined $retval and $retval =~ s/^\{(.*)\}$/$1/
!ll&&!rl&&r
3351
MC/DC100%2/2
defined $retval and $retval =~ s/^\{(.*)\}$/$1/
defined $retval $retval =~ s/^\{(.*)\}$/$1/
Truth table100%3/3
A and B
A= defined $retvalB= $retval =~ s/^\{(.*)\}$/$1/
ABresult
0X0
100
111
161 1 my $listref = [];
162 1 ▶ push @$listref, _parse(\$retval) while $retval;
Branch: 1 branch
BranchTrueFalseTotal
$retval and do { push @$listref, _parse(\$retval) } 2 1 3
163 1 $retval = $listref;
164 }
165 39 return $retval;
166 }
167  
168 =head2 pseudocsv_tohash($coldata, $colnames) DEPRECATED
169  
170 Takes an arrayref of column data and an arrayref of column names and returns
171 a hash. This is mostly a helper function designed to help with tuple types.
172  
173 This interface is deprecated and will go away in 2.0. Use pcsv2hash instead.
174  
175 =cut
176  
177 0 ▶sub pseudocsv_tohash {
Subroutine pseudocsv_tohash: not called CC=2 SCAR=17.9
Pod documented
178 0 my ($cols, $colnames) = @_;
179 0 my $hash = { map{ $_ => shift @$cols } @$colnames };
180 0 ▶ return %$hash if wantarray;
Branch: 1 branch
BranchTrueFalseTotal
if wantarray 0 0 0
181 0 return $hash;
182 }
183  
184 =head2 to_pseudocsv($datalist, $is_tuple)
185  
186 Takes a list of data and an is_tuple argument and creates a pseudocsv.
187  
188 Note: this does not check for array sanity. If you are not careful you can
189 get arrays returned which are not valid SQL arrays. Remember that SQL arrays
190 have every item being the same size, and all SQL arrays are are regular in
191 size (so all 1 and 2d arrays follow the same form as mathematical matrices).
192  
193 =cut
194  
195 18 ▶sub _val {
Subroutine _val: called CC=7 SCAR=19.6
196 18 my ($val, $is_tuple) = @_;
197 18 ▶ return 'NULL' unless defined $val;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $val 2 16 18
198  
199 16 ▶ $val = $val->to_db if eval { $val->can('to_db') };
Branch: 1 branch
BranchTrueFalseTotal
if eval { do { $val->can("to_db") } } 0 16 16
200 16 ▶ $val = to_pseudocsv($_, 0) if ref $_ eq ref [];
Branch: 1 branch
BranchTrueFalseTotal
if ref $_ eq ref [] 1 15 16
201 16 ▶ return $val if ref $_ eq ref [] and !$is_tuple;
Branch: 1 branch
BranchTrueFalseTotal
if ref $_ eq ref [] and not $is_tuple 0 16 16
Condition66%2/3
ref $_ eq ref [] and not $is_tuple
!ll&&!rl&&r
1510
MC/DC0%0/2
ref $_ eq ref [] and not $is_tuple
ref $_ eq ref [] not $is_tuple
Truth table66%2/3
A and not(B)
A= ref $_ eq ref []B= not $is_tuple
ABresult
0X0
100
111
202  
203 16 $val =~ s/"/""/;
204 16 ▶ $val = qq("$val") if $val =~ /(^null$|[",{}])/i;
Branch: 1 branch
BranchTrueFalseTotal
if $val =~ /(^null$|[",{}])/i 2 14 16
205 16 return $val;
206 }
207  
208 5 ▶sub to_pseudocsv {
Subroutine to_pseudocsv: called CC=3 SCAR=11.0
Pod documented
209 5 my ($list, $is_tuple) = @_;
210 5 ▶ Carp::croak 'First arg must be an arrayref' unless ref $list;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $list 0 5 5
211 5 my $csv = join(',', map { _val($_, $is_tuple) } @$list);
212 5 ▶ return qq|($csv)| if $is_tuple;
Branch: 1 branch
BranchTrueFalseTotal
if $is_tuple 3 2 5
213 2 return qq|{$csv}|;
214 }
215  
216 =head1 AUTHOR
217  
218 Chris Travers, C<< <chris.travers at gmail.com> >>
219  
220 =head1 BUGS
221  
222 Please report any bugs or feature requests to C<bug-pgobject-util-pseudocsv at rt.cpan.org>, or through
223 the web interface at L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=PGObject-Util-PseudoCSV>. I will be notified, and then you'll
224 automatically be notified of progress on your bug as I make changes.
225  
226  
227  
228  
229 =head1 SUPPORT
230  
231 You can find documentation for this module with the perldoc command.
232  
233 perldoc PGObject::Util::PseudoCSV
234  
235  
236 You can also look for information at:
237  
238 =over 4
239  
240 =item * RT: CPAN's request tracker (report bugs here)
241  
242 L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=PGObject-Util-PseudoCSV>
243  
244 =item * AnnoCPAN: Annotated CPAN documentation
245  
246 L<http://annocpan.org/dist/PGObject-Util-PseudoCSV>
247  
248 =item * CPAN Ratings
249  
250 L<http://cpanratings.perl.org/d/PGObject-Util-PseudoCSV>
251  
252 =item * Search CPAN
253  
254 L<http://search.cpan.org/dist/PGObject-Util-PseudoCSV/>
255  
256 =back
257  
258  
259 =head1 ACKNOWLEDGEMENTS
260  
261  
262 =head1 LICENSE AND COPYRIGHT
263  
264 Copyright 2014-2017 Chris Travers.
265  
266 Redistribution and use in source and binary forms, with or without modification,
267 are permitted provided that the following conditions are met:
268  
269 * Redistributions of source code must retain the above copyright notice, this
270 list of conditions and the following disclaimer.
271  
272 * Redistributions in binary form must reproduce the above copyright notice, this
273 list of conditions and the following disclaimer in the documentation and/or
274 other materials provided with the distribution.
275  
276 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
277 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
278 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
279 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
280 ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
281 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
282 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
283 ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
284 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
285 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
286  
287 =cut
288  
289 4 1; # End of PGObject::Util::PseudoCSV
↑ Summary