Skip to content

JSON fields support #25479

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

Merged
merged 2 commits into from
Jan 3, 2020
Merged
Show file tree
Hide file tree
Changes from all 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
3 changes: 3 additions & 0 deletions app/etc/di.xml
Original file line number Diff line number Diff line change
Expand Up @@ -1453,6 +1453,7 @@
<item name="primary" xsi:type="object">\Magento\Framework\Setup\Declaration\Schema\Dto\Factories\Primary</item>
<item name="foreign" xsi:type="object">\Magento\Framework\Setup\Declaration\Schema\Dto\Factories\Foreign</item>
<item name="index" xsi:type="object">\Magento\Framework\Setup\Declaration\Schema\Dto\Factories\Index</item>
<item name="json" xsi:type="object">\Magento\Framework\Setup\Declaration\Schema\Dto\Factories\Json</item>
</argument>
</arguments>
</type>
Expand Down Expand Up @@ -1480,6 +1481,7 @@
<item name="varchar" xsi:type="object">\Magento\Framework\Setup\Declaration\Schema\Db\MySQL\Definition\Columns\StringBinary</item>
<item name="binary" xsi:type="object">\Magento\Framework\Setup\Declaration\Schema\Db\MySQL\Definition\Columns\StringBinary</item>
<item name="varbinary" xsi:type="object">\Magento\Framework\Setup\Declaration\Schema\Db\MySQL\Definition\Columns\StringBinary</item>
<item name="json" xsi:type="object">\Magento\Framework\Setup\Declaration\Schema\Db\MySQL\Definition\Columns\Json</item>
<item name="index" xsi:type="object">\Magento\Framework\Setup\Declaration\Schema\Db\MySQL\Definition\Index</item>
<item name="unique" xsi:type="object">\Magento\Framework\Setup\Declaration\Schema\Db\MySQL\Definition\Constraints\Internal</item>
<item name="primary" xsi:type="object">\Magento\Framework\Setup\Declaration\Schema\Db\MySQL\Definition\Constraints\Internal</item>
Expand Down Expand Up @@ -1593,6 +1595,7 @@
<item name="datetime" xsi:type="object">Magento\Framework\Setup\SchemaListenerDefinition\TimestampDefinition</item>
<item name="date" xsi:type="object">Magento\Framework\Setup\SchemaListenerDefinition\DateDefinition</item>
<item name="boolean" xsi:type="object">Magento\Framework\Setup\SchemaListenerDefinition\BooleanDefinition</item>
<item name="json" xsi:type="object">Magento\Framework\Setup\SchemaListenerDefinition\JsonDefinition</item>
</argument>
</arguments>
</type>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/

namespace Magento\Framework\Setup\Declaration\Schema\Db\MySQL\Definition\Columns;

use Magento\Framework\App\ResourceConnection;
use Magento\Framework\Setup\Declaration\Schema\Db\DbDefinitionProcessorInterface;
use Magento\Framework\Setup\Declaration\Schema\Dto\ElementInterface;

/**
* Process json data type.
*/
class Json implements DbDefinitionProcessorInterface
{
/**
* @var Nullable
*/
private $nullable;

/**
* @var ResourceConnection
*/
private $resourceConnection;

/**
* @var Comment
*/
private $comment;

/**
* Blob constructor.
*
* @param Nullable $nullable
* @param Comment $comment
* @param ResourceConnection $resourceConnection
*/
public function __construct(
Nullable $nullable,
Comment $comment,
ResourceConnection $resourceConnection
) {
$this->nullable = $nullable;
$this->resourceConnection = $resourceConnection;
$this->comment = $comment;
}

/**
* @inheritdoc
*/
public function toDefinition(ElementInterface $column)
{
return sprintf(
'%s %s %s %s',
$this->resourceConnection->getConnection()->quoteIdentifier($column->getName()),
$column->getType(),
$this->nullable->toDefinition($column),
$this->comment->toDefinition($column)
);
}

/**
* Returns an array of column definitions
*
* @param array $data
* @return array
*/
public function fromDefinition(array $data)
{
return $data;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/

namespace Magento\Framework\Setup\Declaration\Schema\Dto\Columns;

use Magento\Framework\Setup\Declaration\Schema\Dto\Column;
use Magento\Framework\Setup\Declaration\Schema\Dto\ElementDiffAwareInterface;
use Magento\Framework\Setup\Declaration\Schema\Dto\Table;

/**
* Json
*
* Text column.
* Declared in SQL, like: JSON
*/
class Json extends Column implements ElementDiffAwareInterface, ColumnNullableAwareInterface
{
/**
* @var bool
*/
private $nullable;

/**
* Constructor.
*
* @param string $name
* @param string $type
* @param Table $table
* @param bool $nullable
* @param string|null $comment
* @param string|null $onCreate
*/
public function __construct(
string $name,
string $type,
Table $table,
bool $nullable = true,
string $comment = null,
string $onCreate = null
) {
parent::__construct($name, $type, $table, $comment, $onCreate);
$this->nullable = $nullable;
}

/**
* Check whether column can be nullable.
*
* @return bool
*/
public function isNullable()
{
return $this->nullable;
}

/**
* @inheritdoc
*/
public function getDiffSensitiveParams()
{
return [
'type' => $this->getType(),
'nullable' => $this->isNullable(),
'comment' => $this->getComment()
];
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/

namespace Magento\Framework\Setup\Declaration\Schema\Dto\Factories;

use Magento\Framework\ObjectManagerInterface;

/**
* Class Json
*/
class Json implements FactoryInterface
{
/**
* @var ObjectManagerInterface
*/
private $objectManager;

/**
* @var string
*/
private $className;

/**
* Constructor.
*
* @param ObjectManagerInterface $objectManager
* @param string $className
*/
public function __construct(
ObjectManagerInterface $objectManager,
$className = \Magento\Framework\Setup\Declaration\Schema\Dto\Columns\Blob::class
) {
$this->objectManager = $objectManager;
$this->className = $className;
}

/**
* Create element using definition data array.
*
* @param array $data
* @return \Magento\Framework\Setup\Declaration\Schema\Dto\ElementInterface|mixed
*/
public function create(array $data)
{
return $this->objectManager->create($this->className, $data);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
<xs:include schemaLocation="urn:magento:framework:Setup/Declaration/Schema/etc/types/texts/longtext.xsd" />
<xs:include schemaLocation="urn:magento:framework:Setup/Declaration/Schema/etc/types/texts/mediumtext.xsd" />
<xs:include schemaLocation="urn:magento:framework:Setup/Declaration/Schema/etc/types/texts/varchar.xsd" />
<xs:include schemaLocation="urn:magento:framework:Setup/Declaration/Schema/etc/types/texts/json.xsd" />
<xs:include schemaLocation="urn:magento:framework:Setup/Declaration/Schema/etc/types/binaries/blob.xsd" />
<xs:include schemaLocation="urn:magento:framework:Setup/Declaration/Schema/etc/types/binaries/mediumblob.xsd" />
<xs:include schemaLocation="urn:magento:framework:Setup/Declaration/Schema/etc/types/binaries/longblob.xsd" />
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
-->
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified">
<xs:include schemaLocation="urn:magento:framework:Setup/Declaration/Schema/etc/types/column.xsd"/>

<xs:complexType name="json">
<xs:complexContent>
<xs:extension base="abstractColumnType">
<xs:annotation>
<xs:documentation>
Well formatted Json object
</xs:documentation>
</xs:annotation>
<xs:attribute name="nullable" type="xs:boolean" />
</xs:extension>
</xs:complexContent>
</xs:complexType>
</xs:schema>
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/

namespace Magento\Framework\Setup\SchemaListenerDefinition;

/**
* Json type definition.
*/
class JsonDefinition implements DefinitionConverterInterface
{
/**
* @inheritdoc
*/
public function convertToDefinition(array $definition)
{
return [
'xsi:type' => $definition['type'],
'name' => $definition['name'],
'nullable' => $definition['nullable'] ?? true
];
}
}