[+BUGFIX][~API] Extbase (Persistence): Cleaned up implementation of TYPO3-specific...
[Packages/TYPO3.CMS.git] / typo3 / sysext / extbase / Classes / Persistence / Storage / Typo3DbBackend.php
1 <?php
2 /***************************************************************
3 * Copyright notice
4 *
5 * (c) 2009 Jochen Rau <jochen.rau@typoplanet.de>
6 * All rights reserved
7 *
8 * This class is a backport of the corresponding class of FLOW3.
9 * All credits go to the v5 team.
10 *
11 * This script is part of the TYPO3 project. The TYPO3 project is
12 * free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2 of the License, or
15 * (at your option) any later version.
16 *
17 * The GNU General Public License can be found at
18 * http://www.gnu.org/copyleft/gpl.html.
19 *
20 * This script is distributed in the hope that it will be useful,
21 * but WITHOUT ANY WARRANTY; without even the implied warranty of
22 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 * GNU General Public License for more details.
24 *
25 * This copyright notice MUST APPEAR in all copies of the script!
26 ***************************************************************/
27
28 /**
29 * A Storage backend
30 *
31 * @package Extbase
32 * @subpackage Persistence
33 * @version $Id: $
34 */
35 class Tx_Extbase_Persistence_Storage_Typo3DbBackend implements Tx_Extbase_Persistence_Storage_BackendInterface, t3lib_Singleton {
36
37 const OPERATOR_EQUAL_TO_NULL = 'operatorEqualToNull';
38 const OPERATOR_NOT_EQUAL_TO_NULL = 'operatorNotEqualToNull';
39
40 /**
41 * The TYPO3 database object
42 *
43 * @var t3lib_db
44 */
45 protected $databaseHandle;
46
47 /**
48 * The TYPO3 page select object. Used for language and workspace overlay
49 *
50 * @var t3lib_pageSelect
51 */
52 protected $pageSelectObject;
53
54 /**
55 * TRUE if automatic cache clearing in TCEMAIN should be done on insert/update/delete, FALSE otherwise.
56 *
57 * @var boolean
58 */
59 protected $automaticCacheClearing = FALSE;
60
61 /**
62 * Constructs this Storage Backend instance
63 *
64 * @param t3lib_db $databaseHandle The database handle
65 */
66 public function __construct($databaseHandle) {
67 $this->databaseHandle = $databaseHandle;
68 }
69
70 /**
71 * Set the automatic cache clearing flag.
72 * If TRUE, then inserted/updated/deleted records trigger a TCEMAIN cache clearing.
73 *
74 * @param $automaticCacheClearing boolean if TRUE, enables automatic cache clearing
75 * @return void
76 * @internal
77 */
78 public function setAutomaticCacheClearing($automaticCacheClearing) {
79 $this->automaticCacheClearing = (boolean)$automaticCacheClearing;
80 }
81
82 /**
83 * Adds a row to the storage
84 *
85 * @param string $tableName The database table name
86 * @param array $row The row to be inserted
87 * @param boolean $isRelation TRUE if we are currently inserting into a relation table, FALSE by default
88 * @return int The uid of the inserted row
89 */
90 public function addRow($tableName, array $row, $isRelation = FALSE) {
91 $fields = array();
92 $values = array();
93 $parameters = array();
94 unset($row['uid']); // TODO Check if the offset exists
95 foreach ($row as $columnName => $value) {
96 $fields[] = $columnName;
97 $values[] = '?';
98 $parameters[] = $value;
99 }
100
101 $sqlString = 'INSERT INTO ' . $tableName . ' (' . implode(', ', $fields) . ') VALUES (' . implode(', ', $values) . ')';
102 $this->replacePlaceholders($sqlString, $parameters);
103 $this->databaseHandle->sql_query($sqlString);
104 $this->checkSqlErrors();
105 $uid = $this->databaseHandle->sql_insert_id();
106 if (!$isRelation) {
107 $this->clearPageCache($tableName, $uid);
108 }
109 return $uid;
110 }
111
112 /**
113 * Updates a row in the storage
114 *
115 * @param string $tableName The database table name
116 * @param array $row The row to be updated
117 * @param boolean $isRelation TRUE if we are currently inserting into a relation table, FALSE by default
118 * @return void
119 */
120 public function updateRow($tableName, array $row, $isRelation = FALSE) {
121 if (!isset($row['uid'])) throw new InvalidArgumentException('The given row must contain a value for "uid".');
122 $uid = (int)$row['uid'];
123 unset($row['uid']);
124 $fields = array();
125 $parameters = array();
126 foreach ($row as $columnName => $value) {
127 $fields[] = $columnName . '=?';
128 $parameters[] = $value;
129 }
130 $parameters[] = $uid;
131
132 $sqlString = 'UPDATE ' . $tableName . ' SET ' . implode(', ', $fields) . ' WHERE uid=?';
133 $this->replacePlaceholders($sqlString, $parameters);
134
135 $returnValue = $this->databaseHandle->sql_query($sqlString);
136 $this->checkSqlErrors();
137 if (!$isRelation) {
138 $this->clearPageCache($tableName, $uid);
139 }
140 return $returnValue;
141 }
142
143 /**
144 * Deletes a row in the storage
145 *
146 * @param string $tableName The database table name
147 * @param array $uid The uid of the row to be deleted
148 * @param boolean $isRelation TRUE if we are currently inserting into a relation table, FALSE by default
149 * @return void
150 */
151 public function removeRow($tableName, $uid, $isRelation = FALSE) {
152 $sqlString = 'DELETE FROM ' . $tableName . ' WHERE uid=?';
153 $this->replacePlaceholders($sqlString, array((int)$uid));
154 if (!$isRelation) {
155 $this->clearPageCache($tableName, $uid, $isRelation);
156 }
157 $returnValue = $this->databaseHandle->sql_query($sqlString);
158 $this->checkSqlErrors();
159 return $returnValue;
160 }
161
162 /**
163 * Returns an array with tuples matching the query.
164 *
165 * @param Tx_Extbase_Persistence_QOM_QueryObjectModelInterface $query
166 * @return array The matching tuples
167 */
168 public function getRows(Tx_Extbase_Persistence_QOM_QueryObjectModelInterface $query) {
169 $sql = array();
170 $sql['tables'] = array();
171 $sql['fields'] = array();
172 $sql['where'] = array();
173 $sql['enableFields'] = array();
174 $sql['orderings'] = array();
175 $parameters = array();
176 $tuples = array();
177
178 $this->parseSource($query, $sql, $parameters);
179 $sqlString = 'SELECT ' . implode(',', $sql['fields']) . ' FROM ' . implode(' ', $sql['tables']);
180
181 $this->parseConstraint($query->getConstraint(), $sql, $parameters, $query->getBoundVariableValues());
182
183 if (!empty($sql['where'])) {
184 $sqlString .= ' WHERE ' . implode('', $sql['where']);
185 if (!empty($sql['enableFields'])) {
186 $sqlString .= ' AND ' . implode(' AND ', $sql['enableFields']);
187 }
188 } elseif (!empty($sql['enableFields'])) {
189 $sqlString .= ' WHERE ' . implode(' AND ', $sql['enableFields']);
190 }
191
192 $this->parseOrderings($query->getOrderings(), $sql, $parameters, $query->getBoundVariableValues());
193 if (!empty($sql['orderings'])) {
194 $sqlString .= ' ORDER BY ' . implode(', ', $sql['orderings']);
195 }
196
197 $this->replacePlaceholders($sqlString, $parameters);
198
199 $result = $this->databaseHandle->sql_query($sqlString);
200 $this->checkSqlErrors();
201 if ($result) {
202 // TODO Check for selector name
203 $tuples = $this->getRowsFromResult($query->getSelectorName(), $result);
204 }
205
206 return $tuples;
207 }
208
209 /**
210 * Checks if a Value Object equal to the given Object exists in the data base
211 *
212 * @param array $properties The properties of the Value Object
213 * @param Tx_Extbase_Persistence_Mapper_DataMap $dataMap The Data Map
214 * @return array The matching tuples
215 */
216 public function hasValueObject(array $properties, Tx_Extbase_Persistence_Mapper_DataMap $dataMap) {
217 $fields = array();
218 $parameters = array();
219 foreach ($properties as $propertyName => $propertyValue) {
220 if ($dataMap->isPersistableProperty($propertyName) && ($propertyName !== 'uid')) {
221 $fields[] = $dataMap->getColumnMap($propertyName)->getColumnName() . '=?';
222 $parameters[] = $dataMap->convertPropertyValueToFieldValue($propertyValue);
223 }
224 }
225
226 $sqlString = 'SELECT * FROM ' . $dataMap->getTableName() . ' WHERE ' . implode('', $fields);
227 $this->replacePlaceholders($sqlString, $parameters);
228 $res = $this->databaseHandle->sql_query($sqlString);
229 $this->checkSqlErrors();
230 $row = $this->databaseHandle->sql_fetch_assoc($res);
231 if ($row !== FALSE) {
232 return $row['uid'];
233 } else {
234 return FALSE;
235 }
236 }
237
238 /**
239 * Transforms a Query Source into SQL and parameter arrays
240 *
241 * @param Tx_Extbase_Persistence_QOM_QueryObjectModel $query
242 * @param array &$sql
243 * @param array &$parameters
244 * @return void
245 */
246 protected function parseSource(Tx_Extbase_Persistence_QOM_QueryObjectModelInterface $query, array &$sql, array &$parameters) {
247 $source = $query->getSource();
248 if ($source instanceof Tx_Extbase_Persistence_QOM_SelectorInterface) {
249 $selectorName = $source->getSelectorName();
250 $sql['fields'][] = $selectorName . '.*';
251 $sql['tables'][] = $selectorName;
252 if ($query->getBackendSpecificQuerySettings()->enableFieldsEnabled()) {
253 $this->addEnableFieldsStatement($selectorName, $sql);
254 }
255 if ($query->getBackendSpecificQuerySettings()->storagePageEnabled()) {
256 $sql['enableFields'][] = $selectorName . '.pid=' . intval($query->getBackendSpecificQuerySettings()->getStoragePageId());
257 }
258 } elseif ($source instanceof Tx_Extbase_Persistence_QOM_JoinInterface) {
259 $this->parseJoin($source, $sql, $parameters);
260 }
261 }
262
263 /**
264 * Transforms a Join into SQL and parameter arrays
265 *
266 * @param Tx_Extbase_Persistence_QOM_JoinInterface $join
267 * @param array &$sql
268 * @param array &$parameters
269 * @return void
270 */
271 protected function parseJoin(Tx_Extbase_Persistence_QOM_JoinInterface $join, array &$sql, array &$parameters) {
272 $leftSelectorName = $join->getLeft()->getSelectorName();
273 $rightSelectorName = $join->getRight()->getSelectorName();
274
275 $sql['fields'][] = $leftSelectorName . '.*';
276 $sql['fields'][] = $rightSelectorName . '.*';
277
278 // TODO Implement support for different join types and nested joins
279 $sql['tables'][] = $leftSelectorName . ' LEFT JOIN ' . $rightSelectorName;
280
281 $joinCondition = $join->getJoinCondition();
282 // TODO Check the parsing of the join
283 if ($joinCondition instanceof Tx_Extbase_Persistence_QOM_EquiJoinCondition) {
284 $sql['tables'][] = 'ON ' . $joinCondition->getSelector1Name() . '.' . $joinCondition->getProperty1Name() . ' = ' . $joinCondition->getSelector2Name() . '.' . $joinCondition->getProperty2Name();
285 }
286 // TODO Implement childtableWhere
287
288 $this->addEnableFieldsStatement($leftSelectorName, $sql);
289 $this->addEnableFieldsStatement($rightSelectorName, $sql);
290 }
291
292 /**
293 * Transforms a constraint into SQL and parameter arrays
294 *
295 * @param Tx_Extbase_Persistence_QOM_ConstraintInterface $constraint
296 * @param array &$sql
297 * @param array &$parameters
298 * @param array $boundVariableValues
299 * @return void
300 */
301 protected function parseConstraint(Tx_Extbase_Persistence_QOM_ConstraintInterface $constraint = NULL, array &$sql, array &$parameters, array $boundVariableValues) {
302 if ($constraint instanceof Tx_Extbase_Persistence_QOM_AndInterface) {
303 $sql['where'][] = '(';
304 $this->parseConstraint($constraint->getConstraint1(), $sql, $parameters, $boundVariableValues);
305 $sql['where'][] = ' AND ';
306 $this->parseConstraint($constraint->getConstraint2(), $sql, $parameters, $boundVariableValues);
307 $sql['where'][] = ')';
308 } elseif ($constraint instanceof Tx_Extbase_Persistence_QOM_OrInterface) {
309 $sql['where'][] = '(';
310 $this->parseConstraint($constraint->getConstraint1(), $sql, $parameters, $boundVariableValues);
311 $sql['where'][] = ' OR ';
312 $this->parseConstraint($constraint->getConstraint2(), $sql, $parameters, $boundVariableValues);
313 $sql['where'][] = ')';
314 } elseif ($constraint instanceof Tx_Extbase_Persistence_QOM_NotInterface) {
315 $sql['where'][] = 'NOT (';
316 $this->parseConstraint($constraint->getConstraint(), $sql, $parameters, $boundVariableValues);
317 $sql['where'][] = ')';
318 } elseif ($constraint instanceof Tx_Extbase_Persistence_QOM_ComparisonInterface) {
319 $this->parseComparison($constraint, $sql, $parameters, $boundVariableValues);
320 } elseif ($constraint instanceof Tx_Extbase_Persistence_QOM_RelatedInterface) {
321 $this->parseRelated($constraint, $sql, $parameters, $boundVariableValues);
322 }
323 }
324
325 /**
326 * Parse a Comparison into SQL and parameter arrays.
327 *
328 * @param Tx_Extbase_Persistence_QOM_ComparisonInterface $comparison The comparison to parse
329 * @param array &$sql SQL query parts to add to
330 * @param array &$parameters Parameters to bind to the SQL
331 * @param array $boundVariableValues The bound variables in the query and their values
332 * @return void
333 */
334 protected function parseComparison(Tx_Extbase_Persistence_QOM_ComparisonInterface $comparison, array &$sql, array &$parameters, array $boundVariableValues) {
335 if (!($comparison->getOperand2() instanceof Tx_Extbase_Persistence_QOM_BindVariableValueInterface)) throw new Tx_Extbase_Persistence_Exception('Type of operand is not supported', 1247581135);
336
337 $value = $boundVariableValues[$comparison->getOperand2()->getBindVariableName()];
338 $operator = $comparison->getOperator();
339 if ($value === NULL) {
340 if ($operator === Tx_Extbase_Persistence_QOM_QueryObjectModelConstantsInterface::JCR_OPERATOR_EQUAL_TO) {
341 $operator = self::OPERATOR_EQUAL_TO_NULL;
342 } elseif ($operator === Tx_Extbase_Persistence_QOM_QueryObjectModelConstantsInterface::JCR_OPERATOR_NOT_EQUAL_TO) {
343 $operator = self::OPERATOR_NOT_EQUAL_TO_NULL;
344 } else {
345 // TODO Throw exception
346 }
347 }
348 $parameters[] = $value;
349
350 $this->parseDynamicOperand($comparison->getOperand1(), $operator, $sql, $parameters);
351 }
352
353 /**
354 * Parse a DynamicOperand into SQL and parameter arrays.
355 *
356 * @param Tx_Extbase_Persistence_QOM_DynamicOperandInterface $operand
357 * @param string $operator One of the JCR_OPERATOR_* constants
358 * @param array $boundVariableValues
359 * @param array &$parameters
360 * @param string $valueFunction an aoptional SQL function to apply to the operand value
361 * @return void
362 */
363 protected function parseDynamicOperand(Tx_Extbase_Persistence_QOM_DynamicOperandInterface $operand, $operator, array &$sql, array &$parameters, $valueFunction = NULL) {
364 if ($operand instanceof Tx_Extbase_Persistence_QOM_LowerCaseInterface) {
365 $this->parseDynamicOperand($operand->getOperand(), $operator, $sql, $parameters, 'LOWER');
366 } elseif ($operand instanceof Tx_Extbase_Persistence_QOM_UpperCaseInterface) {
367 $this->parseDynamicOperand($operand->getOperand(), $operator, $sql, $parameters, 'UPPER');
368 } elseif ($operand instanceof Tx_Extbase_Persistence_QOM_PropertyValueInterface) {
369 $selectorName = $operand->getSelectorName();
370 $operator = $this->resolveOperator($operator);
371
372 if ($valueFunction === NULL) {
373 $constraintSQL .= (!empty($selectorName) ? $selectorName . '.' : '') . $operand->getPropertyName() . ' ' . $operator . ' ?';
374 } else {
375 $constraintSQL .= $valueFunction . '(' . (!empty($selectorName) ? $selectorName . '.' : '') . $operand->getPropertyName() . ' ' . $operator . ' ?';
376 }
377
378 $sql['where'][] = $constraintSQL;
379 }
380 }
381
382 /**
383 * Returns the SQL operator for the given JCR operator type.
384 *
385 * @param string $operator One of the JCR_OPERATOR_* constants
386 * @return string an SQL operator
387 */
388 protected function resolveOperator($operator) {
389 switch ($operator) {
390 case self::OPERATOR_EQUAL_TO_NULL:
391 $operator = 'IS';
392 break;
393 case self::OPERATOR_NOT_EQUAL_TO_NULL:
394 $operator = 'IS NOT';
395 break;
396 case Tx_Extbase_Persistence_QOM_QueryObjectModelConstantsInterface::JCR_OPERATOR_EQUAL_TO:
397 $operator = '=';
398 break;
399 case Tx_Extbase_Persistence_QOM_QueryObjectModelConstantsInterface::JCR_OPERATOR_NOT_EQUAL_TO:
400 $operator = '!=';
401 break;
402 case Tx_Extbase_Persistence_QOM_QueryObjectModelConstantsInterface::JCR_OPERATOR_LESS_THAN:
403 $operator = '<';
404 break;
405 case Tx_Extbase_Persistence_QOM_QueryObjectModelConstantsInterface::JCR_OPERATOR_LESS_THAN_OR_EQUAL_TO:
406 $operator = '<=';
407 break;
408 case Tx_Extbase_Persistence_QOM_QueryObjectModelConstantsInterface::JCR_OPERATOR_GREATER_THAN:
409 $operator = '>';
410 break;
411 case Tx_Extbase_Persistence_QOM_QueryObjectModelConstantsInterface::JCR_OPERATOR_GREATER_THAN_OR_EQUAL_TO:
412 $operator = '>=';
413 break;
414 case Tx_Extbase_Persistence_QOM_QueryObjectModelConstantsInterface::JCR_OPERATOR_LIKE:
415 $operator = 'LIKE';
416 break;
417 default:
418 throw new Tx_Extbase_Persistence_Exception('Unsupported operator encountered.', 1242816073);
419 }
420
421 return $operator;
422 }
423
424 /**
425 * Replace query placeholders in a query part by the given
426 * parameters.
427 *
428 * @param string $queryPart The query part with placeholders
429 * @param array $parameters The parameters
430 * @return string The query part with replaced placeholders
431 */
432 protected function replacePlaceholders(&$sqlString, array $parameters) {
433 if (substr_count($sqlString, '?') !== count($parameters)) throw new Tx_Extbase_Persistence_Exception('The number of question marks to replace must be equal to the number of parameters.', 1242816074);
434 foreach ($parameters as $parameter) {
435 $markPosition = strpos($sqlString, '?');
436 if ($markPosition !== FALSE) {
437 // TODO This is a bit hacky; improve the handling of $parameter === NULL
438 if ($parameter === NULL) {
439 $parameter = 'NULL';
440 } else {
441 $parameter = '"' . $parameter . '"';
442 }
443 $sqlString = substr($sqlString, 0, $markPosition) . $parameter . substr($sqlString, $markPosition + 1);
444 }
445 }
446 }
447
448 /**
449 * Builds the enable fields statement
450 *
451 * @param string $selectorName The selector name (= database table name)
452 * @param array &$sql The query parts
453 * @return void
454 */
455 protected function addEnableFieldsStatement($selectorName, array &$sql) {
456 // TODO We have to call the appropriate API method if we are in TYPO3BE mode
457 if (is_array($GLOBALS['TCA'][$selectorName]['ctrl'])) {
458 $statement = substr($GLOBALS['TSFE']->sys_page->enableFields($selectorName), 4);
459 if(!empty($statement)) {
460 $sql['enableFields'][] = $statement;
461 }
462 }
463 }
464
465 /**
466 * Transforms orderings into SQL
467 *
468 * @param array $orderings
469 * @param array &$sql
470 * @param array &$parameters
471 * @param array $boundVariableValues
472 * @return void
473 */
474 protected function parseOrderings(array $orderings, array &$sql, array &$parameters, array $boundVariableValues) {
475 foreach ($orderings as $ordering) {
476 $operand = $ordering->getOperand();
477 $order = $ordering->getOrder();
478 if ($operand instanceof Tx_Extbase_Persistence_QOM_PropertyValue) {
479 switch ($order) {
480 case Tx_Extbase_Persistence_QOM_QueryObjectModelConstantsInterface::JCR_ORDER_ASCENDING:
481 $order = 'ASC';
482 break;
483 case Tx_Extbase_Persistence_QOM_QueryObjectModelConstantsInterface::JCR_ORDER_DESCENDING:
484 $order = 'DESC';
485 break;
486 default:
487 throw new Tx_Extbase_Persistence_Exception('Unsupported order encountered.', 1242816074);
488 }
489
490 $sql['orderings'][] = $ordering->getOperand()->getPropertyName() . ' ' . $order;
491 }
492 }
493 }
494
495 /**
496 * Transforms a Resource from a database query to an array of rows. Performs the language and
497 * workspace overlay before.
498 *
499 * @return array The result as an array of rows (tuples)
500 */
501 protected function getRowsFromResult($tableName, $res) {
502 $rows = array();
503 while ($row = $this->databaseHandle->sql_fetch_assoc($res)) {
504 $row = $this->doLanguageAndWorkspaceOverlay($tableName, $row);
505 if (is_array($row)) {
506 // TODO Check if this is necessary, maybe the last line is enough
507 $arrayKeys = range(0,count($row));
508 array_fill_keys($arrayKeys, $row);
509 $rows[] = $row;
510 }
511 }
512 return $rows;
513 }
514
515 /**
516 * Performs workspace and language overlay on the given row array. The language and workspace id is automatically
517 * detected (depending on FE or BE context). You can also explicitly set the language/workspace id.
518 *
519 * @param Tx_Extbase_Persistence_Mapper_DataMap $dataMap
520 * @param array $row The row array (as reference)
521 * @param string $languageUid The language id
522 * @param string $workspaceUidUid The workspace id
523 * @return void
524 */
525 protected function doLanguageAndWorkspaceOverlay($tableName, array $row, $languageUid = NULL, $workspaceUid = NULL) {
526 if (!($this->pageSelectObject instanceof t3lib_pageSelect)) {
527 if (TYPO3_MODE == 'FE') {
528 if (is_object($GLOBALS['TSFE'])) {
529 $this->pageSelectObject = $GLOBALS['TSFE']->sys_page;
530 if ($languageUid === NULL) {
531 $languageUid = $GLOBALS['TSFE']->sys_language_content;
532 }
533 } else {
534 require_once(PATH_t3lib . 'class.t3lib_page.php');
535 $this->pageSelectObject = t3lib_div::makeInstance('t3lib_pageSelect');
536 if ($languageUid === NULL) {
537 $languageUid = intval(t3lib_div::_GP('L'));
538 }
539 }
540 if ($workspaceUid !== NULL) {
541 $this->pageSelectObject->versioningWorkspaceId = $workspaceUid;
542 }
543 } else {
544 require_once(PATH_t3lib . 'class.t3lib_page.php');
545 $this->pageSelectObject = t3lib_div::makeInstance( 't3lib_pageSelect' );
546 //$this->pageSelectObject->versioningPreview = TRUE;
547 if ($workspaceUid === NULL) {
548 $workspaceUid = $GLOBALS['BE_USER']->workspace;
549 }
550 $this->pageSelectObject->versioningWorkspaceId = $workspaceUid;
551 }
552 }
553
554 $this->pageSelectObject->versionOL($tableName, $row, TRUE);
555 $row = $this->pageSelectObject->getRecordOverlay($tableName, $row, $languageUid, ''); //'hideNonTranslated'
556 // TODO Skip if empty languageoverlay (languagevisibility)
557 return $row;
558 }
559
560 /**
561 * Checks if there are SQL errors in the last query, and if yes, throw an exception.
562 *
563 * @return void
564 * @throws Tx_Extbase_Persistence_Storage_Exception_SqlError
565 */
566 protected function checkSqlErrors() {
567 $error = $this->databaseHandle->sql_error();
568 if ($error !== '') {
569 throw new Tx_Extbase_Persistence_Storage_Exception_SqlError($error, 1247602160);
570 }
571 }
572
573 /**
574 * Clear the TYPO3 page cache for the given record.
575 * Much of this functionality is taken from t3lib_tcemain::clear_cache() which unfortunately only works with logged-in BE user.
576 *
577 * @param $tableName Table name of the record
578 * @param $uid UID of the record
579 * @return void
580 */
581 protected function clearPageCache($tableName, $uid) {
582 if ($this->automaticCacheClearing !== TRUE) return;
583
584 $pageCache = $GLOBALS['typo3CacheManager']->getCache('cache_pages');
585 $pageSectionCache = $GLOBALS['typo3CacheManager']->getCache('cache_pagesection');
586
587 $result = $GLOBALS['TYPO3_DB']->exec_SELECTquery('pid', $tableName, 'uid='.intval($uid));
588
589 $pageIdsToClear = array();
590 if ($row = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($result)) {
591 $storagePage = $row['pid'];
592 $pageIdsToClear[] = $storagePage;
593 }
594 if (!$storagePage) {
595 return;
596 }
597
598 $pageTSConfig = t3lib_BEfunc::getPagesTSconfig($storagePage);
599
600 if ($pageTSConfig['clearCacheCmd']) {
601 $clearCacheCommands = t3lib_div::trimExplode(',',strtolower($TSConfig['clearCacheCmd']),1);
602 $clearCacheCommands = array_unique($clearCacheCommands);
603 foreach ($clearCacheCommands as $clearCacheCommand) {
604 if (t3lib_div::testInt($clearCacheCommand)) {
605 $pageIdsToClear[] = $clearCacheCommand;
606 }
607 }
608 }
609
610 foreach ($pageIdsToClear as $pageIdToClear) {
611 $pageCache->flushByTag('pageId_' . $pageIdToClear);
612 $pageSectionCache->flushByTag('pageId_' . $pageIdToClear);
613 }
614 }
615 }
616
617 ?>