Skip to content

Commit

Permalink
Add support for field metadata retrieval.
Browse files Browse the repository at this point in the history
This might only be used for MySQL but is required to preserve
table/column encodings & comments.
  • Loading branch information
markstory committed Mar 29, 2013
1 parent 1dc32f2 commit c98e544
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 3 deletions.
10 changes: 8 additions & 2 deletions lib/Cake/Model/Datasource/Database/Connection.php
Expand Up @@ -452,8 +452,9 @@ public function describe($table) {
list($sql, $params) = $this->_driver->describeTableSql($table);
$statement = $this->execute($sql, $params);
$schema = [];
// TODO complete.
// TODO add tableParameters for platform specific features.

$fieldParams = $this->_driver->extraSchemaColumns();

while ($row = $statement->fetch('assoc')) {
list($type, $length) = $this->_driver->columnType($row['Type']);
$schema[$row['Field']] = [
Expand All @@ -465,6 +466,11 @@ public function describe($table) {
if (!empty($row['Key'])) {
$schema[$row['Field']]['key'] = $this->_driver->keyType($row['Key']);
}
foreach ($fieldParams as $key => $metadata) {
if (!empty($row[$metadata['column']])) {
$schema[$row['Field']][$key] = $row[$metadata['column']];
}
}
}
return $schema;
}
Expand Down
19 changes: 19 additions & 0 deletions lib/Cake/Model/Datasource/Database/Dialect/MysqlDialectTrait.php
Expand Up @@ -131,4 +131,23 @@ public function columnType($column) {
return 'text';
}

/**
* Get additional column meta data used in schema reflections.
*
* @return array
*/
public function extraSchemaColumns() {
return [
'charset' => [
'column' => false,
],
'collate' => [
'column' => 'Collation',
],
'comment' => [
'column' => 'Comment',
]
];
}

}
10 changes: 10 additions & 0 deletions lib/Cake/Model/Datasource/Database/SqlDialectTrait.php
Expand Up @@ -176,4 +176,14 @@ public function rollbackSavePointSQL($name) {
return 'ROLLBACK TO SAVEPOINT LEVEL' . $name;
}

/**
* Get extra schema metadata columns
*
* This method returns information about additional metadata present in the data
* generated by describeTableSql
*
* @return void
*/
abstract function extraSchemaColumns();

}
Expand Up @@ -129,7 +129,7 @@ protected function _createTables($connection) {
body TEXT,
author_id INT(11) NOT NULL,
created DATETIME
)
) COLLATE=utf8_general_ci
SQL;
$connection->execute($table);
}
Expand Down Expand Up @@ -250,12 +250,14 @@ public function testDescribeTable() {
'null' => true,
'default' => null,
'length' => 20,
'collate' => 'utf8_general_ci',
],
'body' => [
'type' => 'text',
'null' => true,
'default' => null,
'length' => null,
'collate' => 'utf8_general_ci',
],
'author_id' => [
'type' => 'integer',
Expand Down

0 comments on commit c98e544

Please sign in to comment.