Skip to content

Commit

Permalink
Add JsonArrayAgg and JsonObjectAgg
Browse files Browse the repository at this point in the history
  • Loading branch information
willemverspyck committed Jul 15, 2022
1 parent 02fcdcc commit de5bed4
Show file tree
Hide file tree
Showing 6 changed files with 77 additions and 1 deletion.
6 changes: 5 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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` |
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down
14 changes: 14 additions & 0 deletions src/Query/AST/Functions/Mysql/JsonArrayAgg.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php

namespace Scienta\DoctrineJsonFunctions\Query\AST\Functions\Mysql;

/**
* "JSON_ARRAYAGG" "(" NewValue ")"
*/
class JsonArrayAgg extends MysqlJsonFunctionNode
{
const FUNCTION_NAME = 'JSON_ARRAYAGG';

/** @var string[] */
protected $optionalArgumentTypes = [self::VALUE_ARG];
}
14 changes: 14 additions & 0 deletions src/Query/AST/Functions/Mysql/JsonObjectAgg.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php

namespace Scienta\DoctrineJsonFunctions\Query\AST\Functions\Mysql;

/**
* "JSON_OBJECTAGG" "(" StringPrimary "," NewValue ")"
*/
class JsonObjectAgg extends MysqlJsonFunctionNode
{
const FUNCTION_NAME = 'JSON_OBJECTAGG';

/** @var string[] */
protected $optionalArgumentTypes = [self::STRING_PRIMARY_ARG, self::VALUE_ARG];
}
16 changes: 16 additions & 0 deletions tests/Query/Functions/Mysql/JsonArrayAggTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

namespace Scienta\DoctrineJsonFunctions\Tests\Query\Functions\Mysql;

use Scienta\DoctrineJsonFunctions\Tests\Query\MysqlTestCase;

class JsonGroupArrayTest extends MysqlTestCase
{
public function testJsonArrayAgg()
{
$this->assertDqlProducesSql(
"SELECT JSON_ARRAYAGG('a') FROM Scienta\DoctrineJsonFunctions\Tests\Entities\Blank b",
"SELECT JSON_ARRAYAGG('a') AS sclr_0 FROM Blank b0_"
);
}
}
26 changes: 26 additions & 0 deletions tests/Query/Functions/Mysql/JsonObjectAggTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

namespace Scienta\DoctrineJsonFunctions\Tests\Query\Functions\Mysql;

use Scienta\DoctrineJsonFunctions\Tests\Query\MysqlTestCase;

class JsonObjectAggTest extends MysqlTestCase
{
public function testJsonObjectAgg()
{
$this->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_"
);
}
}
2 changes: 2 additions & 0 deletions tests/Query/MysqlTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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);
Expand Down

0 comments on commit de5bed4

Please sign in to comment.