Quiq/ModelCache.pm

filter: Statementstmt 21.6% 13 / 60 Branchbran 0.0% 0 / 12 Conditioncond 0.0% 0 / 9 MC/DCmcdc 0.0% 0 / 6 Subroutinesub 36.3% 4 / 11 Podpod 100% 7 / 7 totaltotal 22.8% 24 / 105 CC 15 SCAR 26.8
CC
15
Cov
16%
new
20.0
all
20.0
lookup
20.0
CRAP 14.6
« Quiq/DataStructure.pm ↑ Summary Quiq/ImagePool.pm »
1 # -----------------------------------------------------------------------------
2  
3 =encoding utf8
4  
5 =head1 NAME
6  
7 Quiq::ModelCache - Verwaltung/Caching von Modell-Objekten
8  
9 =head1 BASE CLASS
10  
11 L<Quiq::Hash>
12  
13 =head1 SYNOPSIS
14  
15 Instantiiere das Modell-Objekt für eine Reihe von Tabellen:
16  
17 $mod = Quiq::ModelCache->new($db,@types);
18  
19 Liefere alle Datensätze einer Tabelle:
20  
21 @rows|$tab = $mod->all($type);
22  
23 Liefere Datensatz zu Primärschlüssel:
24  
25 $row = $mod->lookup($type,$id);
26  
27 Liefere zu einem Datensatz alle Kind-Datensätze einer Kind-Tabelle:
28  
29 @rows|$tab = $mod->childs($row,$childType);
30  
31 Liefere zu einem Datensatz den Eltern-Datensatz einer Eltern-Tabelle:
32  
33 $row = $mod->parent($row,$parentType);
34  
35 =head1 DESCRIPTION
36  
37 Ein Objekt der Klasse stellt einen Cache für eine Reihe von
38 Tabellen/Views einer Datenbank dar. Mit den Methoden der Klasse
39 kann über die Datensätze navigiert werden, wobei diese sukzessive
40 geladen werden. Tabellen, deren Inhalt nicht zugegriffen wird,
41 werden auch nicht geladen.
42  
43 ACHTUNG: Da eine Tabelle/View beim ersten Zugriff vollständig geladen
44 wird, ist die Klasse nicht für Tabellen mit Massendaten geeignet.
45  
46 =head1 EXAMPLES
47  
48 =head2 Telefonliste
49  
50 Tabellen:
51  
52 Person Telefon
53 ------ -------
54 id id
55 name person_id
56 nummer
57  
58 Programm:
59  
60 my $mod = Quiq::ModelCache->new($db,
61 'person',
62 'telefon',
63 );
64
65 for my $per ($mod->all('person')) {
66 printf "%s\n",$per->name;
67 for my $tel ($mod->childs($per,'telefon')) {
68 printf " %s\n",$tel->nummer;
69 }
70 }
71 __END__
72 Frank Seitz
73 0176/78243503
74  
75 =head2 Kolumnen mit Präfix
76  
77 Tabellen:
78  
79 Person Telefon
80 ------ -------
81 per_id tel_id
82 per_name tel_person_id
83 tel_nummer
84  
85 Programm:
86  
87 my $mod = Quiq::ModelCache->new($db,
88 [person => 'per'],
89 [telefon => 'tel'],
90 );
91
92 for my $per ($mod->all('person')) {
93 printf "%s\n",$per->per_name;
94 for my $tel ($mod->childs($per,'telefon')) {
95 printf " %s\n",$tel->tel_nummer;
96 }
97 }
98 __END__
99 Frank Seitz
100 0176/78243503
101  
102 =cut
103  
104 # -----------------------------------------------------------------------------
105  
106 package Quiq::ModelCache;
107 1 ▶use base qw/Quiq::Hash/;
Subroutine BEGIN: called CC=1 SCAR=0.0
108  
109 1 ▶use v5.10;
Subroutine BEGIN: called CC=1 SCAR=0.0
110 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
111 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
112  
113 1 our $VERSION = '1.240';
114  
115 # -----------------------------------------------------------------------------
116  
117 =head1 METHODS
118  
119 =head2 Konstruktor
120  
121 =head3 new() - Konstruktor
122  
123 =head4 Synopsis
124  
125 $mod = $class->new($db,@types);
126  
127 =head4 Description
128  
129 Instantiiere einen Cache für Modell-Objekte @types der Datenbank $db
130 und liefere eine Referenz auf dieses Objekt zurück.
131  
132 =cut
133  
134 # -----------------------------------------------------------------------------
135  
136 0 ▶sub new {
Subroutine new: not called CC=4 SCAR=30.0
Pod documented
137 0 my ($class,$db,@types) = @_;
138  
139 0 my (@keyVal,%prefix);
140 0 for (@types) {
141 0 ▶ my ($type,$prefix) = ref($_)? @$_: ($_,'');
Branch: 1 branch
BranchTrueFalseTotal
ref $_ ? : 0 0 0
142 0 ▶ if ($prefix) {
Branch: 1 branch
BranchTrueFalseTotal
if ($prefix) 0 0 0
143 0 $prefix .= '_';
144 }
145 0 $prefix{$type} = $prefix;
146 0 push @keyVal,"${type}T"=>undef,"${type}H"=>undef;
147 }
148  
149 0 return $class->SUPER::new(
150 db => $db,
151 prefixH => \%prefix,
152 @keyVal,
153 );
154 }
155  
156 # -----------------------------------------------------------------------------
157  
158 =head2 Navigation
159  
160 =head3 all() - Entitätsmenge
161  
162 =head4 Synopsis
163  
164 @rows|$tab = $mod->all($type);
165  
166 =head4 Description
167  
168 Liefere alle Datensätze vom Typ $type. Im Skalarkontext liefere ein
169 Tabellenobjekt mit den Datensätzen.
170  
171 =cut
172  
173 # -----------------------------------------------------------------------------
174  
175 0 ▶sub all {
Subroutine all: not called CC=4 SCAR=30.0
Pod documented
176 0 my ($self,$type) = @_;
177  
178 0 ▶ my $tab = $self->{"${type}T"} //= do {
Condition0%0/3
$$self{"${type}T"} //= do { my $tab = $self->db->select($type); foreach my $ent ($tab->rows) { $ent->add("type", $type); $ent->add("model", $self); $ent->weaken("model"); } ; $tab }
l!l&&r!l&&!r
000
MC/DC0%0/2
$$self{"${type}T"} //= do { my $tab = $self->db->select($type); foreach my $ent ($tab->rows) { $ent->add("type", $type); $ent->add("model", $self); $ent->weaken("model"); } ; $tab }
$$self{"${type}T"} do { my $tab = $self->db->select($type); foreach my $ent ($tab->rows) { $ent->add("type", $type); $ent->add("model", $self); $ent->weaken("model"); } ; $tab }
Truth table0%0/3
A //= B
A= $$self{"${type}T"}B= do { my $tab = $self->db->select($type); foreach my $ent ($tab->rows) { $ent->add("type", $type); $ent->add("model", $self); $ent->weaken("model"); } ; $tab }
ABresult
1X1
011
000
179 # Entitätsmenge selektieren
180 0 my $tab = $self->db->select($type);
181  
182 # Jeder Entität den Typ und eine Referenz auf den Cache hinzufügen
183
184 0 for my $ent ($tab->rows) {
185 # Typbezeichnung hinzufügen
186 0 $ent->add(type => $type);
187
188 # Referenz auf Cache hinzufügen
189 0 $ent->add(model => $self);
190 0 $ent->weaken('model');
191 }
192
193 0 $tab;
194 };
195  
196 0 ▶ return wantarray? $tab->rows: $tab;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 0 0
197 }
198  
199 # -----------------------------------------------------------------------------
200  
201 =head3 lookup() - Datensatz zu Schlüsselwert
202  
203 =head4 Synopsis
204  
205 $ent = $mod->lookup($type,$id);
206  
207 =head4 Description
208  
209 Ermittele in der Tabelle $type den Datensatz mit dem Schlüsselwert $id
210 und liefere diesen zurück. Existiert kein Datensatz mit dem
211 Schlüsselwert $id, wird eine Exception geworfen.
212  
213 =cut
214  
215 # -----------------------------------------------------------------------------
216  
217 0 ▶sub lookup {
Subroutine lookup: not called CC=4 SCAR=30.0
Pod documented
218 0 my ($self,$type,$id) = @_;
219  
220 0 ▶ my $h = $self->{"${type}H"} //= do {
Condition0%0/3
$$self{"${type}H"} //= do { my %h; my $pk = $self->pk($type); foreach my $ent ($self->all($type)) { $h{$ent->$pk} = $ent; } ; \%h }
l!l&&r!l&&!r
000
MC/DC0%0/2
$$self{"${type}H"} //= do { my %h; my $pk = $self->pk($type); foreach my $ent ($self->all($type)) { $h{$ent->$pk} = $ent; } ; \%h }
$$self{"${type}H"} do { my %h; my $pk = $self->pk($type); foreach my $ent ($self->all($type)) { $h{$ent->$pk} = $ent; } ; \%h }
Truth table0%0/3
A //= B
A= $$self{"${type}H"}B= do { my %h; my $pk = $self->pk($type); foreach my $ent ($self->all($type)) { $h{$ent->$pk} = $ent; } ; \%h }
ABresult
1X1
011
000
221 0 my %h;
222 0 my $pk = $self->pk($type);
223 0 for my $ent ($self->all($type)) {
224 0 $h{$ent->$pk} = $ent;
225 };
226 0 \%h;
227 };
228  
229 0 ▶ return $h->{$id} || $self->throw(
Condition0%0/3
$h->{$id} || $self->throw('MODEL-00001: Entity not found', 'Type', $type, 'PrimaryKey', $id)
l!l&&r!l&&!r
000
MC/DC0%0/2
$h->{$id} || $self->throw('MODEL-00001: Entity not found', 'Type', $type, 'PrimaryKey', $id)
$h->{$id} $self->throw('MODEL-00001: Entity not found', 'Type', $type, 'PrimaryKey', $id)
Truth table0%0/3
A || B
A= $h->{$id}B= $self->throw('MODEL-00001: Entity not found', 'Type', $type, 'PrimaryKey', $id)
ABresult
1X1
011
000
230 'MODEL-00001: Entity not found',
231 Type => $type,
232 PrimaryKey => $id,
233 );
234 }
235  
236 # -----------------------------------------------------------------------------
237  
238 =head3 childs() - Kind-Datensätze eines Datensatzes
239  
240 =head4 Synopsis
241  
242 @rows|$tab = $mod->childs($row,$childType);
243  
244 =head4 Description
245  
246 Ermittele zu Datensatz $row alle Kind-Datensätze vom Typ $childType
247 und liefere diese zurück. Im Skalarkontext liefere ein
248 Tabellenobjekt mit diesen Datensätzen.
249  
250 =cut
251  
252 # -----------------------------------------------------------------------------
253  
254 0 ▶sub childs {
Subroutine childs: not called CC=4 SCAR=30.0
Pod documented
255 0 my ($self,$ent,$childType) = @_;
256  
257 0 ▶ if (!$ent->childTypeExists($childType)) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($ent->childTypeExists($childType)) 0 0 0
258 # Kind-Typ zu allen Eltern-Entitäten hinzufügen
259
260 0 my $parentType = $ent->type;
261 0 for my $par ($self->all($parentType)) {
262 0 $par->addChildType($childType);
263 }
264  
265 # Alle Kind-Entitäten ihren Eltern-Entitäten hinzufügen
266
267 0 my $fk = $self->fk($childType,$parentType);
268 0 for my $cld ($self->all($childType)) {
269 0 my $par = $self->lookup($parentType,$cld->$fk);
270 0 $par->addChild($childType,$cld);
271 }
272 }
273  
274 0 return $ent->getChilds($childType);
275 }
276  
277 # -----------------------------------------------------------------------------
278  
279 =head3 parent() - Eltern-Datensatz eines Datensatzes
280  
281 =head4 Synopsis
282  
283 $par = $mod->parent($row,$parentType);
284  
285 =head4 Description
286  
287 Liefere zu Datensatz $row dessen Eltern-Datensatz vom Typ $parentType.
288  
289 =cut
290  
291 # -----------------------------------------------------------------------------
292  
293 0 ▶sub parent {
Subroutine parent: not called CC=3 SCAR=24.8
Pod documented
294 0 my ($self,$ent,$parentType) = @_;
295  
296 0 ▶ if (!$ent->parentExists($parentType)) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($ent->parentExists($parentType)) 0 0 0
297 0 my $fk = $self->fk($ent->type,$parentType);
298 0 my $parentId = $ent->$fk;
299 0 ▶ my $par = $parentId? $self->lookup($parentType,$ent->$fk): undef;
Branch: 1 branch
BranchTrueFalseTotal
$parentId ? : 0 0 0
300 0 $ent->addParent($parentType,$par);
301 }
302  
303 0 return $ent->getParent($parentType);
304 }
305  
306 # -----------------------------------------------------------------------------
307  
308 =head2 Kolumnen
309  
310 =head3 pk() - Name Primärschlüsselkolumne
311  
312 =head4 Synopsis
313  
314 $pk = $mod->pk($type);
315  
316 =head4 Description
317  
318 Liefere den Namen der Primärschlüsselkolumne des Modell-Objekts $type.
319 Ohne vereinbarten Kolumnenpräfix lautet der Name
320  
321 id
322  
323 Mit vereinbarten Kolumnenpräfix lautet der Name
324  
325 <prefix>_id
326  
327 =cut
328  
329 # -----------------------------------------------------------------------------
330  
331 0 ▶sub pk {
Subroutine pk: not called CC=1 SCAR=6.9
Pod documented
332 0 my ($self,$type) = @_;
333 0 return sprintf '%sid',$self->{'prefixH'}->{$type};
334 }
335  
336 # -----------------------------------------------------------------------------
337  
338 =head3 fk() - Name Fremdschlüsselkolumne
339  
340 =head4 Synopsis
341  
342 $fk = $mod->fk($type,$parentType);
343  
344 =head4 Description
345  
346 Liefere den Namen der Fremdschlüsselkolumne eines Modell-Objekts
347 vom Typ $type für ein Parent-Modell-Objekt vom Typ $parentType.
348  
349 Ohne vereinbarten Kolumnenpräfix für $type lautet der Name
350  
351 <parentType>_id
352  
353 Mit vereinbarten Kolumnenpräfix für $type lautet der Name
354  
355 <prefix>_<parentType>_id
356  
357 =cut
358  
359 # -----------------------------------------------------------------------------
360  
361 0 ▶sub fk {
Subroutine fk: not called CC=1 SCAR=6.9
Pod documented
362 0 my ($self,$type,$parentType) = @_;
363 0 return sprintf '%s%s_id',$self->{'prefixH'}->{$type},$parentType;
364 }
365  
366 # -----------------------------------------------------------------------------
367  
368 =head1 VERSION
369  
370 1.240
371  
372 =head1 AUTHOR
373  
374 Frank Seitz, L<http://fseitz.de/>
375  
376 =head1 COPYRIGHT
377  
378 Copyright (C) 2026 Frank Seitz
379  
380 =head1 LICENSE
381  
382 This code is free software; you can redistribute it and/or modify
383 it under the same terms as Perl itself.
384  
385 =cut
386  
387 # -----------------------------------------------------------------------------
388  
389 1 1;
390  
391 # eof
« Quiq/DataStructure.pm ↑ Summary Quiq/ImagePool.pm »