1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Phuria\QueryBuilder\Table; |
4
|
|
|
|
5
|
|
|
use Phuria\QueryBuilder\ExprBuilder; |
6
|
|
|
use Phuria\QueryBuilder\Expression\ColumnExpression; |
7
|
|
|
use Phuria\QueryBuilder\Expression\ExpressionInterface; |
8
|
|
|
use Phuria\QueryBuilder\QueryBuilder; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* @author Beniamin Jonatan Šimko <[email protected]> |
12
|
|
|
*/ |
13
|
|
|
abstract class AbstractTable implements ExpressionInterface |
14
|
|
|
{ |
15
|
|
|
const CROSS_JOIN = 'CROSS JOIN'; |
16
|
|
|
const LEFT_JOIN = 'LEFT JOIN'; |
17
|
|
|
const INNER_JOIN = 'INNER JOIN'; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* @var QueryBuilder $qb |
21
|
|
|
*/ |
22
|
|
|
private $qb; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @var string $tableAlias |
26
|
|
|
*/ |
27
|
|
|
private $tableAlias; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @var string $joinType |
31
|
|
|
*/ |
32
|
|
|
private $joinType; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @var ExpressionInterface $joinOn |
36
|
|
|
*/ |
37
|
|
|
private $joinOn = []; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @var bool $root |
41
|
|
|
*/ |
42
|
|
|
private $root = false; |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* @var bool $join |
46
|
|
|
*/ |
47
|
|
|
private $join = false; |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* @param QueryBuilder $qb |
51
|
|
|
*/ |
52
|
23 |
|
public function __construct(QueryBuilder $qb) |
53
|
|
|
{ |
54
|
23 |
|
$this->qb = $qb; |
55
|
23 |
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* @return string |
59
|
|
|
*/ |
60
|
|
|
abstract public function getTableName(); |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* @inheritdoc |
64
|
|
|
*/ |
65
|
21 |
|
public function compile() |
66
|
|
|
{ |
67
|
21 |
|
$declaration = ''; |
68
|
|
|
|
69
|
21 |
|
if ($this->isJoin()) { |
70
|
3 |
|
$declaration .= $this->getJoinType() . ' '; |
71
|
3 |
|
} |
72
|
|
|
|
73
|
21 |
|
$declaration .= $this->getTableName(); |
74
|
|
|
|
75
|
21 |
|
if ($alias = $this->getAlias()) { |
76
|
8 |
|
$declaration .= ' AS ' . $alias; |
77
|
8 |
|
} |
78
|
|
|
|
79
|
21 |
|
if ($joinOn = $this->getJoinOn()) { |
80
|
2 |
|
$declaration .= ' ON ' . $joinOn->compile(); |
81
|
2 |
|
} |
82
|
|
|
|
83
|
21 |
|
return $declaration; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* @return string |
88
|
|
|
*/ |
89
|
21 |
|
public function getAlias() |
90
|
|
|
{ |
91
|
21 |
|
return $this->tableAlias; |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* @param string $alias |
96
|
|
|
* |
97
|
|
|
* @return $this |
98
|
|
|
*/ |
99
|
8 |
|
public function setAlias($alias) |
100
|
|
|
{ |
101
|
8 |
|
$this->tableAlias = $alias; |
102
|
|
|
|
103
|
8 |
|
return $this; |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* @return bool |
108
|
|
|
*/ |
109
|
21 |
|
public function isJoin() |
110
|
|
|
{ |
111
|
21 |
|
return $this->join; |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* @return string |
116
|
|
|
*/ |
117
|
3 |
|
public function getJoinType() |
118
|
|
|
{ |
119
|
3 |
|
return $this->joinType; |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
/** |
123
|
|
|
* @param string $joinType |
124
|
|
|
* |
125
|
|
|
* @return $this |
126
|
|
|
*/ |
127
|
3 |
|
public function setJoinType($joinType) |
128
|
|
|
{ |
129
|
3 |
|
$this->joinType = $joinType; |
130
|
3 |
|
$this->join = true; |
131
|
|
|
|
132
|
3 |
|
return $this; |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
/** |
136
|
|
|
* @return bool |
137
|
|
|
*/ |
138
|
21 |
|
public function isRoot() |
139
|
|
|
{ |
140
|
21 |
|
return $this->root; |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
/** |
144
|
|
|
* @param bool $root |
145
|
|
|
* |
146
|
|
|
* @return $this |
147
|
|
|
*/ |
148
|
23 |
|
public function setRoot($root) |
149
|
|
|
{ |
150
|
23 |
|
$this->root = $root; |
151
|
|
|
|
152
|
23 |
|
return $this; |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
/** |
156
|
|
|
* @return string |
157
|
|
|
*/ |
158
|
15 |
|
public function getAliasOrName() |
159
|
|
|
{ |
160
|
15 |
|
return $this->getAlias() ?: $this->getTableName(); |
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
/** |
164
|
|
|
* @param string $clause |
165
|
|
|
* |
166
|
|
|
* @return $this |
167
|
|
|
*/ |
168
|
5 |
|
public function addSelect($clause) |
169
|
|
|
{ |
170
|
5 |
|
$this->qb->addSelect($clause); |
171
|
|
|
|
172
|
5 |
|
return $this; |
173
|
|
|
} |
174
|
|
|
|
175
|
|
|
/** |
176
|
|
|
* @return $this |
177
|
|
|
*/ |
178
|
2 |
|
public function joinOn() |
179
|
|
|
{ |
180
|
2 |
|
$this->joinOn = new ExprBuilder(func_get_args()); |
181
|
|
|
|
182
|
2 |
|
return $this; |
183
|
|
|
} |
184
|
|
|
|
185
|
|
|
/** |
186
|
|
|
* @return ExpressionInterface |
187
|
|
|
*/ |
188
|
21 |
|
public function getJoinOn() |
189
|
|
|
{ |
190
|
21 |
|
return $this->joinOn; |
191
|
|
|
} |
192
|
|
|
|
193
|
|
|
/** |
194
|
|
|
* @param string $name |
195
|
|
|
* |
196
|
|
|
* @return ExprBuilder |
197
|
|
|
*/ |
198
|
15 |
|
public function column($name) |
199
|
|
|
{ |
200
|
15 |
|
return new ExprBuilder(new ColumnExpression($this, $name)); |
201
|
|
|
} |
202
|
|
|
} |