Skip to content

Commit 35bd28d

Browse files
committed
Between: Refactor bypass logic
1 parent 3bfdfdd commit 35bd28d

File tree

1 file changed

+35
-42
lines changed

1 file changed

+35
-42
lines changed

src/SQLParser/Node/Between.php

Lines changed: 35 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@ public function toInstanceDescriptor(MoufManager $moufManager)
148148
}
149149

150150
/**
151-
* Renders the object as a SQL string.
151+
* Renders the object as an SQL string.
152152
*
153153
* @param array $parameters
154154
* @param AbstractPlatform $platform
@@ -160,52 +160,45 @@ public function toInstanceDescriptor(MoufManager $moufManager)
160160
*/
161161
public function toSql(array $parameters, AbstractPlatform $platform, int $indent = 0, $conditionsMode = self::CONDITION_APPLY, bool $extrapolateParameters = true): ?string
162162
{
163-
$minBypass = false;
164-
$maxBypass = false;
165-
166-
if ($conditionsMode == self::CONDITION_GUESS) {
167-
if ($this->minValueOperand instanceof Parameter) {
168-
if ($this->minValueOperand->isDiscardedOnNull() && !isset($parameters[$this->minValueOperand->getName()])) {
169-
$minBypass = true;
170-
}
171-
}
163+
switch ($conditionsMode) {
164+
case self::CONDITION_APPLY:
165+
$minBypass = $this->minValueCondition && !$this->minValueCondition->isOk($parameters);
166+
$maxBypass = $this->maxValueCondition && !$this->maxValueCondition->isOk($parameters);
167+
break;
168+
case self::CONDITION_GUESS:
169+
$minBypass = $this->minValueOperand instanceof Parameter && $this->minValueOperand->isDiscardedOnNull() && !isset($parameters[$this->minValueOperand->getName()]);
170+
$maxBypass = $this->maxValueOperand instanceof Parameter && $this->maxValueOperand->isDiscardedOnNull() && !isset($parameters[$this->maxValueOperand->getName()]);
171+
break;
172+
case self::CONDITION_IGNORE:
173+
$minBypass = false;
174+
$maxBypass = false;
175+
break;
176+
default:
177+
throw new \InvalidArgumentException('Invalid `$conditionsMode`: "' . $conditionsMode. '"');
178+
}
172179

173-
if ($this->maxValueOperand instanceof Parameter) {
174-
if ($this->maxValueOperand->isDiscardedOnNull() && !isset($parameters[$this->maxValueOperand->getName()])) {
175-
$maxBypass = true;
176-
}
177-
}
178-
} elseif ($conditionsMode == self::CONDITION_IGNORE) {
179-
$minBypass = false;
180-
$maxBypass = false;
181-
} else {
182-
if ($this->minValueCondition && !$this->minValueCondition->isOk($parameters)) {
183-
$minBypass = true;
184-
}
185-
if ($this->maxValueCondition && !$this->maxValueCondition->isOk($parameters)) {
186-
$maxBypass = true;
187-
}
180+
if ($maxBypass && $minBypass) {
181+
return null;
182+
}
183+
184+
if ($minBypass) {
185+
return sprintf('%s <= %s',
186+
NodeFactory::toSql($this->leftOperand, $platform, $parameters, ' ', false, $indent, $conditionsMode, $extrapolateParameters),
187+
NodeFactory::toSql($this->maxValueOperand, $platform, $parameters, ' ', false, $indent, $conditionsMode, $extrapolateParameters)
188+
);
188189
}
189190

190-
if (!$minBypass && !$maxBypass) {
191-
$sql = NodeFactory::toSql($this->leftOperand, $platform, $parameters, ' ', false, $indent, $conditionsMode, $extrapolateParameters);
192-
$sql .= ' BETWEEN ';
193-
$sql .= NodeFactory::toSql($this->minValueOperand, $platform, $parameters, ' ', false, $indent, $conditionsMode, $extrapolateParameters);
194-
$sql .= ' AND ';
195-
$sql .= NodeFactory::toSql($this->maxValueOperand, $platform, $parameters, ' ', false, $indent, $conditionsMode, $extrapolateParameters);
196-
} elseif (!$minBypass && $maxBypass) {
197-
$sql = NodeFactory::toSql($this->leftOperand, $platform, $parameters, ' ', false, $indent, $conditionsMode, $extrapolateParameters);
198-
$sql .= ' >= ';
199-
$sql .= NodeFactory::toSql($this->minValueOperand, $platform, $parameters, ' ', false, $indent, $conditionsMode, $extrapolateParameters);
200-
} elseif ($minBypass && !$maxBypass) {
201-
$sql = NodeFactory::toSql($this->leftOperand, $platform, $parameters, ' ', false, $indent, $conditionsMode, $extrapolateParameters);
202-
$sql .= ' <= ';
203-
$sql .= NodeFactory::toSql($this->maxValueOperand, $platform, $parameters, ' ', false, $indent, $conditionsMode, $extrapolateParameters);
204-
} else {
205-
$sql = null;
191+
if ($maxBypass) {
192+
return sprintf('%s >= %s',
193+
NodeFactory::toSql($this->leftOperand, $platform, $parameters, ' ', false, $indent, $conditionsMode, $extrapolateParameters),
194+
NodeFactory::toSql($this->minValueOperand, $platform, $parameters, ' ', false, $indent, $conditionsMode, $extrapolateParameters));
206195
}
207196

208-
return $sql;
197+
return sprintf('%s BETWEEN %s AND %s',
198+
NodeFactory::toSql($this->leftOperand, $platform, $parameters, ' ', false, $indent, $conditionsMode, $extrapolateParameters),
199+
NodeFactory::toSql($this->minValueOperand, $platform, $parameters, ' ', false, $indent, $conditionsMode, $extrapolateParameters),
200+
NodeFactory::toSql($this->maxValueOperand, $platform, $parameters, ' ', false, $indent, $conditionsMode, $extrapolateParameters)
201+
);
209202
}
210203

211204
/**

0 commit comments

Comments
 (0)