Quiq/Database/Row/Object/Table.pm

filter: Statementstmt 47.1% 42 / 89 Branchbran 40.0% 4 / 10 Conditioncond 33.3% 2 / 6 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 50.0% 10 / 20 Podpod 100% 12 / 12 totaltotal 49.6% 70 / 141 CC 11 SCAR 14.8
CC
11
Cov
46%
load
12.0
tableName
6.4
primaryKeyWhere
6.0
CRAP 4.4
« Quiq/Html/Table/Simple.pm ↑ Summary Quiq/Imap/Client.pm »
1 # -----------------------------------------------------------------------------
2  
3 =encoding utf8
4  
5 =head1 NAME
6  
7 Quiq::Database::Row::Object::Table - Datensatz einer Tabelle
8  
9 =head1 BASE CLASSES
10  
11 =over 2
12  
13 =item *
14  
15 L<Quiq::Database::Row::Object>
16  
17 =item *
18  
19 L<Quiq::ClassConfig>
20  
21 =back
22  
23 =head1 DESCRIPTION
24  
25 Ein Objekt der Klasse repräsentiert einen Datensatz, der aus einer
26 einzelnen Datenbank-Tabelle stammt.
27  
28 Die DML-Operationen (select, insert, update, delete) der Klasse
29 operieren auf der Tabelle. Entsprechend definiert die Klasse einen
30 Tabellennamen. Dieser wird von der Methode L<tableName|"tableName() - Liefere Namen der Datenbanktabelle">() geliefert.
31  
32 Über eine Tabelle wird vorausgesetzt, dass diese eine
33 Primärschlüssel-Kolumne besitzt. Deren Name wird von der Methode
34 L<primaryKey|"primaryKey() - Liefere Namen der Primärschlüssel-Kolumne">() geliefert.
35  
36 =cut
37  
38 # -----------------------------------------------------------------------------
39  
40 package Quiq::Database::Row::Object::Table;
41 2 ▶use base qw/Quiq::Database::Row::Object Quiq::ClassConfig/;
Subroutine BEGIN: called CC=1 SCAR=0.0
42  
43 2 ▶use v5.10;
Subroutine BEGIN: called CC=1 SCAR=0.0
44 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
45 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
46 2 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
47  
48 2 our $VERSION = '1.240';
49  
50 2 ▶use Quiq::Perl;
Subroutine BEGIN: called CC=1 SCAR=0.0
51  
52 # -----------------------------------------------------------------------------
53  
54 =head1 METHODS
55  
56 =head2 Meta-Information
57  
58 =head3 tableName() - Liefere Namen der Datenbanktabelle
59  
60 =head4 Synopsis
61  
62 $tableName = $this->tableName;
63  
64 =head4 Alias
65  
66 table()
67  
68 =head4 Returns
69  
70 Tabellenname (String)
71  
72 =head4 Description
73  
74 Bestimme den Namen der Datenbanktabelle, welche die Klasse kapselt,
75 und liefere diesen zurück.
76  
77 =head4 Example
78  
79 Tabellenname wird aus Klassenname abgeleitet:
80  
81 Adb::Table::Person => person
82 Adb::Person => person
83 Person => person
84  
85 Tabellenname per Klassenvariable definieren:
86  
87 our $TableName = 'adb.person';
88  
89 =head4 Details
90  
91 Der Tabellenname ist per Default die in Kleinschreibung gewandelte
92 letzte Komponente des Klassennamens.
93  
94 Abweichend vom Default kann die Datensatzklasse den Tabellennamen
95 über die Klassenvariable
96  
97 our $TableName = '...';
98  
99 festlegen.
100  
101 Die Methode kann in abgeleiteten Klassen überschrieben werden,
102 um den Tabellennamen auf andere Weise zu bestimmen, z.B. um einen
103 Schemapräfix aus einer weiteren Klassennamen-Komponente hinzuzufügen.
104  
105 =cut
106  
107 # -----------------------------------------------------------------------------
108  
109 2 my %cache;
110  
111 4 ▶sub tableName {
Subroutine tableName: called CC=6 SCAR=18.6
Pod documented
112 4 ▶ my $class = ref $_[0] || $_[0];
Condition66%2/3
ref $_[0] || $_[0]
l!l&&r!l&&!r
220
MC/DC0%0/2
ref $_[0] || $_[0]
ref $_[0] $_[0]
Truth table66%2/3
A || B
A= ref $_[0]B= $_[0]
ABresult
1X1
011
000
113  
114 # FXIME: auf Klasse ClassConfig umstellen (?)
115  
116 # state %cache;
117  
118 4 ▶ if (!$cache{$class}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($cache{$class}) 3 1 4
119 2 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
120 3 my $found = 0;
121 3 for ($class,Quiq::Perl->baseClassesISA($class)) {
122 18 my $ref = *{"$_\::TableName"}{SCALAR};
123 18 ▶ if ($$ref) {
Branch: 1 branch
BranchTrueFalseTotal
if ($$ref) 0 18 18
124 0 $cache{$class} = $$ref;
125 0 $found = 1;
126 0 last;
127 }
128 }
129 3 ▶ if (!$found) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($found) 3 0 3
130 3 $class =~ /(\w+)$/;
131 3 $cache{$class} = lc $1;
132 }
133 }
134  
135 4 return $cache{$class};
136 }
137  
138 {
139 2 ▶ no warnings 'once';
Subroutine BEGIN: called CC=1 SCAR=0.0
140 2 *table = \&tableName;
141 }
142  
143 # -----------------------------------------------------------------------------
144  
145 =head3 primaryKey() - Liefere Namen der Primärschlüssel-Kolumne
146  
147 =head4 Synopsis
148  
149 $title = $this->primaryKey($db);
150  
151 =head4 Description
152  
153 Bestimme den Namen der Primärschlüsselkolumne und liefere diesen zurück.
154 Der Name wird folgendermaßen ermittelt:
155  
156 =over 4
157  
158 =item 1.
159  
160 ...
161  
162 =item 2.
163  
164 Ist 1. nicht der Fall, wird als Primärschlüsselkolumne die
165 erste Kolumne der Ergebnistabelle angenommen.
166  
167 =back
168  
169 =cut
170  
171 # -----------------------------------------------------------------------------
172  
173 0 ▶sub primaryKey {
Subroutine primaryKey: not called CC=1 SCAR=6.9
Pod documented
174 0 my ($this,$db) = @_;
175 0 return $db->titles($this->tableName)->[0];
176 }
177  
178 # -----------------------------------------------------------------------------
179  
180 =head3 primaryKeyWhere() - Liefere Primary-Key Bedingung
181  
182 =head4 Synopsis
183  
184 @where = $row->primaryKeyWhere($db);
185  
186 =head4 Description
187  
188 Liefere die WHERE-Bedingung ($key=>$value) für den Datensatz $row,
189 bestehend aus dem Namen der Primärschlüsselkolumne und deren Wert.
190 Hat die Primärschlüsselkolumne keinen Wert, wirf eine Exception.
191  
192 Die Methode liefert die WHERE-Bedingung für UPDATEs und DELETEs
193 auf dem Datensatz.
194  
195 =cut
196  
197 # -----------------------------------------------------------------------------
198  
199 0 ▶sub primaryKeyWhere {
Subroutine primaryKeyWhere: not called CC=2 SCAR=17.9
Pod documented
200 0 my ($self,$db) = @_;
201  
202 0 my $key = $self->primaryKey($db);
203 0 my $val = $self->$key;
204 0 ▶ if ($val eq '') {
Branch: 1 branch
BranchTrueFalseTotal
if ($val eq "") 0 0 0
205 0 $self->throw(
206 'ROW-00005: Primärschlüsselkolumne ist NULL',
207 PrimaryKeyColumn => $key,
208 Row => $self->asString('|'),
209 );
210 }
211  
212 0 return ($key,$val);
213 }
214  
215 # -----------------------------------------------------------------------------
216  
217 =head3 nullRow() - Liefere Null-Datensatz
218  
219 =head4 Synopsis
220  
221 $row = $class->nullRow($db);
222  
223 =cut
224  
225 # -----------------------------------------------------------------------------
226  
227 0 ▶sub nullRow {
Subroutine nullRow: not called CC=1 SCAR=6.9
Pod documented
228 0 my ($class,$db) = @_;
229 0 return $db->nullRow($class->tableName,-rowClass=>$class);
230 }
231  
232 # -----------------------------------------------------------------------------
233  
234 =head2 Statement-Generierung
235  
236 =head3 selectStmt() - Liefere Select-Statement der Klasse
237  
238 =head4 Synopsis
239  
240 $stmt = $class->selectStmt($db,@select);
241  
242 =cut
243  
244 # -----------------------------------------------------------------------------
245  
246 2 ▶sub selectStmt {
Subroutine selectStmt: called CC=1 SCAR=0.0
Pod documented
247 2 my $class = shift;
248 2 my $db = shift;
249 # @_: @select
250  
251 2 return $db->stmt->select($class->tableName,@_);
252 }
253  
254 # -----------------------------------------------------------------------------
255  
256 =head3 insertStmt() - Liefere Insert-Statement für Datensatz
257  
258 =head4 Synopsis
259  
260 $stmt = $row->insertStmt($db);
261  
262 =cut
263  
264 # -----------------------------------------------------------------------------
265  
266 0 ▶sub insertStmt {
Subroutine insertStmt: not called CC=2 SCAR=17.9
Pod documented
267 0 my ($self,$db) = @_;
268  
269 0 my @keyVal;
270 0 for my $title ($self->titles) {
271 0 push @keyVal,$title,$self->$title;
272 }
273  
274 0 return $db->stmt->insert($self->tableName,@keyVal);
275 }
276  
277 # -----------------------------------------------------------------------------
278  
279 =head3 updateStmt() - Liefere Update-Statement für Datensatz
280  
281 =head4 Synopsis
282  
283 $stmt = $row->updateStmt($db);
284  
285 =cut
286  
287 # -----------------------------------------------------------------------------
288  
289 0 ▶sub updateStmt {
Subroutine updateStmt: not called CC=2 SCAR=17.9
Pod documented
290 0 my ($self,$db) = @_;
291  
292 0 my @keyVal;
293 0 for my $title ($self->titles) {
294 0 push @keyVal,$title,$self->$title;
295 }
296  
297 0 return $db->stmt->update($self->tableName,@keyVal,
298 -where,$self->primaryKeyWhere($db),
299 );
300 }
301  
302 # -----------------------------------------------------------------------------
303  
304 =head3 deleteStmt() - Liefere Delete-Statement für Datensatz
305  
306 =head4 Synopsis
307  
308 $stmt = $row->deleteStmt($db);
309  
310 =cut
311  
312 # -----------------------------------------------------------------------------
313  
314 0 ▶sub deleteStmt {
Subroutine deleteStmt: not called CC=1 SCAR=6.9
Pod documented
315 0 my ($self,$db) = @_;
316 0 return $db->stmt->delete($self->tableName,$self->primaryKeyWhere($db));
317 }
318  
319 # -----------------------------------------------------------------------------
320  
321 =head2 Datenbank-Operationen
322  
323 =head3 load() - Lade Datensatz
324  
325 =head4 Synopsis
326  
327 $row = $class->load($db,$pkValue);
328  
329 =head4 Description
330  
331 Lade Datensatz mit Primärschlüssel $pkValue. Ist $pkValue nicht
332 angegeben oder Null (Leerstring oder undef), liefere einen leeren
333 Datensatz.
334  
335 Diese Methode ist nützlich, um ein Formular mit einem neuen
336 oder existierenden Datensatz zu versorgen.
337  
338 =cut
339  
340 # -----------------------------------------------------------------------------
341  
342 0 ▶sub load {
Subroutine load: not called CC=3 SCAR=24.8
Pod documented
343 0 my $class = shift;
344 0 my $db = shift;
345 0 my $pkValue = shift;
346  
347 0 ▶ if (defined $pkValue && $pkValue ne '') {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $pkValue and $pkValue ne "") 0 0 0
Condition0%0/3
defined $pkValue and $pkValue ne ""
!ll&&!rl&&r
000
MC/DC0%0/2
defined $pkValue and $pkValue ne ""
defined $pkValue $pkValue ne ""
Truth table0%0/3
A and B
A= defined $pkValueB= $pkValue ne ""
ABresult
0X0
100
111
348 0 my $pkName = $class->primaryKey($db);
349 0 return $class->lookup($db,$pkName=>$pkValue);
350 }
351  
352 0 return $class->nullRow($db);
353 }
354  
355 # -----------------------------------------------------------------------------
356  
357 =head3 insert() - Füge Datensatz zur Datenbank hinzu
358  
359 =head4 Synopsis
360  
361 $cur = $row->insert($db);
362  
363 =head4 Description
364  
365 Füge den Datensatz zur Datenbank hinzu und liefere das Resultat der
366 Ausführung zurück.
367  
368 Nach der Ausführung hat der Datensatz den Rowstatus 0.
369  
370 =cut
371  
372 # -----------------------------------------------------------------------------
373  
374 0 ▶sub insert {
Subroutine insert: not called CC=1 SCAR=6.9
Pod documented
375 0 my ($self,$db) = @_;
376  
377 0 my $stmt = $self->insertStmt($db);
378 0 my $cur = $db->sql($stmt);
379 0 $self->rowStatus(0);
380  
381 0 return $cur;
382 }
383  
384 # -----------------------------------------------------------------------------
385  
386 =head3 update() - Aktualisiere Datensatz auf der Datenbank
387  
388 =head4 Synopsis
389  
390 $cur = $row->update($db);
391  
392 =head4 Description
393  
394 Aktualisiere den Datensatz auf der Datenbank und liefere das Resultat der
395 Ausführung zurück.
396  
397 Nach der Ausführung hat der Datensatz den Rowstatus 0.
398  
399 =cut
400  
401 # -----------------------------------------------------------------------------
402  
403 0 ▶sub update {
Subroutine update: not called CC=1 SCAR=6.9
Pod documented
404 0 my ($self,$db) = @_;
405  
406 0 my $stmt = $self->updateStmt($db);
407 0 my $cur = $db->sql($stmt);
408 0 $self->rowStatus(0);
409  
410 0 return $cur;
411 }
412  
413 # -----------------------------------------------------------------------------
414  
415 =head3 delete() - Lösche Datensatz von der Datenbank
416  
417 =head4 Synopsis
418  
419 $cur = $row->delete($db);
420  
421 =head4 Description
422  
423 Lösche den Datensatz von der Datenbank und liefere das Resultat der
424 Ausführung zurück.
425  
426 Nach der Ausführung hat der Datensatz den Rowstatus 'I'.
427  
428 =cut
429  
430 # -----------------------------------------------------------------------------
431  
432 0 ▶sub delete {
Subroutine delete: not called CC=1 SCAR=6.9
Pod documented
433 0 my ($self,$db) = @_;
434  
435 0 my $stmt = $self->deleteStmt($db);
436 0 my $cur = $db->sql($stmt);
437 0 $self->rowStatus('I');
438  
439 0 return $cur;
440 }
441  
442 # -----------------------------------------------------------------------------
443  
444 =head1 VERSION
445  
446 1.240
447  
448 =head1 AUTHOR
449  
450 Frank Seitz, L<http://fseitz.de/>
451  
452 =head1 COPYRIGHT
453  
454 Copyright (C) 2026 Frank Seitz
455  
456 =head1 LICENSE
457  
458 This code is free software; you can redistribute it and/or modify
459 it under the same terms as Perl itself.
460  
461 =cut
462  
463 # -----------------------------------------------------------------------------
464  
465 2 1;
466  
467 # eof
« Quiq/Html/Table/Simple.pm ↑ Summary Quiq/Imap/Client.pm »