File tree Expand file tree Collapse file tree 5 files changed +161
-3
lines changed
src/ORM/Query/AST/Functions Expand file tree Collapse file tree 5 files changed +161
-3
lines changed Original file line number Diff line number Diff 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}
Original file line number Diff line number Diff line change 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+ }
Original file line number Diff line number Diff line change 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+ }
Original file line number Diff line number Diff line change 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+ }
Original file line number Diff line number Diff line change 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+ }
You can’t perform that action at this time.
0 commit comments