Skip to content

Commit

Permalink
Merge pull request #71 from Hikariii/json_overlaps
Browse files Browse the repository at this point in the history
added json_overlaps function
  • Loading branch information
Hikariii committed Sep 17, 2021
2 parents 355e9ef + 08046f8 commit 64c3f88
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 4 deletions.
4 changes: 3 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_INSERT, JSON_KEYS, JSON_LENGTH, JSON_MERGE, 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_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_PATCH, JSON_OBJECT, 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` |
| 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 @@ -183,6 +183,8 @@ The library provides this set of DQL functions.
- Returns the maximum depth of a JSON document.
* [JSON_EXTRACT(json_doc, path[, path] ...)](https://dev.mysql.com/doc/refman/5.7/en/json-search-functions.html#function_json-extract)
- Returns data from a JSON document, selected from the parts of the document matched by the path arguments.
* [JSON_OVERLAPS(json_doc1, json_doc2)](https://dev.mysql.com/doc/refman/8.0/en/json-search-functions.html#function_json-overlaps)
- Compares two JSON documents. Returns true (1) if the two document have any key-value pairs or array elements in common. If both arguments are scalars, the function performs a simple equality test.
* [JSON_INSERT(json_doc, path, val[, path, val] ...)](https://dev.mysql.com/doc/refman/5.7/en/json-modification-functions.html#function_json-insert)
- Inserts data into a JSON document and returns the result.
* [JSON_KEYS(json_doc[, path])](https://dev.mysql.com/doc/refman/5.7/en/json-search-functions.html#function_json-keys)
Expand Down
21 changes: 21 additions & 0 deletions src/Query/AST/Functions/Mysql/JsonOverlaps.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

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

use Doctrine\DBAL\DBALException;
use Doctrine\DBAL\Platforms\MySqlPlatform;
use Doctrine\ORM\Query\AST\Functions\FunctionNode;
use Doctrine\ORM\Query\Lexer;
use Doctrine\ORM\Query\Parser;
use Doctrine\ORM\Query\SqlWalker;

/**
* "JSON_OVERLAPS" "(" StringPrimary "," StringPrimary ")"
*/
class JsonOverlaps extends MysqlJsonFunctionNode
{
const FUNCTION_NAME = 'JSON_OVERLAPS';

/** @var string[] */
protected $requiredArgumentTypes = [self::STRING_PRIMARY_ARG, self::STRING_PRIMARY_ARG];
}
3 changes: 0 additions & 3 deletions tests/Query/DbTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

namespace Scienta\DoctrineJsonFunctions\Tests\Query;

use Doctrine\Common\Cache\ArrayCache;
use Doctrine\ORM\Configuration;
use Doctrine\ORM\EntityManager;
use Scienta\DoctrineJsonFunctions\Tests\DoctrineJsonTestcase;
Expand All @@ -22,8 +21,6 @@ abstract class DbTestCase extends DoctrineJsonTestcase
public function setUp(): void
{
$this->configuration = new Configuration();
$this->configuration->setMetadataCacheImpl(new ArrayCache());
$this->configuration->setQueryCacheImpl(new ArrayCache());
$this->configuration->setProxyDir(__DIR__ . '/Proxies');
$this->configuration->setProxyNamespace('DoctrineExtensions\Tests\Proxies');
$this->configuration->setAutoGenerateProxyClasses(true);
Expand Down
41 changes: 41 additions & 0 deletions tests/Query/Functions/Mysql/JsonOverlapsTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php

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

use Scienta\DoctrineJsonFunctions\Tests\Query\MysqlTestCase;
use Doctrine\ORM\Query\Expr;

class JsonOverlapsTest extends MysqlTestCase
{
public function testJsonOverlapsArray()
{
$this->assertDqlProducesSql(
"SELECT JSON_OVERLAPS('[\"bar\", 2]', '[2,5]') FROM Scienta\DoctrineJsonFunctions\Tests\Entities\Blank b",
"SELECT JSON_OVERLAPS('[\"bar\", 2]', '[2,5]') AS sclr_0 FROM Blank b0_"
);
}

public function testJsonOverlapsObject()
{
$this->assertDqlProducesSql(
"SELECT JSON_OVERLAPS('{\"a\":1, \"b\":10}', '{\"c\":1,\"b\":10}') FROM Scienta\DoctrineJsonFunctions\Tests\Entities\Blank b",
"SELECT JSON_OVERLAPS('{\"a\":1, \"b\":10}', '{\"c\":1,\"b\":10}') AS sclr_0 FROM Blank b0_"
);
}

public function testJsonOverlapsSingle()
{
$this->assertDqlProducesSql(
"SELECT JSON_OVERLAPS('[\"foo\", 2, 18]', '2') FROM Scienta\DoctrineJsonFunctions\Tests\Entities\Blank b",
"SELECT JSON_OVERLAPS('[\"foo\", 2, 18]', '2') AS sclr_0 FROM Blank b0_"
);
}

public function testJsonTestWhere()
{
$this->assertDqlProducesSql(
"SELECT d.id FROM Scienta\DoctrineJsonFunctions\Tests\Entities\JsonData d WHERE JSON_OVERLAPS(d.jsonData, '[2,6,8]') = 1",
"SELECT j0_.id AS id_0 FROM JsonData j0_ WHERE JSON_OVERLAPS(j0_.jsonData, '[2,6,8]') = 1"
);
}
}
1 change: 1 addition & 0 deletions tests/Query/MysqlTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ public static function loadDqlFunctions(Configuration $configuration)
$configuration->addCustomStringFunction(DqlFunctions\JsonContainsPath::FUNCTION_NAME, DqlFunctions\JsonContainsPath::class);
$configuration->addCustomStringFunction(DqlFunctions\JsonDepth::FUNCTION_NAME, DqlFunctions\JsonDepth::class);
$configuration->addCustomStringFunction(DqlFunctions\JsonExtract::FUNCTION_NAME, DqlFunctions\JsonExtract::class);
$configuration->addCustomStringFunction(DqlFunctions\JsonOverlaps::FUNCTION_NAME, DqlFunctions\JsonOverlaps::class);
$configuration->addCustomStringFunction(DqlFunctions\JsonInsert::FUNCTION_NAME, DqlFunctions\JsonInsert::class);
$configuration->addCustomStringFunction(DqlFunctions\JsonKeys::FUNCTION_NAME, DqlFunctions\JsonKeys::class);
$configuration->addCustomStringFunction(DqlFunctions\JsonLength::FUNCTION_NAME, DqlFunctions\JsonLength::class);
Expand Down

0 comments on commit 64c3f88

Please sign in to comment.