Quiq/Database/ResultSet/Object.pm

filter: Statementstmt 32.7% 54 / 165 Branchbran 17.8% 5 / 28 Conditioncond 5.1% 2 / 39 MC/DCmcdc 0.0% 0 / 21 Subroutinesub 47.8% 11 / 23 Podpod 100% 15 / 15 totaltotal 29.9% 87 / 291 CC 45 SCAR 29.1
CC
45
Cov
26%
min
30.0
maxLength
30.0
max
30.0
CRAP 18.4
« Quiq/Gd/Component/ScatterGraph.pm ↑ Summary Quiq/Database/Api/Dbi/Cursor.pm »
1 # -----------------------------------------------------------------------------
2  
3 =encoding utf8
4  
5 =head1 NAME
6  
7 Quiq::Database::ResultSet::Object - Liste von Datensätzen in Objekt-Repräsentation
8  
9 =head1 BASE CLASS
10  
11 L<Quiq::Database::ResultSet>
12  
13 =head1 DESCRIPTION
14  
15 Ein Objekt der Klasse repräsentiert eine Liste von gleichartigen
16 Datensätzen in Objekt-Repräsentation.
17  
18 =cut
19  
20 # -----------------------------------------------------------------------------
21  
22 package Quiq::Database::ResultSet::Object;
23 14 ▶use base qw/Quiq::Database::ResultSet/;
Subroutine BEGIN: called CC=1 SCAR=0.0
24  
25 14 ▶use v5.10;
Subroutine BEGIN: called CC=1 SCAR=0.0
26 14 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
27 14 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
28  
29 14 our $VERSION = '1.240';
30  
31 14 ▶use Quiq::Option;
Subroutine BEGIN: called CC=1 SCAR=0.0
32 14 ▶use Quiq::Hash;
Subroutine BEGIN: called CC=1 SCAR=0.0
33 14 ▶use Quiq::Formatter;
Subroutine BEGIN: called CC=1 SCAR=0.0
34  
35 # -----------------------------------------------------------------------------
36  
37 =head1 METHODS
38  
39 =head2 Subklassenfunktionalität
40  
41 =head3 lookupSub() - Suche Datensatz
42  
43 =head4 Synopsis
44  
45 $row = $tab->lookupSub($key=>$val);
46  
47 =head4 Description
48  
49 Durchsuche die Tabelle nach dem ersten Datensatz, dessen
50 Attribut $key den Wert $val besitzt und liefere diesen zurück.
51 Erfüllt kein Datensatz das Kriterium, liefere undef.
52  
53 =cut
54  
55 # -----------------------------------------------------------------------------
56  
57 3 ▶sub lookupSub {
Subroutine lookupSub: called CC=3 SCAR=11.0
Pod documented
58 3 my ($self,$key,$val) = @_;
59  
60 3 for my $row (@{$self->rows}) {
61 22 ▶ if ($row->$key eq $val) {
Branch: 1 branch
BranchTrueFalseTotal
if ($row->$key eq $val) 2 20 22
62 2 return $row;
63 }
64 }
65  
66 1 return undef;
67 }
68  
69 # -----------------------------------------------------------------------------
70  
71 =head3 index() - Indiziere Tabelle nach Kolumne(n)
72  
73 =head4 Synopsis
74  
75 %idx|$idxH = $tab->index(@keys,@opts);
76  
77 =head4 Options
78  
79 =over 4
80  
81 =item -unique => $bool (Default: 1)
82  
83 Sollte auf 0 gesetzt werden, wenn die @keys nicht eindeutig sind.
84 Dann ist der Hashwert nicht die jeweilige Row, sondern eine
85 Referenz auf ein Array von Rows (auch wenn nur eine Row enthalten
86 ist).
87  
88 =back
89  
90 =head4 Description
91  
92 Generiere einen Hash mit den Werten der Kolumen @keys als Schlüssel
93 und mit dem Datensatz als Wert. Im skalaren Kontext liefere eine
94 Referenz auf den Hash.
95  
96 Wird der Index über mehreren Keys gebildet, werden die einzelnen
97 Werte im Hash mit einem senkrechten Strich ('|') getrennt.
98  
99 =cut
100  
101 # -----------------------------------------------------------------------------
102  
103 1 ▶sub index {
Subroutine index: called CC=7 SCAR=21.8
Pod documented
104 1 my $self = shift;
105 # @_: @keys,@opts
106  
107 # Optionen
108  
109 1 my $unique = 1;
110  
111 1 Quiq::Option->extract(\@_,
112 -unique => \$unique,
113 );
114  
115 # Verarbeitung
116  
117 1 my %idx;
118 1 for my $row (@{$self->rows}) {
119 4 my $indexKey;
120 4 for my $key (@_) {
121 4 ▶ if ($indexKey) {
Branch: 1 branch
BranchTrueFalseTotal
if ($indexKey) 0 4 4
122 0 $indexKey .= '|';
123 }
124 4 $indexKey .= $row->$key;
125 }
126 4 ▶ if ($unique) {
Branch: 1 branch
BranchTrueFalseTotal
if ($unique) { } 4 0 4
127 # FIXME: Eindeutigkeit prüfen?
128 4 $idx{$indexKey} = $row;
129 }
130 else {
131 0 ▶ my $arr = $idx{$indexKey} ||= [];
Condition0%0/2
$idx{$indexKey} ||= []
l!l
00
MC/DC0%0/1
$idx{$indexKey} ||= []
$idx{$indexKey}
Truth table0%0/2
A
A= $idx{$indexKey}
Aresult
11
00
132 0 push @$arr,$row;
133 }
134 }
135  
136 1 ▶ return wantarray? %idx: Quiq::Hash->new(\%idx)->unlockKeys;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 1 1
137 }
138  
139 # -----------------------------------------------------------------------------
140  
141 =head3 min() - Numerisches Minimum der Kolumne
142  
143 =head4 Synopsis
144  
145 $min = $tab->min($key);
146  
147 =cut
148  
149 # -----------------------------------------------------------------------------
150  
151 0 ▶sub min {
Subroutine min: not called CC=5 SCAR=34.0
Pod documented
152 0 my ($self,$key) = @_;
153  
154 0 my $min;
155 0 for my $row (@{$self->rows}) {
156 0 my $x = $row->$key;
157 0 ▶ if ($x ne '' && (!defined($min) || $x < $min)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($x ne "" and !defined($min) || $x < $min) 0 0 0
Condition0%0/6
$x ne "" and !defined($min) || $x < $min
!ll&&!rl&&r
000
not defined $min or $x < $min
l!l&&r!l&&!r
000
MC/DC0%0/3
$x ne "" and !defined($min) || $x < $min
$x ne "" not defined $min $x < $min
Truth table0%0/4
A and not(B) or C
A= $x ne ""B= not defined $minC= $x < $min
ABCresult
0XX0
1000
11X1
1011
158 0 $min = $x;
159 }
160 }
161  
162 0 return $min;
163 }
164  
165 # -----------------------------------------------------------------------------
166  
167 =head3 maxLength() - Maximale Länge der Kolumnenwerte
168  
169 =head4 Synopsis
170  
171 $len = $tab->maxLength($key);
172 @len = $tab->maxLength(@keys);
173  
174 =cut
175  
176 # -----------------------------------------------------------------------------
177  
178 0 ▶sub maxLength {
Subroutine maxLength: not called CC=5 SCAR=34.0
Pod documented
179 0 my $self = shift;
180 # @_: @keys
181  
182 0 my @len = (0) x @_;
183 0 for my $row (@{$self->rows}) {
184 0 ▶ for (my $i = 0; $i < @_; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @_) 0 0 0
185 0 my $key = $_[$i];
186 0 my $l = length $row->$key;
187 0 ▶ if ($l > $len[$i]) {
Branch: 1 branch
BranchTrueFalseTotal
if ($l > $len[$i]) 0 0 0
188 0 $len[$i] = $l;
189 }
190 }
191 }
192  
193 0 ▶ return wantarray? @len: $len[0];
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 0 0
194 }
195  
196 # -----------------------------------------------------------------------------
197  
198 =head3 max() - Numerisches Maximum der Kolumne
199  
200 =head4 Synopsis
201  
202 $max = $tab->max($key);
203  
204 =cut
205  
206 # -----------------------------------------------------------------------------
207  
208 0 ▶sub max {
Subroutine max: not called CC=5 SCAR=34.0
Pod documented
209 0 my ($self,$key) = @_;
210  
211 0 my $max;
212 0 for my $row (@{$self->rows}) {
213 0 my $x = $row->$key;
214 0 ▶ if ($x ne '' && (!defined($max) || $x > $max)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($x ne "" and !defined($max) || $x > $max) 0 0 0
Condition0%0/6
$x ne "" and !defined($max) || $x > $max
!ll&&!rl&&r
000
not defined $max or $x > $max
l!l&&r!l&&!r
000
MC/DC0%0/3
$x ne "" and !defined($max) || $x > $max
$x ne "" not defined $max $x > $max
Truth table0%0/4
A and not(B) or C
A= $x ne ""B= not defined $maxC= $x > $max
ABCresult
0XX0
1000
11X1
1011
215 0 $max = $x;
216 }
217 }
218  
219 0 return $max;
220 }
221  
222 # -----------------------------------------------------------------------------
223  
224 =head3 minStr() - Alphaumerisches Minimum der Kolumne
225  
226 =head4 Synopsis
227  
228 $min = $tab->minStr($key);
229  
230 =cut
231  
232 # -----------------------------------------------------------------------------
233  
234 0 ▶sub minStr {
Subroutine minStr: not called CC=5 SCAR=34.0
Pod documented
235 0 my ($self,$key) = @_;
236  
237 0 my $min;
238 0 for my $row (@{$self->rows}) {
239 0 my $x = $row->$key;
240 0 ▶ if ($x ne '' && (!defined($min) || $x le $min)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($x ne "" and !defined($min) || $x le $min) 0 0 0
Condition0%0/6
$x ne "" and !defined($min) || $x le $min
!ll&&!rl&&r
000
not defined $min or $x le $min
l!l&&r!l&&!r
000
MC/DC0%0/3
$x ne "" and !defined($min) || $x le $min
$x ne "" not defined $min $x le $min
Truth table0%0/4
A and not(B) or C
A= $x ne ""B= not defined $minC= $x le $min
ABCresult
0XX0
1000
11X1
1011
241 0 $min = $x;
242 }
243 }
244  
245 0 return $min;
246 }
247  
248 # -----------------------------------------------------------------------------
249  
250 =head3 maxStr() - Alphanumerisches Maximum der Kolumne
251  
252 =head4 Synopsis
253  
254 $max = $tab->maxStr($key);
255  
256 =cut
257  
258 # -----------------------------------------------------------------------------
259  
260 0 ▶sub maxStr {
Subroutine maxStr: not called CC=5 SCAR=34.0
Pod documented
261 0 my ($self,$key) = @_;
262  
263 0 my $max;
264 0 for my $row (@{$self->rows}) {
265 0 my $x = $row->$key;
266 0 ▶ if ($x ne '' && (!defined($max) || $x gt $max)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($x ne "" and !defined($max) || $x gt $max) 0 0 0
Condition0%0/6
$x ne "" and !defined($max) || $x gt $max
!ll&&!rl&&r
000
not defined $max or $x gt $max
l!l&&r!l&&!r
000
MC/DC0%0/3
$x ne "" and !defined($max) || $x gt $max
$x ne "" not defined $max $x gt $max
Truth table0%0/4
A and not(B) or C
A= $x ne ""B= not defined $maxC= $x gt $max
ABCresult
0XX0
1000
11X1
1011
267 0 $max = $x;
268 }
269 }
270  
271 0 return $max;
272 }
273  
274 # -----------------------------------------------------------------------------
275  
276 =head2 Verschiedenes
277  
278 =head3 sort() - Sortiere Datensätze
279  
280 =head4 Synopsis
281  
282 $tab->sort($sub);
283  
284 =head4 Description
285  
286 Sortiere die Datensätze gemäß der anonymen Sortierfunktion $sub.
287  
288 ACHTUNG: Die Sortierfunktion muss mit Prototype ($$) vereinbart
289 werden, damit die Elemente per Parameter und nicht mittels
290 der globalen Variablen $a und $b übergeben werden. Denn die globalen
291 Variablen befinden sich in einem anderen Package als dem, in dem
292 die Sortierfunktion aufgerufen wird. Für eine korrekte
293 Definition siehe Beispiel.
294  
295 =head4 Example
296  
297 $tab->sort(sub ($$) {
298 my ($a,$b) = @_;
299 uc($a->pfad) cmp uc($b->pfad);
300 });
301  
302 =cut
303  
304 # -----------------------------------------------------------------------------
305  
306 0 ▶sub sort {
Subroutine sort: not called CC=1 SCAR=6.9
Pod documented
307 0 my ($self,$sub) = @_;
308  
309 0 my $rowA = $self->{'rows'};
310 0 @$rowA = sort $sub @$rowA;
311  
312 0 return;
313 }
314  
315 # -----------------------------------------------------------------------------
316  
317 =head3 absorbModifications() - Absorbiere Datensatz-Änderungen
318  
319 =head4 Synopsis
320  
321 $tab->absorbModifications;
322  
323 =head4 Returns
324  
325 nichts
326  
327 =head4 See Also
328  
329 $row->absorbModifications()
330  
331 =cut
332  
333 # -----------------------------------------------------------------------------
334  
335 0 ▶sub absorbModifications {
Subroutine absorbModifications: not called CC=2 SCAR=17.9
Pod documented
336 0 my $self = shift;
337  
338 0 for my $row (@{$self->rows}) {
339 0 $row->absorbModifications;
340 }
341  
342 0 return;
343 }
344  
345 # -----------------------------------------------------------------------------
346  
347 =head3 addAttribute() - Füge Attribut zu allen Datensätzen hinzu
348  
349 =head4 Synopsis
350  
351 $tab->addAttribute($key);
352 $tab->addAttribute($key=>$val);
353  
354 =head4 Arguments
355  
356 =over 4
357  
358 =item $key
359  
360 Attributname.
361  
362 =item $val
363  
364 Attributwert.
365  
366 =back
367  
368 =head4 Description
369  
370 Füge Attribut $key mit Wert $val zu allen Datensätzen der
371 Ergebnismenge hinzu. Ist $val nicht angegeben, setze den Wert auf
372 den Nullwert (Leerstring).
373  
374 =cut
375  
376 # -----------------------------------------------------------------------------
377  
378 0 ▶sub addAttribute {
Subroutine addAttribute: not called CC=3 SCAR=24.8
Pod documented
379 0 my ($self,$key) = splice @_,0,2;
380 0 ▶ my $val = shift // '';
Condition0%0/2
shift() // ""
l!l
00
MC/DC0%0/1
shift() // ""
shift()
Truth table0%0/2
A
A= shift()
Aresult
11
00
381  
382 0 for my $row (@{$self->rows}) {
383 0 $row->addAttribute($key=>$val);
384 }
385  
386 0 return;
387 }
388  
389 # -----------------------------------------------------------------------------
390  
391 =head3 normalizeNumber() - Normalisiere Zahldarstellung
392  
393 =head4 Synopsis
394  
395 $tab->normalizeNumber(@titles);
396  
397 =head4 Alias
398  
399 fixNumber()
400  
401 =head4 Returns
402  
403 nichts
404  
405 =head4 Description
406  
407 Normalisiere die Zahldarstellung der genannten Kolumnen. D.h. entferne
408 unnötige Nullen und forciere als Dezimaltrennzeichen einen Punkt
409 (anstelle eines Komma).
410  
411 =cut
412  
413 # -----------------------------------------------------------------------------
414  
415 0 ▶sub normalizeNumber {
Subroutine normalizeNumber: not called CC=3 SCAR=24.8
Pod documented
416 0 my $self = shift;
417 # @_: @titles
418  
419 0 for my $row (@{$self->rows}) {
420 0 for my $title (@_) {
421 0 my $val = $row->$title;
422 0 $val = Quiq::Formatter->normalizeNumber($val);
423 0 $row->$title($val);
424 }
425 }
426  
427 0 return;
428 }
429  
430 {
431 14 ▶ no warnings 'once';
Subroutine BEGIN: called CC=1 SCAR=0.0
432 14 *fixNumber = \&normalizeNumber;
433 }
434  
435 # -----------------------------------------------------------------------------
436  
437 =head3 addChildType() - Füge Kind-Datensatz-Typ zu allen Datensätzen hinzu
438  
439 =head4 Synopsis
440  
441 $tab->addChildType($type);
442 $tab->addChildType($type,$rowClass,\@titles);
443  
444 =head4 Description
445  
446 Füge Kind-Datensatz-Typ $type mit Datensatz-Klasse $rowClass und
447 den Kolumnentiteln @titles zu allen Datensätzen des ResultSet $tab
448 hinzu.
449  
450 Findet die Verknüfung zwischen den Datensätzen des ResultSet
451 selbst statt, müssen $rowClass und \@titles nicht angegeben
452 werden. Es werden dann die Angaben aus $tab genommen.
453  
454 =cut
455  
456 # -----------------------------------------------------------------------------
457  
458 1 ▶sub addChildType {
Subroutine addChildType: called CC=4 SCAR=14.8
Pod documented
459 1 my $self = shift;
460 1 my $type = shift;
461 1 ▶ my $rowClass = shift || $self->rowClass;
Condition33%1/3
shift() || $self->rowClass
l!l&&r!l&&!r
010
MC/DC0%0/2
shift() || $self->rowClass
shift() $self->rowClass
Truth table33%1/3
A || B
A= shift()B= $self->rowClass
ABresult
1X1
011
000
462 1 ▶ my $titleA = shift || $self->titles;
Condition33%1/3
shift() || $self->titles
l!l&&r!l&&!r
010
MC/DC0%0/2
shift() || $self->titles
shift() $self->titles
Truth table33%1/3
A || B
A= shift()B= $self->titles
ABresult
1X1
011
000
463  
464 1 for my $row (@{$self->rows}) {
465 4 $row->addChildType($type,$rowClass,$titleA);
466 }
467  
468 1 return;
469 }
470  
471 # -----------------------------------------------------------------------------
472  
473 =head3 selectChilds() - Selektiere Kind-Datensätze
474  
475 =head4 Synopsis
476  
477 @rows|$rowT = $tab->selectChilds($db,$primaryKeyColumn,
478 $foreignTable,$foreignKeyColumn,@opt);
479  
480 =head4 Options
481  
482 =over 4
483  
484 =item -type => $type (Default: "$foreignTable.$foreignKeyColumn")
485  
486 Bezeichner für den Satz an Kind-Objekten.
487  
488 =item I<Select-Optionen>
489  
490 Select-Optionen, die der Selektion der Kinddatensätze
491 hinzugefügt werden.
492  
493 =back
494  
495 =head4 Description
496  
497 Selektiere alle Datensätze der Tabelle $foreignTable, deren
498 Kolumne $foreignKeyColumn auf die Kolumne $primaryKeyColumn
499 verweist und liefere diese zurück.
500  
501 Die Kind-Datensätze werden ihren Eltern-Datensätzen zugeordnet
502 und können per
503  
504 @childRows = $row->childs("$foreignTable,$foreignKeyColumn");
505  
506 oder
507  
508 $childRowT = $row->childs("$foreignTable,$foreignKeyColumn");
509  
510 abgefragt werden. Z.B.
511  
512 -select=>@titles oder -oderBy=>@titles
513  
514 Mittels der Option C<< -type=>$type >> kann ein anderer Typbezeichner
515 anstelle von "$foreignTable,$foreignKeyColumn" für den Satz an
516 Kinddatensätzen vereinbart werden.
517  
518 =cut
519  
520 # -----------------------------------------------------------------------------
521  
522 0 ▶sub selectChilds {
Subroutine selectChilds: not called CC=5 SCAR=34.0
Pod documented
523 0 my $self = shift;
524 0 my $db = shift;
525 0 my $primaryKeyColumn = shift;
526 0 my $foreignTable = shift;
527 0 my $foreignKeyColumn = shift;
528 # @_: @opt
529  
530 # Optionen
531  
532 0 my $type = "$foreignTable.$foreignKeyColumn";
533  
534 0 Quiq::Option->extract(-mode=>'sloppy',\@_,
535 -type => \$type,
536 );
537  
538 # Subselect generieren
539  
540 0 my $stmt = $self->stmtBody;
541 0 $stmt = "SELECT\n $primaryKeyColumn\n$stmt";
542 0 $stmt =~ s/^/ /gm;
543  
544 # Kind-Datensätze selektieren
545 # (die restlichen Optionen sind Select-Optionen)
546  
547 0 my $tab = $db->select($foreignTable,
548 -where,"$foreignKeyColumn IN (\n$stmt\n)",
549 @_,
550 );
551  
552 # Kind-Datensätze zuordnen
553  
554 0 my $rowClass = $tab->rowClass;
555 0 my $titleA = $tab->titles;
556  
557 # Eltern-Datensätze um Kind-Typ erweitern
558  
559 0 for my $row ($self->rows) {
560 0 $row->addChildType($type,$rowClass,$titleA);
561 }
562  
563 # Indiziere Eltern-Datensätze nach Primärschlüssel
564 0 my %idx = $self->index($primaryKeyColumn);
565  
566 0 for my $childRow ($tab->rows) {
567 0 my $key = $childRow->$foreignKeyColumn;
568 0 ▶ my $parentRow = $idx{$key} || die;
Condition0%0/2
$idx{$key} || die()
l!l
00
MC/DC0%0/1
$idx{$key} || die()
$idx{$key}
Truth table0%0/2
A
A= $idx{$key}
Aresult
11
00
569  
570 # Kind-Datensatz zum Elterndatensatz hinzufügen
571 0 $parentRow->addChild($type,$childRow);
572 }
573  
574 0 ▶ return wantarray? $tab->rows: $tab;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 0 0
575 }
576  
577 # -----------------------------------------------------------------------------
578  
579 =head3 selectParents() - Selektiere Parent-Datensätze
580  
581 =head4 Synopsis
582  
583 @rows|$rowT = $tab->selectParents($db,$foreignKeyColumn,
584 $parentTable,$primaryKeyColumn,@opt);
585  
586 =head4 Options
587  
588 =over 4
589  
590 =item -type => $type (Default: $foreignKeyColumn)
591  
592 Bezeichner für den Parent-Datensatz beim Child-Datensatz.
593  
594 =item I<Select-Optionen>
595  
596 Select-Optionen, die der Selektion der Parent-Datensatzes
597 hinzugefügt werden.
598  
599 =back
600  
601 =head4 Description
602  
603 Selektiere alle Datensätze der Tabelle $parentTable, auf die
604 von der Kolumne $foreignKeyColumn aller in Tabelle $tab
605 enthaltenen Datensätze verwiesen wird und liefere diese zurück.
606  
607 Der Parent-Datensatz wird jeweils seinem Kind-Datensatz
608 zugeordnet und kann per
609  
610 $parentRow = $row->getParent($foreignKeyColumn);
611  
612 abgefragt werden.
613  
614 Mittels der Option C<< -type=>$type >> kann ein anderer Typbezeichner
615 anstelle von "$foreignKeyColumn" für den Parent-Datensatz
616 vereinbart werden.
617  
618 =cut
619  
620 # -----------------------------------------------------------------------------
621  
622 0 ▶sub selectParents {
Subroutine selectParents: not called CC=5 SCAR=34.0
Pod documented
623 0 my $self = shift;
624 0 my $db = shift;
625 0 my $foreignKeyColumn = shift;
626 0 my $parentTable = shift;
627 0 my $primaryKeyColumn = shift;
628 # @_: @opt
629  
630 # Optionen
631  
632 0 my $type = $foreignKeyColumn;
633  
634 0 Quiq::Option->extract(-mode=>'sloppy',\@_,
635 -type => \$type,
636 );
637  
638 # Subselect generieren
639  
640 0 my $stmt = $self->stmtBody;
641 0 $stmt = "SELECT\n $foreignKeyColumn\n$stmt";
642 0 $stmt =~ s/^/ /gm;
643  
644 # Parent-Datensätze selektieren
645 # (die restlichen Optionen sind Select-Optionen)
646  
647 0 my $tab = $db->select($parentTable,
648 -where,"$primaryKeyColumn IN (\n$stmt\n)",
649 @_,
650 );
651 0 my %idx = $tab->index('id');
652  
653 # Datensätze mit Eltern-Datensatz verknüpfen erweitern
654  
655 0 for my $row ($self->rows) {
656 0 my $parentRow;
657 0 ▶ if (my $parentId = $row->$foreignKeyColumn) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $parentId = $row->$foreignKeyColumn) 0 0 0
658 0 ▶ $parentRow = $idx{$parentId} || $self->throw;
Condition0%0/3
$idx{$parentId} || $self->throw
l!l&&r!l&&!r
000
MC/DC0%0/2
$idx{$parentId} || $self->throw
$idx{$parentId} $self->throw
Truth table0%0/3
A || B
A= $idx{$parentId}B= $self->throw
ABresult
1X1
011
000
659 }
660 0 $row->addParent($foreignKeyColumn=>$parentRow);
661 }
662  
663 0 ▶ return wantarray? $tab->rows: $tab;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 0 0
664 }
665  
666 # -----------------------------------------------------------------------------
667  
668 =head3 selectParentRows() - Selektiere Datensätze via Schlüsselkolumne
669  
670 =head4 Synopsis
671  
672 @rows|$rowT = $tab->selectParentRows($db,$fkTitle,$pClass,@select);
673  
674 =head4 Returns
675  
676 =over 4
677  
678 =item Array-Kontext
679  
680 Liste von Datensätzen
681  
682 =item Skalar-Kontext
683  
684 Tabellenobjekt (Quiq::Database::ResultSet::Object)
685  
686 =back
687  
688 =head4 Description
689  
690 Die Methode ermöglicht es, Fremschlüsselverweise einer Selektion
691 durch effiziente Nachselektion aufzulösen.
692  
693 Die Methode selektiert die Elterndatensätze der Tabellen-Klasse
694 C<$pClass> zu den Fremdschlüsselwerten der Kolumne C<$fkTitle> und
695 den zusätzlichen Selektionsdirektiven C<@select>. Die
696 Selektionsdirektiven sind typischerweise C<-select> und C<-orderBy>.
697  
698 Die Klasse C<$pClass> muss eine Tabellenklasse sein, denn nur diese
699 definiert eine Primäschlüsselkolumne.
700  
701 =head4 Example
702  
703 Bestimme Informationen zu Route, Abschnitt, Fahrt, Fahrt_Parameter
704 und Parameter zu der Kombination aus Fahrten und Parametern:
705  
706 my @pas_id = $req->getArray('pas_id');
707 my @mea_id = $req->getArray('mea_id');
708
709 my $tab = FerryBox::Model::Join::RouSecPasPamMea->select($db2,
710 -select => 'rou.id rou_id','sec.id sec_id','pas.id pas_id',
711 'pam.id pam_id','mea.id mea_id',
712 -where,
713 'pas.id' => ['IN',@pas_id],
714 'mea.id' => ['IN',@mea_id],
715 );
716
717 my $rouT = $tab->selectParentRows($db2,
718 rou_id => 'FerryBox::Model::Table::Route',
719 -select => qw/id name/,
720 );
721
722 my $secT = $tab->selectParentRows($db2,
723 sec_id => 'FerryBox::Model::Table::Section',
724 -select => qw/id route_id secname/,
725 );
726
727 my $pasT = $tab->selectParentRows($db2,
728 pas_id => 'FerryBox::Model::Table::Passage',
729 -select => qw/id section_id starttime/,
730 );
731
732 my $pamT = $tab->selectParentRows($db2,
733 pam_id => 'FerryBox::Model::Table::Passage_Measseq',
734 -select => qw/id passage_id measseq_id/,
735 );
736
737 my $meaT = $tab->selectParentRows($db2,
738 mea_id => 'FerryBox::Model::Table::Measseq',
739 -select => qw/id route_id meas/,
740 );
741  
742 =cut
743  
744 # -----------------------------------------------------------------------------
745  
746 0 ▶sub selectParentRows {
Subroutine selectParentRows: not called CC=1 SCAR=6.9
Pod documented
747 0 my ($self,$db,$fkTitle,$pClass,@select) = @_;
748  
749 # Bestimme alle Foreign-Key-Werte
750 0 my @pkValues = $self->values($fkTitle,-notNull=>1,-distinct=>1);
751  
752 # Bestimme PK-Kolumne der Parent-Tabelle
753 0 my $pkTitle = $pClass->primaryKey($db);
754  
755 # Selektiere alle Parent-Datensätze
756  
757 0 return $pClass->select($db,
758 -where,'+null',$pkTitle=>['IN',@pkValues],
759 @select,
760 );
761 }
762  
763 # -----------------------------------------------------------------------------
764  
765 =head1 VERSION
766  
767 1.240
768  
769 =head1 AUTHOR
770  
771 Frank Seitz, L<http://fseitz.de/>
772  
773 =head1 COPYRIGHT
774  
775 Copyright (C) 2026 Frank Seitz
776  
777 =head1 LICENSE
778  
779 This code is free software; you can redistribute it and/or modify
780 it under the same terms as Perl itself.
781  
782 =cut
783  
784 # -----------------------------------------------------------------------------
785  
786 14 1;
787  
788 # eof
« Quiq/Gd/Component/ScatterGraph.pm ↑ Summary Quiq/Database/Api/Dbi/Cursor.pm »