-
-
Notifications
You must be signed in to change notification settings - Fork 56
feat: add (limited) support for json_exists, json_query, json_scalar, json_serialize and json_value
#277
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
feat: add (limited) support for json_exists, json_query, json_scalar, json_serialize and json_value
#277
Changes from 3 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
f0f3c9f
feat: add support for `json_exists`, `json_query`, `json_scalar`, `js…
martin-georgiev a960738
Merge branch 'main' into new-json-functions
martin-georgiev 831e7d6
feat: add support for `json_exists`, `json_query`, `json_scalar`, `js…
martin-georgiev cd6cc4f
improve documentation for the implemented support and the limitations…
martin-georgiev File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
23 changes: 23 additions & 0 deletions
23
src/MartinGeorgiev/Doctrine/ORM/Query/AST/Functions/JsonExists.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,23 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace MartinGeorgiev\Doctrine\ORM\Query\AST\Functions; | ||
|
|
||
| /** | ||
| * Implementation of PostgreSQL JSON_EXISTS(). | ||
| * | ||
| * @see https://www.postgresql.org/docs/17/functions-json.html | ||
| * @since 2.10 | ||
| * | ||
| * @author Martin Georgiev <martin.georgiev@gmail.com> | ||
| */ | ||
| class JsonExists extends BaseFunction | ||
| { | ||
| protected function customizeFunction(): void | ||
| { | ||
| $this->setFunctionPrototype('json_exists(%s, %s)'); | ||
| $this->addNodeMapping('StringPrimary'); | ||
| $this->addNodeMapping('StringPrimary'); | ||
| } | ||
| } |
23 changes: 23 additions & 0 deletions
23
src/MartinGeorgiev/Doctrine/ORM/Query/AST/Functions/JsonQuery.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,23 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace MartinGeorgiev\Doctrine\ORM\Query\AST\Functions; | ||
|
|
||
| /** | ||
| * Implementation of PostgreSQL JSON_QUERY(). | ||
| * | ||
| * @see https://www.postgresql.org/docs/17/functions-json.html | ||
| * @since 2.10 | ||
| * | ||
| * @author Martin Georgiev <martin.georgiev@gmail.com> | ||
| */ | ||
| class JsonQuery extends BaseFunction | ||
| { | ||
| protected function customizeFunction(): void | ||
| { | ||
| $this->setFunctionPrototype('json_query(%s, %s)'); | ||
| $this->addNodeMapping('StringPrimary'); | ||
| $this->addNodeMapping('StringPrimary'); | ||
| } | ||
| } |
22 changes: 22 additions & 0 deletions
22
src/MartinGeorgiev/Doctrine/ORM/Query/AST/Functions/JsonScalar.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,22 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace MartinGeorgiev\Doctrine\ORM\Query\AST\Functions; | ||
|
|
||
| /** | ||
| * Implementation of PostgreSQL JSON_SCALAR(). | ||
| * | ||
| * @see https://www.postgresql.org/docs/17/functions-json.html | ||
| * @since 2.10 | ||
| * | ||
| * @author Martin Georgiev <martin.georgiev@gmail.com> | ||
| */ | ||
| class JsonScalar extends BaseFunction | ||
| { | ||
| protected function customizeFunction(): void | ||
| { | ||
| $this->setFunctionPrototype('json_scalar(%s)'); | ||
| $this->addNodeMapping('StringPrimary'); | ||
| } | ||
| } |
25 changes: 25 additions & 0 deletions
25
src/MartinGeorgiev/Doctrine/ORM/Query/AST/Functions/JsonSerialize.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,25 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace MartinGeorgiev\Doctrine\ORM\Query\AST\Functions; | ||
|
|
||
| /** | ||
| * Implementation of PostgreSQL JSON_SERIALIZE(). | ||
| * | ||
| * Supports basic form: | ||
| * - json_serialize(expression) | ||
| * | ||
| * @see https://www.postgresql.org/docs/17/functions-json.html | ||
| * @since 2.10 | ||
| * | ||
| * @author Martin Georgiev <martin.georgiev@gmail.com> | ||
| */ | ||
| class JsonSerialize extends BaseFunction | ||
| { | ||
| protected function customizeFunction(): void | ||
| { | ||
| $this->setFunctionPrototype('json_serialize(%s)'); | ||
| $this->addNodeMapping('StringPrimary'); | ||
| } | ||
| } |
23 changes: 23 additions & 0 deletions
23
src/MartinGeorgiev/Doctrine/ORM/Query/AST/Functions/JsonValue.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,23 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace MartinGeorgiev\Doctrine\ORM\Query\AST\Functions; | ||
|
|
||
| /** | ||
| * Implementation of PostgreSQL JSON_VALUE(). | ||
| * | ||
| * @see https://www.postgresql.org/docs/17/functions-json.html | ||
| * @since 2.10 | ||
| * | ||
| * @author Martin Georgiev <martin.georgiev@gmail.com> | ||
| */ | ||
| class JsonValue extends BaseFunction | ||
| { | ||
| protected function customizeFunction(): void | ||
| { | ||
| $this->setFunctionPrototype('json_value(%s, %s)'); | ||
| $this->addNodeMapping('StringPrimary'); | ||
| $this->addNodeMapping('StringPrimary'); | ||
| } | ||
| } |
39 changes: 39 additions & 0 deletions
39
tests/MartinGeorgiev/Doctrine/ORM/Query/AST/Functions/JsonExistsTest.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,39 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace Tests\MartinGeorgiev\Doctrine\ORM\Query\AST\Functions; | ||
|
|
||
| use Fixtures\MartinGeorgiev\Doctrine\Entity\ContainsJsons; | ||
| use MartinGeorgiev\Doctrine\ORM\Query\AST\Functions\JsonExists; | ||
|
|
||
| class JsonExistsTest extends TestCase | ||
| { | ||
| protected function getStringFunctions(): array | ||
| { | ||
| return [ | ||
| 'JSON_EXISTS' => JsonExists::class, | ||
| ]; | ||
| } | ||
|
|
||
| protected function getExpectedSqlStatements(): array | ||
| { | ||
| return [ | ||
| // Basic usage | ||
| "SELECT json_exists(c0_.object1, '$.name') AS sclr_0 FROM ContainsJsons c0_", | ||
| // Nested path | ||
| "SELECT json_exists(c0_.object1, '$.address.city') AS sclr_0 FROM ContainsJsons c0_", | ||
| // Array element | ||
| "SELECT json_exists(c0_.object1, '$.items[0]') AS sclr_0 FROM ContainsJsons c0_", | ||
| ]; | ||
| } | ||
|
|
||
| protected function getDqlStatements(): array | ||
| { | ||
| return [ | ||
| \sprintf("SELECT JSON_EXISTS(e.object1, '$.name') FROM %s e", ContainsJsons::class), | ||
| \sprintf("SELECT JSON_EXISTS(e.object1, '$.address.city') FROM %s e", ContainsJsons::class), | ||
| \sprintf("SELECT JSON_EXISTS(e.object1, '$.items[0]') FROM %s e", ContainsJsons::class), | ||
| ]; | ||
| } | ||
| } | ||
42 changes: 42 additions & 0 deletions
42
tests/MartinGeorgiev/Doctrine/ORM/Query/AST/Functions/JsonQueryTest.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,42 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace Tests\MartinGeorgiev\Doctrine\ORM\Query\AST\Functions; | ||
|
|
||
| use Fixtures\MartinGeorgiev\Doctrine\Entity\ContainsJsons; | ||
| use MartinGeorgiev\Doctrine\ORM\Query\AST\Functions\JsonQuery; | ||
|
|
||
| class JsonQueryTest extends TestCase | ||
| { | ||
| protected function getStringFunctions(): array | ||
| { | ||
| return [ | ||
| 'JSON_QUERY' => JsonQuery::class, | ||
| ]; | ||
| } | ||
|
|
||
| protected function getExpectedSqlStatements(): array | ||
| { | ||
| return [ | ||
| "SELECT json_query(c0_.object1, '$.items[*]') AS sclr_0 FROM ContainsJsons c0_", | ||
| "SELECT json_query(c0_.object1, '$.address') AS sclr_0 FROM ContainsJsons c0_", | ||
| // Additional test cases for important scenarios | ||
| "SELECT json_query(c0_.object1, '$.store.book[*].author') AS sclr_0 FROM ContainsJsons c0_", | ||
| "SELECT json_query(c0_.object1, '$.store.book[0 to 2]') AS sclr_0 FROM ContainsJsons c0_", | ||
| "SELECT json_query(c0_.object1, '$.store.book[*]?(@.price > 10)') AS sclr_0 FROM ContainsJsons c0_", | ||
| ]; | ||
| } | ||
|
|
||
| protected function getDqlStatements(): array | ||
| { | ||
| return [ | ||
| \sprintf("SELECT JSON_QUERY(e.object1, '$.items[*]') FROM %s e", ContainsJsons::class), | ||
| \sprintf("SELECT JSON_QUERY(e.object1, '$.address') FROM %s e", ContainsJsons::class), | ||
| // Additional test cases matching the SQL statements above | ||
| \sprintf("SELECT JSON_QUERY(e.object1, '$.store.book[*].author') FROM %s e", ContainsJsons::class), | ||
| \sprintf("SELECT JSON_QUERY(e.object1, '$.store.book[0 to 2]') FROM %s e", ContainsJsons::class), | ||
| \sprintf("SELECT JSON_QUERY(e.object1, '$.store.book[*]?(@.price > 10)') FROM %s e", ContainsJsons::class), | ||
| ]; | ||
| } | ||
| } |
32 changes: 32 additions & 0 deletions
32
tests/MartinGeorgiev/Doctrine/ORM/Query/AST/Functions/JsonScalarTest.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,32 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace Tests\MartinGeorgiev\Doctrine\ORM\Query\AST\Functions; | ||
|
|
||
| use Fixtures\MartinGeorgiev\Doctrine\Entity\ContainsJsons; | ||
| use MartinGeorgiev\Doctrine\ORM\Query\AST\Functions\JsonScalar; | ||
|
|
||
| class JsonScalarTest extends TestCase | ||
| { | ||
| protected function getStringFunctions(): array | ||
| { | ||
| return [ | ||
| 'JSON_SCALAR' => JsonScalar::class, | ||
| ]; | ||
| } | ||
|
|
||
| protected function getExpectedSqlStatements(): array | ||
| { | ||
| return [ | ||
| 'SELECT json_scalar(c0_.object1) AS sclr_0 FROM ContainsJsons c0_', | ||
| ]; | ||
| } | ||
|
|
||
| protected function getDqlStatements(): array | ||
| { | ||
| return [ | ||
| \sprintf('SELECT JSON_SCALAR(e.object1) FROM %s e', ContainsJsons::class), | ||
| ]; | ||
| } | ||
| } |
39 changes: 39 additions & 0 deletions
39
tests/MartinGeorgiev/Doctrine/ORM/Query/AST/Functions/JsonSerializeTest.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,39 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace Tests\MartinGeorgiev\Doctrine\ORM\Query\AST\Functions; | ||
|
|
||
| use Fixtures\MartinGeorgiev\Doctrine\Entity\ContainsJsons; | ||
| use MartinGeorgiev\Doctrine\ORM\Query\AST\Functions\JsonSerialize; | ||
|
|
||
| class JsonSerializeTest extends TestCase | ||
| { | ||
| protected function getStringFunctions(): array | ||
| { | ||
| return [ | ||
| 'JSON_SERIALIZE' => JsonSerialize::class, | ||
| ]; | ||
| } | ||
|
|
||
| protected function getExpectedSqlStatements(): array | ||
| { | ||
| return [ | ||
| // Basic serialization | ||
| 'SELECT json_serialize(c0_.object1) AS sclr_0 FROM ContainsJsons c0_', | ||
| // With expression | ||
| 'SELECT json_serialize(UPPER(c0_.object1)) AS sclr_0 FROM ContainsJsons c0_', | ||
| // With literal | ||
| "SELECT json_serialize('{\"key\": \"value\"}') AS sclr_0 FROM ContainsJsons c0_", | ||
| ]; | ||
| } | ||
martin-georgiev marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| protected function getDqlStatements(): array | ||
| { | ||
| return [ | ||
| \sprintf('SELECT JSON_SERIALIZE(e.object1) FROM %s e', ContainsJsons::class), | ||
| \sprintf('SELECT JSON_SERIALIZE(UPPER(e.object1)) FROM %s e', ContainsJsons::class), | ||
| \sprintf("SELECT JSON_SERIALIZE('{\"key\": \"value\"}') FROM %s e", ContainsJsons::class), | ||
| ]; | ||
| } | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.