Collection.php 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * CakePHP(tm) : Rapid Development Framework (https://cakephp.org)
  5. * Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
  6. *
  7. * Licensed under The MIT License
  8. * For full copyright and license information, please see the LICENSE.txt
  9. * Redistributions of files must retain the above copyright notice.
  10. *
  11. * @copyright Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
  12. * @link https://cakephp.org CakePHP(tm) Project
  13. * @since 3.0.0
  14. * @license https://opensource.org/licenses/mit-license.php MIT License
  15. */
  16. namespace Cake\Database\Schema;
  17. use Cake\Database\Connection;
  18. use Cake\Database\Exception;
  19. use PDOException;
  20. /**
  21. * Represents a database schema collection
  22. *
  23. * Used to access information about the tables,
  24. * and other data in a database.
  25. */
  26. class Collection implements CollectionInterface
  27. {
  28. /**
  29. * Connection object
  30. *
  31. * @var \Cake\Database\Connection
  32. */
  33. protected $_connection;
  34. /**
  35. * Schema dialect instance.
  36. *
  37. * @var \Cake\Database\Schema\BaseSchema
  38. */
  39. protected $_dialect;
  40. /**
  41. * Constructor.
  42. *
  43. * @param \Cake\Database\Connection $connection The connection instance.
  44. */
  45. public function __construct(Connection $connection)
  46. {
  47. $this->_connection = $connection;
  48. $this->_dialect = $connection->getDriver()->schemaDialect();
  49. }
  50. /**
  51. * Get the list of tables available in the current connection.
  52. *
  53. * @return string[] The list of tables in the connected database/schema.
  54. */
  55. public function listTables(): array
  56. {
  57. [$sql, $params] = $this->_dialect->listTablesSql($this->_connection->config());
  58. $result = [];
  59. $statement = $this->_connection->execute($sql, $params);
  60. while ($row = $statement->fetch()) {
  61. $result[] = $row[0];
  62. }
  63. $statement->closeCursor();
  64. return $result;
  65. }
  66. /**
  67. * Get the column metadata for a table.
  68. *
  69. * The name can include a database schema name in the form 'schema.table'.
  70. *
  71. * Caching will be applied if `cacheMetadata` key is present in the Connection
  72. * configuration options. Defaults to _cake_model_ when true.
  73. *
  74. * ### Options
  75. *
  76. * - `forceRefresh` - Set to true to force rebuilding the cached metadata.
  77. * Defaults to false.
  78. *
  79. * @param string $name The name of the table to describe.
  80. * @param array $options The options to use, see above.
  81. * @return \Cake\Database\Schema\TableSchema Object with column metadata.
  82. * @throws \Cake\Database\Exception when table cannot be described.
  83. */
  84. public function describe(string $name, array $options = []): TableSchemaInterface
  85. {
  86. $config = $this->_connection->config();
  87. if (strpos($name, '.')) {
  88. [$config['schema'], $name] = explode('.', $name);
  89. }
  90. $table = $this->_connection->getDriver()->newTableSchema($name);
  91. $this->_reflect('Column', $name, $config, $table);
  92. if (count($table->columns()) === 0) {
  93. throw new Exception(sprintf('Cannot describe %s. It has 0 columns.', $name));
  94. }
  95. $this->_reflect('Index', $name, $config, $table);
  96. $this->_reflect('ForeignKey', $name, $config, $table);
  97. $this->_reflect('Options', $name, $config, $table);
  98. return $table;
  99. }
  100. /**
  101. * Helper method for running each step of the reflection process.
  102. *
  103. * @param string $stage The stage name.
  104. * @param string $name The table name.
  105. * @param array $config The config data.
  106. * @param \Cake\Database\Schema\TableSchema $schema The table schema instance.
  107. * @return void
  108. * @throws \Cake\Database\Exception on query failure.
  109. * @uses \Cake\Database\Schema\BaseSchema::describeColumnSql
  110. * @uses \Cake\Database\Schema\BaseSchema::describeIndexSql
  111. * @uses \Cake\Database\Schema\BaseSchema::describeForeignKeySql
  112. * @uses \Cake\Database\Schema\BaseSchema::describeOptionsSql
  113. * @uses \Cake\Database\Schema\BaseSchema::convertColumnDescription
  114. * @uses \Cake\Database\Schema\BaseSchema::convertIndexDescription
  115. * @uses \Cake\Database\Schema\BaseSchema::convertForeignKeyDescription
  116. * @uses \Cake\Database\Schema\BaseSchema::convertOptionsDescription
  117. */
  118. protected function _reflect(string $stage, string $name, array $config, TableSchema $schema): void
  119. {
  120. $describeMethod = "describe{$stage}Sql";
  121. $convertMethod = "convert{$stage}Description";
  122. [$sql, $params] = $this->_dialect->{$describeMethod}($name, $config);
  123. if (empty($sql)) {
  124. return;
  125. }
  126. try {
  127. $statement = $this->_connection->execute($sql, $params);
  128. } catch (PDOException $e) {
  129. throw new Exception($e->getMessage(), 500, $e);
  130. }
  131. /** @psalm-suppress PossiblyFalseIterator */
  132. foreach ($statement->fetchAll('assoc') as $row) {
  133. $this->_dialect->{$convertMethod}($schema, $row);
  134. }
  135. $statement->closeCursor();
  136. }
  137. }