Skip to content

Commit 666cda4

Browse files
committed
Merge branch 'custom-dbal-types' of https://github.com/JacksonIV/framework into JacksonIV-custom-dbal-types
2 parents be89773 + 1bcad05 commit 666cda4

File tree

5 files changed

+128
-0
lines changed

5 files changed

+128
-0
lines changed

src/Illuminate/Database/Schema/Builder.php

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use Closure;
66
use LogicException;
7+
use Doctrine\DBAL\Types\Type;
78
use Illuminate\Database\Connection;
89

910
class Builder
@@ -317,4 +318,28 @@ public function blueprintResolver(Closure $resolver)
317318
{
318319
$this->resolver = $resolver;
319320
}
321+
322+
/**
323+
* Register your own Doctrine mapping type.
324+
*
325+
* @param string $class
326+
* @param string $name
327+
* @param string $type
328+
* @return void
329+
*
330+
* @throws \Doctrine\DBAL\DBALException
331+
*/
332+
public function registerCustomDBALType($class, $name, $type)
333+
{
334+
if (Type::hasType($name)) {
335+
return;
336+
}
337+
338+
Type::addType($name, $class);
339+
340+
$this->connection
341+
->getDoctrineSchemaManager()
342+
->getDatabasePlatform()
343+
->registerDoctrineTypeMapping($type, $name);
344+
}
320345
}

src/Illuminate/Database/Schema/MySqlBuilder.php

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,24 @@
22

33
namespace Illuminate\Database\Schema;
44

5+
use Illuminate\Database\Connection;
6+
use Illuminate\Database\Schema\Types\TinyInteger;
7+
58
class MySqlBuilder extends Builder
69
{
10+
/**
11+
* MySqlBuilder constructor.
12+
*
13+
* @param \Illuminate\Database\Connection $connection
14+
* @throws \Doctrine\DBAL\DBALException
15+
*/
16+
public function __construct(Connection $connection)
17+
{
18+
parent::__construct($connection);
19+
20+
$this->registerCustomDBALTypes();
21+
}
22+
723
/**
824
* Determine if the given table exists.
925
*
@@ -111,4 +127,20 @@ protected function getAllViews()
111127
$this->grammar->compileGetAllViews()
112128
);
113129
}
130+
131+
/**
132+
* Register custom DBAL types for the MySQL builder.
133+
*
134+
* @return void
135+
*
136+
* @throws \Doctrine\DBAL\DBALException
137+
*/
138+
private function registerCustomDBALTypes()
139+
{
140+
if (! $this->connection->isDoctrineAvailable()) {
141+
return;
142+
}
143+
144+
$this->registerCustomDBALType(TinyInteger::class, TinyInteger::NAME, 'TINYINT');
145+
}
114146
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
<?php
2+
3+
namespace Illuminate\Database\Schema\Types;
4+
5+
use Doctrine\DBAL\Types\Type;
6+
use Doctrine\DBAL\Platforms\AbstractPlatform;
7+
8+
class TinyInteger extends Type
9+
{
10+
/**
11+
* The name of the custom type.
12+
*
13+
* @var string
14+
*/
15+
const NAME = 'tinyinteger';
16+
17+
/**
18+
* Gets the SQL declaration snippet for a field of this type.
19+
*
20+
* @param array $fieldDeclaration
21+
* @param \Doctrine\DBAL\Platforms\AbstractPlatform $platform
22+
* @return string
23+
*/
24+
public function getSQLDeclaration(array $fieldDeclaration, AbstractPlatform $platform)
25+
{
26+
return 'TINYINT';
27+
}
28+
29+
/**
30+
* The name of the custom type.
31+
*
32+
* @return string
33+
*/
34+
public function getName()
35+
{
36+
return self::NAME;
37+
}
38+
}

src/Illuminate/Support/Facades/Schema.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
* @method static void defaultStringLength(int $length)
1212
* @method static \Illuminate\Database\Schema\Builder disableForeignKeyConstraints()
1313
* @method static \Illuminate\Database\Schema\Builder enableForeignKeyConstraints()
14+
* @method static void registerCustomDBALType(string $class, string $name, string $type)
1415
*
1516
* @see \Illuminate\Database\Schema\Builder
1617
*/

tests/Integration/Database/SchemaBuilderTest.php

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,12 @@
22

33
namespace Illuminate\Tests\Integration\Database\SchemaTest;
44

5+
use Doctrine\DBAL\Types\Type;
56
use Illuminate\Support\Facades\DB;
67
use Illuminate\Support\Facades\Schema;
78
use Illuminate\Database\Schema\Blueprint;
9+
use Illuminate\Database\Schema\Types\TinyInteger;
10+
use Illuminate\Database\Schema\Grammars\SQLiteGrammar;
811
use Illuminate\Tests\Integration\Database\DatabaseTestCase;
912

1013
/**
@@ -37,4 +40,33 @@ public function test_drop_all_views()
3740

3841
$this->assertTrue(true);
3942
}
43+
44+
public function test_register_custom_DBAL_type()
45+
{
46+
Schema::registerCustomDBALType(TinyInteger::class, TinyInteger::NAME, 'TINYINT');
47+
48+
Schema::create('test', function (Blueprint $table) {
49+
$table->string('test_column');
50+
});
51+
52+
$blueprint = new Blueprint('test', function (Blueprint $table) {
53+
$table->tinyInteger('test_column')->change();
54+
});
55+
56+
$expected = [
57+
'CREATE TEMPORARY TABLE __temp__test AS SELECT test_column FROM test',
58+
'DROP TABLE test',
59+
'CREATE TABLE test (test_column TINYINT NOT NULL COLLATE BINARY)',
60+
'INSERT INTO test (test_column) SELECT test_column FROM __temp__test',
61+
'DROP TABLE __temp__test',
62+
];
63+
64+
$statements = $blueprint->toSql($this->getConnection(), new SQLiteGrammar());
65+
66+
$blueprint->build($this->getConnection(), new SQLiteGrammar());
67+
68+
$this->assertArrayHasKey(TinyInteger::NAME, Type::getTypesMap());
69+
$this->assertEquals('tinyinteger', Schema::getColumnType('test', 'test_column'));
70+
$this->assertEquals($expected, $statements);
71+
}
4072
}

0 commit comments

Comments
 (0)