Skip to content

Commit

Permalink
Between: Refactor bypass logic
Browse files Browse the repository at this point in the history
  • Loading branch information
homersimpsons committed Sep 23, 2021
1 parent 3bfdfdd commit 35bd28d
Showing 1 changed file with 35 additions and 42 deletions.
77 changes: 35 additions & 42 deletions src/SQLParser/Node/Between.php
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ public function toInstanceDescriptor(MoufManager $moufManager)
}

/**
* Renders the object as a SQL string.
* Renders the object as an SQL string.
*
* @param array $parameters
* @param AbstractPlatform $platform
Expand All @@ -160,52 +160,45 @@ public function toInstanceDescriptor(MoufManager $moufManager)
*/
public function toSql(array $parameters, AbstractPlatform $platform, int $indent = 0, $conditionsMode = self::CONDITION_APPLY, bool $extrapolateParameters = true): ?string
{
$minBypass = false;
$maxBypass = false;

if ($conditionsMode == self::CONDITION_GUESS) {
if ($this->minValueOperand instanceof Parameter) {
if ($this->minValueOperand->isDiscardedOnNull() && !isset($parameters[$this->minValueOperand->getName()])) {
$minBypass = true;
}
}
switch ($conditionsMode) {
case self::CONDITION_APPLY:
$minBypass = $this->minValueCondition && !$this->minValueCondition->isOk($parameters);
$maxBypass = $this->maxValueCondition && !$this->maxValueCondition->isOk($parameters);
break;
case self::CONDITION_GUESS:
$minBypass = $this->minValueOperand instanceof Parameter && $this->minValueOperand->isDiscardedOnNull() && !isset($parameters[$this->minValueOperand->getName()]);
$maxBypass = $this->maxValueOperand instanceof Parameter && $this->maxValueOperand->isDiscardedOnNull() && !isset($parameters[$this->maxValueOperand->getName()]);
break;
case self::CONDITION_IGNORE:
$minBypass = false;
$maxBypass = false;
break;
default:
throw new \InvalidArgumentException('Invalid `$conditionsMode`: "' . $conditionsMode. '"');
}

if ($this->maxValueOperand instanceof Parameter) {
if ($this->maxValueOperand->isDiscardedOnNull() && !isset($parameters[$this->maxValueOperand->getName()])) {
$maxBypass = true;
}
}
} elseif ($conditionsMode == self::CONDITION_IGNORE) {
$minBypass = false;
$maxBypass = false;
} else {
if ($this->minValueCondition && !$this->minValueCondition->isOk($parameters)) {
$minBypass = true;
}
if ($this->maxValueCondition && !$this->maxValueCondition->isOk($parameters)) {
$maxBypass = true;
}
if ($maxBypass && $minBypass) {
return null;
}

if ($minBypass) {
return sprintf('%s <= %s',
NodeFactory::toSql($this->leftOperand, $platform, $parameters, ' ', false, $indent, $conditionsMode, $extrapolateParameters),
NodeFactory::toSql($this->maxValueOperand, $platform, $parameters, ' ', false, $indent, $conditionsMode, $extrapolateParameters)
);
}

if (!$minBypass && !$maxBypass) {
$sql = NodeFactory::toSql($this->leftOperand, $platform, $parameters, ' ', false, $indent, $conditionsMode, $extrapolateParameters);
$sql .= ' BETWEEN ';
$sql .= NodeFactory::toSql($this->minValueOperand, $platform, $parameters, ' ', false, $indent, $conditionsMode, $extrapolateParameters);
$sql .= ' AND ';
$sql .= NodeFactory::toSql($this->maxValueOperand, $platform, $parameters, ' ', false, $indent, $conditionsMode, $extrapolateParameters);
} elseif (!$minBypass && $maxBypass) {
$sql = NodeFactory::toSql($this->leftOperand, $platform, $parameters, ' ', false, $indent, $conditionsMode, $extrapolateParameters);
$sql .= ' >= ';
$sql .= NodeFactory::toSql($this->minValueOperand, $platform, $parameters, ' ', false, $indent, $conditionsMode, $extrapolateParameters);
} elseif ($minBypass && !$maxBypass) {
$sql = NodeFactory::toSql($this->leftOperand, $platform, $parameters, ' ', false, $indent, $conditionsMode, $extrapolateParameters);
$sql .= ' <= ';
$sql .= NodeFactory::toSql($this->maxValueOperand, $platform, $parameters, ' ', false, $indent, $conditionsMode, $extrapolateParameters);
} else {
$sql = null;
if ($maxBypass) {
return sprintf('%s >= %s',
NodeFactory::toSql($this->leftOperand, $platform, $parameters, ' ', false, $indent, $conditionsMode, $extrapolateParameters),
NodeFactory::toSql($this->minValueOperand, $platform, $parameters, ' ', false, $indent, $conditionsMode, $extrapolateParameters));
}

return $sql;
return sprintf('%s BETWEEN %s AND %s',
NodeFactory::toSql($this->leftOperand, $platform, $parameters, ' ', false, $indent, $conditionsMode, $extrapolateParameters),
NodeFactory::toSql($this->minValueOperand, $platform, $parameters, ' ', false, $indent, $conditionsMode, $extrapolateParameters),
NodeFactory::toSql($this->maxValueOperand, $platform, $parameters, ' ', false, $indent, $conditionsMode, $extrapolateParameters)
);
}

/**
Expand Down

0 comments on commit 35bd28d

Please sign in to comment.