From b5f7e59f46d575e6b217c63c3a6e7ec423f2fb4d Mon Sep 17 00:00:00 2001 From: Mark Story Date: Wed, 1 May 2013 22:13:24 -0400 Subject: [PATCH] Update schema classes to return a hash. This will make adding fixed length strings much easier. --- lib/Cake/Database/Schema/MysqlSchema.php | 29 ++++++------ lib/Cake/Database/Schema/PostgresSchema.php | 37 ++++++++-------- lib/Cake/Database/Schema/SqliteSchema.php | 26 +++++------ .../Database/Schema/MysqlSchemaTest.php | 30 ++++++------- .../Database/Schema/PostgresSchemaTest.php | 44 +++++++++---------- .../Database/Schema/SqliteSchemaTest.php | 30 ++++++------- 6 files changed, 97 insertions(+), 99 deletions(-) diff --git a/lib/Cake/Database/Schema/MysqlSchema.php b/lib/Cake/Database/Schema/MysqlSchema.php index cd6affe35e1..f0ffdd70faa 100644 --- a/lib/Cake/Database/Schema/MysqlSchema.php +++ b/lib/Cake/Database/Schema/MysqlSchema.php @@ -18,6 +18,9 @@ use Cake\Database\Schema\Table; +/** + * Schema dialect/support for MySQL + */ class MysqlSchema { /** @@ -78,33 +81,33 @@ public function convertColumn($column) { } if (in_array($col, array('date', 'time', 'datetime', 'timestamp'))) { - return [$col, null]; + return ['type' => $col, 'length' => null]; } if (($col === 'tinyint' && $length === 1) || $col === 'boolean') { - return ['boolean', null]; + return ['type' => 'boolean', 'length' => null]; } if (strpos($col, 'bigint') !== false || $col === 'bigint') { - return ['biginteger', $length]; + return ['type' => 'biginteger', 'length' => $length]; } if (strpos($col, 'int') !== false) { - return ['integer', $length]; + return ['type' => 'integer', 'length' => $length]; } if (strpos($col, 'char') !== false || $col === 'tinytext') { - return ['string', $length]; + return ['type' => 'string', 'length' => $length]; } if (strpos($col, 'text') !== false) { - return ['text', $length]; + return ['type' => 'text', 'length' => $length]; } if (strpos($col, 'blob') !== false || $col === 'binary') { - return ['binary', $length]; + return ['type' => 'binary', 'length' => $length]; } if (strpos($col, 'float') !== false || strpos($col, 'double') !== false) { - return ['float', $length]; + return ['type' => 'float', 'length' => $length]; } if (strpos($col, 'decimal') !== false) { - return ['decimal', null]; + return ['type' => 'decimal', 'length' => null]; } - return ['text', null]; + return ['type' => 'text', 'length' => null]; } /** @@ -116,12 +119,10 @@ public function convertColumn($column) { * @return void */ public function convertFieldDescription(Table $table, $row, $fieldParams = []) { - list($type, $length) = $this->convertColumn($row['Type']); - $field = [ - 'type' => $type, + $field = $this->convertColumn($row['Type']); + $field += [ 'null' => $row['Null'] === 'YES' ? true : false, 'default' => $row['Default'], - 'length' => $length, ]; foreach ($fieldParams as $key => $metadata) { if (!empty($row[$metadata['column']])) { diff --git a/lib/Cake/Database/Schema/PostgresSchema.php b/lib/Cake/Database/Schema/PostgresSchema.php index 0ae74cf5025..2dfc881664e 100644 --- a/lib/Cake/Database/Schema/PostgresSchema.php +++ b/lib/Cake/Database/Schema/PostgresSchema.php @@ -74,51 +74,51 @@ public function describeTableSql($table, $config) { * Cake\Database\Type can handle. * * @param string $column The column type + length - * @return array List of (type, length) + * @return array Array of column information. */ public function convertColumn($column) { $col = strtolower($column); if (in_array($col, array('date', 'time', 'boolean'))) { - return [$col, null]; + return ['type' =>$col, 'length' => null]; } if (strpos($col, 'timestamp') !== false) { - return ['datetime', null]; + return ['type' => 'datetime', 'length' => null]; } if ($col === 'serial' || $col === 'integer') { - return ['integer', 10]; + return ['type' => 'integer', 'length' => 10]; } if ($col === 'bigserial' || $col === 'bigint') { - return ['biginteger', 20]; + return ['type' => 'biginteger', 'length' => 20]; } if ($col === 'smallint') { - return ['integer', 5]; + return ['type' => 'integer', 'length' => 5]; } if ($col === 'inet') { - return ['string', 39]; + return ['type' => 'string', 'length' => 39]; } if ($col === 'uuid') { - return ['string', 36]; + return ['type' => 'string', 'length' => 36]; } if (strpos($col, 'char') !== false) { - return ['string', null]; + return ['type' => 'string', 'length' => null]; } if (strpos($col, 'text') !== false) { - return ['text', null]; + return ['type' => 'text', 'length' => null]; } if ($col === 'bytea') { - return ['binary', null]; + return ['type' => 'binary', 'length' => null]; } if ($col === 'real' || strpos($col, 'double') !== false) { - return ['float', null]; + return ['type' => 'float', 'length' => null]; } if ( strpos($col, 'numeric') !== false || strpos($col, 'money') !== false || strpos($col, 'decimal') !== false ) { - return ['decimal', null]; + return ['type' => 'decimal', 'length' => null]; } - return ['text', null]; + return ['type' => 'text', 'length' => null]; } /** @@ -143,9 +143,9 @@ public function extraSchemaColumns() { * @return void */ public function convertFieldDescription(Table $table, $row, $fieldParams = []) { - list($type, $length) = $this->convertColumn($row['type']); + $field = $this->convertColumn($row['type']); - if ($type === 'boolean') { + if ($field['type'] === 'boolean') { if ($row['default'] === 'true') { $row['default'] = 1; } @@ -154,12 +154,11 @@ public function convertFieldDescription(Table $table, $row, $fieldParams = []) { } } - $field = [ - 'type' => $type, + $field += [ 'null' => $row['null'] === 'YES' ? true : false, 'default' => $row['default'], - 'length' => $row['char_length'] ?: $length, ]; + $field['length'] = $row['char_length'] ?: $field['length']; foreach ($fieldParams as $key => $metadata) { if (!empty($row[$metadata['column']])) { $field[$key] = $row[$metadata['column']]; diff --git a/lib/Cake/Database/Schema/SqliteSchema.php b/lib/Cake/Database/Schema/SqliteSchema.php index 9bf09d6b563..62f7add9c38 100644 --- a/lib/Cake/Database/Schema/SqliteSchema.php +++ b/lib/Cake/Database/Schema/SqliteSchema.php @@ -39,7 +39,7 @@ public function __construct($driver) { * * @param string $column The column type + length * @throws Cake\Error\Exception - * @return array List of (type, length) + * @return array Array of column information. */ public function convertColumn($column) { preg_match('/([a-z]+)(?:\(([0-9,]+)\))?/i', $column, $matches); @@ -53,31 +53,31 @@ public function convertColumn($column) { } if ($col === 'bigint') { - return ['biginteger', $length]; + return ['type' => 'biginteger', 'length' => $length]; } if (in_array($col, ['blob', 'clob'])) { - return ['binary', null]; + return ['type' => 'binary', 'length' => null]; } if (in_array($col, ['date', 'time', 'timestamp', 'datetime'])) { - return [$col, null]; + return ['type' => $col, 'length' => null]; } if (strpos($col, 'decimal') !== false) { - return ['decimal', null]; + return ['type' => 'decimal', 'length' => null]; } if (strpos($col, 'boolean') !== false) { - return ['boolean', null]; + return ['type' => 'boolean', 'length' => null]; } if (strpos($col, 'int') !== false) { - return ['integer', $length]; + return ['type' => 'integer', 'length' => $length]; } if (strpos($col, 'char') !== false) { - return ['string', $length]; + return ['type' => 'string', 'length' => $length]; } if (in_array($col, ['float', 'real', 'double'])) { - return ['float', null]; + return ['type' => 'float', 'length' => null]; } - return ['text', null]; + return ['type' => 'text', 'length' => null]; } /** @@ -118,12 +118,10 @@ public function extraSchemaColumns() { * @param array $fieldParams Additional field parameters to parse. */ public function convertFieldDescription(Table $table, $row, $fieldParams = []) { - list($type, $length) = $this->convertColumn($row['type']); - $field = [ - 'type' => $type, + $field = $this->convertColumn($row['type']); + $field += [ 'null' => !$row['notnull'], 'default' => $row['dflt_value'] === null ? null : trim($row['dflt_value'], "'"), - 'length' => $length, ]; if ($row['pk'] == true) { $field['null'] = false; diff --git a/lib/Cake/Test/TestCase/Database/Schema/MysqlSchemaTest.php b/lib/Cake/Test/TestCase/Database/Schema/MysqlSchemaTest.php index 887eb20a882..ca346461257 100644 --- a/lib/Cake/Test/TestCase/Database/Schema/MysqlSchemaTest.php +++ b/lib/Cake/Test/TestCase/Database/Schema/MysqlSchemaTest.php @@ -47,63 +47,63 @@ public static function columnProvider() { return [ [ 'DATETIME', - ['datetime', null] + ['type' => 'datetime', 'length' => null] ], [ 'DATE', - ['date', null] + ['type' => 'date', 'length' => null] ], [ 'TIME', - ['time', null] + ['type' => 'time', 'length' => null] ], [ 'TINYINT(1)', - ['boolean', null] + ['type' => 'boolean', 'length' => null] ], [ 'TINYINT(2)', - ['integer', 2] + ['type' => 'integer', 'length' => 2] ], [ 'INTEGER(11)', - ['integer', 11] + ['type' => 'integer', 'length' => 11] ], [ 'BIGINT', - ['biginteger', null] + ['type' => 'biginteger', 'length' => null] ], [ 'VARCHAR(255)', - ['string', 255] + ['type' => 'string', 'length' => 255] ], [ 'CHAR(25)', - ['string', 25] + ['type' => 'string', 'length' => 25] ], [ 'TINYTEXT', - ['string', null] + ['type' => 'string', 'length' => null] ], [ 'BLOB', - ['binary', null] + ['type' => 'binary', 'length' => null] ], [ 'MEDIUMBLOB', - ['binary', null] + ['type' => 'binary', 'length' => null] ], [ 'FLOAT', - ['float', null] + ['type' => 'float', 'length' => null] ], [ 'DOUBLE', - ['float', null] + ['type' => 'float', 'length' => null] ], [ 'DECIMAL(11,2)', - ['decimal', null] + ['type' => 'decimal', 'length' => null] ], ]; } diff --git a/lib/Cake/Test/TestCase/Database/Schema/PostgresSchemaTest.php b/lib/Cake/Test/TestCase/Database/Schema/PostgresSchemaTest.php index f4726aab657..c59564754f4 100644 --- a/lib/Cake/Test/TestCase/Database/Schema/PostgresSchemaTest.php +++ b/lib/Cake/Test/TestCase/Database/Schema/PostgresSchemaTest.php @@ -82,91 +82,91 @@ public static function columnProvider() { return [ [ 'TIMESTAMP', - ['datetime', null] + ['type' => 'datetime', 'length' => null] ], [ 'TIMESTAMP WITHOUT TIME ZONE', - ['datetime', null] + ['type' => 'datetime', 'length' => null] ], [ 'DATE', - ['date', null] + ['type' => 'date', 'length' => null] ], [ 'TIME', - ['time', null] + ['type' => 'time', 'length' => null] ], [ 'SMALLINT', - ['integer', 5] + ['type' => 'integer', 'length' => 5] ], [ 'INTEGER', - ['integer', 10] + ['type' => 'integer', 'length' => 10] ], [ 'SERIAL', - ['integer', 10] + ['type' => 'integer', 'length' => 10] ], [ 'BIGINT', - ['biginteger', 20] + ['type' => 'biginteger', 'length' => 20] ], [ 'NUMERIC', - ['decimal', null] + ['type' => 'decimal', 'length' => null] ], [ 'DECIMAL(10,2)', - ['decimal', null] + ['type' => 'decimal', 'length' => null] ], [ 'MONEY', - ['decimal', null] + ['type' => 'decimal', 'length' => null] ], [ 'VARCHAR', - ['string', null] + ['type' => 'string', 'length' => null] ], [ 'CHARACTER VARYING', - ['string', null] + ['type' => 'string', 'length' => null] ], [ 'CHAR', - ['string', null] + ['type' => 'string', 'length' => null] ], [ 'UUID', - ['string', 36] + ['type' => 'string', 'length' => 36] ], [ 'CHARACTER', - ['string', null] + ['type' => 'string', 'length' => null] ], [ 'INET', - ['string', 39] + ['type' => 'string', 'length' => 39] ], [ 'TEXT', - ['text', null] + ['type' => 'text', 'length' => null] ], [ 'BYTEA', - ['binary', null] + ['type' => 'binary', 'length' => null] ], [ 'REAL', - ['float', null] + ['type' => 'float', 'length' => null] ], [ 'DOUBLE PRECISION', - ['float', null] + ['type' => 'float', 'length' => null] ], [ 'BIGSERIAL', - ['biginteger', 20] + ['type' => 'biginteger', 'length' => 20] ], ]; } diff --git a/lib/Cake/Test/TestCase/Database/Schema/SqliteSchemaTest.php b/lib/Cake/Test/TestCase/Database/Schema/SqliteSchemaTest.php index 0cb33a3b387..f83b66c3799 100644 --- a/lib/Cake/Test/TestCase/Database/Schema/SqliteSchemaTest.php +++ b/lib/Cake/Test/TestCase/Database/Schema/SqliteSchemaTest.php @@ -47,63 +47,63 @@ public static function columnProvider() { return [ [ 'DATETIME', - ['datetime', null] + ['type' => 'datetime', 'length' => null] ], [ 'DATE', - ['date', null] + ['type' => 'date', 'length' => null] ], [ 'TIME', - ['time', null] + ['type' => 'time', 'length' => null] ], [ 'BOOLEAN', - ['boolean', null] + ['type' => 'boolean', 'length' => null] ], [ 'BIGINT', - ['biginteger', null] + ['type' => 'biginteger', 'length' => null] ], [ 'VARCHAR(255)', - ['string', 255] + ['type' => 'string', 'length' => 255] ], [ 'CHAR(25)', - ['string', 25] + ['type' => 'string', 'length' => 25] ], [ 'BLOB', - ['binary', null] + ['type' => 'binary', 'length' => null] ], [ 'INTEGER(11)', - ['integer', 11] + ['type' => 'integer', 'length' => 11] ], [ 'TINYINT(5)', - ['integer', 5] + ['type' => 'integer', 'length' => 5] ], [ 'MEDIUMINT(10)', - ['integer', 10] + ['type' => 'integer', 'length' => 10] ], [ 'FLOAT', - ['float', null] + ['type' => 'float', 'length' => null] ], [ 'DOUBLE', - ['float', null] + ['type' => 'float', 'length' => null] ], [ 'REAL', - ['float', null] + ['type' => 'float', 'length' => null] ], [ 'DECIMAL(11,2)', - ['decimal', null] + ['type' => 'decimal', 'length' => null] ], ]; }