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