Skip to content

Commit 33f6af8

Browse files
committed
add jsonb basic functions support
1 parent 27fbf42 commit 33f6af8

File tree

5 files changed

+161
-3
lines changed

5 files changed

+161
-3
lines changed

src/ORM/Query/AST/Functions/JsonGetArrayElement.php

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,18 +18,15 @@ public function parse(Parser $parser): void
1818
{
1919
$parser->match(Lexer::T_IDENTIFIER);
2020
$parser->match(Lexer::T_OPEN_PARENTHESIS);
21-
2221
$this->field = $parser->StringPrimary();
2322
$parser->match(Lexer::T_COMMA);
2423
$this->path[] = $parser->ArithmeticPrimary();
25-
2624
if (!$parser->getLexer()->isNextToken(Lexer::T_CLOSE_PARENTHESIS)) {
2725
while ($parser->getLexer()->isNextToken(Lexer::T_COMMA)) {
2826
$parser->match(Lexer::T_COMMA);
2927
$this->path[] = $parser->ArithmeticPrimary();
3028
}
3129
}
32-
3330
$parser->match(Lexer::T_CLOSE_PARENTHESIS);
3431
}
3532
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Pfilsx\PostgreSQLDoctrine\ORM\Query\AST\Functions;
6+
7+
use Doctrine\ORM\Query\AST\Functions\FunctionNode;
8+
use Doctrine\ORM\Query\AST\Node;
9+
use Doctrine\ORM\Query\Lexer;
10+
use Doctrine\ORM\Query\Parser;
11+
use Doctrine\ORM\Query\SqlWalker;
12+
13+
14+
/**
15+
* Implementation of PostgreSql jsonb concatenation function
16+
* @see https://www.postgresql.org/docs/current/functions-json.html#FUNCTIONS-JSONB-OP-TABLE
17+
* @example JSONB_CONCAT(entity.field, entity2.field)
18+
*/
19+
final class JsonbConcat extends FunctionNode
20+
{
21+
protected Node $json1;
22+
23+
protected Node $json2;
24+
public function parse(Parser $parser): void
25+
{
26+
$parser->match(Lexer::T_IDENTIFIER);
27+
$parser->match(Lexer::T_OPEN_PARENTHESIS);
28+
$this->json1 = $parser->StringPrimary();
29+
$parser->match(Lexer::T_COMMA);
30+
$this->json2 = $parser->StringPrimary();
31+
$parser->match(Lexer::T_CLOSE_PARENTHESIS);
32+
}
33+
34+
35+
public function getSql(SqlWalker $sqlWalker): string
36+
{
37+
return "{$this->json1->dispatch($sqlWalker)} || {$this->json2->dispatch($sqlWalker)}";
38+
}
39+
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Pfilsx\PostgreSQLDoctrine\ORM\Query\AST\Functions;
6+
7+
use Doctrine\ORM\Query\AST\Functions\FunctionNode;
8+
use Doctrine\ORM\Query\AST\InputParameter;
9+
use Doctrine\ORM\Query\AST\Literal;
10+
use Doctrine\ORM\Query\AST\Node;
11+
use Doctrine\ORM\Query\Lexer;
12+
use Doctrine\ORM\Query\Parser;
13+
use Doctrine\ORM\Query\SqlWalker;
14+
15+
/**
16+
* Implementation of PostgreSql jsonb contains function
17+
* @see https://www.postgresql.org/docs/current/functions-json.html#FUNCTIONS-JSONB-OP-TABLE
18+
* @example JSONB_CONTAINS(entity.field, '{"a": 1}')
19+
*/
20+
final class JsonbContains extends FunctionNode
21+
{
22+
protected Node $field;
23+
24+
protected Node $value;
25+
26+
public function parse(Parser $parser): void
27+
{
28+
$parser->match(Lexer::T_IDENTIFIER);
29+
$parser->match(Lexer::T_OPEN_PARENTHESIS);
30+
$this->field = $parser->StringPrimary();
31+
$parser->match(Lexer::T_COMMA);
32+
$this->value = $parser->StringPrimary();
33+
$parser->match(Lexer::T_CLOSE_PARENTHESIS);
34+
}
35+
36+
public function getSql(SqlWalker $sqlWalker): string
37+
{
38+
$value = $this->value->dispatch($sqlWalker);
39+
if ($this->value instanceof Literal || $this->value instanceof InputParameter) {
40+
$value .= '::jsonb';
41+
}
42+
43+
return "{$this->field->dispatch($sqlWalker)} @> $value";
44+
}
45+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Pfilsx\PostgreSQLDoctrine\ORM\Query\AST\Functions;
6+
7+
use Doctrine\ORM\Query\AST\Functions\FunctionNode;
8+
use Doctrine\ORM\Query\AST\Node;
9+
use Doctrine\ORM\Query\Lexer;
10+
use Doctrine\ORM\Query\Parser;
11+
use Doctrine\ORM\Query\SqlWalker;
12+
13+
/**
14+
* Implementation of PostgreSql jsonb contains function
15+
* @see https://www.postgresql.org/docs/current/functions-json.html#FUNCTIONS-JSONB-OP-TABLE
16+
* @example JSONB_KEY_EXISTS(entity.field, 'a')
17+
*/
18+
final class JsonbKeyExists extends FunctionNode
19+
{
20+
protected Node $field;
21+
22+
protected Node $key;
23+
24+
public function parse(Parser $parser): void
25+
{
26+
$parser->match(Lexer::T_IDENTIFIER);
27+
$parser->match(Lexer::T_OPEN_PARENTHESIS);
28+
$this->field = $parser->StringPrimary();
29+
$parser->match(Lexer::T_COMMA);
30+
$this->key = $parser->StringPrimary();
31+
$parser->match(Lexer::T_CLOSE_PARENTHESIS);
32+
}
33+
34+
public function getSql(SqlWalker $sqlWalker): string
35+
{
36+
return "{$this->field->dispatch($sqlWalker)} ? {$this->key->dispatch($sqlWalker)}";
37+
}
38+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Pfilsx\PostgreSQLDoctrine\ORM\Query\AST\Functions;
6+
7+
use Doctrine\ORM\Query\AST\Functions\FunctionNode;
8+
use Doctrine\ORM\Query\AST\Node;
9+
use Doctrine\ORM\Query\Lexer;
10+
use Doctrine\ORM\Query\Parser;
11+
use Doctrine\ORM\Query\SqlWalker;
12+
13+
14+
/**
15+
* Implementation of PostgreSql jsonb remove by key function
16+
* @see https://www.postgresql.org/docs/current/functions-json.html#FUNCTIONS-JSONB-OP-TABLE
17+
* @example JSONB_REMOVE(entity.field, 'a')
18+
*/
19+
final class JsonbRemove extends FunctionNode
20+
{
21+
protected Node $jsonb;
22+
23+
protected Node $key;
24+
public function parse(Parser $parser): void
25+
{
26+
$parser->match(Lexer::T_IDENTIFIER);
27+
$parser->match(Lexer::T_OPEN_PARENTHESIS);
28+
$this->jsonb = $parser->StringPrimary();
29+
$parser->match(Lexer::T_COMMA);
30+
$this->key = $parser->StringPrimary();
31+
$parser->match(Lexer::T_CLOSE_PARENTHESIS);
32+
}
33+
34+
35+
public function getSql(SqlWalker $sqlWalker): string
36+
{
37+
return "{$this->jsonb->dispatch($sqlWalker)} - {$this->key->dispatch($sqlWalker)}";
38+
}
39+
}

0 commit comments

Comments
 (0)