Quiq/Database/Row/Object.pm

filter: Statementstmt 61.0% 188 / 308 Branchbran 55.5% 70 / 126 Conditioncond 60.8% 14 / 23 MC/DCmcdc 46.6% 7 / 15 Subroutinesub 69.2% 36 / 52 Podpod 100% 38 / 38 totaltotal 62.8% 353 / 562 CC 81 SCAR 24.6
CC
81
Cov
60%
modifiedColumns
56.0
asRecord
42.0
modificationInfo
30.0
CRAP 11.7
« Quiq/StreamServe/Block.pm ↑ Summary Quiq/File/Video.pm »
1 # -----------------------------------------------------------------------------
2  
3 =encoding utf8
4  
5 =head1 NAME
6  
7 Quiq::Database::Row::Object - Datensatz als Objekt
8  
9 =head1 BASE CLASS
10  
11 L<Quiq::Database::Row>
12  
13 =head1 DESCRIPTION
14  
15 Ein Objekt der Klasse repräsentiert einen Datensatz.
16  
17 =cut
18  
19 # -----------------------------------------------------------------------------
20  
21 package Quiq::Database::Row::Object;
22 10 ▶use base qw/Quiq::Database::Row/;
Subroutine BEGIN: called CC=1 SCAR=0.0
23  
24 10 ▶use v5.10;
Subroutine BEGIN: called CC=1 SCAR=0.0
25 10 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
26 10 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
27 10 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
28  
29 10 our $VERSION = '1.240';
30  
31 10 ▶use Quiq::Hash;
Subroutine BEGIN: called CC=1 SCAR=0.0
32 10 ▶use Quiq::Option;
Subroutine BEGIN: called CC=1 SCAR=0.0
33 10 ▶use Scalar::Util ();
Subroutine BEGIN: called CC=1 SCAR=0.0
34 10 ▶use Quiq::Database::ResultSet;
Subroutine BEGIN: called CC=1 SCAR=0.0
35 10 ▶use Quiq::AnsiColor;
Subroutine BEGIN: called CC=1 SCAR=0.0
36  
37 # -----------------------------------------------------------------------------
38  
39 # Default-Tabellenklasse
40 10 our $TableClass = 'Quiq::Database::ResultSet::Object';
41  
42 # -----------------------------------------------------------------------------
43  
44 =head1 METHODS
45  
46 =head2 Konstructor
47  
48 =head3 new() - Konstruktor
49  
50 =head4 Synopsis
51  
52 $row = $class->new($db,@keyVal); # [1]
53 $row = $class->new(\@titles,\@values,@keyVal); # [2]
54 $row = $class->new(\@titles,@keyVal); # [3]
55 $row = $class->new(@keyVal); # [4]
56  
57 =head4 Description
58  
59 Instantiiere ein Datensatz-Objekt mit den Kolumnen @titles und
60 den Kolumnenwerten @values und liefere eine Referenz auf
61 dieses Objekt zurück.
62  
63 =cut
64  
65 # -----------------------------------------------------------------------------
66  
67 73 ▶sub new {
Subroutine new: called CC=8 SCAR=21.6
Pod documented
68 73 my $class = shift;
69 # @_: Argumente
70  
71 73 my ($titleA,$valueA);
72  
73 73 ▶ if (ref $_[0]) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $_[0]) { } 53 20 73
74 53 ▶ if (Scalar::Util::blessed($_[0]) &&
Branch: 1 branch
BranchTrueFalseTotal
if (builtin::blessed($_[0]) and $_[0]->isa('Quiq::Database::Connection')) { } 0 53 53
Condition33%1/3
builtin::blessed($_[0]) and $_[0]->isa("Quiq::Database::Connection")
!ll&&!rl&&r
5300
MC/DC0%0/2
builtin::blessed($_[0]) and $_[0]->isa("Quiq::Database::Connection")
builtin::blessed($_[0]) $_[0]->isa("Quiq::Database::Connection")
Truth table33%1/3
A and B
A= builtin::blessed($_[0])B= $_[0]->isa("Quiq::Database::Connection")
ABresult
0X0
100
111
75 $_[0]->isa('Quiq::Database::Connection')) {
76 # Aufruf: $class->new($db,@keyVal);
77 0 $titleA = $class->titles(shift);
78 }
79 else {
80 # Aufruf: $class->new(\@titles,@keyVal);
81 # Aufruf: $class->new(\@titles,\@values,@keyVal);
82 53 $titleA = shift;
83 53 ▶ if (ref $_[0]) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $_[0]) 52 1 53
84 52 $valueA = shift;
85 }
86 }
87 }
88 else {
89 # Aufruf: $row = $class->new(@keyVal);
90  
91 20 ▶ while (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 57 20 77
92 57 push @$titleA,shift;
93 57 push @$valueA,shift;
94 }
95 }
96  
97 73 ▶ my $hash = Quiq::Hash->new($titleA,$valueA? $valueA: (),'')
Branch: 1 branch
BranchTrueFalseTotal
$valueA ? : 72 1 73
98 ->unlockKeys;
99  
100 73 my $self = bless [
101 $hash, # [0] Daten-Hash
102 $titleA, # [1] Titel-Liste
103 'I', # [2] Datensatz-Status
104 undef, # [3] Änderungs-Hash
105 undef, # [4] Kind-Datensätze-Hash
106 undef, # [5] Eltern-Datensätze-Hash
107 ],$class;
108  
109 # @keyVal setzen, falls vorhanden
110  
111 73 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 0 73 73
112 0 $self->set(@_);
113 0 $self->[3] = undef;
114 }
115  
116 73 return $self;
117 }
118  
119 # -----------------------------------------------------------------------------
120  
121 =head2 Attributmethoden
122  
123 =head3 exists() - Prüfe, ob Datensatz-Attribut existiert
124  
125 =head4 Synopsis
126  
127 $bool = $row->exists($key);
128  
129 =head4 Description
130  
131 Liefere "wahr", wenn Datensatz-Attribut $key existiert,
132 anderfalls "falsch".
133  
134 =cut
135  
136 # -----------------------------------------------------------------------------
137  
138 5 ▶sub exists {
Subroutine exists: called CC=2 SCAR=6.9
Pod documented
139 5 my ($self,$key) = @_;
140 5 ▶ return exists $self->[0]->{$key}? 1: 0;
Branch: 1 branch
BranchTrueFalseTotal
exists $self->[0]{$key} ? : 3 2 5
141 }
142  
143 # -----------------------------------------------------------------------------
144  
145 =head3 get() - Liefere Datensatz-Attributwerte
146  
147 =head4 Synopsis
148  
149 $val = $row->get($key);
150 @vals = $row->get(@keys);
151  
152 =head4 Description
153  
154 Liefere die Datensatz-Attributwerte zu den angegebenen
155 Schlüsseln. In skalarem Kontext liefere keine Liste, sondern den
156 Wert des ersten Schlüssels.
157  
158 Ein Datensatz-Wert kann der Wert einer Kolumne oder das Ergebnis
159 einer Berechnung sein.
160  
161 =cut
162  
163 # -----------------------------------------------------------------------------
164  
165 3 ▶sub get {
Subroutine get: called CC=3 SCAR=11.0
Pod documented
166 3 my $self = shift;
167  
168 3 my @vals;
169 3 ▶ while (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 7 3 10
170 7 my $key = shift;
171 7 push @vals,$self->$key;
172 }
173  
174 3 ▶ return wantarray? @vals: $vals[0];
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 2 1 3
175 }
176  
177 # -----------------------------------------------------------------------------
178  
179 =head3 try() - Liefere Wert oder undef
180  
181 =head4 Synopsis
182  
183 $val = $row->try($key);
184  
185 =head4 Description
186  
187 Liefere den Wert des Attributs I<$key>, falls es existiert,
188 sonst C<undef>.
189  
190 =cut
191  
192 # -----------------------------------------------------------------------------
193  
194 2 ▶sub try {
Subroutine try: called CC=2 SCAR=6.9
Pod documented
195 2 my ($self,$key) = @_;
196 2 ▶ return exists $self->[0]->{$key}? $self->$key: undef;
Branch: 1 branch
BranchTrueFalseTotal
exists $self->[0]{$key} ? : 1 1 2
197 }
198  
199 # -----------------------------------------------------------------------------
200  
201 =head3 set() - Setze Datensatz-Attribute
202  
203 =head4 Synopsis
204  
205 $row->set(@keyVal);
206  
207 =head4 Description
208  
209 Setze die angegebenen Datensatz-Attribute. Es wird eine Exception
210 ausgelöst, wenn ein Attribut zu setzen versucht wird, das nicht existiert.
211 Die Methode liefert keinen Wert zurück.
212  
213 =cut
214  
215 # -----------------------------------------------------------------------------
216  
217 5 ▶sub set {
Subroutine set: called CC=2 SCAR=6.9
Pod documented
218 5 my $self = shift;
219 # @_: @keyVal
220  
221 5 ▶ while (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 5 4 9
222 5 my $key = shift;
223 5 $self->$key(shift);
224 }
225  
226 4 return;
227 }
228  
229 # -----------------------------------------------------------------------------
230  
231 =head3 init() - Initialisieren Datensatz-Attribute aus Objekt
232  
233 =head4 Synopsis
234  
235 $row->init($obj);
236  
237 =head4 Description
238  
239 Setze die Datensatz-Komunen in $row auf die Werte der Attribute in $obj.
240 Eine Kolumne wird nur gesetzt, wenn $obj für sie einen Wert hat,
241 d.h. das Attribut muss existieren und es muss einen von '' und
242 undef verschiedenen Wert haben.
243  
244 Die Methode liefert keinen Wert zurück.
245  
246 =cut
247  
248 # -----------------------------------------------------------------------------
249  
250 1 ▶sub init {
Subroutine init: called CC=5 SCAR=17.2
Pod documented
251 1 my ($self,$obj) = @_;
252  
253 1 for my $key ($self->titles) {
254 3 ▶ if ($obj->exists($key)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($obj->exists($key)) 3 0 3
255 3 my $val = $obj->get($key);
256 3 ▶ if (defined($val) && $val ne '') {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $val and $val ne "") 3 0 3
Condition33%1/3
defined $val and $val ne ""
!ll&&!rl&&r
003
MC/DC0%0/2
defined $val and $val ne ""
defined $val $val ne ""
Truth table33%1/3
A and B
A= defined $valB= $val ne ""
ABresult
0X0
100
111
257 3 $self->$key($val);
258 }
259 }
260 }
261  
262 1 return;
263 }
264  
265 # -----------------------------------------------------------------------------
266  
267 =head3 initFromCgi() - Initialisieren Datensatz-Attribute aus CGI-Objekt
268  
269 =head4 Synopsis
270  
271 $row->initFromCgi($cgi);
272  
273 =head4 Description
274  
275 Wie init(), nur dass ein CGI-Objekt per Methode param() befragt wird.
276  
277 =cut
278  
279 # -----------------------------------------------------------------------------
280  
281 0 ▶sub initFromCgi {
Subroutine initFromCgi: not called CC=3 SCAR=24.8
Pod documented
282 0 my ($self,$obj) = @_;
283  
284 0 for my $key ($self->titles) {
285 0 my $val = $obj->param($key);
286 0 ▶ if (defined $val) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $val) 0 0 0
287 0 $self->$key($val);
288 }
289 }
290  
291 0 return;
292 }
293  
294 # -----------------------------------------------------------------------------
295  
296 =head3 addAttribute() - Füge Datensatz-Attribute hinzu
297  
298 =head4 Synopsis
299  
300 $row->addAttribute(@keys);
301  
302 =head4 Description
303  
304 Füge die Attribute @keys zum Datensatz hinzu, sofern noch nicht existent.
305 Die Methode liefert keinen Wert zurück.
306  
307 =cut
308  
309 # -----------------------------------------------------------------------------
310  
311 1 ▶sub addAttribute {
Subroutine addAttribute: called CC=3 SCAR=11.0
Pod documented
312 1 my $self = shift;
313 # @_: @keyVal
314  
315 1 ▶ while (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 2 1 3
316 2 my $key = shift;
317 # erzeuge DS-Attribut, wenn es nicht existiert
318 2 ▶ if (!exists $self->[0]->{$key}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $self->[0]{$key}) 2 0 2
319 2 $self->[0]->{$key} = '';
320 }
321 }
322  
323 1 return;
324 }
325  
326 # -----------------------------------------------------------------------------
327  
328 =head3 removeColumn() - Entferne Datensatz-Kolumne(n)
329  
330 =head4 Synopsis
331  
332 $row->removeColumn(@keys);
333  
334 =head4 Description
335  
336 Entferne die Kolumnen @keys aus dem Datensatz. Die Methode liefert
337 keinen Wert zurück.
338  
339 =cut
340  
341 # -----------------------------------------------------------------------------
342  
343 0 ▶sub removeColumn {
Subroutine removeColumn: not called CC=4 SCAR=30.0
Pod documented
344 0 my $self = shift;
345 # @_: @keyVal
346  
347 0 my $valueH = $self->[0];
348 0 my $titleA = $self->[1];
349 0 ▶ while (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 0 0 0
350 0 my $key = shift;
351 0 CORE::delete $valueH->{$key};
352 0 ▶ for (my $i = 0; $i < @$titleA; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @$titleA) 0 0 0
353 0 ▶ if ($titleA->[$i] eq $key) {
Branch: 1 branch
BranchTrueFalseTotal
if ($titleA->[$i] eq $key) 0 0 0
354 0 CORE::splice @$titleA,$i,1;
355 0 last;
356 }
357 }
358 }
359  
360 0 return;
361 }
362  
363 # -----------------------------------------------------------------------------
364  
365 =head3 add() - Setze Datensatz-Attribute forciert
366  
367 =head4 Synopsis
368  
369 $row->add(@keyVal);
370  
371 =head4 Alias
372  
373 setValue()
374  
375 =head4 Description
376  
377 Forciere das Setzen der Datensatz-Attribute @keyVal, d.h. erzeuge
378 ein Datensatz-Attribut, falls es nicht existiert.
379 Die Methode liefert keinen Wert zurück.
380  
381 =cut
382  
383 # -----------------------------------------------------------------------------
384  
385 2 ▶sub add {
Subroutine add: called CC=3 SCAR=11.0
Pod documented
386 2 my $self = shift;
387 # @_: @keyVal
388  
389 2 ▶ while (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 2 2 4
390 2 my $key = shift;
391 # erzeuge DS-Attribut, wenn es nicht existiert
392 2 ▶ if (!exists $self->[0]->{$key}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $self->[0]{$key}) 2 0 2
393 2 $self->[0]->{$key} = '';
394 }
395 2 $self->$key(shift);
396 }
397  
398 2 return;
399 }
400  
401 {
402 10 ▶ no warnings 'once';
Subroutine BEGIN: called CC=1 SCAR=0.0
403 10 *setValue = \&add;
404 }
405  
406 # -----------------------------------------------------------------------------
407  
408 =head3 memoize() - Füge Datensatz-Attribute mit berechnetem Wert hinzu
409  
410 =head4 Synopsis
411  
412 $val = $row->memoize($key,$sub);
413  
414 =head4 Description
415  
416 Existiert das Datensatz-Attribut $key, liefere seinen Wert.
417 Andernfalls berechne dessen Wert mittels der anonymen Subroutine $sub
418 und speichere ihn auf dem Attribut.
419  
420 =cut
421  
422 # -----------------------------------------------------------------------------
423  
424 1 ▶sub memoize {
Subroutine memoize: called CC=2 SCAR=7.0
Pod documented
425 1 my ($self,$key,$sub) = @_;
426  
427 1 ▶ if (!$self->exists($key)) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->exists($key)) 1 0 1
428 1 my $val = $self->$sub($key);
429 1 $self->add($key=>$val);
430 }
431  
432 1 return $self->$key;
433 }
434  
435 # -----------------------------------------------------------------------------
436  
437 =head3 getSet() - Methode zum Liefern/Setzen eines einzelnen Datensatz-Attributs
438  
439 =head4 Synopsis
440  
441 $val = $row->getSet($key);
442 $val = $row->getSet($key,$val);
443  
444 =head4 Examples
445  
446 =over 2
447  
448 =item *
449  
450 Implementierung einer einfachen Attributmethode
451  
452 Dies setzt voraus, dass das Attribut vorhanden ist. Falls dies
453 nicht der Fall ist, kann es mit $row->add(xxx=>$val) eingeführt
454 werden. Diese Form der Attributmethode wird von selbst per
455 AUTOLOAD erzeugt, braucht also nicht implementiert werden
456  
457 sub xxx {
458 return shift->getSet(xxx=>@_);
459 }
460  
461 =item *
462  
463 Eine Attributmethode, die eine Liste oder eine Arrayreferenz liefert
464  
465 sub xxx {
466 my $self = shift;
467 # @_: $arr
468 my $arr = $self->getSet(xxx=>@_);
469 return wantarray? @$arr: $arr;
470 }
471  
472 =back
473  
474 =cut
475  
476 # -----------------------------------------------------------------------------
477  
478 179 ▶sub getSet {
Subroutine getSet: called CC=9 SCAR=22.0
Pod documented
479 179 my $self = shift;
480 179 my $key = shift;
481 # @_: $val
482  
483 179 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 19 160 179
484 19 my $val = shift;
485 19 ▶ $val = '' if !defined $val;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $val 1 18 19
486  
487 19 my $oldVal = $self->[0]->{$key};
488 19 ▶ if (!$oldVal && !exists $self->[0]->{$key}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($oldVal or exists $self->[0]{$key}) 1 18 19
Condition100%3/3
$oldVal or exists $self->[0]{$key}
l!l&&r!l&&!r
1261
MC/DC100%2/2
$oldVal or exists $self->[0]{$key}
$oldVal exists $self->[0]{$key}
Truth table100%3/3
A or B
A= $oldValB= exists $self->[0]{$key}
ABresult
1X1
011
000
489 1 $self->throw(
490 'ROW-00004: Datensatz-Attribut existiert nicht',
491 Key => $key,
492 );
493 }
494  
495 18 ▶ if ($val ne $oldVal) {
Branch: 1 branch
BranchTrueFalseTotal
if ($val ne $oldVal) 17 1 18
496 # Daten-Hash
497 17 $self->[0]->{$key} = $val;
498  
499 # Datensatz-Status setzen. Dieser ändert sich genau dann,
500 # wenn der Datensatz-Status 0 ist und ein Kolumnen-Attribut
501 # geändert wird. Das Ändern eines Nicht-Datenbank-Attributs
502 # ändert den Datensatz-Status nicht!
503 # FIXME: von grep auf effizientere Suche umstellen?
504  
505 17 ▶ if (!$self->[2] && grep {$key eq $_} @{$self->[1]}) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $self->[2] and grep {$key eq $_;} @{$$self[1];}) 1 16 17
Condition66%2/3
not $self->[2] and grep {$key eq $_;} @{$$self[1];}
!ll&&!rl&&r
1601
MC/DC50%1/2
not $self->[2] and grep {$key eq $_;} @{$$self[1];}
not $self->[2] grep {$key eq $_;} @{$$self[1];}
Truth table66%2/3
not(A) and B
A= not $self->[2]B= grep {$key eq $_;} @{$$self[1];}
ABresult
0X0
100
111
506 1 $self->[2] = 'U';
507 }
508  
509 # Änderungs-Hash. Dieser umfasst alle Attribute, sowohl
510 # Datensatz-Attribute als auch zusätzliche Attribute.
511  
512 17 ▶ unless ($self->[3]) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->[3]) 9 8 17
513 9 $self->[3] = Quiq::Hash->new->unlockKeys;
514 }
515  
516 # warn "UPDATE $key: '$oldVal' => '$val'\n";
517 17 $self->[3]->{$key} = $oldVal;
518 }
519 }
520  
521 178 return $self->[0]->{$key};
522 }
523  
524 # -----------------------------------------------------------------------------
525  
526 =head3 rowStatus() - Liefere/Setze Datensatzstatus
527  
528 =head4 Synopsis
529  
530 $rowStatus = $row->rowStatus;
531 $rowStatus = $row->rowStatus($rowStatus);
532  
533 =head4 Description
534  
535 Liefere/Setze den Status des Datensatzes. Der Status beschreibt
536 den Änderungsstand des Datensatzes hinsichtlich seiner Kolumnenwerte.
537  
538 Folgende Status sind definiert:
539  
540 =over 4
541  
542 =item '0' (unverändert)
543  
544 =back
545  
546 Der Datensatz wurde von der Datenbank selektiert und nicht modifiziert.
547  
548 =over 4
549  
550 =item 'U' (modifiziert)
551  
552 =back
553  
554 Der Datensatz wurde von der Datenbank selektiert und durch eine der
555 Attributmethoden modifiziert.
556  
557 =over 4
558  
559 =item 'I' (neu)
560  
561 =back
562  
563 Der Datensatz wurde durch new() erzeugt. Er existiert auf der
564 Datenbank nicht.
565  
566 =over 4
567  
568 =item 'D' (zu löschen)
569  
570 =back
571  
572 Der Datensatz wurde zum Löschen markiert. Dies geschah durch Aufruf
573 von $row->rowStatus('D'). Mit dem nächsen Aufruf von $row->save($db);
574 wird die Löschoperation auf der Datenbank ausgeführt.
575  
576 =cut
577  
578 # -----------------------------------------------------------------------------
579  
580 35 ▶sub rowStatus {
Subroutine rowStatus: called CC=3 SCAR=11.0
Pod documented
581 35 my $self = shift;
582 # @_: $rowStatus
583  
584 35 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 31 4 35
585 31 my $rowStatus = shift;
586 31 ▶ if ($rowStatus !~ /^[0IUD]$/) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($rowStatus =~ /^[0IUD]$/) 1 30 31
587 1 $self->throw(
588 'ROW-00005: Ungültiger Datensatz-Status',
589 RowStatus => $rowStatus,
590 );
591 }
592 30 $self->[2] = $rowStatus;
593 }
594  
595 34 return $self->[2];
596 }
597  
598 # -----------------------------------------------------------------------------
599  
600 =head3 titles() - Liefere Kolumnentitel
601  
602 =head4 Synopsis
603  
604 $titleA|@titles = $row->titles; # [1]
605 $titleA|@titles = $class->titles($db); # [2]
606  
607 =head4 Description
608  
609 =over 4
610  
611 =item 1.
612  
613 Liefere die Liste der Kolumentitel des Datensatzes, entweder
614 als Referenz (Skalarkontext) oder als Array (Listkontext).
615  
616 =item 2.
617  
618 Liefere die Liste der Kolumnentitel der Datensatz-Klasse.
619  
620 =back
621  
622 =cut
623  
624 # -----------------------------------------------------------------------------
625  
626 27 ▶sub titles {
Subroutine titles: called CC=3 SCAR=11.0
Pod documented
627 27 my $this = shift;
628 # @_: $db bei Aufruf als Klassenmethode
629  
630 27 ▶ if (ref $this) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $this) 26 1 27
631 # Aufruf als Objektmethode
632 26 ▶ return wantarray? @{$this->[1]}: $this->[1];
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 1 25 26
633 }
634  
635 # Aufruf als Klassenmethode
636  
637 1 my $db = shift;
638 1 my $stmt = $this->selectStmt($db);
639 1 return $db->titles(-stmt=>$stmt);
640 }
641  
642 # -----------------------------------------------------------------------------
643  
644 =head3 isModified() - Prüfe, ob Kolumne modifiziert wurde
645  
646 =head4 Synopsis
647  
648 $bool = $row->isModified($title);
649  
650 =head4 Description
651  
652 Liefere 1, wenn die Kolumnen $title modifiziert wurde, andernfalls 0.
653  
654 =cut
655  
656 # -----------------------------------------------------------------------------
657  
658 0 ▶sub isModified {
Subroutine isModified: not called CC=2 SCAR=17.9
Pod documented
659 0 my ($self,$key) = @_;
660 0 ▶ return CORE::exists $self->[3]->{$key}? 1: 0;
Branch: 1 branch
BranchTrueFalseTotal
exists $self->[3]{$key} ? : 0 0 0
661 }
662  
663 # -----------------------------------------------------------------------------
664  
665 =head3 modifiedColumns() - Liefere die Liste der geänderten Kolumnen
666  
667 =head4 Synopsis
668  
669 @keys|$keyA = $row->modifiedColumns;
670 @pairs|$pairA = $row->modifiedColumns(-withValue=>1);
671  
672 =head4 Options
673  
674 =over 4
675  
676 =item -columns => @colSpec (Default: alle Kolumnen)
677  
678 Einschränkung auf die angegebenen Kolumnen. Ist als Kolumnenname
679 eine Arrayreferenz
680  
681 [$key=>$retKey]
682  
683 angegeben, wird Kolumne $key geprüft, aber $retKey als Kolumnenname
684 geliefert. Dies ist bei View-Datensätzen nützlich, wenn $row ein
685 View-Datensatz ist, aber ein Tabellen-Datensatz manipuliert
686 werden soll und die Kolumnennamen differieren.
687  
688 =item -withValue => $bool (Default: 0)
689  
690 Liefere nicht nur die Kolumnennamen, sondern auch deren Wert.
691 Die Methode liefert in dem Fall die Datensatz-Änderungen
692 als Schlüssel/Wert-Paare.
693  
694 =back
695  
696 =head4 Example
697  
698 Generiere eine SET-Klausel für ein UPDATE-Statement aus einem
699 View-Datensatz, dessen Kolumnen teilweise anders beannt sind,
700 als die der zu aktualierenden Tabelle:
701  
702 @setClause = $row->modifiedColumns(
703 -columns=>[
704 lieferantid,
705 [lieferantenartikelnr=>'liefernr'],
706 [ekpreis=>'preis_ek'],
707 ],
708 -widthValues=>1,
709 );
710  
711 =cut
712  
713 # -----------------------------------------------------------------------------
714  
715 0 ▶sub modifiedColumns {
Subroutine modifiedColumns: not called CC=7 SCAR=40.3
Pod documented
716 0 my $self = shift;
717 # @_: @opt
718  
719 0 my $columns = $self->[1];
720 0 my $modHash = $self->[3];
721 0 my $datHash = $self->[0];
722 0 my $withValue = 0;
723  
724 0 Quiq::Option->extract(\@_,
725 -columns => \$columns,
726 -withValue => \$withValue,
727 );
728  
729 0 my @arr;
730  
731 0 for (@$columns) {
732 0 (my $key) = (my $retKey) = $_;
733 0 ▶ if (ref) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $_) 0 0 0
734 0 ($key,$retKey) = @$_;
735 }
736 0 ▶ if (!exists $self->[0]->{$key}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $self->[0]{$key}) 0 0 0
737 0 $self->throw(
738 'ROW-00001: Datensatz-Attribut existiert nicht',
739 Key => $key,
740 );
741 }
742 0 ▶ if (CORE::exists $modHash->{$key}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $modHash->{$key}) 0 0 0
743 0 push @arr,$retKey;
744 0 ▶ if ($withValue) {
Branch: 1 branch
BranchTrueFalseTotal
if ($withValue) 0 0 0
745 0 push @arr,$datHash->{$key};
746 }
747 }
748 }
749  
750 0 ▶ return wantarray? @arr: \@arr;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 0 0
751 }
752  
753 # -----------------------------------------------------------------------------
754  
755 =head3 setClauseFromModifiedRow() - Liefere SET-Klausel über geänderten Kolumnen
756  
757 =head4 Synopsis
758  
759 @clauses|$clauseA = $row->setClauseFromModifiedRow(@columns);
760  
761 =head4 See Also
762  
763 modifiedColumns()
764  
765 =head4 Example
766  
767 Auf einen View-Datensatz wurden Werte geschrieben. Wir wissen
768 nicht, welche Information sich geändert hat. Die Methode
769 setClauseFromModifiedRow() liefert uns die SET-Klausel für ein UPDATE:
770  
771 @setClause = $row->setClauseFromModifiedRow(
772 [lieferantenid=>'lieferantid'],
773 [lieferantenartikelnr=>'liefernr'],
774 [ekpreis=>'preis_ek'],
775 [lieferantenid1=>'lieferantid_1'],
776 [lieferantenartikelnr1=>'liefernr_1'],
777 [ekpreis1=>'preis_ek_1'],
778 [lieferantenid2=>'lieferantid_2'],
779 [lieferantenartikelnr2=>'liefernr_2'],
780 [ekpreis2=>'preis_ek_2'],
781 );
782 $db->update('shopartikellieferanteninfo',
783 @setClause,
784 -where,artikelid => $artId,
785 );
786  
787 Wurde keine der Kolumnen geändert, liefert setClauseFromModifiedRow() eine leere
788 Liste und $db->update() ist eine Nulloperation.
789  
790 =cut
791  
792 # -----------------------------------------------------------------------------
793  
794 0 ▶sub setClauseFromModifiedRow {
Subroutine setClauseFromModifiedRow: not called CC=1 SCAR=6.9
Pod documented
795 0 my $self = shift;
796 # @_: @columns
797  
798 0 return $self->modifiedColumns(
799 -columns => \@_,
800 -withValue => 1,
801 );
802 }
803  
804 # -----------------------------------------------------------------------------
805  
806 =head3 absorbModifications() - Absorbiere Datensatz-Änderungen
807  
808 =head4 Synopsis
809  
810 $row->absorbModifications;
811  
812 =head4 Description
813  
814 Setze den Datensatz-Status auf 0 (unverändert) und lösche den
815 Änderungs-Hash. Nach Aufruf der Methode sind alle vorangegangenen
816 Änderungen am Datensatz nicht mehr feststellbar.
817  
818 Die Methode kann benutzt werden, um Datenkorrekturen, z.B. durch
819 normalizeNumber(), verschwinden zu lassen.
820  
821 =head4 See Also
822  
823 $tab->absorbModifications()
824  
825 =cut
826  
827 # -----------------------------------------------------------------------------
828  
829 0 ▶sub absorbModifications {
Subroutine absorbModifications: not called CC=1 SCAR=6.9
Pod documented
830 0 my $self = shift;
831  
832 0 $self->[2] = 0; # setze Datensatz-Status auf 0 (unverändert)
833 0 $self->[3] = undef; # entferne die Modifikationen
834  
835 0 return;
836 }
837  
838 # -----------------------------------------------------------------------------
839  
840 =head3 modificationInfo() - Liefere Information über Datensatz-Änderungen
841  
842 =head4 Synopsis
843  
844 $str = $row->modificationInfo;
845  
846 =cut
847  
848 # -----------------------------------------------------------------------------
849  
850 0 ▶sub modificationInfo {
Subroutine modificationInfo: not called CC=5 SCAR=34.0
Pod documented
851 0 my $self = shift;
852  
853 0 my $str = '';
854  
855 0 ▶ if ($self->[2]) { # Datensatz-Status
Branch: 1 branch
BranchTrueFalseTotal
if ($self->[2]) 0 0 0
856 0 my $datHash = $self->[0];
857 0 my $modHash = $self->[3];
858 0 for my $key (@{$self->[1]}) {
859 0 ▶ if (CORE::exists $modHash->{$key}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $modHash->{$key}) 0 0 0
860 0 ▶ if ($str) {
Branch: 1 branch
BranchTrueFalseTotal
if ($str) 0 0 0
861 0 $str .= ', ';
862 }
863 0 $str .= "$key: '$modHash->{$key}' => '$datHash->{$key}'";
864 }
865 }
866 }
867  
868 0 return $str;
869 }
870  
871 # -----------------------------------------------------------------------------
872  
873 =head2 Eltern-Datensätze
874  
875 =head3 parentExists() - Prüfe, ob Eltern-Datensatz existiert
876  
877 =head4 Synopsis
878  
879 $row = $row->parentExists($type);
880  
881 =head4 Description
882  
883 Prüfe, ob ein Eltern-Datensätze vom Typ $type existiert.
884 Falls ja, liefere I<wahr>, andernfalls I<falsch>.
885  
886 =cut
887  
888 # -----------------------------------------------------------------------------
889  
890 0 ▶sub parentExists {
Subroutine parentExists: not called CC=2 SCAR=17.9
Pod documented
891 0 my ($self,$type) = @_;
892 0 ▶ return CORE::exists $self->[5]->{$type}? 1: 0;
Branch: 1 branch
BranchTrueFalseTotal
exists $self->[5]{$type} ? : 0 0 0
893 }
894  
895 # -----------------------------------------------------------------------------
896  
897 =head3 addParent() - Füge Eltern-Datensatz hinzu
898  
899 =head4 Synopsis
900  
901 $row->addParent($type,$parentRow);
902  
903 =head4 Description
904  
905 Füge Datensatz $parentRow als Eltern-Datensatz vom Typ $type hinzu.
906 Die Referenz wird als schwache Referenz gekennzeichnet.
907  
908 =cut
909  
910 # -----------------------------------------------------------------------------
911  
912 3 ▶sub addParent {
Subroutine addParent: called CC=1 SCAR=0.0
Pod documented
913 3 my ($self,$type,$parentRow) = @_;
914 3 $self->[5]->{$type} = $parentRow;
915 3 Scalar::Util::weaken($self->[5]->{$type});
916 3 return;
917 }
918  
919 # -----------------------------------------------------------------------------
920  
921 =head3 getParent() - Liefere Eltern-Datensatz
922  
923 =head4 Synopsis
924  
925 $parentRow = $row->getParent($type);
926  
927 =head4 Description
928  
929 Liefere den Eltern-Datensatz vom Typ $type. Existiert keine
930 Elterndatensatz vom Typ $type, liefere undef.
931  
932 =cut
933  
934 # -----------------------------------------------------------------------------
935  
936 35 ▶sub getParent {
Subroutine getParent: called CC=1 SCAR=0.0
Pod documented
937 35 my ($self,$type) = @_;
938 35 return $self->[5]->{$type};
939 }
940  
941 # -----------------------------------------------------------------------------
942  
943 =head2 Kind-Datensätze
944  
945 =head3 childTypeExists() - Prüfe, ob Kind-Datensatz-Typ existiert
946  
947 =head4 Synopsis
948  
949 $bool = $row->childTypeExists($type);
950  
951 =head4 Description
952  
953 Prüfe, ob Kind-Datensätze des Typs $type zum Datensatz hinzugefügt
954 werden können. Falls ja, liefere I<wahr>, andernfalls I<falsch>.
955  
956 =cut
957  
958 # -----------------------------------------------------------------------------
959  
960 0 ▶sub childTypeExists {
Subroutine childTypeExists: not called CC=2 SCAR=17.9
Pod documented
961 0 my ($self,$type) = @_;
962 0 ▶ return CORE::exists $self->[4]->{$type}? 1: 0;
Branch: 1 branch
BranchTrueFalseTotal
exists $self->[4]{$type} ? : 0 0 0
963 }
964  
965 # -----------------------------------------------------------------------------
966  
967 =head3 addChildType() - Füge Kind-Datensatz-Typ hinzu
968  
969 =head4 Synopsis
970  
971 $tab = $row->addChildType($type,$rowClass,\@titles);
972  
973 =head4 Description
974  
975 Bevor Kind-Datensätze einem Datenstatz zugeordnet werden können, muss
976 ein entsprechendes ResultSet-Objekt hinzugefügt werden. Dieses wird
977 per $type angesprochen. Z.B. liefert
978  
979 $tab = $row->getChilds($type);
980  
981 die Menge der zugeordenten Kind-Objekte vom Typ $type.
982  
983 =cut
984  
985 # -----------------------------------------------------------------------------
986  
987 4 ▶sub addChildType {
Subroutine addChildType: called CC=1 SCAR=0.0
Pod documented
988 4 my ($self,$type,$rowClass,$titleA) = @_;
989 4 return $self->[4]->{$type} =
990 Quiq::Database::ResultSet->new($rowClass,$titleA);
991 }
992  
993 # -----------------------------------------------------------------------------
994  
995 =head3 addChild() - Füge Kind-Datensatz hinzu
996  
997 =head4 Synopsis
998  
999 $row->addChild($type,$childRow);
1000  
1001 =head4 Description
1002  
1003 Füge Datensatz $childRow als Kinddatensatz vom Typ $type hinzu.
1004  
1005 =cut
1006  
1007 # -----------------------------------------------------------------------------
1008  
1009 3 ▶sub addChild {
Subroutine addChild: called CC=1 SCAR=0.0
Pod documented
1010 3 my ($self,$type,$childRow) = @_;
1011 3 $self->[4]->{$type}->push($childRow);
1012 3 return;
1013 }
1014  
1015 # -----------------------------------------------------------------------------
1016  
1017 =head3 getChilds() - Liefere Kind-Datensätze
1018  
1019 =head4 Synopsis
1020  
1021 @rows|$rowT = $row->getChilds($type);
1022  
1023 =head4 Description
1024  
1025 Liefere die Kind-Datensätze vom Typ $type.
1026  
1027 =cut
1028  
1029 # -----------------------------------------------------------------------------
1030  
1031 18 ▶sub getChilds {
Subroutine getChilds: called CC=3 SCAR=11.7
Pod documented
1032 18 my ($self,$type) = @_;
1033 18 ▶ my $tab = $self->[4]->{$type} || die;
Condition50%1/2
$self->[4]{$type} || die()
l!l
180
MC/DC0%0/1
$self->[4]{$type} || die()
$self->[4]{$type}
Truth table50%1/2
A
A= $self->[4]{$type}
Aresult
11
00
1034 18 ▶ return wantarray? $tab->rows: $tab;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 18 0 18
1035 }
1036  
1037 # -----------------------------------------------------------------------------
1038  
1039 =head2 Subklassen-Methoden
1040  
1041 Die folgenden Methoden implementieren die abstrakten Methoden der
1042 Basisklasse Quiq::Database::Row. Die Methoden hat die Klasse mit
1043 der Klasse Quiq::Database::Row::Array gemeinsam.
1044  
1045 =head3 asArray() - Liefere Datensatz als Array
1046  
1047 =head4 Synopsis
1048  
1049 $arr|@arr = $row->asArray;
1050  
1051 =head4 Description
1052  
1053 Liefere den Datensatz als Array, entweder in Form einer Referenz
1054 (Skalarkontext) oder als Array von Werten (Listkontext).
1055  
1056 =cut
1057  
1058 # -----------------------------------------------------------------------------
1059  
1060 23 ▶sub asArray {
Subroutine asArray: called CC=3 SCAR=11.0
Pod documented
1061 23 my $self = shift;
1062  
1063 23 my @arr;
1064 23 my $titles = $self->titles;
1065 23 for my $key (@$titles) {
1066 72 push @arr,$self->$key;
1067 }
1068  
1069 23 ▶ return wantarray? @arr: \@arr;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 1 22 23
1070 }
1071  
1072 # -----------------------------------------------------------------------------
1073  
1074 =head3 asString() - Liefere Datensatz als Zeichenkette
1075  
1076 =head4 Synopsis
1077  
1078 $str = $row->asString;
1079 $str = $row->asString($colSep);
1080  
1081 =head4 Description
1082  
1083 Liefere den Datensatz als Zeichenkette. Per Default werden die Kolumnen
1084 per TAB getrennt. Der Trenner kann mittels $colSep explizit angegeben
1085 werden.
1086  
1087 =cut
1088  
1089 # -----------------------------------------------------------------------------
1090  
1091 0 ▶sub asString {
Subroutine asString: not called CC=3 SCAR=24.8
Pod documented
1092 0 my $self = shift;
1093 0 ▶ my $colSep = @_? shift: "\t";
Branch: 1 branch
BranchTrueFalseTotal
@_ ? : 0 0 0
1094  
1095 0 my @arr;
1096 0 for my $key (@{$self->titles}) {
1097 0 push @arr,$self->$key;
1098 }
1099  
1100 0 return join $colSep,@arr;
1101 }
1102  
1103 # -----------------------------------------------------------------------------
1104  
1105 =head3 copy() - Kopiere Datensatz
1106  
1107 =head4 Synopsis
1108  
1109 $newRow = $row->copy;
1110  
1111 =head4 Description
1112  
1113 Erstelle eine Kopie des Datensatzes $row und liefere eine Referenz
1114 auf die Kopie zurück.
1115  
1116 Die Kopie ist identisch zum Original, bis darauf, dass der
1117 Daten-Hash und der Änderungs-Hash kopiert werden:
1118  
1119 Daten-Hash.........................: kopiert
1120 Referenz auf Titel-Liste...........: identisch
1121 Datensatz-Status...................: identisch
1122 Änderungs-Hash.....................: kopiert
1123 Referenz auf Kind-Datensätze-Hash..: identisch
1124 Referenz auf Eltern-Datensätze-Hash: identisch
1125  
1126 =cut
1127  
1128 # -----------------------------------------------------------------------------
1129  
1130 1 ▶sub copy {
Subroutine copy: called CC=2 SCAR=7.0
Pod documented
1131 1 my $self = shift;
1132  
1133 1 my @row = @$self; # Array kopieren
1134 1 $row[0] = $self->[0]->copy; # Daten-Hash kopieren
1135 1 ▶ if ($self->[3]) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->[3]) 1 0 1
1136 1 $row[3] = $self->[3]->copy; # Änderungs-Hash kopieren
1137 }
1138  
1139 1 return bless \@row,ref($self);
1140 }
1141  
1142 # -----------------------------------------------------------------------------
1143  
1144 =head3 isRaw() - Liefere, ob Klasse Raw-Datensätze repräsentiert
1145  
1146 =head4 Synopsis
1147  
1148 $bool = $row->isRaw;
1149  
1150 =cut
1151  
1152 # -----------------------------------------------------------------------------
1153  
1154 2 ▶sub isRaw {
Subroutine isRaw: called CC=1 SCAR=0.0
Pod documented
1155 2 return 0;
1156 }
1157  
1158 # -----------------------------------------------------------------------------
1159  
1160 =head2 Sonstiges
1161  
1162 =head3 asRecord() - Liefere Datensatz in Record-Darstellung
1163  
1164 =head4 Synopsis
1165  
1166 $str = $row->asRecord(@opt);
1167  
1168 =head4 Options
1169  
1170 =over 4
1171  
1172 =item -color => $a
1173  
1174 AnsiColor-Objekt.
1175  
1176 =item -indent => $n (Default: 4)
1177  
1178 Tiefe der Einrückung der Werte.
1179  
1180 =item -null => $str (Default: 'NULL')
1181  
1182 Gibt an, welcher Wert für einen Nullwert ausgegeben wird. Ist -null undef,
1183 wird das Attribut nicht ausgegeben (weder Name noch Wert).
1184 Ist $null ein Leerstring (''), wird nur der Wert nicht ausgegeben.
1185  
1186 =back
1187  
1188 =head4 Description
1189  
1190 Liefere den Datensatz in mehrzeiliger Record-Darstellung.
1191 Die Darstellung hat den Aufbau:
1192  
1193 <key1>:
1194 <val1>
1195 <key2>:
1196 <val2>
1197 ...
1198  
1199 =cut
1200  
1201 # -----------------------------------------------------------------------------
1202  
1203 0 ▶sub asRecord {
Subroutine asRecord: not called CC=6 SCAR=37.4
Pod documented
1204 0 my $self = shift;
1205  
1206 # Optionen und Argumente
1207  
1208 0 my $color = undef;
1209 0 my $indent = 4;
1210 0 my $null = 'NULL';
1211  
1212 0 my $opt = $self->parameters(\@_,
1213 -color => \$color,
1214 -indent => \$indent,
1215 -null => \$null,
1216 );
1217  
1218 0 ▶ my $a = $color // Quiq::AnsiColor->new(1);
Condition0%0/3
$color // "Quiq::AnsiColor"->new(1)
l!l&&r!l&&!r
000
MC/DC0%0/2
$color // "Quiq::AnsiColor"->new(1)
$color "Quiq::AnsiColor"->new(1)
Truth table0%0/3
A // B
A= $colorB= "Quiq::AnsiColor"->new(1)
ABresult
1X1
011
000
1219 0 $indent = ' ' x $indent;
1220  
1221 0 my $str = '';
1222 0 for my $key (@{$self->titles}) {
1223 0 my $val = $self->$key;
1224 0 ▶ if ($val eq '') {
Branch: 1 branch
BranchTrueFalseTotal
if ($val eq "") 0 0 0
1225 0 ▶ if (!defined $null) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $null) 0 0 0
1226 0 next;
1227 }
1228 0 $val = $null;
1229 }
1230 0 $str .= $a->str('bold',"$key:")."\n";
1231 0 ▶ $val = $a->str($val eq $null? 'red': 'green',$val);
Branch: 1 branch
BranchTrueFalseTotal
$val eq $null ? : 0 0 0
1232 0 $val =~ s/^/$indent/mg;
1233 0 $val .= "\n";
1234 0 $str .= $val;
1235 }
1236  
1237 0 return $str;
1238 }
1239  
1240 # -----------------------------------------------------------------------------
1241  
1242 =head3 copyData() - Kopiere Attributwerte von Datensatz zu Datensatz
1243  
1244 =head4 Synopsis
1245  
1246 $row->copyData($row0,@opt);
1247  
1248 =head4 Options
1249  
1250 =over 4
1251  
1252 =item -ignore => \@keys (Default: [])
1253  
1254 Übergehe die Attribute @keys, d.h. kopiere die Werte dieser
1255 Attribute nicht.
1256  
1257 =item -dontCopyNull => $bool (Default: 0)
1258  
1259 Kopiere keine Nullwerte, d.h. im Falle eines Nullwerts in
1260 Datensatz $row0 bleibt der Attributwert in $row erhalten.
1261 Mögliche Erweiterung: Liste von Kolumnennamen.
1262  
1263 =back
1264  
1265 =head4 Description
1266  
1267 Setze die Datensatz-Attribute in $row auf deren Werte in $row0.
1268 Attribute, die in $row0 nicht vorkommen, werden nicht gesetzt.
1269 Die Methode liefert keinen Wert zurück.
1270  
1271 Die Methode ist nützlich, wenn ein Datensatz auf der Datenbank
1272 aktualisiert werden soll und dessen neue Werte auf einem anderen
1273 Datensatz stehen.
1274  
1275 =cut
1276  
1277 # -----------------------------------------------------------------------------
1278  
1279 3 ▶sub copyData {
Subroutine copyData: called CC=9 SCAR=22.0
Pod documented
1280 3 my $self = shift;
1281 3 my $row0 = shift;
1282 # @_: @opt
1283  
1284 # Optionen
1285  
1286 3 my $ignore = undef;
1287 3 my $dontCopyNull = 0;
1288  
1289 3 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 2 1 3
1290 2 Quiq::Option->extract(\@_,
1291 -ignore => \$ignore,
1292 -dontCopyNull => \$dontCopyNull,
1293 );
1294 }
1295  
1296 3 ▶ if ($ignore) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ignore) 1 2 3
1297 1 $ignore = Quiq::Hash->new($ignore,1)->unlockKeys;
1298 }
1299  
1300 # Operation ausführen. Die Methode operiert allein auf den
1301 # Daten-Hashes der beteiligten Datensätze.
1302  
1303 3 for my $key (keys %{$self->[0]}) {
1304 12 ▶ next if $ignore && $ignore->{$key};
Branch: 1 branch
BranchTrueFalseTotal
if $ignore and $ignore->{$key} 2 10 12
Condition100%3/3
$ignore and $ignore->{$key}
!ll&&!rl&&r
822
MC/DC100%2/2
$ignore and $ignore->{$key}
$ignore $ignore->{$key}
Truth table100%3/3
A and B
A= $ignoreB= $ignore->{$key}
ABresult
0X0
100
111
1305 10 ▶ next if !exists $row0->[0]->{$key};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $row0->[0]{$key} 3 7 10
1306 7 my $val = $row0->[0]->{$key};
1307 7 ▶ next if $dontCopyNull && $val eq '';
Branch: 1 branch
BranchTrueFalseTotal
if $dontCopyNull and $val eq "" 2 5 7
Condition100%3/3
$dontCopyNull and $val eq ""
!ll&&!rl&&r
412
MC/DC100%2/2
$dontCopyNull and $val eq ""
$dontCopyNull $val eq ""
Truth table100%3/3
A and B
A= $dontCopyNullB= $val eq ""
ABresult
0X0
100
111
1308 5 $self->getSet($key,$val);
1309 }
1310  
1311 3 return;
1312 }
1313  
1314 # -----------------------------------------------------------------------------
1315  
1316 =head3 save() - Aktualisiere Datensatz auf Datenbank
1317  
1318 =head4 Synopsis
1319  
1320 $cur = $row->save($db);
1321  
1322 =head4 Description
1323  
1324 Aktualisiert den Datensatz $row gemäß seines Status auf der Datenbank
1325 $db und liefere das Resultat der Statement-Ausführung zurück.
1326  
1327 Welche Datenbankoperation konkret ausgeführt wird, ergibt sich aus
1328 dem Status des Datensatzes.
1329  
1330 B<Statuswerte>
1331  
1332 =over 4
1333  
1334 =item '0' (unverändert)
1335  
1336 Es wird keine Datenbankoperation ausgeführt.
1337  
1338 =item 'U' (modifiziert)
1339  
1340 Es wird eine Update-Operation auf der Datenbank ausgeführt, d.h. es
1341 wird die Methode $row->update() gerufen.
1342  
1343 =item 'I' (neu)
1344  
1345 Es wird eine Insert-Operation auf der Datenbank ausgeführt, d.h. es
1346 wird die Methode $row->insert() gerufen.
1347  
1348 =back
1349  
1350 ['D' (zu löschen)]
1351  
1352 Es wird eine Delete-Operation auf der Datenbank ausgeführt, d.h. es
1353 wird die Methode $row->delete() gerufen.
1354  
1355 =cut
1356  
1357 # -----------------------------------------------------------------------------
1358  
1359 0 ▶sub save {
Subroutine save: not called CC=5 SCAR=34.0
Pod documented
1360 0 my ($self,$db) = @_;
1361  
1362 0 my $cur;
1363 0 my $stat = $self->rowStatus;
1364 0 ▶ if (!$stat) { # Datensatz wurde selektiert und nicht geändert
Branch: 4 branches
BranchTrueFalseTotal
if (not $stat) { } 0 0 0
elsif ($stat eq 'I') { } 0 0 0
elsif ($stat eq 'U') { } 0 0 0
elsif ($stat eq 'D') { } 0 0 0
1365 0 $cur = $db->sql;
1366 }
1367 elsif ($stat eq 'I') { # Datensatz ist neu
1368 0 $cur = $self->insert($db);
1369 }
1370 elsif ($stat eq 'U') { # Datensatz wurde modifiziert
1371 0 $cur = $self->update($db);
1372 }
1373 elsif ($stat eq 'D') { # Datensatz wurde zum Löschen markiert
1374 0 $cur = $self->delete($db);
1375 }
1376 else {
1377 0 $self->throw(
1378 'ROW-00005: Ungültiger Datensatz-Status',
1379 RowStatus => $stat,
1380 );
1381 }
1382 0 $self->rowStatus(0); # NEU 2022-08-22
1383 0 $cur->{'rowOperation'} = $stat;
1384  
1385 0 return $cur;
1386 }
1387  
1388 # -----------------------------------------------------------------------------
1389  
1390 =head3 weaken() - Erzeuge schwache Referenz
1391  
1392 =head4 Synopsis
1393  
1394 $ref = $row->weaken($key);
1395 $ref = $row->weaken($key=>$ref);
1396  
1397 =head4 Description
1398  
1399 Mache die Referenz von Schlüssel $key zu einer schwachen Referenz
1400 und liefere sie zurück. Ist eine Referenz $ref als Parameter angegeben,
1401 setze die Referenz zuvor.
1402  
1403 =cut
1404  
1405 # -----------------------------------------------------------------------------
1406  
1407 0 ▶sub weaken {
Subroutine weaken: not called CC=1 SCAR=6.9
Pod documented
1408 0 return shift->[0]->weaken(@_);
1409 }
1410  
1411 # -----------------------------------------------------------------------------
1412  
1413 =head2 DML Statements
1414  
1415 =head3 select() - Liefere Datensätze der Klasse
1416  
1417 =head4 Synopsis
1418  
1419 $tab|@rows|$cur = $class->select($db,@select,@opt);
1420  
1421 =head4 Options
1422  
1423 =over 4
1424  
1425 =item -cursor => $bool (Default: 0)
1426  
1427 Liefere Cursor statt Liste der Datensätze.
1428  
1429 =item -tableClass => $tableClass (Default: undef)
1430  
1431 Name der Klasse, die die Ergebnismenge speichert.
1432  
1433 =back
1434  
1435 =cut
1436  
1437 # -----------------------------------------------------------------------------
1438  
1439 1 ▶sub select {
Subroutine select: called CC=1 SCAR=0.0
Pod documented
1440 1 my $class = shift;
1441 1 my $db = shift;
1442 # @_: @select,@opt
1443  
1444 # Optionen
1445  
1446 1 my $cursor = 0;
1447 1 my $tableClass;
1448  
1449 1 Quiq::Option->extract(-mode=>'sloppy',\@_,
1450 -cursor => \$cursor,
1451 -tableClass => \$tableClass,
1452 );
1453  
1454 # Operation ausführen
1455  
1456 1 my $stmt = $class->selectStmt($db,@_);
1457  
1458 1 return $db->select(
1459 -cursor => $cursor,
1460 -rowClass => $class,
1461 -stmt => $stmt,
1462 -tableClass => $tableClass,
1463 );
1464 }
1465  
1466 # -----------------------------------------------------------------------------
1467  
1468 =head3 lookup() - Liefere Datensatz der Klasse
1469  
1470 =head4 Synopsis
1471  
1472 $row|@vals = $class->lookup($db,@select,@opt);
1473  
1474 =head4 Options
1475  
1476 =over 4
1477  
1478 =item -sloppy => $bool (default: 0)
1479  
1480 Wirf keine Exception, wenn der Datensatz nicht existiert, sondern
1481 undef (Skalarkontext) bzw. eine leere Liste (Listkontext).
1482  
1483 =back
1484  
1485 =cut
1486  
1487 # -----------------------------------------------------------------------------
1488  
1489 0 ▶sub lookup {
Subroutine lookup: not called CC=1 SCAR=6.9
Pod documented
1490 0 my $class = shift;
1491 0 my $db = shift;
1492 # @_: @select,@opt
1493  
1494 # Optionen
1495  
1496 0 my $sloppy = 0;
1497  
1498 0 Quiq::Option->extract(-mode=>'sloppy',\@_,
1499 -sloppy => \$sloppy,
1500 );
1501  
1502 # Operation ausführen
1503  
1504 0 my $stmt = $class->selectStmt($db,@_);
1505  
1506 0 return $db->lookup(
1507 -stmt => $stmt,
1508 -rowClass => $class,
1509 -sloppy => $sloppy,
1510 );
1511 }
1512  
1513 # -----------------------------------------------------------------------------
1514  
1515 =head3 value() - Liefere Kolumnenwert
1516  
1517 =head4 Synopsis
1518  
1519 $val = $class->value($db,@select,@opt);
1520  
1521 =head4 Options
1522  
1523 Siehe $db->value().
1524  
1525 =cut
1526  
1527 # -----------------------------------------------------------------------------
1528  
1529 0 ▶sub value {
Subroutine value: not called CC=1 SCAR=6.9
Pod documented
1530 0 my $class = shift;
1531 0 my $db = shift;
1532 # @_: @select,@opt
1533  
1534 0 return $db->value($class->table,@_);
1535 }
1536  
1537 # -----------------------------------------------------------------------------
1538  
1539 =head2 Kompatibilität
1540  
1541 =head3 toSbit() - Generiere Sbit-Datensatz
1542  
1543 =head4 Synopsis
1544  
1545 $sbitRow = $row->toSbit($sbitClass);
1546  
1547 =head4 Arguments
1548  
1549 =over 4
1550  
1551 =item $sbitClass
1552  
1553 Datensatz-Klasse der Sbit-Klassenbibliothek
1554  
1555 =back
1556  
1557 =head4 Returns
1558  
1559 Referenz auf Sbit-Datensatz
1560  
1561 =head4 Description
1562  
1563 Generiere aus Datensatz I<$row> einen Sbit-Datensatz der Klasse
1564 I<$sbitClass> und liefere diesen zurück.
1565  
1566 =head4 Details
1567  
1568 Die Methode ist nützlich, wenn über die Klassenbibliothek
1569 selektiert, aber die weitere Verarbeitung über Klassen auf
1570 Basis der Sbit-Klassenbibliothek erfolgt.
1571  
1572 =cut
1573  
1574 # -----------------------------------------------------------------------------
1575  
1576 0 ▶sub toSbit {
Subroutine toSbit: not called CC=2 SCAR=17.9
Pod documented
1577 0 my ($self,$sbitClass) = @_;
1578  
1579 # Implementierung gemäß dem Konstruktor in der Sbit-Klassenbibliothek
1580  
1581 0 my $titles = $self->titles;
1582  
1583 0 my %hash;
1584 0 @hash{@$titles} = ('') x @$titles;
1585 0 for my $key (@$titles) {
1586 0 $hash{$key} = $self->$key;
1587 }
1588  
1589 0 return bless [\%hash,$titles,$self->rowStatus,0,undef,undef],$sbitClass;
1590 }
1591  
1592 # -----------------------------------------------------------------------------
1593  
1594 =head2 Autoload
1595  
1596 =head3 AUTOLOAD() - Generiere Attributmethode
1597  
1598 =head4 Synopsis
1599  
1600 $val = $row->AUTOLOAD;
1601 $val = $row->AUTOLOAD($val);
1602  
1603 =head4 Description
1604  
1605 Generiere Attributmethode, rufe diese auf und liefere den Attributwert.
1606  
1607 =cut
1608  
1609 # -----------------------------------------------------------------------------
1610  
1611 102 ▶sub AUTOLOAD {
Subroutine AUTOLOAD: called CC=5 SCAR=16.1
1612 102 my $this = shift;
1613 # @_: Methodenargumente
1614  
1615 102 my ($rowClass,$key) = our $AUTOLOAD =~ /^(.*)::(\w+)$/;
1616 102 ▶ if (!defined $key) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $key) 0 102 102
1617 0 $this->throw(
1618 'ROW-00003: Methodenname enthält ungültige Zeichen',
1619 Method => $AUTOLOAD,
1620 );
1621 }
1622 102 ▶ return if $key !~ /[^A-Z]/;
Branch: 1 branch
BranchTrueFalseTotal
unless $key =~ /[^A-Z]/ 74 28 102
1623  
1624 # Wir prüfen, dass eine Attributmethode nur einmal gerufen wird
1625 # warn "$AUTOLOAD\n";
1626  
1627 # Aufruf als Klassenmethode ist nicht vorgesehen
1628  
1629 28 ▶ if (!ref $this) {
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $this) 1 27 28
1630 1 $this->throw(
1631 'ROW-00001: Klassen-Methode existiert nicht',
1632 Attribute => $key,
1633 );
1634 }
1635  
1636 # Methode nur generieren, wenn Attribut existiert
1637  
1638 27 ▶ if (!exists $this->[0]->{$key}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $this->[0]{$key}) 3 24 27
1639 3 $this->throw(
1640 'ROW-00002: Datensatz-Attribut oder Methode existiert nicht',
1641 Attribute => $key,
1642 );
1643 }
1644  
1645 # Attribut-Methode generieren
1646  
1647 10 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
1648 24 ▶ *{$AUTOLOAD} = sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
1649 168 return shift->getSet($key,@_);
1650 24 };
1651  
1652 # Methode aufrufen
1653 24 return $this->$key(@_);
1654 }
1655  
1656 # -----------------------------------------------------------------------------
1657  
1658 =head1 VERSION
1659  
1660 1.240
1661  
1662 =head1 AUTHOR
1663  
1664 Frank Seitz, L<http://fseitz.de/>
1665  
1666 =head1 COPYRIGHT
1667  
1668 Copyright (C) 2026 Frank Seitz
1669  
1670 =head1 LICENSE
1671  
1672 This code is free software; you can redistribute it and/or modify
1673 it under the same terms as Perl itself.
1674  
1675 =cut
1676  
1677 # -----------------------------------------------------------------------------
1678  
1679 10 1;
1680  
1681 # eof
« Quiq/StreamServe/Block.pm ↑ Summary Quiq/File/Video.pm »