|
| 1 | +<?php |
| 2 | + |
| 3 | +/** |
| 4 | + * This file is part of the Nette Framework (https://nette.org) |
| 5 | + * Copyright (c) 2004 David Grudl (https://davidgrudl.com) |
| 6 | + */ |
| 7 | + |
| 8 | +declare(strict_types=1); |
| 9 | + |
| 10 | +namespace Nette\Database\Reflection; |
| 11 | + |
| 12 | +use Nette\Database\Reflection; |
| 13 | + |
| 14 | + |
| 15 | +/** |
| 16 | + * Table reflection. |
| 17 | + */ |
| 18 | +final class Table |
| 19 | +{ |
| 20 | + /** @var array<string, Column> */ |
| 21 | + public readonly array $columns; |
| 22 | + |
| 23 | + /** @var list<Index> */ |
| 24 | + public readonly array $indexes; |
| 25 | + public readonly ?Index $primaryKey; |
| 26 | + |
| 27 | + /** @var list<ForeignKey> */ |
| 28 | + public readonly array $foreignKeys; |
| 29 | + |
| 30 | + |
| 31 | + /** @internal */ |
| 32 | + public function __construct( |
| 33 | + private readonly Reflection $reflection, |
| 34 | + public readonly string $name, |
| 35 | + public readonly bool $view = false, |
| 36 | + public readonly ?string $fullName = null, |
| 37 | + ) { |
| 38 | + unset($this->columns, $this->indexes, $this->primaryKey, $this->foreignKeys); |
| 39 | + } |
| 40 | + |
| 41 | + |
| 42 | + public function getColumn(string $name): Column |
| 43 | + { |
| 44 | + return $this->columns[$name] ?? throw new \InvalidArgumentException("Column '$name' not found in table '$this->name'."); |
| 45 | + } |
| 46 | + |
| 47 | + |
| 48 | + private function initColumns(): void |
| 49 | + { |
| 50 | + $res = []; |
| 51 | + foreach ($this->reflection->getDriver()->getColumns($this->name) as $row) { |
| 52 | + $res[$row['name']] = new Column($row['name'], $this, $row['nativetype'], $row['size'], $row['nullable'], $row['default'], $row['autoincrement'], $row['primary'], $row['vendor']); |
| 53 | + } |
| 54 | + $this->columns = $res; |
| 55 | + } |
| 56 | + |
| 57 | + |
| 58 | + private function initIndexes(): void |
| 59 | + { |
| 60 | + $this->indexes = array_map( |
| 61 | + fn($row) => new Index( |
| 62 | + array_map(fn($name) => $this->getColumn($name), $row['columns']), |
| 63 | + $row['unique'], |
| 64 | + $row['primary'], |
| 65 | + is_string($row['name']) ? $row['name'] : null, |
| 66 | + ), |
| 67 | + $this->reflection->getDriver()->getIndexes($this->name), |
| 68 | + ); |
| 69 | + } |
| 70 | + |
| 71 | + |
| 72 | + private function initPrimaryKey(): void |
| 73 | + { |
| 74 | + $res = array_filter( |
| 75 | + $this->columns, |
| 76 | + fn($row) => $row->primary, |
| 77 | + ); |
| 78 | + $this->primaryKey = $res ? new Index(array_values($res), true, true) : null; |
| 79 | + } |
| 80 | + |
| 81 | + |
| 82 | + private function initForeignKeys(): void |
| 83 | + { |
| 84 | + $tmp = []; |
| 85 | + foreach ($this->reflection->getDriver()->getForeignKeys($this->name) as $row) { |
| 86 | + $id = $row['name']; |
| 87 | + $foreignTable = $this->reflection->getTable($row['table']); |
| 88 | + $tmp[$id][0] = $foreignTable; |
| 89 | + $tmp[$id][1][] = $this->getColumn($row['local']); |
| 90 | + $tmp[$id][2][] = $foreignTable->getColumn($row['foreign']); |
| 91 | + $tmp[$id][3] = is_string($id) ? $id : null; |
| 92 | + } |
| 93 | + $this->foreignKeys = array_map(fn($row) => new ForeignKey(...$row), array_values($tmp)); |
| 94 | + } |
| 95 | + |
| 96 | + |
| 97 | + public function __get($name): mixed |
| 98 | + { |
| 99 | + match ($name) { |
| 100 | + 'columns' => $this->initColumns(), |
| 101 | + 'indexes' => $this->initIndexes(), |
| 102 | + 'primaryKey' => $this->initPrimaryKey(), |
| 103 | + 'foreignKeys' => $this->initForeignKeys(), |
| 104 | + default => throw new \LogicException("Undefined property '$name'."), |
| 105 | + }; |
| 106 | + return $this->$name; |
| 107 | + } |
| 108 | + |
| 109 | + |
| 110 | + public function __toString(): string |
| 111 | + { |
| 112 | + return $this->name; |
| 113 | + } |
| 114 | +} |
0 commit comments