Commit 25e695f6 authored by Steve Müller's avatar Steve Müller

add native JSON type declaration to platform

parent 2196da9c
...@@ -268,6 +268,21 @@ abstract class AbstractPlatform ...@@ -268,6 +268,21 @@ abstract class AbstractPlatform
return $this->getVarcharTypeDeclarationSQL($field); return $this->getVarcharTypeDeclarationSQL($field);
} }
/**
* Returns the SQL snippet to declare a JSON field.
*
* By default this maps directly to a CLOB and only maps to more
* special datatypes when the underlying databases support this datatype.
*
* @param array $field
*
* @return string
*/
public function getJsonTypeDeclarationSQL(array $field)
{
return $this->getClobTypeDeclarationSQL($field);
}
/** /**
* @param integer $length * @param integer $length
* @param boolean $fixed * @param boolean $fixed
...@@ -2874,6 +2889,16 @@ abstract class AbstractPlatform ...@@ -2874,6 +2889,16 @@ abstract class AbstractPlatform
return false; return false;
} }
/**
* Does this platform have native JSON type.
*
* @return boolean
*/
public function hasNativeJsonType()
{
return false;
}
/** /**
* @deprecated * @deprecated
* @todo Remove in 3.0 * @todo Remove in 3.0
......
...@@ -34,7 +34,7 @@ class JsonArrayType extends Type ...@@ -34,7 +34,7 @@ class JsonArrayType extends Type
*/ */
public function getSQLDeclaration(array $fieldDeclaration, AbstractPlatform $platform) public function getSQLDeclaration(array $fieldDeclaration, AbstractPlatform $platform)
{ {
return $platform->getClobTypeDeclarationSQL($fieldDeclaration); return $platform->getJsonTypeDeclarationSQL($fieldDeclaration);
} }
/** /**
...@@ -76,6 +76,6 @@ class JsonArrayType extends Type ...@@ -76,6 +76,6 @@ class JsonArrayType extends Type
*/ */
public function requiresSQLCommentHint(AbstractPlatform $platform) public function requiresSQLCommentHint(AbstractPlatform $platform)
{ {
return true; return ! $platform->hasNativeJsonType();
} }
} }
...@@ -29,6 +29,7 @@ class TypeConversionTest extends \Doctrine\Tests\DbalFunctionalTestCase ...@@ -29,6 +29,7 @@ class TypeConversionTest extends \Doctrine\Tests\DbalFunctionalTestCase
$table->addColumn('test_time', 'time', array('notnull' => false)); $table->addColumn('test_time', 'time', array('notnull' => false));
$table->addColumn('test_text', 'text', array('notnull' => false)); $table->addColumn('test_text', 'text', array('notnull' => false));
$table->addColumn('test_array', 'array', array('notnull' => false)); $table->addColumn('test_array', 'array', array('notnull' => false));
$table->addColumn('test_json_array', 'json_array', array('notnull' => false));
$table->addColumn('test_object', 'object', array('notnull' => false)); $table->addColumn('test_object', 'object', array('notnull' => false));
$table->addColumn('test_float', 'float', array('notnull' => false)); $table->addColumn('test_float', 'float', array('notnull' => false));
$table->addColumn('test_decimal', 'decimal', array('notnull' => false, 'scale' => 2, 'precision' => 10)); $table->addColumn('test_decimal', 'decimal', array('notnull' => false, 'scale' => 2, 'precision' => 10));
...@@ -61,6 +62,7 @@ class TypeConversionTest extends \Doctrine\Tests\DbalFunctionalTestCase ...@@ -61,6 +62,7 @@ class TypeConversionTest extends \Doctrine\Tests\DbalFunctionalTestCase
array('time', new \DateTime('10:10:10'), 'DateTime'), array('time', new \DateTime('10:10:10'), 'DateTime'),
array('text', str_repeat('foo ', 1000), 'string'), array('text', str_repeat('foo ', 1000), 'string'),
array('array', array('foo' => 'bar'), 'array'), array('array', array('foo' => 'bar'), 'array'),
array('json_array', array('foo' => 'bar'), 'array'),
array('object', $obj, 'object'), array('object', $obj, 'object'),
array('float', 1.5, 'float'), array('float', 1.5, 'float'),
array('decimal', 1.55, 'string'), array('decimal', 1.55, 'string'),
......
...@@ -31,6 +31,14 @@ class MockPlatform extends \Doctrine\DBAL\Platforms\AbstractPlatform ...@@ -31,6 +31,14 @@ class MockPlatform extends \Doctrine\DBAL\Platforms\AbstractPlatform
return 'DUMMYCLOB'; return 'DUMMYCLOB';
} }
/**
* {@inheritdoc}
*/
public function getJsonTypeDeclarationSQL(array $field)
{
return 'DUMMYJSON';
}
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
......
...@@ -8,6 +8,7 @@ use Doctrine\DBAL\Platforms\AbstractPlatform; ...@@ -8,6 +8,7 @@ use Doctrine\DBAL\Platforms\AbstractPlatform;
use Doctrine\DBAL\Schema\ForeignKeyConstraint; use Doctrine\DBAL\Schema\ForeignKeyConstraint;
use Doctrine\DBAL\Schema\Table; use Doctrine\DBAL\Schema\Table;
use Doctrine\DBAL\Schema\TableDiff; use Doctrine\DBAL\Schema\TableDiff;
use Doctrine\DBAL\Types\Type;
abstract class AbstractPlatformTestCase extends \Doctrine\Tests\DbalTestCase abstract class AbstractPlatformTestCase extends \Doctrine\Tests\DbalTestCase
{ {
...@@ -575,4 +576,29 @@ abstract class AbstractPlatformTestCase extends \Doctrine\Tests\DbalTestCase ...@@ -575,4 +576,29 @@ abstract class AbstractPlatformTestCase extends \Doctrine\Tests\DbalTestCase
{ {
$this->_platform->getBinaryTypeDeclarationSQL(array()); $this->_platform->getBinaryTypeDeclarationSQL(array());
} }
/**
* @group DBAL-553
*/
public function hasNativeJsonType()
{
$this->assertFalse($this->_platform->hasNativeJsonType());
}
/**
* @group DBAL-553
*/
public function testReturnsJsonTypeDeclarationSQL()
{
$column = array(
'length' => 666,
'notnull' => true,
'type' => Type::getType('json_array'),
);
$this->assertSame(
$this->_platform->getClobTypeDeclarationSQL($column),
$this->_platform->getJsonTypeDeclarationSQL($column)
);
}
} }
<?php
namespace Doctrine\Tests\DBAL\Types;
use Doctrine\DBAL\Types\Type;
use Doctrine\Tests\DBAL\Mocks\MockPlatform;
require_once __DIR__ . '/../../TestInit.php';
class JsonArrayTest extends \Doctrine\Tests\DbalTestCase
{
/**
* @var \Doctrine\Tests\DBAL\Mocks\MockPlatform
*/
protected $platform;
/**
* @var \Doctrine\DBAL\Types\JsonArrayType
*/
protected $type;
/**
* {@inheritdoc}
*/
protected function setUp()
{
$this->platform = new MockPlatform();
$this->type = Type::getType('json_array');
}
public function testReturnsBindingType()
{
$this->assertSame(\PDO::PARAM_STR, $this->type->getBindingType());
}
public function testReturnsName()
{
$this->assertSame(Type::JSON_ARRAY, $this->type->getName());
}
public function testReturnsSQLDeclaration()
{
$this->assertSame('DUMMYJSON', $this->type->getSQLDeclaration(array(), $this->platform));
}
public function testJsonNullConvertsToPHPValue()
{
$this->assertSame(array(), $this->type->convertToPHPValue(null, $this->platform));
}
public function testJsonStringConvertsToPHPValue()
{
$value = array('foo' => 'bar', 'bar' => 'foo');
$databaseValue = json_encode($value);
$phpValue = $this->type->convertToPHPValue($databaseValue, $this->platform);
$this->assertEquals($value, $phpValue);
}
public function testJsonResourceConvertsToPHPValue()
{
$value = array('foo' => 'bar', 'bar' => 'foo');
$databaseValue = fopen('data://text/plain;base64,' . base64_encode(json_encode($value)), 'r');
$phpValue = $this->type->convertToPHPValue($databaseValue, $this->platform);
$this->assertSame($value, $phpValue);
}
public function testRequiresSQLCommentHint()
{
$this->assertTrue($this->type->requiresSQLCommentHint($this->platform));
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment