Tamino/Tran/Cursor.pm untested

filter: Statementstmt 0.0% 0 / 56 Branchbran 0.0% 0 / 22 Conditioncond 0.0% 0 / 22 MC/DCmcdc 0.0% 0 / 13 Subroutinesub 0.0% 0 / 7 Podpod 0.0% 0 / 5 totaltotal 0.0% 0 / 125 CC 12 SCAR 33.3
CC
12
Cov
0%
new
56.0
fetch
20.0
close
6.0
CRAP 27.9
1 use strict;
2  
3 package Tamino::Tran::Cursor;
4 use base qw/Class::Accessor Class::Data::Inheritable/;
5  
6 =head1 NAME
7  
8 Tamino::Tran::Cursor - The L<Tamino> driver's class implementing cursors.
9  
10 =head1 SYNOPSIS
11  
12 use Tamino;
13
14 my $tamino_client = Tamino->new(
15 server => '127.0.0.1/tamino'
16 db => 'mydb'
17 );
18
19 # $t will be a Tamino::Tran object
20 my $t = $tamino_client->begin_tran
21 or die $tamino_client->error;
22
23 $c = $t->xquery_cursor(q{
24 for $x in collection('mycollection')/doctype/xxx[@yyy=%s][zzz='%s']
25 return $x
26 }, "('y1','y2')", "z1") or die $t->error;
27  
28 while($xml_bare_simple_tree = $c->fetch) {
29 print XML::Simple::XMLout($xml_bare_simple_tree, KeyAttr => []);
30 }
31
32 $c->close;
33  
34 =head1 DESCRIPTION
35  
36 This is just an API wrapper.
37 This driver is based on L<LWP::UserAgent>, L<XML::Twig>, and inherits from L<Class::Accessor> and L<Class::Data::Inheritable>.
38  
39 =cut
40  
41 __PACKAGE__->mk_classdata($_) for qw/fetch_size/;
42 __PACKAGE__->mk_ro_accessors(qw/tran handle scrollable vague result/);
43 __PACKAGE__->mk_accessors(qw/fetch_size _prefetched _pos error/);
44  
45 __PACKAGE__->fetch_size(8);
46  
47 =head1 CONSTRUCTOR
48  
49 Constructor is called internally by L<Tamino::Tran> class object.
50  
51 =cut
52  
53 sub new ($@) {
54 my $class = shift;
55 my %args = @_;
56
57 my $data = delete $args{data} or return;
58
59 $class = ref $class || $class;
60 my $self = $class->SUPER::new({
61 tran => $args{tran},
62 scrollable => !!$args{scrollable},
63 vague => !!$args{vague},
64 fetch_size => $args{fetch_size} || __PACKAGE__->fetch_size,
65 result => $args{result},
66 });
67
68 $data->{_cursor} = 'open';
69 $data->{_scroll} = $args{scrollable} ? 'yes' : 'no';
70 $data->{_sensitive} = $args{vague} ? 'vague' : 'no';
71
72 $self->{_pos} = $args{pos} || 1;
73 unless($args{no_fetch}) {
74 $data->{_position} = $self->{_pos};
75 $data->{_quantity} = $self->{fetch_size};
76 }
77
78 my $q = $self->tran->_cmd($data, result => $self->{result}, _twig_handlers => {
79 'ino:cursor' => sub {
80 my ($twig,$cur) = @_;
81 $self->{handle} = $cur->{'ino:handle'};
82 $self->_get_pos($cur);
83 1;
84 }
85 });
86 return unless defined $q;
87
88 $self->{_prefetched} = $q unless($args{no_fetch});
89 return unless($q && $self->{handle});
90 return $self;
91 }
92  
93 =head1 METHODS
94  
95 =head2 close
96  
97 $c->close;
98  
99 Close cursor
100  
101 =cut
102  
103 sub close ($) {
104 my ($self) = @_;
105 if($self->{handle} && defined $self->tran->_cmd({_cursor => 'close', _handle => $self->{handle}})) {
106 $self->{handle} = undef;
107 return 1;
108 }
109 return 0;
110 }
111  
112 =head2 fetch
113  
114 $xml = $c->fetch();
115 $xml = $c->fetch($num);
116 $xml = $c->fetch($num, $pos);
117  
118 Fetch $num number of records from a cursor, startings from $pos position.
119 If $pos is omitted, fetch from current position.
120 If $num is omiited, fetch default number of records, which is set with C<< $c->fetch_size($num) >>
121  
122 =cut
123  
124 sub fetch ($;$$) {
125 my ($self, $n, $pos) = @_;
126 if(my $r = $self->{_prefetched}) {
127 $self->{_prefetched} = undef;
128 return $r;
129 }
130 return unless defined $self->{_pos};
131 return $self->tran->_cmd({
132 _cursor => 'fetch',
133 _handle => $self->{handle},
134 _position => $pos || $self->{_pos},
135 _quantity => $n || $self->{fetch_size},
136 },
137 result => $self->result,
138 _twig_handlers => {
139 'ino:cursor' => sub {
140 my ($twig,$cur) = @_;
141 return 1 if($cur->{'ino:handle'} != $self->{handle}); # FIXME
142 $self->_get_pos($cur);
143 1;
144 }
145 }
146 );
147 }
148  
149 sub _get_pos ($$) {
150 my ($self, $cur) = @_;
151 if(exists $cur->{'ino:next'}) {
152 $self->{_pos} = $cur->{'ino:next'}->{'ino:position'};
153 } else {
154 $self->tran->error("No more data");
155 $self->{_pos} = undef;
156 }
157 }
158  
159 sub DESTROY {
160 $_[0]->close;
161 }
162  
163 =head2 fetch_size
164  
165 Tamino::Tran::Cursor->fetch_size($num);
166 $c->fetch_size($num);
167  
168 =cut
169  
170 1;
171