-
-
Notifications
You must be signed in to change notification settings - Fork 11
/
QueryGenerator.php
296 lines (271 loc) · 8.62 KB
/
QueryGenerator.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
<?php
/**
* KumbiaPHP web & app Framework.
*
* LICENSE
*
* This source file is subject to the new BSD license that is bundled
* with this package in the file LICENSE.txt.
* It is also available through the world-wide-web at this URL:
* http://wiki.kumbiaphp.com/Licencia
* If you did not receive a copy of the license and are unable to
* obtain it through the world-wide-web, please send an email
* to [email protected] so we can send you a copy immediately.
*
* @category Kumbia
*
* @copyright 2005 - 2020 Kumbia Team (http://www.kumbiaphp.com)
* @license http://wiki.kumbiaphp.com/Licencia New BSD License
*/
namespace Kumbia\ActiveRecord;
/**
* Generador de codigo SQL.
*/
class QueryGenerator
{
/**
* Construye una consulta select desde una lista de parametros.
*
* where: condiciones where
* order: criterio de ordenamiento
* fields: lista de campos
* join: joins de tablas
* group: agrupar campos
* having: condiciones de grupo
* limit: valor limit
* offset: valor offset
* @param array $params parametros de consulta select
* @param string $source
* @param string $type
* @return string
*/
public static function select($source, $type, array $params)
{
$params = array_merge([
'fields' => '*',
'join' => '',
'limit' => \null,
'offset' => \null,
'where' => \null,
'group' => \null,
'having' => \null,
'order' => \null
], $params);
list($where, $group, $having, $order) = static::prepareParam($params);
$sql = "SELECT {$params['fields']} FROM $source {$params['join']} $where $group $having $order";
if ( ! \is_null($params['limit']) || ! \is_null($params['offset'])) {
$sql = self::query($type, 'limit', $sql, $params['limit'], $params['offset']);
}
return $sql;
}
/**
* Permite construir el WHERE, GROUP BY, HAVING y ORDER BY de una consulta SQL
* en base a los parámetros $params.
*
* @param array $params
* @return string[]
*/
protected static function prepareParam(array $params)
{
return [
static::where($params['where']),
static::group($params['group']),
static::having($params['having']),
static::order($params['order'])
];
}
/**
* Genera una sentencia where.
*
* @param string $where
* @return string
*/
protected static function where($where)
{
return empty($where) ? '' : "WHERE $where";
}
/**
* Genera una sentencia GROUP.
*
* @param string $group
* @return string
*/
protected static function group($group)
{
return empty($group) ? '' : "GROUP BY $group";
}
/**
* Genera una sentencia HAVING.
*
* @param string $having
* @return string
*/
protected static function having($having)
{
return empty($having) ? '' : "HAVING $having";
}
/**
* Genera una sentencia ORDER BY.
*
* @param string $order
* @return string
*/
protected static function order($order)
{
return empty($order) ? '' : "ORDER BY $order";
}
/**
* Construye una consulta INSERT.
*
* @param LiteRecord $model Modelo a actualizar
* @param array $data Datos pasados a la consulta preparada
* @return string
*/
public static function insert(LiteRecord $model, array &$data)
{
$meta = $model::metadata();
$data = [];
$columns = [];
$values = [];
// Preparar consulta
foreach ($meta->getFieldsList() as $field) {
$columns[] = $field;
static::insertField($field, $model, $data, $values);
}
$columns = \implode(',', $columns);
$values = \implode(',', $values);
$source = $model::getSource();
return "INSERT INTO $source ($columns) VALUES ($values)";
}
/**
* Agrega un campo a para generar una consulta preparada para un INSERT.
*
* @param string $field Nombre del campo
* @param LiteRecord $model valor del campo
* @param array $data array de datos
* @param array $values array de valores
* @return void
*/
protected static function insertField($field, LiteRecord $model, array &$data, array &$values)
{
//$meta = $model::metadata();
if (self::haveValue($model, $field)) {
$data[":$field"] = $model->$field;
$values[] = ":$field";
} else {
//if (!\in_array($field, $meta->getWithDefault()) && !\in_array($field, $meta->getAutoFields())) {
$values[] = 'NULL';
}
}
/**
* Permite conocer si la columna debe definirse como nula.
*
* @param LiteRecord $model
* @param string $field
* @return bool
*/
protected static function haveValue(LiteRecord $model, $field)
{
return isset($model->$field) && $model->$field !== '';
}
/**
* Construye una consulta UPDATE.
*
* @param LiteRecord $model Modelo a actualizar
* @param array $data Datos pasados a la consulta preparada
* @return string
*/
public static function update(LiteRecord $model, array &$data)
{
$set = [];
$pk = $model::getPK();
/*elimina la clave primaria*/
$list = \array_diff($model::metadata()->getFieldsList(), [$pk]);
foreach ($list as $field) {
$value = isset($model->$field) ? $model->$field : \null;
static::updateField($field, $value, $data, $set);
}
$set = \implode(', ', $set);
$source = $model::getSource();
$data[":$pk"] = $model->$pk;
return "UPDATE $source SET $set WHERE $pk = :$pk";
}
/**
* Generate SQL for DELETE sentence.
*
* @param string $source source
* @param string $where condition
* @return string SQL
*/
public static function deleteAll($source, $where)
{
return "DELETE FROM $source ".static::where($where);
}
/**
* Generate SQL for COUNT sentence.
*
* @param string $source source
* @param string $where condition
* @return string SQL
*/
public static function count($source, $where)
{
return "SELECT COUNT(*) AS count FROM $source ".static::where($where);
}
/**
* Agrega un campo a para generar una consulta preparada para un UPDATE.
*
* @param string $field Nombre del campo
* @param mixed $value valor
* @param array $data array de datos
* @param array $set array de valores
* @return void
*/
protected static function updateField($field, $value, array &$data, array &$set)
{
if ( ! empty($value)) {
$data[":$field"] = $value;
$set[] = "$field = :$field";
} else {
$set[] = "$field = NULL";
}
}
/**
* Construye una consulta UPDATE.
*
* @todo ¿Hay que escapar los nombres de los campos?
* @param string $model nombre del modelo a actualizar
* @param array $fields campos a actualizar
* @param array $data Datos pasados a la consulta preparada
* @param string|null $where
* @return string
*/
public static function updateAll($model, array $fields, array &$data, $where)
{
$set = [];
//$pk = $model::getPK();
/*elimina la clave primaria*/
foreach ($fields as $field => $value) {
static::updateField($field, $value, $data, $set);
}
$set = \implode(', ', $set);
$source = $model::getSource();
$where = static::where($where);
return "UPDATE $source SET $set $where";
}
/**
* Ejecuta una consulta.
*
* @thow KumbiaException
* @param string $type tipo de driver
* @param string $query_function nombre de funcion
* @return mixed
*/
public static function query($type, $query_function)
{
$query_function = "{$type}_{$query_function}";
require_once __DIR__."/Query/{$query_function}.php";
$args = \array_slice(\func_get_args(), 2);
return \call_user_func_array(__NAMESPACE__."\\Query\\$query_function", $args);
}
}