Skip to content

Commit

Permalink
Merge branch 'custom-dbal-types' of https://github.com/JacksonIV/fram…
Browse files Browse the repository at this point in the history
…ework into JacksonIV-custom-dbal-types
  • Loading branch information
taylorotwell committed Apr 17, 2019
2 parents be89773 + 1bcad05 commit 666cda4
Show file tree
Hide file tree
Showing 5 changed files with 128 additions and 0 deletions.
25 changes: 25 additions & 0 deletions src/Illuminate/Database/Schema/Builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Closure;
use LogicException;
use Doctrine\DBAL\Types\Type;
use Illuminate\Database\Connection;

class Builder
Expand Down Expand Up @@ -317,4 +318,28 @@ public function blueprintResolver(Closure $resolver)
{
$this->resolver = $resolver;
}

/**
* Register your own Doctrine mapping type.
*
* @param string $class
* @param string $name
* @param string $type
* @return void
*
* @throws \Doctrine\DBAL\DBALException
*/
public function registerCustomDBALType($class, $name, $type)
{
if (Type::hasType($name)) {
return;
}

Type::addType($name, $class);

$this->connection
->getDoctrineSchemaManager()
->getDatabasePlatform()
->registerDoctrineTypeMapping($type, $name);
}
}
32 changes: 32 additions & 0 deletions src/Illuminate/Database/Schema/MySqlBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,24 @@

namespace Illuminate\Database\Schema;

use Illuminate\Database\Connection;
use Illuminate\Database\Schema\Types\TinyInteger;

class MySqlBuilder extends Builder
{
/**
* MySqlBuilder constructor.
*
* @param \Illuminate\Database\Connection $connection
* @throws \Doctrine\DBAL\DBALException
*/
public function __construct(Connection $connection)
{
parent::__construct($connection);

$this->registerCustomDBALTypes();
}

/**
* Determine if the given table exists.
*
Expand Down Expand Up @@ -111,4 +127,20 @@ protected function getAllViews()
$this->grammar->compileGetAllViews()
);
}

/**
* Register custom DBAL types for the MySQL builder.
*
* @return void
*
* @throws \Doctrine\DBAL\DBALException
*/
private function registerCustomDBALTypes()
{
if (! $this->connection->isDoctrineAvailable()) {
return;
}

$this->registerCustomDBALType(TinyInteger::class, TinyInteger::NAME, 'TINYINT');
}
}
38 changes: 38 additions & 0 deletions src/Illuminate/Database/Schema/Types/TinyInteger.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php

namespace Illuminate\Database\Schema\Types;

use Doctrine\DBAL\Types\Type;
use Doctrine\DBAL\Platforms\AbstractPlatform;

class TinyInteger extends Type
{
/**
* The name of the custom type.
*
* @var string
*/
const NAME = 'tinyinteger';

/**
* Gets the SQL declaration snippet for a field of this type.
*
* @param array $fieldDeclaration
* @param \Doctrine\DBAL\Platforms\AbstractPlatform $platform
* @return string
*/
public function getSQLDeclaration(array $fieldDeclaration, AbstractPlatform $platform)
{
return 'TINYINT';
}

/**
* The name of the custom type.
*
* @return string
*/
public function getName()
{
return self::NAME;
}
}
1 change: 1 addition & 0 deletions src/Illuminate/Support/Facades/Schema.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
* @method static void defaultStringLength(int $length)
* @method static \Illuminate\Database\Schema\Builder disableForeignKeyConstraints()
* @method static \Illuminate\Database\Schema\Builder enableForeignKeyConstraints()
* @method static void registerCustomDBALType(string $class, string $name, string $type)
*
* @see \Illuminate\Database\Schema\Builder
*/
Expand Down
32 changes: 32 additions & 0 deletions tests/Integration/Database/SchemaBuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,12 @@

namespace Illuminate\Tests\Integration\Database\SchemaTest;

use Doctrine\DBAL\Types\Type;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Schema\Types\TinyInteger;
use Illuminate\Database\Schema\Grammars\SQLiteGrammar;
use Illuminate\Tests\Integration\Database\DatabaseTestCase;

/**
Expand Down Expand Up @@ -37,4 +40,33 @@ public function test_drop_all_views()

$this->assertTrue(true);
}

public function test_register_custom_DBAL_type()
{
Schema::registerCustomDBALType(TinyInteger::class, TinyInteger::NAME, 'TINYINT');

Schema::create('test', function (Blueprint $table) {
$table->string('test_column');
});

$blueprint = new Blueprint('test', function (Blueprint $table) {
$table->tinyInteger('test_column')->change();
});

$expected = [
'CREATE TEMPORARY TABLE __temp__test AS SELECT test_column FROM test',
'DROP TABLE test',
'CREATE TABLE test (test_column TINYINT NOT NULL COLLATE BINARY)',
'INSERT INTO test (test_column) SELECT test_column FROM __temp__test',
'DROP TABLE __temp__test',
];

$statements = $blueprint->toSql($this->getConnection(), new SQLiteGrammar());

$blueprint->build($this->getConnection(), new SQLiteGrammar());

$this->assertArrayHasKey(TinyInteger::NAME, Type::getTypesMap());
$this->assertEquals('tinyinteger', Schema::getColumnType('test', 'test_column'));
$this->assertEquals($expected, $statements);
}
}

0 comments on commit 666cda4

Please sign in to comment.