Skip to content

Commit

Permalink
Merge pull request doctrine#2266 from ismailbaskin/master
Browse files Browse the repository at this point in the history
[MySQL] Add platform for version >=5.7.8 with native JSON type support
  • Loading branch information
deeky666 committed Jan 22, 2016
2 parents eb48797 + 103ea21 commit 217ea73
Show file tree
Hide file tree
Showing 8 changed files with 186 additions and 2 deletions.
1 change: 1 addition & 0 deletions docs/en/reference/platforms.rst
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ MySQL

- ``MySqlPlatform`` for version 5.0 and above.
- ``MySQL57Platform`` for version 5.7 and above.
- ``MySQL578Platform`` for version 5.7.8 and above.

Oracle
^^^^^^
Expand Down
5 changes: 5 additions & 0 deletions lib/Doctrine/DBAL/Driver/AbstractMySQLDriver.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
use Doctrine\DBAL\DBALException;
use Doctrine\DBAL\Driver;
use Doctrine\DBAL\Exception;
use Doctrine\DBAL\Platforms\MySQL578Platform;
use Doctrine\DBAL\Platforms\MySQL57Platform;
use Doctrine\DBAL\Platforms\MySqlPlatform;
use Doctrine\DBAL\Schema\MySqlSchemaManager;
Expand Down Expand Up @@ -140,6 +141,10 @@ public function createDatabasePlatformForVersion($version)
$patchVersion = isset($versionParts['patch']) ? $versionParts['patch'] : 0;
$version = $majorVersion . '.' . $minorVersion . '.' . $patchVersion;

if (version_compare($version, '5.7.8', '>=')) {
return new MySQL578Platform();
}

if (version_compare($version, '5.7', '>=')) {
return new MySQL57Platform();
}
Expand Down
57 changes: 57 additions & 0 deletions lib/Doctrine/DBAL/Platforms/Keywords/MySQL578Keywords.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
<?php
/*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* This software consists of voluntary contributions made by many individuals
* and is licensed under the MIT license. For more information, see
* <http://www.doctrine-project.org>.
*/

namespace Doctrine\DBAL\Platforms\Keywords;

/**
* MySQL 5.7.8 reserved keywords list.
*
* @author Steve Müller <st.mueller@dzh-online.de>
* @link www.doctrine-project.org
* @since 2.6
*/
class MySQL578Keywords extends MySQL57Keywords
{
/**
* {@inheritdoc}
*/
public function getName()
{
return 'MySQL578';
}

/**
* {@inheritdoc}
*/
protected function getKeywords()
{
return array_merge(
array_diff(
parent::getKeywords(),
array('NONBLOCKING')
),
array(
'GENERATED',
'OPTIMIZER_COSTS',
'STORED',
'VIRTUAL'
)
);
}
}
2 changes: 1 addition & 1 deletion lib/Doctrine/DBAL/Platforms/Keywords/MySQL57Keywords.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public function getName()
/**
* {@inheritdoc}
*
* @link http://dev.mysql.com/doc/mysqld-version-reference/en/mysqld-version-reference-reservedwords-5-7.html
* @link https://dev.mysql.com/doc/refman/5.7/en/keywords.html
*/
protected function getKeywords()
{
Expand Down
65 changes: 65 additions & 0 deletions lib/Doctrine/DBAL/Platforms/MySQL578Platform.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
<?php
/*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* This software consists of voluntary contributions made by many individuals
* and is licensed under the MIT license. For more information, see
* <http://www.doctrine-project.org>.
*/

namespace Doctrine\DBAL\Platforms;

/**
* Provides the behavior, features and SQL dialect of the MySQL >=5.7.8 database platform.
* Extends for json support >=5.7.8 version
*
* @author İsmail BASKIN <ismailbaskin1@gmail.com>
* @link www.doctrine-project.org
* @since 2.6
*/
class MySQL578Platform extends MySQL57Platform
{
/**
* {@inheritdoc}
*/
protected function getReservedKeywordsClass()
{
return 'Doctrine\DBAL\Platforms\Keywords\MySQL578Keywords';
}

/**
* {@inheritdoc}
*/
protected function initializeDoctrineTypeMappings()
{
parent::initializeDoctrineTypeMappings();

$this->doctrineTypeMapping['json'] = 'json_array';
}

/**
* {@inheritdoc}
*/
public function hasNativeJsonType()
{
return true;
}

/**
* {@inheritdoc}
*/
public function getJsonTypeDeclarationSQL(array $field)
{
return 'JSON';
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ class ReservedWordsCommand extends Command
private $keywordListClasses = array(
'mysql' => 'Doctrine\DBAL\Platforms\Keywords\MySQLKeywords',
'mysql57' => 'Doctrine\DBAL\Platforms\Keywords\MySQL57Keywords',
'mysql578' => 'Doctrine\DBAL\Platforms\Keywords\MySQL578Keywords',
'sqlserver' => 'Doctrine\DBAL\Platforms\Keywords\SQLServerKeywords',
'sqlserver2005' => 'Doctrine\DBAL\Platforms\Keywords\SQLServer2005Keywords',
'sqlserver2008' => 'Doctrine\DBAL\Platforms\Keywords\SQLServer2008Keywords',
Expand Down Expand Up @@ -93,6 +94,7 @@ protected function configure()
* mysql
* mysql57
* mysql578
* pgsql
* pgsql92
* sqlite
Expand Down Expand Up @@ -123,6 +125,7 @@ protected function execute(InputInterface $input, OutputInterface $output)
$keywordLists = array(
'mysql',
'mysql57',
'mysql578',
'pgsql',
'pgsql92',
'sqlite',
Expand Down
3 changes: 2 additions & 1 deletion tests/Doctrine/Tests/DBAL/Driver/AbstractMySQLDriverTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,8 @@ protected function getDatabasePlatformsForVersions()
array('5.7', 'Doctrine\DBAL\Platforms\MySQL57Platform'),
array('5.7.0', 'Doctrine\DBAL\Platforms\MySQL57Platform'),
array('5.7.1', 'Doctrine\DBAL\Platforms\MySQL57Platform'),
array('6', 'Doctrine\DBAL\Platforms\MySQL57Platform'),
array('5.7.8', 'Doctrine\DBAL\Platforms\MySQL578Platform'),
array('6', 'Doctrine\DBAL\Platforms\MySQL578Platform'),
array('10.0.15-MariaDB-1~wheezy', 'Doctrine\DBAL\Platforms\MySqlPlatform'),
array('10.1.2a-MariaDB-a1~lenny-log', 'Doctrine\DBAL\Platforms\MySqlPlatform'),
array('5.5.40-MariaDB-1~wheezy', 'Doctrine\DBAL\Platforms\MySqlPlatform'),
Expand Down
52 changes: 52 additions & 0 deletions tests/Doctrine/Tests/DBAL/Platforms/MySQL578PlatformTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<?php

namespace Doctrine\Tests\DBAL\Platforms;

use Doctrine\DBAL\Platforms\MySQL578Platform;

class MySQL578PlatformTest extends MySQL57PlatformTest
{
/**
* {@inheritdoc}
*/
public function createPlatform()
{
return new MySQL578Platform();
}

/**
* @group DBAL-2266
*/
public function testHasNativeJsonType()
{
$this->assertTrue($this->_platform->hasNativeJsonType());
}

/**
* @group DBAL-2266
*/
public function testReturnsJsonTypeDeclarationSQL()
{
$this->assertSame('JSON', $this->_platform->getJsonTypeDeclarationSQL(array()));
}

/**
* @group DBAL-2266
*/
public function testInitializesJsonTypeMapping()
{
$this->assertTrue($this->_platform->hasDoctrineTypeMappingFor('json'));
$this->assertEquals('json_array', $this->_platform->getDoctrineTypeMapping('json'));
}

/**
* @group DBAL-2266
*/
public function testReturnsReservedKeywordsClass()
{
$this->assertInstanceOf(
'Doctrine\DBAL\Platforms\Keywords\MySQL578Keywords',
$this->_platform->getReservedKeywordsList()
);
}
}

0 comments on commit 217ea73

Please sign in to comment.