Passed
Push — master ( 42827c...afb1ed )
by Jean-Christophe
11:19
created

DAOPreparedQuery::updateSqlAdditionalMembers()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 2

Importance

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