Skip to content
Permalink
Browse files

Move driver specific code into MysqlDialect.

Postgres and sqlite implement table schema listings with different
column names. Push references to specific columns into the dialect.
  • Loading branch information...
markstory committed Mar 28, 2013
1 parent c98e544 commit 394321339b81900154da0b434eebd18f7af2f8cd
@@ -454,23 +454,9 @@ public function describe($table) {
$schema = [];
$fieldParams = $this->_driver->extraSchemaColumns();
while ($row = $statement->fetch('assoc')) {
list($type, $length) = $this->_driver->columnType($row['Type']);
$schema[$row['Field']] = [
'type' => $type,
'null' => $row['Null'] === 'YES' ? true : false,
'default' => $row['Default'],
'length' => $length,
];
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']];
}
}
$rows = $statement->fetchAll('assoc');
foreach ($rows as $row) {
$schema += $this->_driver->convertFieldDescription($row, $fieldParams);
}
return $schema;
}
@@ -69,14 +69,14 @@ public function describeTableSql($table) {
* @param string $key The key type to convert.
* @return string The abstract key type (primary, unique, index)
*/
public function keyType($key) {
public function convertIndex($key) {
if ($key === 'PRI') {
return 'primary';
}
if ($key === 'MUL') {
return 'index';
}
if ($key === 'uni') {
if ($key === 'UNI') {
return 'unique';
}
}
@@ -89,7 +89,7 @@ public function keyType($key) {
* @param string $column The column type + length
* @return array List of (type, length)
*/
public function columnType($column) {
public function convertColumn($column) {
preg_match('/([a-z]+)(?:\(([0-9,]+)\))?/i', $column, $matches);
if (empty($matches)) {
throw new Error\Exception(__d('cake_dev', 'Unable to parse column type from "%s"', $column));
@@ -102,7 +102,7 @@ public function columnType($column) {
}
if (in_array($col, array('date', 'time', 'datetime', 'timestamp'))) {
return [$col, $length];
return [$col, null];
}
if (($col === 'tinyint' && $length === 1) || $col === 'boolean') {
return ['boolean', null];
@@ -128,7 +128,32 @@ public function columnType($column) {
if (strpos($col, 'decimal') !== false) {
return ['decimal', null];
}
return 'text';
return ['text', null];
}
/**
* Convert field description results into abstract schema fields.
*
* @return array An array of with the key/values of schema data.
*/
public function convertFieldDescription($row, $fieldParams = []) {
list($type, $length) = $this->convertColumn($row['Type']);
$schema = [];
$schema[$row['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']];
}
}
return $schema;
}
/**
@@ -206,9 +206,32 @@ public static function columnProvider() {
* @dataProvider columnProvider
* @return void
*/
public function testColumnType($input, $expected) {
public function testConvertColumnType($input, $expected) {
$driver = $this->getMock('Cake\Model\Datasource\Database\Driver\Mysql', ['_connect']);
$this->assertEquals($driver->columnType($input), $expected);
$this->assertEquals($driver->convertColumn($input), $expected);
}
/**
* 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\Model\Datasource\Database\Driver\Mysql', ['_connect']);
$this->assertEquals($driver->convertIndex($input), $expected);
}
/**

0 comments on commit 3943213

Please sign in to comment.
You can’t perform that action at this time.