Test Failed
Push — master ( 768736...0faf86 )
by Jean-Christophe
24:11 queued 13:40
created

DAOPreparedQuery::updatePrepareStatement()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
eloc 2
c 0
b 0
f 0
dl 0
loc 3
ccs 0
cts 0
cp 0
rs 10
cc 1
nc 1
nop 0
crap 2
1
<?php
2
namespace Ubiquity\orm\core\prepared;
3
4
use Ubiquity\db\SqlUtils;
5
use Ubiquity\orm\DAO;
6
use Ubiquity\orm\OrmUtils;
7
use Ubiquity\orm\parser\ConditionParser;
8
use Ubiquity\cache\database\DbCache;
9
10
/**
11
 * Ubiquity\orm\core\prepared$DAOPreparedQuery
12
 * This class is part of Ubiquity
13
 *
14
 * @author jcheron <[email protected]>
15
 * @version 1.0.6
16
 *
17
 */
18
abstract class DAOPreparedQuery {
19
20
	protected $databaseOffset;
21
22
	/**
23
	 *
24
	 * @var ConditionParser
25
	 */
26
	protected $conditionParser;
27
28
	protected $included;
29
30
	protected $hasIncluded;
31
32
	protected $useCache;
33
34
	protected $className;
35
36
	protected $tableName;
37
38
	protected $invertedJoinColumns = null;
39
40
	protected $oneToManyFields = null;
41
42
	protected $manyToManyFields = null;
43
44
	protected $transformers;
45
46
	protected $propsKeys;
47
48
	protected $accessors;
49
50 28
	protected $fieldList;
51 28
52 28
	protected $memberList;
53 28
54 28
	protected $firstPropKey;
55 28
56 28
	protected $condition;
57 28
58
	protected $preparedCondition;
59
60
	protected $additionalMembers = false;
61
62
	protected $sqlAdditionalMembers = "";
63
64
	protected $allPublic = false;
65
66
	protected $statement;
67
68
	/**
69
	 *
70
	 * @var \Ubiquity\db\Database
71
	 */
72
	protected $db;
73
74
	public function __construct($className, $condition = null, $included = false, $cache = null) {
75
		$this->className = $className;
76
		$this->included = $included;
77
		$this->condition = $condition;
78
		$this->conditionParser = new ConditionParser($condition);
79
		$this->prepare($cache);
80
	}
81
82
	public function getFirstPropKey() {
83
		return $this->firstPropKey;
84
	}
85
86
	/**
87
	 *
88
	 * @return \Ubiquity\db\Database
89
	 */
90
	public function getDb() {
91
		return $this->db;
92
	}
93
94
	/**
95
	 *
96
	 * @return mixed
97
	 */
98
	public function getDatabaseOffset() {
99
		return $this->databaseOffset;
100
	}
101
102
	/**
103
	 *
104
	 * @return \Ubiquity\orm\parser\ConditionParser
105
	 */
106
	public function getConditionParser() {
107
		return $this->conditionParser;
108
	}
109
110
	/**
111
	 *
112
	 * @return mixed
113
	 */
114
	public function getIncluded() {
115
		return $this->included;
116
	}
117
118
	/**
119
	 *
120
	 * @return boolean
121
	 */
122
	public function getHasIncluded() {
123
		return $this->hasIncluded;
124
	}
125
126
	/**
127
	 *
128
	 * @return mixed
129
	 */
130
	public function getUseCache() {
131
		return $this->useCache;
132
	}
133
134
	/**
135
	 *
136
	 * @return mixed
137
	 */
138
	public function getClassName() {
139
		return $this->className;
140
	}
141
142
	/**
143
	 *
144
	 * @return mixed
145
	 */
146
	public function getTableName() {
147
		return $this->tableName;
148
	}
149
150
	/**
151
	 *
152
	 * @return mixed
153
	 */
154
	public function getInvertedJoinColumns() {
155
		return $this->invertedJoinColumns;
156
	}
157
158
	/**
159
	 *
160
	 * @return mixed
161
	 */
162
	public function getOneToManyFields() {
163
		return $this->oneToManyFields;
164
	}
165
166
	/**
167
	 *
168
	 * @return mixed
169
	 */
170
	public function getManyToManyFields() {
171
		return $this->manyToManyFields;
172
	}
173
174
	public function getTransformers() {
175
		return $this->transformers;
176
	}
177
178
	public function getPropsKeys() {
179 28
		return $this->propsKeys;
180 28
	}
181 28
182
	/**
183 28
	 *
184 28
	 * @return mixed
185 28
	 */
186 28
	public function getAccessors() {
187
		return $this->accessors;
188
	}
189 28
190 28
	public function getFieldList() {
191 28
		return $this->fieldList;
192 28
	}
193 28
194 28
	/**
195 28
	 *
196
	 * @return mixed
197 1
	 */
198 1
	public function getMemberList() {
199 1
		return $this->memberList;
200
	}
201 1
202
	protected function prepare(?DbCache $cache = null) {
203 1
		$this->db = DAO::getDb($this->className);
204 1
		if (isset($cache)) {
205
			$this->db->setCacheInstance($cache);
206
		}
207 1
		$this->included = DAO::_getIncludedForStep($this->included);
208 1
209 1
		$metaDatas = OrmUtils::getModelMetadata($this->className);
210 1
		$this->tableName = $metaDatas['#tableName'];
211
		$this->hasIncluded = $this->included || (\is_array($this->included) && \sizeof($this->included) > 0);
212 1
		if ($this->hasIncluded) {
213
			DAO::_initRelationFields($this->included, $metaDatas, $this->invertedJoinColumns, $this->oneToManyFields, $this->manyToManyFields);
214
		}
215
		$this->transformers = $metaDatas['#transformers'][DAO::$transformerOp] ?? [];
216
		$this->fieldList = DAO::_getFieldList($this->tableName, $metaDatas);
217
		$this->memberList = \array_flip(\array_diff($metaDatas['#fieldNames'], $metaDatas['#notSerializable']));
218
		$this->propsKeys = OrmUtils::getPropKeys($this->className);
219
220
		$this->firstPropKey = OrmUtils::getFirstPropKey($this->className);
221
		if (! ($this->allPublic = OrmUtils::hasAllMembersPublic($this->className))) {
222 1
			$this->accessors = $metaDatas['#accessors'];
223 1
		}
224 1
	}
225 1
226
	protected function updatePrepareStatement() {
227
		$this->preparedCondition = SqlUtils::checkWhere($this->conditionParser->getCondition());
228
		$this->statement = $this->db->getDaoPreparedStatement($this->tableName, $this->preparedCondition, $this->fieldList . $this->sqlAdditionalMembers);
229
	}
230
231
	protected function updateSqlAdditionalMembers() {
232
		if ($this->additionalMembers) {
233
			$this->sqlAdditionalMembers = ',' . $this->parseExpressions();
234
			$this->updatePrepareStatement();
235
		}
236
	}
237
238
	protected function parseExpressions() {
239
		return \implode(',', $this->additionalMembers);
0 ignored issues
show
Bug introduced by
$this->additionalMembers of type boolean is incompatible with the type array expected by parameter $pieces of implode(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

239
		return \implode(',', /** @scrutinizer ignore-type */ $this->additionalMembers);
Loading history...
240
	}
241
242
	protected function addAditionnalMembers($object, $row) {
243
		foreach ($this->additionalMembers as $member => $_) {
0 ignored issues
show
Bug introduced by
The expression $this->additionalMembers of type boolean is not traversable.
Loading history...
244
			$object->{$member} = $row[$member] ?? null;
245
			$object->_rest[$member] = $row[$member] ?? null;
246
		}
247
	}
248
249
	abstract public function execute($params = [], $useCache = false);
250
251
	/**
252
	 * Adds a new expression and associates it with a new member of the class added at runtime.
253
	 *
254
	 * @param string $sqlExpression
255
	 *        	The SQL expression (part of the SQL SELECT)
256
	 * @param string $memberName
257
	 *        	The new associated member name
258
	 */
259
	public function addMember(string $sqlExpression, string $memberName): void {
260
		$this->additionalMembers[$memberName] = $sqlExpression . " AS '{$memberName}'";
261
		$this->updateSqlAdditionalMembers();
262
	}
263
264
	/**
265
	 * Adds new expressions and their associated members at runtime.
266
	 *
267
	 * @param array $expressionsNames
268
	 *        	An associative array of [memberName=>sqlExpression,...]
269
	 */
270
	public function addMembers(array $expressionsNames): void {
271
		foreach ($expressionsNames as $member => $expression) {
272
			$this->additionalMembers[$member] = $expression . " AS '{$member}'";
273
		}
274
		$this->updateSqlAdditionalMembers();
275
	}
276
277
	/**
278
	 * Store the cache for a prepared Query
279
	 */
280
	public function storeDbCache() {
281
		$this->db->storeCache();
282
	}
283
}
284