Skip to content

Commit

Permalink
Add hook to describe table options when reflecting a table.
Browse files Browse the repository at this point in the history
This will let the schema subsystem get engine information from MySQL
when reflecting tables. Other table types do not use options so the base
class methods are noops instead of abstract methods.

Refs #4263
  • Loading branch information
markstory committed Aug 17, 2014
1 parent ecadccc commit 623fe8b
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 2 deletions.
21 changes: 21 additions & 0 deletions src/Database/Schema/BaseSchema.php
Expand Up @@ -135,6 +135,17 @@ abstract public function describeIndexSql($table, $config);
*/
abstract public function describeForeignKeySql($table, $config);

/**
* Generate the SQL to describe table options
*
* @param string $name Table name.
* @param array $config The connection configuration.
* @return array SQL statements to get options for a table.
*/
public function describeOptionsSql($name, $config) {
return ['', ''];
}

/**
* Convert field description results into abstract schema fields.
*
Expand Down Expand Up @@ -210,4 +221,14 @@ abstract public function indexSql(Table $table, $name);
*/
abstract public function truncateTableSql(Table $table);

/**
* Convert options data into table options.
*
* @param \Cake\Database\Schema\Table $table Table instance.
* @param array $row The row of data.
* @return void
*/
public function convertOptions(Table $table, $row) {
}

}
9 changes: 9 additions & 0 deletions src/Database/Schema/Collection.php
Expand Up @@ -135,6 +135,15 @@ public function describe($name, array $options = []) {
}
$statement->closeCursor();

list($sql, $params) = $this->_dialect->describeOptionsSql($name, $config);
if ($sql) {
$statement = $this->_executeSql($sql, $params);
foreach ($statement->fetchAll('assoc') as $row) {
$this->_dialect->convertOptions($table, $row);
}
$statement->closeCursor();
}

if (!empty($cacheConfig)) {
Cache::write($cacheKey, $table, $cacheConfig);
}
Expand Down
20 changes: 18 additions & 2 deletions src/Database/Schema/MysqlSchema.php
Expand Up @@ -39,8 +39,24 @@ public function describeTableSql($name, $config) {
/**
* {@inheritDoc}
*/
public function describeIndexSql($table, $config) {
return ['SHOW INDEXES FROM ' . $this->_driver->quoteIdentifier($table), []];
public function describeIndexSql($name, $config) {
return ['SHOW INDEXES FROM ' . $this->_driver->quoteIdentifier($name), []];
}

/**
* {@inheritDoc}
*/
public function describeOptionsSql($name, $config) {
return ['SHOW TABLE STATUS WHERE Name = ?', [$name]];
}

/**
* {@inheritDoc}
*/
public function convertOptions(Table $table, $row) {
$table->options([
'engine' => $row['Engine']
]);
}

/**
Expand Down
14 changes: 14 additions & 0 deletions tests/TestCase/Database/Schema/MysqlSchemaTest.php
Expand Up @@ -366,6 +366,20 @@ public function testDescribeTableIndexes() {
$this->assertEquals($expected, $result->index('author_idx'));
}

/**
* Test describing a table creates options
*
* @return void
*/
public function testDescribeTableOptions() {
$connection = ConnectionManager::get('test');
$this->_createTables($connection);

$schema = new SchemaCollection($connection);
$result = $schema->describe('schema_articles');
$this->assertArrayHasKey('engine', $result->options());
}

/**
* Column provider for creating column sql
*
Expand Down

0 comments on commit 623fe8b

Please sign in to comment.