From 5bfa0295dedda71a2ec7bd2ac3a3bdd109393956 Mon Sep 17 00:00:00 2001 From: Mark Story Date: Mon, 6 May 2013 21:46:27 -0400 Subject: [PATCH] Refactor case switch and add missing types. --- lib/Cake/Database/Schema/MysqlSchema.php | 53 ++++++++++--------- .../Database/Schema/MysqlSchemaTest.php | 17 ++++++ 2 files changed, 45 insertions(+), 25 deletions(-) diff --git a/lib/Cake/Database/Schema/MysqlSchema.php b/lib/Cake/Database/Schema/MysqlSchema.php index 1d5542e7d3f..bf04c772238 100644 --- a/lib/Cake/Database/Schema/MysqlSchema.php +++ b/lib/Cake/Database/Schema/MysqlSchema.php @@ -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' diff --git a/lib/Cake/Test/TestCase/Database/Schema/MysqlSchemaTest.php b/lib/Cake/Test/TestCase/Database/Schema/MysqlSchemaTest.php index e6ae2407e23..3c1c08fca59 100644 --- a/lib/Cake/Test/TestCase/Database/Schema/MysqlSchemaTest.php +++ b/lib/Cake/Test/TestCase/Database/Schema/MysqlSchemaTest.php @@ -313,6 +313,12 @@ public static function columnSqlProvider() { ['type' => 'biginteger', 'length' => 20], '`post_id` BIGINT' ], + // Float + [ + 'value', + ['type' => 'float'], + '`value` FLOAT' + ], // Boolean [ 'checked', @@ -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',