Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 59 additions & 0 deletions src/Partial/CaseStatement.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
<?php

declare(strict_types=1);

namespace Latitude\QueryBuilder\Partial;

use Latitude\QueryBuilder\EngineInterface;
use Latitude\QueryBuilder\ExpressionInterface;
use Latitude\QueryBuilder\StatementInterface;

final class CaseStatement implements StatementInterface
{
private ExpressionInterface $expression;
private ?ExpressionInterface $else = null;

public function __construct(?StatementInterface $expression = null)
{
$this->expression = new Expression('CASE');

if ($expression) {
$this->expression = $this->expression->append('%s', $expression);
}
}

public function when(StatementInterface $when, StatementInterface $then): self
{
$this->expression = $this->expression->append('WHEN %s THEN %s', $when, $then);

return $this;
}

public function else(StatementInterface $else): self
{
$this->else = new Expression('ELSE %s', $else);

return $this;
}

public function sql(EngineInterface $engine): string
{
return $this->buildCaseStatement()->append('END')->sql($engine);
}

public function params(EngineInterface $engine): array
{
return $this->buildCaseStatement()->params($engine);
}

private function buildCaseStatement(): ExpressionInterface
{
$expression = $this->expression;

if ($this->else) {
$expression = $expression->append('%s', $this->else);
}

return $expression;
}
}
9 changes: 8 additions & 1 deletion src/functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

namespace Latitude\QueryBuilder;

use Latitude\QueryBuilder\Partial\CaseStatement;
use Latitude\QueryBuilder\Partial\Criteria;
use Latitude\QueryBuilder\Partial\Parameter;

use function array_map;
Expand Down Expand Up @@ -56,7 +58,7 @@ function on(string $left, string $right): CriteriaInterface
*/
function order($column, ?string $direction = null): StatementInterface
{
if (! $direction) {
if (!$direction) {
return identify($column);
}

Expand Down Expand Up @@ -152,3 +154,8 @@ function listing(array $values, string $separator = ', '): Partial\Listing
{
return new Partial\Listing($separator, ...paramAll($values));
}

function caseStatement(?StatementInterface $expression = null): CaseStatement
{
return new Partial\CaseStatement($expression);
}
60 changes: 60 additions & 0 deletions tests/Partial/CaseStatementTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
<?php

declare(strict_types=1);

namespace Latitude\QueryBuilder\Partial;

use Latitude\QueryBuilder\TestCase;

use function Latitude\QueryBuilder\caseStatement;
use function Latitude\QueryBuilder\field;
use function Latitude\QueryBuilder\identify;
use function Latitude\QueryBuilder\literal;
use function Latitude\QueryBuilder\param;

class CaseStatementTest extends TestCase
{
public function testAsIfElse(): void
{
$field = field('role');

$expr = caseStatement()
->when(
$field->eq('admin'),
literal(1),
)
->when(
$field->eq('editor'),
literal(2),
)
->when(
$field->eq('user'),
param(3),
)
->else(param(4));

$this->assertSql('CASE WHEN role = ? THEN 1 WHEN role = ? THEN 2 WHEN role = ? THEN ? ELSE ? END', $expr);
$this->assertParams(['admin', 'editor', 'user', 3, 4], $expr);
}

public function testAsSwitch(): void
{
$expr = caseStatement(identify('role'))
->when(
param('admin'),
literal(1),
)
->when(
param('editor'),
literal(2),
)
->when(
param('user'),
param(3),
)
->else(param(4));

$this->assertSql('CASE role WHEN ? THEN 1 WHEN ? THEN 2 WHEN ? THEN ? ELSE ? END', $expr);
$this->assertParams(['admin', 'editor', 'user', 3, 4], $expr);
}
}
Loading