Quiq/Database/ResultSet.pm

filter: Statementstmt 35.6% 103 / 289 Branchbran 14.4% 15 / 104 Conditioncond 13.3% 4 / 30 MC/DCmcdc 10.5% 2 / 19 Subroutinesub 63.1% 24 / 38 Podpod 100% 25 / 25 totaltotal 34.2% 173 / 505 CC 76 SCAR 50.8
CC
76
Cov
29%
asTable
506.0
diffReport
272.0
values
156.0
CRAP 160.1
« Quiq/Database/DataAnalysis.pm ↑ Summary Quiq/Gd/Component/Graph.pm »
1 # -----------------------------------------------------------------------------
2  
3 =encoding utf8
4  
5 =head1 NAME
6  
7 Quiq::Database::ResultSet - Liste von Datensätzen (abstrakt)
8  
9 =head1 BASE CLASS
10  
11 L<Quiq::Hash>
12  
13 =head1 DESCRIPTION
14  
15 Ein Objekt der Klasse repräsentiert eine Liste von gleichartigen
16 Datensätzen.
17  
18 =cut
19  
20 # -----------------------------------------------------------------------------
21  
22 package Quiq::Database::ResultSet;
23 15 ▶use base qw/Quiq::Hash/;
Subroutine BEGIN: called CC=1 SCAR=0.0
24  
25 15 ▶use v5.10;
Subroutine BEGIN: called CC=1 SCAR=0.0
26 15 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
27 15 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
28  
29 15 our $VERSION = '1.240';
30  
31 15 ▶use Quiq::Object;
Subroutine BEGIN: called CC=1 SCAR=0.0
32 15 ▶use Time::HiRes ();
Subroutine BEGIN: called CC=1 SCAR=0.0
33 15 ▶use Quiq::Option;
Subroutine BEGIN: called CC=1 SCAR=0.0
34 15 ▶use Quiq::Hash;
Subroutine BEGIN: called CC=1 SCAR=0.0
35 15 ▶use Quiq::Array;
Subroutine BEGIN: called CC=1 SCAR=0.0
36 15 ▶use Quiq::FileHandle;
Subroutine BEGIN: called CC=1 SCAR=0.0
37 15 ▶use Quiq::Properties;
Subroutine BEGIN: called CC=1 SCAR=0.0
38 15 ▶use Quiq::AnsiColor;
Subroutine BEGIN: called CC=1 SCAR=0.0
39 15 ▶use Quiq::Duration;
Subroutine BEGIN: called CC=1 SCAR=0.0
40  
41 # -----------------------------------------------------------------------------
42  
43 =head1 METHODS
44  
45 =head2 Konstruktor
46  
47 =head3 new() - Instantiiere Tabellen-Objekt
48  
49 =head4 Synopsis
50  
51 $tab = $class->new($rowClass,\@titles);
52 $tab = $class->new($rowClass,\@titles,\@rows,@keyVal);
53
54 $tab = $class->new(\@titles);
55 $tab = $class->new(\@titles,\@rows,@keyVal);
56
57 $newTab = $tab->new;
58 $newTab = $tab->new(\@rows);
59  
60 =head4 Description
61  
62 Instantiiere ein Tabellen-Objekt und liefere eine Referenz auf dieses
63 Objekt zurück.
64  
65 Die Arrays @titles und @rows werden von der Methode I<nicht> kopiert.
66  
67 Ist $rowClass nicht angegeben, wird $class->defaultRowClass() als
68 Row-Klasse angenommen.
69  
70 Als Objektmethode gerufen, wird ein neues Tabellen-Objekt mit
71 $rowClass und $titles aus dem existierenden Tabellenobjekt
72 initialisiert. Diese Methode ist nützlich, wenn ein
73 Tabellen-Objekt mit einer Teilmenge des ursprünglichen
74 Tabellen-Objektes gebildet werden soll.
75  
76 =cut
77  
78 # -----------------------------------------------------------------------------
79  
80 10 ▶sub new {
Subroutine new: called CC=4 SCAR=14.4
Pod documented
81 10 my ($class,$self) = Quiq::Object->this(shift);
82  
83 10 my ($rowClass,$titleA);
84 10 ▶ if ($self) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self) { } 0 10 10
85 0 $rowClass = $self->rowClass;
86 0 $titleA = $self->titles;
87 }
88 else {
89 10 ▶ $rowClass = ref $_[0]? $class->defaultRowClass: shift;
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] ? : 0 10 10
90 10 $titleA = shift;
91 }
92 10 ▶ my $rowA = shift || [];
Condition100%2/2
shift() || []
l!l
64
MC/DC100%1/1
shift() || []
shift()
Truth table100%2/2
A
A= shift()
Aresult
11
00
93  
94 10 $self = $class->SUPER::new(
95 rowClass => $rowClass,
96 titles => $titleA,
97 rows => $rowA,
98 moreRowsExist => 0, # wird von fetchAll() gesetzt
99 stmt => '',
100 hits => 0,
101 startTime => scalar(Time::HiRes::gettimeofday),
102 execTime => 0,
103 fetchTime => 0,
104 formatA => undef, # wird von $self->formats() gesetzt
105 );
106 10 $self->set(@_);
107  
108 10 return $self;
109 }
110  
111 # -----------------------------------------------------------------------------
112  
113 =head2 Akzessoren
114  
115 =head3 rowClass() - Liefere Namen der Datensatz-Klasse
116  
117 =head4 Synopsis
118  
119 $rowClass = $tab->rowClass;
120  
121 =cut
122  
123 # -----------------------------------------------------------------------------
124  
125 3 ▶sub rowClass {
Subroutine rowClass: called CC=1 SCAR=0.0
Pod documented
126 3 return shift->{'rowClass'};
127 }
128  
129 # -----------------------------------------------------------------------------
130  
131 =head3 rows() - Liefere/Setze die Liste der Datensätze
132  
133 =head4 Synopsis
134  
135 $rowA|@rows = $tab->rows;
136 $rowA|@rows = $tab->rows(\@rows);
137  
138 =head4 Description
139  
140 Liefere die Liste der Datensätze der Tabelle. Im Skalarkontext liefere
141 eine Referenz auf die Liste.
142  
143 Ist Parameter \@rows angegeben, wird die Datensatz-Liste auf diese
144 Liste gesetzt.
145  
146 =cut
147  
148 # -----------------------------------------------------------------------------
149  
150 30 ▶sub rows {
Subroutine rows: called CC=3 SCAR=11.3
Pod documented
151 30 my $self = shift;
152 # @_: \@rows
153  
154 30 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 0 30 30
155 0 $self->{'rows'} = shift;
156 }
157  
158 30 my $rows = $self->{'rows'};
159 30 ▶ return wantarray? @$rows: $rows;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 20 10 30
160 }
161  
162 # -----------------------------------------------------------------------------
163  
164 =head3 stmt() - Liefere Statement
165  
166 =head4 Synopsis
167  
168 $stmt = $tab->stmt;
169  
170 =head4 Description
171  
172 Liefere das SQL-Statement, mit welchem die Datensätze der Tabelle
173 selektiert wurden.
174  
175 =cut
176  
177 # -----------------------------------------------------------------------------
178  
179 0 ▶sub stmt {
Subroutine stmt: not called CC=1 SCAR=6.9
Pod documented
180 0 return shift->{'stmt'};
181 }
182  
183 # -----------------------------------------------------------------------------
184  
185 =head3 stmtBody() - Liefere Rumpf für ein Subselect
186  
187 =head4 Synopsis
188  
189 $stmt = $tab->stmtBody(@opt);
190  
191 =head4 Description
192  
193 Liefere den Rumpf des (Select-)Statement. Der Rumpf ist das ürsprüngliche
194 Statement ohne Select- und Order-By-Klausel.
195  
196 =head4 Example
197  
198 Ursprüngliches Select:
199  
200 SELECT
201 per_vorname
202 , per_nachname
203 FROM
204 person
205 WHERE
206 per_nachname = 'Schulz'
207 ORDER BY
208 per_vorname
209  
210 Resultierendes Select:
211  
212 FROM
213 person
214 WHERE
215 per_nachname = 'Schulz'
216  
217 =cut
218  
219 # -----------------------------------------------------------------------------
220  
221 0 ▶sub stmtBody {
Subroutine stmtBody: not called CC=1 SCAR=6.9
Pod documented
222 0 my $self = shift;
223 # @_: @opt
224  
225 0 my $stmt = $self->stmt;
226 0 $stmt =~ s|^.*?(?=FROM)||si; # entferne Select-Klausel
227 0 $stmt =~ s|\s+ORDER\s+BY.*$||si; # entferne Order-By-Klausel
228  
229 0 return $stmt;
230 }
231  
232 # -----------------------------------------------------------------------------
233  
234 =head3 titles() - Liefere Liste der Kolumnentitel
235  
236 =head4 Synopsis
237  
238 $titleA|@titles = $tab->titles;
239  
240 =head4 Description
241  
242 Liefere die Liste der Kolumnentitel der Tabelle. Im Skalarkontext liefere
243 eine Referenz auf die Liste.
244  
245 =cut
246  
247 # -----------------------------------------------------------------------------
248  
249 7 ▶sub titles {
Subroutine titles: called CC=2 SCAR=6.9
Pod documented
250 7 my $self = shift;
251 7 my $arr = $self->{'titles'};
252 7 ▶ return wantarray? @$arr: $arr;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 4 3 7
253 }
254  
255 # -----------------------------------------------------------------------------
256  
257 =head3 values() - Liefere Kolumnenwerte als Liste oder Hash
258  
259 =head4 Synopsis
260  
261 @vals|$valA = $tab->values($key,@opt);
262 %vals|$valH = $tab->values($key,@opt,-hash=>1);
263  
264 =head4 Options
265  
266 =over 4
267  
268 =item -distinct => $bool (Default: 0)
269  
270 Liefere in der Resultatliste nur verschiedene Kolumenwerte. Wird ein
271 Hash geliefert, ist dies zwangsläufig der Fall. Der Wert findet
272 sich in der Resultatliste an der Stelle seines ersten Auftretens.
273  
274 =item -hash => $bool (Default: 0)
275  
276 Liefere einen Hash bzw. eine Hashreferenz (Quiq::Hash) mit den
277 Kolumnenwerten als Schlüssel und 1 als Wert.
278  
279 =item -notNull => $bool (Default: 0)
280  
281 Ignoriere Nullwerte, d.h. nimm sie nicht ins Resultat auf.
282  
283 =back
284  
285 =cut
286  
287 # -----------------------------------------------------------------------------
288  
289 0 ▶sub values {
Subroutine values: not called CC=12 SCAR=50.5
Pod documented
290 0 my $self = shift;
291 0 my $key = shift;
292 # @_: @opt
293  
294 0 my $distinct = 0;
295 0 my $hash = 0;
296 0 my $notNull = 0;
297  
298 0 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 0 0 0
299 0 Quiq::Option->extract(\@_,
300 -distinct => \$distinct,
301 -hash => \$hash,
302 -notNull => \$notNull,
303 );
304 }
305  
306 0 my $isRaw = $self->isRaw;
307 0 ▶ my $idx = $isRaw? $self->columnIndex($key): undef;
Branch: 1 branch
BranchTrueFalseTotal
$isRaw ? : 0 0 0
308  
309 0 my (@arr,%seen);
310 0 for my $row (@{$self->rows}) {
311 0 ▶ my $val = $isRaw? $row->[$idx]: $row->$key;
Branch: 1 branch
BranchTrueFalseTotal
$isRaw ? : 0 0 0
312 0 ▶ if ($notNull && $val eq '') {
Branch: 1 branch
BranchTrueFalseTotal
if ($notNull and $val eq "") 0 0 0
Condition0%0/3
$notNull and $val eq ""
!ll&&!rl&&r
000
MC/DC0%0/2
$notNull and $val eq ""
$notNull $val eq ""
Truth table0%0/3
A and B
A= $notNullB= $val eq ""
ABresult
0X0
100
111
313 0 next;
314 }
315 0 ▶ if ($distinct && $seen{$val}++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($distinct and $seen{$val}++) 0 0 0
Condition0%0/3
$distinct and $seen{$val}++
!ll&&!rl&&r
000
MC/DC0%0/2
$distinct and $seen{$val}++
$distinct $seen{$val}++
Truth table0%0/3
A and B
A= $distinctB= $seen{$val}++
ABresult
0X0
100
111
316 0 next;
317 }
318 0 CORE::push @arr,$val;
319 0 ▶ if ($hash) {
Branch: 1 branch
BranchTrueFalseTotal
if ($hash) 0 0 0
320 0 CORE::push @arr,1;
321 }
322 }
323  
324 0 ▶ if (wantarray) {
Branch: 2 branches
BranchTrueFalseTotal
if (wantarray) { } 0 0 0
elsif ($hash) { } 0 0 0
325 0 return @arr;
326 }
327 elsif ($hash) {
328 0 return Quiq::Hash->new({@arr})->unlockKeys;
329 }
330 else {
331 0 return Quiq::Array->new(\@arr);
332 }
333 }
334  
335 # -----------------------------------------------------------------------------
336  
337 =head2 Tests
338  
339 =head3 isRaw() - Prüfe, ob Raw-Tabelle
340  
341 =head4 Synopsis
342  
343 $bool = $this->isRaw;
344  
345 =cut
346  
347 # -----------------------------------------------------------------------------
348  
349 4 ▶sub isRaw {
Subroutine isRaw: called CC=1 SCAR=0.0
Pod documented
350 4 return shift->{'rowClass'}->isRaw;
351 }
352  
353 # -----------------------------------------------------------------------------
354  
355 =head2 Search
356  
357 =head3 lookup() - Suche Datensatz
358  
359 =head4 Synopsis
360  
361 $row = $tab->lookup(@opt,$key=>$val);
362  
363 =head4 Options
364  
365 =over 4
366  
367 =item -sloppy => $bool (Default: 0)
368  
369 Wenn der gesuchte Datensatz nicht existiert, löse keine Exception aus,
370 sondern liefere undef.
371  
372 =back
373  
374 =head4 Description
375  
376 Durchsuche die Tabelle nach dem ersten Datensatz, dessen
377 Attribut $key den Wert $val besitzt und liefere diesen zurück.
378 Erfüllt kein Datensatz das Kriterium, wird eine Exception ausgelöst.
379  
380 =cut
381  
382 # -----------------------------------------------------------------------------
383  
384 5 ▶sub lookup {
Subroutine lookup: called CC=4 SCAR=14.1
Pod documented
385 5 my $self = shift;
386 # @_: @opt,$key=>$val
387  
388 # Optionen
389  
390 5 my $sloppy = 0;
391  
392 5 ▶ if (substr($_[0],0,1) eq '-') {
Branch: 1 branch
BranchTrueFalseTotal
if (substr($_[0], 0, 1) eq "-") 0 5 5
393 0 Quiq::Option->extract(\@_,
394 -sloppy => \$sloppy,
395 );
396 }
397  
398 # Operation ausführen
399  
400 5 my $key = shift;
401 5 my $val = shift;
402  
403 5 my $row = $self->lookupSub($key,$val);
404 5 ▶ if ($row or $sloppy) {
Branch: 1 branch
BranchTrueFalseTotal
if ($row or $sloppy) 3 2 5
Condition66%2/3
$row or $sloppy
l!l&&r!l&&!r
302
MC/DC50%1/2
$row or $sloppy
$row $sloppy
Truth table66%2/3
A or B
A= $rowB= $sloppy
ABresult
1X1
011
000
405 3 return $row;
406 }
407  
408 # Exception
409  
410 $self->throw(
411 2 'TAB-00001: Datensatz nicht gefunden',
412 Key => $key,
413 Value => $val,
414 );
415 }
416  
417 # -----------------------------------------------------------------------------
418  
419 =head3 select() - Suche Datensätze
420  
421 =head4 Synopsis
422  
423 @rows|$tab = $tab->select($testSub);
424  
425 =head4 Description
426  
427 Durchsuche die Tabelle nach den Datensätzen, die Test-Methode
428 $testSub erfüllen und liefere die Liste dieser Datensätze zurück.
429 Im Skalarkontext liefere ein neues Tabellen-Objekt.
430  
431 =head4 Example
432  
433 Schränke Produkt-Tabelle auf Produkte mit einem Preis > 100 ein:
434  
435 my $sub = sub {
436 my $row = shift;
437 return $row->preis > 100? 1: 0;
438 };
439 $tab = $tab->select($sub);
440  
441 =cut
442  
443 # -----------------------------------------------------------------------------
444  
445 0 ▶sub select {
Subroutine select: not called CC=4 SCAR=30.0
Pod documented
446 0 my ($self,$testSub) = @_;
447  
448 0 my @arr;
449 0 for my $row (@{$self->rows}) {
450 0 ▶ if ($testSub->($row)) {
Branch: 1 branch
BranchTrueFalseTotal
if (&$testSub($row)) 0 0 0
451 0 CORE::push @arr,$row;
452 }
453 }
454  
455 0 ▶ if (wantarray) {
Branch: 1 branch
BranchTrueFalseTotal
if (wantarray) 0 0 0
456 0 return @arr;
457 }
458  
459 0 return ref($self)->new(
460 $self->rowClass,
461 scalar($self->titles),
462 \@arr,
463 );
464 }
465  
466 # -----------------------------------------------------------------------------
467  
468 =head2 File I/O
469  
470 =head3 loadFromFile() - Lade Tabelle aus Datei
471  
472 =head4 Synopsis
473  
474 $tab = $class->loadFromFile($file,@opt);
475  
476 =head4 Options
477  
478 =over 4
479  
480 =item -colSep => $char (Default: '|')
481  
482 Kolumnen-Trennzeichen.
483  
484 =item -rowClass => $rowClass (Default: 'Quiq::Database::Row::Object')
485  
486 Name der Datensatzklasse, auf die die Datensätze geblesst werden.
487 Die Datensatzklasse entscheidet auch über die Tabellenklasse.
488  
489 =item -rowStatus => '0'|'U'|'I'|'D' (Default: 'I')
490  
491 Setze den initialen Datensatz-Status.
492  
493 =back
494  
495 =head4 Description
496  
497 Lade Datensätze aus Datei $file in eine Datensatz-Tabelle und
498 liefere eine Referenz auf dieses Objekt zurück.
499  
500 B<Dateiformat>
501  
502 Die erste Zeile enthält die Kolumentitel, alle weiteren Zeilen die
503 Datensätze. Die Kolumen werden per | getrennt.
504  
505 =head4 Example
506  
507 per_id|per_vorname|per_nachname
508 1|Rudi|Ratlos
509 2|Kai|Nelust
510 3|Elli|Pirelli
511 4|Susi|Sorglos
512  
513 =cut
514  
515 # -----------------------------------------------------------------------------
516  
517 2 ▶sub loadFromFile {
Subroutine loadFromFile: called CC=4 SCAR=13.9
Pod documented
518 2 my $class = shift;
519 2 my $file = shift;
520  
521 # Optionen
522  
523 2 my $colSep = '|';
524 2 my $rowClass = $class->defaultRowClass;
525 2 my $rowStatus = undef;
526  
527 2 Quiq::Option->extract(\@_,
528 -colSep => \$colSep,
529 -rowClass => \$rowClass,
530 -rowStatus => \$rowStatus,
531 );
532 2 $colSep = qr/\Q$colSep/;
533  
534 # Operation ausführen
535  
536 2 my (@titles,@rows);
537 2 my $fh = Quiq::FileHandle->new('<',$file);
538 2 ▶ while (<$fh>) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($_ = readline $fh)) 40 2 42
539 40 chomp;
540 40 ▶ if ($. == 1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($. == 1) { } 2 38 40
541 2 @titles = split /$colSep/;
542 }
543 else {
544 38 my @arr = split /$colSep/;
545 38 my $row = $rowClass->new(\@titles,\@arr);
546 38 ▶ if (defined $rowStatus) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $rowStatus) 0 38 38
547 0 $row->rowStatus($rowStatus);
548 }
549 38 CORE::push @rows,$row;
550 }
551 }
552 2 $fh->close;
553  
554 2 return $rowClass->tableClass->new($rowClass,\@titles,\@rows);
555 }
556  
557 # -----------------------------------------------------------------------------
558  
559 =head3 saveToFile() - Sichere Tabelle in Datei
560  
561 =head4 Synopsis
562  
563 $tab->saveToFile($file);
564  
565 =head4 Description
566  
567 Sichere die Datensätze der Tabelle in Datei $file.
568 Die Methode liefert keinen Wert zurück.
569  
570 =cut
571  
572 # -----------------------------------------------------------------------------
573  
574 2 ▶sub saveToFile {
Subroutine saveToFile: called CC=2 SCAR=6.9
Pod documented
575 2 my $self = shift;
576 2 my $file = shift;
577  
578 2 my $fh = Quiq::FileHandle->new('>',$file);
579 2 print $fh join('|',$self->titles),"\n";
580 2 for my $row (@{$self->{'rows'}}) {
581 38 print $fh join('|',@{$row->asArray}),"\n";
582 }
583 2 $fh->close;
584  
585 2 return;
586 }
587  
588 # -----------------------------------------------------------------------------
589  
590 =head2 Miscellaneous
591  
592 =head3 elapsed() - Dauer seit Beginn der SQL_Ausführung
593  
594 =head4 Synopsis
595  
596 $duration = $tab->elapsed;
597  
598 =cut
599  
600 # -----------------------------------------------------------------------------
601  
602 0 ▶sub elapsed {
Subroutine elapsed: not called CC=1 SCAR=6.9
Pod documented
603 0 my $self = shift;
604 0 return Time::HiRes::gettimeofday-$self->startTime;
605 }
606  
607 # -----------------------------------------------------------------------------
608  
609 =head3 formats() - Liefere Kolumnenformate
610  
611 =head4 Synopsis
612  
613 $fmtA | @fmts = $tab->formats;
614 $fmtA | @fmts = $tab->formats($force);
615  
616 =head4 Description
617  
618 Analysiere den Tabelleninhalt und liefere eine Liste von
619 Kolumnenformat-Objekten zurück. Diese können zur tabellarischen
620 Formatierung der Kolumnenwerte herangezogen werden.
621  
622 Die Analyse wird nur einmal durchgeführt und die resultierende
623 Liste im Tabellenobjekt gespeichert. Ist der Parameter $force
624 angegeben und "wahr", wird eine Neuberechnung forciert.
625  
626 =cut
627  
628 # -----------------------------------------------------------------------------
629  
630 0 ▶sub formats {
Subroutine formats: not called CC=7 SCAR=40.3
Pod documented
631 0 my ($self,$force) = @_;
632  
633 0 ▶ if (!$self->{'formatA'} || $force) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $self->{"formatA"} or $force) 0 0 0
Condition0%0/3
not $self->{"formatA"} or $force
l!l&&r!l&&!r
000
MC/DC0%0/2
not $self->{"formatA"} or $force
not $self->{"formatA"} $force
Truth table0%0/3
not(A) or B
A= not $self->{"formatA"}B= $force
ABresult
1X1
011
000
634 0 my $rowA = $self->{'rows'};
635 0 my $titleA = $self->{'titles'};
636  
637 0 my @fmt;
638 0 ▶ for (my $i = 0; $i < @$titleA; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @$titleA) 0 0 0
639 0 my $title = $titleA->[$i];
640  
641 0 my $prp = Quiq::Properties->new;
642 0 for my $row (@$rowA) {
643 0 ▶ $prp->analyze($self->isRaw? $row->[$i]: $row->$title);
Branch: 1 branch
BranchTrueFalseTotal
$self->isRaw ? : 0 0 0
644
645 }
646 0 push @fmt,$prp;
647 }
648 0 $self->{'formatA'} = \@fmt;
649 }
650  
651 0 my $fmtA = $self->{'formatA'};
652 0 ▶ return wantarray? @$fmtA: $fmtA;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 0 0
653 }
654  
655 # -----------------------------------------------------------------------------
656  
657 =head3 width() - Liefere die Breite der Tabelle
658  
659 =head4 Synopsis
660  
661 $n = $tab->width;
662  
663 =cut
664  
665 # -----------------------------------------------------------------------------
666  
667 0 ▶sub width {
Subroutine width: not called CC=1 SCAR=6.9
Pod documented
668 0 return scalar @{$_[0]->{'titles'}};
669 }
670  
671 # -----------------------------------------------------------------------------
672  
673 =head3 count() - Liefere Anzahl der Datensätze
674  
675 =head4 Synopsis
676  
677 $n = $tab->count;
678  
679 =cut
680  
681 # -----------------------------------------------------------------------------
682  
683 3 ▶sub count {
Subroutine count: called CC=1 SCAR=0.0
Pod documented
684 3 return scalar @{$_[0]->{'rows'}};
685 }
686  
687 # -----------------------------------------------------------------------------
688  
689 =head3 pop() - Entferne Datensatz am Ende
690  
691 =head4 Synopsis
692  
693 $tab->pop;
694  
695 =cut
696  
697 # -----------------------------------------------------------------------------
698  
699 0 ▶sub pop {
Subroutine pop: not called CC=1 SCAR=6.9
Pod documented
700 0 my ($self,$row) = @_;
701 0 CORE::pop @{$self->{'rows'}};
702 0 return;
703 }
704  
705 # -----------------------------------------------------------------------------
706  
707 =head3 push() - Füge Datensatz am Ende hinzu
708  
709 =head4 Synopsis
710  
711 $tab->push($row);
712  
713 =cut
714  
715 # -----------------------------------------------------------------------------
716  
717 3 ▶sub push {
Subroutine push: called CC=1 SCAR=0.0
Pod documented
718 3 my ($self,$row) = @_;
719 3 CORE::push @{$self->{'rows'}},$row;
720 3 return;
721 }
722  
723 # -----------------------------------------------------------------------------
724  
725 =head3 unshift() - Füge Datensatz am Anfang hinzu
726  
727 =head4 Synopsis
728  
729 $tab->unshift($row);
730  
731 =cut
732  
733 # -----------------------------------------------------------------------------
734  
735 0 ▶sub unshift {
Subroutine unshift: not called CC=1 SCAR=6.9
Pod documented
736 0 my ($self,$row) = @_;
737 0 CORE::unshift @{$self->{'rows'}},$row;
738 0 return;
739 }
740  
741 # -----------------------------------------------------------------------------
742  
743 =head3 defaultRowClass() - Liefere Namen der Default-Rowklasse
744  
745 =head4 Synopsis
746  
747 $rowClass = $class->defaultRowClass;
748  
749 =head4 Description
750  
751 Liefere den Namen der Default-Rowklasse: 'Quiq::Database::Row::Object'
752  
753 Auf die Default-Rowklasse werden Datensätze instantiiert, für die
754 bei der Instantiierung einer Table-Klasse keine Row-Klasse
755 explizit angegeben wurde.
756  
757 =head4 Details
758  
759 Als Default-Rowklasse wird für die Quiq::Database::ResultSet-
760 Klassenhierarchie 'Quiq::Database::Row::Object' definiert.
761  
762 Die Methode wird in der Subklasse Quiq::Database::ResultSet::Array
763 überschrieben. Für diesen Zweig ist die Default-Rowklasse
764 'Quiq::Database::Row::Array'.
765  
766 =cut
767  
768 # -----------------------------------------------------------------------------
769  
770 2 ▶sub defaultRowClass {
Subroutine defaultRowClass: called CC=1 SCAR=0.0
Pod documented
771 2 return 'Quiq::Database::Row::Object';
772 }
773  
774 # -----------------------------------------------------------------------------
775  
776 =head3 asExcel() - Tabellen-Repräsentation in Excel-Format
777  
778 =head4 Synopsis
779  
780 $tab->asExcel($file);
781  
782 =head4 Arguments
783  
784 =over 4
785  
786 =item $file
787  
788 Pfad der Ausgabedatei.
789  
790 =back
791  
792 =head4 Description
793  
794 Schreibe die Tabelle im Excel-Format auf Datei $file.
795  
796 =cut
797  
798 # -----------------------------------------------------------------------------
799  
800 0 ▶sub asExcel {
Subroutine asExcel: not called CC=4 SCAR=30.0
Pod documented
801 0 my ($self,$file) = @_;
802  
803 # Dieses Modul laden wir nur, wenn wir diese Methode nutzen
804 0 require Quiq::Excel::Writer;
805  
806 # Erzeuge Excel Workbook
807 0 my $wkb = Quiq::Excel::Writer->new($file);
808  
809 # Füge Worksheet hinzu
810 0 my $wks = $wkb->add_worksheet('Data');
811  
812 # Erzeuge Formate
813  
814 0 my $fmt1 = $wkb->add_format;
815 0 $fmt1->set_bold;
816  
817 # Titelzeile
818  
819 0 my @titles = $self->titles;
820 0 my $x = my $y = 0;
821  
822 0 for my $title (@titles) {
823 0 $wks->write($y,$x++,$title,$fmt1);
824 }
825  
826 # Datenzeilen
827  
828 0 for my $row ($self->rows) {
829 0 $x = 0;
830 0 $y++;
831 0 for my $title (@titles) {
832 0 $wks->write($y,$x++,$row->$title);
833 }
834 }
835  
836 # Schließe Workbook
837 0 $wkb->close;
838  
839 0 return;
840 }
841  
842 # -----------------------------------------------------------------------------
843  
844 =head3 asString() - String-Repräsentation der Tabelle
845  
846 =head4 Synopsis
847  
848 $str = $tab->asString;
849 $str = $tab->asString($colSep);
850 $str = $tab->asString($colSep,$rowSep);
851  
852 =head4 Description
853  
854 Liefere eine String-Repräsentation der Tabelle mit $colSep
855 als Datensatz-Trenner (Default: "\t") und $rowSep als
856 Kolumnentrenner (Default: "\n").
857  
858 =cut
859  
860 # -----------------------------------------------------------------------------
861  
862 0 ▶sub asString {
Subroutine asString: not called CC=4 SCAR=30.0
Pod documented
863 0 my $self = shift;
864 0 ▶ my $colSep = shift || "\t";
Condition0%0/2
shift() || "\t"
l!l
00
MC/DC0%0/1
shift() || "\t"
shift()
Truth table0%0/2
A
A= shift()
Aresult
11
00
865 0 ▶ my $rowSep = shift || "\n";
Condition0%0/2
shift() || "\n"
l!l
00
MC/DC0%0/1
shift() || "\n"
shift()
Truth table0%0/2
A
A= shift()
Aresult
11
00
866  
867 0 my $str = '';
868 0 for my $row (@{$self->rows}) {
869 0 $str .= $row->asString($colSep).$rowSep;
870 }
871  
872 0 return $str;
873 }
874  
875 # -----------------------------------------------------------------------------
876  
877 =head3 asTable() - Tabellen-Repräsentation der Tabellendaten
878  
879 =head4 Synopsis
880  
881 $str = $tab->asTable(@opt);
882  
883 =head4 Options
884  
885 =over 4
886  
887 =item -color => $bool
888  
889 Erzeuge eine farbige Ausgabe mit ANSI Colors.
890  
891 =item -info => $n (Default: 3)
892  
893 Umfang an Information, die über die Daten hinaus ausgegeben wird:
894  
895 =over 4
896  
897 =item Z<>0
898  
899 Nur Daten.
900  
901 =item Z<>1
902  
903 Numerierung der Kolumnen, Anzahl Zeilen, Ausführungszeit.
904  
905 =item Z<>2
906  
907 Informationsumfang 1 plus Liste der Kolumnennamen.
908  
909 =item Z<>3
910  
911 Informationsumfang 2 plus SQL-Statement.
912  
913 =back
914  
915 =item -msg => $msg
916  
917 Füge $msg zur Statistik-Zeile hinzu.
918  
919 =back
920  
921 =head4 Description
922  
923 Liefere eine einfache Tabellen-Repräsentation der Tabellendaten.
924  
925 =head4 Example
926  
927 Beispiel-Ausgabe:
928  
929 SELECT
930 *
931 FROM
932 did.mandant
933 WHERE
934 ROWNUM <= 10+1
935 ORDER BY
936 1
937
938 1 id
939 2 id_person
940 3 bezeichnung
941 4 id_verknuepfungsgruppe
942
943 1 2 3 4
944 | 0 | 14485923 | unbekannter Mandant | 0 |
945 | 1 | 14485924 | Otto | 0 |
946 | 2 | 7834646 | Otto - TZ (HB) | 0 |
947 | 3 | 14485928 | Schwab Versand GmbH | 0 |
948 | 4 | 5423454 | Schwab - TZ (HB) | 0 |
949 | 5 | 14913536 | Hanseatic Bank | 0 |
950 | 6 | 14485937 | 3-Pagen Versand | 0 |
951 | 7 | 8371420 | Fegro Markt G. M. B. H. | 0 |
952 | 8 | 14485941 | Heinrich Heine Versand | 0 |
953 | 9 | 14485942 | Hermes T. Kundendienst | 0 |
954
955 0.093s, 10 rows - *MORE ROWS EXIST*
956  
957 =cut
958  
959 # -----------------------------------------------------------------------------
960  
961 0 ▶sub asTable {
Subroutine asTable: not called CC=22 SCAR=62.3
Pod documented
962 0 my $self = shift;
963 # @_: @opt
964  
965 # Optionen
966  
967 0 my $color = 0;
968 0 my $msg = '';
969 0 my $info = 3;
970  
971 0 Quiq::Option->extract(\@_,
972 -color => \$color,
973 -msg => \$msg,
974 -info => \$info,
975 );
976  
977 0 ▶ if ($msg) {
Branch: 1 branch
BranchTrueFalseTotal
if ($msg) 0 0 0
978 0 ▶ $msg = $info? " - $msg": $msg;
Branch: 1 branch
BranchTrueFalseTotal
$info ? : 0 0 0
979 }
980  
981 0 my $a = Quiq::AnsiColor->new($color);
982  
983 0 my $str = '';
984  
985 # Statement
986  
987 0 ▶ if ($info >= 3 && (my $stmt = $self->stmt)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($info >= 3 and my $stmt = $self->stmt) 0 0 0
Condition0%0/3
$info >= 3 and my $stmt = $self->stmt
!ll&&!rl&&r
000
MC/DC0%0/2
$info >= 3 and my $stmt = $self->stmt
$info >= 3 my $stmt = $self->stmt
Truth table0%0/3
A and B
A= $info >= 3B= my $stmt = $self->stmt
ABresult
0X0
100
111
988 0 $str .= $a->str('dark green',$stmt)."\n\n";
989 }
990 0 my @titles = $self->titles;
991 0 ▶ if ($info >= 2) {
Branch: 1 branch
BranchTrueFalseTotal
if ($info >= 2) 0 0 0
992 # Kolumnenbezeichnungen
993  
994 0 my $l = length scalar @titles;
995 0 ▶ for (my $i = 0; $i < @titles; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @titles) 0 0 0
996 0 $str .= $a->str('dark red',sprintf '%*d %s',
997 $l,$i+1,$titles[$i])."\n";
998 }
999 }
1000  
1001 0 ▶ if ($self->count) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->count) 0 0 0
1002 0 my @fmt = $self->formats;
1003  
1004 # Prüfe, ob die Titelliste des ResultSet von der Titelliste
1005 # der Rows abweicht. Wenn ja, müssen wir die Werte einzeln
1006 # abfragen (siehe map{} unten). Bei Raw-Datensätzen kann
1007 # die Titelliste nicht abweichen.
1008  
1009 0 ▶ my $asArray = $self->isRaw || Quiq::Array->eq(
Condition0%0/3
$self->isRaw || 'Quiq::Array'->eq(\@titles, scalar $self->rows->[0]->titles)
l!l&&r!l&&!r
000
MC/DC0%0/2
$self->isRaw || 'Quiq::Array'->eq(\@titles, scalar $self->rows->[0]->titles)
$self->isRaw 'Quiq::Array'->eq(\@titles, scalar $self->rows->[0]->titles)
Truth table0%0/3
A || B
A= $self->isRawB= 'Quiq::Array'->eq(\@titles, scalar $self->rows->[0]->titles)
ABresult
1X1
011
000
1010 \@titles,scalar $self->rows->[0]->titles);
1011  
1012 0 ▶ if ($info) {
Branch: 1 branch
BranchTrueFalseTotal
if ($info) 0 0 0
1013 # Kolumnenzeile
1014  
1015 0 ▶ if ($info > 1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($info > 1) 0 0 0
1016 0 $str .= "\n";
1017 }
1018 0 ▶ for (my $i = 0; $i < @fmt; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @fmt) 0 0 0
1019 0 my $numWidth = length $i+1;
1020 0 my $width = abs($fmt[$i]->width)+3;
1021 0 $str .= $a->str('dark red',sprintf '%d%s',$i+1,
1022 (' ' x ($width-$numWidth)));
1023 }
1024 0 $str .= "\n";
1025 }
1026  
1027 # Tabelle
1028  
1029 0 for my $row ($self->rows) {
1030 0 ▶ my @arr = $asArray? $row->asArray: map {$row->$_} @titles;
Branch: 1 branch
BranchTrueFalseTotal
$asArray ? : 0 0 0
1031 0 $str .= '| ';
1032 0 ▶ for (my $i = 0; $i < @arr; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @arr) 0 0 0
1033 0 ▶ if ($i) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i) 0 0 0
1034 0 $str .= ' | ';
1035 }
1036 0 $str .= $fmt[$i]->format('text',$arr[$i]);
1037 }
1038 0 $str .= " |\n";
1039 }
1040 }
1041  
1042 0 ▶ if ($info) {
Branch: 1 branch
BranchTrueFalseTotal
if ($info) 0 0 0
1043 # Statistik
1044  
1045 0 my $tmp = sprintf "\n%s rows",$self->count;
1046 0 ▶ if (my $duration = $self->execTime + $self->fetchTime) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $duration = $self->execTime + $self->fetchTime) 0 0 0
1047 0 $tmp .= ', '.Quiq::Duration->new($duration)->asShortString(
1048 -precision => 3,
1049 );
1050 }
1051 0 $tmp = $a->str('dark red',$tmp);
1052  
1053 0 ▶ if ($self->moreRowsExist) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->moreRowsExist) 0 0 0
1054 0 $tmp .= ' - *'.$a->str('dark red','MORE ROWS EXIST').'*';
1055 }
1056 0 $str .= $tmp;
1057 }
1058 0 ▶ if ($msg) {
Branch: 1 branch
BranchTrueFalseTotal
if ($msg) 0 0 0
1059 0 $str .= $msg;
1060 }
1061 0 ▶ if ($info || $msg) {
Branch: 1 branch
BranchTrueFalseTotal
if ($info or $msg) 0 0 0
Condition0%0/3
$info or $msg
l!l&&r!l&&!r
000
MC/DC0%0/2
$info or $msg
$info $msg
Truth table0%0/3
A or B
A= $infoB= $msg
ABresult
1X1
011
000
1062 0 $str .= "\n";
1063 }
1064  
1065 0 return $str;
1066 }
1067  
1068 # -----------------------------------------------------------------------------
1069  
1070 =head3 diffReport() - Report über Differenzen
1071  
1072 =head4 Synopsis
1073  
1074 $str = $tab->diffReport;
1075  
1076 =cut
1077  
1078 # -----------------------------------------------------------------------------
1079  
1080 0 ▶sub diffReport {
Subroutine diffReport: not called CC=16 SCAR=56.1
Pod documented
1081 0 my $self = shift;
1082  
1083 0 my $titles = $self->titles;
1084 0 my $keyTitle = $titles->[0];
1085  
1086 0 my $str = '';
1087 0 my $count = 0;
1088 0 ▶ for (my $i = 1; $i < @$titles; $i += 2) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @$titles) 0 0 0
1089 0 my $title1 = $titles->[$i];
1090 0 my $title2 = $titles->[$i+1];
1091  
1092 0 my @diff;
1093 0 for my $row ($self->rows) {
1094 0 my $val1 = $row->$title1;
1095 0 my $val2 = $row->$title2;
1096  
1097 0 ▶ if ($val1 ne $val2) {
Branch: 1 branch
BranchTrueFalseTotal
if ($val1 ne $val2) 0 0 0
1098 0 my $key = $row->$keyTitle;
1099 0 ▶ $val1 = '(null)' if $val1 eq '';
Branch: 1 branch
BranchTrueFalseTotal
if $val1 eq "" 0 0 0
1100 0 ▶ $val2 = '(null)' if $val2 eq '';
Branch: 1 branch
BranchTrueFalseTotal
if $val2 eq "" 0 0 0
1101 0 CORE::push @diff,[$key,$val1,$val2];
1102 0 $count++;
1103 }
1104 }
1105 0 ▶ if (@diff) {
Branch: 1 branch
BranchTrueFalseTotal
if (@diff) 0 0 0
1106 0 my @type = (1) x 3;
1107 0 my @len = (0) x 3;
1108 0 for my $diff (@diff) {
1109 0 ▶ for (my $i = 0; $i < 3; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < 3) 0 0 0
1110 0 my $val = $diff->[$i];
1111 0 ▶ if ($val ne '(null)' && $val =~ /[a-zA-Z]/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($val ne "(null)" and $val =~ /[a-zA-Z]/) 0 0 0
Condition0%0/3
$val ne "(null)" and $val =~ /[a-zA-Z]/
!ll&&!rl&&r
000
MC/DC0%0/2
$val ne "(null)" and $val =~ /[a-zA-Z]/
$val ne "(null)" $val =~ /[a-zA-Z]/
Truth table0%0/3
A and B
A= $val ne "(null)"B= $val =~ /[a-zA-Z]/
ABresult
0X0
100
111
1112 0 $type[$i] = -1;
1113 }
1114 0 my $l = length $val;
1115 0 ▶ if ($l > $len[$i]) {
Branch: 1 branch
BranchTrueFalseTotal
if ($l > $len[$i]) 0 0 0
1116 0 $len[$i] = $l;
1117 }
1118 }
1119 }
1120 0 ▶ for (my $i = 0; $i < 3; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < 3) 0 0 0
1121 0 $len[$i] *= $type[$i];
1122 }
1123  
1124 0 my $title = "$keyTitle | $title1 | $title2";
1125 0 ▶ if ($str) {
Branch: 1 branch
BranchTrueFalseTotal
if ($str) 0 0 0
1126 0 $str .= "\n";
1127 }
1128 0 $str .= "$title\n".('-' x length($title))."\n";
1129 0 for my $diff (@diff) {
1130 0 $str .= sprintf "%*s | %*s | %*s\n",$len[0],$diff->[0],
1131 $len[1],$diff->[1],$len[2],$diff->[2];
1132 }
1133 }
1134 }
1135
1136 0 ▶ return wantarray? ($str,$count): $str;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 0 0
1137 }
1138  
1139 # -----------------------------------------------------------------------------
1140  
1141 =head3 reverse() - Kehre Datensatz-Reihenfolge um
1142  
1143 =head4 Synopsis
1144  
1145 $tab = $tab->reverse;
1146  
1147 =head4 Returns
1148  
1149 Tabellen-Objekt (für Method-Chaining)
1150  
1151 =head4 Description
1152  
1153 Kehre die Reihenfolge der Datensätze innerhalb des Tabellenobjekts
1154 um und liefere eine Referenz auf das Tabellenobjekt zurück.
1155  
1156 Diese Methode ist nützlich, wenn die ersten N Datensätze einer
1157 geordneten Selektion in umgekehrter Reihenfolge ausgegeben werden
1158 sollen.
1159  
1160 =cut
1161  
1162 # -----------------------------------------------------------------------------
1163  
1164 0 ▶sub reverse {
Subroutine reverse: not called CC=1 SCAR=6.9
Pod documented
1165 0 my $self = shift;
1166  
1167 0 my $rowsA = $self->rows;
1168 0 @$rowsA = reverse @$rowsA;
1169  
1170 0 return $self;
1171 }
1172  
1173 # -----------------------------------------------------------------------------
1174  
1175 =head1 VERSION
1176  
1177 1.240
1178  
1179 =head1 AUTHOR
1180  
1181 Frank Seitz, L<http://fseitz.de/>
1182  
1183 =head1 COPYRIGHT
1184  
1185 Copyright (C) 2026 Frank Seitz
1186  
1187 =head1 LICENSE
1188  
1189 This code is free software; you can redistribute it and/or modify
1190 it under the same terms as Perl itself.
1191  
1192 =cut
1193  
1194 # -----------------------------------------------------------------------------
1195  
1196 15 1;
1197  
1198 # eof
« Quiq/Database/DataAnalysis.pm ↑ Summary Quiq/Gd/Component/Graph.pm »