104 $this->databaseHandle =
$GLOBALS[
'TYPO3_DB'];
113 $this->tableColumnCache = $this->cacheManager->getCache(
'extbase_typo3dbbackend_tablecolumns');
114 $this->queryCache = $this->cacheManager->getCache(
'extbase_typo3dbbackend_queries');
125 public function addRow($tableName, array $fieldValues, $isRelation = FALSE) {
126 if (isset($fieldValues[
'uid'])) {
127 unset($fieldValues[
'uid']);
130 $this->databaseHandle->exec_INSERTquery($tableName, $fieldValues);
132 $uid = $this->databaseHandle->sql_insert_id();
149 public function updateRow($tableName, array $fieldValues, $isRelation = FALSE) {
150 if (!isset($fieldValues[
'uid'])) {
151 throw new \InvalidArgumentException(
'The given row must contain a value for "uid".');
154 $uid = (int)$fieldValues[
'uid'];
155 unset($fieldValues[
'uid']);
157 $updateSuccessful = $this->databaseHandle->exec_UPDATEquery($tableName,
'uid = '.
$uid, $fieldValues);
164 return $updateSuccessful;
176 if (!isset($fieldValues[
'uid_local']) && !isset($fieldValues[
'uid_foreign'])) {
177 throw new \InvalidArgumentException(
178 'The given fieldValues must contain a value for "uid_local" and "uid_foreign".', 1360500126
182 $where[
'uid_local'] = (int)$fieldValues[
'uid_local'];
183 $where[
'uid_foreign'] = (int)$fieldValues[
'uid_foreign'];
184 unset($fieldValues[
'uid_local']);
185 unset($fieldValues[
'uid_foreign']);
187 if (!empty($fieldValues[
'tablenames'])) {
188 $where[
'tablenames'] = $fieldValues[
'tablenames'];
189 unset($fieldValues[
'tablenames']);
191 if (!empty($fieldValues[
'fieldname'])) {
192 $where[
'fieldname'] = $fieldValues[
'fieldname'];
193 unset($fieldValues[
'fieldname']);
196 $updateSuccessful = $this->databaseHandle->exec_UPDATEquery(
203 return $updateSuccessful;
214 public function removeRow($tableName, array $where, $isRelation = FALSE) {
215 $deleteSuccessful = $this->databaseHandle->exec_DELETEquery(
221 if (!$isRelation && isset($where[
'uid'])) {
225 return $deleteSuccessful;
237 $result = $this->databaseHandle->exec_SELECTgetSingleRow(
242 $columnName .
' DESC',
258 $row = $this->databaseHandle->exec_SELECTgetSingleRow(
265 return $row ?: FALSE;
277 $whereStatement = array();
279 foreach ($where as $fieldName => $fieldValue) {
280 $whereStatement[] = $fieldName .
' = ' . $this->databaseHandle->fullQuoteStr($fieldValue, $tableName);
283 return implode(
' AND ', $whereStatement);
313 'selectFields' => implode(
' ', $statementParts[
'keywords']) .
' ' . implode(
',', $statementParts[
'fields']),
314 'fromTable' => implode(
' ', $statementParts[
'tables']) .
' ' . implode(
' ', $statementParts[
'unions']),
315 'whereClause' => (!empty($statementParts[
'where']) ? implode(
'', $statementParts[
'where']) :
'1=1')
316 . (!empty($statementParts[
'additionalWhereClause'])
317 ?
' AND ' . implode(
' AND ', $statementParts[
'additionalWhereClause'])
320 'orderBy' => (!empty($statementParts[
'orderings']) ? implode(
', ', $statementParts[
'orderings']) :
''),
321 'limit' => ($statementParts[
'offset'] ? $statementParts[
'offset'] .
', ' :
'')
322 . ($statementParts[
'limit'] ? $statementParts[
'limit'] :
'')
352 $rows = $this->databaseHandle->exec_SELECTgetRows(
353 $queryCommandParameters[
'selectFields'],
354 $queryCommandParameters[
'fromTable'],
355 $queryCommandParameters[
'whereClause'],
357 $queryCommandParameters[
'orderBy'],
358 $queryCommandParameters[
'limit']
374 $preparedStatement = $this->databaseHandle->prepare_SELECTquery(
375 $queryCommandParameters[
'selectFields'],
376 $queryCommandParameters[
'fromTable'],
377 $queryCommandParameters[
'whereClause'],
379 $queryCommandParameters[
'orderBy'],
380 $queryCommandParameters[
'limit']
383 $preparedStatement->execute($parameters);
384 $rows = $preparedStatement->fetchAll();
386 $preparedStatement->free();
400 if ($realStatement instanceof \
TYPO3\CMS\Core\Database\PreparedStatement) {
402 $rows = $realStatement->fetchAll();
404 $realStatement->free();
414 $result = $this->databaseHandle->sql_query($realStatement);
418 while ($row = $this->databaseHandle->sql_fetch_assoc(
$result)) {
419 if (is_array($row)) {
423 $this->databaseHandle->sql_free_result(
$result);
438 throw new \TYPO3\CMS\Extbase\Persistence\Generic\Storage\Exception\BadConstraintException(
'Could not execute count on queries with a constraint of type TYPO3\\CMS\\Extbase\\Persistence\\Generic\\Qom\\Statement', 1256661045);
444 if (isset($statementParts[
'keywords'][
'distinct'])) {
445 $fields =
'DISTINCT ' . reset($statementParts[
'tables']) .
'.uid';
449 $count = $this->databaseHandle->exec_SELECTcountRows(
451 $queryCommandParameters[
'fromTable'],
452 $queryCommandParameters[
'whereClause']
456 if ($statementParts[
'offset']) {
457 $count -= $statementParts[
'offset'];
460 if ($statementParts[
'limit']) {
461 $count = min($count, $statementParts[
'limit']);
464 return (
int)max(0, $count);
485 list($queryHash,
$parameters) = $this->queryParser->preparseQuery($query);
487 if ($query->getQuerySettings()->getUseQueryCache()) {
489 if ($queryHash && !$statementParts) {
490 $statementParts = $this->queryParser->parseQuery($query);
494 $statementParts = $this->queryParser->parseQuery($query);
497 if (!$statementParts) {
498 throw new \RuntimeException(
'Your query could not be built.', 1394453197);
501 $this->queryParser->addDynamicQueryParts($query->getQuerySettings(), $statementParts);
504 $statementParts[
'limit'] = ((int)$query->getLimit() ?: NULL);
505 $statementParts[
'offset'] = ((int)$query->getOffset() ?: NULL);
507 if ($resolveParameterPlaceholders === TRUE) {
522 $tableNameForEscape = (reset($statementParts[
'tables']) ?:
'foo');
524 foreach ($parameters as $parameterPlaceholder => $parameter) {
526 $parameter = $parameter->_loadRealInstance();
529 if ($parameter instanceof \DateTime) {
530 $parameter = $parameter->format(
'U');
532 $parameter = (int)$parameter->getUid();
533 } elseif (is_array($parameter)) {
534 $subParameters = array();
535 foreach ($parameter as $subParameter) {
536 $subParameters[] = $this->databaseHandle->fullQuoteStr($subParameter, $tableNameForEscape);
538 $parameter = implode(
',', $subParameters);
539 } elseif ($parameter === NULL) {
541 } elseif (is_bool($parameter)) {
542 $parameter = (int)$parameter;
544 $parameter = $this->databaseHandle->fullQuoteStr((
string)$parameter, $tableNameForEscape);
547 $statementParts[
'where'] = str_replace($parameterPlaceholder, $parameter, $statementParts[
'where']);
550 return $statementParts;
563 $dataMap = $this->dataMapper->getDataMap(get_class($object));
564 $properties = $object->_getProperties();
565 foreach ($properties as $propertyName => $propertyValue) {
567 if ($dataMap->isPersistableProperty($propertyName) && $propertyName !==
'uid' && $propertyName !==
'pid' && $propertyName !==
'isClone') {
568 if ($propertyValue === NULL) {
569 $fields[] = $dataMap->getColumnMap($propertyName)->getColumnName() .
' IS NULL';
571 $fields[] = $dataMap->getColumnMap($propertyName)->getColumnName() .
'=?';
577 $sql[
'additionalWhereClause'] = array();
578 $tableName = $dataMap->getTableName();
580 $statement =
'SELECT * FROM ' . $tableName;
581 $statement .=
' WHERE ' . implode(
' AND ', $fields);
582 if (!empty(
$sql[
'additionalWhereClause'])) {
583 $statement .=
' AND ' . implode(
' AND ',
$sql[
'additionalWhereClause']);
587 $res = $this->databaseHandle->sql_query($statement);
589 $row = $this->databaseHandle->sql_fetch_assoc($res);
590 if ($row !== FALSE) {
591 return (
int)$row[
'uid'];
606 if (is_array($input)) {
607 throw new \TYPO3\CMS\Extbase\Persistence\Generic\Exception\UnexpectedTypeException(
'An array could not be converted to a plain value.', 1274799932);
609 if ($input instanceof \DateTime) {
610 return $input->format(
'U');
611 } elseif ($input instanceof \
TYPO3\CMS\Core\Type\TypeInterface) {
612 return (
string) $input;
613 } elseif (is_object($input)) {
615 $realInput = $input->_loadRealInstance();
620 return $realInput->getUid();
622 throw new \TYPO3\CMS\Extbase\Persistence\Generic\Exception\UnexpectedTypeException(
'An object of class "' . get_class($realInput) .
'" could not be converted to a plain value.', 1274799934);
624 } elseif (is_bool($input)) {
645 if (substr_count($sqlString,
'?') !== count($parameters)) {
646 throw new \TYPO3\CMS\Extbase\Persistence\Generic\Exception(
'The number of question marks to replace must be equal to the number of parameters.', 1242816074);
649 foreach ($parameters as $parameter) {
650 $markPosition = strpos($sqlString,
'?', $offset);
651 if ($markPosition !== FALSE) {
652 if ($parameter === NULL) {
654 } elseif (is_array($parameter) || $parameter instanceof \ArrayAccess || $parameter instanceof \Traversable) {
656 foreach ($parameter as $item) {
657 $items[] = $this->databaseHandle->fullQuoteStr($item, $tableName);
659 $parameter =
'(' . implode(
',', $items) .
')';
661 $parameter = $this->databaseHandle->fullQuoteStr($parameter, $tableName);
663 $sqlString = substr($sqlString, 0, $markPosition) . $parameter . substr($sqlString, ($markPosition + 1));
665 $offset = $markPosition + strlen($parameter);
680 if (is_array(
$GLOBALS[
'TCA'][$tableName][
'ctrl'])) {
681 $ignoreEnableFields = $querySettings->getIgnoreEnableFields();
682 $enableFieldsToBeIgnored = $querySettings->getEnableFieldsToBeIgnored();
683 $includeDeleted = $querySettings->getIncludeDeleted();
684 if ($this->environmentService->isEnvironmentInFrontendMode()) {
690 if (!empty($statement)) {
691 $statement = strtolower(substr($statement, 1, 3)) ===
'and' ? substr($statement, 5) : $statement;
692 $sql[
'additionalWhereClause'][] = $statement;
710 if ($ignoreEnableFields && !$includeDeleted) {
711 if (count($enableFieldsToBeIgnored)) {
713 $statement .= $this->
getPageRepository()->enableFields($tableName, -1, array_combine($enableFieldsToBeIgnored, $enableFieldsToBeIgnored));
717 } elseif (!$ignoreEnableFields && !$includeDeleted) {
719 } elseif (!$ignoreEnableFields && $includeDeleted) {
720 throw new \TYPO3\CMS\Extbase\Persistence\Generic\Exception\InconsistentQuerySettingsException(
'Query setting "ignoreEnableFields=FALSE" can not be used together with "includeDeleted=TRUE" in frontend context.', 1327678173);
736 if (!$ignoreEnableFields) {
739 if (!$includeDeleted) {
757 $tableName = $source->getSelectorName();
758 } elseif ($source instanceof \
TYPO3\CMS\Extbase\Persistence\Generic\Qom\
JoinInterface) {
759 $tableName = $source->getRight()->getSelectorName();
768 if ($workspaceUid !== NULL) {
772 if ($workspaceUid === NULL) {
773 $workspaceUid =
$GLOBALS[
'BE_USER']->workspace;
783 && !empty(
$GLOBALS[
'TCA'][$tableName][
'ctrl'][
'versioningWS'])
784 && (
int)
$GLOBALS[
'TCA'][$tableName][
'ctrl'][
'versioningWS'] >= 2
785 && count($rows) === 1
787 $movePlaceholder = $this->databaseHandle->exec_SELECTgetSingleRow(
790 't3ver_state=3 AND t3ver_wsid=' .
$pageRepository->versioningWorkspaceId
791 .
' AND t3ver_move_id=' . $rows[0][
'uid']
793 if (!empty($movePlaceholder)) {
794 $rows = array($movePlaceholder);
798 $overlaidRows = array();
799 foreach ($rows as $row) {
801 if (isset($tableName) && isset(
$GLOBALS[
'TCA'][$tableName])
802 && isset(
$GLOBALS[
'TCA'][$tableName][
'ctrl'][
'languageField'])
803 && isset(
$GLOBALS[
'TCA'][$tableName][
'ctrl'][
'transOrigPointerField'])
804 && !isset(
$GLOBALS[
'TCA'][$tableName][
'ctrl'][
'transOrigPointerTable'])
806 if (isset($row[
$GLOBALS[
'TCA'][$tableName][
'ctrl'][
'transOrigPointerField']])
807 && $row[
$GLOBALS[
'TCA'][$tableName][
'ctrl'][
'transOrigPointerField']] > 0
809 $row = $this->databaseHandle->exec_SELECTgetSingleRow(
812 $tableName .
'.uid=' . (
int)$row[
$GLOBALS[
'TCA'][$tableName][
'ctrl'][
'transOrigPointerField']] .
813 ' AND ' . $tableName .
'.' .
$GLOBALS[
'TCA'][$tableName][
'ctrl'][
'languageField'] .
'=0' 818 if ($tableName ==
'pages') {
819 $row =
$pageRepository->getPageOverlay($row, $querySettings->getLanguageUid());
820 } elseif (isset(
$GLOBALS[
'TCA'][$tableName][
'ctrl'][
'languageField'])
821 &&
$GLOBALS[
'TCA'][$tableName][
'ctrl'][
'languageField'] !==
'' 822 && !isset(
$GLOBALS[
'TCA'][$tableName][
'ctrl'][
'transOrigPointerTable'])
824 if (in_array($row[
$GLOBALS[
'TCA'][$tableName][
'ctrl'][
'languageField']], array(-1, 0))) {
825 $overlayMode = $querySettings->getLanguageMode() ===
'strict' ?
'hideNonTranslated' :
'';
826 $row =
$pageRepository->getRecordOverlay($tableName, $row, $querySettings->getLanguageUid(), $overlayMode);
829 if ($row !== NULL && is_array($row)) {
830 $overlaidRows[] = $row;
833 return $overlaidRows;
840 if (!$this->pageRepository instanceof \
TYPO3\CMS\Frontend\Page\PageRepository) {
841 if ($this->environmentService->isEnvironmentInFrontendMode() && is_object(
$GLOBALS[
'TSFE'])) {
842 $this->pageRepository =
$GLOBALS[
'TSFE']->sys_page;
859 $error = $this->databaseHandle->sql_error();
862 throw new \TYPO3\CMS\Extbase\Persistence\Generic\Storage\Exception\SqlErrorException($error, 1247602160);
878 $frameworkConfiguration = $this->configurationManager->getConfiguration(\
TYPO3\CMS\Extbase\Configuration\ConfigurationManagerInterface::CONFIGURATION_TYPE_FRAMEWORK);
879 if (isset($frameworkConfiguration[
'persistence'][
'enableAutomaticCacheClearing']) && $frameworkConfiguration[
'persistence'][
'enableAutomaticCacheClearing'] ===
'1') {
884 $pageIdsToClear = array();
886 $columns = $this->databaseHandle->admin_get_fields($tableName);
887 if (array_key_exists(
'pid', $columns)) {
888 $result = $this->databaseHandle->exec_SELECTquery(
'pid', $tableName,
'uid=' . (
int)
$uid);
889 if ($row = $this->databaseHandle->sql_fetch_assoc(
$result)) {
890 $storagePage = $row[
'pid'];
891 $pageIdsToClear[] = $storagePage;
893 } elseif (isset(
$GLOBALS[
'TSFE'])) {
895 $storagePage =
$GLOBALS[
'TSFE']->id;
896 $pageIdsToClear[] = $storagePage;
898 if ($storagePage === NULL) {
901 if (!isset($this->pageTSConfigCache[$storagePage])) {
904 if (isset($this->pageTSConfigCache[$storagePage][
'TCEMAIN.'][
'clearCacheCmd'])) {
906 $clearCacheCommands = array_unique($clearCacheCommands);
907 foreach ($clearCacheCommands as $clearCacheCommand) {
908 if (\
TYPO3\CMS\Core\Utility\MathUtility::canBeInterpretedAsInteger($clearCacheCommand)) {
909 $pageIdsToClear[] = $clearCacheCommand;
914 foreach ($pageIdsToClear as $pageIdToClear) {
915 $this->cacheService->getPageIdStack()->push($pageIdToClear);
926 if (!isset($this->queryRuntimeCache[$entryIdentifier])) {
927 $this->queryRuntimeCache[$entryIdentifier] = $this->queryCache->get($entryIdentifier);
929 return $this->queryRuntimeCache[$entryIdentifier];
940 $this->queryRuntimeCache[$entryIdentifier] = $variable;
941 $this->queryCache->set($entryIdentifier, $variable, array(), 0);
addRow($tableName, array $fieldValues, $isRelation=FALSE)
getRowsFromDatabase(array $statementParts)
clearPageCache($tableName, $uid)
resolveParameterPlaceholders(array $statementParts, array $parameters)
getRowByIdentifier($tableName, array $where)
getStatementParts($query, $resolveParameterPlaceholders=TRUE)
getUidOfAlreadyPersistedValueObject(\TYPO3\CMS\Extbase\DomainObject\AbstractValueObject $object)
replacePlaceholders(&$sqlString, array $parameters, $tableName='foo')
getBackendConstraintStatement($tableName, $ignoreEnableFields, $includeDeleted)
static makeInstance($className)
static trimExplode($delim, $string, $removeEmptyValues=FALSE, $limit=0)
getObjectDataByRawQuery(Statement $statement)
doLanguageAndWorkspaceOverlay(\TYPO3\CMS\Extbase\Persistence\Generic\Qom\SourceInterface $source, array $rows, \TYPO3\CMS\Extbase\Persistence\Generic\QuerySettingsInterface $querySettings, $workspaceUid=NULL)
createQueryCommandParametersFromStatementParts(array $statementParts)
if($list_of_literals) if(!empty($literals)) if(!empty($literals)) $result
Analyse literals to prepend the N char to them if their contents aren't numeric.
getObjectDataByQuery(QueryInterface $query)
getObjectCountByQuery(QueryInterface $query)
addVisibilityConstraintStatement(\TYPO3\CMS\Extbase\Persistence\Generic\QuerySettingsInterface $querySettings, $tableName, array &$sql)
getFrontendConstraintStatement($tableName, $ignoreEnableFields, array $enableFieldsToBeIgnored=array(), $includeDeleted)
getQueryCacheEntry($entryIdentifier)
removeRow($tableName, array $where, $isRelation=FALSE)
if(!defined('TYPO3_MODE')) $GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['t3lib/class.t3lib_userauth.php']['logoff_pre_processing'][]
updateRelationTableRow($tableName, array $fieldValues)
static getPagesTSconfig($id, $rootLine=NULL, $returnPartArray=FALSE)
getRowsByStatementParts(QueryInterface $query)
resolveWhereStatement(array $where, $tableName='foo')
getMaxValueFromTable($tableName, array $where, $columnName)
static BEenableFields($table, $inv=0)
static deleteClause($table, $tableAlias='')
updateRow($tableName, array $fieldValues, $isRelation=FALSE)
getRowsFromPreparedDatabase(array $statementParts, array $parameters)
setQueryCacheEntry($entryIdentifier, $variable)