Ubiquity 2.5.2
php rapid development framework
Loading...
Searching...
No Matches
DAOPreparedQuery.php
Go to the documentation of this file.
1<?php
2
4
10
19abstract class DAOPreparedQuery {
20 protected $databaseOffset;
21
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;
43 protected $primaryKeys;
44
49 protected $additionalMembers = false;
50 protected $sqlAdditionalMembers = "";
51 protected $allPublic = false;
52 protected $statement;
53
58 protected $db;
59
60 public function __construct($className, $condition = null, $included = false, $cache = null) {
61 $this->className = $className;
62 $this->included = $included;
63 $this->condition = $condition;
64 $this->conditionParser = new ConditionParser ($condition);
65 $this->prepare($cache);
66 }
67
68 public function getFirstPropKey() {
70 }
71
76 public function getDb() {
77 return $this->db;
78 }
79
84 public function getDatabaseOffset() {
86 }
87
92 public function getConditionParser() {
94 }
95
100 public function getIncluded() {
101 return $this->included;
102 }
103
108 public function getHasIncluded() {
109 return $this->hasIncluded;
110 }
111
116 public function getUseCache() {
117 return $this->useCache;
118 }
119
124 public function getClassName() {
125 return $this->className;
126 }
127
132 public function getTableName() {
133 return $this->tableName;
134 }
135
140 public function getInvertedJoinColumns() {
142 }
143
148 public function getOneToManyFields() {
150 }
151
156 public function getManyToManyFields() {
158 }
159
160 public function getTransformers() {
161 return $this->transformers;
162 }
163
164 public function getPropsKeys() {
165 return $this->propsKeys;
166 }
167
172 public function getAccessors() {
173 return $this->accessors;
174 }
175
176 public function getFieldList() {
177 return $this->fieldList;
178 }
179
184 public function getMemberList() {
185 return $this->memberList;
186 }
187
188 protected function prepare(?DbCache $cache = null) {
189 $this->db = DAO::getDb($this->className);
190 if (isset ($cache)) {
191 $this->db->setCacheInstance($cache);
192 }
193 $this->included = DAO::_getIncludedForStep($this->included);
194
195 $metaDatas = OrmUtils::getModelMetadata($this->className);
196 $this->tableName = $metaDatas ['#tableName'];
197 $this->hasIncluded = $this->included || (\is_array($this->included) && \count($this->included) > 0);
198 if ($this->hasIncluded) {
199 DAO::_initRelationFields($this->included, $metaDatas, $this->invertedJoinColumns, $this->oneToManyFields, $this->manyToManyFields);
200 }
201 $this->transformers = $metaDatas ['#transformers'] [DAO::$transformerOp] ?? [];
202 $this->fieldList = DAO::_getFieldList($this->tableName, $metaDatas);
203 $this->memberList = \array_flip(\array_diff($metaDatas ['#fieldNames'], $metaDatas ['#notSerializable']));
204 $this->propsKeys = OrmUtils::getPropKeys($this->className);
205
206 $this->firstPropKey = OrmUtils::getFirstPropKey($this->className);
207 $this->primaryKeys = OrmUtils::getPrimaryKeys($this->className);
208 if (!($this->allPublic = OrmUtils::hasAllMembersPublic($this->className))) {
209 $this->accessors = $metaDatas ['#accessors'];
210 }
211 }
212
213 protected function updatePrepareStatement() {
214 $this->preparedCondition = SqlUtils::checkWhere($this->conditionParser->getCondition());
215 $this->statement = $this->db->getDaoPreparedStatement($this->tableName, $this->preparedCondition, $this->fieldList . $this->sqlAdditionalMembers);
216 }
217
218 protected function updateSqlAdditionalMembers() {
219 if ($this->additionalMembers) {
220 $this->sqlAdditionalMembers = ',' . $this->parseExpressions();
221 $this->updatePrepareStatement();
222 }
223 }
224
225 protected function parseExpressions() {
226 return \implode(',', $this->additionalMembers);
227 }
228
229 protected function addAditionnalMembers($object, $row) {
230 foreach ($this->additionalMembers as $member => $_) {
231 $object->{$member} = $row [$member] ?? null;
232 $object->_rest [$member] = $row [$member] ?? null;
233 }
234 }
235
236 abstract public function execute($params = [], $useCache = false);
237
244 public function addMember(string $sqlExpression, string $memberName): void {
245 $this->additionalMembers [$memberName] = $sqlExpression . " AS '{$memberName}'";
246 $this->updateSqlAdditionalMembers();
247 }
248
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
264 public function storeDbCache() {
265 $this->db->storeCache();
266 }
267}
Abstract class for database caching Ubiquity\cache\database$DbCache This class is part of Ubiquity.
Definition DbCache.php:20
SQL utilities.
Definition SqlUtils.php:13
Gateway class between database and object model.
Definition DAO.php:33
static $transformerOp
Definition DAO.php:41
static getDb($model)
Definition DAO.php:51
Object/relational mapping utilities.
Definition OrmUtils.php:17
static hasAllMembersPublic($className)
Definition OrmUtils.php:199
static getPrimaryKeys($className)
Definition OrmUtils.php:73
static getModelMetadata($className)
Definition OrmUtils.php:21
Ubiquity\orm\core\prepared$DAOPreparedQuery This class is part of Ubiquity.
storeDbCache()
Store the cache for a prepared Query.
addMembers(array $expressionsNames)
Adds new expressions and their associated members at runtime.
execute($params=[], $useCache=false)
__construct($className, $condition=null, $included=false, $cache=null)
addMember(string $sqlExpression, string $memberName)
Adds a new expression and associates it with a new member of the class added at runtime.
Represents a query condition.
static _getIncludedForStep($included)
static _getFieldList($tableName, $metaDatas)
static _initRelationFields($included, $metaDatas, &$invertedJoinColumns, &$oneToManyFields, &$manyToManyFields)