1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* This file is part of UnderQuery package. |
5
|
|
|
* |
6
|
|
|
* Copyright (c) 2016 Beniamin Jonatan Šimko |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace Phuria\UnderQuery\QueryBuilder; |
13
|
|
|
|
14
|
|
|
use Phuria\UnderQuery\Parameter\ParameterCollection; |
15
|
|
|
use Phuria\UnderQuery\Parameter\ParameterCollectionInterface; |
16
|
|
|
use Phuria\UnderQuery\Query\Query; |
17
|
|
|
use Phuria\UnderQuery\Reference\ReferenceCollection; |
18
|
|
|
use Phuria\UnderQuery\Reference\ReferenceCollectionInterface; |
19
|
|
|
use Phuria\UnderQuery\Statement\StatementInterface; |
20
|
|
|
use Phuria\UnderQuery\Table\AbstractTable; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @author Beniamin Jonatan Šimko <[email protected]> |
24
|
|
|
*/ |
25
|
|
|
abstract class AbstractBuilder implements BuilderInterface |
26
|
|
|
{ |
27
|
|
|
/** |
28
|
|
|
* @var QueryBuilderFacade |
29
|
|
|
*/ |
30
|
|
|
private $facade; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @var ReferenceCollectionInterface |
34
|
|
|
*/ |
35
|
|
|
private $referenceCollection; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @var ParameterCollectionInterface |
39
|
|
|
*/ |
40
|
|
|
private $parameterCollection; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @var AbstractTable[] $tables |
44
|
|
|
*/ |
45
|
|
|
private $rootTables = []; |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* @param QueryBuilderFacade $facade |
49
|
|
|
*/ |
50
|
7 |
|
public function __construct(QueryBuilderFacade $facade) |
51
|
|
|
{ |
52
|
7 |
|
$this->facade = $facade; |
53
|
7 |
|
$this->parameterCollection = new ParameterCollection(); |
54
|
7 |
|
$this->referenceCollection = new ReferenceCollection(); |
55
|
7 |
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* @return BuilderInterface |
59
|
|
|
*/ |
60
|
1 |
|
public function getQueryBuilder() |
61
|
|
|
{ |
62
|
1 |
|
return $this; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* @return ParameterCollectionInterface |
67
|
|
|
*/ |
68
|
2 |
|
public function getParameters() |
69
|
|
|
{ |
70
|
2 |
|
return $this->parameterCollection; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* @return ReferenceCollectionInterface |
75
|
|
|
*/ |
76
|
4 |
|
public function getReferences() |
77
|
|
|
{ |
78
|
4 |
|
return $this->referenceCollection; |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* @param mixed $table |
83
|
|
|
* @param string $alias |
|
|
|
|
84
|
|
|
* |
85
|
|
|
* @return AbstractTable |
86
|
|
|
*/ |
87
|
1 |
|
public function addRootTable($table, $alias = null) |
88
|
|
|
{ |
89
|
1 |
|
$this->rootTables[] = $table = $this->createTable($table, $alias); |
90
|
|
|
|
91
|
1 |
|
return $table; |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* @return AbstractTable[] |
96
|
|
|
*/ |
97
|
3 |
|
public function getRootTables() |
98
|
|
|
{ |
99
|
3 |
|
return $this->rootTables; |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* @param mixed $table |
104
|
|
|
* @param string|null $alias |
105
|
|
|
* |
106
|
|
|
* @return AbstractTable |
107
|
|
|
*/ |
108
|
1 |
|
public function createTable($table, $alias = null) |
109
|
|
|
{ |
110
|
1 |
|
$table = $this->facade->createTable($this, $table); |
111
|
|
|
|
112
|
1 |
|
if ($alias) { |
|
|
|
|
113
|
1 |
|
$table->setAlias($alias); |
114
|
1 |
|
} |
115
|
|
|
|
116
|
1 |
|
return $table; |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* @inheritdoc |
121
|
|
|
*/ |
122
|
1 |
|
public function objectToString($object) |
123
|
|
|
{ |
124
|
1 |
|
return $this->getReferences()->createReference($object); |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
/** |
128
|
|
|
* @inheritdoc |
129
|
|
|
*/ |
130
|
1 |
|
public function setParameter($name, $value) |
131
|
|
|
{ |
132
|
1 |
|
$parameter = $this->getParameters()->getParameter($name); |
133
|
1 |
|
$parameter->setValue($value); |
134
|
|
|
|
135
|
1 |
|
return $this; |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
/** |
139
|
|
|
* @return Query |
140
|
|
|
*/ |
141
|
1 |
|
public function buildQuery() |
142
|
|
|
{ |
143
|
1 |
|
return new Query($this->buildSQL(), $this->parameterCollection->toArray()); |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
/** |
147
|
|
|
* @inheritdoc |
148
|
|
|
*/ |
149
|
2 |
|
public function buildSQL() |
150
|
|
|
{ |
151
|
2 |
|
return $this->facade->buildSQL($this); |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
/** |
155
|
|
|
* @param mixed|null $connectionHint |
156
|
|
|
* |
157
|
|
|
* @return StatementInterface |
158
|
|
|
*/ |
159
|
|
|
public function buildStatement($connectionHint = null) |
160
|
|
|
{ |
161
|
|
|
return $this->facade->buildStatement($this->buildSQL(), $this->getParameters()->toArray(), $connectionHint); |
|
|
|
|
162
|
|
|
} |
163
|
|
|
} |
This check looks for
@param
annotations where the type inferred by our type inference engine differs from the declared type.It makes a suggestion as to what type it considers more descriptive.
Most often this is a case of a parameter that can be null in addition to its declared types.