Table.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. <?php
  2. /**
  3. * This file is part of the Dibi, smart database abstraction layer (https://dibiphp.com)
  4. * Copyright (c) 2005 David Grudl (https://davidgrudl.com)
  5. */
  6. declare(strict_types=1);
  7. namespace Dibi\Reflection;
  8. use Dibi;
  9. /**
  10. * Reflection metadata class for a database table.
  11. *
  12. * @property-read string $name
  13. * @property-read bool $view
  14. * @property-read array $columns
  15. * @property-read array $columnNames
  16. * @property-read array $foreignKeys
  17. * @property-read array $indexes
  18. * @property-read Index $primaryKey
  19. */
  20. class Table
  21. {
  22. use Dibi\Strict;
  23. /** @var Dibi\Reflector */
  24. private $reflector;
  25. /** @var string */
  26. private $name;
  27. /** @var bool */
  28. private $view;
  29. /** @var Column[]|null */
  30. private $columns;
  31. /** @var ForeignKey[]|null */
  32. private $foreignKeys;
  33. /** @var Index[]|null */
  34. private $indexes;
  35. /** @var Index|null */
  36. private $primaryKey;
  37. public function __construct(Dibi\Reflector $reflector, array $info)
  38. {
  39. $this->reflector = $reflector;
  40. $this->name = $info['name'];
  41. $this->view = !empty($info['view']);
  42. }
  43. public function getName(): string
  44. {
  45. return $this->name;
  46. }
  47. public function isView(): bool
  48. {
  49. return $this->view;
  50. }
  51. /** @return Column[] */
  52. public function getColumns(): array
  53. {
  54. $this->initColumns();
  55. return array_values($this->columns);
  56. }
  57. /** @return string[] */
  58. public function getColumnNames(): array
  59. {
  60. $this->initColumns();
  61. $res = [];
  62. foreach ($this->columns as $column) {
  63. $res[] = $column->getName();
  64. }
  65. return $res;
  66. }
  67. public function getColumn(string $name): Column
  68. {
  69. $this->initColumns();
  70. $l = strtolower($name);
  71. if (isset($this->columns[$l])) {
  72. return $this->columns[$l];
  73. } else {
  74. throw new Dibi\Exception("Table '$this->name' has no column '$name'.");
  75. }
  76. }
  77. public function hasColumn(string $name): bool
  78. {
  79. $this->initColumns();
  80. return isset($this->columns[strtolower($name)]);
  81. }
  82. /** @return ForeignKey[] */
  83. public function getForeignKeys(): array
  84. {
  85. $this->initForeignKeys();
  86. return $this->foreignKeys;
  87. }
  88. /** @return Index[] */
  89. public function getIndexes(): array
  90. {
  91. $this->initIndexes();
  92. return $this->indexes;
  93. }
  94. public function getPrimaryKey(): Index
  95. {
  96. $this->initIndexes();
  97. return $this->primaryKey;
  98. }
  99. protected function initColumns(): void
  100. {
  101. if ($this->columns === null) {
  102. $this->columns = [];
  103. foreach ($this->reflector->getColumns($this->name) as $info) {
  104. $this->columns[strtolower($info['name'])] = new Column($this->reflector, $info);
  105. }
  106. }
  107. }
  108. protected function initIndexes(): void
  109. {
  110. if ($this->indexes === null) {
  111. $this->initColumns();
  112. $this->indexes = [];
  113. foreach ($this->reflector->getIndexes($this->name) as $info) {
  114. foreach ($info['columns'] as $key => $name) {
  115. $info['columns'][$key] = $this->columns[strtolower($name)];
  116. }
  117. $this->indexes[strtolower($info['name'])] = new Index($info);
  118. if (!empty($info['primary'])) {
  119. $this->primaryKey = $this->indexes[strtolower($info['name'])];
  120. }
  121. }
  122. }
  123. }
  124. protected function initForeignKeys(): void
  125. {
  126. throw new Dibi\NotImplementedException;
  127. }
  128. }