Skip to content

Commit

Permalink
修复 MySQL 的 json 操作时,无法用双引号把字段名括起来 (#494)
Browse files Browse the repository at this point in the history
* 修复 MySQL 的 json 操作时,无法用双引号把字段名括起来

* 更新测试
  • Loading branch information
Yurunsoft committed Apr 14, 2023
1 parent 9d9b07f commit 87c5862
Show file tree
Hide file tree
Showing 6 changed files with 8 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,5 +30,5 @@ class QueryCurdTest extends QueryCurdBaseTest
*
* @var string
*/
protected $expectedTestJsonSelectSql = 'select * from `tb_test_json` where `json_data`->"$.uid" = :p1';
protected $expectedTestJsonSelectSql = 'select * from `tb_test_json` where `json_data`->\'$.uid\' = :p1';
}
2 changes: 1 addition & 1 deletion src/Db/Mysql/Query/Builder/UpdateBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ public function parseJsonSet(array $jsonSets): string
$item = $field . '=JSON_SET(' . $field;
foreach ($options as $option)
{
$item .= ',"$.' . implode('.', $option['jsonKeywords']) . '",' . ($option['raw'] ?? $option['valueParam']);
$item .= ',\'$.' . implode('.', $option['jsonKeywords']) . '\',' . ($option['raw'] ?? $option['valueParam']);
}
$result[] = $item . ')';
}
Expand Down
2 changes: 1 addition & 1 deletion src/Db/Mysql/Query/MysqlQuery.php
Original file line number Diff line number Diff line change
Expand Up @@ -403,7 +403,7 @@ public function parseKeywordToText(array $keywords, ?string $alias = null, ?arra
}
if (null !== $jsonKeywords)
{
$result .= '->"$.' . implode('.', $jsonKeywords) . '"';
$result .= '->\'$.' . implode('.', $jsonKeywords) . '\'';
}
if (!Text::isEmpty($alias))
{
Expand Down
2 changes: 1 addition & 1 deletion tests/unit/Component/Tests/Db/Mysqli/QueryCurdTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,5 +30,5 @@ class QueryCurdTest extends QueryCurdBaseTest
*
* @var string
*/
protected $expectedTestJsonSelectSql = 'select * from `tb_test_json` where `json_data`->"$.uid" = :p1';
protected $expectedTestJsonSelectSql = 'select * from `tb_test_json` where `json_data`->\'$.uid\' = :p1';
}
2 changes: 1 addition & 1 deletion tests/unit/Component/Tests/Db/Pdo/QueryCurdTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,5 +30,5 @@ class QueryCurdTest extends QueryCurdBaseTest
*
* @var string
*/
protected $expectedTestJsonSelectSql = 'select * from `tb_test_json` where `json_data`->"$.uid" = :p1';
protected $expectedTestJsonSelectSql = 'select * from `tb_test_json` where `json_data`->\'$.uid\' = :p1';
}
5 changes: 3 additions & 2 deletions tests/unit/Component/Tests/Db/QueryCurdBaseTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -387,7 +387,7 @@ public function testRawAlias(array $args): void
public function testJson(): void
{
$query = Db::query($this->poolName);
$jsonStr = '{"uid": "' . ($uid = uniqid('', true)) . '", "name": "aaa", "list1": [{"id": 1}]}';
$jsonStr = '{"uid": "' . ($uid = uniqid('', true)) . '", "name": "aaa", "list1": [{"id": 1}], "测试": {"值": "imi"}}';
// 插入数据
$insertResult = $query->from('tb_test_json')->insert([
'json_data' => $jsonStr,
Expand All @@ -406,11 +406,12 @@ public function testJson(): void
'json_data->name' => 'bbb',
'json_data->list1[0].id' => '2',
'json_data->list2' => [1, 2, 3],
'json_data->"测试"."值"' => 'imi niubi',
]);
$result = $query->from('tb_test_json')->where('json_data->uid', '=', $uid)->order('json_data->uid')->select();
$this->assertEquals([
'id' => $id,
'json_data' => '{"a": "1", "uid": "' . $uid . '", "name": "bbb", "list1": [{"id": "2"}], "list2": [1, 2, 3]}',
'json_data' => '{"a": "1", "uid": "' . $uid . '", "name": "bbb", "list1": [{"id": "2"}], "list2": [1, 2, 3], "测试": {"值": "imi niubi"}}',
], $result->get());
}
}

0 comments on commit 87c5862

Please sign in to comment.