Table.php 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850
  1. <?php
  2. /*
  3. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  4. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  5. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  6. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  7. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  8. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  9. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  10. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  11. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  12. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  13. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  14. *
  15. * This software consists of voluntary contributions made by many individuals
  16. * and is licensed under the MIT license. For more information, see
  17. * <http://www.doctrine-project.org>.
  18. */
  19. namespace Doctrine\DBAL\Schema;
  20. use Doctrine\DBAL\Types\Type;
  21. use Doctrine\DBAL\Schema\Visitor\Visitor;
  22. use Doctrine\DBAL\DBALException;
  23. /**
  24. * Object Representation of a table.
  25. *
  26. * @link www.doctrine-project.org
  27. * @since 2.0
  28. * @author Benjamin Eberlei <kontakt@beberlei.de>
  29. */
  30. class Table extends AbstractAsset
  31. {
  32. /**
  33. * @var string
  34. */
  35. protected $_name = null;
  36. /**
  37. * @var Column[]
  38. */
  39. protected $_columns = array();
  40. /**
  41. * @var Index[]
  42. */
  43. private $implicitIndexes = array();
  44. /**
  45. * @var Index[]
  46. */
  47. protected $_indexes = array();
  48. /**
  49. * @var string
  50. */
  51. protected $_primaryKeyName = false;
  52. /**
  53. * @var ForeignKeyConstraint[]
  54. */
  55. protected $_fkConstraints = array();
  56. /**
  57. * @var array
  58. */
  59. protected $_options = array();
  60. /**
  61. * @var SchemaConfig
  62. */
  63. protected $_schemaConfig = null;
  64. /**
  65. * @param string $tableName
  66. * @param Column[] $columns
  67. * @param Index[] $indexes
  68. * @param ForeignKeyConstraint[] $fkConstraints
  69. * @param integer $idGeneratorType
  70. * @param array $options
  71. *
  72. * @throws DBALException
  73. */
  74. public function __construct($tableName, array $columns=array(), array $indexes=array(), array $fkConstraints=array(), $idGeneratorType = 0, array $options=array())
  75. {
  76. if (strlen($tableName) == 0) {
  77. throw DBALException::invalidTableName($tableName);
  78. }
  79. $this->_setName($tableName);
  80. foreach ($columns as $column) {
  81. $this->_addColumn($column);
  82. }
  83. foreach ($indexes as $idx) {
  84. $this->_addIndex($idx);
  85. }
  86. foreach ($fkConstraints as $constraint) {
  87. $this->_addForeignKeyConstraint($constraint);
  88. }
  89. $this->_options = $options;
  90. }
  91. /**
  92. * @param SchemaConfig $schemaConfig
  93. *
  94. * @return void
  95. */
  96. public function setSchemaConfig(SchemaConfig $schemaConfig)
  97. {
  98. $this->_schemaConfig = $schemaConfig;
  99. }
  100. /**
  101. * @return integer
  102. */
  103. protected function _getMaxIdentifierLength()
  104. {
  105. if ($this->_schemaConfig instanceof SchemaConfig) {
  106. return $this->_schemaConfig->getMaxIdentifierLength();
  107. } else {
  108. return 63;
  109. }
  110. }
  111. /**
  112. * Sets the Primary Key.
  113. *
  114. * @param array $columns
  115. * @param string|boolean $indexName
  116. *
  117. * @return self
  118. */
  119. public function setPrimaryKey(array $columns, $indexName = false)
  120. {
  121. $this->_addIndex($this->_createIndex($columns, $indexName ?: "primary", true, true));
  122. foreach ($columns as $columnName) {
  123. $column = $this->getColumn($columnName);
  124. $column->setNotnull(true);
  125. }
  126. return $this;
  127. }
  128. /**
  129. * @param array $columnNames
  130. * @param string|null $indexName
  131. * @param array $flags
  132. * @param array $options
  133. *
  134. * @return self
  135. */
  136. public function addIndex(array $columnNames, $indexName = null, array $flags = array(), array $options = array())
  137. {
  138. if ($indexName == null) {
  139. $indexName = $this->_generateIdentifierName(
  140. array_merge(array($this->getName()), $columnNames), "idx", $this->_getMaxIdentifierLength()
  141. );
  142. }
  143. return $this->_addIndex($this->_createIndex($columnNames, $indexName, false, false, $flags, $options));
  144. }
  145. /**
  146. * Drops the primary key from this table.
  147. *
  148. * @return void
  149. */
  150. public function dropPrimaryKey()
  151. {
  152. $this->dropIndex($this->_primaryKeyName);
  153. $this->_primaryKeyName = false;
  154. }
  155. /**
  156. * Drops an index from this table.
  157. *
  158. * @param string $indexName The index name.
  159. *
  160. * @return void
  161. *
  162. * @throws SchemaException If the index does not exist.
  163. */
  164. public function dropIndex($indexName)
  165. {
  166. $indexName = $this->normalizeIdentifier($indexName);
  167. if ( ! $this->hasIndex($indexName)) {
  168. throw SchemaException::indexDoesNotExist($indexName, $this->_name);
  169. }
  170. unset($this->_indexes[$indexName]);
  171. }
  172. /**
  173. * @param array $columnNames
  174. * @param string|null $indexName
  175. * @param array $options
  176. *
  177. * @return self
  178. */
  179. public function addUniqueIndex(array $columnNames, $indexName = null, array $options = array())
  180. {
  181. if ($indexName === null) {
  182. $indexName = $this->_generateIdentifierName(
  183. array_merge(array($this->getName()), $columnNames), "uniq", $this->_getMaxIdentifierLength()
  184. );
  185. }
  186. return $this->_addIndex($this->_createIndex($columnNames, $indexName, true, false, array(), $options));
  187. }
  188. /**
  189. * Renames an index.
  190. *
  191. * @param string $oldIndexName The name of the index to rename from.
  192. * @param string|null $newIndexName The name of the index to rename to.
  193. * If null is given, the index name will be auto-generated.
  194. *
  195. * @return self This table instance.
  196. *
  197. * @throws SchemaException if no index exists for the given current name
  198. * or if an index with the given new name already exists on this table.
  199. */
  200. public function renameIndex($oldIndexName, $newIndexName = null)
  201. {
  202. $oldIndexName = $this->normalizeIdentifier($oldIndexName);
  203. $normalizedNewIndexName = $this->normalizeIdentifier($newIndexName);
  204. if ($oldIndexName === $normalizedNewIndexName) {
  205. return $this;
  206. }
  207. if ( ! $this->hasIndex($oldIndexName)) {
  208. throw SchemaException::indexDoesNotExist($oldIndexName, $this->_name);
  209. }
  210. if ($this->hasIndex($normalizedNewIndexName)) {
  211. throw SchemaException::indexAlreadyExists($normalizedNewIndexName, $this->_name);
  212. }
  213. $oldIndex = $this->_indexes[$oldIndexName];
  214. if ($oldIndex->isPrimary()) {
  215. $this->dropPrimaryKey();
  216. return $this->setPrimaryKey($oldIndex->getColumns(), $newIndexName);
  217. }
  218. unset($this->_indexes[$oldIndexName]);
  219. if ($oldIndex->isUnique()) {
  220. return $this->addUniqueIndex($oldIndex->getColumns(), $newIndexName, $oldIndex->getOptions());
  221. }
  222. return $this->addIndex($oldIndex->getColumns(), $newIndexName, $oldIndex->getFlags(), $oldIndex->getOptions());
  223. }
  224. /**
  225. * Checks if an index begins in the order of the given columns.
  226. *
  227. * @param array $columnsNames
  228. *
  229. * @return boolean
  230. */
  231. public function columnsAreIndexed(array $columnsNames)
  232. {
  233. foreach ($this->getIndexes() as $index) {
  234. /* @var $index Index */
  235. if ($index->spansColumns($columnsNames)) {
  236. return true;
  237. }
  238. }
  239. return false;
  240. }
  241. /**
  242. * @param array $columnNames
  243. * @param string $indexName
  244. * @param boolean $isUnique
  245. * @param boolean $isPrimary
  246. * @param array $flags
  247. * @param array $options
  248. *
  249. * @return Index
  250. *
  251. * @throws SchemaException
  252. */
  253. private function _createIndex(array $columnNames, $indexName, $isUnique, $isPrimary, array $flags = array(), array $options = array())
  254. {
  255. if (preg_match('(([^a-zA-Z0-9_]+))', $this->normalizeIdentifier($indexName))) {
  256. throw SchemaException::indexNameInvalid($indexName);
  257. }
  258. foreach ($columnNames as $columnName => $indexColOptions) {
  259. if (is_numeric($columnName) && is_string($indexColOptions)) {
  260. $columnName = $indexColOptions;
  261. }
  262. if ( ! $this->hasColumn($columnName)) {
  263. throw SchemaException::columnDoesNotExist($columnName, $this->_name);
  264. }
  265. }
  266. return new Index($indexName, $columnNames, $isUnique, $isPrimary, $flags, $options);
  267. }
  268. /**
  269. * @param string $columnName
  270. * @param string $typeName
  271. * @param array $options
  272. *
  273. * @return Column
  274. */
  275. public function addColumn($columnName, $typeName, array $options=array())
  276. {
  277. $column = new Column($columnName, Type::getType($typeName), $options);
  278. $this->_addColumn($column);
  279. return $column;
  280. }
  281. /**
  282. * Renames a Column.
  283. *
  284. * @param string $oldColumnName
  285. * @param string $newColumnName
  286. *
  287. * @return self
  288. *
  289. * @throws DBALException
  290. */
  291. public function renameColumn($oldColumnName, $newColumnName)
  292. {
  293. throw new DBALException("Table#renameColumn() was removed, because it drops and recreates " .
  294. "the column instead. There is no fix available, because a schema diff cannot reliably detect if a " .
  295. "column was renamed or one column was created and another one dropped.");
  296. }
  297. /**
  298. * Change Column Details.
  299. *
  300. * @param string $columnName
  301. * @param array $options
  302. *
  303. * @return self
  304. */
  305. public function changeColumn($columnName, array $options)
  306. {
  307. $column = $this->getColumn($columnName);
  308. $column->setOptions($options);
  309. return $this;
  310. }
  311. /**
  312. * Drops a Column from the Table.
  313. *
  314. * @param string $columnName
  315. *
  316. * @return self
  317. */
  318. public function dropColumn($columnName)
  319. {
  320. $columnName = $this->normalizeIdentifier($columnName);
  321. unset($this->_columns[$columnName]);
  322. return $this;
  323. }
  324. /**
  325. * Adds a foreign key constraint.
  326. *
  327. * Name is inferred from the local columns.
  328. *
  329. * @param Table|string $foreignTable Table schema instance or table name
  330. * @param array $localColumnNames
  331. * @param array $foreignColumnNames
  332. * @param array $options
  333. * @param string|null $constraintName
  334. *
  335. * @return self
  336. */
  337. public function addForeignKeyConstraint($foreignTable, array $localColumnNames, array $foreignColumnNames, array $options=array(), $constraintName = null)
  338. {
  339. $constraintName = $constraintName ?: $this->_generateIdentifierName(array_merge((array) $this->getName(), $localColumnNames), "fk", $this->_getMaxIdentifierLength());
  340. return $this->addNamedForeignKeyConstraint($constraintName, $foreignTable, $localColumnNames, $foreignColumnNames, $options);
  341. }
  342. /**
  343. * Adds a foreign key constraint.
  344. *
  345. * Name is to be generated by the database itself.
  346. *
  347. * @deprecated Use {@link addForeignKeyConstraint}
  348. *
  349. * @param Table|string $foreignTable Table schema instance or table name
  350. * @param array $localColumnNames
  351. * @param array $foreignColumnNames
  352. * @param array $options
  353. *
  354. * @return self
  355. */
  356. public function addUnnamedForeignKeyConstraint($foreignTable, array $localColumnNames, array $foreignColumnNames, array $options=array())
  357. {
  358. return $this->addForeignKeyConstraint($foreignTable, $localColumnNames, $foreignColumnNames, $options);
  359. }
  360. /**
  361. * Adds a foreign key constraint with a given name.
  362. *
  363. * @deprecated Use {@link addForeignKeyConstraint}
  364. *
  365. * @param string $name
  366. * @param Table|string $foreignTable Table schema instance or table name
  367. * @param array $localColumnNames
  368. * @param array $foreignColumnNames
  369. * @param array $options
  370. *
  371. * @return self
  372. *
  373. * @throws SchemaException
  374. */
  375. public function addNamedForeignKeyConstraint($name, $foreignTable, array $localColumnNames, array $foreignColumnNames, array $options=array())
  376. {
  377. if ($foreignTable instanceof Table) {
  378. foreach ($foreignColumnNames as $columnName) {
  379. if ( ! $foreignTable->hasColumn($columnName)) {
  380. throw SchemaException::columnDoesNotExist($columnName, $foreignTable->getName());
  381. }
  382. }
  383. }
  384. foreach ($localColumnNames as $columnName) {
  385. if ( ! $this->hasColumn($columnName)) {
  386. throw SchemaException::columnDoesNotExist($columnName, $this->_name);
  387. }
  388. }
  389. $constraint = new ForeignKeyConstraint(
  390. $localColumnNames, $foreignTable, $foreignColumnNames, $name, $options
  391. );
  392. $this->_addForeignKeyConstraint($constraint);
  393. return $this;
  394. }
  395. /**
  396. * @param string $name
  397. * @param string $value
  398. *
  399. * @return self
  400. */
  401. public function addOption($name, $value)
  402. {
  403. $this->_options[$name] = $value;
  404. return $this;
  405. }
  406. /**
  407. * @param Column $column
  408. *
  409. * @return void
  410. *
  411. * @throws SchemaException
  412. */
  413. protected function _addColumn(Column $column)
  414. {
  415. $columnName = $column->getName();
  416. $columnName = $this->normalizeIdentifier($columnName);
  417. if (isset($this->_columns[$columnName])) {
  418. throw SchemaException::columnAlreadyExists($this->getName(), $columnName);
  419. }
  420. $this->_columns[$columnName] = $column;
  421. }
  422. /**
  423. * Adds an index to the table.
  424. *
  425. * @param Index $indexCandidate
  426. *
  427. * @return self
  428. *
  429. * @throws SchemaException
  430. */
  431. protected function _addIndex(Index $indexCandidate)
  432. {
  433. $indexName = $indexCandidate->getName();
  434. $indexName = $this->normalizeIdentifier($indexName);
  435. $replacedImplicitIndexes = array();
  436. foreach ($this->implicitIndexes as $name => $implicitIndex) {
  437. if ($implicitIndex->isFullfilledBy($indexCandidate) && isset($this->_indexes[$name])) {
  438. $replacedImplicitIndexes[] = $name;
  439. }
  440. }
  441. if ((isset($this->_indexes[$indexName]) && ! in_array($indexName, $replacedImplicitIndexes, true)) ||
  442. ($this->_primaryKeyName != false && $indexCandidate->isPrimary())
  443. ) {
  444. throw SchemaException::indexAlreadyExists($indexName, $this->_name);
  445. }
  446. foreach ($replacedImplicitIndexes as $name) {
  447. unset($this->_indexes[$name], $this->implicitIndexes[$name]);
  448. }
  449. if ($indexCandidate->isPrimary()) {
  450. $this->_primaryKeyName = $indexName;
  451. }
  452. $this->_indexes[$indexName] = $indexCandidate;
  453. return $this;
  454. }
  455. /**
  456. * @param ForeignKeyConstraint $constraint
  457. *
  458. * @return void
  459. */
  460. protected function _addForeignKeyConstraint(ForeignKeyConstraint $constraint)
  461. {
  462. $constraint->setLocalTable($this);
  463. if (strlen($constraint->getName())) {
  464. $name = $constraint->getName();
  465. } else {
  466. $name = $this->_generateIdentifierName(
  467. array_merge((array) $this->getName(), $constraint->getLocalColumns()), "fk", $this->_getMaxIdentifierLength()
  468. );
  469. }
  470. $name = $this->normalizeIdentifier($name);
  471. $this->_fkConstraints[$name] = $constraint;
  472. // add an explicit index on the foreign key columns. If there is already an index that fulfils this requirements drop the request.
  473. // In the case of __construct calling this method during hydration from schema-details all the explicitly added indexes
  474. // lead to duplicates. This creates computation overhead in this case, however no duplicate indexes are ever added (based on columns).
  475. $indexName = $this->_generateIdentifierName(
  476. array_merge(array($this->getName()), $constraint->getColumns()),
  477. "idx",
  478. $this->_getMaxIdentifierLength()
  479. );
  480. $indexCandidate = $this->_createIndex($constraint->getColumns(), $indexName, false, false);
  481. foreach ($this->_indexes as $existingIndex) {
  482. if ($indexCandidate->isFullfilledBy($existingIndex)) {
  483. return;
  484. }
  485. }
  486. $this->_addIndex($indexCandidate);
  487. $this->implicitIndexes[$this->normalizeIdentifier($indexName)] = $indexCandidate;
  488. }
  489. /**
  490. * Returns whether this table has a foreign key constraint with the given name.
  491. *
  492. * @param string $constraintName
  493. *
  494. * @return boolean
  495. */
  496. public function hasForeignKey($constraintName)
  497. {
  498. $constraintName = $this->normalizeIdentifier($constraintName);
  499. return isset($this->_fkConstraints[$constraintName]);
  500. }
  501. /**
  502. * Returns the foreign key constraint with the given name.
  503. *
  504. * @param string $constraintName The constraint name.
  505. *
  506. * @return ForeignKeyConstraint
  507. *
  508. * @throws SchemaException If the foreign key does not exist.
  509. */
  510. public function getForeignKey($constraintName)
  511. {
  512. $constraintName = $this->normalizeIdentifier($constraintName);
  513. if (!$this->hasForeignKey($constraintName)) {
  514. throw SchemaException::foreignKeyDoesNotExist($constraintName, $this->_name);
  515. }
  516. return $this->_fkConstraints[$constraintName];
  517. }
  518. /**
  519. * Removes the foreign key constraint with the given name.
  520. *
  521. * @param string $constraintName The constraint name.
  522. *
  523. * @return void
  524. *
  525. * @throws SchemaException
  526. */
  527. public function removeForeignKey($constraintName)
  528. {
  529. $constraintName = $this->normalizeIdentifier($constraintName);
  530. if (!$this->hasForeignKey($constraintName)) {
  531. throw SchemaException::foreignKeyDoesNotExist($constraintName, $this->_name);
  532. }
  533. unset($this->_fkConstraints[$constraintName]);
  534. }
  535. /**
  536. * @return Column[]
  537. */
  538. public function getColumns()
  539. {
  540. $columns = $this->_columns;
  541. $pkCols = array();
  542. $fkCols = array();
  543. if ($this->hasPrimaryKey()) {
  544. $pkCols = $this->getPrimaryKey()->getColumns();
  545. }
  546. foreach ($this->getForeignKeys() as $fk) {
  547. /* @var $fk ForeignKeyConstraint */
  548. $fkCols = array_merge($fkCols, $fk->getColumns());
  549. }
  550. $colNames = array_unique(array_merge($pkCols, $fkCols, array_keys($columns)));
  551. uksort($columns, function ($a, $b) use ($colNames) {
  552. return (array_search($a, $colNames) >= array_search($b, $colNames));
  553. });
  554. return $columns;
  555. }
  556. /**
  557. * Returns whether this table has a Column with the given name.
  558. *
  559. * @param string $columnName The column name.
  560. *
  561. * @return boolean
  562. */
  563. public function hasColumn($columnName)
  564. {
  565. $columnName = $this->normalizeIdentifier($columnName);
  566. return isset($this->_columns[$columnName]);
  567. }
  568. /**
  569. * Returns the Column with the given name.
  570. *
  571. * @param string $columnName The column name.
  572. *
  573. * @return Column
  574. *
  575. * @throws SchemaException If the column does not exist.
  576. */
  577. public function getColumn($columnName)
  578. {
  579. $columnName = $this->normalizeIdentifier($columnName);
  580. if ( ! $this->hasColumn($columnName)) {
  581. throw SchemaException::columnDoesNotExist($columnName, $this->_name);
  582. }
  583. return $this->_columns[$columnName];
  584. }
  585. /**
  586. * Returns the primary key.
  587. *
  588. * @return Index|null The primary key, or null if this Table has no primary key.
  589. */
  590. public function getPrimaryKey()
  591. {
  592. if ( ! $this->hasPrimaryKey()) {
  593. return null;
  594. }
  595. return $this->getIndex($this->_primaryKeyName);
  596. }
  597. /**
  598. * Returns the primary key columns.
  599. *
  600. * @return array
  601. *
  602. * @throws DBALException
  603. */
  604. public function getPrimaryKeyColumns()
  605. {
  606. if ( ! $this->hasPrimaryKey()) {
  607. throw new DBALException("Table " . $this->getName() . " has no primary key.");
  608. }
  609. return $this->getPrimaryKey()->getColumns();
  610. }
  611. /**
  612. * Returns whether this table has a primary key.
  613. *
  614. * @return boolean
  615. */
  616. public function hasPrimaryKey()
  617. {
  618. return ($this->_primaryKeyName && $this->hasIndex($this->_primaryKeyName));
  619. }
  620. /**
  621. * Returns whether this table has an Index with the given name.
  622. *
  623. * @param string $indexName The index name.
  624. *
  625. * @return boolean
  626. */
  627. public function hasIndex($indexName)
  628. {
  629. $indexName = $this->normalizeIdentifier($indexName);
  630. return (isset($this->_indexes[$indexName]));
  631. }
  632. /**
  633. * Returns the Index with the given name.
  634. *
  635. * @param string $indexName The index name.
  636. *
  637. * @return Index
  638. *
  639. * @throws SchemaException If the index does not exist.
  640. */
  641. public function getIndex($indexName)
  642. {
  643. $indexName = $this->normalizeIdentifier($indexName);
  644. if ( ! $this->hasIndex($indexName)) {
  645. throw SchemaException::indexDoesNotExist($indexName, $this->_name);
  646. }
  647. return $this->_indexes[$indexName];
  648. }
  649. /**
  650. * @return Index[]
  651. */
  652. public function getIndexes()
  653. {
  654. return $this->_indexes;
  655. }
  656. /**
  657. * Returns the foreign key constraints.
  658. *
  659. * @return ForeignKeyConstraint[]
  660. */
  661. public function getForeignKeys()
  662. {
  663. return $this->_fkConstraints;
  664. }
  665. /**
  666. * @param string $name
  667. *
  668. * @return boolean
  669. */
  670. public function hasOption($name)
  671. {
  672. return isset($this->_options[$name]);
  673. }
  674. /**
  675. * @param string $name
  676. *
  677. * @return mixed
  678. */
  679. public function getOption($name)
  680. {
  681. return $this->_options[$name];
  682. }
  683. /**
  684. * @return array
  685. */
  686. public function getOptions()
  687. {
  688. return $this->_options;
  689. }
  690. /**
  691. * @param Visitor $visitor
  692. *
  693. * @return void
  694. */
  695. public function visit(Visitor $visitor)
  696. {
  697. $visitor->acceptTable($this);
  698. foreach ($this->getColumns() as $column) {
  699. $visitor->acceptColumn($this, $column);
  700. }
  701. foreach ($this->getIndexes() as $index) {
  702. $visitor->acceptIndex($this, $index);
  703. }
  704. foreach ($this->getForeignKeys() as $constraint) {
  705. $visitor->acceptForeignKey($this, $constraint);
  706. }
  707. }
  708. /**
  709. * Clone of a Table triggers a deep clone of all affected assets.
  710. *
  711. * @return void
  712. */
  713. public function __clone()
  714. {
  715. foreach ($this->_columns as $k => $column) {
  716. $this->_columns[$k] = clone $column;
  717. }
  718. foreach ($this->_indexes as $k => $index) {
  719. $this->_indexes[$k] = clone $index;
  720. }
  721. foreach ($this->_fkConstraints as $k => $fk) {
  722. $this->_fkConstraints[$k] = clone $fk;
  723. $this->_fkConstraints[$k]->setLocalTable($this);
  724. }
  725. }
  726. /**
  727. * Normalizes a given identifier.
  728. *
  729. * Trims quotes and lowercases the given identifier.
  730. *
  731. * @param string $identifier The identifier to normalize.
  732. *
  733. * @return string The normalized identifier.
  734. */
  735. private function normalizeIdentifier($identifier)
  736. {
  737. return $this->trimQuotes(strtolower($identifier));
  738. }
  739. }