Skip to content

Commit

Permalink
Refactor case switch and add missing types.
Browse files Browse the repository at this point in the history
  • Loading branch information
markstory committed May 7, 2013
1 parent acbb923 commit 5bfa029
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 25 deletions.
53 changes: 28 additions & 25 deletions lib/Cake/Database/Schema/MysqlSchema.php
Expand Up @@ -182,31 +182,34 @@ public function createTableSql($table, $lines) {
public function columnSql(Table $table, $name) {
$data = $table->column($name);
$out = $this->_driver->quoteIdentifier($name);
switch ($data['type']) {
case 'string':
$out .= !empty($data['fixed']) ? ' CHAR' : ' VARCHAR';
if (!isset($data['length'])) {
$data['length'] = 255;
}
break;
case 'integer':
$out .= ' INTEGER';
break;
case 'biginteger':
$out .= ' BIGINT';
break;
case 'boolean':
$out .= ' BOOLEAN';
break;
case 'text':
$out .= ' TEXT';
break;
case 'datetime':
$out .= ' DATETIME';
break;
case 'timestamp':
$out .= ' TIMESTAMP';
break;
$typeMap = [
'integer' => ' INTEGER',
'biginteger' => ' BIGINT',
'boolean' => ' BOOLEAN',
'binary' => ' BLOB',
'float' => ' FLOAT',
'decimal' => ' DECIMAL',
'text' => ' TEXT',
'date' => ' DATE',
'time' => ' TIME',
'datetime' => ' DATETIME',
'timestamp' => ' TIMESTAMP',
];
$specialMap = [
'string' => true,
];
if (isset($typeMap[$data['type']])) {
$out .= $typeMap[$data['type']];
}
if (isset($specialMap[$data['type']])) {
switch ($data['type']) {
case 'string':
$out .= !empty($data['fixed']) ? ' CHAR' : ' VARCHAR';
if (!isset($data['length'])) {
$data['length'] = 255;
}
break;
}
}
$hasLength = [
'integer', 'string', 'float'
Expand Down
17 changes: 17 additions & 0 deletions lib/Cake/Test/TestCase/Database/Schema/MysqlSchemaTest.php
Expand Up @@ -313,6 +313,12 @@ public static function columnSqlProvider() {
['type' => 'biginteger', 'length' => 20],
'`post_id` BIGINT'
],
// Float
[
'value',
['type' => 'float'],
'`value` FLOAT'
],
// Boolean
[
'checked',
Expand All @@ -330,6 +336,17 @@ public static function columnSqlProvider() {
['type' => 'datetime', 'comment' => 'Created timestamp'],
'`created` DATETIME COMMENT "Created timestamp"'
],
// Date & Time
[
'start_date',
['type' => 'date'],
'`start_date` DATE'
],
[
'start_time',
['type' => 'time'],
'`start_time` TIME'
],
// timestamps
[
'created',
Expand Down

0 comments on commit 5bfa029

Please sign in to comment.