Skip to content

Commit bedbb37

Browse files
committed
JSON fields support
1 parent b3bd86b commit bedbb37

File tree

7 files changed

+240
-0
lines changed

7 files changed

+240
-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: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
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+
* @inheritdoc
17+
*/
18+
class Json implements DbDefinitionProcessorInterface
19+
{
20+
/**
21+
* @var Nullable
22+
*/
23+
private $nullable;
24+
25+
/**
26+
* @var ResourceConnection
27+
*/
28+
private $resourceConnection;
29+
30+
/**
31+
* @var Comment
32+
*/
33+
private $comment;
34+
35+
/**
36+
* Blob constructor.
37+
*
38+
* @param Nullable $nullable
39+
* @param Comment $comment
40+
* @param ResourceConnection $resourceConnection
41+
*/
42+
public function __construct(
43+
Nullable $nullable,
44+
Comment $comment,
45+
ResourceConnection $resourceConnection
46+
) {
47+
$this->nullable = $nullable;
48+
$this->resourceConnection = $resourceConnection;
49+
$this->comment = $comment;
50+
}
51+
52+
/**
53+
* @inheritdoc
54+
*/
55+
public function toDefinition(ElementInterface $column)
56+
{
57+
return sprintf(
58+
'%s %s %s %s',
59+
$this->resourceConnection->getConnection()->quoteIdentifier($column->getName()),
60+
$column->getType(),
61+
$this->nullable->toDefinition($column),
62+
$this->comment->toDefinition($column)
63+
);
64+
}
65+
66+
/**
67+
* @inheritdoc
68+
*/
69+
public function fromDefinition(array $data)
70+
{
71+
return $data;
72+
}
73+
}
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
namespace Magento\Framework\Setup\Declaration\Schema\Dto\Columns;
7+
8+
use Magento\Framework\Setup\Declaration\Schema\Dto\Column;
9+
use Magento\Framework\Setup\Declaration\Schema\Dto\ElementDiffAwareInterface;
10+
use Magento\Framework\Setup\Declaration\Schema\Dto\Table;
11+
12+
/**
13+
* Text column.
14+
* Declared in SQL, like: JSON
15+
*/
16+
class Json extends Column implements
17+
ElementDiffAwareInterface,
18+
ColumnNullableAwareInterface
19+
{
20+
/**Json
21+
* @var bool
22+
*/
23+
private $nullable;
24+
25+
/**
26+
* Constructor.
27+
*
28+
* @param string $name
29+
* @param string $type
30+
* @param Table $table
31+
* @param bool $nullable
32+
* @param string|null $comment
33+
* @param string|null $onCreate
34+
*/
35+
public function __construct(
36+
string $name,
37+
string $type,
38+
Table $table,
39+
bool $nullable = true,
40+
string $comment = null,
41+
string $onCreate = null
42+
) {
43+
parent::__construct($name, $type, $table, $comment, $onCreate);
44+
$this->nullable = $nullable;
45+
}
46+
47+
/**
48+
* Check whether column can be nullable.
49+
*
50+
* @return bool
51+
*/
52+
public function isNullable()
53+
{
54+
return $this->nullable;
55+
}
56+
57+
/**
58+
* @inheritdoc
59+
*/
60+
public function getDiffSensitiveParams()
61+
{
62+
return [
63+
'type' => $this->getType(),
64+
'nullable' => $this->isNullable(),
65+
'comment' => $this->getComment()
66+
];
67+
}
68+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
namespace Magento\Framework\Setup\Declaration\Schema\Dto\Factories;
7+
8+
use Magento\Framework\ObjectManagerInterface;
9+
10+
/**
11+
* Class Json
12+
*/
13+
class Json implements FactoryInterface
14+
{
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+
* {@inheritdoc}
42+
*/
43+
public function create(array $data)
44+
{
45+
return $this->objectManager->create($this->className, $data);
46+
}
47+
}

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)