Skip to content

Commit

Permalink
Add support for JsonTypeof function (#115)
Browse files Browse the repository at this point in the history
Co-authored-by: Martin Georgiev <[email protected]>
  • Loading branch information
mpiot and martin-georgiev authored Aug 21, 2022
1 parent 5e726ec commit 826d1df
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 0 deletions.
1 change: 1 addition & 0 deletions docs/AVAILABLE-FUNCTIONS-AND-OPERATORS.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
| json_object_agg | JSON_OBJECT_AGG | `MartinGeorgiev\Doctrine\ORM\Query\AST\Functions\JsonObjectAgg` |
| json_object_keys | JSON_OBJECT_KEYS | `MartinGeorgiev\Doctrine\ORM\Query\AST\Functions\JsonObjectKeys` |
| json_strip_nulls | JSON_STRIP_NULLS | `MartinGeorgiev\Doctrine\ORM\Query\AST\Functions\JsonStripNulls` |
| json_typeof | JSON_TYPEOF | `MartinGeorgiev\Doctrine\ORM\Query\AST\Functions\JsonTypeof` |
| jsonb_array_elements | JSONB_ARRAY_ELEMENTS | `MartinGeorgiev\Doctrine\ORM\Query\AST\Functions\JsonbArrayElements` |
| jsonb_agg | JSONB_AGG | `MartinGeorgiev\Doctrine\ORM\Query\AST\Functions\JsonbAgg` |
| jsonb_array_elements_text | JSONB_ARRAY_ELEMENTS_TEXT | `MartinGeorgiev\Doctrine\ORM\Query\AST\Functions\JsonbArrayElementsText` |
Expand Down
1 change: 1 addition & 0 deletions docs/INTEGRATING-WITH-SYMFONY.md
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@ doctrine:
JSON_STRIP_NULLS: MartinGeorgiev\Doctrine\ORM\Query\AST\Functions\JsonStripNulls
TO_JSON: MartinGeorgiev\Doctrine\ORM\Query\AST\Functions\ToJson
ROW_TO_JSON: MartinGeorgiev\Doctrine\ORM\Query\AST\Functions\RowToJson
JSON_TYPEOF: MartinGeorgiev\Doctrine\ORM\Query\AST\Functions\JsonTypeof
# jsonb specific functions
JSONB_AGG: MartinGeorgiev\Doctrine\ORM\Query\AST\Functions\JsonbAgg
Expand Down
20 changes: 20 additions & 0 deletions src/MartinGeorgiev/Doctrine/ORM/Query/AST/Functions/JsonTypeof.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

declare(strict_types=1);

namespace MartinGeorgiev\Doctrine\ORM\Query\AST\Functions;

/**
* Implementation of PostgreSql JSON_TYPEOF().
*
* @see https://www.postgresql.org/docs/14/functions-json.html
* @since 1.8.0
*/
class JsonTypeof extends BaseFunction
{
protected function customiseFunction(): void
{
$this->setFunctionPrototype('json_typeof(%s)');
$this->addNodeMapping('StringPrimary');
}
}
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 MartinGeorgiev\Doctrine\ORM\Query\AST\Functions\JsonTypeof;
use Tests\MartinGeorgiev\Doctrine\Fixtures\Entity\ContainsJsons;

class JsonTypeofTest extends TestCase
{
protected function getStringFunctions(): array
{
return [
'JSON_TYPEOF' => JsonTypeof::class,
];
}

protected function getExpectedSqlStatements(): array
{
return [
'SELECT json_typeof(c0_.object1) AS sclr_0 FROM ContainsJsons c0_',
];
}

protected function getDqlStatements(): array
{
return [
\sprintf('SELECT JSON_TYPEOF(e.object1) FROM %s e', ContainsJsons::class),
];
}
}

0 comments on commit 826d1df

Please sign in to comment.