| 1 | # ----------------------------------------------------------------------------- | ||||||||||||||
| 2 | |||||||||||||||
| 3 | =encoding utf8 | ||||||||||||||
| 4 | |||||||||||||||
| 5 | =head1 NAME | ||||||||||||||
| 6 | |||||||||||||||
| 7 | Quiq::Database::Tree - Baum von Datensätzen | ||||||||||||||
| 8 | |||||||||||||||
| 9 | =head1 BASE CLASS | ||||||||||||||
| 10 | |||||||||||||||
| 11 | L<Quiq::Hash> | ||||||||||||||
| 12 | |||||||||||||||
| 13 | =head1 DESCRIPTION | ||||||||||||||
| 14 | |||||||||||||||
| 15 | Ein Objekt der Klasse repräsentiert eine Menge von Datensätzen, | ||||||||||||||
| 16 | die mittels zweier Attribute $pkColumn und $fkColumn in einer | ||||||||||||||
| 17 | hierarchischen Beziehung zueinander stehen und somit eine | ||||||||||||||
| 18 | Baumstruktur bilden. Die Klasse bietet Methoden, um auf dieser | ||||||||||||||
| 19 | Baumstruktur operieren zu können. | ||||||||||||||
| 20 | |||||||||||||||
| 21 | =cut | ||||||||||||||
| 22 | |||||||||||||||
| 23 | # ----------------------------------------------------------------------------- | ||||||||||||||
| 24 | |||||||||||||||
| 25 | package Quiq::Database::Tree; | ||||||||||||||
| 26 | 1 | ▶ | use base qw/Quiq::Hash/; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 27 | |||||||||||||||
| 28 | 1 | ▶ | use v5.10; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 29 | 1 | ▶ | use strict; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 30 | 1 | ▶ | use warnings; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 31 | |||||||||||||||
| 32 | 1 | our $VERSION = '1.240'; | |||||||||||||
| 33 | |||||||||||||||
| 34 | 1 | ▶ | use Time::HiRes (); | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 35 | |||||||||||||||
| 36 | # ----------------------------------------------------------------------------- | ||||||||||||||
| 37 | |||||||||||||||
| 38 | =head1 METHODS | ||||||||||||||
| 39 | |||||||||||||||
| 40 | =head2 Konstruktor | ||||||||||||||
| 41 | |||||||||||||||
| 42 | =head3 new() - Instantiiere Baum von Datensätzen | ||||||||||||||
| 43 | |||||||||||||||
| 44 | =head4 Synopsis | ||||||||||||||
| 45 | |||||||||||||||
| 46 | $tree = $class->new($tab,$pkColumn,$fkColumn,@opt); | ||||||||||||||
| 47 | |||||||||||||||
| 48 | =head4 Options | ||||||||||||||
| 49 | |||||||||||||||
| 50 | =over 4 | ||||||||||||||
| 51 | |||||||||||||||
| 52 | =item -whenNoParentRow => 'removeRow'|'removeReference'|'throwException' \ | ||||||||||||||
| 53 | |||||||||||||||
| 54 | (Default: 'throwException') | ||||||||||||||
| 55 | Was getan werden soll, wenn der Parent eines Child-Datensatzes in | ||||||||||||||
| 56 | der Ergebnismenge nicht enthalten ist: | ||||||||||||||
| 57 | |||||||||||||||
| 58 | =over 4 | ||||||||||||||
| 59 | |||||||||||||||
| 60 | =item 'removeRow' | ||||||||||||||
| 61 | |||||||||||||||
| 62 | Entferne den Datensatz aus der Ergebnismenge. | ||||||||||||||
| 63 | |||||||||||||||
| 64 | =item 'removeReference' | ||||||||||||||
| 65 | |||||||||||||||
| 66 | Setze die Referenz auf NULL (Leerstring). Der Child-Datensatz | ||||||||||||||
| 67 | wird damit logisch zu einem Parent-Datensatz. | ||||||||||||||
| 68 | |||||||||||||||
| 69 | =item 'throwException' | ||||||||||||||
| 70 | |||||||||||||||
| 71 | Wirf eine Exception. | ||||||||||||||
| 72 | |||||||||||||||
| 73 | =back | ||||||||||||||
| 74 | |||||||||||||||
| 75 | Die Varianten 'removeRow' und 'removeReference' sind nützlich, wenn | ||||||||||||||
| 76 | die Ergebnismenge nicht alle Sätze enthält, sondern nur eine Teilmenge, | ||||||||||||||
| 77 | z.B. aufgrund einer Selektion mit -limit. | ||||||||||||||
| 78 | |||||||||||||||
| 79 | =back | ||||||||||||||
| 80 | |||||||||||||||
| 81 | =head4 Description | ||||||||||||||
| 82 | |||||||||||||||
| 83 | Instantiiere ein Baum-Objekt aus den Datensätzen des ResultSet | ||||||||||||||
| 84 | $tab. Die Datensätze stehen über die Attribute $pkColumn und | ||||||||||||||
| 85 | $fkColumn in einer hierarchischen Beziehung. | ||||||||||||||
| 86 | |||||||||||||||
| 87 | =head4 Example | ||||||||||||||
| 88 | |||||||||||||||
| 89 | Datensätze: | ||||||||||||||
| 90 | |||||||||||||||
| 91 | id parent_id name | ||||||||||||||
| 92 | -- --------- ---- | ||||||||||||||
| 93 | 1 NULL A | ||||||||||||||
| 94 | 2 1 B | ||||||||||||||
| 95 | 3 2 C | ||||||||||||||
| 96 | 4 1 D | ||||||||||||||
| 97 | |||||||||||||||
| 98 | Pfade: | ||||||||||||||
| 99 | |||||||||||||||
| 100 | A | ||||||||||||||
| 101 | A/B | ||||||||||||||
| 102 | A/B/C | ||||||||||||||
| 103 | A/D | ||||||||||||||
| 104 | |||||||||||||||
| 105 | Baum: | ||||||||||||||
| 106 | |||||||||||||||
| 107 | A | ||||||||||||||
| 108 | +-B | ||||||||||||||
| 109 | | \-C | ||||||||||||||
| 110 | +-D | ||||||||||||||
| 111 | |||||||||||||||
| 112 | Aufruf: | ||||||||||||||
| 113 | |||||||||||||||
| 114 | $tree = Quiq::Database::Tree->new($tab,'id','parent_id'); | ||||||||||||||
| 115 | |||||||||||||||
| 116 | =cut | ||||||||||||||
| 117 | |||||||||||||||
| 118 | # ----------------------------------------------------------------------------- | ||||||||||||||
| 119 | |||||||||||||||
| 120 | 1 | ▶ | sub new { | ||||||||||||
|
Subroutine
new: called CC=6 SCAR=20.3
Pod
documented
| |||||||||||||||
| 121 | 1 | my ($class,$tab,$pkColumn,$fkColumn) = splice @_,0,4; | |||||||||||||
| 122 | |||||||||||||||
| 123 | # Optionen | ||||||||||||||
| 124 | |||||||||||||||
| 125 | 1 | my $whenNoParentRow = 'throwException'; | |||||||||||||
| 126 | |||||||||||||||
| 127 | 1 | $class->parameters(\@_, | |||||||||||||
| 128 | -whenNoParentRow => \$whenNoParentRow, | ||||||||||||||
| 129 | ); | ||||||||||||||
| 130 | |||||||||||||||
| 131 | # Operation ausführen | ||||||||||||||
| 132 | |||||||||||||||
| 133 | # Typbezeichner für Baumverknüpfung erzeugen | ||||||||||||||
| 134 | 1 | my $type = sprintf 'Tree%s',scalar Time::HiRes::gettimeofday; | |||||||||||||
| 135 | |||||||||||||||
| 136 | # Kind-Typ zu allen Datensätzen hinzufügen | ||||||||||||||
| 137 | 1 | $tab->addChildType($type); | |||||||||||||
| 138 | |||||||||||||||
| 139 | # Primary-Key-Index erzeugen | ||||||||||||||
| 140 | 1 | my $h = $tab->index($pkColumn)->lockKeys; | |||||||||||||
| 141 | |||||||||||||||
| 142 | # Datensätze miteinander verknüpfen | ||||||||||||||
| 143 | |||||||||||||||
| 144 | 1 | my $rowA = $tab->rows; | |||||||||||||
| 145 | 1 | ▶ | for (my $i = 0; $i < @$rowA; $i++) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 146 | 4 | my $row = $rowA->[$i]; | |||||||||||||
| 147 | 4 | ▶ | if (my $pk = $row->$fkColumn) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 148 | 3 | my $par = $h->try($pk); | |||||||||||||
| 149 | 3 | ▶ | if (!$par) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 150 | 0 | ▶ | if ($whenNoParentRow eq 'removeRow') { | ||||||||||||
|
Branch: 2 branches
| |||||||||||||||
| 151 | 0 | splice @$rowA,$i--,1; | |||||||||||||
| 152 | 0 | next; | |||||||||||||
| 153 | } | ||||||||||||||
| 154 | elsif ($whenNoParentRow eq 'removeReference') { | ||||||||||||||
| 155 | 0 | $row->$fkColumn(''); | |||||||||||||
| 156 | 0 | next; | |||||||||||||
| 157 | } | ||||||||||||||
| 158 | $class->throw( | ||||||||||||||
| 159 | 0 | 'TREE-00099: Parent row not found', | |||||||||||||
| 160 | $pkColumn => $pk, | ||||||||||||||
| 161 | ); | ||||||||||||||
| 162 | } | ||||||||||||||
| 163 | 3 | $row->addParent($type,$par); | |||||||||||||
| 164 | 3 | $par->addChild($type,$row); | |||||||||||||
| 165 | } | ||||||||||||||
| 166 | } | ||||||||||||||
| 167 | |||||||||||||||
| 168 | 1 | return $class->SUPER::new( | |||||||||||||
| 169 | table => $tab, | ||||||||||||||
| 170 | fkColumn => $fkColumn, | ||||||||||||||
| 171 | type => $type, | ||||||||||||||
| 172 | pkIndex => $h, | ||||||||||||||
| 173 | ); | ||||||||||||||
| 174 | } | ||||||||||||||
| 175 | |||||||||||||||
| 176 | # ----------------------------------------------------------------------------- | ||||||||||||||
| 177 | |||||||||||||||
| 178 | =head2 Akzessoren | ||||||||||||||
| 179 | |||||||||||||||
| 180 | =head3 table() - ResultSet-Objekt | ||||||||||||||
| 181 | |||||||||||||||
| 182 | =head4 Synopsis | ||||||||||||||
| 183 | |||||||||||||||
| 184 | $tab = $tree->table; | ||||||||||||||
| 185 | |||||||||||||||
| 186 | =head4 Description | ||||||||||||||
| 187 | |||||||||||||||
| 188 | Liefere das ResultSet-Objekt, das beim Konstruktor angegeben wurde. | ||||||||||||||
| 189 | |||||||||||||||
| 190 | =head3 pkIndex() - Primary-Key-Index | ||||||||||||||
| 191 | |||||||||||||||
| 192 | =head4 Synopsis | ||||||||||||||
| 193 | |||||||||||||||
| 194 | $h = $tree->pkIndex; | ||||||||||||||
| 195 | |||||||||||||||
| 196 | =head4 Description | ||||||||||||||
| 197 | |||||||||||||||
| 198 | Liefere eine Referenz auf den Hash, der die Datensätze nach | ||||||||||||||
| 199 | Primary-Key-Index indiziert. | ||||||||||||||
| 200 | |||||||||||||||
| 201 | =head3 type() - Typ-Bezeichner | ||||||||||||||
| 202 | |||||||||||||||
| 203 | =head4 Synopsis | ||||||||||||||
| 204 | |||||||||||||||
| 205 | $type = $tree->type; | ||||||||||||||
| 206 | |||||||||||||||
| 207 | =head4 Description | ||||||||||||||
| 208 | |||||||||||||||
| 209 | Liefere den (intern generierten) Typ-Bezeichner, für den die | ||||||||||||||
| 210 | Datensatz-Verknüpfung definiert ist. | ||||||||||||||
| 211 | |||||||||||||||
| 212 | =head2 Objektmethoden | ||||||||||||||
| 213 | |||||||||||||||
| 214 | =head3 childs() - Kind-Datensätze | ||||||||||||||
| 215 | |||||||||||||||
| 216 | =head4 Synopsis | ||||||||||||||
| 217 | |||||||||||||||
| 218 | @rows|$tab = $tree->childs($row); | ||||||||||||||
| 219 | @rows|$tab = $tree->childs($pk); | ||||||||||||||
| 220 | |||||||||||||||
| 221 | =head4 Description | ||||||||||||||
| 222 | |||||||||||||||
| 223 | Liefere die Liste der Kind-Datensätze - also der I<unmittelbar> | ||||||||||||||
| 224 | untergeordneten Datensätze - zum Datensatz $row bzw. zum Datensatz | ||||||||||||||
| 225 | mit dem Primärschlüssel $pk. Besitzt der Datensatz keine | ||||||||||||||
| 226 | Kind-Datensätze, ist die Liste leer. Im Skalarkontext liefere ein | ||||||||||||||
| 227 | ResultSet-Objekt mit den Datensätzen. | ||||||||||||||
| 228 | |||||||||||||||
| 229 | =head4 Example | ||||||||||||||
| 230 | |||||||||||||||
| 231 | Aufruf: | ||||||||||||||
| 232 | |||||||||||||||
| 233 | @rows = $tree->childs(1); | ||||||||||||||
| 234 | |||||||||||||||
| 235 | Resultat: | ||||||||||||||
| 236 | |||||||||||||||
| 237 | id parent_id name | ||||||||||||||
| 238 | -- --------- ---- | ||||||||||||||
| 239 | 2 1 B | ||||||||||||||
| 240 | 4 1 D | ||||||||||||||
| 241 | |||||||||||||||
| 242 | =cut | ||||||||||||||
| 243 | |||||||||||||||
| 244 | # ----------------------------------------------------------------------------- | ||||||||||||||
| 245 | |||||||||||||||
| 246 | 18 | ▶ | sub childs { | ||||||||||||
|
Subroutine
childs: called CC=2 SCAR=7.0
Pod
documented
| |||||||||||||||
| 247 | 18 | my $self = shift; | |||||||||||||
| 248 | 18 | my $row = $self->lookup(shift); | |||||||||||||
| 249 | 18 | my @rows = $row->getChilds($self->type); | |||||||||||||
| 250 | 18 | ▶ | return wantarray? @rows: $self->table->new(\@rows); | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 251 | } | ||||||||||||||
| 252 | |||||||||||||||
| 253 | # ----------------------------------------------------------------------------- | ||||||||||||||
| 254 | |||||||||||||||
| 255 | =head3 descendants() - Untergeordnete Datensätze | ||||||||||||||
| 256 | |||||||||||||||
| 257 | =head4 Synopsis | ||||||||||||||
| 258 | |||||||||||||||
| 259 | @rows = $tree->descendants($row); | ||||||||||||||
| 260 | @rows = $tree->descendants($pk); | ||||||||||||||
| 261 | |||||||||||||||
| 262 | =head4 Description | ||||||||||||||
| 263 | |||||||||||||||
| 264 | Liefere die Liste I<aller> untergeordneten Datensätze zum | ||||||||||||||
| 265 | Datensatz $row bzw. zum Datensatz mit dem Primärschlüssel | ||||||||||||||
| 266 | $pk. Besitzt der Datensatz keine untergeordneten Datensätze, ist | ||||||||||||||
| 267 | die Liste leer. Die Reihenfolge der Datensätze entspricht der | ||||||||||||||
| 268 | einer Tiefensuche. Im Skalarkontext liefere ein ResultSet-Objekt | ||||||||||||||
| 269 | mit den Datensätzen. | ||||||||||||||
| 270 | |||||||||||||||
| 271 | =head4 Example | ||||||||||||||
| 272 | |||||||||||||||
| 273 | Aufruf: | ||||||||||||||
| 274 | |||||||||||||||
| 275 | @rows = $tree->descendants(1); | ||||||||||||||
| 276 | |||||||||||||||
| 277 | Resultat: | ||||||||||||||
| 278 | |||||||||||||||
| 279 | id parent_id name | ||||||||||||||
| 280 | -- --------- ---- | ||||||||||||||
| 281 | 2 1 B | ||||||||||||||
| 282 | 3 2 C | ||||||||||||||
| 283 | 4 1 D | ||||||||||||||
| 284 | |||||||||||||||
| 285 | =cut | ||||||||||||||
| 286 | |||||||||||||||
| 287 | # ----------------------------------------------------------------------------- | ||||||||||||||
| 288 | |||||||||||||||
| 289 | 8 | ▶ | sub descendants { | ||||||||||||
|
Subroutine
descendants: called CC=3 SCAR=11.1
Pod
documented
| |||||||||||||||
| 290 | 8 | my ($self,$arg) = @_; | |||||||||||||
| 291 | |||||||||||||||
| 292 | 8 | my @rows; | |||||||||||||
| 293 | 8 | for my $row ($self->childs($arg)) { | |||||||||||||
| 294 | 6 | push @rows,$row,$self->childs($row); | |||||||||||||
| 295 | } | ||||||||||||||
| 296 | |||||||||||||||
| 297 | 8 | ▶ | return wantarray? @rows: $self->table->new(\@rows); | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 298 | } | ||||||||||||||
| 299 | |||||||||||||||
| 300 | # ----------------------------------------------------------------------------- | ||||||||||||||
| 301 | |||||||||||||||
| 302 | =head3 generatePathAttribute() - Erzeuge Pfad-Attribut | ||||||||||||||
| 303 | |||||||||||||||
| 304 | =head4 Synopsis | ||||||||||||||
| 305 | |||||||||||||||
| 306 | $tree->generatePathAttribute($key,$valColumn,$sep); | ||||||||||||||
| 307 | |||||||||||||||
| 308 | =head4 Description | ||||||||||||||
| 309 | |||||||||||||||
| 310 | Füge zu allen Datensätzen das Attribut $key hinzu und setze | ||||||||||||||
| 311 | es auf den Pfad gemäß Datensatz-Attribut $valColumn mit | ||||||||||||||
| 312 | der Trenn-Zeichenkette $sep. Die Methode liefert keinen Wert zurück. | ||||||||||||||
| 313 | |||||||||||||||
| 314 | =head4 Example | ||||||||||||||
| 315 | |||||||||||||||
| 316 | Aufruf: | ||||||||||||||
| 317 | |||||||||||||||
| 318 | $tree->generatePathAttribute('path','name','/'); | ||||||||||||||
| 319 | |||||||||||||||
| 320 | Erweitert alle Datensätze um das Attribut 'path': | ||||||||||||||
| 321 | |||||||||||||||
| 322 | id parent_id name path | ||||||||||||||
| 323 | -- --------- ---- ----- | ||||||||||||||
| 324 | 1 NULL A A | ||||||||||||||
| 325 | 2 1 B A/B | ||||||||||||||
| 326 | 3 2 C A/B/C | ||||||||||||||
| 327 | 4 1 D A/D | ||||||||||||||
| 328 | |||||||||||||||
| 329 | =cut | ||||||||||||||
| 330 | |||||||||||||||
| 331 | # ----------------------------------------------------------------------------- | ||||||||||||||
| 332 | |||||||||||||||
| 333 | 0 | ▶ | sub generatePathAttribute { | ||||||||||||
|
Subroutine
generatePathAttribute: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||||
| 334 | 0 | my ($self,$key,$valColumn,$sep) = @_; | |||||||||||||
| 335 | |||||||||||||||
| 336 | 0 | for my $row (@{$self->rows}) { | |||||||||||||
| 337 | 0 | $row->add($key=>$self->path($row,$valColumn,$sep)); | |||||||||||||
| 338 | } | ||||||||||||||
| 339 | |||||||||||||||
| 340 | 0 | return; | |||||||||||||
| 341 | } | ||||||||||||||
| 342 | |||||||||||||||
| 343 | # ----------------------------------------------------------------------------- | ||||||||||||||
| 344 | |||||||||||||||
| 345 | =head3 hierarchy() - Datensätze als Hierarchie | ||||||||||||||
| 346 | |||||||||||||||
| 347 | =head4 Synopsis | ||||||||||||||
| 348 | |||||||||||||||
| 349 | @rows|$rowA = $tree->hierarchy(@opt); | ||||||||||||||
| 350 | |||||||||||||||
| 351 | =head4 Options | ||||||||||||||
| 352 | |||||||||||||||
| 353 | =over 4 | ||||||||||||||
| 354 | |||||||||||||||
| 355 | =item -childSort => $sub (Default: sub {0}) | ||||||||||||||
| 356 | |||||||||||||||
| 357 | Sortierfunktion für die Kind-Datensätze (die Wurzel-Datensätze bleiben | ||||||||||||||
| 358 | in ihrer gegebenen Reihenfolge). | ||||||||||||||
| 359 | |||||||||||||||
| 360 | ACHTUNG: Die Sortierfunktion muss mit Prototype ($$) vereinbart werden, | ||||||||||||||
| 361 | damit die Elemente per Parameter und nicht mittels der globalen Variablen | ||||||||||||||
| 362 | $a und $b übergeben werden. Denn die globalen Variablen befinden sich | ||||||||||||||
| 363 | in einem anderen Package als dem, in dem die Sortierfunktion aufgerufen | ||||||||||||||
| 364 | wird. Beispiel: | ||||||||||||||
| 365 | |||||||||||||||
| 366 | -childSort => sub ($$) { | ||||||||||||||
| 367 | $_[0]->id <=> $_[1]->id; | ||||||||||||||
| 368 | } | ||||||||||||||
| 369 | |||||||||||||||
| 370 | =item -setTable => $bool (Default: 0) | ||||||||||||||
| 371 | |||||||||||||||
| 372 | Setze die hierarchische Reihenfolge auf der zugrunde liegenden Tabelle. | ||||||||||||||
| 373 | D.h. $tree->table->rows() liefert die Datensätze fortan in dieser | ||||||||||||||
| 374 | Reihenfolge. | ||||||||||||||
| 375 | |||||||||||||||
| 376 | =back | ||||||||||||||
| 377 | |||||||||||||||
| 378 | =head4 Description | ||||||||||||||
| 379 | |||||||||||||||
| 380 | Liefere die Datensätze der Ergebnismenge in hierarchischer Reihenfolge, | ||||||||||||||
| 381 | also die Kind-Sätze in der Reihenfolge einer Tiefensuche. | ||||||||||||||
| 382 | |||||||||||||||
| 383 | =cut | ||||||||||||||
| 384 | |||||||||||||||
| 385 | # ----------------------------------------------------------------------------- | ||||||||||||||
| 386 | |||||||||||||||
| 387 | 0 | ▶ | sub hierarchy { | ||||||||||||
|
Subroutine
hierarchy: not called CC=4 SCAR=30.0
Pod
documented
| |||||||||||||||
| 388 | 0 | my $self = shift; | |||||||||||||
| 389 | |||||||||||||||
| 390 | # Optionen | ||||||||||||||
| 391 | |||||||||||||||
| 392 | 0 | ▶ | my $childSort = sub {0}; | ||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||
| 393 | 0 | my $setTable = 0; | |||||||||||||
| 394 | |||||||||||||||
| 395 | 0 | $self->parameters(\@_, | |||||||||||||
| 396 | -childSort => \$childSort, | ||||||||||||||
| 397 | -setTable => \$setTable, | ||||||||||||||
| 398 | ); | ||||||||||||||
| 399 | |||||||||||||||
| 400 | # Eingebettete rekursive Funktion | ||||||||||||||
| 401 | |||||||||||||||
| 402 | 0 | my $treeSub; # Vorab-Deklaration wg. Rekursion | |||||||||||||
| 403 | 0 | ▶ | $treeSub = sub { | ||||||||||||
|
Subroutine
__ANON__: not called CC=2 SCAR=17.9
| |||||||||||||||
| 404 | 0 | my $row = shift; | |||||||||||||
| 405 | |||||||||||||||
| 406 | 0 | my @rows = ($row); | |||||||||||||
| 407 | 0 | for my $row (sort $childSort $self->childs($row)) { | |||||||||||||
| 408 | 0 | push @rows,$treeSub->($row); | |||||||||||||
| 409 | } | ||||||||||||||
| 410 | |||||||||||||||
| 411 | 0 | return @rows; | |||||||||||||
| 412 | 0 | }; | |||||||||||||
| 413 | |||||||||||||||
| 414 | # Wurzelknoten in ihrer gegebenen Reihenfolge | ||||||||||||||
| 415 | |||||||||||||||
| 416 | 0 | my @rows; | |||||||||||||
| 417 | 0 | for my $row (@{$self->roots}) { | |||||||||||||
| 418 | 0 | push @rows,$treeSub->($row); | |||||||||||||
| 419 | } | ||||||||||||||
| 420 | |||||||||||||||
| 421 | 0 | ▶ | if ($setTable) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 422 | # Setze die Reihenfolge der Datensätze auf der | ||||||||||||||
| 423 | # zugrunde liegenden Tabelle | ||||||||||||||
| 424 | 0 | $self->table->set(rows=>\@rows); | |||||||||||||
| 425 | } | ||||||||||||||
| 426 | |||||||||||||||
| 427 | 0 | ▶ | return wantarray? @rows: \@rows; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 428 | } | ||||||||||||||
| 429 | |||||||||||||||
| 430 | # ----------------------------------------------------------------------------- | ||||||||||||||
| 431 | |||||||||||||||
| 432 | =head3 level() - Anzahl der übergeordneten Knoten | ||||||||||||||
| 433 | |||||||||||||||
| 434 | =head4 Synopsis | ||||||||||||||
| 435 | |||||||||||||||
| 436 | $level = $tree->level($pk); | ||||||||||||||
| 437 | $level = $tree->level($row); | ||||||||||||||
| 438 | |||||||||||||||
| 439 | =cut | ||||||||||||||
| 440 | |||||||||||||||
| 441 | # ----------------------------------------------------------------------------- | ||||||||||||||
| 442 | |||||||||||||||
| 443 | 0 | ▶ | sub level { | ||||||||||||
|
Subroutine
level: not called CC=3 SCAR=24.8
Pod
documented
| |||||||||||||||
| 444 | 0 | my ($self,$arg) = @_; | |||||||||||||
| 445 | |||||||||||||||
| 446 | 0 | ▶ | my $row = ref $arg? $arg: $self->pkIndex->get($arg); | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 447 | 0 | my $type = $self->type; | |||||||||||||
| 448 | |||||||||||||||
| 449 | 0 | my $i = 0; | |||||||||||||
| 450 | 0 | ▶ | while ($row = $row->getParent($type)) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 451 | 0 | $i++; | |||||||||||||
| 452 | } | ||||||||||||||
| 453 | |||||||||||||||
| 454 | 0 | return $i; | |||||||||||||
| 455 | } | ||||||||||||||
| 456 | |||||||||||||||
| 457 | # ----------------------------------------------------------------------------- | ||||||||||||||
| 458 | |||||||||||||||
| 459 | =head3 lookup() - Datensatz-Lookup | ||||||||||||||
| 460 | |||||||||||||||
| 461 | =head4 Synopsis | ||||||||||||||
| 462 | |||||||||||||||
| 463 | $row = $tree->lookup($pk); | ||||||||||||||
| 464 | $row = $tree->lookup($row); | ||||||||||||||
| 465 | |||||||||||||||
| 466 | =head4 Description | ||||||||||||||
| 467 | |||||||||||||||
| 468 | Liefere den Datensatz mit dem Primärschlüssel $pk. Wird ein | ||||||||||||||
| 469 | Datensatz $row übergeben, wird dieser unmittelbar | ||||||||||||||
| 470 | zurückgeliefert. Dies ist nützlich, wenn die Methode genutzt wird | ||||||||||||||
| 471 | um eine Variable zu einem Datensatz aufzulösen, die ein | ||||||||||||||
| 472 | Primärschlüssel oder Datensatz sein kann. Die Klasse selbst nutzt | ||||||||||||||
| 473 | die Methode zu diesem Zweck. | ||||||||||||||
| 474 | |||||||||||||||
| 475 | =cut | ||||||||||||||
| 476 | |||||||||||||||
| 477 | # ----------------------------------------------------------------------------- | ||||||||||||||
| 478 | |||||||||||||||
| 479 | 70 | ▶ | sub lookup { | ||||||||||||
|
Subroutine
lookup: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||
| 480 | 70 | my ($self,$arg) = @_; | |||||||||||||
| 481 | 70 | ▶ | return ref $arg? $arg: $self->pkIndex->get($arg); | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 482 | } | ||||||||||||||
| 483 | |||||||||||||||
| 484 | # ----------------------------------------------------------------------------- | ||||||||||||||
| 485 | |||||||||||||||
| 486 | =head3 parent() - Eltern-Datensatz | ||||||||||||||
| 487 | |||||||||||||||
| 488 | =head4 Synopsis | ||||||||||||||
| 489 | |||||||||||||||
| 490 | $par = $tree->parent($row); | ||||||||||||||
| 491 | $par = $tree->parent($pk); | ||||||||||||||
| 492 | |||||||||||||||
| 493 | =head4 Description | ||||||||||||||
| 494 | |||||||||||||||
| 495 | Liefere den Eltern-Datensatz zum Datensatz $row bzw. zum | ||||||||||||||
| 496 | Datensatz mit dem Primärschlüssel $pk. Besitzt der Datensatz | ||||||||||||||
| 497 | keinen Eltern-Datensatz, liefere undef. | ||||||||||||||
| 498 | |||||||||||||||
| 499 | =head4 Example | ||||||||||||||
| 500 | |||||||||||||||
| 501 | Aufruf: | ||||||||||||||
| 502 | |||||||||||||||
| 503 | $row = $tree->parent(3); | ||||||||||||||
| 504 | |||||||||||||||
| 505 | Resultat (ein Datensatz): | ||||||||||||||
| 506 | |||||||||||||||
| 507 | id parent_id name | ||||||||||||||
| 508 | -- --------- ---- | ||||||||||||||
| 509 | 2 1 B | ||||||||||||||
| 510 | |||||||||||||||
| 511 | =cut | ||||||||||||||
| 512 | |||||||||||||||
| 513 | # ----------------------------------------------------------------------------- | ||||||||||||||
| 514 | |||||||||||||||
| 515 | 35 | ▶ | sub parent { | ||||||||||||
|
Subroutine
parent: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||
| 516 | 35 | my $self = shift; | |||||||||||||
| 517 | 35 | my $row = $self->lookup(shift); | |||||||||||||
| 518 | 35 | return $row->getParent($self->type); | |||||||||||||
| 519 | } | ||||||||||||||
| 520 | |||||||||||||||
| 521 | # ----------------------------------------------------------------------------- | ||||||||||||||
| 522 | |||||||||||||||
| 523 | =head3 path() - Datensatz-Pfad (Datensatz-Liste, Wert-Liste, Zeichenkette) | ||||||||||||||
| 524 | |||||||||||||||
| 525 | =head4 Synopsis | ||||||||||||||
| 526 | |||||||||||||||
| 527 | @rows = $tree->path($row); | ||||||||||||||
| 528 | @rows = $tree->path($pk); | ||||||||||||||
| 529 | |||||||||||||||
| 530 | @values = $tree->path($row,$key); | ||||||||||||||
| 531 | @values = $tree->path($pk,$key); | ||||||||||||||
| 532 | |||||||||||||||
| 533 | $path = $tree->path($row,$key,$sep); | ||||||||||||||
| 534 | $path = $tree->path($pk,$key,$sep); | ||||||||||||||
| 535 | |||||||||||||||
| 536 | =head4 Description | ||||||||||||||
| 537 | |||||||||||||||
| 538 | Ermittele die Pfad-Datensätze, die Pfad-Werte oder den Pfad des | ||||||||||||||
| 539 | Datensatzes $row bzw. des Datensatzes mit dem Primärschlüssel $pk | ||||||||||||||
| 540 | gemäß der Datensatz-Hierarchie und liefere das Resultat zurück. | ||||||||||||||
| 541 | |||||||||||||||
| 542 | Ist Argument $key angegeben, wird die Liste der Werte des | ||||||||||||||
| 543 | Attributs $key geliefert. | ||||||||||||||
| 544 | |||||||||||||||
| 545 | Ist zusätzlich Argument $sep angegeben, wird die Liste der Werte | ||||||||||||||
| 546 | mit $sep getrennt zu einer Zeichenkette zusammengefügt. | ||||||||||||||
| 547 | |||||||||||||||
| 548 | =head4 Examples | ||||||||||||||
| 549 | |||||||||||||||
| 550 | =over 2 | ||||||||||||||
| 551 | |||||||||||||||
| 552 | =item * | ||||||||||||||
| 553 | |||||||||||||||
| 554 | Pfad als Liste von id-Werten | ||||||||||||||
| 555 | |||||||||||||||
| 556 | Aufruf: | ||||||||||||||
| 557 | |||||||||||||||
| 558 | @values = $tree->path(3,'id'); | ||||||||||||||
| 559 | |||||||||||||||
| 560 | Resultat: | ||||||||||||||
| 561 | |||||||||||||||
| 562 | (1,2,3) | ||||||||||||||
| 563 | |||||||||||||||
| 564 | Datensätze und ihre id-Wert-Pfade: | ||||||||||||||
| 565 | |||||||||||||||
| 566 | id parent_id name @values | ||||||||||||||
| 567 | -- --------- ---- ------- | ||||||||||||||
| 568 | 1 NULL A (1) | ||||||||||||||
| 569 | 2 1 B (1,2) | ||||||||||||||
| 570 | 3 2 C (1,2,3) | ||||||||||||||
| 571 | 4 1 D (1,4) | ||||||||||||||
| 572 | |||||||||||||||
| 573 | =item * | ||||||||||||||
| 574 | |||||||||||||||
| 575 | Pfad als Zeichenkette | ||||||||||||||
| 576 | |||||||||||||||
| 577 | Aufruf: | ||||||||||||||
| 578 | |||||||||||||||
| 579 | $path = $tree->path(3,'name','/'); | ||||||||||||||
| 580 | |||||||||||||||
| 581 | Resultat: | ||||||||||||||
| 582 | |||||||||||||||
| 583 | 'A/B/C' | ||||||||||||||
| 584 | |||||||||||||||
| 585 | Datensätze und ihre name-Pfade: | ||||||||||||||
| 586 | |||||||||||||||
| 587 | id parent_id name $path | ||||||||||||||
| 588 | -- --------- ---- ----- | ||||||||||||||
| 589 | 1 NULL A A | ||||||||||||||
| 590 | 2 1 B A/B | ||||||||||||||
| 591 | 3 2 C A/B/C | ||||||||||||||
| 592 | 4 1 D A/D | ||||||||||||||
| 593 | |||||||||||||||
| 594 | =back | ||||||||||||||
| 595 | |||||||||||||||
| 596 | =cut | ||||||||||||||
| 597 | |||||||||||||||
| 598 | # ----------------------------------------------------------------------------- | ||||||||||||||
| 599 | |||||||||||||||
| 600 | 13 | ▶ | sub path { | ||||||||||||
|
Subroutine
path: called CC=4 SCAR=13.9
Pod
documented
| |||||||||||||||
| 601 | 13 | my $self = shift; | |||||||||||||
| 602 | 13 | my $row = $self->lookup(shift); | |||||||||||||
| 603 | # @_: $key -or- $key,$sep | ||||||||||||||
| 604 | |||||||||||||||
| 605 | # Liste der Pfad-Datensätze | ||||||||||||||
| 606 | |||||||||||||||
| 607 | 13 | my @rows = ($row); | |||||||||||||
| 608 | 13 | ▶ | while ($row = $self->parent($row)) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 609 | 14 | unshift @rows,$row; | |||||||||||||
| 610 | } | ||||||||||||||
| 611 | 13 | ▶ | if (!@_) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 612 | 4 | return @rows; | |||||||||||||
| 613 | } | ||||||||||||||
| 614 | |||||||||||||||
| 615 | # Liste der Pfad-Werte | ||||||||||||||
| 616 | |||||||||||||||
| 617 | 9 | my $key = shift; | |||||||||||||
| 618 | 9 | my @values = map {$_->$key} @rows; | |||||||||||||
| 619 | 9 | ▶ | if (!@_) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 620 | 4 | return @values; | |||||||||||||
| 621 | } | ||||||||||||||
| 622 | |||||||||||||||
| 623 | # Pfad-Zeichenkette | ||||||||||||||
| 624 | |||||||||||||||
| 625 | 5 | my $sep = shift; | |||||||||||||
| 626 | 5 | return join($sep,@values); | |||||||||||||
| 627 | } | ||||||||||||||
| 628 | |||||||||||||||
| 629 | # ----------------------------------------------------------------------------- | ||||||||||||||
| 630 | |||||||||||||||
| 631 | =head3 rows() - Alle Datensätze (Knoten) des Baums | ||||||||||||||
| 632 | |||||||||||||||
| 633 | =head4 Synopsis | ||||||||||||||
| 634 | |||||||||||||||
| 635 | @rows|$rowA = $tree->rows; | ||||||||||||||
| 636 | |||||||||||||||
| 637 | =head4 Description | ||||||||||||||
| 638 | |||||||||||||||
| 639 | Liefere die Datensätze des Baums. Die Reihenfolge entspricht | ||||||||||||||
| 640 | der Reihenfolge der zugrundeliegenden Tabelle $tab (s. Konstruktor). | ||||||||||||||
| 641 | |||||||||||||||
| 642 | =cut | ||||||||||||||
| 643 | |||||||||||||||
| 644 | # ----------------------------------------------------------------------------- | ||||||||||||||
| 645 | |||||||||||||||
| 646 | 0 | ▶ | sub rows { | ||||||||||||
|
Subroutine
rows: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||
| 647 | 0 | shift->table->rows; | |||||||||||||
| 648 | } | ||||||||||||||
| 649 | |||||||||||||||
| 650 | # ----------------------------------------------------------------------------- | ||||||||||||||
| 651 | |||||||||||||||
| 652 | =head3 roots() - Alle Wurzel-Datensätze | ||||||||||||||
| 653 | |||||||||||||||
| 654 | =head4 Synopsis | ||||||||||||||
| 655 | |||||||||||||||
| 656 | @rows|$rowA = $tree->roots; | ||||||||||||||
| 657 | |||||||||||||||
| 658 | =head4 Description | ||||||||||||||
| 659 | |||||||||||||||
| 660 | Liefere die Liste der Wurzel-Datensätze, also alle Datensätze, die | ||||||||||||||
| 661 | keinen Parent haben. Die Reihenfolge entspricht der Reihenfolge der | ||||||||||||||
| 662 | zugrundeliegenden Tabelle $tab (s. Konstruktor). | ||||||||||||||
| 663 | |||||||||||||||
| 664 | =cut | ||||||||||||||
| 665 | |||||||||||||||
| 666 | # ----------------------------------------------------------------------------- | ||||||||||||||
| 667 | |||||||||||||||
| 668 | 0 | ▶ | sub roots { | ||||||||||||
|
Subroutine
roots: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||||
| 669 | 0 | my $self = shift; | |||||||||||||
| 670 | 0 | my @rows = grep {!$self->parent($_)} $self->rows; | |||||||||||||
| 671 | 0 | ▶ | return wantarray? @rows: \@rows; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 672 | } | ||||||||||||||
| 673 | |||||||||||||||
| 674 | # ----------------------------------------------------------------------------- | ||||||||||||||
| 675 | |||||||||||||||
| 676 | =head3 siblings() - Geschwister-Datensätze | ||||||||||||||
| 677 | |||||||||||||||
| 678 | =head4 Synopsis | ||||||||||||||
| 679 | |||||||||||||||
| 680 | @rows|$tab = $tree->siblings($row); | ||||||||||||||
| 681 | @rows|$tab = $tree->siblings($pk); | ||||||||||||||
| 682 | |||||||||||||||
| 683 | =head4 Description | ||||||||||||||
| 684 | |||||||||||||||
| 685 | Liefere die Liste der Geschwister-Datensätze zum Datensatz $row | ||||||||||||||
| 686 | bzw. zum Datensatz mit dem Primärschlüssel $pk. Besitzt der | ||||||||||||||
| 687 | Datensatz keine Geschwister-Datensätze, ist die Liste leer. | ||||||||||||||
| 688 | Im Skalarkontext liefere ein ResultSet-Objekt mit den Datensätzen. | ||||||||||||||
| 689 | |||||||||||||||
| 690 | =cut | ||||||||||||||
| 691 | |||||||||||||||
| 692 | # ----------------------------------------------------------------------------- | ||||||||||||||
| 693 | |||||||||||||||
| 694 | 4 | ▶ | sub siblings { | ||||||||||||
|
Subroutine
siblings: called CC=5 SCAR=16.2
Pod
documented
| |||||||||||||||
| 695 | 4 | my $self = shift; | |||||||||||||
| 696 | 4 | my $row = $self->lookup(shift); | |||||||||||||
| 697 | |||||||||||||||
| 698 | 4 | my @rows; | |||||||||||||
| 699 | 4 | ▶ | if (my $par = $self->parent($row)) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 700 | 3 | @rows = $self->childs($par); | |||||||||||||
| 701 | 3 | ▶ | for (my $i = 0; $i < @rows; $i++) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 702 | 4 | ▶ | if ($rows[$i] == $row) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 703 | 3 | splice @rows,$i,1; | |||||||||||||
| 704 | 3 | last; | |||||||||||||
| 705 | } | ||||||||||||||
| 706 | } | ||||||||||||||
| 707 | } | ||||||||||||||
| 708 | |||||||||||||||
| 709 | 4 | ▶ | return wantarray? @rows: $self->table->new(\@rows); | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 710 | } | ||||||||||||||
| 711 | |||||||||||||||
| 712 | # ----------------------------------------------------------------------------- | ||||||||||||||
| 713 | |||||||||||||||
| 714 | =head1 VERSION | ||||||||||||||
| 715 | |||||||||||||||
| 716 | 1.240 | ||||||||||||||
| 717 | |||||||||||||||
| 718 | =head1 AUTHOR | ||||||||||||||
| 719 | |||||||||||||||
| 720 | Frank Seitz, L<http://fseitz.de/> | ||||||||||||||
| 721 | |||||||||||||||
| 722 | =head1 COPYRIGHT | ||||||||||||||
| 723 | |||||||||||||||
| 724 | Copyright (C) 2026 Frank Seitz | ||||||||||||||
| 725 | |||||||||||||||
| 726 | =head1 LICENSE | ||||||||||||||
| 727 | |||||||||||||||
| 728 | This code is free software; you can redistribute it and/or modify | ||||||||||||||
| 729 | it under the same terms as Perl itself. | ||||||||||||||
| 730 | |||||||||||||||
| 731 | =cut | ||||||||||||||
| 732 | |||||||||||||||
| 733 | # ----------------------------------------------------------------------------- | ||||||||||||||
| 734 | |||||||||||||||
| 735 | 1 | 1; | |||||||||||||
| 736 | |||||||||||||||
| 737 | # eof | ||||||||||||||