@@ -11,44 +11,44 @@ |
||
11 | 11 | */ |
12 | 12 | class FindObjectsQueryFactory extends AbstractQueryFactory |
13 | 13 | { |
14 | - private $mainTable; |
|
15 | - private $additionalTablesFetch; |
|
16 | - private $filterString; |
|
17 | - |
|
18 | - public function __construct(string $mainTable, array $additionalTablesFetch, $filterString, $orderBy, TDBMService $tdbmService, Schema $schema, OrderByAnalyzer $orderByAnalyzer) |
|
19 | - { |
|
20 | - parent::__construct($tdbmService, $schema, $orderByAnalyzer, $orderBy); |
|
21 | - $this->mainTable = $mainTable; |
|
22 | - $this->additionalTablesFetch = $additionalTablesFetch; |
|
23 | - $this->filterString = $filterString; |
|
24 | - } |
|
25 | - |
|
26 | - protected function compute() |
|
27 | - { |
|
28 | - list($columnDescList, $columnsList, $orderString) = $this->getColumnsList($this->mainTable, $this->additionalTablesFetch, $this->orderBy); |
|
29 | - |
|
30 | - $sql = 'SELECT DISTINCT '.implode(', ', $columnsList).' FROM MAGICJOIN('.$this->mainTable.')'; |
|
31 | - |
|
32 | - $pkColumnNames = $this->schema->getTable($this->mainTable)->getPrimaryKeyColumns(); |
|
33 | - $pkColumnNames = array_map(function ($pkColumn) { |
|
34 | - return $this->tdbmService->getConnection()->quoteIdentifier($this->mainTable).'.'.$this->tdbmService->getConnection()->quoteIdentifier($pkColumn); |
|
35 | - }, $pkColumnNames); |
|
36 | - |
|
37 | - $countSql = 'SELECT COUNT(DISTINCT '.implode(', ', $pkColumnNames).') FROM MAGICJOIN('.$this->mainTable.')'; |
|
38 | - |
|
39 | - if (!empty($this->filterString)) { |
|
40 | - $sql .= ' WHERE '.$this->filterString; |
|
41 | - $countSql .= ' WHERE '.$this->filterString; |
|
42 | - } |
|
43 | - |
|
44 | - if (!empty($orderString)) { |
|
45 | - $sql .= ' ORDER BY '.$orderString; |
|
46 | - } |
|
47 | - |
|
48 | - $this->magicSql = $sql; |
|
49 | - $this->magicSqlCount = $countSql; |
|
50 | - $this->columnDescList = $columnDescList; |
|
51 | - } |
|
14 | + private $mainTable; |
|
15 | + private $additionalTablesFetch; |
|
16 | + private $filterString; |
|
17 | + |
|
18 | + public function __construct(string $mainTable, array $additionalTablesFetch, $filterString, $orderBy, TDBMService $tdbmService, Schema $schema, OrderByAnalyzer $orderByAnalyzer) |
|
19 | + { |
|
20 | + parent::__construct($tdbmService, $schema, $orderByAnalyzer, $orderBy); |
|
21 | + $this->mainTable = $mainTable; |
|
22 | + $this->additionalTablesFetch = $additionalTablesFetch; |
|
23 | + $this->filterString = $filterString; |
|
24 | + } |
|
25 | + |
|
26 | + protected function compute() |
|
27 | + { |
|
28 | + list($columnDescList, $columnsList, $orderString) = $this->getColumnsList($this->mainTable, $this->additionalTablesFetch, $this->orderBy); |
|
29 | + |
|
30 | + $sql = 'SELECT DISTINCT '.implode(', ', $columnsList).' FROM MAGICJOIN('.$this->mainTable.')'; |
|
31 | + |
|
32 | + $pkColumnNames = $this->schema->getTable($this->mainTable)->getPrimaryKeyColumns(); |
|
33 | + $pkColumnNames = array_map(function ($pkColumn) { |
|
34 | + return $this->tdbmService->getConnection()->quoteIdentifier($this->mainTable).'.'.$this->tdbmService->getConnection()->quoteIdentifier($pkColumn); |
|
35 | + }, $pkColumnNames); |
|
36 | + |
|
37 | + $countSql = 'SELECT COUNT(DISTINCT '.implode(', ', $pkColumnNames).') FROM MAGICJOIN('.$this->mainTable.')'; |
|
38 | + |
|
39 | + if (!empty($this->filterString)) { |
|
40 | + $sql .= ' WHERE '.$this->filterString; |
|
41 | + $countSql .= ' WHERE '.$this->filterString; |
|
42 | + } |
|
43 | + |
|
44 | + if (!empty($orderString)) { |
|
45 | + $sql .= ' ORDER BY '.$orderString; |
|
46 | + } |
|
47 | + |
|
48 | + $this->magicSql = $sql; |
|
49 | + $this->magicSqlCount = $countSql; |
|
50 | + $this->columnDescList = $columnDescList; |
|
51 | + } |
|
52 | 52 | |
53 | 53 | |
54 | 54 | } |
@@ -9,25 +9,25 @@ |
||
9 | 9 | */ |
10 | 10 | interface QueryFactory |
11 | 11 | { |
12 | - /** |
|
13 | - * Sets the ORDER BY directive executed in SQL. |
|
14 | - * |
|
15 | - * For instance: |
|
16 | - * |
|
17 | - * $queryFactory->sort('label ASC, status DESC'); |
|
18 | - * |
|
19 | - * **Important:** TDBM does its best to protect you from SQL injection. In particular, it will only allow column names in the "ORDER BY" clause. This means you are safe to pass input from the user directly in the ORDER BY parameter. |
|
20 | - * If you want to pass an expression to the ORDER BY clause, you will need to tell TDBM to stop checking for SQL injections. You do this by passing a `UncheckedOrderBy` object as a parameter: |
|
21 | - * |
|
22 | - * $queryFactory->sort(new UncheckedOrderBy('RAND()')) |
|
23 | - * |
|
24 | - * @param string|UncheckedOrderBy|null $orderBy |
|
25 | - */ |
|
26 | - public function sort($orderBy); |
|
12 | + /** |
|
13 | + * Sets the ORDER BY directive executed in SQL. |
|
14 | + * |
|
15 | + * For instance: |
|
16 | + * |
|
17 | + * $queryFactory->sort('label ASC, status DESC'); |
|
18 | + * |
|
19 | + * **Important:** TDBM does its best to protect you from SQL injection. In particular, it will only allow column names in the "ORDER BY" clause. This means you are safe to pass input from the user directly in the ORDER BY parameter. |
|
20 | + * If you want to pass an expression to the ORDER BY clause, you will need to tell TDBM to stop checking for SQL injections. You do this by passing a `UncheckedOrderBy` object as a parameter: |
|
21 | + * |
|
22 | + * $queryFactory->sort(new UncheckedOrderBy('RAND()')) |
|
23 | + * |
|
24 | + * @param string|UncheckedOrderBy|null $orderBy |
|
25 | + */ |
|
26 | + public function sort($orderBy); |
|
27 | 27 | |
28 | - public function getMagicSql() : string; |
|
28 | + public function getMagicSql() : string; |
|
29 | 29 | |
30 | - public function getMagicSqlCount() : string; |
|
30 | + public function getMagicSqlCount() : string; |
|
31 | 31 | |
32 | - public function getColumnDescriptors() : array; |
|
32 | + public function getColumnDescriptors() : array; |
|
33 | 33 | } |
@@ -32,285 +32,285 @@ |
||
32 | 32 | */ |
33 | 33 | class ResultIterator implements Result, \ArrayAccess, \JsonSerializable |
34 | 34 | { |
35 | - /** |
|
36 | - * @var Statement |
|
37 | - */ |
|
38 | - protected $statement; |
|
39 | - |
|
40 | - private $objectStorage; |
|
41 | - private $className; |
|
42 | - |
|
43 | - private $tdbmService; |
|
44 | - private $parameters; |
|
45 | - private $magicQuery; |
|
46 | - |
|
47 | - /** |
|
48 | - * @var QueryFactory |
|
49 | - */ |
|
50 | - private $queryFactory; |
|
51 | - |
|
52 | - /** |
|
53 | - * @var InnerResultIterator |
|
54 | - */ |
|
55 | - private $innerResultIterator; |
|
56 | - |
|
57 | - private $databasePlatform; |
|
58 | - |
|
59 | - private $totalCount; |
|
60 | - |
|
61 | - private $mode; |
|
62 | - |
|
63 | - private $logger; |
|
64 | - |
|
65 | - public function __construct(QueryFactory $queryFactory, array $parameters, $objectStorage, $className, TDBMService $tdbmService, MagicQuery $magicQuery, $mode, LoggerInterface $logger) |
|
66 | - { |
|
67 | - if ($mode !== null && $mode !== TDBMService::MODE_CURSOR && $mode !== TDBMService::MODE_ARRAY) { |
|
68 | - throw new TDBMException("Unknown fetch mode: '".$mode."'"); |
|
69 | - } |
|
70 | - |
|
71 | - $this->queryFactory = $queryFactory; |
|
72 | - $this->objectStorage = $objectStorage; |
|
73 | - $this->className = $className; |
|
74 | - $this->tdbmService = $tdbmService; |
|
75 | - $this->parameters = $parameters; |
|
76 | - $this->magicQuery = $magicQuery; |
|
77 | - $this->databasePlatform = $this->tdbmService->getConnection()->getDatabasePlatform(); |
|
78 | - $this->mode = $mode; |
|
79 | - $this->logger = $logger; |
|
80 | - } |
|
81 | - |
|
82 | - protected function executeCountQuery() |
|
83 | - { |
|
84 | - $sql = $this->magicQuery->build($this->queryFactory->getMagicSqlCount(), $this->parameters); |
|
85 | - $this->logger->debug('Running count query: '.$sql); |
|
86 | - $this->totalCount = $this->tdbmService->getConnection()->fetchColumn($sql, $this->parameters); |
|
87 | - } |
|
88 | - |
|
89 | - /** |
|
90 | - * Counts found records (this is the number of records fetched, taking into account the LIMIT and OFFSET settings). |
|
91 | - * |
|
92 | - * @return int |
|
93 | - */ |
|
94 | - public function count() |
|
95 | - { |
|
96 | - if ($this->totalCount === null) { |
|
97 | - $this->executeCountQuery(); |
|
98 | - } |
|
99 | - |
|
100 | - return $this->totalCount; |
|
101 | - } |
|
102 | - |
|
103 | - /** |
|
104 | - * Casts the result set to a PHP array. |
|
105 | - * |
|
106 | - * @return array |
|
107 | - */ |
|
108 | - public function toArray() |
|
109 | - { |
|
110 | - return iterator_to_array($this->getIterator()); |
|
111 | - } |
|
112 | - |
|
113 | - /** |
|
114 | - * Returns a new iterator mapping any call using the $callable function. |
|
115 | - * |
|
116 | - * @param callable $callable |
|
117 | - * |
|
118 | - * @return MapIterator |
|
119 | - */ |
|
120 | - public function map(callable $callable) |
|
121 | - { |
|
122 | - return new MapIterator($this->getIterator(), $callable); |
|
123 | - } |
|
124 | - |
|
125 | - /** |
|
126 | - * Retrieve an external iterator. |
|
127 | - * |
|
128 | - * @link http://php.net/manual/en/iteratoraggregate.getiterator.php |
|
129 | - * |
|
130 | - * @return InnerResultIterator An instance of an object implementing <b>Iterator</b> or |
|
131 | - * <b>Traversable</b> |
|
132 | - * |
|
133 | - * @since 5.0.0 |
|
134 | - */ |
|
135 | - public function getIterator() |
|
136 | - { |
|
137 | - if ($this->innerResultIterator === null) { |
|
138 | - if ($this->mode === TDBMService::MODE_CURSOR) { |
|
139 | - $this->innerResultIterator = new InnerResultIterator($this->queryFactory->getMagicSql(), $this->parameters, null, null, $this->queryFactory->getColumnDescriptors(), $this->objectStorage, $this->className, $this->tdbmService, $this->magicQuery, $this->logger); |
|
140 | - } else { |
|
141 | - $this->innerResultIterator = new InnerResultArray($this->queryFactory->getMagicSql(), $this->parameters, null, null, $this->queryFactory->getColumnDescriptors(), $this->objectStorage, $this->className, $this->tdbmService, $this->magicQuery, $this->logger); |
|
142 | - } |
|
143 | - } |
|
144 | - |
|
145 | - return $this->innerResultIterator; |
|
146 | - } |
|
147 | - |
|
148 | - /** |
|
149 | - * @param int $offset |
|
150 | - * @param int $limit |
|
151 | - * |
|
152 | - * @return PageIterator |
|
153 | - */ |
|
154 | - public function take($offset, $limit) |
|
155 | - { |
|
156 | - return new PageIterator($this, $this->queryFactory->getMagicSql(), $this->parameters, $limit, $offset, $this->queryFactory->getColumnDescriptors(), $this->objectStorage, $this->className, $this->tdbmService, $this->magicQuery, $this->mode, $this->logger); |
|
157 | - } |
|
158 | - |
|
159 | - /** |
|
160 | - * Whether a offset exists. |
|
161 | - * |
|
162 | - * @link http://php.net/manual/en/arrayaccess.offsetexists.php |
|
163 | - * |
|
164 | - * @param mixed $offset <p> |
|
165 | - * An offset to check for. |
|
166 | - * </p> |
|
167 | - * |
|
168 | - * @return bool true on success or false on failure. |
|
169 | - * </p> |
|
170 | - * <p> |
|
171 | - * The return value will be casted to boolean if non-boolean was returned |
|
172 | - * |
|
173 | - * @since 5.0.0 |
|
174 | - */ |
|
175 | - public function offsetExists($offset) |
|
176 | - { |
|
177 | - return $this->getIterator()->offsetExists($offset); |
|
178 | - } |
|
179 | - |
|
180 | - /** |
|
181 | - * Offset to retrieve. |
|
182 | - * |
|
183 | - * @link http://php.net/manual/en/arrayaccess.offsetget.php |
|
184 | - * |
|
185 | - * @param mixed $offset <p> |
|
186 | - * The offset to retrieve. |
|
187 | - * </p> |
|
188 | - * |
|
189 | - * @return mixed Can return all value types |
|
190 | - * |
|
191 | - * @since 5.0.0 |
|
192 | - */ |
|
193 | - public function offsetGet($offset) |
|
194 | - { |
|
195 | - return $this->getIterator()->offsetGet($offset); |
|
196 | - } |
|
197 | - |
|
198 | - /** |
|
199 | - * Offset to set. |
|
200 | - * |
|
201 | - * @link http://php.net/manual/en/arrayaccess.offsetset.php |
|
202 | - * |
|
203 | - * @param mixed $offset <p> |
|
204 | - * The offset to assign the value to. |
|
205 | - * </p> |
|
206 | - * @param mixed $value <p> |
|
207 | - * The value to set. |
|
208 | - * </p> |
|
209 | - * |
|
210 | - * @since 5.0.0 |
|
211 | - */ |
|
212 | - public function offsetSet($offset, $value) |
|
213 | - { |
|
214 | - return $this->getIterator()->offsetSet($offset, $value); |
|
215 | - } |
|
216 | - |
|
217 | - /** |
|
218 | - * Offset to unset. |
|
219 | - * |
|
220 | - * @link http://php.net/manual/en/arrayaccess.offsetunset.php |
|
221 | - * |
|
222 | - * @param mixed $offset <p> |
|
223 | - * The offset to unset. |
|
224 | - * </p> |
|
225 | - * |
|
226 | - * @since 5.0.0 |
|
227 | - */ |
|
228 | - public function offsetUnset($offset) |
|
229 | - { |
|
230 | - return $this->getIterator()->offsetUnset($offset); |
|
231 | - } |
|
232 | - |
|
233 | - /** |
|
234 | - * Specify data which should be serialized to JSON. |
|
235 | - * |
|
236 | - * @link http://php.net/manual/en/jsonserializable.jsonserialize.php |
|
237 | - * |
|
238 | - * @param bool $stopRecursion Parameter used internally by TDBM to |
|
239 | - * stop embedded objects from embedding |
|
240 | - * other objects |
|
241 | - * |
|
242 | - * @return mixed data which can be serialized by <b>json_encode</b>, |
|
243 | - * which is a value of any type other than a resource |
|
244 | - * |
|
245 | - * @since 5.4.0 |
|
246 | - */ |
|
247 | - public function jsonSerialize($stopRecursion = false) |
|
248 | - { |
|
249 | - return array_map(function (AbstractTDBMObject $item) use ($stopRecursion) { |
|
250 | - return $item->jsonSerialize($stopRecursion); |
|
251 | - }, $this->toArray()); |
|
252 | - } |
|
253 | - |
|
254 | - /** |
|
255 | - * Returns only one value (the first) of the result set. |
|
256 | - * Returns null if no value exists. |
|
257 | - * |
|
258 | - * @return mixed|null |
|
259 | - */ |
|
260 | - public function first() |
|
261 | - { |
|
262 | - $page = $this->take(0, 1); |
|
263 | - foreach ($page as $bean) { |
|
264 | - return $bean; |
|
265 | - } |
|
266 | - |
|
267 | - return; |
|
268 | - } |
|
269 | - |
|
270 | - /** |
|
271 | - * Sets the ORDER BY directive executed in SQL and returns a NEW ResultIterator. |
|
272 | - * |
|
273 | - * For instance: |
|
274 | - * |
|
275 | - * $resultSet = $resultSet->withOrder('label ASC, status DESC'); |
|
276 | - * |
|
277 | - * **Important:** TDBM does its best to protect you from SQL injection. In particular, it will only allow column names in the "ORDER BY" clause. This means you are safe to pass input from the user directly in the ORDER BY parameter. |
|
278 | - * If you want to pass an expression to the ORDER BY clause, you will need to tell TDBM to stop checking for SQL injections. You do this by passing a `UncheckedOrderBy` object as a parameter: |
|
279 | - * |
|
280 | - * $resultSet->withOrder(new UncheckedOrderBy('RAND()')) |
|
281 | - * |
|
282 | - * @param string|UncheckedOrderBy|null $orderBy |
|
283 | - * |
|
284 | - * @return ResultIterator |
|
285 | - */ |
|
286 | - public function withOrder($orderBy) : ResultIterator |
|
287 | - { |
|
288 | - $clone = clone $this; |
|
289 | - $clone->queryFactory = clone $this->queryFactory; |
|
290 | - $clone->queryFactory->sort($orderBy); |
|
291 | - $clone->innerResultIterator = null; |
|
292 | - |
|
293 | - return $clone; |
|
294 | - } |
|
295 | - |
|
296 | - /** |
|
297 | - * Sets new parameters for the SQL query and returns a NEW ResultIterator. |
|
298 | - * |
|
299 | - * For instance: |
|
300 | - * |
|
301 | - * $resultSet = $resultSet->withParameters('label ASC, status DESC'); |
|
302 | - * |
|
303 | - * @param string|UncheckedOrderBy|null $orderBy |
|
304 | - * |
|
305 | - * @return ResultIterator |
|
306 | - */ |
|
307 | - public function withParameters(array $parameters) : ResultIterator |
|
308 | - { |
|
309 | - $clone = clone $this; |
|
310 | - $clone->parameters = $parameters; |
|
311 | - $clone->innerResultIterator = null; |
|
312 | - $clone->totalCount = null; |
|
313 | - |
|
314 | - return $clone; |
|
315 | - } |
|
35 | + /** |
|
36 | + * @var Statement |
|
37 | + */ |
|
38 | + protected $statement; |
|
39 | + |
|
40 | + private $objectStorage; |
|
41 | + private $className; |
|
42 | + |
|
43 | + private $tdbmService; |
|
44 | + private $parameters; |
|
45 | + private $magicQuery; |
|
46 | + |
|
47 | + /** |
|
48 | + * @var QueryFactory |
|
49 | + */ |
|
50 | + private $queryFactory; |
|
51 | + |
|
52 | + /** |
|
53 | + * @var InnerResultIterator |
|
54 | + */ |
|
55 | + private $innerResultIterator; |
|
56 | + |
|
57 | + private $databasePlatform; |
|
58 | + |
|
59 | + private $totalCount; |
|
60 | + |
|
61 | + private $mode; |
|
62 | + |
|
63 | + private $logger; |
|
64 | + |
|
65 | + public function __construct(QueryFactory $queryFactory, array $parameters, $objectStorage, $className, TDBMService $tdbmService, MagicQuery $magicQuery, $mode, LoggerInterface $logger) |
|
66 | + { |
|
67 | + if ($mode !== null && $mode !== TDBMService::MODE_CURSOR && $mode !== TDBMService::MODE_ARRAY) { |
|
68 | + throw new TDBMException("Unknown fetch mode: '".$mode."'"); |
|
69 | + } |
|
70 | + |
|
71 | + $this->queryFactory = $queryFactory; |
|
72 | + $this->objectStorage = $objectStorage; |
|
73 | + $this->className = $className; |
|
74 | + $this->tdbmService = $tdbmService; |
|
75 | + $this->parameters = $parameters; |
|
76 | + $this->magicQuery = $magicQuery; |
|
77 | + $this->databasePlatform = $this->tdbmService->getConnection()->getDatabasePlatform(); |
|
78 | + $this->mode = $mode; |
|
79 | + $this->logger = $logger; |
|
80 | + } |
|
81 | + |
|
82 | + protected function executeCountQuery() |
|
83 | + { |
|
84 | + $sql = $this->magicQuery->build($this->queryFactory->getMagicSqlCount(), $this->parameters); |
|
85 | + $this->logger->debug('Running count query: '.$sql); |
|
86 | + $this->totalCount = $this->tdbmService->getConnection()->fetchColumn($sql, $this->parameters); |
|
87 | + } |
|
88 | + |
|
89 | + /** |
|
90 | + * Counts found records (this is the number of records fetched, taking into account the LIMIT and OFFSET settings). |
|
91 | + * |
|
92 | + * @return int |
|
93 | + */ |
|
94 | + public function count() |
|
95 | + { |
|
96 | + if ($this->totalCount === null) { |
|
97 | + $this->executeCountQuery(); |
|
98 | + } |
|
99 | + |
|
100 | + return $this->totalCount; |
|
101 | + } |
|
102 | + |
|
103 | + /** |
|
104 | + * Casts the result set to a PHP array. |
|
105 | + * |
|
106 | + * @return array |
|
107 | + */ |
|
108 | + public function toArray() |
|
109 | + { |
|
110 | + return iterator_to_array($this->getIterator()); |
|
111 | + } |
|
112 | + |
|
113 | + /** |
|
114 | + * Returns a new iterator mapping any call using the $callable function. |
|
115 | + * |
|
116 | + * @param callable $callable |
|
117 | + * |
|
118 | + * @return MapIterator |
|
119 | + */ |
|
120 | + public function map(callable $callable) |
|
121 | + { |
|
122 | + return new MapIterator($this->getIterator(), $callable); |
|
123 | + } |
|
124 | + |
|
125 | + /** |
|
126 | + * Retrieve an external iterator. |
|
127 | + * |
|
128 | + * @link http://php.net/manual/en/iteratoraggregate.getiterator.php |
|
129 | + * |
|
130 | + * @return InnerResultIterator An instance of an object implementing <b>Iterator</b> or |
|
131 | + * <b>Traversable</b> |
|
132 | + * |
|
133 | + * @since 5.0.0 |
|
134 | + */ |
|
135 | + public function getIterator() |
|
136 | + { |
|
137 | + if ($this->innerResultIterator === null) { |
|
138 | + if ($this->mode === TDBMService::MODE_CURSOR) { |
|
139 | + $this->innerResultIterator = new InnerResultIterator($this->queryFactory->getMagicSql(), $this->parameters, null, null, $this->queryFactory->getColumnDescriptors(), $this->objectStorage, $this->className, $this->tdbmService, $this->magicQuery, $this->logger); |
|
140 | + } else { |
|
141 | + $this->innerResultIterator = new InnerResultArray($this->queryFactory->getMagicSql(), $this->parameters, null, null, $this->queryFactory->getColumnDescriptors(), $this->objectStorage, $this->className, $this->tdbmService, $this->magicQuery, $this->logger); |
|
142 | + } |
|
143 | + } |
|
144 | + |
|
145 | + return $this->innerResultIterator; |
|
146 | + } |
|
147 | + |
|
148 | + /** |
|
149 | + * @param int $offset |
|
150 | + * @param int $limit |
|
151 | + * |
|
152 | + * @return PageIterator |
|
153 | + */ |
|
154 | + public function take($offset, $limit) |
|
155 | + { |
|
156 | + return new PageIterator($this, $this->queryFactory->getMagicSql(), $this->parameters, $limit, $offset, $this->queryFactory->getColumnDescriptors(), $this->objectStorage, $this->className, $this->tdbmService, $this->magicQuery, $this->mode, $this->logger); |
|
157 | + } |
|
158 | + |
|
159 | + /** |
|
160 | + * Whether a offset exists. |
|
161 | + * |
|
162 | + * @link http://php.net/manual/en/arrayaccess.offsetexists.php |
|
163 | + * |
|
164 | + * @param mixed $offset <p> |
|
165 | + * An offset to check for. |
|
166 | + * </p> |
|
167 | + * |
|
168 | + * @return bool true on success or false on failure. |
|
169 | + * </p> |
|
170 | + * <p> |
|
171 | + * The return value will be casted to boolean if non-boolean was returned |
|
172 | + * |
|
173 | + * @since 5.0.0 |
|
174 | + */ |
|
175 | + public function offsetExists($offset) |
|
176 | + { |
|
177 | + return $this->getIterator()->offsetExists($offset); |
|
178 | + } |
|
179 | + |
|
180 | + /** |
|
181 | + * Offset to retrieve. |
|
182 | + * |
|
183 | + * @link http://php.net/manual/en/arrayaccess.offsetget.php |
|
184 | + * |
|
185 | + * @param mixed $offset <p> |
|
186 | + * The offset to retrieve. |
|
187 | + * </p> |
|
188 | + * |
|
189 | + * @return mixed Can return all value types |
|
190 | + * |
|
191 | + * @since 5.0.0 |
|
192 | + */ |
|
193 | + public function offsetGet($offset) |
|
194 | + { |
|
195 | + return $this->getIterator()->offsetGet($offset); |
|
196 | + } |
|
197 | + |
|
198 | + /** |
|
199 | + * Offset to set. |
|
200 | + * |
|
201 | + * @link http://php.net/manual/en/arrayaccess.offsetset.php |
|
202 | + * |
|
203 | + * @param mixed $offset <p> |
|
204 | + * The offset to assign the value to. |
|
205 | + * </p> |
|
206 | + * @param mixed $value <p> |
|
207 | + * The value to set. |
|
208 | + * </p> |
|
209 | + * |
|
210 | + * @since 5.0.0 |
|
211 | + */ |
|
212 | + public function offsetSet($offset, $value) |
|
213 | + { |
|
214 | + return $this->getIterator()->offsetSet($offset, $value); |
|
215 | + } |
|
216 | + |
|
217 | + /** |
|
218 | + * Offset to unset. |
|
219 | + * |
|
220 | + * @link http://php.net/manual/en/arrayaccess.offsetunset.php |
|
221 | + * |
|
222 | + * @param mixed $offset <p> |
|
223 | + * The offset to unset. |
|
224 | + * </p> |
|
225 | + * |
|
226 | + * @since 5.0.0 |
|
227 | + */ |
|
228 | + public function offsetUnset($offset) |
|
229 | + { |
|
230 | + return $this->getIterator()->offsetUnset($offset); |
|
231 | + } |
|
232 | + |
|
233 | + /** |
|
234 | + * Specify data which should be serialized to JSON. |
|
235 | + * |
|
236 | + * @link http://php.net/manual/en/jsonserializable.jsonserialize.php |
|
237 | + * |
|
238 | + * @param bool $stopRecursion Parameter used internally by TDBM to |
|
239 | + * stop embedded objects from embedding |
|
240 | + * other objects |
|
241 | + * |
|
242 | + * @return mixed data which can be serialized by <b>json_encode</b>, |
|
243 | + * which is a value of any type other than a resource |
|
244 | + * |
|
245 | + * @since 5.4.0 |
|
246 | + */ |
|
247 | + public function jsonSerialize($stopRecursion = false) |
|
248 | + { |
|
249 | + return array_map(function (AbstractTDBMObject $item) use ($stopRecursion) { |
|
250 | + return $item->jsonSerialize($stopRecursion); |
|
251 | + }, $this->toArray()); |
|
252 | + } |
|
253 | + |
|
254 | + /** |
|
255 | + * Returns only one value (the first) of the result set. |
|
256 | + * Returns null if no value exists. |
|
257 | + * |
|
258 | + * @return mixed|null |
|
259 | + */ |
|
260 | + public function first() |
|
261 | + { |
|
262 | + $page = $this->take(0, 1); |
|
263 | + foreach ($page as $bean) { |
|
264 | + return $bean; |
|
265 | + } |
|
266 | + |
|
267 | + return; |
|
268 | + } |
|
269 | + |
|
270 | + /** |
|
271 | + * Sets the ORDER BY directive executed in SQL and returns a NEW ResultIterator. |
|
272 | + * |
|
273 | + * For instance: |
|
274 | + * |
|
275 | + * $resultSet = $resultSet->withOrder('label ASC, status DESC'); |
|
276 | + * |
|
277 | + * **Important:** TDBM does its best to protect you from SQL injection. In particular, it will only allow column names in the "ORDER BY" clause. This means you are safe to pass input from the user directly in the ORDER BY parameter. |
|
278 | + * If you want to pass an expression to the ORDER BY clause, you will need to tell TDBM to stop checking for SQL injections. You do this by passing a `UncheckedOrderBy` object as a parameter: |
|
279 | + * |
|
280 | + * $resultSet->withOrder(new UncheckedOrderBy('RAND()')) |
|
281 | + * |
|
282 | + * @param string|UncheckedOrderBy|null $orderBy |
|
283 | + * |
|
284 | + * @return ResultIterator |
|
285 | + */ |
|
286 | + public function withOrder($orderBy) : ResultIterator |
|
287 | + { |
|
288 | + $clone = clone $this; |
|
289 | + $clone->queryFactory = clone $this->queryFactory; |
|
290 | + $clone->queryFactory->sort($orderBy); |
|
291 | + $clone->innerResultIterator = null; |
|
292 | + |
|
293 | + return $clone; |
|
294 | + } |
|
295 | + |
|
296 | + /** |
|
297 | + * Sets new parameters for the SQL query and returns a NEW ResultIterator. |
|
298 | + * |
|
299 | + * For instance: |
|
300 | + * |
|
301 | + * $resultSet = $resultSet->withParameters('label ASC, status DESC'); |
|
302 | + * |
|
303 | + * @param string|UncheckedOrderBy|null $orderBy |
|
304 | + * |
|
305 | + * @return ResultIterator |
|
306 | + */ |
|
307 | + public function withParameters(array $parameters) : ResultIterator |
|
308 | + { |
|
309 | + $clone = clone $this; |
|
310 | + $clone->parameters = $parameters; |
|
311 | + $clone->innerResultIterator = null; |
|
312 | + $clone->totalCount = null; |
|
313 | + |
|
314 | + return $clone; |
|
315 | + } |
|
316 | 316 | } |
@@ -10,214 +10,214 @@ |
||
10 | 10 | |
11 | 11 | abstract class AbstractQueryFactory implements QueryFactory |
12 | 12 | { |
13 | - /** |
|
14 | - * @var TDBMService |
|
15 | - */ |
|
16 | - protected $tdbmService; |
|
17 | - |
|
18 | - /** |
|
19 | - * @var Schema |
|
20 | - */ |
|
21 | - protected $schema; |
|
22 | - |
|
23 | - /** |
|
24 | - * @var OrderByAnalyzer |
|
25 | - */ |
|
26 | - protected $orderByAnalyzer; |
|
27 | - |
|
28 | - /** |
|
29 | - * @var string|UncheckedOrderBy|null |
|
30 | - */ |
|
31 | - protected $orderBy; |
|
32 | - |
|
33 | - protected $magicSql; |
|
34 | - protected $magicSqlCount; |
|
35 | - protected $columnDescList; |
|
36 | - |
|
37 | - /** |
|
38 | - * @param TDBMService $tdbmService |
|
39 | - */ |
|
40 | - public function __construct(TDBMService $tdbmService, Schema $schema, OrderByAnalyzer $orderByAnalyzer, $orderBy) |
|
41 | - { |
|
42 | - $this->tdbmService = $tdbmService; |
|
43 | - $this->schema = $schema; |
|
44 | - $this->orderByAnalyzer = $orderByAnalyzer; |
|
45 | - $this->orderBy = $orderBy; |
|
46 | - } |
|
47 | - |
|
48 | - /** |
|
49 | - * Returns the column list that must be fetched for the SQL request. |
|
50 | - * |
|
51 | - * Note: MySQL dictates that ORDER BYed columns should appear in the SELECT clause. |
|
52 | - * |
|
53 | - * @param string $mainTable |
|
54 | - * @param array $additionalTablesFetch |
|
55 | - * @param string|UncheckedOrderBy|null $orderBy |
|
56 | - * |
|
57 | - * @return array |
|
58 | - * |
|
59 | - * @throws \Doctrine\DBAL\Schema\SchemaException |
|
60 | - */ |
|
61 | - protected function getColumnsList(string $mainTable, array $additionalTablesFetch = array(), $orderBy = null) |
|
62 | - { |
|
63 | - // From the table name and the additional tables we want to fetch, let's build a list of all tables |
|
64 | - // that must be part of the select columns. |
|
65 | - |
|
66 | - $connection = $this->tdbmService->getConnection(); |
|
67 | - |
|
68 | - $tableGroups = []; |
|
69 | - $allFetchedTables = $this->tdbmService->_getRelatedTablesByInheritance($mainTable); |
|
70 | - $tableGroupName = $this->getTableGroupName($allFetchedTables); |
|
71 | - foreach ($allFetchedTables as $table) { |
|
72 | - $tableGroups[$table] = $tableGroupName; |
|
73 | - } |
|
74 | - |
|
75 | - $columnsList = []; |
|
76 | - $columnDescList = []; |
|
77 | - $sortColumn = 0; |
|
78 | - $reconstructedOrderBy = null; |
|
79 | - |
|
80 | - if (is_string($orderBy)) { |
|
81 | - $orderBy = trim($orderBy); |
|
82 | - if ($orderBy === '') { |
|
83 | - $orderBy = null; |
|
84 | - } |
|
85 | - } |
|
86 | - |
|
87 | - // Now, let's deal with "order by columns" |
|
88 | - if ($orderBy !== null) { |
|
89 | - if ($orderBy instanceof UncheckedOrderBy) { |
|
90 | - $securedOrderBy = false; |
|
91 | - $orderBy = $orderBy->getOrderBy(); |
|
92 | - $reconstructedOrderBy = $orderBy; |
|
93 | - } else { |
|
94 | - $securedOrderBy = true; |
|
95 | - $reconstructedOrderBys = []; |
|
96 | - } |
|
97 | - $orderByColumns = $this->orderByAnalyzer->analyzeOrderBy($orderBy); |
|
98 | - |
|
99 | - // If we sort by a column, there is a high chance we will fetch the bean containing this column. |
|
100 | - // Hence, we should add the table to the $additionalTablesFetch |
|
101 | - foreach ($orderByColumns as $orderByColumn) { |
|
102 | - if ($orderByColumn['type'] === 'colref') { |
|
103 | - if ($orderByColumn['table'] !== null) { |
|
104 | - $additionalTablesFetch[] = $orderByColumn['table']; |
|
105 | - } |
|
106 | - if ($securedOrderBy) { |
|
107 | - $reconstructedOrderBys[] = ($orderByColumn['table'] !== null ? $connection->quoteIdentifier($orderByColumn['table']).'.' : '').$connection->quoteIdentifier($orderByColumn['column']).' '.$orderByColumn['direction']; |
|
108 | - } |
|
109 | - } elseif ($orderByColumn['type'] === 'expr') { |
|
110 | - $sortColumnName = 'sort_column_'.$sortColumn; |
|
111 | - $columnsList[] = $orderByColumn['expr'].' as '.$sortColumnName; |
|
112 | - $columnDescList[] = [ |
|
113 | - 'tableGroup' => null, |
|
114 | - ]; |
|
115 | - ++$sortColumn; |
|
116 | - |
|
117 | - if ($securedOrderBy) { |
|
118 | - throw new TDBMInvalidArgumentException('Invalid ORDER BY column: "'.$orderByColumn['expr'].'". If you want to use expression in your ORDER BY clause, you must wrap them in a UncheckedOrderBy object. For instance: new UncheckedOrderBy("col1 + col2 DESC")'); |
|
119 | - } |
|
120 | - } |
|
121 | - } |
|
122 | - |
|
123 | - if ($reconstructedOrderBy === null) { |
|
124 | - $reconstructedOrderBy = implode(', ', $reconstructedOrderBys); |
|
125 | - } |
|
126 | - } |
|
127 | - |
|
128 | - foreach ($additionalTablesFetch as $additionalTable) { |
|
129 | - $relatedTables = $this->tdbmService->_getRelatedTablesByInheritance($additionalTable); |
|
130 | - $tableGroupName = $this->getTableGroupName($relatedTables); |
|
131 | - foreach ($relatedTables as $table) { |
|
132 | - $tableGroups[$table] = $tableGroupName; |
|
133 | - } |
|
134 | - $allFetchedTables = array_merge($allFetchedTables, $relatedTables); |
|
135 | - } |
|
136 | - |
|
137 | - // Let's remove any duplicate |
|
138 | - $allFetchedTables = array_flip(array_flip($allFetchedTables)); |
|
139 | - |
|
140 | - // Now, let's build the column list |
|
141 | - foreach ($allFetchedTables as $table) { |
|
142 | - foreach ($this->schema->getTable($table)->getColumns() as $column) { |
|
143 | - $columnName = $column->getName(); |
|
144 | - $columnDescList[] = [ |
|
145 | - 'as' => $table.'____'.$columnName, |
|
146 | - 'table' => $table, |
|
147 | - 'column' => $columnName, |
|
148 | - 'type' => $column->getType(), |
|
149 | - 'tableGroup' => $tableGroups[$table], |
|
150 | - ]; |
|
151 | - $columnsList[] = $connection->quoteIdentifier($table).'.'.$connection->quoteIdentifier($columnName).' as '. |
|
152 | - $connection->quoteIdentifier($table.'____'.$columnName); |
|
153 | - } |
|
154 | - } |
|
155 | - |
|
156 | - return [$columnDescList, $columnsList, $reconstructedOrderBy]; |
|
157 | - } |
|
158 | - |
|
159 | - abstract protected function compute(); |
|
160 | - |
|
161 | - /** |
|
162 | - * Returns an identifier for the group of tables passed in parameter. |
|
163 | - * |
|
164 | - * @param string[] $relatedTables |
|
165 | - * |
|
166 | - * @return string |
|
167 | - */ |
|
168 | - protected function getTableGroupName(array $relatedTables) |
|
169 | - { |
|
170 | - sort($relatedTables); |
|
171 | - |
|
172 | - return implode('_``_', $relatedTables); |
|
173 | - } |
|
174 | - |
|
175 | - public function getMagicSql() : string |
|
176 | - { |
|
177 | - if ($this->magicSql === null) { |
|
178 | - $this->compute(); |
|
179 | - } |
|
180 | - |
|
181 | - return $this->magicSql; |
|
182 | - } |
|
183 | - |
|
184 | - public function getMagicSqlCount() : string |
|
185 | - { |
|
186 | - if ($this->magicSqlCount === null) { |
|
187 | - $this->compute(); |
|
188 | - } |
|
189 | - |
|
190 | - return $this->magicSqlCount; |
|
191 | - } |
|
192 | - |
|
193 | - public function getColumnDescriptors() : array |
|
194 | - { |
|
195 | - if ($this->columnDescList === null) { |
|
196 | - $this->compute(); |
|
197 | - } |
|
198 | - |
|
199 | - return $this->columnDescList; |
|
200 | - } |
|
201 | - |
|
202 | - /** |
|
203 | - * Sets the ORDER BY directive executed in SQL. |
|
204 | - * |
|
205 | - * For instance: |
|
206 | - * |
|
207 | - * $queryFactory->sort('label ASC, status DESC'); |
|
208 | - * |
|
209 | - * **Important:** TDBM does its best to protect you from SQL injection. In particular, it will only allow column names in the "ORDER BY" clause. This means you are safe to pass input from the user directly in the ORDER BY parameter. |
|
210 | - * If you want to pass an expression to the ORDER BY clause, you will need to tell TDBM to stop checking for SQL injections. You do this by passing a `UncheckedOrderBy` object as a parameter: |
|
211 | - * |
|
212 | - * $queryFactory->sort(new UncheckedOrderBy('RAND()')) |
|
213 | - * |
|
214 | - * @param string|UncheckedOrderBy|null $orderBy |
|
215 | - */ |
|
216 | - public function sort($orderBy) |
|
217 | - { |
|
218 | - $this->orderBy = $orderBy; |
|
219 | - $this->magicSql = null; |
|
220 | - $this->magicSqlCount = null; |
|
221 | - $this->columnDescList = null; |
|
222 | - } |
|
13 | + /** |
|
14 | + * @var TDBMService |
|
15 | + */ |
|
16 | + protected $tdbmService; |
|
17 | + |
|
18 | + /** |
|
19 | + * @var Schema |
|
20 | + */ |
|
21 | + protected $schema; |
|
22 | + |
|
23 | + /** |
|
24 | + * @var OrderByAnalyzer |
|
25 | + */ |
|
26 | + protected $orderByAnalyzer; |
|
27 | + |
|
28 | + /** |
|
29 | + * @var string|UncheckedOrderBy|null |
|
30 | + */ |
|
31 | + protected $orderBy; |
|
32 | + |
|
33 | + protected $magicSql; |
|
34 | + protected $magicSqlCount; |
|
35 | + protected $columnDescList; |
|
36 | + |
|
37 | + /** |
|
38 | + * @param TDBMService $tdbmService |
|
39 | + */ |
|
40 | + public function __construct(TDBMService $tdbmService, Schema $schema, OrderByAnalyzer $orderByAnalyzer, $orderBy) |
|
41 | + { |
|
42 | + $this->tdbmService = $tdbmService; |
|
43 | + $this->schema = $schema; |
|
44 | + $this->orderByAnalyzer = $orderByAnalyzer; |
|
45 | + $this->orderBy = $orderBy; |
|
46 | + } |
|
47 | + |
|
48 | + /** |
|
49 | + * Returns the column list that must be fetched for the SQL request. |
|
50 | + * |
|
51 | + * Note: MySQL dictates that ORDER BYed columns should appear in the SELECT clause. |
|
52 | + * |
|
53 | + * @param string $mainTable |
|
54 | + * @param array $additionalTablesFetch |
|
55 | + * @param string|UncheckedOrderBy|null $orderBy |
|
56 | + * |
|
57 | + * @return array |
|
58 | + * |
|
59 | + * @throws \Doctrine\DBAL\Schema\SchemaException |
|
60 | + */ |
|
61 | + protected function getColumnsList(string $mainTable, array $additionalTablesFetch = array(), $orderBy = null) |
|
62 | + { |
|
63 | + // From the table name and the additional tables we want to fetch, let's build a list of all tables |
|
64 | + // that must be part of the select columns. |
|
65 | + |
|
66 | + $connection = $this->tdbmService->getConnection(); |
|
67 | + |
|
68 | + $tableGroups = []; |
|
69 | + $allFetchedTables = $this->tdbmService->_getRelatedTablesByInheritance($mainTable); |
|
70 | + $tableGroupName = $this->getTableGroupName($allFetchedTables); |
|
71 | + foreach ($allFetchedTables as $table) { |
|
72 | + $tableGroups[$table] = $tableGroupName; |
|
73 | + } |
|
74 | + |
|
75 | + $columnsList = []; |
|
76 | + $columnDescList = []; |
|
77 | + $sortColumn = 0; |
|
78 | + $reconstructedOrderBy = null; |
|
79 | + |
|
80 | + if (is_string($orderBy)) { |
|
81 | + $orderBy = trim($orderBy); |
|
82 | + if ($orderBy === '') { |
|
83 | + $orderBy = null; |
|
84 | + } |
|
85 | + } |
|
86 | + |
|
87 | + // Now, let's deal with "order by columns" |
|
88 | + if ($orderBy !== null) { |
|
89 | + if ($orderBy instanceof UncheckedOrderBy) { |
|
90 | + $securedOrderBy = false; |
|
91 | + $orderBy = $orderBy->getOrderBy(); |
|
92 | + $reconstructedOrderBy = $orderBy; |
|
93 | + } else { |
|
94 | + $securedOrderBy = true; |
|
95 | + $reconstructedOrderBys = []; |
|
96 | + } |
|
97 | + $orderByColumns = $this->orderByAnalyzer->analyzeOrderBy($orderBy); |
|
98 | + |
|
99 | + // If we sort by a column, there is a high chance we will fetch the bean containing this column. |
|
100 | + // Hence, we should add the table to the $additionalTablesFetch |
|
101 | + foreach ($orderByColumns as $orderByColumn) { |
|
102 | + if ($orderByColumn['type'] === 'colref') { |
|
103 | + if ($orderByColumn['table'] !== null) { |
|
104 | + $additionalTablesFetch[] = $orderByColumn['table']; |
|
105 | + } |
|
106 | + if ($securedOrderBy) { |
|
107 | + $reconstructedOrderBys[] = ($orderByColumn['table'] !== null ? $connection->quoteIdentifier($orderByColumn['table']).'.' : '').$connection->quoteIdentifier($orderByColumn['column']).' '.$orderByColumn['direction']; |
|
108 | + } |
|
109 | + } elseif ($orderByColumn['type'] === 'expr') { |
|
110 | + $sortColumnName = 'sort_column_'.$sortColumn; |
|
111 | + $columnsList[] = $orderByColumn['expr'].' as '.$sortColumnName; |
|
112 | + $columnDescList[] = [ |
|
113 | + 'tableGroup' => null, |
|
114 | + ]; |
|
115 | + ++$sortColumn; |
|
116 | + |
|
117 | + if ($securedOrderBy) { |
|
118 | + throw new TDBMInvalidArgumentException('Invalid ORDER BY column: "'.$orderByColumn['expr'].'". If you want to use expression in your ORDER BY clause, you must wrap them in a UncheckedOrderBy object. For instance: new UncheckedOrderBy("col1 + col2 DESC")'); |
|
119 | + } |
|
120 | + } |
|
121 | + } |
|
122 | + |
|
123 | + if ($reconstructedOrderBy === null) { |
|
124 | + $reconstructedOrderBy = implode(', ', $reconstructedOrderBys); |
|
125 | + } |
|
126 | + } |
|
127 | + |
|
128 | + foreach ($additionalTablesFetch as $additionalTable) { |
|
129 | + $relatedTables = $this->tdbmService->_getRelatedTablesByInheritance($additionalTable); |
|
130 | + $tableGroupName = $this->getTableGroupName($relatedTables); |
|
131 | + foreach ($relatedTables as $table) { |
|
132 | + $tableGroups[$table] = $tableGroupName; |
|
133 | + } |
|
134 | + $allFetchedTables = array_merge($allFetchedTables, $relatedTables); |
|
135 | + } |
|
136 | + |
|
137 | + // Let's remove any duplicate |
|
138 | + $allFetchedTables = array_flip(array_flip($allFetchedTables)); |
|
139 | + |
|
140 | + // Now, let's build the column list |
|
141 | + foreach ($allFetchedTables as $table) { |
|
142 | + foreach ($this->schema->getTable($table)->getColumns() as $column) { |
|
143 | + $columnName = $column->getName(); |
|
144 | + $columnDescList[] = [ |
|
145 | + 'as' => $table.'____'.$columnName, |
|
146 | + 'table' => $table, |
|
147 | + 'column' => $columnName, |
|
148 | + 'type' => $column->getType(), |
|
149 | + 'tableGroup' => $tableGroups[$table], |
|
150 | + ]; |
|
151 | + $columnsList[] = $connection->quoteIdentifier($table).'.'.$connection->quoteIdentifier($columnName).' as '. |
|
152 | + $connection->quoteIdentifier($table.'____'.$columnName); |
|
153 | + } |
|
154 | + } |
|
155 | + |
|
156 | + return [$columnDescList, $columnsList, $reconstructedOrderBy]; |
|
157 | + } |
|
158 | + |
|
159 | + abstract protected function compute(); |
|
160 | + |
|
161 | + /** |
|
162 | + * Returns an identifier for the group of tables passed in parameter. |
|
163 | + * |
|
164 | + * @param string[] $relatedTables |
|
165 | + * |
|
166 | + * @return string |
|
167 | + */ |
|
168 | + protected function getTableGroupName(array $relatedTables) |
|
169 | + { |
|
170 | + sort($relatedTables); |
|
171 | + |
|
172 | + return implode('_``_', $relatedTables); |
|
173 | + } |
|
174 | + |
|
175 | + public function getMagicSql() : string |
|
176 | + { |
|
177 | + if ($this->magicSql === null) { |
|
178 | + $this->compute(); |
|
179 | + } |
|
180 | + |
|
181 | + return $this->magicSql; |
|
182 | + } |
|
183 | + |
|
184 | + public function getMagicSqlCount() : string |
|
185 | + { |
|
186 | + if ($this->magicSqlCount === null) { |
|
187 | + $this->compute(); |
|
188 | + } |
|
189 | + |
|
190 | + return $this->magicSqlCount; |
|
191 | + } |
|
192 | + |
|
193 | + public function getColumnDescriptors() : array |
|
194 | + { |
|
195 | + if ($this->columnDescList === null) { |
|
196 | + $this->compute(); |
|
197 | + } |
|
198 | + |
|
199 | + return $this->columnDescList; |
|
200 | + } |
|
201 | + |
|
202 | + /** |
|
203 | + * Sets the ORDER BY directive executed in SQL. |
|
204 | + * |
|
205 | + * For instance: |
|
206 | + * |
|
207 | + * $queryFactory->sort('label ASC, status DESC'); |
|
208 | + * |
|
209 | + * **Important:** TDBM does its best to protect you from SQL injection. In particular, it will only allow column names in the "ORDER BY" clause. This means you are safe to pass input from the user directly in the ORDER BY parameter. |
|
210 | + * If you want to pass an expression to the ORDER BY clause, you will need to tell TDBM to stop checking for SQL injections. You do this by passing a `UncheckedOrderBy` object as a parameter: |
|
211 | + * |
|
212 | + * $queryFactory->sort(new UncheckedOrderBy('RAND()')) |
|
213 | + * |
|
214 | + * @param string|UncheckedOrderBy|null $orderBy |
|
215 | + */ |
|
216 | + public function sort($orderBy) |
|
217 | + { |
|
218 | + $this->orderBy = $orderBy; |
|
219 | + $this->magicSql = null; |
|
220 | + $this->magicSqlCount = null; |
|
221 | + $this->columnDescList = null; |
|
222 | + } |
|
223 | 223 | } |
@@ -15,191 +15,191 @@ |
||
15 | 15 | */ |
16 | 16 | class FindObjectsFromSqlQueryFactory extends AbstractQueryFactory |
17 | 17 | { |
18 | - private $mainTable; |
|
19 | - private $from; |
|
20 | - private $filterString; |
|
21 | - private $cache; |
|
22 | - private $cachePrefix; |
|
23 | - |
|
24 | - public function __construct(string $mainTable, string $from, $filterString, $orderBy, TDBMService $tdbmService, Schema $schema, OrderByAnalyzer $orderByAnalyzer, SchemaAnalyzer $schemaAnalyzer, Cache $cache, string $cachePrefix) |
|
25 | - { |
|
26 | - parent::__construct($tdbmService, $schema, $orderByAnalyzer, $orderBy); |
|
27 | - $this->mainTable = $mainTable; |
|
28 | - $this->from = $from; |
|
29 | - $this->filterString = $filterString; |
|
30 | - $this->schemaAnalyzer = $schemaAnalyzer; |
|
31 | - $this->cache = $cache; |
|
32 | - $this->cachePrefix = $cachePrefix; |
|
33 | - } |
|
34 | - |
|
35 | - protected function compute() |
|
36 | - { |
|
37 | - $connection = $this->tdbmService->getConnection(); |
|
38 | - |
|
39 | - $columnsList = null; |
|
40 | - |
|
41 | - $allFetchedTables = $this->tdbmService->_getRelatedTablesByInheritance($this->mainTable); |
|
42 | - |
|
43 | - $columnDescList = []; |
|
44 | - |
|
45 | - $tableGroupName = $this->getTableGroupName($allFetchedTables); |
|
46 | - |
|
47 | - foreach ($this->schema->getTable($this->mainTable)->getColumns() as $column) { |
|
48 | - $columnName = $column->getName(); |
|
49 | - $columnDescList[] = [ |
|
50 | - 'as' => $columnName, |
|
51 | - 'table' => $this->mainTable, |
|
52 | - 'column' => $columnName, |
|
53 | - 'type' => $column->getType(), |
|
54 | - 'tableGroup' => $tableGroupName, |
|
55 | - ]; |
|
56 | - } |
|
57 | - |
|
58 | - $sql = 'SELECT DISTINCT '.implode(', ', array_map(function ($columnDesc) { |
|
59 | - return $this->tdbmService->getConnection()->quoteIdentifier($this->mainTable).'.'.$this->tdbmService->getConnection()->quoteIdentifier($columnDesc['column']); |
|
60 | - }, $columnDescList)).' FROM '.$this->from; |
|
61 | - |
|
62 | - if (count($allFetchedTables) > 1) { |
|
63 | - list($columnDescList, $columnsList, $orderString) = $this->getColumnsList($this->mainTable, [], $this->orderBy); |
|
64 | - } elseif ($this->orderBy) { |
|
65 | - list(, , $orderString) = $this->getColumnsList($this->mainTable, [], $this->orderBy); |
|
66 | - } |
|
67 | - |
|
68 | - // Let's compute the COUNT. |
|
69 | - $pkColumnNames = $this->schema->getTable($this->mainTable)->getPrimaryKeyColumns(); |
|
70 | - $pkColumnNames = array_map(function ($pkColumn) { |
|
71 | - return $this->tdbmService->getConnection()->quoteIdentifier($this->mainTable).'.'.$this->tdbmService->getConnection()->quoteIdentifier($pkColumn); |
|
72 | - }, $pkColumnNames); |
|
73 | - |
|
74 | - $countSql = 'SELECT COUNT(DISTINCT '.implode(', ', $pkColumnNames).') FROM '.$this->from; |
|
75 | - |
|
76 | - if (!empty($this->filterString)) { |
|
77 | - $sql .= ' WHERE '.$this->filterString; |
|
78 | - $countSql .= ' WHERE '.$this->filterString; |
|
79 | - } |
|
80 | - |
|
81 | - if (!empty($orderString)) { |
|
82 | - $sql .= ' ORDER BY '.$orderString; |
|
83 | - } |
|
84 | - |
|
85 | - if (stripos($countSql, 'GROUP BY') !== false) { |
|
86 | - throw new TDBMException('Unsupported use of GROUP BY in SQL request.'); |
|
87 | - } |
|
88 | - |
|
89 | - if ($columnsList !== null) { |
|
90 | - $joinSql = ''; |
|
91 | - $parentFks = $this->getParentRelationshipForeignKeys($this->mainTable); |
|
92 | - foreach ($parentFks as $fk) { |
|
93 | - $joinSql .= sprintf(' JOIN %s ON (%s.%s = %s.%s)', |
|
94 | - $connection->quoteIdentifier($fk->getForeignTableName()), |
|
95 | - $connection->quoteIdentifier($fk->getLocalTableName()), |
|
96 | - $connection->quoteIdentifier($fk->getLocalColumns()[0]), |
|
97 | - $connection->quoteIdentifier($fk->getForeignTableName()), |
|
98 | - $connection->quoteIdentifier($fk->getForeignColumns()[0]) |
|
99 | - ); |
|
100 | - } |
|
101 | - |
|
102 | - $childrenFks = $this->getChildrenRelationshipForeignKeys($this->mainTable); |
|
103 | - foreach ($childrenFks as $fk) { |
|
104 | - $joinSql .= sprintf(' LEFT JOIN %s ON (%s.%s = %s.%s)', |
|
105 | - $connection->quoteIdentifier($fk->getLocalTableName()), |
|
106 | - $connection->quoteIdentifier($fk->getForeignTableName()), |
|
107 | - $connection->quoteIdentifier($fk->getForeignColumns()[0]), |
|
108 | - $connection->quoteIdentifier($fk->getLocalTableName()), |
|
109 | - $connection->quoteIdentifier($fk->getLocalColumns()[0]) |
|
110 | - ); |
|
111 | - } |
|
112 | - |
|
113 | - $sql = 'SELECT '.implode(', ', $columnsList).' FROM ('.$sql.') AS '.$this->mainTable.' '.$joinSql; |
|
114 | - if (!empty($orderString)) { |
|
115 | - $sql .= ' ORDER BY '.$orderString; |
|
116 | - } |
|
117 | - } |
|
118 | - |
|
119 | - $this->magicSql = $sql; |
|
120 | - $this->magicSqlCount = $countSql; |
|
121 | - $this->columnDescList = $columnDescList; |
|
122 | - } |
|
123 | - |
|
124 | - /** |
|
125 | - * @param string $tableName |
|
126 | - * |
|
127 | - * @return ForeignKeyConstraint[] |
|
128 | - */ |
|
129 | - private function getParentRelationshipForeignKeys($tableName) |
|
130 | - { |
|
131 | - return $this->fromCache($this->cachePrefix.'_parentrelationshipfks_'.$tableName, function () use ($tableName) { |
|
132 | - return $this->getParentRelationshipForeignKeysWithoutCache($tableName); |
|
133 | - }); |
|
134 | - } |
|
135 | - |
|
136 | - /** |
|
137 | - * @param string $tableName |
|
138 | - * |
|
139 | - * @return ForeignKeyConstraint[] |
|
140 | - */ |
|
141 | - private function getParentRelationshipForeignKeysWithoutCache($tableName) |
|
142 | - { |
|
143 | - $parentFks = []; |
|
144 | - $currentTable = $tableName; |
|
145 | - while ($currentFk = $this->schemaAnalyzer->getParentRelationship($currentTable)) { |
|
146 | - $currentTable = $currentFk->getForeignTableName(); |
|
147 | - $parentFks[] = $currentFk; |
|
148 | - } |
|
149 | - |
|
150 | - return $parentFks; |
|
151 | - } |
|
152 | - |
|
153 | - /** |
|
154 | - * @param string $tableName |
|
155 | - * |
|
156 | - * @return ForeignKeyConstraint[] |
|
157 | - */ |
|
158 | - private function getChildrenRelationshipForeignKeys(string $tableName) : array |
|
159 | - { |
|
160 | - return $this->fromCache($this->cachePrefix.'_childrenrelationshipfks_'.$tableName, function () use ($tableName) { |
|
161 | - return $this->getChildrenRelationshipForeignKeysWithoutCache($tableName); |
|
162 | - }); |
|
163 | - } |
|
164 | - |
|
165 | - /** |
|
166 | - * @param string $tableName |
|
167 | - * |
|
168 | - * @return ForeignKeyConstraint[] |
|
169 | - */ |
|
170 | - private function getChildrenRelationshipForeignKeysWithoutCache(string $tableName) : array |
|
171 | - { |
|
172 | - $children = $this->schemaAnalyzer->getChildrenRelationships($tableName); |
|
173 | - |
|
174 | - if (!empty($children)) { |
|
175 | - $fksTables = array_map(function (ForeignKeyConstraint $fk) { |
|
176 | - return $this->getChildrenRelationshipForeignKeys($fk->getLocalTableName()); |
|
177 | - }, $children); |
|
178 | - |
|
179 | - $fks = array_merge($children, call_user_func_array('array_merge', $fksTables)); |
|
180 | - |
|
181 | - return $fks; |
|
182 | - } else { |
|
183 | - return []; |
|
184 | - } |
|
185 | - } |
|
186 | - |
|
187 | - /** |
|
188 | - * Returns an item from cache or computes it using $closure and puts it in cache. |
|
189 | - * |
|
190 | - * @param string $key |
|
191 | - * @param callable $closure |
|
192 | - * |
|
193 | - * @return mixed |
|
194 | - */ |
|
195 | - protected function fromCache(string $key, callable $closure) |
|
196 | - { |
|
197 | - $item = $this->cache->fetch($key); |
|
198 | - if ($item === false) { |
|
199 | - $item = $closure(); |
|
200 | - $this->cache->save($key, $item); |
|
201 | - } |
|
202 | - |
|
203 | - return $item; |
|
204 | - } |
|
18 | + private $mainTable; |
|
19 | + private $from; |
|
20 | + private $filterString; |
|
21 | + private $cache; |
|
22 | + private $cachePrefix; |
|
23 | + |
|
24 | + public function __construct(string $mainTable, string $from, $filterString, $orderBy, TDBMService $tdbmService, Schema $schema, OrderByAnalyzer $orderByAnalyzer, SchemaAnalyzer $schemaAnalyzer, Cache $cache, string $cachePrefix) |
|
25 | + { |
|
26 | + parent::__construct($tdbmService, $schema, $orderByAnalyzer, $orderBy); |
|
27 | + $this->mainTable = $mainTable; |
|
28 | + $this->from = $from; |
|
29 | + $this->filterString = $filterString; |
|
30 | + $this->schemaAnalyzer = $schemaAnalyzer; |
|
31 | + $this->cache = $cache; |
|
32 | + $this->cachePrefix = $cachePrefix; |
|
33 | + } |
|
34 | + |
|
35 | + protected function compute() |
|
36 | + { |
|
37 | + $connection = $this->tdbmService->getConnection(); |
|
38 | + |
|
39 | + $columnsList = null; |
|
40 | + |
|
41 | + $allFetchedTables = $this->tdbmService->_getRelatedTablesByInheritance($this->mainTable); |
|
42 | + |
|
43 | + $columnDescList = []; |
|
44 | + |
|
45 | + $tableGroupName = $this->getTableGroupName($allFetchedTables); |
|
46 | + |
|
47 | + foreach ($this->schema->getTable($this->mainTable)->getColumns() as $column) { |
|
48 | + $columnName = $column->getName(); |
|
49 | + $columnDescList[] = [ |
|
50 | + 'as' => $columnName, |
|
51 | + 'table' => $this->mainTable, |
|
52 | + 'column' => $columnName, |
|
53 | + 'type' => $column->getType(), |
|
54 | + 'tableGroup' => $tableGroupName, |
|
55 | + ]; |
|
56 | + } |
|
57 | + |
|
58 | + $sql = 'SELECT DISTINCT '.implode(', ', array_map(function ($columnDesc) { |
|
59 | + return $this->tdbmService->getConnection()->quoteIdentifier($this->mainTable).'.'.$this->tdbmService->getConnection()->quoteIdentifier($columnDesc['column']); |
|
60 | + }, $columnDescList)).' FROM '.$this->from; |
|
61 | + |
|
62 | + if (count($allFetchedTables) > 1) { |
|
63 | + list($columnDescList, $columnsList, $orderString) = $this->getColumnsList($this->mainTable, [], $this->orderBy); |
|
64 | + } elseif ($this->orderBy) { |
|
65 | + list(, , $orderString) = $this->getColumnsList($this->mainTable, [], $this->orderBy); |
|
66 | + } |
|
67 | + |
|
68 | + // Let's compute the COUNT. |
|
69 | + $pkColumnNames = $this->schema->getTable($this->mainTable)->getPrimaryKeyColumns(); |
|
70 | + $pkColumnNames = array_map(function ($pkColumn) { |
|
71 | + return $this->tdbmService->getConnection()->quoteIdentifier($this->mainTable).'.'.$this->tdbmService->getConnection()->quoteIdentifier($pkColumn); |
|
72 | + }, $pkColumnNames); |
|
73 | + |
|
74 | + $countSql = 'SELECT COUNT(DISTINCT '.implode(', ', $pkColumnNames).') FROM '.$this->from; |
|
75 | + |
|
76 | + if (!empty($this->filterString)) { |
|
77 | + $sql .= ' WHERE '.$this->filterString; |
|
78 | + $countSql .= ' WHERE '.$this->filterString; |
|
79 | + } |
|
80 | + |
|
81 | + if (!empty($orderString)) { |
|
82 | + $sql .= ' ORDER BY '.$orderString; |
|
83 | + } |
|
84 | + |
|
85 | + if (stripos($countSql, 'GROUP BY') !== false) { |
|
86 | + throw new TDBMException('Unsupported use of GROUP BY in SQL request.'); |
|
87 | + } |
|
88 | + |
|
89 | + if ($columnsList !== null) { |
|
90 | + $joinSql = ''; |
|
91 | + $parentFks = $this->getParentRelationshipForeignKeys($this->mainTable); |
|
92 | + foreach ($parentFks as $fk) { |
|
93 | + $joinSql .= sprintf(' JOIN %s ON (%s.%s = %s.%s)', |
|
94 | + $connection->quoteIdentifier($fk->getForeignTableName()), |
|
95 | + $connection->quoteIdentifier($fk->getLocalTableName()), |
|
96 | + $connection->quoteIdentifier($fk->getLocalColumns()[0]), |
|
97 | + $connection->quoteIdentifier($fk->getForeignTableName()), |
|
98 | + $connection->quoteIdentifier($fk->getForeignColumns()[0]) |
|
99 | + ); |
|
100 | + } |
|
101 | + |
|
102 | + $childrenFks = $this->getChildrenRelationshipForeignKeys($this->mainTable); |
|
103 | + foreach ($childrenFks as $fk) { |
|
104 | + $joinSql .= sprintf(' LEFT JOIN %s ON (%s.%s = %s.%s)', |
|
105 | + $connection->quoteIdentifier($fk->getLocalTableName()), |
|
106 | + $connection->quoteIdentifier($fk->getForeignTableName()), |
|
107 | + $connection->quoteIdentifier($fk->getForeignColumns()[0]), |
|
108 | + $connection->quoteIdentifier($fk->getLocalTableName()), |
|
109 | + $connection->quoteIdentifier($fk->getLocalColumns()[0]) |
|
110 | + ); |
|
111 | + } |
|
112 | + |
|
113 | + $sql = 'SELECT '.implode(', ', $columnsList).' FROM ('.$sql.') AS '.$this->mainTable.' '.$joinSql; |
|
114 | + if (!empty($orderString)) { |
|
115 | + $sql .= ' ORDER BY '.$orderString; |
|
116 | + } |
|
117 | + } |
|
118 | + |
|
119 | + $this->magicSql = $sql; |
|
120 | + $this->magicSqlCount = $countSql; |
|
121 | + $this->columnDescList = $columnDescList; |
|
122 | + } |
|
123 | + |
|
124 | + /** |
|
125 | + * @param string $tableName |
|
126 | + * |
|
127 | + * @return ForeignKeyConstraint[] |
|
128 | + */ |
|
129 | + private function getParentRelationshipForeignKeys($tableName) |
|
130 | + { |
|
131 | + return $this->fromCache($this->cachePrefix.'_parentrelationshipfks_'.$tableName, function () use ($tableName) { |
|
132 | + return $this->getParentRelationshipForeignKeysWithoutCache($tableName); |
|
133 | + }); |
|
134 | + } |
|
135 | + |
|
136 | + /** |
|
137 | + * @param string $tableName |
|
138 | + * |
|
139 | + * @return ForeignKeyConstraint[] |
|
140 | + */ |
|
141 | + private function getParentRelationshipForeignKeysWithoutCache($tableName) |
|
142 | + { |
|
143 | + $parentFks = []; |
|
144 | + $currentTable = $tableName; |
|
145 | + while ($currentFk = $this->schemaAnalyzer->getParentRelationship($currentTable)) { |
|
146 | + $currentTable = $currentFk->getForeignTableName(); |
|
147 | + $parentFks[] = $currentFk; |
|
148 | + } |
|
149 | + |
|
150 | + return $parentFks; |
|
151 | + } |
|
152 | + |
|
153 | + /** |
|
154 | + * @param string $tableName |
|
155 | + * |
|
156 | + * @return ForeignKeyConstraint[] |
|
157 | + */ |
|
158 | + private function getChildrenRelationshipForeignKeys(string $tableName) : array |
|
159 | + { |
|
160 | + return $this->fromCache($this->cachePrefix.'_childrenrelationshipfks_'.$tableName, function () use ($tableName) { |
|
161 | + return $this->getChildrenRelationshipForeignKeysWithoutCache($tableName); |
|
162 | + }); |
|
163 | + } |
|
164 | + |
|
165 | + /** |
|
166 | + * @param string $tableName |
|
167 | + * |
|
168 | + * @return ForeignKeyConstraint[] |
|
169 | + */ |
|
170 | + private function getChildrenRelationshipForeignKeysWithoutCache(string $tableName) : array |
|
171 | + { |
|
172 | + $children = $this->schemaAnalyzer->getChildrenRelationships($tableName); |
|
173 | + |
|
174 | + if (!empty($children)) { |
|
175 | + $fksTables = array_map(function (ForeignKeyConstraint $fk) { |
|
176 | + return $this->getChildrenRelationshipForeignKeys($fk->getLocalTableName()); |
|
177 | + }, $children); |
|
178 | + |
|
179 | + $fks = array_merge($children, call_user_func_array('array_merge', $fksTables)); |
|
180 | + |
|
181 | + return $fks; |
|
182 | + } else { |
|
183 | + return []; |
|
184 | + } |
|
185 | + } |
|
186 | + |
|
187 | + /** |
|
188 | + * Returns an item from cache or computes it using $closure and puts it in cache. |
|
189 | + * |
|
190 | + * @param string $key |
|
191 | + * @param callable $closure |
|
192 | + * |
|
193 | + * @return mixed |
|
194 | + */ |
|
195 | + protected function fromCache(string $key, callable $closure) |
|
196 | + { |
|
197 | + $item = $this->cache->fetch($key); |
|
198 | + if ($item === false) { |
|
199 | + $item = $closure(); |
|
200 | + $this->cache->save($key, $item); |
|
201 | + } |
|
202 | + |
|
203 | + return $item; |
|
204 | + } |
|
205 | 205 | } |
@@ -4,36 +4,36 @@ |
||
4 | 4 | |
5 | 5 | interface MethodDescriptorInterface |
6 | 6 | { |
7 | - /** |
|
8 | - * Returns the name of the method to be generated. |
|
9 | - * |
|
10 | - * @return string |
|
11 | - */ |
|
12 | - public function getName() : string; |
|
7 | + /** |
|
8 | + * Returns the name of the method to be generated. |
|
9 | + * |
|
10 | + * @return string |
|
11 | + */ |
|
12 | + public function getName() : string; |
|
13 | 13 | |
14 | - /** |
|
15 | - * Requests the use of an alternative name for this method. |
|
16 | - */ |
|
17 | - public function useAlternativeName(); |
|
14 | + /** |
|
15 | + * Requests the use of an alternative name for this method. |
|
16 | + */ |
|
17 | + public function useAlternativeName(); |
|
18 | 18 | |
19 | - /** |
|
20 | - * Returns the code of the method. |
|
21 | - * |
|
22 | - * @return string |
|
23 | - */ |
|
24 | - public function getCode() : string; |
|
19 | + /** |
|
20 | + * Returns the code of the method. |
|
21 | + * |
|
22 | + * @return string |
|
23 | + */ |
|
24 | + public function getCode() : string; |
|
25 | 25 | |
26 | - /** |
|
27 | - * Returns an array of classes that needs a "use" for this method. |
|
28 | - * |
|
29 | - * @return string[] |
|
30 | - */ |
|
31 | - public function getUsedClasses() : array; |
|
26 | + /** |
|
27 | + * Returns an array of classes that needs a "use" for this method. |
|
28 | + * |
|
29 | + * @return string[] |
|
30 | + */ |
|
31 | + public function getUsedClasses() : array; |
|
32 | 32 | |
33 | - /** |
|
34 | - * Returns the code to past in jsonSerialize. |
|
35 | - * |
|
36 | - * @return string |
|
37 | - */ |
|
38 | - public function getJsonSerializeCode() : string; |
|
33 | + /** |
|
34 | + * Returns the code to past in jsonSerialize. |
|
35 | + * |
|
36 | + * @return string |
|
37 | + */ |
|
38 | + public function getJsonSerializeCode() : string; |
|
39 | 39 | } |
@@ -14,274 +14,274 @@ |
||
14 | 14 | */ |
15 | 15 | class AlterableResultIterator implements Result, \ArrayAccess, \JsonSerializable |
16 | 16 | { |
17 | - /** |
|
18 | - * @var \Iterator|null |
|
19 | - */ |
|
20 | - private $resultIterator; |
|
17 | + /** |
|
18 | + * @var \Iterator|null |
|
19 | + */ |
|
20 | + private $resultIterator; |
|
21 | 21 | |
22 | - /** |
|
23 | - * Key: the object to alter in the result set. |
|
24 | - * Value: "add" => the object will be added to the resultset (if it is not found in it) |
|
25 | - * "delete" => the object will be removed from the resultset (if found). |
|
26 | - * |
|
27 | - * @var \SplObjectStorage |
|
28 | - */ |
|
29 | - private $alterations; |
|
22 | + /** |
|
23 | + * Key: the object to alter in the result set. |
|
24 | + * Value: "add" => the object will be added to the resultset (if it is not found in it) |
|
25 | + * "delete" => the object will be removed from the resultset (if found). |
|
26 | + * |
|
27 | + * @var \SplObjectStorage |
|
28 | + */ |
|
29 | + private $alterations; |
|
30 | 30 | |
31 | - /** |
|
32 | - * The result array from the result set. |
|
33 | - * |
|
34 | - * @var array|null |
|
35 | - */ |
|
36 | - private $resultArray; |
|
31 | + /** |
|
32 | + * The result array from the result set. |
|
33 | + * |
|
34 | + * @var array|null |
|
35 | + */ |
|
36 | + private $resultArray; |
|
37 | 37 | |
38 | - /** |
|
39 | - * @param \Iterator|null $resultIterator |
|
40 | - */ |
|
41 | - public function __construct(\Iterator $resultIterator = null) |
|
42 | - { |
|
43 | - $this->resultIterator = $resultIterator; |
|
44 | - $this->alterations = new \SplObjectStorage(); |
|
45 | - } |
|
38 | + /** |
|
39 | + * @param \Iterator|null $resultIterator |
|
40 | + */ |
|
41 | + public function __construct(\Iterator $resultIterator = null) |
|
42 | + { |
|
43 | + $this->resultIterator = $resultIterator; |
|
44 | + $this->alterations = new \SplObjectStorage(); |
|
45 | + } |
|
46 | 46 | |
47 | - /** |
|
48 | - * Sets a new iterator as the base iterator to be altered. |
|
49 | - * |
|
50 | - * @param \Iterator $resultIterator |
|
51 | - */ |
|
52 | - public function setResultIterator(\Iterator $resultIterator) |
|
53 | - { |
|
54 | - $this->resultIterator = $resultIterator; |
|
55 | - $this->resultArray = null; |
|
56 | - } |
|
47 | + /** |
|
48 | + * Sets a new iterator as the base iterator to be altered. |
|
49 | + * |
|
50 | + * @param \Iterator $resultIterator |
|
51 | + */ |
|
52 | + public function setResultIterator(\Iterator $resultIterator) |
|
53 | + { |
|
54 | + $this->resultIterator = $resultIterator; |
|
55 | + $this->resultArray = null; |
|
56 | + } |
|
57 | 57 | |
58 | - /** |
|
59 | - * Returns the non altered result iterator (or null if none exist). |
|
60 | - * |
|
61 | - * @return \Iterator|null |
|
62 | - */ |
|
63 | - public function getUnderlyingResultIterator() |
|
64 | - { |
|
65 | - return $this->resultIterator; |
|
66 | - } |
|
58 | + /** |
|
59 | + * Returns the non altered result iterator (or null if none exist). |
|
60 | + * |
|
61 | + * @return \Iterator|null |
|
62 | + */ |
|
63 | + public function getUnderlyingResultIterator() |
|
64 | + { |
|
65 | + return $this->resultIterator; |
|
66 | + } |
|
67 | 67 | |
68 | - /** |
|
69 | - * Adds an additional object to the result set (if not already available). |
|
70 | - * |
|
71 | - * @param $object |
|
72 | - */ |
|
73 | - public function add($object) |
|
74 | - { |
|
75 | - $this->alterations->attach($object, 'add'); |
|
68 | + /** |
|
69 | + * Adds an additional object to the result set (if not already available). |
|
70 | + * |
|
71 | + * @param $object |
|
72 | + */ |
|
73 | + public function add($object) |
|
74 | + { |
|
75 | + $this->alterations->attach($object, 'add'); |
|
76 | 76 | |
77 | - if ($this->resultArray !== null) { |
|
78 | - $foundKey = array_search($object, $this->resultArray, true); |
|
79 | - if ($foundKey === false) { |
|
80 | - $this->resultArray[] = $object; |
|
81 | - } |
|
82 | - } |
|
83 | - } |
|
77 | + if ($this->resultArray !== null) { |
|
78 | + $foundKey = array_search($object, $this->resultArray, true); |
|
79 | + if ($foundKey === false) { |
|
80 | + $this->resultArray[] = $object; |
|
81 | + } |
|
82 | + } |
|
83 | + } |
|
84 | 84 | |
85 | - /** |
|
86 | - * Removes an object from the result set. |
|
87 | - * |
|
88 | - * @param $object |
|
89 | - */ |
|
90 | - public function remove($object) |
|
91 | - { |
|
92 | - $this->alterations->attach($object, 'delete'); |
|
85 | + /** |
|
86 | + * Removes an object from the result set. |
|
87 | + * |
|
88 | + * @param $object |
|
89 | + */ |
|
90 | + public function remove($object) |
|
91 | + { |
|
92 | + $this->alterations->attach($object, 'delete'); |
|
93 | 93 | |
94 | - if ($this->resultArray !== null) { |
|
95 | - $foundKey = array_search($object, $this->resultArray, true); |
|
96 | - if ($foundKey !== false) { |
|
97 | - unset($this->resultArray[$foundKey]); |
|
98 | - } |
|
99 | - } |
|
100 | - } |
|
94 | + if ($this->resultArray !== null) { |
|
95 | + $foundKey = array_search($object, $this->resultArray, true); |
|
96 | + if ($foundKey !== false) { |
|
97 | + unset($this->resultArray[$foundKey]); |
|
98 | + } |
|
99 | + } |
|
100 | + } |
|
101 | 101 | |
102 | - /** |
|
103 | - * Casts the result set to a PHP array. |
|
104 | - * |
|
105 | - * @return array |
|
106 | - */ |
|
107 | - public function toArray() |
|
108 | - { |
|
109 | - if ($this->resultArray === null) { |
|
110 | - if ($this->resultIterator !== null) { |
|
111 | - $this->resultArray = iterator_to_array($this->resultIterator); |
|
112 | - } else { |
|
113 | - $this->resultArray = []; |
|
114 | - } |
|
102 | + /** |
|
103 | + * Casts the result set to a PHP array. |
|
104 | + * |
|
105 | + * @return array |
|
106 | + */ |
|
107 | + public function toArray() |
|
108 | + { |
|
109 | + if ($this->resultArray === null) { |
|
110 | + if ($this->resultIterator !== null) { |
|
111 | + $this->resultArray = iterator_to_array($this->resultIterator); |
|
112 | + } else { |
|
113 | + $this->resultArray = []; |
|
114 | + } |
|
115 | 115 | |
116 | - foreach ($this->alterations as $obj) { |
|
117 | - $action = $this->alterations->getInfo(); // return, if exists, associated with cur. obj. data; else NULL |
|
116 | + foreach ($this->alterations as $obj) { |
|
117 | + $action = $this->alterations->getInfo(); // return, if exists, associated with cur. obj. data; else NULL |
|
118 | 118 | |
119 | - $foundKey = array_search($obj, $this->resultArray, true); |
|
119 | + $foundKey = array_search($obj, $this->resultArray, true); |
|
120 | 120 | |
121 | - if ($action === 'add' && $foundKey === false) { |
|
122 | - $this->resultArray[] = $obj; |
|
123 | - } elseif ($action === 'delete' && $foundKey !== false) { |
|
124 | - unset($this->resultArray[$foundKey]); |
|
125 | - } |
|
126 | - } |
|
127 | - } |
|
121 | + if ($action === 'add' && $foundKey === false) { |
|
122 | + $this->resultArray[] = $obj; |
|
123 | + } elseif ($action === 'delete' && $foundKey !== false) { |
|
124 | + unset($this->resultArray[$foundKey]); |
|
125 | + } |
|
126 | + } |
|
127 | + } |
|
128 | 128 | |
129 | - return array_values($this->resultArray); |
|
130 | - } |
|
129 | + return array_values($this->resultArray); |
|
130 | + } |
|
131 | 131 | |
132 | - /** |
|
133 | - * Whether a offset exists. |
|
134 | - * |
|
135 | - * @link http://php.net/manual/en/arrayaccess.offsetexists.php |
|
136 | - * |
|
137 | - * @param mixed $offset <p> |
|
138 | - * An offset to check for. |
|
139 | - * </p> |
|
140 | - * |
|
141 | - * @return bool true on success or false on failure. |
|
142 | - * </p> |
|
143 | - * <p> |
|
144 | - * The return value will be casted to boolean if non-boolean was returned |
|
145 | - * |
|
146 | - * @since 5.0.0 |
|
147 | - */ |
|
148 | - public function offsetExists($offset) |
|
149 | - { |
|
150 | - return isset($this->toArray()[$offset]); |
|
151 | - } |
|
132 | + /** |
|
133 | + * Whether a offset exists. |
|
134 | + * |
|
135 | + * @link http://php.net/manual/en/arrayaccess.offsetexists.php |
|
136 | + * |
|
137 | + * @param mixed $offset <p> |
|
138 | + * An offset to check for. |
|
139 | + * </p> |
|
140 | + * |
|
141 | + * @return bool true on success or false on failure. |
|
142 | + * </p> |
|
143 | + * <p> |
|
144 | + * The return value will be casted to boolean if non-boolean was returned |
|
145 | + * |
|
146 | + * @since 5.0.0 |
|
147 | + */ |
|
148 | + public function offsetExists($offset) |
|
149 | + { |
|
150 | + return isset($this->toArray()[$offset]); |
|
151 | + } |
|
152 | 152 | |
153 | - /** |
|
154 | - * Offset to retrieve. |
|
155 | - * |
|
156 | - * @link http://php.net/manual/en/arrayaccess.offsetget.php |
|
157 | - * |
|
158 | - * @param mixed $offset <p> |
|
159 | - * The offset to retrieve. |
|
160 | - * </p> |
|
161 | - * |
|
162 | - * @return mixed Can return all value types |
|
163 | - * |
|
164 | - * @since 5.0.0 |
|
165 | - */ |
|
166 | - public function offsetGet($offset) |
|
167 | - { |
|
168 | - return $this->toArray()[$offset]; |
|
169 | - } |
|
153 | + /** |
|
154 | + * Offset to retrieve. |
|
155 | + * |
|
156 | + * @link http://php.net/manual/en/arrayaccess.offsetget.php |
|
157 | + * |
|
158 | + * @param mixed $offset <p> |
|
159 | + * The offset to retrieve. |
|
160 | + * </p> |
|
161 | + * |
|
162 | + * @return mixed Can return all value types |
|
163 | + * |
|
164 | + * @since 5.0.0 |
|
165 | + */ |
|
166 | + public function offsetGet($offset) |
|
167 | + { |
|
168 | + return $this->toArray()[$offset]; |
|
169 | + } |
|
170 | 170 | |
171 | - /** |
|
172 | - * Offset to set. |
|
173 | - * |
|
174 | - * @link http://php.net/manual/en/arrayaccess.offsetset.php |
|
175 | - * |
|
176 | - * @param mixed $offset <p> |
|
177 | - * The offset to assign the value to. |
|
178 | - * </p> |
|
179 | - * @param mixed $value <p> |
|
180 | - * The value to set. |
|
181 | - * </p> |
|
182 | - * |
|
183 | - * @since 5.0.0 |
|
184 | - */ |
|
185 | - public function offsetSet($offset, $value) |
|
186 | - { |
|
187 | - throw new TDBMInvalidOperationException('You can set values in a TDBM result set, even in an alterable one. Use the add method instead.'); |
|
188 | - } |
|
171 | + /** |
|
172 | + * Offset to set. |
|
173 | + * |
|
174 | + * @link http://php.net/manual/en/arrayaccess.offsetset.php |
|
175 | + * |
|
176 | + * @param mixed $offset <p> |
|
177 | + * The offset to assign the value to. |
|
178 | + * </p> |
|
179 | + * @param mixed $value <p> |
|
180 | + * The value to set. |
|
181 | + * </p> |
|
182 | + * |
|
183 | + * @since 5.0.0 |
|
184 | + */ |
|
185 | + public function offsetSet($offset, $value) |
|
186 | + { |
|
187 | + throw new TDBMInvalidOperationException('You can set values in a TDBM result set, even in an alterable one. Use the add method instead.'); |
|
188 | + } |
|
189 | 189 | |
190 | - /** |
|
191 | - * Offset to unset. |
|
192 | - * |
|
193 | - * @link http://php.net/manual/en/arrayaccess.offsetunset.php |
|
194 | - * |
|
195 | - * @param mixed $offset <p> |
|
196 | - * The offset to unset. |
|
197 | - * </p> |
|
198 | - * |
|
199 | - * @since 5.0.0 |
|
200 | - */ |
|
201 | - public function offsetUnset($offset) |
|
202 | - { |
|
203 | - throw new TDBMInvalidOperationException('You can unset values in a TDBM result set, even in an alterable one. Use the delete method instead.'); |
|
204 | - } |
|
190 | + /** |
|
191 | + * Offset to unset. |
|
192 | + * |
|
193 | + * @link http://php.net/manual/en/arrayaccess.offsetunset.php |
|
194 | + * |
|
195 | + * @param mixed $offset <p> |
|
196 | + * The offset to unset. |
|
197 | + * </p> |
|
198 | + * |
|
199 | + * @since 5.0.0 |
|
200 | + */ |
|
201 | + public function offsetUnset($offset) |
|
202 | + { |
|
203 | + throw new TDBMInvalidOperationException('You can unset values in a TDBM result set, even in an alterable one. Use the delete method instead.'); |
|
204 | + } |
|
205 | 205 | |
206 | - /** |
|
207 | - * @param int $offset |
|
208 | - * |
|
209 | - * @return \Porpaginas\Page |
|
210 | - */ |
|
211 | - public function take($offset, $limit) |
|
212 | - { |
|
213 | - // TODO: replace this with a class implementing the map method. |
|
214 | - return new ArrayPage(array_slice($this->toArray(), $offset, $limit), $offset, $limit, count($this->toArray())); |
|
215 | - } |
|
206 | + /** |
|
207 | + * @param int $offset |
|
208 | + * |
|
209 | + * @return \Porpaginas\Page |
|
210 | + */ |
|
211 | + public function take($offset, $limit) |
|
212 | + { |
|
213 | + // TODO: replace this with a class implementing the map method. |
|
214 | + return new ArrayPage(array_slice($this->toArray(), $offset, $limit), $offset, $limit, count($this->toArray())); |
|
215 | + } |
|
216 | 216 | |
217 | - /** |
|
218 | - * Return the number of all results in the paginatable. |
|
219 | - * |
|
220 | - * @return int |
|
221 | - */ |
|
222 | - public function count() |
|
223 | - { |
|
224 | - return count($this->toArray()); |
|
225 | - } |
|
217 | + /** |
|
218 | + * Return the number of all results in the paginatable. |
|
219 | + * |
|
220 | + * @return int |
|
221 | + */ |
|
222 | + public function count() |
|
223 | + { |
|
224 | + return count($this->toArray()); |
|
225 | + } |
|
226 | 226 | |
227 | - /** |
|
228 | - * Return an iterator over all results of the paginatable. |
|
229 | - * |
|
230 | - * @return Iterator |
|
231 | - */ |
|
232 | - public function getIterator() |
|
233 | - { |
|
234 | - if ($this->alterations->count() === 0) { |
|
235 | - if ($this->resultIterator !== null) { |
|
236 | - return $this->resultIterator; |
|
237 | - } else { |
|
238 | - return new \ArrayIterator([]); |
|
239 | - } |
|
240 | - } else { |
|
241 | - return new \ArrayIterator($this->toArray()); |
|
242 | - } |
|
243 | - } |
|
227 | + /** |
|
228 | + * Return an iterator over all results of the paginatable. |
|
229 | + * |
|
230 | + * @return Iterator |
|
231 | + */ |
|
232 | + public function getIterator() |
|
233 | + { |
|
234 | + if ($this->alterations->count() === 0) { |
|
235 | + if ($this->resultIterator !== null) { |
|
236 | + return $this->resultIterator; |
|
237 | + } else { |
|
238 | + return new \ArrayIterator([]); |
|
239 | + } |
|
240 | + } else { |
|
241 | + return new \ArrayIterator($this->toArray()); |
|
242 | + } |
|
243 | + } |
|
244 | 244 | |
245 | - /** |
|
246 | - * Specify data which should be serialized to JSON. |
|
247 | - * |
|
248 | - * @link http://php.net/manual/en/jsonserializable.jsonserialize.php |
|
249 | - * |
|
250 | - * @return mixed data which can be serialized by <b>json_encode</b>, |
|
251 | - * which is a value of any type other than a resource |
|
252 | - * |
|
253 | - * @since 5.4.0 |
|
254 | - */ |
|
255 | - public function jsonSerialize() |
|
256 | - { |
|
257 | - return $this->toArray(); |
|
258 | - } |
|
245 | + /** |
|
246 | + * Specify data which should be serialized to JSON. |
|
247 | + * |
|
248 | + * @link http://php.net/manual/en/jsonserializable.jsonserialize.php |
|
249 | + * |
|
250 | + * @return mixed data which can be serialized by <b>json_encode</b>, |
|
251 | + * which is a value of any type other than a resource |
|
252 | + * |
|
253 | + * @since 5.4.0 |
|
254 | + */ |
|
255 | + public function jsonSerialize() |
|
256 | + { |
|
257 | + return $this->toArray(); |
|
258 | + } |
|
259 | 259 | |
260 | - /** |
|
261 | - * Returns only one value (the first) of the result set. |
|
262 | - * Returns null if no value exists. |
|
263 | - * |
|
264 | - * @return mixed|null |
|
265 | - */ |
|
266 | - public function first() |
|
267 | - { |
|
268 | - $page = $this->take(0, 1); |
|
269 | - foreach ($page as $bean) { |
|
270 | - return $bean; |
|
271 | - } |
|
260 | + /** |
|
261 | + * Returns only one value (the first) of the result set. |
|
262 | + * Returns null if no value exists. |
|
263 | + * |
|
264 | + * @return mixed|null |
|
265 | + */ |
|
266 | + public function first() |
|
267 | + { |
|
268 | + $page = $this->take(0, 1); |
|
269 | + foreach ($page as $bean) { |
|
270 | + return $bean; |
|
271 | + } |
|
272 | 272 | |
273 | - return; |
|
274 | - } |
|
273 | + return; |
|
274 | + } |
|
275 | 275 | |
276 | - /** |
|
277 | - * Returns a new iterator mapping any call using the $callable function. |
|
278 | - * |
|
279 | - * @param callable $callable |
|
280 | - * |
|
281 | - * @return MapIterator |
|
282 | - */ |
|
283 | - public function map(callable $callable) |
|
284 | - { |
|
285 | - return new MapIterator($this->getIterator(), $callable); |
|
286 | - } |
|
276 | + /** |
|
277 | + * Returns a new iterator mapping any call using the $callable function. |
|
278 | + * |
|
279 | + * @param callable $callable |
|
280 | + * |
|
281 | + * @return MapIterator |
|
282 | + */ |
|
283 | + public function map(callable $callable) |
|
284 | + { |
|
285 | + return new MapIterator($this->getIterator(), $callable); |
|
286 | + } |
|
287 | 287 | } |
@@ -7,23 +7,23 @@ |
||
7 | 7 | */ |
8 | 8 | class TDBMInheritanceException extends TDBMException |
9 | 9 | { |
10 | - public static function create(array $tables) : TDBMInheritanceException |
|
11 | - { |
|
12 | - return new self(sprintf('The tables (%s) cannot be linked by an inheritance relationship. Does your data set contains multiple children for one parent row? (multiple inheritance is not supported by TDBM)', implode(', ', $tables))); |
|
13 | - } |
|
10 | + public static function create(array $tables) : TDBMInheritanceException |
|
11 | + { |
|
12 | + return new self(sprintf('The tables (%s) cannot be linked by an inheritance relationship. Does your data set contains multiple children for one parent row? (multiple inheritance is not supported by TDBM)', implode(', ', $tables))); |
|
13 | + } |
|
14 | 14 | |
15 | - public static function extendException(TDBMInheritanceException $e, TDBMService $tdbmService, array $beanData) : TDBMInheritanceException |
|
16 | - { |
|
17 | - $pks = []; |
|
18 | - foreach ($beanData as $table => $row) { |
|
19 | - $primaryKeyColumns = $tdbmService->getPrimaryKeyColumns($table); |
|
20 | - foreach ($primaryKeyColumns as $columnName) { |
|
21 | - if ($row[$columnName] !== null) { |
|
22 | - $pks[] = $table.'.'.$columnName.' => '.var_export($row[$columnName], true); |
|
23 | - } |
|
24 | - } |
|
25 | - } |
|
15 | + public static function extendException(TDBMInheritanceException $e, TDBMService $tdbmService, array $beanData) : TDBMInheritanceException |
|
16 | + { |
|
17 | + $pks = []; |
|
18 | + foreach ($beanData as $table => $row) { |
|
19 | + $primaryKeyColumns = $tdbmService->getPrimaryKeyColumns($table); |
|
20 | + foreach ($primaryKeyColumns as $columnName) { |
|
21 | + if ($row[$columnName] !== null) { |
|
22 | + $pks[] = $table.'.'.$columnName.' => '.var_export($row[$columnName], true); |
|
23 | + } |
|
24 | + } |
|
25 | + } |
|
26 | 26 | |
27 | - throw new self($e->getMessage().' (row in error: '.implode(', ', $pks).')', 0, $e); |
|
28 | - } |
|
27 | + throw new self($e->getMessage().' (row in error: '.implode(', ', $pks).')', 0, $e); |
|
28 | + } |
|
29 | 29 | } |
@@ -4,13 +4,13 @@ |
||
4 | 4 | |
5 | 5 | class TDBMCyclicReferenceException extends TDBMException |
6 | 6 | { |
7 | - public static function createCyclicReference(string $tableName, AbstractTDBMObject $object) : TDBMCyclicReferenceException |
|
8 | - { |
|
9 | - return new self(sprintf("You are trying a grape of objects that reference each other. Unable to save object '%s' in table '%s'. It is already in the process of being saved.", get_class($object), $tableName)); |
|
10 | - } |
|
7 | + public static function createCyclicReference(string $tableName, AbstractTDBMObject $object) : TDBMCyclicReferenceException |
|
8 | + { |
|
9 | + return new self(sprintf("You are trying a grape of objects that reference each other. Unable to save object '%s' in table '%s'. It is already in the process of being saved.", get_class($object), $tableName)); |
|
10 | + } |
|
11 | 11 | |
12 | - public static function extendCyclicReference(TDBMCyclicReferenceException $e, string $tableName, AbstractTDBMObject $object, string $fkName) : TDBMCyclicReferenceException |
|
13 | - { |
|
14 | - return new self($e->getMessage().sprintf(" This object is referenced by an object of type '%s' (table '%s') via foreign key '%s'", get_class($object), $tableName, $fkName), $e->getCode(), $e); |
|
15 | - } |
|
12 | + public static function extendCyclicReference(TDBMCyclicReferenceException $e, string $tableName, AbstractTDBMObject $object, string $fkName) : TDBMCyclicReferenceException |
|
13 | + { |
|
14 | + return new self($e->getMessage().sprintf(" This object is referenced by an object of type '%s' (table '%s') via foreign key '%s'", get_class($object), $tableName, $fkName), $e->getCode(), $e); |
|
15 | + } |
|
16 | 16 | } |