From de5bed4ec1fb633d11ca1b3ca910a0c3d6751bd5 Mon Sep 17 00:00:00 2001 From: Willem Verspyck Date: Fri, 15 Jul 2022 14:44:27 +0200 Subject: [PATCH] Add JsonArrayAgg and JsonObjectAgg --- README.md | 6 ++++- .../AST/Functions/Mysql/JsonArrayAgg.php | 14 ++++++++++ .../AST/Functions/Mysql/JsonObjectAgg.php | 14 ++++++++++ .../Functions/Mysql/JsonArrayAggTest.php | 16 ++++++++++++ .../Functions/Mysql/JsonObjectAggTest.php | 26 +++++++++++++++++++ tests/Query/MysqlTestCase.php | 2 ++ 6 files changed, 77 insertions(+), 1 deletion(-) create mode 100644 src/Query/AST/Functions/Mysql/JsonArrayAgg.php create mode 100644 src/Query/AST/Functions/Mysql/JsonObjectAgg.php create mode 100644 tests/Query/Functions/Mysql/JsonArrayAggTest.php create mode 100644 tests/Query/Functions/Mysql/JsonObjectAggTest.php diff --git a/README.md b/README.md index 29620c5..c5b354f 100644 --- a/README.md +++ b/README.md @@ -8,7 +8,7 @@ A set of extensions to Doctrine 2 that add support for json query functions. | DB | Functions | |:--:|:---------:| -| MySQL | `JSON_APPEND, JSON_ARRAY, JSON_ARRAY_APPEND, JSON_ARRAY_INSERT, JSON_CONTAINS, JSON_CONTAINS_PATH, JSON_DEPTH, JSON_EXTRACT, JSON_OVERLAPS, JSON_INSERT, JSON_KEYS, JSON_LENGTH, JSON_MERGE, JSON_MERGE_PRESERVE, JSON_MERGE_PATCH, JSON_OBJECT, JSON_PRETTY, JSON_QUOTE, JSON_REMOVE, JSON_REPLACE, JSON_SEARCH, JSON_SET, JSON_TYPE, JSON_UNQUOTE, JSON_VALID` | +| MySQL | `JSON_APPEND, JSON_ARRAY, JSON_ARRAYAGG, JSON_ARRAY_APPEND, JSON_ARRAY_INSERT, JSON_CONTAINS, JSON_CONTAINS_PATH, JSON_DEPTH, JSON_EXTRACT, JSON_OVERLAPS, JSON_INSERT, JSON_KEYS, JSON_LENGTH, JSON_MERGE, JSON_MERGE_PRESERVE, JSON_MERGE_PATCH, JSON_OBJECT, JSON_OBJECTAGG, JSON_PRETTY, JSON_QUOTE, JSON_REMOVE, JSON_REPLACE, JSON_SEARCH, JSON_SET, JSON_TYPE, JSON_UNQUOTE, JSON_VALID` | | PostgreSQL | `JSON_EXTRACT_PATH, GT, GT_GT, SHARP_GT, SHARP_GT_GT` | | MariaDb | `JSON_VALUE, JSON_EXISTS, JSON_QUERY, JSON_COMPACT, JSON_DETAILED, JSON_LOOSE, JSON_EQUALS, JSON_NORMALIZE` | | SQLite | `JSON, JSON_ARRAY, JSON_ARRAY_LENGTH, JSON_EXTRACT, JSON_GROUP_ARRAY, JSON_GROUP_OBJECT, JSON_INSERT, JSON_OBJECT, JSON_PATCH, JSON_QUOTE, JSON_REMOVE, JSON_REPLACE, JSON_SET, JSON_TYPE, JSON_VALID` | @@ -171,6 +171,8 @@ The library provides this set of DQL functions. ### Mysql 5.7+ JSON operators * [JSON_ARRAY_APPEND(json_doc, path, val[, path, val] ...)](https://dev.mysql.com/doc/refman/5.7/en/json-modification-functions.html#function_json-array-append) - Appends values to the end of the indicated arrays within a JSON document and returns the result. +* [JSON_ARRAYAGG(value)](https://dev.mysql.com/doc/refman/5.7/en/aggregate-functions.html#function_json-arrayagg) + - Aggregates a result set as a single JSON array whose elements consist of the rows. * [JSON_ARRAY_INSERT(json_doc, path, val[, path, val] ...)](https://dev.mysql.com/doc/refman/5.7/en/json-modification-functions.html#function_json-array-insert) - Updates a JSON document, inserting into an array within the document and returning the modified document. * [JSON_ARRAY([val[, val] ...])](https://dev.mysql.com/doc/refman/5.7/en/json-creation-functions.html#function_json-array) @@ -199,6 +201,8 @@ The library provides this set of DQL functions. - Performs an RFC 7396 compliant merge of two or more JSON documents and returns the merged result. * [JSON_OBJECT([key, val[, key, val] ...])](https://dev.mysql.com/doc/refman/5.7/en/json-creation-functions.html#function_json-object) - Evaluates a (possibly empty) list of key/value pairs and returns a JSON object containing those pairs. +* [JSON_OBJECTAGG(key, val)](https://dev.mysql.com/doc/refman/5.7/en/json-creation-functions.html#function_json-object) + - Takes two column names or expressions as arguments, the first of these being used as a key and the second as a value, and returns a JSON object containing key-value pairs. * [JSON_PRETTY(json_val)](https://dev.mysql.com/doc/refman/5.7/en/json-utility-functions.html#function_json-pretty) - Provides pretty-printing of JSON values similar to that implemented in PHP and by other languages and database systems * [JSON_QUOTE(json_val)](https://dev.mysql.com/doc/refman/5.7/en/json-creation-functions.html#function_json-quote) diff --git a/src/Query/AST/Functions/Mysql/JsonArrayAgg.php b/src/Query/AST/Functions/Mysql/JsonArrayAgg.php new file mode 100644 index 0000000..c37137c --- /dev/null +++ b/src/Query/AST/Functions/Mysql/JsonArrayAgg.php @@ -0,0 +1,14 @@ +assertDqlProducesSql( + "SELECT JSON_ARRAYAGG('a') FROM Scienta\DoctrineJsonFunctions\Tests\Entities\Blank b", + "SELECT JSON_ARRAYAGG('a') AS sclr_0 FROM Blank b0_" + ); + } +} diff --git a/tests/Query/Functions/Mysql/JsonObjectAggTest.php b/tests/Query/Functions/Mysql/JsonObjectAggTest.php new file mode 100644 index 0000000..b0d17d2 --- /dev/null +++ b/tests/Query/Functions/Mysql/JsonObjectAggTest.php @@ -0,0 +1,26 @@ +assertDqlProducesSql( + "SELECT JSON_OBJECTAGG('a', 1) FROM Scienta\DoctrineJsonFunctions\Tests\Entities\Blank b", + "SELECT JSON_OBJECTAGG('a', 1) AS sclr_0 FROM Blank b0_" + ); + } + + public function testJsonObjectAggMissingParameter() + { + $this->expectException(\Doctrine\ORM\Query\QueryException::class); + + $this->assertDqlProducesSql( + "SELECT JSON_OBJECTAGG('a') FROM Scienta\DoctrineJsonFunctions\Tests\Entities\Blank b", + "SELECT JSON_OBJECTAGG('a') AS sclr_0 FROM Blank b0_" + ); + } +} diff --git a/tests/Query/MysqlTestCase.php b/tests/Query/MysqlTestCase.php index a3e79c0..f0f55b6 100644 --- a/tests/Query/MysqlTestCase.php +++ b/tests/Query/MysqlTestCase.php @@ -26,6 +26,7 @@ public function setUp(): void public static function loadDqlFunctions(Configuration $configuration) { $configuration->addCustomStringFunction(DqlFunctions\JsonArray::FUNCTION_NAME, DqlFunctions\JsonArray::class); + $configuration->addCustomStringFunction(DqlFunctions\JsonArrayAgg::FUNCTION_NAME, DqlFunctions\JsonArrayAgg::class); $configuration->addCustomStringFunction(DqlFunctions\JsonArrayAppend::FUNCTION_NAME, DqlFunctions\JsonArrayAppend::class); $configuration->addCustomStringFunction(DqlFunctions\JsonArrayInsert::FUNCTION_NAME, DqlFunctions\JsonArrayInsert::class); $configuration->addCustomStringFunction(DqlFunctions\JsonContains::FUNCTION_NAME, DqlFunctions\JsonContains::class); @@ -40,6 +41,7 @@ public static function loadDqlFunctions(Configuration $configuration) $configuration->addCustomStringFunction(DqlFunctions\JsonMergePreserve::FUNCTION_NAME, DqlFunctions\JsonMergePreserve::class); $configuration->addCustomStringFunction(DqlFunctions\JsonMergePatch::FUNCTION_NAME, DqlFunctions\JsonMergePatch::class); $configuration->addCustomStringFunction(DqlFunctions\JsonObject::FUNCTION_NAME, DqlFunctions\JsonObject::class); + $configuration->addCustomStringFunction(DqlFunctions\JsonObjectAgg::FUNCTION_NAME, DqlFunctions\JsonObjectAgg::class); $configuration->addCustomStringFunction(DqlFunctions\JsonPretty::FUNCTION_NAME, DqlFunctions\JsonPretty::class); $configuration->addCustomStringFunction(DqlFunctions\JsonQuote::FUNCTION_NAME, DqlFunctions\JsonQuote::class); $configuration->addCustomStringFunction(DqlFunctions\JsonRemove::FUNCTION_NAME, DqlFunctions\JsonRemove::class);