|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace MartinGeorgiev\Doctrine\ORM\Query\AST\Functions; |
|
6
|
|
|
|
|
7
|
|
|
use Doctrine\ORM\Query\Lexer; |
|
8
|
|
|
use Doctrine\ORM\Query\Parser; |
|
9
|
|
|
use Doctrine\ORM\Query\QueryException; |
|
10
|
|
|
use Doctrine\ORM\Query\TokenType; |
|
11
|
|
|
use MartinGeorgiev\Utils\DoctrineOrm; |
|
12
|
|
|
|
|
13
|
|
|
/** |
|
14
|
|
|
* Implementation of PostgreSQL json field retrieval, filtered by key (using ->). |
|
15
|
|
|
* |
|
16
|
|
|
* Supports both string keys for object property access and integer indices for array element access: |
|
17
|
|
|
* - JSON_GET_FIELD(json_column, 'property_name') -> json_column->'property_name' |
|
18
|
|
|
* - JSON_GET_FIELD(json_column, 0) -> json_column->0 |
|
19
|
|
|
* |
|
20
|
|
|
* @see https://www.postgresql.org/docs/9.4/static/functions-json.html |
|
21
|
|
|
* @since 0.1 |
|
22
|
|
|
* |
|
23
|
|
|
* @author Martin Georgiev <[email protected]> |
|
24
|
|
|
*/ |
|
25
|
|
|
class JsonGetField extends BaseFunction |
|
26
|
|
|
{ |
|
27
|
3 |
|
protected function customizeFunction(): void |
|
28
|
|
|
{ |
|
29
|
3 |
|
$this->setFunctionPrototype('(%s -> %s)'); |
|
30
|
|
|
} |
|
31
|
|
|
|
|
32
|
3 |
|
protected function feedParserWithNodes(Parser $parser): void |
|
33
|
|
|
{ |
|
34
|
3 |
|
$shouldUseLexer = DoctrineOrm::isPre219(); |
|
35
|
|
|
|
|
36
|
|
|
// Parse first parameter (always StringPrimary for the JSON column) |
|
37
|
3 |
|
$this->nodes[0] = $parser->StringPrimary(); |
|
38
|
3 |
|
$parser->match($shouldUseLexer ? Lexer::T_COMMA : TokenType::T_COMMA); |
|
|
|
|
|
|
39
|
|
|
|
|
40
|
|
|
// Parse second parameter - try ArithmeticPrimary first, then StringPrimary |
|
41
|
|
|
try { |
|
42
|
3 |
|
$this->nodes[1] = $parser->ArithmeticPrimary(); |
|
43
|
|
|
} catch (QueryException) { |
|
44
|
|
|
// If ArithmeticPrimary fails (e.g., when encountering a string), try StringPrimary |
|
45
|
|
|
$this->nodes[1] = $parser->StringPrimary(); |
|
46
|
|
|
} |
|
47
|
|
|
} |
|
48
|
|
|
} |
|
49
|
|
|
|