Skip to content

Commit

Permalink
improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
tpetry committed Nov 30, 2023
1 parent 6b14de6 commit c4f8181
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 23 deletions.
17 changes: 9 additions & 8 deletions src/Language/CaseBlock.php → src/Language/CaseGroup.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,31 +4,32 @@

namespace Tpetry\QueryExpressions\Language;

use Illuminate\Contracts\Database\Query\ConditionExpression;
use Illuminate\Contracts\Database\Query\Expression;
use Illuminate\Database\Grammar;
use Tpetry\QueryExpressions\Concerns\IdentifiesDriver;
use Tpetry\QueryExpressions\Concerns\StringizeExpression;
use Tpetry\QueryExpressions\Function\Conditional\ManyArgumentsExpression;

class CaseBlock extends ManyArgumentsExpression implements ConditionExpression
class CaseGroup implements Expression
{
use IdentifiesDriver;
use StringizeExpression;

/**
* @param non-empty-array<int, CaseCondition> $when
* @param non-empty-array<int, CaseRule> $when
*/
public function __construct(
array $when,
private readonly array $when,
private readonly string|Expression|null $else = null,
) {
parent::__construct($when);
}

public function getValue(Grammar $grammar)
public function getValue(Grammar $grammar): string
{
$conditions = implode(' ', $this->getExpressions($grammar));
$conditions = array_map(
callback: fn ($expression) => $this->stringize($grammar, $expression),
array: $this->when,
);
$conditions = implode(' ', $conditions);

return match ($this->else) {
null => "(case {$conditions} end)",
Expand Down
4 changes: 2 additions & 2 deletions src/Language/CaseCondition.php → src/Language/CaseRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
use Illuminate\Database\Grammar;
use Tpetry\QueryExpressions\Concerns\StringizeExpression;

class CaseCondition implements Expression
class CaseRule implements Expression
{
use StringizeExpression;

Expand All @@ -19,7 +19,7 @@ public function __construct(
) {
}

public function getValue(Grammar $grammar)
public function getValue(Grammar $grammar): string
{
$condition = $this->stringize($grammar, $this->condition);
$result = $this->stringize($grammar, $this->result);
Expand Down
26 changes: 13 additions & 13 deletions tests/Language/CaseTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@
declare(strict_types=1);

use Illuminate\Database\Query\Expression;
use Tpetry\QueryExpressions\Language\CaseBlock;
use Tpetry\QueryExpressions\Language\CaseCondition;
use Tpetry\QueryExpressions\Language\CaseGroup;
use Tpetry\QueryExpressions\Language\CaseRule;
use Tpetry\QueryExpressions\Tests\ConditionExpression;

it('can create create a case-expression with a single branch')
->expect(
new CaseBlock([
new CaseCondition(new Expression(2), new ConditionExpression('1 = 1')),
new CaseGroup([
new CaseRule(new Expression(2), new ConditionExpression('1 = 1')),
])
)
->toBeExecutable()
Expand All @@ -20,9 +20,9 @@
->toBeSqlsrv('(case when 1 = 1 then 2 end)');

it('can create create a case-expression with multiple branches')
->expect(new CaseBlock([
new CaseCondition(new Expression(2), new ConditionExpression('1 = 1')),
new CaseCondition('val', new ConditionExpression('2 = 2')),
->expect(new CaseGroup([
new CaseRule(new Expression(2), new ConditionExpression('1 = 1')),
new CaseRule('val', new ConditionExpression('2 = 2')),
]))
->toBeExecutable(['val int'])
->toBeMysql('(case when 1 = 1 then 2 when 2 = 2 then `val` end)')
Expand All @@ -31,10 +31,10 @@
->toBeSqlsrv('(case when 1 = 1 then 2 when 2 = 2 then [val] end)');

it('can create create a case-expression with multiple branches and expression default')
->expect(new CaseBlock(
->expect(new CaseGroup(
[
new CaseCondition(new Expression(2), new ConditionExpression('1 = 1')),
new CaseCondition('val', new ConditionExpression('2 = 2')),
new CaseRule(new Expression(2), new ConditionExpression('1 = 1')),
new CaseRule('val', new ConditionExpression('2 = 2')),
],
new Expression('4'),
))
Expand All @@ -45,10 +45,10 @@
->toBeSqlsrv('(case when 1 = 1 then 2 when 2 = 2 then [val] else 4 end)');

it('can create create a case-expression with multiple branches and column default')
->expect(new CaseBlock(
->expect(new CaseGroup(
[
new CaseCondition(new Expression(2), new ConditionExpression('1 = 1')),
new CaseCondition('val', new ConditionExpression('2 = 2')),
new CaseRule(new Expression(2), new ConditionExpression('1 = 1')),
new CaseRule('val', new ConditionExpression('2 = 2')),
],
'val',
))
Expand Down

0 comments on commit c4f8181

Please sign in to comment.