Skip to content

Commit 5ab6edd

Browse files
committed
Add JSON_MERGE_PATH method into mysql predicate helper
1 parent e2b24dd commit 5ab6edd

7 files changed

+41
-24
lines changed

src/Command/ProductInsertCompare.php

+5-6
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,12 @@ public function __construct(
2626
protected function execute(InputInterface $input, OutputInterface $output): int
2727
{
2828
$items = [
29-
['ext_props' => 0, 'batch' => false, 'count' => 5],
29+
['ext_props' => 0, 'batch' => false, 'count' => 3],
3030
['ext_props' => 0, 'batch' => true, 'count' => 5],
31-
['ext_props' => 4000, 'batch' => false, 'count' => 5],
31+
['ext_props' => 0, 'batch' => true, 'count' => 1000],
32+
['ext_props' => 4000, 'batch' => false, 'count' => 3],
3233
['ext_props' => 4000, 'batch' => true, 'count' => 5],
34+
['ext_props' => 4000, 'batch' => true, 'count' => 1000],
3335
];
3436
$results = [];
3537

@@ -98,10 +100,7 @@ private function createData(int $count, int $propsCount = 0): array
98100
$properties = [];
99101

100102
for ($pIndex = 1; $pIndex <= $propsCount; $pIndex++) {
101-
$properties["p_{$pIndex}"] = $faker->randomElement([
102-
$faker->randomFloat(2, 1, 100000),
103-
$faker->numberBetween(1, 10000),
104-
$faker->word
103+
$properties["p_{$pIndex}"] = $faker->randomElement([''
105104
]);
106105
}
107106

src/Helper/MysqlPredicateHelper.php

+27-13
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace App\Helper;
44

55
use Doctrine\Common\Collections\ArrayCollection;
6+
use JsonException;
67

78
class MysqlPredicateHelper extends AbstractSqlPredicateHelper
89
{
@@ -61,21 +62,34 @@ public function getWhereCollection(): ArrayCollection
6162
]));
6263
}
6364

64-
public function createJsonSetPredicate(array $data): string
65+
/**
66+
* @throws JsonException
67+
*/
68+
public function createJsonUpdatePredicate(array $data): string
6569
{
66-
$format = sprintf(
67-
'JSON_SET(properties, %s)',
68-
implode(',',
69-
array_fill(0, count($data) * 2, '%s'))
70-
);
71-
$flatData = [];
72-
foreach ($data as $key => $value) {
73-
$flatData[] = "'$.$key'";
74-
$flatData[] = match (gettype($value)) {
70+
return match (count($data)) {
71+
1 => $this->createJsonSetPredicate(array_key_first($data), current($data)),
72+
default => $this->createJsonMergePatch($data)
73+
};
74+
}
75+
76+
private function createJsonSetPredicate(string $key, float|int|string $value): string
77+
{
78+
return sprintf(
79+
"JSON_SET(properties, '$.%s', %s)",
80+
$key,
81+
match (gettype($value)) {
7582
'string' => sprintf('"%s"', $value),
7683
default => $value,
77-
};
78-
}
79-
return sprintf($format, ...$flatData);
84+
}
85+
);
86+
}
87+
88+
private function createJsonMergePatch(array $data): string
89+
{
90+
return sprintf(
91+
"JSON_MERGE_PATCH(properties, '%s')",
92+
json_encode($data, JSON_THROW_ON_ERROR)
93+
);
8094
}
8195
}

src/Helper/PostgresqlPredicateHelper.php

+5-1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace App\Helper;
44

55
use Doctrine\Common\Collections\ArrayCollection;
6+
use JsonException;
67

78
class PostgresqlPredicateHelper extends AbstractSqlPredicateHelper
89
{
@@ -64,7 +65,7 @@ public function getWhereCollection(): ArrayCollection
6465
]));
6566
}
6667

67-
public function createJsonSetPredicate(array $data): string
68+
public function createJsonUpdatePredicate(array $data): string
6869
{
6970
return match (count($data)) {
7071
1 => $this->createJsonSet(array_key_first($data), current($data)),
@@ -84,6 +85,9 @@ private function createJsonSet(string $key, string|int|float $value): string
8485
);
8586
}
8687

88+
/**
89+
* @throws JsonException
90+
*/
8791
private function createJsonMerge(array $data): string
8892
{
8993
return sprintf("properties || '%s'", json_encode($data, JSON_THROW_ON_ERROR));

src/Helper/SqlPredicateHelper.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,5 +15,5 @@ public function getSelect(): array;
1515

1616
public function getValueSetting(): SqlCompareValueSetting;
1717

18-
public function createJsonSetPredicate(array $data): string;
18+
public function createJsonUpdatePredicate(array $data): string;
1919
}

src/Service/ProductSqlUpdateService.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ public function setPredicateHelper(SqlPredicateHelper $helper): self
2626

2727
public function setPropertyKeys(array $data, int $limit = 0): DebugStack
2828
{
29-
$predicate = $this->helper->createJsonSetPredicate($data);
29+
$predicate = $this->helper->createJsonUpdatePredicate($data);
3030
$manager = $this->registry->getManager();
3131
/**@var Connection $connection*/
3232
$connection = $this->registry->getConnection($this->helper->getConnectionName());

tests/Unit/SqlPredicteHelper/MysqlPredicateHelperTest.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ class MysqlPredicateHelperTest extends AbstractSqlPredicateHelperTest
1212
{
1313
public function testCreateJsonSetPredicate(): void
1414
{
15-
$predicate = $this->helper->createJsonSetPredicate([
15+
$predicate = $this->helper->createJsonUpdatePredicate([
1616
'int_string' => '32',
1717
'int' => 23,
1818
'string' => 'string_value',

tests/Unit/SqlPredicteHelper/PostgresqlPredicateHelperTest.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ class PostgresqlPredicateHelperTest extends AbstractSqlPredicateHelperTest
1313
*/
1414
public function testCreateJsonSetPredicateSingle(array $value)
1515
{
16-
$predicate = $this->helper->createJsonSetPredicate($value);
16+
$predicate = $this->helper->createJsonUpdatePredicate($value);
1717
$sql = $this->createUpdatePropertySql($predicate);
1818
$this->explain($sql);
1919
$this->addToAssertionCount(1);

0 commit comments

Comments
 (0)