Skip to content

Commit

Permalink
ENGCOM-6276: JSON fields support #25479
Browse files Browse the repository at this point in the history
  • Loading branch information
VladimirZaets authored Dec 31, 2019
2 parents 39c0b21 + 1eec0fe commit cbbaac2
Show file tree
Hide file tree
Showing 7 changed files with 245 additions and 0 deletions.
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
];
}
}

0 comments on commit cbbaac2

Please sign in to comment.