/** * Parse a Comparison into SQL and parameter arrays. * * @param ComparisonInterface $comparison The comparison to parse * @param SourceInterface $source The source * @param array &$sql SQL query parts to add to * @param array &$parameters Parameters to bind to the SQL * @throws Exception\RepositoryException * @return void */ protected function parseComparison(ComparisonInterface $comparison, SourceInterface $source, array &$sql, array &$parameters) { $operand1 = $comparison->getOperand1(); $operator = $comparison->getOperator(); $operand2 = $comparison->getOperand2(); if ($operator === QueryInterface::OPERATOR_IN) { $items = array(); $hasValue = FALSE; foreach ($operand2 as $value) { $value = $this->getPlainValue($value); if ($value !== NULL) { $items[] = $value; $hasValue = TRUE; } } if ($hasValue === FALSE) { $sql['where'][] = '1<>1'; } else { $this->parseDynamicOperand($operand1, $operator, $source, $sql, $parameters, NULL); $parameters[] = $items; } } elseif ($operator === QueryInterface::OPERATOR_CONTAINS) { if ($operand2 === NULL) { $sql['where'][] = '1<>1'; } else { throw new \Exception('Not implemented! Contact extension author.', 1412931227); # @todo re-implement me if necessary. #$tableName = $this->query->getType(); #$propertyName = $operand1->getPropertyName(); #while (strpos($propertyName, '.') !== FALSE) { # $this->addUnionStatement($tableName, $propertyName, $sql); #} #$columnName = $propertyName; #$columnMap = $propertyName; #$typeOfRelation = $columnMap instanceof ColumnMap ? $columnMap->getTypeOfRelation() : NULL; #if ($typeOfRelation === ColumnMap::RELATION_HAS_AND_BELONGS_TO_MANY) { # $relationTableName = $columnMap->getRelationTableName(); # $sql['where'][] = $tableName . '.uid IN (SELECT ' . $columnMap->getParentKeyFieldName() . ' FROM ' . $relationTableName . ' WHERE ' . $columnMap->getChildKeyFieldName() . '=?)'; # $parameters[] = intval($this->getPlainValue($operand2)); #} elseif ($typeOfRelation === ColumnMap::RELATION_HAS_MANY) { # $parentKeyFieldName = $columnMap->getParentKeyFieldName(); # if (isset($parentKeyFieldName)) { # $childTableName = $columnMap->getChildTableName(); # $sql['where'][] = $tableName . '.uid=(SELECT ' . $childTableName . '.' . $parentKeyFieldName . ' FROM ' . $childTableName . ' WHERE ' . $childTableName . '.uid=?)'; # $parameters[] = intval($this->getPlainValue($operand2)); # } else { # $sql['where'][] = 'FIND_IN_SET(?,' . $tableName . '.' . $columnName . ')'; # $parameters[] = intval($this->getPlainValue($operand2)); # } #} else { # throw new Exception\RepositoryException('Unsupported or non-existing property name "' . $propertyName . '" used in relation matching.', 1327065745); #} } } else { if ($operand2 === NULL) { if ($operator === QueryInterface::OPERATOR_EQUAL_TO) { $operator = self::OPERATOR_EQUAL_TO_NULL; } elseif ($operator === QueryInterface::OPERATOR_NOT_EQUAL_TO) { $operator = self::OPERATOR_NOT_EQUAL_TO_NULL; } } $this->parseDynamicOperand($operand1, $operator, $source, $sql, $parameters); $parameters[] = $this->getPlainValue($operand2); } }
/** * Parse a DynamicOperand into SQL and parameter arrays. * * @param Qom\ComparisonInterface $comparison * @param Qom\SourceInterface $source The source * @param array &$sql The query parts * @return void */ protected function parseDynamicOperand(Qom\ComparisonInterface $comparison, Qom\SourceInterface $source, array &$sql) { $operator = $this->resolveOperator($comparison->getOperator()); $operand = $comparison->getOperand1(); $constraintSQL = $this->parseOperand($operand, $source, $sql) . ' ' . $operator . ' '; $parameterIdentifier = $this->normalizeParameterIdentifier($comparison->getParameterIdentifier()); if ($operator === 'IN') { $parameterIdentifier = '(' . $parameterIdentifier . ')'; } $constraintSQL .= $parameterIdentifier; $sql['where'][] = $constraintSQL; }
/** * Parse a DynamicOperand into SQL and parameter arrays. * * @param Qom\ComparisonInterface $comparison * @param Qom\SourceInterface $source The source * @return string * @throws \TYPO3\CMS\Extbase\Persistence\Generic\Exception */ protected function parseDynamicOperand(Qom\ComparisonInterface $comparison, Qom\SourceInterface $source) { $value = $comparison->getOperand2(); $fieldName = $this->parseOperand($comparison->getOperand1(), $source); $expr = null; $exprBuilder = $this->queryBuilder->expr(); switch ($comparison->getOperator()) { case QueryInterface::OPERATOR_IN: $hasValue = false; $plainValues = []; foreach ($value as $singleValue) { $plainValue = $this->dataMapper->getPlainValue($singleValue); if ($plainValue !== null) { $hasValue = true; $plainValues[] = $plainValue; } } if ($hasValue) { $expr = $exprBuilder->comparison($fieldName, 'IN', '(' . implode(', ', $plainValues) . ')'); } else { $expr = '1<>1'; } break; case QueryInterface::OPERATOR_EQUAL_TO: if ($value === null) { $expr = $fieldName . ' IS NULL'; } else { $value = $this->queryBuilder->createNamedParameter($this->dataMapper->getPlainValue($value)); $expr = $exprBuilder->comparison($fieldName, $exprBuilder::EQ, $value); } break; case QueryInterface::OPERATOR_EQUAL_TO_NULL: $expr = $fieldName . ' IS NULL'; break; case QueryInterface::OPERATOR_NOT_EQUAL_TO: if ($value === null) { $expr = $fieldName . ' IS NOT NULL'; } else { $value = $this->queryBuilder->createNamedParameter($this->dataMapper->getPlainValue($value)); $expr = $exprBuilder->comparison($fieldName, $exprBuilder::NEQ, $value); } break; case QueryInterface::OPERATOR_NOT_EQUAL_TO_NULL: $expr = $fieldName . ' IS NOT NULL'; break; case QueryInterface::OPERATOR_LESS_THAN: $value = $this->queryBuilder->createNamedParameter($this->dataMapper->getPlainValue($value), \PDO::PARAM_INT); $expr = $exprBuilder->comparison($fieldName, $exprBuilder::LT, $value); break; case QueryInterface::OPERATOR_LESS_THAN_OR_EQUAL_TO: $value = $this->queryBuilder->createNamedParameter($this->dataMapper->getPlainValue($value), \PDO::PARAM_INT); $expr = $exprBuilder->comparison($fieldName, $exprBuilder::LTE, $value); break; case QueryInterface::OPERATOR_GREATER_THAN: $value = $this->queryBuilder->createNamedParameter($this->dataMapper->getPlainValue($value), \PDO::PARAM_INT); $expr = $exprBuilder->comparison($fieldName, $exprBuilder::GT, $value); break; case QueryInterface::OPERATOR_GREATER_THAN_OR_EQUAL_TO: $value = $this->queryBuilder->createNamedParameter($this->dataMapper->getPlainValue($value), \PDO::PARAM_INT); $expr = $exprBuilder->comparison($fieldName, $exprBuilder::GTE, $value); break; case QueryInterface::OPERATOR_LIKE: $value = $this->queryBuilder->createNamedParameter($this->dataMapper->getPlainValue($value)); $expr = $exprBuilder->comparison($fieldName, 'LIKE', $value); break; default: throw new \TYPO3\CMS\Extbase\Persistence\Generic\Exception('Unsupported operator encountered.', 1242816073); } return $expr; }