Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CaseWhenExpression для PostgreSQL #229

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
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
87 changes: 87 additions & 0 deletions core/Logic/CaseWhenExpression.class.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
<?php
/****************************************************************************
* Copyright (C) 2014 by Alexey S. Denisov *
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU Lesser General Public License as *
* published by the Free Software Foundation; either version 3 of the *
* License, or (at your option) any later version. *
* *
****************************************************************************/

class CaseWhenExpression implements MappableObject
{
private $cases = array();
private $else = null;

public static function create($expression = null, $result = null, $else = null)
{
return new static($expression, $result, $else);
}

public function __construct($expression = null, $result = null, $else = null)
{
if ($expression !== null && $result !== null) {
$this->addCase($expression, $result);
}
if ($else !== null) {
$this->addElse($else);
}
}

/**
* @param $expression
* @param $result
* @return CaseWhenExpression
*/
public function addCase($expression, $result)
{
$this->cases[] = array($expression, $result);
return $this;
}

/**
* @param $result
* @return CaseWhenExpression
*/
public function addElse($result)
{
$this->else = $result;
return $this;
}

/**
* @param ProtoDAO $dao
* @param JoinCapableQuery $query
* @return MappableObject
*/
public function toMapped(ProtoDAO $dao, JoinCapableQuery $query)
{
$expr = new CaseWhenExpression();
foreach ($this->cases as $case) {
$expr->addCase(
$dao->guessAtom($case[0], $query),
$dao->guessAtom($case[1], $query)
);
}
if ($this->else) {
$expr->addElse($dao->guessAtom($this->else, $query));
}

return $expr;
}

public function toDialectString(Dialect $dialect)
{
$sqlCases = array();
foreach ($this->cases as $case) {
$sqlCases[] = 'WHEN '.$dialect->toFieldString($case[0])
.' THEN '.$dialect->toValueString($case[1]);
}
if ($this->else) {
$sqlCases[] = 'ELSE '.$dialect->toValueString($this->else);
}

return 'CASE '.implode(' ', $sqlCases).' END';
}
}
2 changes: 1 addition & 1 deletion core/Logic/PrefixUnaryExpression.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ final class PrefixUnaryExpression implements LogicalObject, MappableObject
*/
public static function create($subject, $logic)
{
return new self($subject, $logic);
return new self($logic, $subject);
}

public function __construct($logic, $subject)
Expand Down
42 changes: 42 additions & 0 deletions test/db/CaseWhenExpressionDBTest.class.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?php
class CaseWhenExpressionDBTest extends TestCaseDAO
{
/**
* @group caseWhen
*/
public function testCaseWhenExpression()
{
$dialect = $this->getDbByType('PgSQL')->getDialect();

$beautifulNameExpr = CaseWhenExpression::create(
Expression::isTrue('capital'),
SQLFunction::create('upper', 'name'),
SQLFunction::create('lower', 'name')
)
->addCase(Expression::eq('name', 'St. Peterburg'), 'ST. PETERBURG');

$orderExpr = CaseWhenExpression::create()
->addCase(Expression::isTrue('capital'), SQLFunction::create('upper', 'name'))
->addCase(Expression::eq('name', 'St. Peterburg'), 'ST. PETERBURG')
->addElse('name');

$criteria = Criteria::create(TestCity::dao())
->addProjection(Projection::property($beautifulNameExpr, 'beautifulName'))
->addOrder($orderExpr);

$expectation = 'SELECT CASE '
.'WHEN ("custom_table"."capital" IS TRUE) THEN upper("custom_table"."name") '
.'WHEN ("custom_table"."name" = \'St. Peterburg\') THEN \'ST. PETERBURG\' '
.'ELSE lower("custom_table"."name") '
.'END AS "beautifulName" '
.'FROM "custom_table" '
.'ORDER BY CASE '
.'WHEN ("custom_table"."capital" IS TRUE) THEN upper("custom_table"."name") '
.'WHEN ("custom_table"."name" = \'St. Peterburg\') THEN \'ST. PETERBURG\' '
.'ELSE "custom_table"."name" '
.'END';

$this->assertEquals($expectation, $criteria->toDialectString($dialect));
}
}
?>