1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Ubiquity\orm\core\prepared; |
4
|
|
|
|
5
|
|
|
use Ubiquity\cache\database\DbCache; |
6
|
|
|
use Ubiquity\db\SqlUtils; |
7
|
|
|
use Ubiquity\orm\DAO; |
8
|
|
|
use Ubiquity\orm\OrmUtils; |
9
|
|
|
use Ubiquity\orm\parser\ConditionParser; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* Ubiquity\orm\core\prepared$DAOPreparedQuery |
13
|
|
|
* This class is part of Ubiquity |
14
|
|
|
* |
15
|
|
|
* @author jcheron <[email protected]> |
16
|
|
|
* @version 1.0.7 |
17
|
|
|
* |
18
|
|
|
*/ |
19
|
|
|
abstract class DAOPreparedQuery { |
20
|
|
|
protected $databaseOffset; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* |
24
|
|
|
* @var ConditionParser |
25
|
|
|
*/ |
26
|
|
|
protected $conditionParser; |
27
|
|
|
protected $included; |
28
|
|
|
protected $hasIncluded; |
29
|
|
|
protected $useCache; |
30
|
|
|
protected $className; |
31
|
|
|
protected $tableName; |
32
|
|
|
protected $invertedJoinColumns = null; |
33
|
|
|
protected $oneToManyFields = null; |
34
|
|
|
protected $manyToManyFields = null; |
35
|
|
|
protected $transformers; |
36
|
|
|
protected $propsKeys; |
37
|
|
|
protected $accessors; |
38
|
|
|
protected $fieldList; |
39
|
|
|
protected $memberList; |
40
|
|
|
protected $firstPropKey; |
41
|
|
|
protected $condition; |
42
|
|
|
protected $preparedCondition; |
43
|
|
|
protected $primaryKeys; |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* |
47
|
|
|
* @var array|boolean |
48
|
|
|
*/ |
49
|
|
|
protected $additionalMembers = false; |
50
|
|
|
protected $sqlAdditionalMembers = ""; |
51
|
|
|
protected $allPublic = false; |
52
|
|
|
protected $statement; |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* |
56
|
|
|
* @var \Ubiquity\db\Database |
57
|
|
|
*/ |
58
|
|
|
protected $db; |
59
|
|
|
|
60
|
65 |
|
public function __construct($className, $condition = null, $included = false, $cache = null) { |
61
|
65 |
|
$this->className = $className; |
62
|
65 |
|
$this->included = $included; |
63
|
65 |
|
$this->condition = $condition; |
64
|
65 |
|
$this->conditionParser = new ConditionParser ($condition); |
65
|
65 |
|
$this->prepare($cache); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
public function getFirstPropKey() { |
69
|
|
|
return $this->firstPropKey; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* |
74
|
|
|
* @return \Ubiquity\db\Database |
75
|
|
|
*/ |
76
|
|
|
public function getDb() { |
77
|
|
|
return $this->db; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* |
82
|
|
|
* @return mixed |
83
|
|
|
*/ |
84
|
|
|
public function getDatabaseOffset() { |
85
|
|
|
return $this->databaseOffset; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* |
90
|
|
|
* @return \Ubiquity\orm\parser\ConditionParser |
91
|
|
|
*/ |
92
|
|
|
public function getConditionParser() { |
93
|
|
|
return $this->conditionParser; |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* |
98
|
|
|
* @return mixed |
99
|
|
|
*/ |
100
|
|
|
public function getIncluded() { |
101
|
|
|
return $this->included; |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* |
106
|
|
|
* @return boolean |
107
|
|
|
*/ |
108
|
|
|
public function getHasIncluded() { |
109
|
|
|
return $this->hasIncluded; |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* |
114
|
|
|
* @return mixed |
115
|
|
|
*/ |
116
|
|
|
public function getUseCache() { |
117
|
|
|
return $this->useCache; |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* |
122
|
|
|
* @return mixed |
123
|
|
|
*/ |
124
|
|
|
public function getClassName() { |
125
|
|
|
return $this->className; |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
/** |
129
|
|
|
* |
130
|
|
|
* @return mixed |
131
|
|
|
*/ |
132
|
|
|
public function getTableName() { |
133
|
|
|
return $this->tableName; |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
/** |
137
|
|
|
* |
138
|
|
|
* @return mixed |
139
|
|
|
*/ |
140
|
|
|
public function getInvertedJoinColumns() { |
141
|
|
|
return $this->invertedJoinColumns; |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
/** |
145
|
|
|
* |
146
|
|
|
* @return mixed |
147
|
|
|
*/ |
148
|
|
|
public function getOneToManyFields() { |
149
|
|
|
return $this->oneToManyFields; |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
/** |
153
|
|
|
* |
154
|
|
|
* @return mixed |
155
|
|
|
*/ |
156
|
|
|
public function getManyToManyFields() { |
157
|
|
|
return $this->manyToManyFields; |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
public function getTransformers() { |
161
|
|
|
return $this->transformers; |
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
public function getPropsKeys() { |
165
|
|
|
return $this->propsKeys; |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
/** |
169
|
|
|
* |
170
|
|
|
* @return mixed |
171
|
|
|
*/ |
172
|
|
|
public function getAccessors() { |
173
|
|
|
return $this->accessors; |
174
|
|
|
} |
175
|
|
|
|
176
|
|
|
public function getFieldList() { |
177
|
|
|
return $this->fieldList; |
178
|
|
|
} |
179
|
|
|
|
180
|
|
|
/** |
181
|
|
|
* |
182
|
|
|
* @return mixed |
183
|
|
|
*/ |
184
|
|
|
public function getMemberList() { |
185
|
|
|
return $this->memberList; |
186
|
|
|
} |
187
|
|
|
|
188
|
65 |
|
protected function prepare(?DbCache $cache = null) { |
189
|
65 |
|
$this->db = DAO::getDb($this->className); |
190
|
65 |
|
if (isset ($cache)) { |
191
|
|
|
$this->db->setCacheInstance($cache); |
192
|
|
|
} |
193
|
65 |
|
$this->included = DAO::_getIncludedForStep($this->included); |
194
|
|
|
|
195
|
65 |
|
$metaDatas = OrmUtils::getModelMetadata($this->className); |
196
|
65 |
|
$this->tableName = $metaDatas ['#tableName']; |
197
|
65 |
|
$this->hasIncluded = $this->included || (\is_array($this->included) && \count($this->included) > 0); |
198
|
65 |
|
if ($this->hasIncluded) { |
199
|
|
|
DAO::_initRelationFields($this->included, $metaDatas, $this->invertedJoinColumns, $this->oneToManyFields, $this->manyToManyFields); |
200
|
|
|
} |
201
|
65 |
|
$this->transformers = $metaDatas ['#transformers'] [DAO::$transformerOp] ?? []; |
202
|
65 |
|
$this->fieldList = DAO::_getFieldList($this->tableName, $metaDatas); |
203
|
65 |
|
$this->memberList = \array_flip(\array_diff($metaDatas ['#fieldNames'], $metaDatas ['#notSerializable'])); |
204
|
65 |
|
$this->propsKeys = OrmUtils::getPropKeys($this->className); |
205
|
|
|
|
206
|
65 |
|
$this->firstPropKey = OrmUtils::getFirstPropKey($this->className); |
207
|
65 |
|
$this->primaryKeys = OrmUtils::getPrimaryKeys($this->className); |
208
|
65 |
|
if (!($this->allPublic = OrmUtils::hasAllMembersPublic($this->className))) { |
209
|
61 |
|
$this->accessors = $metaDatas ['#accessors']; |
210
|
|
|
} |
211
|
|
|
} |
212
|
|
|
|
213
|
65 |
|
protected function updatePrepareStatement() { |
214
|
65 |
|
$this->preparedCondition = SqlUtils::checkWhere($this->conditionParser->getCondition()); |
215
|
65 |
|
$this->statement = $this->db->getDaoPreparedStatement($this->tableName, $this->preparedCondition, $this->fieldList . $this->sqlAdditionalMembers); |
216
|
|
|
} |
217
|
|
|
|
218
|
2 |
|
protected function updateSqlAdditionalMembers() { |
219
|
2 |
|
if ($this->additionalMembers) { |
220
|
2 |
|
$this->sqlAdditionalMembers = ',' . $this->parseExpressions(); |
221
|
2 |
|
$this->updatePrepareStatement(); |
222
|
|
|
} |
223
|
|
|
} |
224
|
|
|
|
225
|
2 |
|
protected function parseExpressions() { |
226
|
2 |
|
return \implode(',', $this->additionalMembers); |
227
|
|
|
} |
228
|
|
|
|
229
|
2 |
|
protected function addAditionnalMembers($object, $row) { |
230
|
2 |
|
foreach ($this->additionalMembers as $member => $_) { |
231
|
2 |
|
$object->{$member} = $row [$member] ?? null; |
232
|
2 |
|
$object->_rest [$member] = $row [$member] ?? null; |
233
|
|
|
} |
234
|
|
|
} |
235
|
|
|
|
236
|
|
|
abstract public function execute($params = [], $useCache = false); |
237
|
|
|
|
238
|
|
|
/** |
239
|
|
|
* Adds a new expression and associates it with a new member of the class added at runtime. |
240
|
|
|
* |
241
|
|
|
* @param string $sqlExpression The SQL expression (part of the SQL SELECT) |
242
|
|
|
* @param string $memberName The new associated member name |
243
|
|
|
*/ |
244
|
2 |
|
public function addMember(string $sqlExpression, string $memberName): void { |
245
|
2 |
|
$this->additionalMembers [$memberName] = $sqlExpression . " AS '{$memberName}'"; |
246
|
2 |
|
$this->updateSqlAdditionalMembers(); |
247
|
|
|
} |
248
|
|
|
|
249
|
|
|
/** |
250
|
|
|
* Adds new expressions and their associated members at runtime. |
251
|
|
|
* |
252
|
|
|
* @param array $expressionsNames An associative array of [memberName=>sqlExpression,...] |
253
|
|
|
*/ |
254
|
|
|
public function addMembers(array $expressionsNames): void { |
255
|
|
|
foreach ($expressionsNames as $member => $expression) { |
256
|
|
|
$this->additionalMembers [$member] = $expression . " AS '{$member}'"; |
257
|
|
|
} |
258
|
|
|
$this->updateSqlAdditionalMembers(); |
259
|
|
|
} |
260
|
|
|
|
261
|
|
|
/** |
262
|
|
|
* Store the cache for a prepared Query |
263
|
|
|
*/ |
264
|
|
|
public function storeDbCache() { |
265
|
|
|
$this->db->storeCache(); |
266
|
|
|
} |
267
|
|
|
} |
268
|
|
|
|