|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Author: Nil Portugués Calderó <[email protected]> |
|
4
|
|
|
* Date: 6/3/14 |
|
5
|
|
|
* Time: 12:07 AM. |
|
6
|
|
|
* |
|
7
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
8
|
|
|
* file that was distributed with this source code. |
|
9
|
|
|
*/ |
|
10
|
|
|
|
|
11
|
|
|
namespace NilPortugues\Sql\QueryBuilder\Manipulation; |
|
12
|
|
|
|
|
13
|
|
|
use NilPortugues\Sql\QueryBuilder\Syntax\OrderBy; |
|
14
|
|
|
use NilPortugues\Sql\QueryBuilder\Syntax\QueryPartInterface; |
|
15
|
|
|
use NilPortugues\Sql\QueryBuilder\Syntax\SyntaxFactory; |
|
16
|
|
|
use NilPortugues\Sql\QueryBuilder\Syntax\Table; |
|
17
|
|
|
use NilPortugues\Sql\QueryBuilder\Syntax\Where; |
|
18
|
|
|
// Builder injects itself into query for convestion to SQL string. |
|
19
|
|
|
use NilPortugues\Sql\QueryBuilder\Builder\BuilderInterface; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* Class AbstractBaseQuery. |
|
23
|
|
|
*/ |
|
24
|
|
|
abstract class AbstractBaseQuery implements QueryInterface, QueryPartInterface |
|
25
|
|
|
{ |
|
26
|
|
|
/** |
|
27
|
|
|
* @var string |
|
28
|
|
|
*/ |
|
29
|
|
|
protected $comment = ''; |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* @var \NilPortugues\Sql\QueryBuilder\Builder\BuilderInterface |
|
33
|
|
|
*/ |
|
34
|
|
|
private $builder; |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* @var string |
|
38
|
|
|
*/ |
|
39
|
|
|
protected $table; |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* @var string |
|
43
|
|
|
*/ |
|
44
|
|
|
protected $whereOperator = 'AND'; |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* @var Where |
|
48
|
|
|
*/ |
|
49
|
|
|
protected $where; |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* @var array |
|
53
|
|
|
*/ |
|
54
|
|
|
protected $joins = []; |
|
55
|
|
|
|
|
56
|
|
|
/** |
|
57
|
|
|
* @var int |
|
58
|
|
|
*/ |
|
59
|
|
|
protected $limitStart; |
|
60
|
|
|
|
|
61
|
|
|
/** |
|
62
|
|
|
* @var int |
|
63
|
|
|
*/ |
|
64
|
|
|
protected $limitCount; |
|
65
|
|
|
|
|
66
|
|
|
/** |
|
67
|
|
|
* @var array |
|
68
|
|
|
*/ |
|
69
|
|
|
protected $orderBy = []; |
|
70
|
|
|
|
|
71
|
|
|
/** |
|
72
|
|
|
* @return Where |
|
73
|
|
|
*/ |
|
74
|
|
|
protected function filter() |
|
75
|
|
|
{ |
|
76
|
|
|
if (!isset($this->where)) { |
|
77
|
|
|
$this->where = QueryFactory::createWhere($this); |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
return $this->where; |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
/** |
|
84
|
|
|
* Stores the builder that created this query. |
|
85
|
|
|
* |
|
86
|
|
|
* @param BuilderInterface $builder |
|
87
|
|
|
* |
|
88
|
|
|
* @return $this |
|
89
|
|
|
*/ |
|
90
|
|
|
final public function setBuilder(BuilderInterface $builder) |
|
91
|
|
|
{ |
|
92
|
|
|
$this->builder = $builder; |
|
93
|
|
|
|
|
94
|
|
|
return $this; |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
/** |
|
98
|
|
|
* @return BuilderInterface |
|
99
|
|
|
* |
|
100
|
|
|
* @throws \RuntimeException when builder has not been injected |
|
101
|
|
|
*/ |
|
102
|
|
|
final public function getBuilder() |
|
103
|
|
|
{ |
|
104
|
|
|
if (!$this->builder) { |
|
105
|
|
|
throw new \RuntimeException('Query builder has not been injected with setBuilder'); |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
return $this->builder; |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
|
|
/** |
|
112
|
|
|
* Converts this query into an SQL string by using the injected builder. |
|
113
|
|
|
* |
|
114
|
|
|
* @return string |
|
115
|
|
|
*/ |
|
116
|
|
|
public function __toString() |
|
117
|
|
|
{ |
|
118
|
|
|
try { |
|
119
|
|
|
return $this->getSql(); |
|
120
|
|
|
} catch (\Exception $e) { |
|
121
|
|
|
return \sprintf('[%s] %s', \get_class($e), $e->getMessage()); |
|
122
|
|
|
} |
|
123
|
|
|
} |
|
124
|
|
|
|
|
125
|
|
|
/** |
|
126
|
|
|
* Converts this query into an SQL string by using the injected builder. |
|
127
|
|
|
* Optionally can return the SQL with formatted structure. |
|
128
|
|
|
* |
|
129
|
|
|
* @param bool $formatted |
|
130
|
|
|
* |
|
131
|
|
|
* @return string |
|
132
|
|
|
*/ |
|
133
|
|
|
public function getSql($formatted = false) |
|
134
|
|
|
{ |
|
135
|
|
|
if ($formatted) { |
|
136
|
|
|
return $this->getBuilder()->writeFormatted($this); |
|
137
|
|
|
} |
|
138
|
|
|
|
|
139
|
|
|
return $this->getBuilder()->write($this); |
|
140
|
|
|
} |
|
141
|
|
|
|
|
142
|
|
|
/** |
|
143
|
|
|
* @return string |
|
144
|
|
|
*/ |
|
145
|
|
|
abstract public function partName(); |
|
146
|
|
|
|
|
147
|
|
|
/** |
|
148
|
|
|
* @return Where |
|
149
|
|
|
*/ |
|
150
|
|
|
public function getWhere() |
|
151
|
|
|
{ |
|
152
|
|
|
return $this->where; |
|
153
|
|
|
} |
|
154
|
|
|
|
|
155
|
|
|
/** |
|
156
|
|
|
* @return Table |
|
157
|
|
|
*/ |
|
158
|
|
|
public function getTable() |
|
159
|
|
|
{ |
|
160
|
|
|
$newTable = array($this->table); |
|
161
|
|
|
|
|
162
|
|
|
return \is_null($this->table) ? null : SyntaxFactory::createTable($newTable); |
|
163
|
|
|
} |
|
164
|
|
|
|
|
165
|
|
|
/** |
|
166
|
|
|
* @param string $table |
|
167
|
|
|
* |
|
168
|
|
|
* @return $this |
|
169
|
|
|
*/ |
|
170
|
|
|
public function setTable($table) |
|
171
|
|
|
{ |
|
172
|
|
|
$this->table = (string) $table; |
|
173
|
|
|
|
|
174
|
|
|
return $this; |
|
175
|
|
|
} |
|
176
|
|
|
|
|
177
|
|
|
/** |
|
178
|
|
|
* @param string $whereOperator |
|
179
|
|
|
* |
|
180
|
|
|
* @return Where |
|
181
|
|
|
*/ |
|
182
|
|
|
public function where($whereOperator = 'AND') |
|
183
|
|
|
{ |
|
184
|
|
|
if (!isset($this->where)) { |
|
185
|
|
|
$this->where = $this->filter(); |
|
186
|
|
|
} |
|
187
|
|
|
|
|
188
|
|
|
$this->where->conjunction($whereOperator); |
|
189
|
|
|
|
|
190
|
|
|
return $this->where; |
|
191
|
|
|
} |
|
192
|
|
|
|
|
193
|
|
|
/** |
|
194
|
|
|
* @return string |
|
195
|
|
|
*/ |
|
196
|
|
|
public function getWhereOperator() |
|
197
|
|
|
{ |
|
198
|
|
|
if (!isset($this->where)) { |
|
199
|
|
|
$this->where = $this->filter(); |
|
200
|
|
|
} |
|
201
|
|
|
|
|
202
|
|
|
return $this->where->getConjunction(); |
|
203
|
|
|
} |
|
204
|
|
|
|
|
205
|
|
|
/** |
|
206
|
|
|
* @param string $column |
|
207
|
|
|
* @param string $direction |
|
208
|
|
|
* @param null $table |
|
209
|
|
|
* |
|
210
|
|
|
* @return $this |
|
211
|
|
|
*/ |
|
212
|
|
|
public function orderBy($column, $direction = OrderBy::ASC, $table = null) |
|
213
|
|
|
{ |
|
214
|
|
|
$newColumn = array($column); |
|
215
|
|
|
$column = SyntaxFactory::createColumn($newColumn, \is_null($table) ? $this->getTable() : $table); |
|
216
|
|
|
$this->orderBy[] = new OrderBy($column, $direction); |
|
217
|
|
|
|
|
218
|
|
|
return $this; |
|
219
|
|
|
} |
|
220
|
|
|
|
|
221
|
|
|
/** |
|
222
|
|
|
* @return int |
|
223
|
|
|
*/ |
|
224
|
|
|
public function getLimitCount() |
|
225
|
|
|
{ |
|
226
|
|
|
return $this->limitCount; |
|
227
|
|
|
} |
|
228
|
|
|
|
|
229
|
|
|
/** |
|
230
|
|
|
* @return int |
|
231
|
|
|
*/ |
|
232
|
|
|
public function getLimitStart() |
|
233
|
|
|
{ |
|
234
|
|
|
return $this->limitStart; |
|
235
|
|
|
} |
|
236
|
|
|
|
|
237
|
|
|
/** |
|
238
|
|
|
* @param string $comment |
|
239
|
|
|
* |
|
240
|
|
|
* @return $this |
|
241
|
|
|
*/ |
|
242
|
|
|
public function setComment($comment) |
|
243
|
|
|
{ |
|
244
|
|
|
// Make each line of the comment prefixed with "--", |
|
245
|
|
|
// and remove any trailing whitespace. |
|
246
|
|
|
$comment = '-- '.str_replace("\n", "\n-- ", \rtrim($comment)); |
|
247
|
|
|
|
|
248
|
|
|
// Trim off any trailing "-- ", to ensure that the comment is valid. |
|
249
|
|
|
$this->comment = \rtrim($comment, '- '); |
|
250
|
|
|
|
|
251
|
|
|
if ($this->comment) { |
|
252
|
|
|
$this->comment .= "\n"; |
|
253
|
|
|
} |
|
254
|
|
|
|
|
255
|
|
|
return $this; |
|
256
|
|
|
} |
|
257
|
|
|
|
|
258
|
|
|
/** |
|
259
|
|
|
* @return string |
|
260
|
|
|
*/ |
|
261
|
|
|
public function getComment() |
|
262
|
|
|
{ |
|
263
|
|
|
return $this->comment; |
|
264
|
|
|
} |
|
265
|
|
|
} |
|
266
|
|
|
|