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