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