|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace App\Component\Model\Orm; |
|
4
|
|
|
|
|
5
|
|
|
use App\Config; |
|
6
|
|
|
use App\Container; |
|
7
|
|
|
use NilPortugues\Sql\QueryBuilder\Builder\GenericBuilder; |
|
8
|
|
|
use NilPortugues\Sql\QueryBuilder\Manipulation\Select; |
|
9
|
|
|
use NilPortugues\Sql\QueryBuilder\Manipulation\Update; |
|
10
|
|
|
use NilPortugues\Sql\QueryBuilder\Manipulation\Insert; |
|
11
|
|
|
use NilPortugues\Sql\QueryBuilder\Manipulation\Delete; |
|
12
|
|
|
|
|
13
|
|
|
/** |
|
14
|
|
|
* Poor man orm |
|
15
|
|
|
*/ |
|
16
|
|
|
class Orm implements IOrm |
|
17
|
|
|
{ |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* config |
|
21
|
|
|
* |
|
22
|
|
|
* @var Config |
|
23
|
|
|
*/ |
|
24
|
|
|
protected $config; |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* what fields |
|
28
|
|
|
* @var array |
|
29
|
|
|
*/ |
|
30
|
|
|
protected $what; |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* where criterias |
|
34
|
|
|
* @var array |
|
35
|
|
|
*/ |
|
36
|
|
|
protected $where; |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* table name |
|
40
|
|
|
* @var string |
|
41
|
|
|
*/ |
|
42
|
|
|
protected $tablename; |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* table primary key |
|
46
|
|
|
* @var string |
|
47
|
|
|
*/ |
|
48
|
|
|
protected $primary; |
|
49
|
|
|
|
|
50
|
|
|
/** |
|
51
|
|
|
* table name |
|
52
|
|
|
* @var string |
|
53
|
|
|
*/ |
|
54
|
|
|
protected $dbname; |
|
55
|
|
|
|
|
56
|
|
|
/** |
|
57
|
|
|
* pool name |
|
58
|
|
|
* @var string |
|
59
|
|
|
*/ |
|
60
|
|
|
protected $poolname; |
|
61
|
|
|
|
|
62
|
|
|
/** |
|
63
|
|
|
* query builder instance |
|
64
|
|
|
* @var GenericBuilder |
|
65
|
|
|
* @see https://github.com/nilportugues/php-sql-query-builder |
|
66
|
|
|
*/ |
|
67
|
|
|
protected $queryBuilder; |
|
68
|
|
|
|
|
69
|
|
|
/** |
|
70
|
|
|
* query from builder |
|
71
|
|
|
* @var object |
|
72
|
|
|
*/ |
|
73
|
|
|
protected $query; |
|
74
|
|
|
|
|
75
|
|
|
/** |
|
76
|
|
|
* instanciate |
|
77
|
|
|
* @param Container $container |
|
78
|
|
|
*/ |
|
79
|
8 |
|
public function __construct(Container $container) |
|
80
|
|
|
{ |
|
81
|
8 |
|
$this->config = $container->getService(Config::class); |
|
82
|
8 |
|
$this->queryBuilder = new GenericBuilder(); |
|
83
|
8 |
|
$this->query = null; |
|
84
|
8 |
|
return $this; |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
/** |
|
88
|
|
|
* find a record with what field matching where criterias |
|
89
|
|
|
* @param array $what |
|
90
|
|
|
* @param array $where |
|
91
|
|
|
*/ |
|
92
|
2 |
|
public function find(array $what = [], array $where = []): Orm |
|
93
|
|
|
{ |
|
94
|
2 |
|
$this->what = $what; |
|
95
|
2 |
|
$this->where = $where; |
|
96
|
2 |
|
$this->query = new Select(); |
|
97
|
2 |
|
$this->build(); |
|
98
|
2 |
|
return $this; |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
/** |
|
102
|
|
|
* count records matching where criterias. |
|
103
|
|
|
* aliases is formely [columnn => column_alias] |
|
104
|
|
|
* |
|
105
|
|
|
* @param array $where |
|
106
|
|
|
* @param array $aliases |
|
107
|
|
|
* @return Orm |
|
108
|
|
|
*/ |
|
109
|
2 |
|
public function count(array $where = [], array $aliases = []): Orm |
|
110
|
|
|
{ |
|
111
|
2 |
|
$this->where = $where; |
|
112
|
2 |
|
$this->query = new Select(); |
|
113
|
2 |
|
if (empty($aliases)) { |
|
114
|
1 |
|
$this->query->count(); |
|
115
|
|
|
} else { |
|
116
|
1 |
|
reset($aliases); |
|
117
|
1 |
|
$fistKey = key($aliases); |
|
118
|
1 |
|
$aliasValue = $aliases[$fistKey]; |
|
119
|
1 |
|
$this->query->count($fistKey, $aliasValue); |
|
120
|
|
|
} |
|
121
|
2 |
|
$this->buildWhere(); |
|
122
|
2 |
|
return $this; |
|
123
|
|
|
} |
|
124
|
|
|
|
|
125
|
|
|
/** |
|
126
|
|
|
* update a record with what fields matching where criterias |
|
127
|
|
|
* @param array $what |
|
128
|
|
|
* @param array $where |
|
129
|
|
|
*/ |
|
130
|
1 |
|
public function update(array $what = [], array $where = []): Orm |
|
131
|
|
|
{ |
|
132
|
1 |
|
$this->what = $what; |
|
133
|
1 |
|
$this->where = $where; |
|
134
|
1 |
|
$this->query = new Update(); |
|
135
|
1 |
|
$this->build(); |
|
136
|
1 |
|
return $this; |
|
137
|
|
|
} |
|
138
|
|
|
|
|
139
|
|
|
/** |
|
140
|
|
|
* insert record with what fields |
|
141
|
|
|
* @param array $what |
|
142
|
|
|
*/ |
|
143
|
1 |
|
public function insert(array $what = []): Orm |
|
144
|
|
|
{ |
|
145
|
1 |
|
$this->what = $what; |
|
146
|
1 |
|
$this->query = new Insert(); |
|
147
|
1 |
|
$this->build(); |
|
148
|
1 |
|
return $this; |
|
149
|
|
|
} |
|
150
|
|
|
|
|
151
|
|
|
/** |
|
152
|
|
|
* delete matching where criterias |
|
153
|
|
|
* @param array $where |
|
154
|
|
|
*/ |
|
155
|
1 |
|
public function delete(array $where = []): Orm |
|
156
|
|
|
{ |
|
157
|
1 |
|
$this->where = $where; |
|
158
|
1 |
|
$this->query = new Delete(); |
|
159
|
1 |
|
$this->build(); |
|
160
|
1 |
|
return $this; |
|
161
|
|
|
} |
|
162
|
|
|
|
|
163
|
|
|
/** |
|
164
|
|
|
* builder instance |
|
165
|
|
|
* @return GenericBuilder |
|
166
|
|
|
*/ |
|
167
|
|
|
public function getQueryBuilder(): GenericBuilder |
|
168
|
|
|
{ |
|
169
|
|
|
return $this->queryBuilder; |
|
170
|
|
|
} |
|
171
|
|
|
|
|
172
|
|
|
/** |
|
173
|
|
|
* query instance |
|
174
|
|
|
* @return object |
|
175
|
|
|
*/ |
|
176
|
|
|
public function getQuery() |
|
177
|
|
|
{ |
|
178
|
|
|
return $this->query; |
|
179
|
|
|
} |
|
180
|
|
|
|
|
181
|
|
|
/** |
|
182
|
|
|
* query sql string |
|
183
|
|
|
* @return string |
|
184
|
|
|
*/ |
|
185
|
1 |
|
public function getSql() |
|
186
|
|
|
{ |
|
187
|
1 |
|
return $this->queryBuilder->write($this->query); |
|
188
|
|
|
} |
|
189
|
|
|
|
|
190
|
|
|
/** |
|
191
|
|
|
* build sql query |
|
192
|
|
|
* |
|
193
|
|
|
* @return Orm |
|
194
|
|
|
*/ |
|
195
|
|
|
protected function build(): Orm |
|
196
|
|
|
{ |
|
197
|
|
|
if (false === is_object($this->query)) { |
|
198
|
|
|
throw new \Exception('Build : Invalid query instance'); |
|
199
|
|
|
} |
|
200
|
|
|
$queryClassname = get_class($this->query); |
|
201
|
|
|
if (false === class_exists($queryClassname)) { |
|
202
|
|
|
throw new \Exception('Build : Invalid query type'); |
|
203
|
|
|
} |
|
204
|
|
|
$this->query->setTable($this->tablename); |
|
205
|
|
|
switch ($queryClassname) { |
|
206
|
|
|
case Select::class: |
|
207
|
|
|
$this->query->setColumns($this->what); |
|
208
|
|
|
break; |
|
209
|
|
|
case Update::class: |
|
210
|
|
|
if (empty($this->what)) { |
|
211
|
|
|
throw new \Exception( |
|
212
|
|
|
'Build : Update requires not empty payload' |
|
213
|
|
|
); |
|
214
|
|
|
} |
|
215
|
|
|
if (empty($this->where)) { |
|
216
|
|
|
throw new \Exception( |
|
217
|
|
|
'Build : Update requires at least one condition' |
|
218
|
|
|
); |
|
219
|
|
|
} |
|
220
|
|
|
$this->query->setValues($this->what); |
|
221
|
|
|
break; |
|
222
|
|
|
case Insert::class: |
|
223
|
|
|
if (empty($this->what)) { |
|
224
|
|
|
throw new \Exception( |
|
225
|
|
|
'Build : Insert requires not empty payload' |
|
226
|
|
|
); |
|
227
|
|
|
} |
|
228
|
|
|
$this->query->setValues($this->what); |
|
229
|
|
|
break; |
|
230
|
|
|
case Delete::class: |
|
231
|
|
|
if (empty($this->where)) { |
|
232
|
|
|
throw new \Exception( |
|
233
|
|
|
'Build : Delete requires at least one condition' |
|
234
|
|
|
); |
|
235
|
|
|
} |
|
236
|
|
|
break; |
|
237
|
|
|
default: |
|
238
|
|
|
break; |
|
239
|
|
|
} |
|
240
|
|
|
$this->buildWhere(); |
|
241
|
|
|
return $this; |
|
242
|
|
|
} |
|
243
|
|
|
|
|
244
|
|
|
/** |
|
245
|
|
|
* build where condition on query |
|
246
|
|
|
* |
|
247
|
|
|
* @return Orm |
|
248
|
|
|
*/ |
|
249
|
|
|
protected function buildWhere(): Orm |
|
250
|
|
|
{ |
|
251
|
|
|
if (false === empty($this->where)) { |
|
252
|
|
|
foreach ($this->where as $k => $v) { |
|
253
|
|
|
$whereOperator = $this->getWhereOperator($k, $v); |
|
254
|
|
|
$this->query->where()->{$whereOperator}($k, $v); |
|
255
|
|
|
} |
|
256
|
|
|
} |
|
257
|
|
|
return $this; |
|
258
|
|
|
} |
|
259
|
|
|
|
|
260
|
|
|
/** |
|
261
|
|
|
* check where condition ket values to find operators |
|
262
|
|
|
* |
|
263
|
|
|
* @param string $whereColumn |
|
264
|
|
|
* @return string |
|
265
|
|
|
*/ |
|
266
|
|
|
protected function getWhereOperator(string &$whereColumn, $value): string |
|
267
|
|
|
{ |
|
268
|
|
|
$hasArray = is_array($value); |
|
269
|
|
|
$operator = $whereColumn[strlen($whereColumn) - 1]; |
|
270
|
|
|
$hasOperator = in_array($operator, self::OPERATORS); |
|
271
|
|
|
if (false === $hasOperator) { |
|
272
|
|
|
return ($hasArray) ? 'in' : 'equals'; |
|
273
|
|
|
} |
|
274
|
|
|
foreach (self::OPERATORS as $op) { |
|
275
|
|
|
$whereColumn = str_replace($op, '', $whereColumn); |
|
276
|
|
|
} |
|
277
|
|
|
switch ($operator) { |
|
278
|
|
|
case '!': |
|
279
|
|
|
return ($hasArray) ? 'notIn' : 'notEquals'; |
|
280
|
|
|
break; |
|
|
|
|
|
|
281
|
|
|
case '<': |
|
282
|
|
|
return 'lessThan'; |
|
283
|
|
|
break; |
|
284
|
|
|
case '>': |
|
285
|
|
|
return 'greaterThan'; |
|
286
|
|
|
break; |
|
287
|
|
|
case '#': |
|
288
|
|
|
return 'like'; |
|
289
|
|
|
break; |
|
290
|
|
|
} |
|
291
|
|
|
} |
|
292
|
|
|
} |
|
293
|
|
|
|
The
breakstatement is not necessary if it is preceded for example by areturnstatement:If you would like to keep this construct to be consistent with other
casestatements, you can safely mark this issue as a false-positive.