Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add native JSON type support for MySQL >=5.7.8 #2266

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -136,6 +137,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
56 changes: 56 additions & 0 deletions lib/Doctrine/DBAL/Platforms/MySQL578Platform.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
<?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 <[email protected]>
* @link www.doctrine-project.org
* @since 2.5
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@since 2.6

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

*/
class MySQL578Platform extends MySQL57Platform
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we introduce a new platform, we also need to introduce a new reserved keywords class. See MySQL57Platform for reference. I had the time to get a diff of keywords between 5.7.0 and 5.7.8 (according to the documentation:

New reserved keywords

  • GENERATED
  • OPTIMIZER_COSTS
  • STORED
  • VIRTUAL

Removed reserved keywords

  • NONBLOCKING

The new keywords class can simply extend MySQL57Keywords class and then you can array_merge and array_diff the 5.7.0 keywords from parent.

Please also update the ReservedWordsCommand command.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

{
/**
* {@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';
}
}
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
41 changes: 41 additions & 0 deletions tests/Doctrine/Tests/DBAL/Platforms/MySQL578PlatformTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php

namespace Doctrine\Tests\DBAL\Platforms;

use Doctrine\DBAL\Platforms\MySQL578Platform;

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

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

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

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