Skip to content

Commit

Permalink
Store the primary key in Table objects generated by describe.
Browse files Browse the repository at this point in the history
Most of CakePHP will need to know about the primary key, store
that in the Table instance generated by basic describe().
  • Loading branch information
markstory committed Apr 27, 2013
1 parent bdbb331 commit 5f652f4
Show file tree
Hide file tree
Showing 8 changed files with 56 additions and 73 deletions.
9 changes: 4 additions & 5 deletions lib/Cake/Database/Schema/Collection.php
Expand Up @@ -85,13 +85,12 @@ public function describe($name) {
return null;
}

$columns = [];
$table = new Table($name);
$fieldParams = $this->_dialect->extraSchemaColumns();
$rows = $statement->fetchAll('assoc');
foreach ($rows as $row) {
$columns += $this->_dialect->convertFieldDescription($row, $fieldParams);
foreach ($statement->fetchAll('assoc') as $row) {
$this->_dialect->convertFieldDescription($table, $row, $fieldParams);
}
return new Table($name, $columns);
return $table;
}

/**
Expand Down
45 changes: 17 additions & 28 deletions lib/Cake/Database/Schema/MysqlSchema.php
Expand Up @@ -16,6 +16,8 @@
*/
namespace Cake\Database\Schema;

use Cake\Database\Schema\Table;

class MysqlSchema {

/**
Expand Down Expand Up @@ -55,28 +57,10 @@ public function describeTableSql($table) {
return ["SHOW FULL COLUMNS FROM " . $this->_driver->quoteIdentifier($table), []];
}

/**
* Convert a platform specific index type to the abstract type
*
* @param string $key The key type to convert.
* @return string The abstract key type (primary, unique, index)
*/
public function convertIndex($key) {
if ($key === 'PRI') {
return 'primary';
}
if ($key === 'MUL') {
return 'index';
}
if ($key === 'UNI') {
return 'unique';
}
}

/**
* Convert a MySQL column type into an abstract type.
*
* The returnned type will be a type that Cake\Database\Type can handle.
* The returned type will be a type that Cake\Database\Type can handle.
*
* @param string $column The column type + length
* @return array List of (type, length)
Expand Down Expand Up @@ -126,26 +110,31 @@ public function convertColumn($column) {
/**
* Convert field description results into abstract schema fields.
*
* @return array An array of with the key/values of schema data.
* @param Cake\Database\Schema\Table $table The table object to append fields to.
* @param array $row The row data from describeTableSql
* @param array $fieldParams Additional field parameters to parse.
* @return void
*/
public function convertFieldDescription($row, $fieldParams = []) {
public function convertFieldDescription(Table $table, $row, $fieldParams = []) {
list($type, $length) = $this->convertColumn($row['Type']);
$schema = [];
$schema[$row['Field']] = [
$field = [
'type' => $type,
'null' => $row['Null'] === 'YES' ? true : false,
'default' => $row['Default'],
'length' => $length,
];
if (!empty($row['Key'])) {
$schema[$row['Field']]['key'] = $this->convertIndex($row['Key']);
}
foreach ($fieldParams as $key => $metadata) {
if (!empty($row[$metadata['column']])) {
$schema[$row['Field']][$key] = $row[$metadata['column']];
$field[$key] = $row[$metadata['column']];
}
}
return $schema;
$table->addColumn($row['Field'], $field);
if (!empty($row['Key']) && $row['Key'] === 'PRI') {
$table->addIndex('primary', [
'type' => Table::INDEX_PRIMARY,
'columns' => [$row['Field']]
]);
}
}

/**
Expand Down
25 changes: 16 additions & 9 deletions lib/Cake/Database/Schema/PostgresSchema.php
Expand Up @@ -16,6 +16,8 @@
*/
namespace Cake\Database\Schema;

use Cake\Database\Schema\Table;

class PostgresSchema {

/**
Expand Down Expand Up @@ -135,9 +137,12 @@ public function extraSchemaColumns() {
/**
* Convert field description results into abstract schema fields.
*
* @return array An array of with the key/values of schema data.
* @param Cake\Database\Schema\Table $table The table object to append fields to.
* @param array $row The row data from describeTableSql
* @param array $fieldParams Additional field parameters to parse.
* @return void
*/
public function convertFieldDescription($row, $fieldParams = []) {
public function convertFieldDescription(Table $table, $row, $fieldParams = []) {
list($type, $length) = $this->convertColumn($row['type']);

if ($type === 'boolean') {
Expand All @@ -149,22 +154,24 @@ public function convertFieldDescription($row, $fieldParams = []) {
}
}

$schema = [];
$schema[$row['name']] = [
$field = [
'type' => $type,
'null' => $row['null'] === 'YES' ? true : false,
'default' => $row['default'],
'length' => $row['char_length'] ?: $length,
];
if (!empty($row['pk'])) {
$schema[$row['name']]['key'] = 'primary';
}
foreach ($fieldParams as $key => $metadata) {
if (!empty($row[$metadata['column']])) {
$schema[$row['name']][$key] = $row[$metadata['column']];
$field[$key] = $row[$metadata['column']];
}
}
return $schema;
$table->addColumn($row['name'], $field);
if (!empty($row['pk'])) {
$table->addIndex('primary', [
'type' => Table::INDEX_PRIMARY,
'columns' => [$row['name']]
]);
}
}

}
22 changes: 15 additions & 7 deletions lib/Cake/Database/Schema/SqliteSchema.php
Expand Up @@ -16,6 +16,8 @@
*/
namespace Cake\Database\Schema;

use Cake\Database\Schema\Table;

class SqliteSchema {

/**
Expand Down Expand Up @@ -111,22 +113,28 @@ public function extraSchemaColumns() {
/**
* Convert field description results into abstract schema fields.
*
* @return array An array of with the key/values of schema data.
* @param Cake\Database\Schema\Table $table The table object to append fields to.
* @param array $row The row data from describeTableSql
* @param array $fieldParams Additional field parameters to parse.
*/
public function convertFieldDescription($row, $fieldParams = []) {
public function convertFieldDescription(Table $table, $row, $fieldParams = []) {
list($type, $length) = $this->convertColumn($row['type']);
$schema = [];
$schema[$row['name']] = [
$field = [
'type' => $type,
'null' => !$row['notnull'],
'default' => $row['dflt_value'] === null ? null : trim($row['dflt_value'], "'"),
'length' => $length,
];
if ($row['pk'] == true) {
$schema[$row['name']]['key'] = 'primary';
$schema[$row['name']]['null'] = false;
$field['null'] = false;
}
$table->addColumn($row['name'], $field);
if ($row['pk'] == true) {
$table->addIndex('primary', [
'type' => Table::INDEX_PRIMARY,
'columns' => [$row['name']]
]);
}
return $schema;
}

}
1 change: 1 addition & 0 deletions lib/Cake/Test/TestCase/Database/Schema/CollectionTest.php
Expand Up @@ -53,6 +53,7 @@ public function tearDown() {
* @return void
*/
public function testDescribe() {

}

/**
Expand Down
25 changes: 1 addition & 24 deletions lib/Cake/Test/TestCase/Database/Schema/MysqlSchemaTest.php
Expand Up @@ -120,30 +120,6 @@ public function testConvertColumnType($input, $expected) {
$this->assertEquals($expected, $dialect->convertColumn($input));
}

/**
* Provider for testing index conversion
*
* @return array
*/
public static function convertIndexProvider() {
return [
['PRI', 'primary'],
['UNI', 'unique'],
['MUL', 'index'],
];
}
/**
* Test parsing MySQL index types.
*
* @dataProvider convertIndexProvider
* @return void
*/
public function testConvertIndex($input, $expected) {
$driver = $this->getMock('Cake\Database\Driver\Mysql');
$dialect = new MysqlSchema($driver);
$this->assertEquals($expected, $dialect->convertIndex($input));
}

/**
* Helper method for testing methods.
*
Expand Down Expand Up @@ -280,6 +256,7 @@ public function testDescribeTable() {
'charset' => null,
],
];
$this->assertEquals(['id'], $result->primaryKey());
foreach ($expected as $field => $definition) {
$this->assertEquals($definition, $result->column($field));
}
Expand Down
Expand Up @@ -283,6 +283,7 @@ public function testDescribeTable() {
'charset' => null,
],
];
$this->assertEquals(['id'], $result->primaryKey());
foreach ($expected as $field => $definition) {
$this->assertEquals($definition, $result->column($field));
}
Expand Down
Expand Up @@ -247,6 +247,7 @@ public function testDescribeTable() {
],
];
$this->assertInstanceOf('Cake\Database\Schema\Table', $result);
$this->assertEquals(['id'], $result->primaryKey());
foreach ($expected as $field => $definition) {
$this->assertEquals($definition, $result->column($field));
}
Expand Down

0 comments on commit 5f652f4

Please sign in to comment.