|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace DoctrineElastic\Query; |
|
4
|
|
|
|
|
5
|
|
|
use Doctrine\ORM\Query; |
|
6
|
|
|
use Doctrine\ORM\Query\Parser; |
|
7
|
|
|
use DoctrineElastic\DoctrineMock\GetConfigurationEntityManager; |
|
8
|
|
|
use DoctrineElastic\Elastic\ElasticQuery; |
|
9
|
|
|
|
|
10
|
|
|
/** |
|
11
|
|
|
* Elastic Query parser |
|
12
|
|
|
* Prepares query for execution |
|
13
|
|
|
* |
|
14
|
|
|
* @author Andsalves <[email protected]> |
|
15
|
|
|
*/ |
|
16
|
|
|
class QueryParser { |
|
17
|
|
|
|
|
18
|
|
|
/** @var ElasticQuery */ |
|
19
|
|
|
protected $query; |
|
20
|
|
|
|
|
21
|
|
|
/** @var Query\AST\SelectStatement */ |
|
22
|
|
|
private $_ast; |
|
23
|
|
|
|
|
24
|
|
|
/** @var Parser */ |
|
25
|
|
|
private $doctrineParser; |
|
26
|
|
|
|
|
27
|
|
|
public function __construct(ElasticQuery $query) { |
|
28
|
|
|
$this->query = $query; |
|
29
|
|
|
|
|
30
|
|
|
$doctrineQuery = new Query(new GetConfigurationEntityManager($query->getEntityManager())); |
|
31
|
|
|
$doctrineQuery->setDQL($query->getDQL()); |
|
32
|
|
|
$this->doctrineParser = new Parser($doctrineQuery); |
|
33
|
|
|
} |
|
34
|
|
|
|
|
35
|
|
|
public function getAST() { |
|
36
|
|
|
if (is_null($this->_ast)) { |
|
37
|
|
|
$this->_ast = $this->doctrineParser->QueryLanguage(); |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
return $this->_ast; |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* Converts ElasticQuery to SearchParams |
|
45
|
|
|
* |
|
46
|
|
|
* @return \DoctrineElastic\Elastic\SearchParams |
|
47
|
|
|
*/ |
|
48
|
|
|
public function parseElasticQuery() { |
|
49
|
|
|
$outputWalker = new ElasticWalker($this->query, $this->getAST(), $this->getRootClass()); |
|
50
|
|
|
|
|
51
|
|
|
return $outputWalker->walkSelectStatement(); |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* @return string |
|
56
|
|
|
*/ |
|
57
|
|
|
public function getRootClass() { |
|
58
|
|
|
/** @var \Doctrine\ORM\Query\AST\IdentificationVariableDeclaration[] $identificationVariableDeclarations */ |
|
59
|
|
|
$identificationVariableDeclarations = $this->getAST()->fromClause->identificationVariableDeclarations; |
|
60
|
|
|
|
|
61
|
|
|
foreach ($identificationVariableDeclarations as $idVarDeclaration) { |
|
62
|
|
|
if ($idVarDeclaration->rangeVariableDeclaration->isRoot) { |
|
63
|
|
|
return $idVarDeclaration->rangeVariableDeclaration->abstractSchemaName; |
|
64
|
|
|
} |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
return reset($identificationVariableDeclarations)->rangeVariableDeclaration->abstractSchemaName; |
|
68
|
|
|
} |
|
69
|
|
|
} |
|
70
|
|
|
|