Passed
Push — master ( 749218...64fe1c )
by Jean-Christophe
09:18
created

DAOPreparedQuery::getMemberList()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

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