_connection = $connection; $this->_dialect = $connection->driver()->schemaDialect(); } /** * Get the list of tables available in the current connection. * * @return array The list of tables in the connected database/schema. */ public function listTables() { list($sql, $params) = $this->_dialect->listTablesSql($this->_connection->config()); $result = []; $statement = $this->_connection->execute($sql, $params); while ($row = $statement->fetch()) { $result[] = $row[0]; } return $result; } /** * Get the column metadata for a table. * * @param string $name The name of the table to describe. * @return Cake\Database\Schema\Table Object with column metadata. * @throws Cake\Database\Exception when table cannot be described. */ public function describe($name) { $config = $this->_connection->config(); list($sql, $params) = $this->_dialect->describeTableSql($name, $config); $statement = $this->_executeSql($sql, $params); if (count($statement) === 0) { throw new Exception(__d('cake_dev', 'Cannot describe %s. It has 0 columns.', $name)); } $table = new Table($name); foreach ($statement->fetchAll('assoc') as $row) { $this->_dialect->convertFieldDescription($table, $row); } list($sql, $params) = $this->_dialect->describeIndexSql($name, $config); $statement = $this->_executeSql($sql, $params); foreach ($statement->fetchAll('assoc') as $row) { $this->_dialect->convertIndexDescription($table, $row); } list($sql, $params) = $this->_dialect->describeForeignKeySql($name, $config); $statement = $this->_executeSql($sql, $params); foreach ($statement->fetchAll('assoc') as $row) { $this->_dialect->convertForeignKeyDescription($table, $row); } return $table; } /** * Helper method to run queries and convert Exceptions to the correct types. * * @param string $sql The sql to run. * @param array $params Parameters for the statement. * @return Cake\Database\Statement Prepared statement * @throws Cake\Database\Exception on query failure. */ protected function _executeSql($sql, $params) { try { return $this->_connection->execute($sql, $params); } catch (\PDOException $e) { throw new Exception($e->getMessage(), 500, $e); } } }