Skip to content

Commit

Permalink
Add support for tinyblob, mediumblob and longblob
Browse files Browse the repository at this point in the history
  • Loading branch information
HavokInspiration committed Apr 7, 2016
1 parent 75e5f8c commit fe78749
Show file tree
Hide file tree
Showing 6 changed files with 80 additions and 7 deletions.
17 changes: 16 additions & 1 deletion src/Database/Schema/MysqlSchema.php
Expand Up @@ -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) {
Expand Down Expand Up @@ -288,7 +290,6 @@ public function columnSql(Table $table, $name)
'integer' => ' INTEGER',
'biginteger' => ' BIGINT',
'boolean' => ' BOOLEAN',
'binary' => ' LONGBLOB',
'float' => ' FLOAT',
'decimal' => ' DECIMAL',
'date' => ' DATE',
Expand All @@ -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']];
Expand All @@ -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;
}
}
Expand Down
11 changes: 10 additions & 1 deletion src/Database/Schema/SqlserverSchema.php
Expand Up @@ -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',
Expand All @@ -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';

Expand Down
3 changes: 2 additions & 1 deletion tests/Fixture/SessionsFixture.php
Expand Up @@ -14,6 +14,7 @@
*/
namespace Cake\Test\Fixture;

use Cake\Database\Schema\Table;
use Cake\TestSuite\Fixture\TestFixture;

/**
Expand All @@ -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']]]
];
Expand Down
35 changes: 34 additions & 1 deletion tests/TestCase/Database/Schema/MysqlSchemaTest.php
Expand Up @@ -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]
Expand All @@ -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',
Expand Down Expand Up @@ -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',
Expand Down
2 changes: 1 addition & 1 deletion tests/TestCase/Database/Schema/PostgresSchemaTest.php
Expand Up @@ -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',
Expand Down
19 changes: 17 additions & 2 deletions tests/TestCase/Database/Schema/SqlserverSchemaTest.php
Expand Up @@ -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',
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit fe78749

Please sign in to comment.