Skip to content

Commit

Permalink
Merge pull request #9 from formal-php/fix-where-lmThanOrEqual
Browse files Browse the repository at this point in the history
Fix applying a moreThanOrEqual or lessThanOrEqual when the columns are different
  • Loading branch information
Baptouuuu authored Jul 14, 2024
2 parents 8264837 + 28c575c commit aec3390
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 0 deletions.
72 changes: 72 additions & 0 deletions proofs/query/where.php
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,42 @@ static function($assert, $column, $value) {
},
);

yield proof(
'Where less than or equal comparator on different columns',
given(
Column::any(),
Column::any(),
Set\Strings::any(),
),
static function($assert, $column1, $column2, $value) {
$lessThan = Property::of(
$column1->name()->toString(),
Sign::lessThan,
$value,
);
$equal = Property::of(
$column2->name()->toString(),
Sign::equality,
$value,
);
$where = Where::of($lessThan->or($equal));

$assert->same(
"WHERE ({$column1->name()->sql(Driver::mysql)} < ? OR {$column2->name()->sql(Driver::mysql)} = ?)",
$where->sql(Driver::mysql),
);
$assert->count(2, $where->parameters());
$assert->same($value, $where->parameters()->first()->match(
static fn($parameter) => $parameter->value(),
static fn() => null,
));
$assert->same($value, $where->parameters()->last()->match(
static fn($parameter) => $parameter->value(),
static fn() => null,
));
},
);

yield proof(
'Where more than comparator',
given(
Expand Down Expand Up @@ -169,6 +205,42 @@ static function($assert, $column, $value) {
},
);

yield proof(
'Where more than or equal comparator on different columns',
given(
Column::any(),
Column::any(),
Set\Strings::any(),
),
static function($assert, $column1, $column2, $value) {
$moreThan = Property::of(
$column1->name()->toString(),
Sign::moreThan,
$value,
);
$equal = Property::of(
$column2->name()->toString(),
Sign::equality,
$value,
);
$where = Where::of($moreThan->or($equal));

$assert->same(
"WHERE ({$column1->name()->sql(Driver::mysql)} > ? OR {$column2->name()->sql(Driver::mysql)} = ?)",
$where->sql(Driver::mysql),
);
$assert->count(2, $where->parameters());
$assert->same($value, $where->parameters()->first()->match(
static fn($parameter) => $parameter->value(),
static fn() => null,
));
$assert->same($value, $where->parameters()->last()->match(
static fn($parameter) => $parameter->value(),
static fn() => null,
));
},
);

yield proof(
'Where is null comparator',
given(Column::any()),
Expand Down
4 changes: 4 additions & 0 deletions src/Query/Where.php
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,7 @@ private function buildComposite(
$specification->left()->sign() === Sign::moreThan &&
$specification->right() instanceof Comparator &&
$specification->right()->sign() === Sign::equality &&
$specification->left()->property() === $specification->right()->property() &&
$specification->left()->value() === $specification->right()->value()
) {
return \sprintf(
Expand All @@ -159,6 +160,7 @@ private function buildComposite(
$specification->left()->sign() === Sign::lessThan &&
$specification->right() instanceof Comparator &&
$specification->right()->sign() === Sign::equality &&
$specification->left()->property() === $specification->right()->property() &&
$specification->left()->value() === $specification->right()->value()
) {
return \sprintf(
Expand Down Expand Up @@ -248,6 +250,7 @@ private function findParamaters(
$specification->left()->sign() === Sign::moreThan &&
$specification->right() instanceof Comparator &&
$specification->right()->sign() === Sign::equality &&
$specification->left()->property() === $specification->right()->property() &&
$specification->left()->value() === $specification->right()->value()
) {
return $this->findComparatorParameters(
Expand All @@ -263,6 +266,7 @@ private function findParamaters(
$specification->left()->sign() === Sign::lessThan &&
$specification->right() instanceof Comparator &&
$specification->right()->sign() === Sign::equality &&
$specification->left()->property() === $specification->right()->property() &&
$specification->left()->value() === $specification->right()->value()
) {
return $this->findComparatorParameters(
Expand Down

0 comments on commit aec3390

Please sign in to comment.