Skip to content

Commit 1eec0fe

Browse files
ENGCOM-6276: JSON fields support #25479
- Merge Pull Request #25479 from akaplya/magento2:json-fields - Merged commits: 1. bedbb37 2. ba5ee6e
2 parents 902d74d + ba5ee6e commit 1eec0fe

File tree

7 files changed

+245
-0
lines changed

7 files changed

+245
-0
lines changed

app/etc/di.xml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1453,6 +1453,7 @@
14531453
<item name="primary" xsi:type="object">\Magento\Framework\Setup\Declaration\Schema\Dto\Factories\Primary</item>
14541454
<item name="foreign" xsi:type="object">\Magento\Framework\Setup\Declaration\Schema\Dto\Factories\Foreign</item>
14551455
<item name="index" xsi:type="object">\Magento\Framework\Setup\Declaration\Schema\Dto\Factories\Index</item>
1456+
<item name="json" xsi:type="object">\Magento\Framework\Setup\Declaration\Schema\Dto\Factories\Json</item>
14561457
</argument>
14571458
</arguments>
14581459
</type>
@@ -1480,6 +1481,7 @@
14801481
<item name="varchar" xsi:type="object">\Magento\Framework\Setup\Declaration\Schema\Db\MySQL\Definition\Columns\StringBinary</item>
14811482
<item name="binary" xsi:type="object">\Magento\Framework\Setup\Declaration\Schema\Db\MySQL\Definition\Columns\StringBinary</item>
14821483
<item name="varbinary" xsi:type="object">\Magento\Framework\Setup\Declaration\Schema\Db\MySQL\Definition\Columns\StringBinary</item>
1484+
<item name="json" xsi:type="object">\Magento\Framework\Setup\Declaration\Schema\Db\MySQL\Definition\Columns\Json</item>
14831485
<item name="index" xsi:type="object">\Magento\Framework\Setup\Declaration\Schema\Db\MySQL\Definition\Index</item>
14841486
<item name="unique" xsi:type="object">\Magento\Framework\Setup\Declaration\Schema\Db\MySQL\Definition\Constraints\Internal</item>
14851487
<item name="primary" xsi:type="object">\Magento\Framework\Setup\Declaration\Schema\Db\MySQL\Definition\Constraints\Internal</item>
@@ -1593,6 +1595,7 @@
15931595
<item name="datetime" xsi:type="object">Magento\Framework\Setup\SchemaListenerDefinition\TimestampDefinition</item>
15941596
<item name="date" xsi:type="object">Magento\Framework\Setup\SchemaListenerDefinition\DateDefinition</item>
15951597
<item name="boolean" xsi:type="object">Magento\Framework\Setup\SchemaListenerDefinition\BooleanDefinition</item>
1598+
<item name="json" xsi:type="object">Magento\Framework\Setup\SchemaListenerDefinition\JsonDefinition</item>
15961599
</argument>
15971600
</arguments>
15981601
</type>
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
7+
namespace Magento\Framework\Setup\Declaration\Schema\Db\MySQL\Definition\Columns;
8+
9+
use Magento\Framework\App\ResourceConnection;
10+
use Magento\Framework\Setup\Declaration\Schema\Db\DbDefinitionProcessorInterface;
11+
use Magento\Framework\Setup\Declaration\Schema\Dto\ElementInterface;
12+
13+
/**
14+
* Process json data type.
15+
*/
16+
class Json implements DbDefinitionProcessorInterface
17+
{
18+
/**
19+
* @var Nullable
20+
*/
21+
private $nullable;
22+
23+
/**
24+
* @var ResourceConnection
25+
*/
26+
private $resourceConnection;
27+
28+
/**
29+
* @var Comment
30+
*/
31+
private $comment;
32+
33+
/**
34+
* Blob constructor.
35+
*
36+
* @param Nullable $nullable
37+
* @param Comment $comment
38+
* @param ResourceConnection $resourceConnection
39+
*/
40+
public function __construct(
41+
Nullable $nullable,
42+
Comment $comment,
43+
ResourceConnection $resourceConnection
44+
) {
45+
$this->nullable = $nullable;
46+
$this->resourceConnection = $resourceConnection;
47+
$this->comment = $comment;
48+
}
49+
50+
/**
51+
* @inheritdoc
52+
*/
53+
public function toDefinition(ElementInterface $column)
54+
{
55+
return sprintf(
56+
'%s %s %s %s',
57+
$this->resourceConnection->getConnection()->quoteIdentifier($column->getName()),
58+
$column->getType(),
59+
$this->nullable->toDefinition($column),
60+
$this->comment->toDefinition($column)
61+
);
62+
}
63+
64+
/**
65+
* Returns an array of column definitions
66+
*
67+
* @param array $data
68+
* @return array
69+
*/
70+
public function fromDefinition(array $data)
71+
{
72+
return $data;
73+
}
74+
}
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
7+
namespace Magento\Framework\Setup\Declaration\Schema\Dto\Columns;
8+
9+
use Magento\Framework\Setup\Declaration\Schema\Dto\Column;
10+
use Magento\Framework\Setup\Declaration\Schema\Dto\ElementDiffAwareInterface;
11+
use Magento\Framework\Setup\Declaration\Schema\Dto\Table;
12+
13+
/**
14+
* Json
15+
*
16+
* Text column.
17+
* Declared in SQL, like: JSON
18+
*/
19+
class Json extends Column implements ElementDiffAwareInterface, ColumnNullableAwareInterface
20+
{
21+
/**
22+
* @var bool
23+
*/
24+
private $nullable;
25+
26+
/**
27+
* Constructor.
28+
*
29+
* @param string $name
30+
* @param string $type
31+
* @param Table $table
32+
* @param bool $nullable
33+
* @param string|null $comment
34+
* @param string|null $onCreate
35+
*/
36+
public function __construct(
37+
string $name,
38+
string $type,
39+
Table $table,
40+
bool $nullable = true,
41+
string $comment = null,
42+
string $onCreate = null
43+
) {
44+
parent::__construct($name, $type, $table, $comment, $onCreate);
45+
$this->nullable = $nullable;
46+
}
47+
48+
/**
49+
* Check whether column can be nullable.
50+
*
51+
* @return bool
52+
*/
53+
public function isNullable()
54+
{
55+
return $this->nullable;
56+
}
57+
58+
/**
59+
* @inheritdoc
60+
*/
61+
public function getDiffSensitiveParams()
62+
{
63+
return [
64+
'type' => $this->getType(),
65+
'nullable' => $this->isNullable(),
66+
'comment' => $this->getComment()
67+
];
68+
}
69+
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
7+
namespace Magento\Framework\Setup\Declaration\Schema\Dto\Factories;
8+
9+
use Magento\Framework\ObjectManagerInterface;
10+
11+
/**
12+
* Class Json
13+
*/
14+
class Json implements FactoryInterface
15+
{
16+
/**
17+
* @var ObjectManagerInterface
18+
*/
19+
private $objectManager;
20+
21+
/**
22+
* @var string
23+
*/
24+
private $className;
25+
26+
/**
27+
* Constructor.
28+
*
29+
* @param ObjectManagerInterface $objectManager
30+
* @param string $className
31+
*/
32+
public function __construct(
33+
ObjectManagerInterface $objectManager,
34+
$className = \Magento\Framework\Setup\Declaration\Schema\Dto\Columns\Blob::class
35+
) {
36+
$this->objectManager = $objectManager;
37+
$this->className = $className;
38+
}
39+
40+
/**
41+
* Create element using definition data array.
42+
*
43+
* @param array $data
44+
* @return \Magento\Framework\Setup\Declaration\Schema\Dto\ElementInterface|mixed
45+
*/
46+
public function create(array $data)
47+
{
48+
return $this->objectManager->create($this->className, $data);
49+
}
50+
}

lib/internal/Magento/Framework/Setup/Declaration/Schema/etc/schema.xsd

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
<xs:include schemaLocation="urn:magento:framework:Setup/Declaration/Schema/etc/types/texts/longtext.xsd" />
2121
<xs:include schemaLocation="urn:magento:framework:Setup/Declaration/Schema/etc/types/texts/mediumtext.xsd" />
2222
<xs:include schemaLocation="urn:magento:framework:Setup/Declaration/Schema/etc/types/texts/varchar.xsd" />
23+
<xs:include schemaLocation="urn:magento:framework:Setup/Declaration/Schema/etc/types/texts/json.xsd" />
2324
<xs:include schemaLocation="urn:magento:framework:Setup/Declaration/Schema/etc/types/binaries/blob.xsd" />
2425
<xs:include schemaLocation="urn:magento:framework:Setup/Declaration/Schema/etc/types/binaries/mediumblob.xsd" />
2526
<xs:include schemaLocation="urn:magento:framework:Setup/Declaration/Schema/etc/types/binaries/longblob.xsd" />
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!--
3+
/**
4+
* Copyright © Magento, Inc. All rights reserved.
5+
* See COPYING.txt for license details.
6+
*/
7+
-->
8+
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified">
9+
<xs:include schemaLocation="urn:magento:framework:Setup/Declaration/Schema/etc/types/column.xsd"/>
10+
11+
<xs:complexType name="json">
12+
<xs:complexContent>
13+
<xs:extension base="abstractColumnType">
14+
<xs:annotation>
15+
<xs:documentation>
16+
Well formatted Json object
17+
</xs:documentation>
18+
</xs:annotation>
19+
<xs:attribute name="nullable" type="xs:boolean" />
20+
</xs:extension>
21+
</xs:complexContent>
22+
</xs:complexType>
23+
</xs:schema>
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
7+
namespace Magento\Framework\Setup\SchemaListenerDefinition;
8+
9+
/**
10+
* Json type definition.
11+
*/
12+
class JsonDefinition implements DefinitionConverterInterface
13+
{
14+
/**
15+
* @inheritdoc
16+
*/
17+
public function convertToDefinition(array $definition)
18+
{
19+
return [
20+
'xsi:type' => $definition['type'],
21+
'name' => $definition['name'],
22+
'nullable' => $definition['nullable'] ?? true
23+
];
24+
}
25+
}

0 commit comments

Comments
 (0)