WDDX/Recordset.pm

filter: Statementstmt 2.3% 6 / 256 Branchbran 0.0% 0 / 160 Conditioncond 0.0% 0 / 52 MC/DCmcdc 0.0% 0 / 30 Subroutinesub 5.4% 2 / 37 Podpod n/a totaltotal 1.5% 8 / 535 CC 105 SCAR 42.6
CC
105
Cov
1%
new
240.0
start_tag
210.0
_serialize
90.0
CRAP 70.5
↑ Summary WDDX.pm »
1 #!/usr/bin/perl -w
2 #
3 # $Id: Recordset.pm,v 1.1.1.1 2003/10/28 16:04:37 andy Exp $
4 #
5 # This code is copyright 1999-2000 by Scott Guelich <scott@scripted.com>
6 # and is distributed according to the same conditions as Perl itself
7 # Please visit http://www.scripted.com/wddx/ for more information
8 #
9  
10 package WDDX::Recordset;
11  
12 # Auto-inserted by build scripts
13 $VERSION = "1.00";
14  
15 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 1 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
17  
18 require WDDX;
19  
20 my @Data_Types = qw( boolean number string datetime binary null );
21  
22 { my $i_hate_the_w_flag_sometimes = [
23 $WDDX::PACKET_HEADER,
24 $WDDX::PACKET_FOOTER,
25 $WDDX::Recordset::VERSION
26 ] }
27  
28 1;
29  
30  
31 #/-----------------------------------------------------------------------
32 # Public Methods
33 #
34  
35 0 ▶sub new {
Subroutine new: not called CC=15 SCAR=54.8
36 0 my( $class, $names, $types, $data ) = @_;
37 0 my( @names, @types, $value ) = ();
38
39 0 ▶ unless ( defined $names and eval { $#$names || 1 } and
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $names and eval { do { $names->$#* or 1 } } and defined $types and eval { do { $types->$#* or 1 } }) 0 0 0
Condition0%0/11
defined $names and eval { do { $names->$#* or 1 } } and defined $types and eval { do { $types->$#* or 1 } }
!ll&&!rl&&r
000
defined $names and eval { do { $names->$#* or 1 } } and defined $types
!ll&&!rl&&r
000
defined $names and eval { do { $names->$#* or 1 } }
!ll&&!rl&&r
000
$names->$#* || 1
l!l
00
MC/DC0%0/4
defined $names and eval { do { $names->$#* or 1 } } and defined $types and eval { do { $types->$#* or 1 } }
defined $names eval { do { $names->$#* or 1 } } defined $types eval { do { $types->$#* or 1 } }
MC/DC0%0/1
$names->$#* || 1
$names->$#*
Truth table0%0/5
A and B and C and D
A= defined $namesB= eval { do { $names->$#* or 1 } }C= defined $typesD= eval { do { $types->$#* or 1 } }
ABCDresult
0XXX0
10XX0
110X0
11100
11111
Truth table0%0/2
A
A= $names->$#*
Aresult
11
00
40 0 ▶ defined $types and eval { $#$types || 1 } ) {
Condition0%0/2
$types->$#* || 1
l!l
00
MC/DC0%0/1
$types->$#* || 1
$types->$#*
Truth table0%0/2
A
A= $types->$#*
Aresult
11
00
41 0 croak "You must supply array refs for names and data types " .
42 "when creating a new $class object";
43 }
44
45 0 ▶ croak "Name and type arrays must contain the same number of elements"
Branch: 1 branch
BranchTrueFalseTotal
unless @$names == @$types 0 0 0
46 unless @$names == @$types;
47
48 0 my $type;
49 0 foreach $type ( @$types ) {
50 0 ▶ next unless defined $type; # supports deserializing empty recordsets
Branch: 1 branch
BranchTrueFalseTotal
unless defined $type 0 0 0
51 0 $type = lc $type;
52 0 ▶ die "Unsupported data type: '$type'" unless
Branch: 1 branch
BranchTrueFalseTotal
unless grep(($type eq $_), @Data_Types) 0 0 0
53 grep $type eq $_, @Data_Types;
54 }
55
56 0 my $row;
57 0 my $i = 0;
58 0 foreach $row ( @$data ) {
59 0 $i++;
60 0 ▶ unless ( ref( $row ) =~ /ARRAY/ ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (ref($row) =~ /ARRAY/) 0 0 0
61 0 croak "Third argument must be a ref to an array of array " .
62 "refs (i.e. a table)";
63 }
64 0 ▶ unless ( @$row == @$names ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@$row == @$names) 0 0 0
65 0 croak "The number of fields in row $i does not match the " .
66 "number of declared names";
67 }
68 }
69
70 0 my @invalid = grep ! /^[_A-Za-z][_.0-9A-Za-z]*$/, @$names;
71 0 ▶ croak "Invalid field names in recordset: @invalid" if @invalid;
Branch: 1 branch
BranchTrueFalseTotal
if @invalid 0 0 0
72
73 0 my $self = {
74 names => $names,
75 types => $types,
76 value => $data,
77 };
78
79 0 bless $self, $class;
80 0 return $self;
81 }
82  
83  
84 0 ▶sub type {
Subroutine type: not called CC=1 SCAR=6.9
85 0 return "recordset";
86 }
87  
88  
89 0 ▶sub as_packet {
Subroutine as_packet: not called CC=1 SCAR=6.9
90 0 my( $self ) = @_;
91 0 my $output = $WDDX::PACKET_HEADER .
92 $self->_serialize .
93 $WDDX::PACKET_FOOTER;
94 }
95  
96  
97 0 ▶sub as_arrayref {
Subroutine as_arrayref: not called CC=1 SCAR=6.9
98 0 my( $self ) = @_;
99 0 return $self->_deserialize;
100 }
101  
102  
103 0 ▶sub as_javascript {
Subroutine as_javascript: not called CC=4 SCAR=30.0
104 0 my( $self, $js_var ) = @_;
105 0 my $output = "$js_var=new WddxRecordset();";
106 0 my $types = $self->types;
107
108 0 ▶ for ( my $col = 0; $col < $self->num_columns; $col++ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($col < $self->num_columns) 0 0 0
109 0 my $name = $self->names()->[$col];
110 0 $output .= "$js_var.$name=new Array();";
111 0 ▶ for ( my $row = 0; $row < $self->num_rows; $row++ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($row < $self->num_rows) 0 0 0
112 0 my $field = $self->get_element( $col, $row );
113 0 my $var = eval "WDDX::\u$types->[$col]\->new( \$field )";
114 0 ▶ die "$@\n" if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 0 0
115 0 $output .= $var->as_javascript( "$js_var.$name\[$row\]" );
116 }
117 }
118 0 return $output;
119 }
120  
121  
122 #/-----------------------------------------------------------------------
123 # Other Public Methods
124 #
125  
126  
127 0 ▶sub num_rows {
Subroutine num_rows: not called CC=1 SCAR=6.9
128 0 my( $self ) = @_;
129 0 return scalar @{ $self->table };
130 }
131  
132 0 ▶sub num_columns {
Subroutine num_columns: not called CC=1 SCAR=6.9
133 0 my( $self ) = @_;
134 0 return scalar @{ $self->{'names'} };
135 }
136  
137 # Returns an array of the field names
138 0 ▶sub names {
Subroutine names: not called CC=3 SCAR=24.8
139 0 my( $self, $new_names ) = @_;
140
141 0 ▶ if ( defined $new_names ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $new_names) 0 0 0
142 0 ▶ croak "You must supply an array ref when setting names"
Branch: 1 branch
BranchTrueFalseTotal
unless ref $new_names 0 0 0
143 unless ref $new_names;
144 0 $self->{'names'} = $new_names;
145 }
146
147 0 return $self->{'names'};
148 }
149  
150 0 ▶sub types {
Subroutine types: not called CC=3 SCAR=24.8
151 0 my( $self, $new_types ) = @_;
152
153 0 ▶ if ( defined $new_types ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $new_types) 0 0 0
154 0 ▶ croak "You must supply an array ref when setting types"
Branch: 1 branch
BranchTrueFalseTotal
unless ref $new_types 0 0 0
155 unless ref $new_types;
156 0 $self->{'types'} = $new_types;
157 }
158
159 0 return $self->{'types'};
160 }
161  
162 0 ▶sub table {
Subroutine table: not called CC=3 SCAR=24.8
163 0 my( $self, $new_value ) = @_;
164
165 0 ▶ if ( defined $new_value ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $new_value) 0 0 0
166 0 ▶ croak "You must supply an array ref when setting the table data"
Branch: 1 branch
BranchTrueFalseTotal
unless ref $new_value 0 0 0
167 unless ref $new_value;
168 0 $self->{'value'} = $new_value;
169 }
170
171 0 return $self->{value};
172 }
173  
174 # Takes field name or number and returns array ref for that field
175 0 ▶sub get_column {
Subroutine get_column: not called CC=4 SCAR=30.0
176 0 my( $self, $label ) = @_;
177 0 my $data = $self->table;
178 0 ▶ my $index = ( $label =~ /^\d+$/ ? $label : $self->get_index( $label ) );
Branch: 1 branch
BranchTrueFalseTotal
$label =~ /^\d+$/ ? : 0 0 0
179
180 0 ▶ croak "Invalid column name" unless defined( $index );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $index 0 0 0
181 0 ▶ croak "Column $index doesn't exist" if $index > $self->num_columns;
Branch: 1 branch
BranchTrueFalseTotal
if $index > $self->num_columns 0 0 0
182
183 0 my @result = map $_->[$index], @$data;
184 0 return \@result;
185 }
186  
187 0 ▶sub set_column {
Subroutine set_column: not called CC=7 SCAR=40.3
188 0 my( $self, $label, $col ) = @_;
189 0 my $data = $self->table;
190 0 ▶ my $index = ( $label =~ /^\d+$/ ? $label : $self->get_index( $label ) );
Branch: 1 branch
BranchTrueFalseTotal
$label =~ /^\d+$/ ? : 0 0 0
191
192 0 ▶ croak "Column not an array reference" unless ref( $col ) =~ /ARRAY/;
Branch: 1 branch
BranchTrueFalseTotal
unless ref($col) =~ /ARRAY/ 0 0 0
193 0 ▶ croak "Invalid column name: '$label'" unless defined( $index );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $index 0 0 0
194 0 ▶ croak "Column $index doesn't exist" if $index > $self->num_columns;
Branch: 1 branch
BranchTrueFalseTotal
if $index > $self->num_columns 0 0 0
195
196 0 ▶ for ( my $i = 0; $i < @$col; $i++ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @$col) 0 0 0
197 0 $data->[$i][$index] = $col->[$i];
198 }
199 # This fills in the rest of the col with undef if they passed
200 # fewer elements than the number the col currently has
201 0 ▶ for ( my $i = @$col; $i < $self->num_rows; $i++ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $self->num_rows) 0 0 0
202 0 $data->[$i][$index] = undef;
203 }
204 }
205  
206 0 ▶sub add_column {
Subroutine add_column: not called CC=5 SCAR=34.0
207 0 my( $self, $name, $type, $col ) = @_;
208 0 my $data = $self->table;
209 0 my $names = $self->names;
210 0 my $types = $self->types;
211
212 0 ▶ croak "You must supply the name and type of the column" unless @_ >= 4;
Branch: 1 branch
BranchTrueFalseTotal
unless @_ >= 4 0 0 0
213 0 ▶ croak "Column not an array reference" unless ref( $col ) =~ /ARRAY/;
Branch: 1 branch
BranchTrueFalseTotal
unless ref($col) =~ /ARRAY/ 0 0 0
214 0 ▶ croak "Duplicate column name: '$name'" if
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->get_index($name) 0 0 0
215 defined( $self->get_index( $name ) );
216
217 0 push @$names, $name;
218 0 push @$types, $type;
219
220 0 ▶ for ( my $i = 0; $i < @$col; $i++ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @$col) 0 0 0
221 0 push @{ $data->[$i] }, $col->[$i];
222 }
223 }
224  
225 0 ▶sub del_column {
Subroutine del_column: not called CC=5 SCAR=34.0
226 0 my( $self, $label ) = @_;
227 0 my $data = $self->table;
228 0 ▶ my $index = ( $label =~ /^\d+$/ ? $label : $self->get_index( $label ) );
Branch: 1 branch
BranchTrueFalseTotal
$label =~ /^\d+$/ ? : 0 0 0
229
230 0 ▶ croak "Invalid column name: '$label'" unless defined( $index );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $index 0 0 0
231 0 ▶ croak "Column $index doesn't exist" if $index > $self->num_columns;
Branch: 1 branch
BranchTrueFalseTotal
if $index > $self->num_columns 0 0 0
232
233 0 _del_from_array( $self->{'names'}, $index );
234 0 _del_from_array( $self->{'types'}, $index );
235
236 0 foreach ( @$data ) {
237 0 _del_from_array( $_, $index );
238 }
239 }
240  
241 # Pass array ref and index to delete; deletes array in place
242 0 ▶sub _del_from_array {
Subroutine _del_from_array: not called CC=4 SCAR=30.0
243 0 my( $arrayref, $del_idx ) = @_;
244
245 0 ▶ return if $del_idx > $#$arrayref;
Branch: 1 branch
BranchTrueFalseTotal
if $del_idx > $arrayref->$#* 0 0 0
246 0 ▶ for ( my $i = 0; $i < @$arrayref; $i++ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @$arrayref) 0 0 0
247 0 ▶ $arrayref->[$i] = $i >= $del_idx ? $arrayref->[$i+1] : $arrayref->[$i];
Branch: 1 branch
BranchTrueFalseTotal
$i >= $del_idx ? : 0 0 0
248 }
249 0 $#$arrayref--;
250 }
251  
252  
253 0 ▶sub get_row {
Subroutine get_row: not called CC=2 SCAR=17.9
254 0 my( $self, $row_num ) = @_;
255 0 ▶ croak "Row $row_num doesn't exist" if $row_num > $self->num_rows;
Branch: 1 branch
BranchTrueFalseTotal
if $row_num > $self->num_rows 0 0 0
256 0 return $self->table->[$row_num];
257 }
258  
259 0 ▶sub set_row {
Subroutine set_row: not called CC=4 SCAR=30.0
260 0 my( $self, $row_num, $row ) = @_;
261
262 0 ▶ croak "Row not an array reference" unless ref( $row ) =~ /ARRAY/;
Branch: 1 branch
BranchTrueFalseTotal
unless ref($row) =~ /ARRAY/ 0 0 0
263 0 ▶ croak "Row $row_num doesn't exist" if $row_num > $self->num_rows;
Branch: 1 branch
BranchTrueFalseTotal
if $row_num > $self->num_rows 0 0 0
264 0 ▶ croak "Number of elements in row does not match number of columns in " .
Branch: 1 branch
BranchTrueFalseTotal
unless @$row == $self->num_columns 0 0 0
265 "recordset" unless @$row == $self->num_columns;
266
267 0 $self->table->[$row_num] = $row;
268 }
269  
270 0 ▶sub add_row {
Subroutine add_row: not called CC=3 SCAR=24.8
271 0 my( $self, $row ) = @_;
272 0 my $data = $self->table;
273
274 0 ▶ croak "Row not an array reference" unless ref( $row ) =~ /ARRAY/;
Branch: 1 branch
BranchTrueFalseTotal
unless ref($row) =~ /ARRAY/ 0 0 0
275 0 ▶ croak "Number of elements in row does not match number of columns in " .
Branch: 1 branch
BranchTrueFalseTotal
unless @$row == $self->num_columns 0 0 0
276 "recordset" unless @$row == $self->num_columns;
277
278 0 push @{ $self->table }, $row;
279 }
280  
281 0 ▶sub _check_data_type {
Subroutine _check_data_type: not called CC=4 SCAR=30.0
282 0 my( $self, $num_rows ) = @_;
283
284 0 ▶ if ( @{ $self->types } ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@{$self->types;}) { } 0 0 0
285 0 ▶ croak "Number of elements in row does not match number of columns in " .
Branch: 1 branch
BranchTrueFalseTotal
unless $num_rows == $self->num_columns 0 0 0
286 "recordset" unless $num_rows == $self->num_columns;
287 }
288 else {
289 0 warn "No data types defined for this recordset; assuming 'string'.\n";
290 0 my @types;
291 0 for ( 1 .. $num_rows ) { push @types, "string"; }
292 0 $self->{'types'} = \@types;
293 }
294  
295 }
296  
297 0 ▶sub del_row {
Subroutine del_row: not called CC=2 SCAR=17.9
298 0 my( $self, $row_num ) = @_;
299 0 my $data = $self->table;
300
301 0 ▶ croak "Row $row_num doesn't exist" if $row_num > $self->num_rows;
Branch: 1 branch
BranchTrueFalseTotal
if $row_num > $self->num_rows 0 0 0
302
303 0 _del_from_array( $data, $row_num );
304 }
305  
306  
307 # Deprecated
308 0 ▶sub get_field {
Subroutine get_field: not called CC=3 SCAR=24.8
309 0 my( $self, $row_num, $col_num ) = @_;
310 0 my $data = $self->table;
311
312 0 carp "get_field is deprecated; you should use get_element instead";
313 0 ▶ croak "Field [$row_num,$col_num] doesn't exist" if
Branch: 1 branch
BranchTrueFalseTotal
if $row_num > $self->num_rows or $col_num > $self->num_columns 0 0 0
Condition0%0/3
$row_num > $self->num_rows or $col_num > $self->num_columns
l!l&&r!l&&!r
000
MC/DC0%0/2
$row_num > $self->num_rows or $col_num > $self->num_columns
$row_num > $self->num_rows $col_num > $self->num_columns
Truth table0%0/3
A or B
A= $row_num > $self->num_rowsB= $col_num > $self->num_columns
ABresult
1X1
011
000
314 $row_num > $self->num_rows or $col_num > $self->num_columns;
315
316 0 return $data->[$row_num][$col_num];
317 }
318  
319 # Deprecated
320 0 ▶sub set_field {
Subroutine set_field: not called CC=3 SCAR=24.8
321 0 my( $self, $row_num, $col_num, $value ) = @_;
322 0 my $data = $self->table;
323
324 0 carp "set_field is deprecated; you should use set_element instead";
325 0 ▶ croak "Field [$row_num,$col_num] doesn't exist" if
Branch: 1 branch
BranchTrueFalseTotal
if $row_num > $self->num_rows or $col_num > $self->num_columns 0 0 0
Condition0%0/3
$row_num > $self->num_rows or $col_num > $self->num_columns
l!l&&r!l&&!r
000
MC/DC0%0/2
$row_num > $self->num_rows or $col_num > $self->num_columns
$row_num > $self->num_rows $col_num > $self->num_columns
Truth table0%0/3
A or B
A= $row_num > $self->num_rowsB= $col_num > $self->num_columns
ABresult
1X1
011
000
326 $row_num > $self->num_rows or $col_num > $self->num_columns;
327
328 0 $data->[$row_num][$col_num] = $value;
329 }
330  
331  
332 0 ▶sub get_element {
Subroutine get_element: not called CC=5 SCAR=34.0
333 0 my( $self, $label, $row_num ) = @_;
334 0 my $data = $self->table;
335 0 ▶ my $col_num = ( $label =~ /^\d+$/ ? $label : $self->get_index( $label ) );
Branch: 1 branch
BranchTrueFalseTotal
$label =~ /^\d+$/ ? : 0 0 0
336
337 0 ▶ croak "Field [ $label, $row_num ] doesn't exist" if
Branch: 1 branch
BranchTrueFalseTotal
if not defined $col_num or $row_num >= $self->num_rows or $col_num >= $self->num_columns 0 0 0
Condition0%0/6
not defined $col_num or $row_num >= $self->num_rows or $col_num >= $self->num_columns
l!l&&r!l&&!r
000
not defined $col_num or $row_num >= $self->num_rows
l!l&&r!l&&!r
000
MC/DC0%0/3
not defined $col_num or $row_num >= $self->num_rows or $col_num >= $self->num_columns
not defined $col_num $row_num >= $self->num_rows $col_num >= $self->num_columns
Truth table0%0/4
not(A) or B or C
A= not defined $col_numB= $row_num >= $self->num_rowsC= $col_num >= $self->num_columns
ABCresult
1XX1
01X1
0011
0000
338 ! defined( $col_num ) or
339 $row_num >= $self->num_rows or
340 $col_num >= $self->num_columns;
341
342 0 return $data->[$row_num][$col_num];
343 }
344  
345 0 ▶sub set_element {
Subroutine set_element: not called CC=5 SCAR=34.0
346 0 my( $self, $label, $row_num, $value ) = @_;
347 0 my $data = $self->table;
348 0 ▶ my $col_num = ( $label =~ /^\d+$/ ? $label : $self->get_index( $label ) );
Branch: 1 branch
BranchTrueFalseTotal
$label =~ /^\d+$/ ? : 0 0 0
349
350 0 ▶ croak "Field [ $label, $row_num ] doesn't exist" if
Branch: 1 branch
BranchTrueFalseTotal
if not defined $col_num or $row_num >= $self->num_rows or $col_num >= $self->num_columns 0 0 0
Condition0%0/6
not defined $col_num or $row_num >= $self->num_rows or $col_num >= $self->num_columns
l!l&&r!l&&!r
000
not defined $col_num or $row_num >= $self->num_rows
l!l&&r!l&&!r
000
MC/DC0%0/3
not defined $col_num or $row_num >= $self->num_rows or $col_num >= $self->num_columns
not defined $col_num $row_num >= $self->num_rows $col_num >= $self->num_columns
Truth table0%0/4
not(A) or B or C
A= not defined $col_numB= $row_num >= $self->num_rowsC= $col_num >= $self->num_columns
ABCresult
1XX1
01X1
0011
0000
351 ! defined( $col_num ) or
352 $row_num >= $self->num_rows or
353 $col_num >= $self->num_columns;
354
355 0 $data->[$row_num][$col_num] = $value;
356 }
357  
358  
359 0 ▶sub get_index {
Subroutine get_index: not called CC=3 SCAR=24.8
360 0 my( $self, $name ) = @_;
361
362 0 ▶ for ( my $i = 0; $i < @{ $self->{'names'} }; $i++ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @{$self->{"names"};}) 0 0 0
363 0 ▶ return $i if lc $name eq lc $self->{'names'}[$i];
Branch: 1 branch
BranchTrueFalseTotal
if lc $name eq lc $self->{"names"}[$i] 0 0 0
364 }
365 0 return undef;
366 }
367  
368  
369  
370 #/-----------------------------------------------------------------------
371 # Private Methods
372 #
373  
374 0 ▶sub is_parser {
Subroutine is_parser: not called CC=1 SCAR=6.9
375 0 return 0;
376 }
377  
378  
379 0 ▶sub _serialize {
Subroutine _serialize: not called CC=9 SCAR=45.0
380 0 my( $self ) = @_;
381 0 my $table = $self->table;
382 0 my $names = $self->names;
383 0 my $types = $self->types;
384 0 my $rows = $self->num_rows;
385 0 my $names_str = join ",", @$names;
386 0 my $type;
387
388 # We don't need to worry about data types if we don't have any data
389 0 ▶ if ( $self->num_rows ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->num_rows) 0 0 0
390 0 foreach $type ( @$types ) {
391 0 ▶ croak "No data types were defined for this recordset" unless
Branch: 1 branch
BranchTrueFalseTotal
unless defined $type 0 0 0
392 defined $type;
393 0 ▶ die "Unsupported data type: '$_'" unless
Branch: 1 branch
BranchTrueFalseTotal
unless grep(($type eq $_), @Data_Types) 0 0 0
394 grep $type eq $_, @Data_Types;
395 }
396 }
397
398 0 my $output = "<recordset rowCount='$rows' fieldNames='$names_str'>";
399
400 0 ▶ for ( my $col_idx = 0; $col_idx < $self->num_columns; $col_idx++ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($col_idx < $self->num_columns) 0 0 0
401 0 $output .= "<field name='$names->[$col_idx]'>";
402 0 my $column = $self->get_column( $col_idx );
403 0 my $field;
404 0 foreach $field ( @$column ) {
405 0 ▶ my $var = defined( $field ) ?
Branch: 1 branch
BranchTrueFalseTotal
defined $field ? : 0 0 0
406 eval "WDDX::\u$types->[$col_idx]\->new( \$field )" :
407 new WDDX::Null();
408 0 ▶ die "$@\n" if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 0 0
409 0 $output .= $var->_serialize;
410 }
411 0 $output .= "</field>";
412 }
413
414 0 $output .= "</recordset>";
415 0 return $output;
416 }
417  
418  
419 0 ▶sub _deserialize {
Subroutine _deserialize: not called CC=1 SCAR=6.9
420 0 my( $self ) = @_;
421 0 return $self;
422 }
423  
424 #/-----------------------------------------------------------------------
425 # Parsing Code
426 #
427  
428 package WDDX::Recordset::Parser;
429  
430  
431 0 ▶sub new {
Subroutine new: not called CC=1 SCAR=6.9
432 0 my $class = shift;
433
434 0 my $self = {
435 row_count => 0,
436 names => "",
437 value => [],
438 curr_field => -1,
439 curr_row => -1,
440 parse_var => "",
441 types => [],
442 seen_recordsets => 0,
443 };
444 0 return bless $self, $class;
445 }
446  
447  
448 0 ▶sub start_tag {
Subroutine start_tag: not called CC=14 SCAR=53.5
449 0 my( $self, $element, $attribs ) = @_;
450 0 my $parse_var = $self->parse_var;
451
452 0 ▶ if ( $element eq "recordset" and not $self->{seen_recordsets}++ ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($element eq 'recordset' and not $self->{'seen_recordsets'}++) { } 0 0 0
elsif ($element eq 'field' and $self->{'seen_recordsets'} == 1) { } 0 0 0
Condition0%0/3
$element eq "recordset" and not $self->{"seen_recordsets"}++
!ll&&!rl&&r
000
MC/DC0%0/2
$element eq "recordset" and not $self->{"seen_recordsets"}++
$element eq "recordset" not $self->{"seen_recordsets"}++
Truth table0%0/3
A and not(B)
A= $element eq "recordset"B= not $self->{"seen_recordsets"}++
ABresult
0X0
100
111
453 0 ▶ unless ( $attribs->{rowcount} =~ /^\d+$/ ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($attribs->{"rowcount"} =~ /^\d+$/) 0 0 0
454 0 die "Invalid value for rowCount attribute in <recordset> tag\n";
455 }
456
457 0 my @names = split ",", $attribs->{fieldnames};
458 0 ▶ if ( ! @names or grep ! /^[_A-Za-z][_.0-9A-Za-z]*$/, @names ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not @names or grep((!/^[_A-Za-z][_.0-9A-Za-z]*$/), @names)) 0 0 0
Condition0%0/3
not @names or grep((!/^[_A-Za-z][_.0-9A-Za-z]*$/), @names)
l!l&&r!l&&!r
000
MC/DC0%0/2
not @names or grep((!/^[_A-Za-z][_.0-9A-Za-z]*$/), @names)
not @names grep((!/^[_A-Za-z][_.0-9A-Za-z]*$/), @names)
Truth table0%0/3
not(A) or B
A= not @namesB= grep((!/^[_A-Za-z][_.0-9A-Za-z]*$/), @names)
ABresult
1X1
011
000
459 0 die "Invalid fieldNames attribute declared in <recordset> tag\n";
460 }
461
462 0 $self->{'names'} = \@names;
463 0 $self->{row_count} = $attribs->{rowcount};
464 }
465 ▶ elsif ( $element eq "field" and $self->{seen_recordsets} == 1 ) {
Condition0%0/3
$element eq "field" and $self->{"seen_recordsets"} == 1
!ll&&!rl&&r
000
MC/DC0%0/2
$element eq "field" and $self->{"seen_recordsets"} == 1
$element eq "field" $self->{"seen_recordsets"} == 1
Truth table0%0/3
A and B
A= $element eq "field"B= $self->{"seen_recordsets"} == 1
ABresult
0X0
100
111
466 0 ▶ die "No name supplied for field\n" unless $attribs->{name};
Branch: 1 branch
BranchTrueFalseTotal
unless $attribs->{"name"} 0 0 0
467 0 ▶ die "Cannot nest <field> elements\n" unless $self->{curr_row} < 0;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"curr_row"} < 0 0 0 0
468
469 0 my $expected = $self->{'names'}[ ++$self->{curr_field} ];
470 0 ▶ unless ( $attribs->{name} eq $expected ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($attribs->{"name"} eq $expected) 0 0 0
471 0 die "Expected <field name='$expected'> and found " .
472 "<field name='$attribs->{name}'>\n";
473 }
474
475 0 $self->{curr_row} = -1;
476 }
477 else {
478 0 ▶ unless ( $parse_var ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($parse_var) 0 0 0
479 0 ▶ die "<$element> not allowed in Recordset element\n" unless
Branch: 1 branch
BranchTrueFalseTotal
unless grep(($element eq $_), @Data_Types) 0 0 0
480 grep $element eq $_, @Data_Types;
481 0 ▶ $parse_var = WDDX::Parser->create_var( $element ) or
Branch: 1 branch
BranchTrueFalseTotal
$parse_var = "WDDX::Parser"->create_var($element) or die 'Expecting some data element (e.g., <string>), ' . "found: <$element>\n" 0 0 0
482 die "Expecting some data element (e.g., <string>), " .
483 "found: <$element>\n"; # shouldn't happen but be safe...
484 0 $self->{'types'}[ $self->{curr_field} ] = $element;
485 0 $self->push( $parse_var );
486 }
487 0 $parse_var->start_tag( $element, $attribs );
488 }
489
490 0 return $self;
491 }
492  
493  
494 0 ▶sub end_tag {
Subroutine end_tag: not called CC=8 SCAR=42.8
495 0 my( $self, $element ) = @_;
496 0 my $parse_var = $self->parse_var;
497
498 0 ▶ if ( $element eq "recordset" and not --$self->{seen_recordsets} ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($element eq 'recordset' and not --$self->{'seen_recordsets'}) { } 0 0 0
elsif ($element eq 'field' and $self->{'seen_recordsets'} == 1) { } 0 0 0
Condition0%0/3
$element eq "recordset" and not --$self->{"seen_recordsets"}
!ll&&!rl&&r
000
MC/DC0%0/2
$element eq "recordset" and not --$self->{"seen_recordsets"}
$element eq "recordset" not --$self->{"seen_recordsets"}
Truth table0%0/3
A and not(B)
A= $element eq "recordset"B= not --$self->{"seen_recordsets"}
ABresult
0X0
100
111
499 0 my @data = map { [ map $_->_deserialize, @$_ ] } @{ $self->{value} };
500
501 # This is kinda a kludge to allow us to deserialize empty recordsets
502 # Since an empty recordset will have no data type tags, we set the
503 # data type of each field to undef
504 0 ▶ unless ( @data ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@data) 0 0 0
505 0 $self->{'types'} = [ map undef, ( 1 .. @{ $self->{'names'} } ) ];
506 }
507
508 $self = new WDDX::Recordset(
509 $self->{'names'},
510 0 $self->{'types'},
511 \@data
512 );
513 }
514 ▶ elsif ( $element eq "field" and $self->{seen_recordsets} == 1 ) {
Condition0%0/3
$element eq "field" and $self->{"seen_recordsets"} == 1
!ll&&!rl&&r
000
MC/DC0%0/2
$element eq "field" and $self->{"seen_recordsets"} == 1
$element eq "field" $self->{"seen_recordsets"} == 1
Truth table0%0/3
A and B
A= $element eq "field"B= $self->{"seen_recordsets"} == 1
ABresult
0X0
100
111
515 0 my $name = $self->{'names'}[ $self->{curr_field} ];
516 0 ▶ if ( $self->{curr_row} != $self->{row_count} - 1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"curr_row"} != $self->{"row_count"} - 1) 0 0 0
517 0 die "Number of elements in field '$name' doesn't match declared " .
518 "row count\n";
519 }
520 0 $self->{curr_row} = -1;
521 }
522 else {
523 0 ▶ unless ( $parse_var ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($parse_var) 0 0 0
524 # XML::Parser should actually catch this
525 0 die "Found </$element> before <$element>\n";
526 }
527 0 $self->parse_var( $parse_var->end_tag( $element ) );
528 }
529
530 0 return $self;
531 }
532  
533  
534 0 ▶sub append_data {
Subroutine append_data: not called CC=3 SCAR=24.8
535 0 my( $self, $data ) = @_;
536 0 my $parse_var = $self->parse_var;
537
538 0 ▶ if ( $parse_var ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($parse_var) { } 0 0 0
elsif ($data =~ /\S/) { } 0 0 0
539 0 $parse_var->append_data( $data );
540 }
541 elsif ( $data =~ /\S/ ) {
542 0 die "No loose character data is allowed within <recordset> elements\n";
543 }
544 }
545  
546  
547 0 ▶sub is_parser {
Subroutine is_parser: not called CC=1 SCAR=6.9
548 0 return 1;
549 }
550  
551  
552 0 ▶sub parse_var {
Subroutine parse_var: not called CC=6 SCAR=37.4
553 0 my( $self, $var ) = @_;
554 0 my $curr_field = $self->{curr_field};
555 0 my $curr_row = $self->{curr_row};
556
557 0 ▶ return "" if $curr_field < 0 or $curr_row < 0;
Branch: 1 branch
BranchTrueFalseTotal
if $curr_field < 0 or $curr_row < 0 0 0 0
Condition0%0/3
$curr_field < 0 or $curr_row < 0
l!l&&r!l&&!r
000
MC/DC0%0/2
$curr_field < 0 or $curr_row < 0
$curr_field < 0 $curr_row < 0
Truth table0%0/3
A or B
A= $curr_field < 0B= $curr_row < 0
ABresult
1X1
011
000
558
559 0 ▶ $self->{value}[$curr_row][$curr_field] = $var if defined $var;
Branch: 1 branch
BranchTrueFalseTotal
if defined $var 0 0 0
560 0 my $curr_var = $self->{value}[$curr_row][$curr_field];
561 0 ▶ return ( ref $curr_var && $curr_var->is_parser ) ? $curr_var : "";
Branch: 1 branch
BranchTrueFalseTotal
ref $curr_var && $curr_var->is_parser ? : 0 0 0
Condition0%0/3
ref $curr_var and $curr_var->is_parser
!ll&&!rl&&r
000
MC/DC0%0/2
ref $curr_var and $curr_var->is_parser
ref $curr_var $curr_var->is_parser
Truth table0%0/3
A and B
A= ref $curr_varB= $curr_var->is_parser
ABresult
0X0
100
111
562 }
563  
564  
565 0 ▶sub push {
Subroutine push: not called CC=3 SCAR=24.8
566 0 my( $self, $element ) = @_;
567 0 my $curr_field = $self->{curr_field};
568 0 my $curr_row = ++$self->{curr_row};
569 0 my $name = $self->{'names'}[$curr_field];
570
571 0 ▶ if ( $curr_field < 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($curr_field < 0) 0 0 0
572 0 die "Missing <field> tag in recordset\n";
573 }
574 0 ▶ if ( $self->{curr_row} >= $self->{row_count} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"curr_row"} >= $self->{"row_count"}) 0 0 0
575 0 die "Number of elements in field '$name' exceeds declared row count\n";
576 }
577
578 0 $self->{value}[$curr_row][$curr_field] = $element;
579 }
↑ Summary WDDX.pm »