QueryCursor.pm

filter: Statementstmt 44.9% 22 / 49 Branchbran 0.0% 0 / 12 Conditioncond 0.0% 0 / 2 MC/DCmcdc 0.0% 0 / 1 Subroutinesub 50.0% 6 / 12 Podpod 100% 5 / 5 totaltotal 40.7% 33 / 81 CC 8 SCAR 20.7
CC
8
Cov
35%
fetch
20.0
execute
12.0
__ANON__
6.0
CRAP 7.9
« SQLHandler.pm ↑ Summary Connection/PostgreSQL/PLSQL.pm »
1 package DBIx::QueryCursor;
2  
3 6 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 6 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 6 ▶use Abstract::Meta::Class ':all';
Subroutine BEGIN: called CC=1 SCAR=0.0
7 6 ▶use Carp 'confess';
Subroutine BEGIN: called CC=1 SCAR=0.0
8 6 ▶use base 'DBIx::SQLHandler';
Subroutine BEGIN: called CC=1 SCAR=0.0
9 6 ▶use vars qw($VERSION);
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 6 $VERSION = 0.03;
12  
13 =head1 NAME
14  
15 DBIx::QueryCursor - Database cursor handler
16  
17 =head1 SYNOPSIS
18  
19 use DBIx::QueryCursor;
20 my $cursor = new DBIx::QueryCursor(
21 connection => $connection,
22 sql => "SELECT * FROM emp WHERE ename = ?"
23 );
24 my $result_set = $cursor->execute(['Smith']);
25  
26 if($cursor->fetch()) {
27 $ename = result_set->{ENAME};
28 ... do some stuff
29 }
30  
31  
32 or
33  
34 use DBIx::Connection;
35  
36 my $cursor = $connection->query_cursor(
37 sql => "SELECT * FROM emp WHERE ename = ?"
38 );
39  
40  
41 =head1 DESCRIPTION
42  
43 Class that represents database cursor.
44  
45 =head2 attributes
46  
47 =over
48  
49 =item result_set
50  
51 Fetch resultset.
52  
53 =cut
54  
55 6 has '$.result_set';
56  
57  
58 =item rows
59  
60 Number of rows retrieved since last execution.
61  
62 =cut
63  
64 6 has '$.rows';
65  
66 =back
67  
68 =head2 methods
69  
70 =over
71  
72 =item columns
73  
74 Function return list of column from current cursor
75  
76 =cut
77  
78 0 ▶sub columns {
Subroutine columns: not called CC=1 SCAR=6.9
Pod documented
79 0 my ($self) = @_;
80 0 \@{$self->sth->{NAME_lc}}
81 }
82  
83  
84 =item execute
85  
86 Executes statements, takes bind parameters as ARRAY ref, optionaly resul set as reference(HASH, SCALAR, ARRAY)
87 Returns result set.
88  
89 =cut
90  
91 0 ▶sub execute {
Subroutine execute: not called CC=3 SCAR=24.8
Pod documented
92 0 my ($self, $bind_params, $result_set) = @_;
93 0 ▶ $result_set ||= {};
Condition0%0/2
$result_set ||= {}
l!l
00
MC/DC0%0/1
$result_set ||= {}
$result_set
Truth table0%0/2
A
A= $result_set
Aresult
11
00
94 0 $self->set_result_set($result_set);
95 0 ▶ $self->finish if $self->rows;
Branch: 1 branch
BranchTrueFalseTotal
if $self->rows 0 0 0
96 0 $self->SUPER::execute(@$bind_params);
97 0 $self->bind_columns($result_set);
98 0 $self->set_rows(0);
99 0 $result_set;
100 }
101  
102  
103 =item iterator
104  
105 Returns the cursor itarator, on each iteration database error is checked.
106 For instance sub query returned more then error exception is capture here.
107  
108 =cut
109  
110 0 ▶sub iterator {
Subroutine iterator: not called CC=1 SCAR=6.9
Pod documented
111 0 my ($self) = @_;
112 0 my $sth = $self->sth;
113 0 my $dbh = $self->connection->dbh;
114 0 ▶ sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
115 0 my $result = $sth->fetch();
116 0 $self->set_rows($self->rows + 1);
117 0 ▶ confess $self->error_handler
Branch: 1 branch
BranchTrueFalseTotal
if $dbh->errstr 0 0 0
118 if $dbh->errstr;
119 0 $result;
120 0 };
121 }
122  
123  
124 =item fetch
125  
126 Move cursor to next result.
127 Returns true if a row was fetched or false if no more results exist.
128  
129 =cut
130  
131 0 ▶sub fetch {
Subroutine fetch: not called CC=4 SCAR=30.0
Pod documented
132 0 my ($self) = @_;
133 0 my $dbh = $self->connection->dbh;
134 0 my $has_result = $self->sth->fetch;
135 0 ▶ $self->error_handler
Branch: 1 branch
BranchTrueFalseTotal
if $dbh->errstr 0 0 0
136 if $dbh->errstr;
137 0 $self->set_rows($self->rows + 1);
138 0 ▶ $has_result ? (wantarray ? @$has_result : $has_result) : ();
Branch: 2 branches
BranchTrueFalseTotal
$has_result ? : 0 0 0
wantarray ? : 0 0 0
139 }
140  
141  
142 =item finish
143  
144 Signals that the cursor will not be used again.
145  
146 =cut
147  
148 0 ▶sub finish {
Subroutine finish: not called CC=2 SCAR=17.9
Pod documented
149 0 my ($self) = @_;
150 0 ▶ $self->sth->finish if $self->sth;
Branch: 1 branch
BranchTrueFalseTotal
if $self->sth 0 0 0
151 }
152  
153 6 1;
154  
155 __END__
« SQLHandler.pm ↑ Summary Connection/PostgreSQL/PLSQL.pm »