Table.php 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Framework\Setup\Declaration\Schema\Dto;
  7. use Magento\Framework\Setup\Declaration\Schema\Dto\Constraints\Internal;
  8. use Magento\Framework\Setup\Declaration\Schema\Dto\Constraints\Reference;
  9. /**
  10. * Table structural element
  11. * Aggregate inside itself: columns, constraints and indexes
  12. * Resource is also specified on this strucural element
  13. */
  14. class Table extends GenericElement implements
  15. ElementInterface,
  16. ElementDiffAwareInterface
  17. {
  18. /**
  19. * In case if we will need to change this object: add, modify or drop, we will need
  20. * to define it by its type
  21. */
  22. const TYPE = 'table';
  23. /**
  24. * @var Constraint[]
  25. */
  26. private $constraints = [];
  27. /**
  28. * @var Column[]
  29. */
  30. private $columns = [];
  31. /**
  32. * @var string
  33. */
  34. protected $type = 'table';
  35. /**
  36. * @var Index[]
  37. */
  38. private $indexes = [];
  39. /**
  40. * @var string
  41. */
  42. private $resource;
  43. /**
  44. * @var string
  45. */
  46. private $engine;
  47. /**
  48. * @var string
  49. */
  50. private $nameWithoutPrefix;
  51. /**
  52. * @var null|string
  53. */
  54. private $comment;
  55. /**
  56. * @var string
  57. */
  58. private $onCreate;
  59. /**
  60. * @var string
  61. */
  62. private $charset;
  63. /**
  64. * @var string
  65. */
  66. private $collation;
  67. /**
  68. * @param string $name
  69. * @param string $type
  70. * @param string $nameWithoutPrefix
  71. * @param string $resource
  72. * @param string $engine
  73. * @param string $charset
  74. * @param string $collation
  75. * @param string $onCreate
  76. * @param string|null $comment
  77. * @param array $columns
  78. * @param array $indexes
  79. * @param array $constraints
  80. * @SuppressWarnings(PHPMD.ExcessiveParameterList)
  81. */
  82. public function __construct(
  83. string $name,
  84. string $type,
  85. string $nameWithoutPrefix,
  86. string $resource,
  87. string $engine,
  88. string $charset,
  89. string $collation,
  90. string $onCreate,
  91. string $comment = null,
  92. array $columns = [],
  93. array $indexes = [],
  94. array $constraints = []
  95. ) {
  96. parent::__construct($name, $type);
  97. $this->columns = $columns;
  98. $this->indexes = $indexes;
  99. $this->constraints = $constraints;
  100. $this->resource = $resource;
  101. $this->engine = $engine;
  102. $this->nameWithoutPrefix = $nameWithoutPrefix;
  103. $this->comment = $comment;
  104. $this->onCreate = $onCreate;
  105. $this->charset = $charset;
  106. $this->collation = $collation;
  107. }
  108. /**
  109. * Return different table constraints.
  110. *
  111. * It can be constraint like unique key or reference to another table, etc
  112. *
  113. * @return Constraint[]
  114. */
  115. public function getConstraints()
  116. {
  117. return $this->constraints;
  118. }
  119. /**
  120. * Get constraint by name.
  121. *
  122. * @param string $name
  123. * @return Constraint | bool
  124. */
  125. public function getConstraintByName($name)
  126. {
  127. return $this->constraints[$name] ?? false;
  128. }
  129. /**
  130. * This method lookup only for foreign keys constraints
  131. *
  132. * @return Reference[]
  133. */
  134. public function getReferenceConstraints()
  135. {
  136. $constraints = [];
  137. foreach ($this->getConstraints() as $constraint) {
  138. if ($constraint instanceof Reference) {
  139. $constraints[$constraint->getName()] = $constraint;
  140. }
  141. }
  142. return $constraints;
  143. }
  144. /**
  145. * Returns primary constraint
  146. *
  147. * As primary constraint always have one name
  148. * and can be only one for table
  149. * it name is allocated into it constraint
  150. *
  151. * @return bool|Internal
  152. */
  153. public function getPrimaryConstraint()
  154. {
  155. return $this->constraints[Internal::PRIMARY_NAME] ?? false;
  156. }
  157. /**
  158. * Retrieve internal constraints
  159. *
  160. * @return array
  161. */
  162. public function getInternalConstraints() : array
  163. {
  164. $constraints = [];
  165. foreach ($this->getConstraints() as $constraint) {
  166. if ($constraint instanceof Internal) {
  167. $constraints[] = $constraint;
  168. }
  169. }
  170. return $constraints;
  171. }
  172. /**
  173. * Get index by name
  174. *
  175. * @param string $name
  176. * @return Index | bool
  177. */
  178. public function getIndexByName($name)
  179. {
  180. return $this->indexes[$name] ?? false;
  181. }
  182. /**
  183. * Return all columns.
  184. *
  185. * Note, table always must have columns
  186. *
  187. * @return Column[]
  188. */
  189. public function getColumns()
  190. {
  191. return $this->columns;
  192. }
  193. /**
  194. * Return all indexes, that are applied to table
  195. *
  196. * @return Index[]
  197. */
  198. public function getIndexes()
  199. {
  200. return $this->indexes;
  201. }
  202. /**
  203. * Retrieve shard name, on which table will exists
  204. *
  205. * @return string
  206. */
  207. public function getResource()
  208. {
  209. return $this->resource;
  210. }
  211. /**
  212. * Add constraints
  213. *
  214. * This is workaround, as any DTO object couldnt be changed after instantiation.
  215. * However there is case, when we have 2 tables with constraints in different tables,
  216. * that depends to each other table. So we need to setup DTO first and only then pass
  217. * problematic constraints to it, in order to avoid circular dependency.
  218. *
  219. * @param Constraint[] $constraints
  220. */
  221. public function addConstraints(array $constraints)
  222. {
  223. $this->constraints = array_replace($this->constraints, $constraints);
  224. }
  225. /**
  226. * Add columns
  227. *
  228. * @param Column[] $columns
  229. */
  230. public function addColumns(array $columns)
  231. {
  232. $this->columns = array_replace($this->columns, $columns);
  233. }
  234. /**
  235. * Retrieve information about trigger
  236. *
  237. * @return string
  238. */
  239. public function getOnCreate()
  240. {
  241. return $this->onCreate;
  242. }
  243. /**
  244. * If column exists - retrieve column
  245. *
  246. * @param string $nameOrId
  247. * @return Column | bool
  248. */
  249. public function getColumnByName($nameOrId)
  250. {
  251. if (isset($this->columns[$nameOrId])) {
  252. return $this->columns[$nameOrId];
  253. }
  254. return false;
  255. }
  256. /**
  257. * Retrieve elements by specific type
  258. *
  259. * Allowed types: columns, constraints, indexes...
  260. *
  261. * @param string $type
  262. * @return ElementInterface[]
  263. */
  264. public function getElementsByType($type)
  265. {
  266. if (!isset($this->{$type})) {
  267. throw new \InvalidArgumentException(sprintf("Type %s is not defined", $type));
  268. }
  269. return $this->{$type};
  270. }
  271. /**
  272. * Add indexes
  273. *
  274. * This is workaround, as any DTO object couldnt be changed after instantiation.
  275. * However there is case, when we depends on column definition we need modify our indexes
  276. *
  277. * @param array $indexes
  278. */
  279. public function addIndexes(array $indexes)
  280. {
  281. $this->indexes = array_replace($this->indexes, $indexes);
  282. }
  283. /**
  284. * @inheritdoc
  285. */
  286. public function getElementType()
  287. {
  288. return self::TYPE;
  289. }
  290. /**
  291. * Get engine name
  292. *
  293. * @return string
  294. */
  295. public function getEngine(): string
  296. {
  297. return $this->engine;
  298. }
  299. /**
  300. * @inheritdoc
  301. */
  302. public function getDiffSensitiveParams()
  303. {
  304. return [
  305. 'resource' => $this->getResource(),
  306. 'engine' => $this->getEngine(),
  307. 'comment' => $this->getComment(),
  308. 'charset' => $this->getCharset(),
  309. 'collation' => $this->getCollation()
  310. ];
  311. }
  312. /**
  313. * Return charset of table
  314. *
  315. * @return string
  316. */
  317. public function getCharset() : string
  318. {
  319. return $this->charset;
  320. }
  321. /**
  322. * Return charset of table
  323. *
  324. * @return string
  325. */
  326. public function getCollation() : string
  327. {
  328. return $this->collation;
  329. }
  330. /**
  331. * Get name without prefix
  332. *
  333. * @return string
  334. */
  335. public function getNameWithoutPrefix(): string
  336. {
  337. return $this->nameWithoutPrefix;
  338. }
  339. /**
  340. * Get comment
  341. *
  342. * @return null|string
  343. */
  344. public function getComment()
  345. {
  346. return $this->comment;
  347. }
  348. }