|
1
|
|
|
<?php |
|
2
|
|
|
namespace Xiag\Rql\Parser; |
|
3
|
|
|
|
|
4
|
|
|
use Xiag\Rql\Parser\Exception\UnknownNodeException; |
|
5
|
|
|
use Xiag\Rql\Parser\Node\SelectNode; |
|
6
|
|
|
use Xiag\Rql\Parser\Node\AbstractQueryNode; |
|
7
|
|
|
use Xiag\Rql\Parser\Node\SortNode; |
|
8
|
|
|
use Xiag\Rql\Parser\Node\LimitNode; |
|
9
|
|
|
use Xiag\Rql\Parser\Node\Query\LogicalOperator\AndNode; |
|
10
|
|
|
|
|
11
|
|
|
class QueryBuilder |
|
12
|
|
|
{ |
|
13
|
|
|
/** |
|
14
|
|
|
* @var Query |
|
15
|
|
|
*/ |
|
16
|
|
|
protected $query; |
|
17
|
|
|
|
|
18
|
64 |
|
public function __construct() |
|
19
|
|
|
{ |
|
20
|
64 |
|
$this->query = new Query(); |
|
21
|
64 |
|
} |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* @param AbstractNode $node |
|
25
|
|
|
* @return $this |
|
26
|
|
|
*/ |
|
27
|
48 |
|
public function addNode(AbstractNode $node) |
|
28
|
|
|
{ |
|
29
|
48 |
|
if ($node instanceof SelectNode) { |
|
30
|
1 |
|
return $this->addSelect($node); |
|
31
|
48 |
|
} elseif ($node instanceof AbstractQueryNode) { |
|
32
|
47 |
|
return $this->addQuery($node); |
|
33
|
1 |
|
} elseif ($node instanceof SortNode) { |
|
34
|
1 |
|
return $this->addSort($node); |
|
35
|
1 |
|
} elseif ($node instanceof LimitNode) { |
|
36
|
1 |
|
return $this->addLimit($node); |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
throw new UnknownNodeException(sprintf('Unknown node type "%s" (%s)', $node->getNodeName(), get_class($node))); |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* @return Query |
|
44
|
|
|
*/ |
|
45
|
48 |
|
public function getQuery() |
|
46
|
|
|
{ |
|
47
|
48 |
|
return $this->query; |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
/** |
|
51
|
|
|
* @param SelectNode $select |
|
52
|
|
|
* @return $this |
|
53
|
|
|
*/ |
|
54
|
1 |
|
public function addSelect(SelectNode $select) |
|
55
|
|
|
{ |
|
56
|
1 |
|
$this->query->setSelect($select); |
|
57
|
|
|
|
|
58
|
1 |
|
return $this; |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
/** |
|
62
|
|
|
* @param AbstractQueryNode $query |
|
63
|
|
|
* @return $this |
|
64
|
|
|
*/ |
|
65
|
47 |
|
public function addQuery(AbstractQueryNode $query) |
|
66
|
|
|
{ |
|
67
|
47 |
|
$current = $this->query->getQuery(); |
|
68
|
47 |
|
if ($current === null) { |
|
69
|
47 |
|
$this->query->setQuery($query); |
|
70
|
47 |
|
} elseif ($current instanceof AndNode) { |
|
71
|
11 |
|
$current->addQuery($query); |
|
72
|
11 |
|
} else { |
|
73
|
11 |
|
$this->query->setQuery(new AndNode([$current, $query])); |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
47 |
|
return $this; |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
/** |
|
80
|
|
|
* @param SortNode $sort |
|
81
|
|
|
* @return $this |
|
82
|
|
|
*/ |
|
83
|
1 |
|
public function addSort(SortNode $sort) |
|
84
|
|
|
{ |
|
85
|
1 |
|
$this->query->setSort($sort); |
|
86
|
|
|
|
|
87
|
1 |
|
return $this; |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
/** |
|
91
|
|
|
* @param LimitNode $limit |
|
92
|
|
|
* @return $this |
|
93
|
|
|
*/ |
|
94
|
1 |
|
public function addLimit(LimitNode $limit) |
|
95
|
|
|
{ |
|
96
|
1 |
|
$this->query->setLimit($limit); |
|
97
|
|
|
|
|
98
|
1 |
|
return $this; |
|
99
|
|
|
} |
|
100
|
|
|
} |
|
101
|
|
|
|