|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Class for query building |
|
4
|
|
|
* |
|
5
|
|
|
* @file QueryBuilder.php |
|
6
|
|
|
* |
|
7
|
|
|
* PHP version 8.0+ |
|
8
|
|
|
* |
|
9
|
|
|
* @author Alexander Yancharuk <alex at itvault dot info> |
|
10
|
|
|
* @copyright © 2012-2021 Alexander Yancharuk |
|
11
|
|
|
* @date Сбт Июл 07 21:55:54 2012 |
|
12
|
|
|
* @license The BSD 3-Clause License |
|
13
|
|
|
* <https://tldrlegal.com/license/bsd-3-clause-license-(revised)> |
|
14
|
|
|
*/ |
|
15
|
|
|
|
|
16
|
|
|
namespace Veles\Model; |
|
17
|
|
|
|
|
18
|
|
|
use Exception; |
|
19
|
|
|
use Veles\DataBase\Db; |
|
20
|
|
|
use Veles\DataBase\DbFilter; |
|
21
|
|
|
use Veles\DataBase\DbPaginator; |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* Класс QueryBuilder |
|
25
|
|
|
* @author Alexander Yancharuk <alex at itvault dot info> |
|
26
|
|
|
*/ |
|
27
|
|
|
class QueryBuilder implements QueryBuilderInterface |
|
28
|
|
|
{ |
|
29
|
|
|
/** |
|
30
|
|
|
* @param $filter DbFilter |
|
31
|
|
|
* |
|
32
|
|
|
* @return array |
|
33
|
|
|
*/ |
|
34
|
12 |
|
protected function extractParams(DbFilter $filter) |
|
35
|
|
|
{ |
|
36
|
12 |
|
return [ |
|
37
|
12 |
|
'where' => $filter->getWhere(), |
|
38
|
12 |
|
'group' => $filter->getGroup(), |
|
39
|
12 |
|
'having' => $filter->getHaving(), |
|
40
|
12 |
|
'order' => $filter->getOrder() |
|
41
|
12 |
|
]; |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* Построение sql-запроса для insert |
|
46
|
|
|
* |
|
47
|
|
|
* @param ActiveRecord $model Экземпляр модели |
|
48
|
|
|
* |
|
49
|
|
|
* @return string |
|
50
|
|
|
* @throws Exception |
|
51
|
|
|
*/ |
|
52
|
2 |
|
public function insert(ActiveRecord $model) |
|
53
|
|
|
{ |
|
54
|
2 |
|
$fields = $values = ''; |
|
55
|
|
|
|
|
56
|
2 |
|
foreach (array_keys($model->getMap()) as $property) { |
|
57
|
2 |
|
$value = $this->sanitize($model, $property); |
|
58
|
|
|
|
|
59
|
2 |
|
if (null === $value) { |
|
60
|
2 |
|
continue; |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
2 |
|
$fields .= "\"$property\", "; |
|
64
|
2 |
|
$values .= "$value, "; |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
2 |
|
$fields = rtrim($fields, ', '); |
|
68
|
2 |
|
$values = rtrim($values, ', '); |
|
69
|
|
|
|
|
70
|
2 |
|
return "INSERT \"" . $model::TBL_NAME . "\" ($fields) VALUES ($values)"; |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
/** |
|
74
|
|
|
* Функция безопасности переменных |
|
75
|
|
|
* |
|
76
|
|
|
* @param ActiveRecord $model |
|
77
|
|
|
* @param mixed $property |
|
78
|
|
|
* |
|
79
|
|
|
* @return mixed |
|
80
|
|
|
* @throws Exception |
|
81
|
|
|
*/ |
|
82
|
4 |
|
private function sanitize(ActiveRecord $model, $property) |
|
83
|
|
|
{ |
|
84
|
4 |
|
if (!isset($model->$property)) { |
|
85
|
3 |
|
return null; |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
4 |
|
$type = $model->getMap()[$property]; |
|
89
|
|
|
|
|
90
|
4 |
|
if ('string' === $type) { |
|
91
|
4 |
|
return Db::escape($model->$property); |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
3 |
|
$value = $model->$property; |
|
95
|
3 |
|
settype($value, $type); |
|
96
|
|
|
|
|
97
|
3 |
|
return $value; |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
/** |
|
101
|
|
|
* Построение sql-запроса для update |
|
102
|
|
|
* |
|
103
|
|
|
* @param ActiveRecord $model Экземпляр модели |
|
104
|
|
|
* |
|
105
|
|
|
* @return string $sql |
|
106
|
|
|
* @throws Exception |
|
107
|
|
|
*/ |
|
108
|
3 |
|
public function update(ActiveRecord $model) |
|
109
|
|
|
{ |
|
110
|
3 |
|
$params = ''; |
|
111
|
3 |
|
$properties = array_diff_key($model->getMap(), ['id' => 1]); |
|
112
|
|
|
|
|
113
|
3 |
|
foreach (array_keys($properties) as $property) { |
|
114
|
3 |
|
$value = $this->sanitize($model, $property); |
|
115
|
|
|
|
|
116
|
3 |
|
if (null === $value) { |
|
117
|
1 |
|
continue; |
|
118
|
|
|
} |
|
119
|
|
|
|
|
120
|
3 |
|
$params .= "\"$property\" = $value, "; |
|
121
|
|
|
} |
|
122
|
|
|
|
|
123
|
3 |
|
$params = rtrim($params, ', '); |
|
124
|
|
|
|
|
125
|
3 |
|
return "UPDATE \"" . $model::TBL_NAME . "\" SET $params WHERE id = $model->id"; |
|
126
|
|
|
} |
|
127
|
|
|
|
|
128
|
|
|
/** |
|
129
|
|
|
* Построение sql-запроса для select |
|
130
|
|
|
* @param ActiveRecord $model Экземпляр модели |
|
131
|
|
|
* @param int $identifier primary key |
|
132
|
|
|
* @return string $sql |
|
133
|
|
|
*/ |
|
134
|
2 |
|
public function getById(ActiveRecord $model, $identifier) |
|
135
|
|
|
{ |
|
136
|
2 |
|
$identifier = (int) $identifier; |
|
137
|
|
|
|
|
138
|
2 |
|
return "SELECT * FROM \"" . $model::TBL_NAME . "\" WHERE id = $identifier LIMIT 1"; |
|
139
|
|
|
} |
|
140
|
|
|
|
|
141
|
|
|
/** |
|
142
|
|
|
* Построение sql-запроса для delete |
|
143
|
|
|
* |
|
144
|
|
|
* @param ActiveRecord $model Экземпляр модели |
|
145
|
|
|
* @param array $ids Массив ID для удаления |
|
146
|
|
|
* |
|
147
|
|
|
* @throws Exception |
|
148
|
|
|
* @return string $sql |
|
149
|
|
|
*/ |
|
150
|
3 |
|
public function delete(ActiveRecord $model, array $ids) |
|
151
|
|
|
{ |
|
152
|
3 |
|
foreach ($ids as &$value) { |
|
153
|
3 |
|
$value = (int) $value; |
|
154
|
|
|
}; |
|
155
|
|
|
|
|
156
|
3 |
|
$ids = implode(',', $ids); |
|
157
|
|
|
|
|
158
|
3 |
|
return "DELETE FROM \"" . $model::TBL_NAME . "\" WHERE id IN ($ids)"; |
|
159
|
|
|
} |
|
160
|
|
|
|
|
161
|
|
|
/** |
|
162
|
|
|
* Построение запроса получения списка объектов |
|
163
|
|
|
* |
|
164
|
|
|
* @param ActiveRecord $model Экземпляр модели |
|
165
|
|
|
* @param bool|DbFilter $filter Экземпляр фильтра |
|
166
|
|
|
* |
|
167
|
|
|
* @return string |
|
168
|
|
|
*/ |
|
169
|
15 |
|
public function find(ActiveRecord $model, $filter) |
|
170
|
|
|
{ |
|
171
|
15 |
|
$fields = '"' . implode('", "', array_keys($model->getMap())) . '"'; |
|
172
|
15 |
|
$where = $group = $having = $order = ''; |
|
173
|
|
|
|
|
174
|
15 |
|
if ($filter instanceof DbFilter) { |
|
175
|
12 |
|
$params = $this->extractParams($filter); |
|
176
|
12 |
|
extract($params, EXTR_IF_EXISTS); |
|
177
|
|
|
} |
|
178
|
|
|
|
|
179
|
15 |
|
return rtrim("SELECT $fields FROM \"" . $model::TBL_NAME . "\" $where $group $having $order"); |
|
180
|
|
|
} |
|
181
|
|
|
|
|
182
|
|
|
/** |
|
183
|
|
|
* Построение произвольного запроса с постраничным выводом |
|
184
|
|
|
* |
|
185
|
|
|
* @param string $sql Запрос |
|
186
|
|
|
* @param DbPaginator $pager Экземпляр постраничного вывода |
|
187
|
|
|
* |
|
188
|
|
|
* @return string |
|
189
|
|
|
*/ |
|
190
|
6 |
|
public function setPage($sql, DbPaginator $pager) |
|
191
|
|
|
{ |
|
192
|
6 |
|
$sql = str_replace('SELECT', 'SELECT SQL_CALC_FOUND_ROWS', $sql); |
|
193
|
|
|
|
|
194
|
6 |
|
return $sql . $pager->getSqlLimit(); |
|
195
|
|
|
} |
|
196
|
|
|
} |
|
197
|
|
|
|