|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace BWC\Share\Data; |
|
4
|
|
|
|
|
5
|
|
|
use Doctrine\DBAL\Connection; |
|
6
|
|
|
|
|
7
|
|
|
class Select |
|
8
|
|
|
{ |
|
9
|
|
|
|
|
10
|
|
|
/** @var string */ |
|
11
|
|
|
private $_tableName; |
|
12
|
|
|
|
|
13
|
|
|
/** @var Connection */ |
|
14
|
|
|
private $_con; |
|
15
|
|
|
|
|
16
|
|
|
/** @var array */ |
|
17
|
|
|
private $_select; |
|
18
|
|
|
|
|
19
|
|
|
/** @var string|null */ |
|
20
|
|
|
private $_alias; |
|
21
|
|
|
|
|
22
|
|
|
/** @var array */ |
|
23
|
|
|
private $_join = array(); |
|
24
|
|
|
|
|
25
|
|
|
/** @var array */ |
|
26
|
|
|
private $_where = array(); |
|
27
|
|
|
|
|
28
|
|
|
private $_sort = array(); |
|
29
|
|
|
|
|
30
|
|
|
private $_pageNum = null; |
|
31
|
|
|
private $_pageSize = null; |
|
32
|
|
|
|
|
33
|
|
|
private $_params = array(); |
|
34
|
|
|
|
|
35
|
|
|
|
|
36
|
|
|
function __construct($tableName, Connection $connection, $select = null, $alias = null) { |
|
|
|
|
|
|
37
|
|
|
$this->_tableName = $tableName; |
|
38
|
|
|
$this->_con = $connection; |
|
39
|
|
|
$this->_select = $select ? (is_array($select) ? $select : array($select)) : array('*'); |
|
40
|
|
|
$this->_alias = $alias; |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* @return null|string |
|
46
|
|
|
*/ |
|
47
|
|
|
function getAlias() { |
|
|
|
|
|
|
48
|
|
|
return $this->_alias; |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* @param array $select |
|
54
|
|
|
* @param string $prefix |
|
55
|
|
|
* @return Select |
|
56
|
|
|
*/ |
|
57
|
|
|
function addSelect(array $select, $prefix = '') { |
|
|
|
|
|
|
58
|
|
|
foreach ($select as $s) { |
|
59
|
|
|
$this->_select[] = "{$prefix}.{$s} as {$prefix}_{$s}"; |
|
60
|
|
|
} |
|
61
|
|
|
return $this; |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
|
|
65
|
|
|
function reset() { |
|
|
|
|
|
|
66
|
|
|
$this->_join = array(); |
|
67
|
|
|
$this->_where = array(); |
|
68
|
|
|
$this->_sort = array(); |
|
69
|
|
|
$this->_pageNum = null; |
|
70
|
|
|
$this->_pageSize = null; |
|
71
|
|
|
$this->_params = array(); |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
/** |
|
75
|
|
|
* @param $table |
|
76
|
|
|
* @param $on |
|
77
|
|
|
* @param $alias |
|
78
|
|
|
* @return Select |
|
79
|
|
|
*/ |
|
80
|
|
|
function join($table, $on, $alias) { |
|
|
|
|
|
|
81
|
|
|
$this->_join[] = new JoinChunk('', $table, $on, $alias); |
|
82
|
|
|
return $this; |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
/** |
|
86
|
|
|
* @param $table |
|
87
|
|
|
* @param $on |
|
88
|
|
|
* @param $alias |
|
89
|
|
|
* @return Select |
|
90
|
|
|
*/ |
|
91
|
|
|
function leftJoin($table, $on, $alias) { |
|
|
|
|
|
|
92
|
|
|
$this->_join[] = new JoinChunk('LEFT', $table, $on, $alias); |
|
93
|
|
|
return $this; |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
/** |
|
97
|
|
|
* @param array|string $key |
|
98
|
|
|
* @param null|mixed $value |
|
99
|
|
|
* @param null|string $operator |
|
100
|
|
|
* @return Select |
|
101
|
|
|
*/ |
|
102
|
|
|
function where($key, $value = null, $operator = null) { |
|
|
|
|
|
|
103
|
|
|
if (is_array($key)) { |
|
104
|
|
|
foreach ($key as $k=>$v) { |
|
105
|
|
|
if ($this->_alias && strpos($k, '.')===false) { |
|
|
|
|
|
|
106
|
|
|
$k = $this->_alias.'.'.$k; |
|
107
|
|
|
} |
|
108
|
|
|
$this->_where[] = new WhereChunk($k, $v, $operator); |
|
109
|
|
|
} |
|
110
|
|
|
} else { |
|
111
|
|
|
$this->_where[] = new WhereChunk($key, $value, $operator); |
|
112
|
|
|
} |
|
113
|
|
|
return $this; |
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
|
|
|
|
117
|
|
|
/** |
|
118
|
|
|
* @param $string |
|
119
|
|
|
* @return Select |
|
120
|
|
|
*/ |
|
121
|
|
|
function whereString($string) { |
|
|
|
|
|
|
122
|
|
|
$this->_where[] = new WhereStringChunk($string); |
|
123
|
|
|
return $this; |
|
124
|
|
|
} |
|
125
|
|
|
|
|
126
|
|
|
/** |
|
127
|
|
|
* @param array|string $column |
|
128
|
|
|
* @param null|string $dir |
|
129
|
|
|
* @return Select |
|
130
|
|
|
*/ |
|
131
|
|
|
function sort($column, $dir = null) { |
|
|
|
|
|
|
132
|
|
|
if (is_array($column)) { |
|
133
|
|
|
foreach ($column as $k=>$v) { |
|
134
|
|
|
$this->_sort[$k] = $this->getSortDir($v); |
|
135
|
|
|
} |
|
136
|
|
|
} else { |
|
137
|
|
|
$this->_sort[$column] = ''; |
|
138
|
|
|
} |
|
139
|
|
|
return $this; |
|
140
|
|
|
} |
|
141
|
|
|
|
|
142
|
|
|
private function getSortDir($dir) { |
|
143
|
|
|
if (is_string($dir)) { |
|
144
|
|
|
$dir = strtoupper($dir); |
|
145
|
|
|
if ($dir != 'ASC' && $dir != 'DESC') { |
|
146
|
|
|
$dir = $dir ? 'ASC' : 'DESC'; |
|
147
|
|
|
} |
|
148
|
|
|
} else { |
|
149
|
|
|
$dir = $dir ? 'ASC' : 'DESC'; |
|
150
|
|
|
} |
|
151
|
|
|
return $dir; |
|
152
|
|
|
} |
|
153
|
|
|
|
|
154
|
|
|
/** |
|
155
|
|
|
* @param $pageNum int |
|
156
|
|
|
* @param $pageSize int |
|
157
|
|
|
* @return Select |
|
158
|
|
|
*/ |
|
159
|
|
|
function paging($pageNum, $pageSize) { |
|
|
|
|
|
|
160
|
|
|
$this->_pageNum = intval($pageNum); |
|
161
|
|
|
$this->_pageSize = intval($pageSize); |
|
162
|
|
|
return $this; |
|
163
|
|
|
} |
|
164
|
|
|
|
|
165
|
|
|
|
|
166
|
|
|
/** |
|
167
|
|
|
* @return string|int |
|
168
|
|
|
*/ |
|
169
|
|
|
function getOne() { |
|
|
|
|
|
|
170
|
|
|
list($sql, $params) = $this->build(); |
|
171
|
|
|
$result = $this->_con->fetchColumn($sql, $params); |
|
172
|
|
|
return $result; |
|
173
|
|
|
} |
|
174
|
|
|
|
|
175
|
|
|
|
|
176
|
|
|
/** |
|
177
|
|
|
* @return array |
|
178
|
|
|
*/ |
|
179
|
|
|
function getRow() { |
|
|
|
|
|
|
180
|
|
|
list($sql, $params) = $this->build(); |
|
181
|
|
|
$result = $this->_con->fetchAssoc($sql, $params); |
|
182
|
|
|
return $result; |
|
183
|
|
|
} |
|
184
|
|
|
|
|
185
|
|
|
|
|
186
|
|
|
/** |
|
187
|
|
|
* @return array |
|
188
|
|
|
*/ |
|
189
|
|
|
function getAll() { |
|
|
|
|
|
|
190
|
|
|
list($sql, $params) = $this->build(); |
|
191
|
|
|
$result = $this->_con->fetchAll($sql, $params); |
|
192
|
|
|
return $result; |
|
193
|
|
|
} |
|
194
|
|
|
|
|
195
|
|
|
|
|
196
|
|
|
/** |
|
197
|
|
|
* @return PagedResult |
|
198
|
|
|
*/ |
|
199
|
|
|
function pagedResult() { |
|
|
|
|
|
|
200
|
|
|
$result = new PagedResult(); |
|
201
|
|
|
$result->data = $this->getAll(); |
|
202
|
|
|
$result->totalRows = $this->getFoundRows(); |
|
203
|
|
|
return $result; |
|
204
|
|
|
} |
|
205
|
|
|
|
|
206
|
|
|
|
|
207
|
|
|
/** |
|
208
|
|
|
* @return int |
|
209
|
|
|
*/ |
|
210
|
|
|
function getFoundRows() { |
|
|
|
|
|
|
211
|
|
|
return $this->_con->fetchColumn('SELECT FOUND_ROWS()'); |
|
212
|
|
|
} |
|
213
|
|
|
|
|
214
|
|
|
/** |
|
215
|
|
|
* @param string $name |
|
216
|
|
|
* @param mixed $value |
|
217
|
|
|
* @return Select |
|
218
|
|
|
*/ |
|
219
|
|
|
function setParam($name, $value) { |
|
|
|
|
|
|
220
|
|
|
$this->_params[$name] = $value; |
|
221
|
|
|
return $this; |
|
222
|
|
|
} |
|
223
|
|
|
|
|
224
|
|
|
|
|
225
|
|
|
|
|
226
|
|
|
private function build() { |
|
227
|
|
|
$params = array(); |
|
228
|
|
|
$sqlSelect = implode(', ', $this->_select); |
|
229
|
|
|
$sql = "SELECT SQL_CALC_FOUND_ROWS $sqlSelect \nFROM `{$this->_tableName}` "; |
|
230
|
|
|
if (!empty($this->_alias)) { |
|
231
|
|
|
$sql .= ' '.$this->_alias; |
|
232
|
|
|
} |
|
233
|
|
|
if (!empty($this->_join)) { |
|
234
|
|
|
$sql .= "\n"; |
|
235
|
|
|
/** @var $chunk JoinChunk */ |
|
236
|
|
|
foreach ($this->_join as $chunk) { |
|
237
|
|
|
$sql .= $chunk->build(); |
|
238
|
|
|
} |
|
239
|
|
|
} |
|
240
|
|
|
if (!empty($this->_where)) { |
|
241
|
|
|
$arr = array(); |
|
242
|
|
|
/** @var $chunk WhereChunk */ |
|
243
|
|
|
foreach ($this->_where as $chunk) { |
|
244
|
|
|
list($wSQL, $wParams) = $chunk->build(); |
|
245
|
|
|
$arr[] = $wSQL; |
|
246
|
|
|
foreach ($wParams as $k=>$v) { |
|
247
|
|
|
$params[$k] = $v; |
|
248
|
|
|
} |
|
249
|
|
|
} |
|
250
|
|
|
$sql .= " WHERE ".implode(' AND ', $arr); |
|
251
|
|
|
} |
|
252
|
|
|
if (!empty($this->_sort)) { |
|
253
|
|
|
$arr = array(); |
|
254
|
|
|
foreach ($this->_sort as $k=>$v) { |
|
255
|
|
|
if ($k) { |
|
256
|
|
|
if ($v) { |
|
257
|
|
|
$arr[] = "{$k} {$v}"; |
|
258
|
|
|
} else { |
|
259
|
|
|
$arr[] = "$k"; |
|
260
|
|
|
} |
|
261
|
|
|
} |
|
262
|
|
|
} |
|
263
|
|
|
if (!empty($arr)) { |
|
264
|
|
|
$sql .= " ORDER BY "; |
|
265
|
|
|
$sql .= implode(', ', $arr); |
|
266
|
|
|
} |
|
267
|
|
|
} |
|
268
|
|
|
if ($this->_pageSize) { |
|
|
|
|
|
|
269
|
|
|
if ($this->_pageNum) { |
|
|
|
|
|
|
270
|
|
|
$from = ($this->_pageNum - 1) * $this->_pageSize; |
|
271
|
|
|
$sql .= " LIMIT {$from}, {$this->_pageSize} "; |
|
272
|
|
|
} else { |
|
273
|
|
|
$sql .= " LIMIT {$this->_pageSize} "; |
|
274
|
|
|
} |
|
275
|
|
|
} |
|
276
|
|
|
$params = array_merge($params, $this->_params); |
|
277
|
|
|
//print $sql; |
|
278
|
|
|
return array($sql, $params); |
|
279
|
|
|
} |
|
280
|
|
|
|
|
281
|
|
|
} |
|
282
|
|
|
|
Adding explicit visibility (
private,protected, orpublic) is generally recommend to communicate to other developers how, and from where this method is intended to be used.