Cursor.pm untested

filter: Statementstmt 0.0% 0 / 81 Branchbran 0.0% 0 / 16 Conditioncond 0.0% 0 / 6 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 0.0% 0 / 11 Podpod 0.0% 0 / 6 totaltotal 0.0% 0 / 124 CC 9 SCAR 22.4
CC
9
Cov
0%
_end
20.0
_derive_header
12.0
_convert_rows
12.0
CRAP 9.4
1 package Sash::Cursor;
2 use strict;
3 use warnings;
4  
5 use base qw( Class::Accessor );
6 use Sash::Table;
7 use Carp;
8  
9 Sash::Cursor->mk_accessors( qw(
10 is_valid
11 closed
12 client
13 size
14 data
15 header
16 caller
17 missing_id_field
18 query
19 batch_size
20 result
21 attributes
22 message
23 ) );
24  
25 sub open {
26 my $class = shift;
27 my $args = shift;
28  
29 my $self = bless {
30 is_valid => 1,
31 closed => 0,
32 client => $args->{client},
33 query => $args->{query},
34 caller => $args->{caller},
35 batch_size => $args->{batch_size},
36 }, ( ref $class || $class );
37  
38 return $self;
39 }
40  
41 sub fetch {
42 my $self = shift;
43  
44 croak 'Invalid Cursor - cursor has previously been closed and is no longer valid' if $self->closed;
45
46 # Make sure we don't have an of these based on the last call to fetch.
47 $self->result( undef );
48 $self->attributes( undef );
49 $self->data( undef );
50 $self->header( undef );
51 $self->message( undef );
52  
53 # for the love of convention and perl quirks
54 my $caller = $self->caller;
55 $self->$caller;
56
57 $self->_end;
58 }
59  
60 sub close {
61 my $self = shift;
62 $self->closed( 1 );
63 }
64  
65 sub _end {
66 my $self = shift;
67
68 # Define the magic variable for the user to use.
69 $Sash::Command::result = $self->result if defined $self->result;
70
71 return $self->result if Sash::Properties->output eq Sash::Properties->perlval;
72
73 if ( Sash::Properties->output eq Sash::Properties->vertical ) {
74 my $vertical_data;
75 my $header = $self->header;
76
77 foreach ( @{$self->data} ) {
78 my $i = 0;
79 push @$vertical_data, [ $header->[$i++], $_ ] foreach ( @$_ );
80 }
81
82 $self->data( $vertical_data );
83 $self->attributes( $header );
84 $self->header( [ 'Attribute', 'Value' ] );
85 }
86  
87 my $table = Sash::Table->new( $self->data, $self->header, $self->attributes );
88  
89 return $table;
90 }
91  
92 sub _derive_header {
93 my $self = shift;
94 my $query = shift;
95  
96 # define defaults for the id field
97 my $missing_id_field = 0;
98 my $re = qr/[^type]/i;
99  
100 # Account for the fact the user included an id field in the query
101 unless ( $query =~ /\bid\b/i ) {
102 $missing_id_field = 1;
103 $re = qr/[^type|id]/i;
104 }
105  
106  
107 # Create the header from the attributes of the query.
108 $query =~ s/(\r|\n)+/ /g;
109 my $header = [ split /\s*,\s*/, lc( $1 ) ] if $query =~ s/select\s*(.*?)\s*from.*/$1/ig;
110  
111 $self->header( $header );
112 $self->missing_id_field( $missing_id_field );
113  
114 return ( $header, $missing_id_field );
115 }
116  
117 sub _convert_rows {
118 my $self = shift;
119 my $rows = shift; #array ref
120  
121 croak 'self->header and self->missing_id_field need to be defined' unless $self->header;
122  
123 my $data = [ map {
124 my $object = $_;
125  
126 # Make our lookup of the hash elements case insensitive in the next command.
127 my $lookup = { map { lc( $_ ) => \$object->{$_} } keys %$object };
128  
129 # Using @$header here ensures the order of the attributes the user typed in.
130 [ map { unless ( $self->missing_id_field && ( 'id' eq $_ ) ) { ${$lookup->{$_}} } else { } } @{$self->header} ];
131 } @$rows ];
132  
133 $self->data( $data );
134  
135 return $self->_end;
136 }
137  
138 1;