From fe78749404cfe7db6377a215d03eacf8a4eea0e5 Mon Sep 17 00:00:00 2001 From: Yves P Date: Wed, 6 Apr 2016 22:33:39 +0200 Subject: [PATCH] Add support for tinyblob, mediumblob and longblob --- src/Database/Schema/MysqlSchema.php | 17 ++++++++- src/Database/Schema/SqlserverSchema.php | 11 +++++- tests/Fixture/SessionsFixture.php | 3 +- .../Database/Schema/MysqlSchemaTest.php | 35 ++++++++++++++++++- .../Database/Schema/PostgresSchemaTest.php | 2 +- .../Database/Schema/SqlserverSchemaTest.php | 19 ++++++++-- 6 files changed, 80 insertions(+), 7 deletions(-) diff --git a/src/Database/Schema/MysqlSchema.php b/src/Database/Schema/MysqlSchema.php index c7851a3796e..1ec10647018 100644 --- a/src/Database/Schema/MysqlSchema.php +++ b/src/Database/Schema/MysqlSchema.php @@ -121,6 +121,8 @@ protected function _convertColumn($column) return ['type' => 'text', 'length' => $length]; } if (strpos($col, 'blob') !== false || $col === 'binary') { + $lengthName = substr($col, 0, -4); + $length = isset(Table::$columnLengths[$lengthName]) ? Table::$columnLengths[$lengthName] : null; return ['type' => 'binary', 'length' => $length]; } if (strpos($col, 'float') !== false || strpos($col, 'double') !== false) { @@ -288,7 +290,6 @@ public function columnSql(Table $table, $name) 'integer' => ' INTEGER', 'biginteger' => ' BIGINT', 'boolean' => ' BOOLEAN', - 'binary' => ' LONGBLOB', 'float' => ' FLOAT', 'decimal' => ' DECIMAL', 'date' => ' DATE', @@ -300,6 +301,7 @@ public function columnSql(Table $table, $name) $specialMap = [ 'string' => true, 'text' => true, + 'binary' => true, ]; if (isset($typeMap[$data['type']])) { $out .= $typeMap[$data['type']]; @@ -324,6 +326,19 @@ public function columnSql(Table $table, $name) $out .= ' ' . strtoupper($length) . 'TEXT'; } + break; + case 'binary': + $isKnownLength = in_array($data['length'], Table::$columnLengths); + if (empty($data['length']) || !$isKnownLength) { + $out .= ' BLOB'; + break; + } + + if ($isKnownLength) { + $length = array_search($data['length'], Table::$columnLengths); + $out .= ' ' . strtoupper($length) . 'BLOB'; + } + break; } } diff --git a/src/Database/Schema/SqlserverSchema.php b/src/Database/Schema/SqlserverSchema.php index d51c42f1711..91d55accdca 100644 --- a/src/Database/Schema/SqlserverSchema.php +++ b/src/Database/Schema/SqlserverSchema.php @@ -302,7 +302,6 @@ public function columnSql(Table $table, $name) 'integer' => ' INTEGER', 'biginteger' => ' BIGINT', 'boolean' => ' BIT', - 'binary' => ' VARBINARY(MAX)', 'float' => ' FLOAT', 'decimal' => ' DECIMAL', 'date' => ' DATE', @@ -327,6 +326,16 @@ public function columnSql(Table $table, $name) $out .= ' NVARCHAR(MAX)'; } + if ($data['type'] === 'binary') { + $out .= ' VARBINARY'; + + if ($data['length'] !== Table::LENGTH_TINY) { + $out .= '(MAX)'; + } else { + $out .= sprintf('(%s)', Table::LENGTH_TINY); + } + } + if ($data['type'] === 'string' || ($data['type'] === 'text' && $data['length'] === Table::LENGTH_TINY)) { $type = ' NVARCHAR'; diff --git a/tests/Fixture/SessionsFixture.php b/tests/Fixture/SessionsFixture.php index ef095858376..498ca7edc1b 100644 --- a/tests/Fixture/SessionsFixture.php +++ b/tests/Fixture/SessionsFixture.php @@ -14,6 +14,7 @@ */ namespace Cake\Test\Fixture; +use Cake\Database\Schema\Table; use Cake\TestSuite\Fixture\TestFixture; /** @@ -30,7 +31,7 @@ class SessionsFixture extends TestFixture */ public $fields = [ 'id' => ['type' => 'string', 'length' => 128], - 'data' => ['type' => 'binary', 'null' => true], + 'data' => ['type' => 'binary', 'length' => Table::LENGTH_MEDIUM, 'null' => true], 'expires' => ['type' => 'integer', 'length' => 11, 'null' => true], '_constraints' => ['primary' => ['type' => 'primary', 'columns' => ['id']]] ]; diff --git a/tests/TestCase/Database/Schema/MysqlSchemaTest.php b/tests/TestCase/Database/Schema/MysqlSchemaTest.php index 93922720f0f..089feb68d87 100644 --- a/tests/TestCase/Database/Schema/MysqlSchemaTest.php +++ b/tests/TestCase/Database/Schema/MysqlSchemaTest.php @@ -98,6 +98,10 @@ public static function convertColumnProvider() 'CHAR(36)', ['type' => 'uuid', 'length' => null] ], + [ + 'TEXT', + ['type' => 'text', 'length' => null] + ], [ 'TINYTEXT', ['type' => 'text', 'length' => Table::LENGTH_TINY] @@ -110,13 +114,21 @@ public static function convertColumnProvider() 'LONGTEXT', ['type' => 'text', 'length' => Table::LENGTH_LONG] ], + [ + 'TINYBLOB', + ['type' => 'binary', 'length' => Table::LENGTH_TINY] + ], [ 'BLOB', ['type' => 'binary', 'length' => null] ], [ 'MEDIUMBLOB', - ['type' => 'binary', 'length' => null] + ['type' => 'binary', 'length' => Table::LENGTH_MEDIUM] + ], + [ + 'LONGBLOB', + ['type' => 'binary', 'length' => Table::LENGTH_LONG] ], [ 'FLOAT', @@ -489,6 +501,27 @@ public static function columnSqlProvider() ['type' => 'text', 'length' => Table::LENGTH_LONG, 'null' => false], '`body` LONGTEXT NOT NULL' ], + // Blob / binary + [ + 'body', + ['type' => 'binary', 'null' => false], + '`body` BLOB NOT NULL' + ], + [ + 'body', + ['type' => 'binary', 'length' => Table::LENGTH_TINY, 'null' => false], + '`body` TINYBLOB NOT NULL' + ], + [ + 'body', + ['type' => 'binary', 'length' => Table::LENGTH_MEDIUM, 'null' => false], + '`body` MEDIUMBLOB NOT NULL' + ], + [ + 'body', + ['type' => 'binary', 'length' => Table::LENGTH_LONG, 'null' => false], + '`body` LONGBLOB NOT NULL' + ], // Integers [ 'post_id', diff --git a/tests/TestCase/Database/Schema/PostgresSchemaTest.php b/tests/TestCase/Database/Schema/PostgresSchemaTest.php index 96b2e79b267..1801d3d740a 100644 --- a/tests/TestCase/Database/Schema/PostgresSchemaTest.php +++ b/tests/TestCase/Database/Schema/PostgresSchemaTest.php @@ -632,7 +632,7 @@ public static function columnSqlProvider() [ 'body', ['type' => 'text', 'length' => Table::LENGTH_TINY, 'null' => false], - '"body" VARCHAR(' . Table::LENGTH_TINY . ') NOT NULL' + sprintf('"body" VARCHAR(%s) NOT NULL', Table::LENGTH_TINY) ], [ 'body', diff --git a/tests/TestCase/Database/Schema/SqlserverSchemaTest.php b/tests/TestCase/Database/Schema/SqlserverSchemaTest.php index 32f00b2059d..3072673d4f0 100644 --- a/tests/TestCase/Database/Schema/SqlserverSchemaTest.php +++ b/tests/TestCase/Database/Schema/SqlserverSchemaTest.php @@ -490,7 +490,7 @@ public static function columnSqlProvider() [ 'body', ['type' => 'text', 'length' => Table::LENGTH_TINY, 'null' => false], - '[body] NVARCHAR(' . Table::LENGTH_TINY . ') NOT NULL' + sprintf('[body] NVARCHAR(%s) NOT NULL', Table::LENGTH_TINY) ], [ 'body', @@ -543,7 +543,22 @@ public static function columnSqlProvider() // Binary [ 'img', - ['type' => 'binary'], + ['type' => 'binary', 'length' => null], + '[img] VARBINARY(MAX)' + ], + [ + 'img', + ['type' => 'binary', 'length' => Table::LENGTH_TINY], + sprintf('[img] VARBINARY(%s)', Table::LENGTH_TINY) + ], + [ + 'img', + ['type' => 'binary', 'length' => Table::LENGTH_MEDIUM], + '[img] VARBINARY(MAX)' + ], + [ + 'img', + ['type' => 'binary', 'length' => Table::LENGTH_LONG], '[img] VARBINARY(MAX)' ], // Boolean