Quiq/Database/Api/Dbi/Cursor.pm

filter: Statementstmt 81.1% 69 / 85 Branchbran 63.0% 29 / 46 Conditioncond 66.6% 2 / 3 MC/DCmcdc 50.0% 1 / 2 Subroutinesub 100% 13 / 13 Podpod 100% 9 / 9 totaltotal 77.8% 123 / 158 CC 27 SCAR 28.7
CC
27
Cov
75%
bindTypes
40.1
bind
9.0
fetch
6.3
CRAP 17.6
« Quiq/Database/ResultSet/Object.pm ↑ Summary Quiq/Database/Cursor.pm »
1 # -----------------------------------------------------------------------------
2  
3 =encoding utf8
4  
5 =head1 NAME
6  
7 Quiq::Database::Api::Dbi::Cursor - DBI Datenbank-Cursor
8  
9 =head1 BASE CLASS
10  
11 L<Quiq::Hash>
12  
13 =head1 DESCRIPTION
14  
15 Ein Objekt der Klasse repräsentiert einen DBI-Datenbank-Cursor.
16  
17 =head1 ATTRIBUTES
18  
19 =over 4
20  
21 =item sth => $sth
22  
23 DBI Statement-Handle.
24  
25 =item bindVars => $n
26  
27 Anzahl an Bind-Variablen, die im Statement enthalten sind. Ist
28 die Anzahl größer 0, handelt es sich um einen Bind-Cursor.
29  
30 =item titles => \@titles
31  
32 Array der Kolumentitel. Ist das Array nicht leer, handelt es
33 sich um einen Select-Cursor.
34  
35 =item hits => $n
36  
37 Anzahl der getroffenen Datensätze.
38  
39 =item id => $id
40  
41 Id nach INSERT in Tabelle mit AUTOINCREMENT-Kolumne (MySQL, SQLite)
42  
43 =back
44  
45 =cut
46  
47 # -----------------------------------------------------------------------------
48  
49 package Quiq::Database::Api::Dbi::Cursor;
50 7 ▶use base qw/Quiq::Hash/;
Subroutine BEGIN: called CC=1 SCAR=0.0
51  
52 7 ▶use v5.10;
Subroutine BEGIN: called CC=1 SCAR=0.0
53 7 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
54 7 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
55  
56 7 our $VERSION = '1.240';
57  
58 # -----------------------------------------------------------------------------
59  
60 =head1 METHODS
61  
62 =head2 Konstruktor/Destruktor
63  
64 =head3 new() - Instantiiere Cursor
65  
66 =head4 Synopsis
67  
68 $cur = $class->new(@keyVal);
69  
70 =head4 Description
71  
72 Instantiiere ein Cursor-Objekt mit den Attributen @keyVal
73 und liefere dieses zurück.
74  
75 =cut
76  
77 # -----------------------------------------------------------------------------
78  
79 206 ▶sub new {
Subroutine new: called CC=1 SCAR=0.0
Pod documented
80 206 my $class = shift;
81  
82 206 my $self = $class->SUPER::new(
83 sth => undef,
84 bindVars => 0,
85 bindTypes => [],
86 db => undef,
87 titles => [],
88 hits => 0,
89 id => 0,
90 );
91 206 $self->set(@_);
92  
93 206 return $self;
94 }
95  
96 # -----------------------------------------------------------------------------
97  
98 =head3 destroy() - Schließe Cursor
99  
100 =head4 Synopsis
101  
102 $cur->destroy;
103  
104 =head4 Description
105  
106 Schließe Cursor. Die Objektreferenz ist anschließend ungültig.
107 Die Methode liefert keinen Wert zurück.
108  
109 =cut
110  
111 # -----------------------------------------------------------------------------
112  
113 178 ▶sub destroy {
Subroutine destroy: called CC=2 SCAR=6.9
Pod documented
114 178 my ($self) = @_;
115  
116 178 ▶ if (my $sth = $self->{'sth'}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $sth = $self->{"sth"}) 116 62 178
117 116 $sth->finish;
118 }
119 178 $_[0] = undef;
120  
121 178 return;
122 }
123  
124 # -----------------------------------------------------------------------------
125  
126 =head2 Akzessoren
127  
128 =head3 bindVars() - Liefere die Anzahl der Bind-Variablen
129  
130 =head4 Synopsis
131  
132 $n = $cur->bindVars;
133  
134 =head4 Description
135  
136 Liefere die Anzahl der Bind-Variablen, die im SQL-Statement enthalten
137 waren.
138  
139 =cut
140  
141 # -----------------------------------------------------------------------------
142  
143 205 ▶sub bindVars {
Subroutine bindVars: called CC=1 SCAR=0.0
Pod documented
144 205 return shift->{'bindVars'};
145 }
146  
147 # -----------------------------------------------------------------------------
148  
149 =head3 bindTypes() - Setze/Liefere Datentypen der Bind-Variablen
150  
151 =head4 Synopsis
152  
153 @arr|$arr = $cur->bindTypes(@dataTypes);
154 @arr|$arr = $cur->bindTypes;
155  
156 =cut
157  
158 # -----------------------------------------------------------------------------
159  
160 3 ▶sub bindTypes {
Subroutine bindTypes: called CC=14 SCAR=36.9
Pod documented
161 3 my $self = shift;
162 # @_: @types
163  
164 3 my $arr = $self->{'bindTypes'};
165 3 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 3 0 3
166 3 @$arr = ();
167 3 my $dbms = $self->{'db'}->{'dbms'};
168 3 for my $type (@_) {
169 6 ▶ if (!defined $type) {
Branch: 5 branches
BranchTrueFalseTotal
if (not defined $type) { } 3 3 6
elsif ($type eq 'BLOB') { } 2 1 3
elsif ($type eq 'TEXT') { } 1 0 1
elsif ($type eq 'NUMBER') { } 0 0 0
elsif ($type eq 'XML') { } 0 0 0
170 3 push @$arr,undef;
171 }
172 elsif ($type eq 'BLOB') {
173 2 ▶ if ($dbms eq 'oracle') {
Branch: 2 branches
BranchTrueFalseTotal
if ($dbms eq 'oracle') { } 0 2 2
elsif ($dbms eq 'postgresql') { } 0 2 2
174 0 push @$arr,{ora_type=>DBD::Oracle::SQLT_BIN()};
175 }
176 elsif ($dbms eq 'postgresql') {
177 0 push @$arr,{pg_type=>DBD::Pg::PG_BYTEA()};
178 }
179 else {
180 2 push @$arr,undef;
181 }
182 }
183 elsif ($type eq 'TEXT') {
184 1 ▶ if ($dbms eq 'oracle') {
Branch: 1 branch
BranchTrueFalseTotal
if ($dbms eq 'oracle') { } 0 1 1
185 0 push @$arr,{ora_type=>DBD::Oracle::SQLT_CHR()};
186 }
187 else {
188 1 push @$arr,undef;
189 }
190 }
191 elsif ($type eq 'NUMBER') {
192 0 ▶ if ($dbms eq 'oracle') {
Branch: 1 branch
BranchTrueFalseTotal
if ($dbms eq 'oracle') { } 0 0 0
193 0 push @$arr,{ora_type=>DBD::Oracle::ORA_NUMBER()};
194 }
195 else {
196 0 push @$arr,undef;
197 }
198 }
199 elsif ($type eq 'XML') {
200 0 ▶ if ($dbms eq 'oracle') {
Branch: 1 branch
BranchTrueFalseTotal
if ($dbms eq 'oracle') { } 0 0 0
201 0 push @$arr,{ora_type=>DBD::Oracle::ORA_XMLTYPE()};
202 }
203 else {
204 0 push @$arr,undef;
205 }
206 }
207 else {
208 0 push @$arr,$type;
209 }
210 }
211 }
212  
213 3 ▶ return wantarray? @$arr: $arr;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 3 3
214 }
215  
216 # -----------------------------------------------------------------------------
217  
218 =head3 hits() - Liefere die Anzahl der getroffenen Datensätze
219  
220 =head4 Synopsis
221  
222 $n = $cur->hits;
223  
224 =head4 Description
225  
226 Liefere die Anzahl der Datesätze, die bei der Ausführung des
227 Statement getroffen wurden. Im Falle einer Selektion ist dies die
228 Anzahl der (bislang) gelesenen Datensätze.
229  
230 =cut
231  
232 # -----------------------------------------------------------------------------
233  
234 217 ▶sub hits {
Subroutine hits: called CC=1 SCAR=0.0
Pod documented
235 217 return shift->{'hits'};
236 }
237  
238 # -----------------------------------------------------------------------------
239  
240 =head3 id() - Liefere die Id des eingefügten Datensatzes
241  
242 =head4 Synopsis
243  
244 $id = $cur->id;
245  
246 =cut
247  
248 # -----------------------------------------------------------------------------
249  
250 217 ▶sub id {
Subroutine id: called CC=1 SCAR=0.0
Pod documented
251 217 return shift->{'id'};
252 }
253  
254 # -----------------------------------------------------------------------------
255  
256 =head3 titles() - Liefere eine Referenz auf Liste der Kolumnentitel
257  
258 =head4 Synopsis
259  
260 $titlesA = $cur->titles;
261  
262 =cut
263  
264 # -----------------------------------------------------------------------------
265  
266 217 ▶sub titles {
Subroutine titles: called CC=1 SCAR=0.0
Pod documented
267 217 return shift->{'titles'};
268 }
269  
270 # -----------------------------------------------------------------------------
271  
272 =head2 Objektmethoden
273  
274 =head3 bind() - Führe Bind-Statement aus
275  
276 =head4 Synopsis
277  
278 $cur = $cur->bind(@vals);
279  
280 =head4 Description
281  
282 Führe Bind-Statement aus und liefere einen (neuen) Cursor über
283 das Resultat der Statement-Ausführung zurück.
284  
285 =cut
286  
287 # -----------------------------------------------------------------------------
288  
289 12 ▶sub bind {
Subroutine bind: called CC=9 SCAR=22.0
Pod documented
290 12 my $self = shift;
291  
292 12 my ($sth,$bindVars,$bindTypes) = $self->get(qw/sth bindVars bindTypes/);
293  
294 12 my $hits = 0;
295 12 ▶ while (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 23 12 35
296 23 my @vals;
297 23 ▶ if (ref $_[0]) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $_[0]) { } 6 17 23
298 # Wert ist (Array-)Referenz, wir expandieren die Referenz
299 6 @vals = @{shift()}
300 }
301 else {
302 # Abfolge von Werten
303 17 @vals = splice @_,0,$bindVars;
304 }
305  
306 # '' auf undef umsetzen
307  
308 23 for (@vals) {
309 49 ▶ if (defined $_ && $_ eq '') {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $_ and $_ eq "") 1 48 49
Condition66%2/3
defined $_ and $_ eq ""
!ll&&!rl&&r
0481
MC/DC50%1/2
defined $_ and $_ eq ""
defined $_ $_ eq ""
Truth table66%2/3
A and B
A= defined $_B= $_ eq ""
ABresult
0X0
100
111
310 1 $_ = undef;
311 }
312 }
313  
314 # Werte binden und Statement ausführen. BindType kann
315 # undef sein. Im Falle von Oracle und BLOB oder CLOB sollte
316 # ein Type spezifiziert sein (siehe Methode bindTypes)
317  
318 23 ▶ for (my $i = 0; $i < @vals; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @vals) 49 23 72
319 49 $sth->bind_param($i+1,$vals[$i],$bindTypes->[$i]);
320 }
321 23 my $r = $sth->execute;
322 23 ▶ $hits += $r if $r > 0;
Branch: 1 branch
BranchTrueFalseTotal
if $r > 0 21 2 23
323 }
324 12 $self->{'hits'} = $hits;
325  
326 # Die Kolumnentitel sind bei SELECT-Statements mit Bind-Variablen
327 # erst nach dem Bind verfügbar.
328  
329 12 ▶ if ($sth->{'NUM_OF_FIELDS'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($sth->{"NUM_OF_FIELDS"}) 2 10 12
330 2 $self->{'titles'} = $sth->{'NAME_lc'};
331 }
332  
333 12 return $self;
334 }
335  
336 # -----------------------------------------------------------------------------
337  
338 =head3 fetch() - Liefere den nächsten Datensatz
339  
340 =head4 Synopsis
341  
342 $row = $cur->fetch;
343  
344 =head4 Description
345  
346 Liefere eine Referenz auf den nächsten Datensatz der Ergebnismenge.
347 Ist das Ende der Ergebnismenge erreicht, liefere undef.
348  
349 Der Datensatz ist ein Array mit den Kolumnenwerten.
350  
351 Bei DBI liefert jeder Aufruf dieselbe Referenz, so dass das Array vom
352 Aufrufer normalerweise kopiert werden muss.
353  
354 Nullwerte werden durch einen Leerstring repräsentiert.
355 Da DBI einen Nullwert durch undef repräsentiert, nimmt die
356 Methode eine Abbildung von undef auf '' vor.
357  
358 =cut
359  
360 # -----------------------------------------------------------------------------
361  
362 102 ▶sub fetch {
Subroutine fetch: called CC=5 SCAR=18.4
Pod documented
363 102 my $self = shift;
364 102 my $curName = shift;
365 102 my $chunkSize = shift;
366 102 my $chunkPosS = shift;
367  
368 102 ▶ if ($curName) { # PostgreSQL
Branch: 1 branch
BranchTrueFalseTotal
if ($curName) 0 102 102
369 0 ▶ if ($$chunkPosS == $chunkSize) {
Branch: 1 branch
BranchTrueFalseTotal
if ($$chunkPosS == $chunkSize) { } 0 0 0
370 0 my $stmt = "FETCH $chunkSize FROM $curName";
371 0 my $cur = $self->{'db'}->sql($stmt,-log=>0);
372 0 $self->{'sth'} = $cur->{'sth'};
373 # $self->{'sth'} = $self->{'db'}->{'dbh'}->prepare($stmt);
374 # $self->{'sth'}->execute;
375 0 $$chunkPosS = 1; # nicht 0, da wir den ersten Satz unten fetchen!
376 }
377 else {
378 0 $$chunkPosS++;
379 }
380 }
381  
382 102 my $row = $self->{'sth'}->fetchrow_arrayref;
383 102 ▶ if ($row) {
Branch: 1 branch
BranchTrueFalseTotal
if ($row) 65 37 102
384 65 ▶ grep { $_ = '' if !defined $_ } @$row; # undef -> ''
Branch: 1 branch
BranchTrueFalseTotal
if not defined $_ 3 144 147
385 }
386  
387 102 return $row;
388 }
389  
390 # -----------------------------------------------------------------------------
391  
392 =head1 VERSION
393  
394 1.240
395  
396 =head1 AUTHOR
397  
398 Frank Seitz, L<http://fseitz.de/>
399  
400 =head1 COPYRIGHT
401  
402 Copyright (C) 2026 Frank Seitz
403  
404 =head1 LICENSE
405  
406 This code is free software; you can redistribute it and/or modify
407 it under the same terms as Perl itself.
408  
409 =cut
410  
411 # -----------------------------------------------------------------------------
412  
413 7 1;
414  
415 # eof
« Quiq/Database/ResultSet/Object.pm ↑ Summary Quiq/Database/Cursor.pm »