Skip to content

Commit d2c245b

Browse files
authored
Merge pull request #84 from willemverspyck/master
Add JsonArrayAgg and JsonObjectAgg
2 parents 02fcdcc + de5bed4 commit d2c245b

File tree

6 files changed

+77
-1
lines changed

6 files changed

+77
-1
lines changed

README.md

+5-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ A set of extensions to Doctrine 2 that add support for json query functions.
88

99
| DB | Functions |
1010
|:--:|:---------:|
11-
| 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` |
11+
| 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` |
1212
| PostgreSQL | `JSON_EXTRACT_PATH, GT, GT_GT, SHARP_GT, SHARP_GT_GT` |
1313
| MariaDb | `JSON_VALUE, JSON_EXISTS, JSON_QUERY, JSON_COMPACT, JSON_DETAILED, JSON_LOOSE, JSON_EQUALS, JSON_NORMALIZE` |
1414
| 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.
171171
### Mysql 5.7+ JSON operators
172172
* [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)
173173
- Appends values to the end of the indicated arrays within a JSON document and returns the result.
174+
* [JSON_ARRAYAGG(value)](https://dev.mysql.com/doc/refman/5.7/en/aggregate-functions.html#function_json-arrayagg)
175+
- Aggregates a result set as a single JSON array whose elements consist of the rows.
174176
* [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)
175177
- Updates a JSON document, inserting into an array within the document and returning the modified document.
176178
* [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.
199201
- Performs an RFC 7396 compliant merge of two or more JSON documents and returns the merged result.
200202
* [JSON_OBJECT([key, val[, key, val] ...])](https://dev.mysql.com/doc/refman/5.7/en/json-creation-functions.html#function_json-object)
201203
- Evaluates a (possibly empty) list of key/value pairs and returns a JSON object containing those pairs.
204+
* [JSON_OBJECTAGG(key, val)](https://dev.mysql.com/doc/refman/5.7/en/json-creation-functions.html#function_json-object)
205+
- 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.
202206
* [JSON_PRETTY(json_val)](https://dev.mysql.com/doc/refman/5.7/en/json-utility-functions.html#function_json-pretty)
203207
- Provides pretty-printing of JSON values similar to that implemented in PHP and by other languages and database systems
204208
* [JSON_QUOTE(json_val)](https://dev.mysql.com/doc/refman/5.7/en/json-creation-functions.html#function_json-quote)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?php
2+
3+
namespace Scienta\DoctrineJsonFunctions\Query\AST\Functions\Mysql;
4+
5+
/**
6+
* "JSON_ARRAYAGG" "(" NewValue ")"
7+
*/
8+
class JsonArrayAgg extends MysqlJsonFunctionNode
9+
{
10+
const FUNCTION_NAME = 'JSON_ARRAYAGG';
11+
12+
/** @var string[] */
13+
protected $optionalArgumentTypes = [self::VALUE_ARG];
14+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?php
2+
3+
namespace Scienta\DoctrineJsonFunctions\Query\AST\Functions\Mysql;
4+
5+
/**
6+
* "JSON_OBJECTAGG" "(" StringPrimary "," NewValue ")"
7+
*/
8+
class JsonObjectAgg extends MysqlJsonFunctionNode
9+
{
10+
const FUNCTION_NAME = 'JSON_OBJECTAGG';
11+
12+
/** @var string[] */
13+
protected $optionalArgumentTypes = [self::STRING_PRIMARY_ARG, self::VALUE_ARG];
14+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?php
2+
3+
namespace Scienta\DoctrineJsonFunctions\Tests\Query\Functions\Mysql;
4+
5+
use Scienta\DoctrineJsonFunctions\Tests\Query\MysqlTestCase;
6+
7+
class JsonGroupArrayTest extends MysqlTestCase
8+
{
9+
public function testJsonArrayAgg()
10+
{
11+
$this->assertDqlProducesSql(
12+
"SELECT JSON_ARRAYAGG('a') FROM Scienta\DoctrineJsonFunctions\Tests\Entities\Blank b",
13+
"SELECT JSON_ARRAYAGG('a') AS sclr_0 FROM Blank b0_"
14+
);
15+
}
16+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?php
2+
3+
namespace Scienta\DoctrineJsonFunctions\Tests\Query\Functions\Mysql;
4+
5+
use Scienta\DoctrineJsonFunctions\Tests\Query\MysqlTestCase;
6+
7+
class JsonObjectAggTest extends MysqlTestCase
8+
{
9+
public function testJsonObjectAgg()
10+
{
11+
$this->assertDqlProducesSql(
12+
"SELECT JSON_OBJECTAGG('a', 1) FROM Scienta\DoctrineJsonFunctions\Tests\Entities\Blank b",
13+
"SELECT JSON_OBJECTAGG('a', 1) AS sclr_0 FROM Blank b0_"
14+
);
15+
}
16+
17+
public function testJsonObjectAggMissingParameter()
18+
{
19+
$this->expectException(\Doctrine\ORM\Query\QueryException::class);
20+
21+
$this->assertDqlProducesSql(
22+
"SELECT JSON_OBJECTAGG('a') FROM Scienta\DoctrineJsonFunctions\Tests\Entities\Blank b",
23+
"SELECT JSON_OBJECTAGG('a') AS sclr_0 FROM Blank b0_"
24+
);
25+
}
26+
}

tests/Query/MysqlTestCase.php

+2
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ public function setUp(): void
2626
public static function loadDqlFunctions(Configuration $configuration)
2727
{
2828
$configuration->addCustomStringFunction(DqlFunctions\JsonArray::FUNCTION_NAME, DqlFunctions\JsonArray::class);
29+
$configuration->addCustomStringFunction(DqlFunctions\JsonArrayAgg::FUNCTION_NAME, DqlFunctions\JsonArrayAgg::class);
2930
$configuration->addCustomStringFunction(DqlFunctions\JsonArrayAppend::FUNCTION_NAME, DqlFunctions\JsonArrayAppend::class);
3031
$configuration->addCustomStringFunction(DqlFunctions\JsonArrayInsert::FUNCTION_NAME, DqlFunctions\JsonArrayInsert::class);
3132
$configuration->addCustomStringFunction(DqlFunctions\JsonContains::FUNCTION_NAME, DqlFunctions\JsonContains::class);
@@ -40,6 +41,7 @@ public static function loadDqlFunctions(Configuration $configuration)
4041
$configuration->addCustomStringFunction(DqlFunctions\JsonMergePreserve::FUNCTION_NAME, DqlFunctions\JsonMergePreserve::class);
4142
$configuration->addCustomStringFunction(DqlFunctions\JsonMergePatch::FUNCTION_NAME, DqlFunctions\JsonMergePatch::class);
4243
$configuration->addCustomStringFunction(DqlFunctions\JsonObject::FUNCTION_NAME, DqlFunctions\JsonObject::class);
44+
$configuration->addCustomStringFunction(DqlFunctions\JsonObjectAgg::FUNCTION_NAME, DqlFunctions\JsonObjectAgg::class);
4345
$configuration->addCustomStringFunction(DqlFunctions\JsonPretty::FUNCTION_NAME, DqlFunctions\JsonPretty::class);
4446
$configuration->addCustomStringFunction(DqlFunctions\JsonQuote::FUNCTION_NAME, DqlFunctions\JsonQuote::class);
4547
$configuration->addCustomStringFunction(DqlFunctions\JsonRemove::FUNCTION_NAME, DqlFunctions\JsonRemove::class);

0 commit comments

Comments
 (0)