예제 #1
0
 /**
  * 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);
     }
 }
예제 #2
0
 /**
  * Parse a Comparison into SQL and parameter arrays.
  *
  * @param Qom\ComparisonInterface $comparison The comparison to parse
  * @param Qom\SourceInterface $source The source
  * @param array &$sql SQL query parts to add to
  * @throws \RuntimeException
  * @throws \TYPO3\CMS\Extbase\Persistence\Generic\Exception\RepositoryException
  * @return void
  */
 protected function parseComparison(Qom\ComparisonInterface $comparison, Qom\SourceInterface $source, array &$sql)
 {
     $parameterIdentifier = $this->normalizeParameterIdentifier($comparison->getParameterIdentifier());
     $operator = $comparison->getOperator();
     $operand2 = $comparison->getOperand2();
     if ($operator === QueryInterface::OPERATOR_IN) {
         $hasValue = FALSE;
         foreach ($operand2 as $value) {
             $value = $this->getPlainValue($value);
             if ($value !== NULL) {
                 $parameters[] = $value;
                 $hasValue = TRUE;
             }
         }
         if ($hasValue === FALSE) {
             $sql['where'][] = '1<>1';
         } else {
             $this->parseDynamicOperand($comparison, $source, $sql);
         }
     } elseif ($operator === QueryInterface::OPERATOR_CONTAINS) {
         if ($operand2 === NULL) {
             $sql['where'][] = '1<>1';
         } else {
             if (!$source instanceof Qom\SelectorInterface) {
                 throw new \RuntimeException('Source is not of type "SelectorInterface"', 1395362539);
             }
             $className = $source->getNodeTypeName();
             $tableName = $this->dataMapper->convertClassNameToTableName($className);
             $operand1 = $comparison->getOperand1();
             $propertyName = $operand1->getPropertyName();
             while (strpos($propertyName, '.') !== FALSE) {
                 $this->addUnionStatement($className, $tableName, $propertyName, $sql);
             }
             $columnName = $this->dataMapper->convertPropertyNameToColumnName($propertyName, $className);
             $dataMap = $this->dataMapper->getDataMap($className);
             $columnMap = $dataMap->getColumnMap($propertyName);
             $typeOfRelation = $columnMap instanceof ColumnMap ? $columnMap->getTypeOfRelation() : NULL;
             if ($typeOfRelation === ColumnMap::RELATION_HAS_AND_BELONGS_TO_MANY) {
                 $relationTableName = $columnMap->getRelationTableName();
                 $relationTableMatchFields = $columnMap->getRelationTableMatchFields();
                 if (is_array($relationTableMatchFields)) {
                     $additionalWhere = array();
                     foreach ($relationTableMatchFields as $fieldName => $value) {
                         $additionalWhere[] = $fieldName . ' = ' . $this->databaseHandle->fullQuoteStr($value, $relationTableName);
                     }
                     $additionalWhereForMatchFields = ' AND ' . implode(' AND ', $additionalWhere);
                 } else {
                     $additionalWhereForMatchFields = '';
                 }
                 $sql['where'][] = $tableName . '.uid IN (SELECT ' . $columnMap->getParentKeyFieldName() . ' FROM ' . $relationTableName . ' WHERE ' . $columnMap->getChildKeyFieldName() . '=' . $parameterIdentifier . $additionalWhereForMatchFields . ')';
             } 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=' . $parameterIdentifier . ')';
                 } else {
                     $sql['where'][] = 'FIND_IN_SET(' . $parameterIdentifier . ', ' . $tableName . '.' . $columnName . ')';
                 }
             } else {
                 throw new \TYPO3\CMS\Extbase\Persistence\Generic\Exception\RepositoryException('Unsupported or non-existing property name "' . $propertyName . '" used in relation matching.', 1327065745);
             }
         }
     } else {
         $this->parseDynamicOperand($comparison, $source, $sql);
     }
 }
예제 #3
0
 /**
  * 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;
 }