Collection.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. <?php
  2. /**
  3. * PHP Version 5.4
  4. *
  5. * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
  6. * Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  7. *
  8. * Licensed under The MIT License
  9. * For full copyright and license information, please see the LICENSE.txt
  10. * Redistributions of files must retain the above copyright notice.
  11. *
  12. * @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  13. * @link http://cakephp.org CakePHP(tm) Project
  14. * @since CakePHP(tm) v 3.0.0
  15. * @license MIT License (http://www.opensource.org/licenses/mit-license.php)
  16. */
  17. namespace Cake\Database\Schema;
  18. use Cake\Database\Connection;
  19. use Cake\Database\Exception;
  20. use Cake\Database\Schema\Table;
  21. /**
  22. * Represents a database schema collection
  23. *
  24. * Used to access information about the tables,
  25. * and other data in a database.
  26. */
  27. class Collection {
  28. /**
  29. * Connection object
  30. *
  31. * @var Cake\Database\Connection
  32. */
  33. protected $_connection;
  34. /**
  35. * Schema dialect instance.
  36. *
  37. * @var
  38. */
  39. protected $_dialect;
  40. /**
  41. * Constructor.
  42. *
  43. * @param Cake\Database\Connection $connection
  44. */
  45. public function __construct(Connection $connection) {
  46. $this->_connection = $connection;
  47. $this->_dialect = $connection->driver()->schemaDialect();
  48. }
  49. /**
  50. * Get the list of tables available in the current connection.
  51. *
  52. * @return array The list of tables in the connected database/schema.
  53. */
  54. public function listTables() {
  55. list($sql, $params) = $this->_dialect->listTablesSql($this->_connection->config());
  56. $result = [];
  57. $statement = $this->_connection->execute($sql, $params);
  58. while ($row = $statement->fetch()) {
  59. $result[] = $row[0];
  60. }
  61. return $result;
  62. }
  63. /**
  64. * Get the column metadata for a table.
  65. *
  66. * @param string $name The name of the table to describe.
  67. * @return Cake\Database\Schema\Table Object with column metadata.
  68. * @throws Cake\Database\Exception when table cannot be described.
  69. */
  70. public function describe($name) {
  71. $config = $this->_connection->config();
  72. list($sql, $params) = $this->_dialect->describeTableSql($name, $config);
  73. $statement = $this->_executeSql($sql, $params);
  74. if (count($statement) === 0) {
  75. throw new Exception(__d('cake_dev', 'Cannot describe %s. It has 0 columns.', $name));
  76. }
  77. $table = new Table($name);
  78. foreach ($statement->fetchAll('assoc') as $row) {
  79. $this->_dialect->convertFieldDescription($table, $row);
  80. }
  81. list($sql, $params) = $this->_dialect->describeIndexSql($name, $config);
  82. $statement = $this->_executeSql($sql, $params);
  83. foreach ($statement->fetchAll('assoc') as $row) {
  84. $this->_dialect->convertIndexDescription($table, $row);
  85. }
  86. list($sql, $params) = $this->_dialect->describeForeignKeySql($name, $config);
  87. $statement = $this->_executeSql($sql, $params);
  88. foreach ($statement->fetchAll('assoc') as $row) {
  89. $this->_dialect->convertForeignKeyDescription($table, $row);
  90. }
  91. return $table;
  92. }
  93. /**
  94. * Helper method to run queries and convert Exceptions to the correct types.
  95. *
  96. * @param string $sql The sql to run.
  97. * @param array $params Parameters for the statement.
  98. * @return Cake\Database\Statement Prepared statement
  99. * @throws Cake\Database\Exception on query failure.
  100. */
  101. protected function _executeSql($sql, $params) {
  102. try {
  103. return $this->_connection->execute($sql, $params);
  104. } catch (\PDOException $e) {
  105. throw new Exception($e->getMessage(), 500, $e);
  106. }
  107. }
  108. }