Skip to content
Merged
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
32 changes: 26 additions & 6 deletions core/Logic/BinaryExpression.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,15 @@ final class BinaryExpression implements LogicalObject, MappableObject
private $left = null;
private $right = null;
private $logic = null;
private $brackets = true;

/**
* @return BinaryExpression
*/
public static function create($left, $right, $logic)
{
return new self($left, $right, $logic);
}

public function __construct($left, $right, $logic)
{
Expand All @@ -66,30 +75,41 @@ public function getLogic()
return $this->logic;
}

/**
* @param boolean $noBrackets
* @return BinaryExpression
*/
public function noBrackets($noBrackets = true)
{
$this->brackets = !$noBrackets;
return $this;
}

public function toDialectString(Dialect $dialect)
{
return
'('
.$dialect->toFieldString($this->left)
$sql = $dialect->toFieldString($this->left)
.' '.$dialect->logicToString($this->logic).' '
.$dialect->toValueString($this->right)
.')';
.$dialect->toValueString($this->right);
return $this->brackets ? "({$sql})" : $sql;
}

/**
* @return BinaryExpression
**/
public function toMapped(ProtoDAO $dao, JoinCapableQuery $query)
{
return new self(
$expression = new self(
$dao->guessAtom($this->left, $query),
$dao->guessAtom($this->right, $query),
$this->logic
);

return $expression->noBrackets(!$this->brackets);
}

public function toBoolean(Form $form)
{
Assert::isTrue($this->brackets, 'brackets must be enabled');
$left = $form->toFormValue($this->left);
$right = $form->toFormValue($this->right);

Expand Down
32 changes: 26 additions & 6 deletions core/Logic/PostfixUnaryExpression.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,35 +22,55 @@ final class PostfixUnaryExpression implements LogicalObject, MappableObject

private $subject = null;
private $logic = null;
private $brackets = true;

/**
* @return PostfixUnaryExpression
*/
public static function create($subject, $logic)
{
return new self($subject, $logic);
}

public function __construct($subject, $logic)
{
$this->subject = $subject;
$this->logic = $logic;
}

/**
* @param boolean $noBrackets
* @return PostfixUnaryExpression
*/
public function noBrackets($noBrackets = true)
{
$this->brackets = !$noBrackets;
return $this;
}

public function toDialectString(Dialect $dialect)
{
return
'('
.$dialect->toFieldString($this->subject)
.' '.$dialect->logicToString($this->logic)
.')';
$sql = $dialect->toFieldString($this->subject)
.' '.$dialect->logicToString($this->logic);
return $this->brackets ? "({$sql})" : $sql;
}

/**
* @return PostfixUnaryExpression
**/
public function toMapped(ProtoDAO $dao, JoinCapableQuery $query)
{
return new self(
$expression = new self(
$dao->guessAtom($this->subject, $query),
$this->logic
);

return $expression->noBrackets(!$this->brackets);
}

public function toBoolean(Form $form)
{
Assert::isTrue($this->brackets, 'brackets must be enabled');
$subject = $form->toFormValue($this->subject);

switch ($this->logic) {
Expand Down
32 changes: 26 additions & 6 deletions core/Logic/PrefixUnaryExpression.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,35 +19,55 @@ final class PrefixUnaryExpression implements LogicalObject, MappableObject

private $subject = null;
private $logic = null;
private $brackets = true;

/**
* @return PrefixUnaryExpression
*/
public static function create($subject, $logic)
{
return new self($subject, $logic);
}

public function __construct($logic, $subject)
{
$this->subject = $subject;
$this->logic = $logic;
}

/**
* @param boolean $noBrackets
* @return PrefixUnaryExpression
*/
public function noBrackets($noBrackets = true)
{
$this->brackets = !$noBrackets;
return $this;
}

public function toDialectString(Dialect $dialect)
{
return
'('
.$dialect->logicToString($this->logic)
.' '.$dialect->toFieldString($this->subject)
.')';
$sql = $dialect->logicToString($this->logic)
.' '.$dialect->toFieldString($this->subject);

return $this->brackets ? "({$sql})" : $sql;
}

/**
* @return PrefixUnaryExpression
**/
public function toMapped(ProtoDAO $dao, JoinCapableQuery $query)
{
return new self(
$expression = new self(
$this->logic,
$dao->guessAtom($this->subject, $query)
);
return $expression->noBrackets($this->brackets);
}

public function toBoolean(Form $form)
{
Assert::isTrue($this->brackets, 'brackets must be enabled');
$subject = $form->toFormValue($this->subject);

switch ($this->logic) {
Expand Down
1 change: 1 addition & 0 deletions core/OSQL/SQLFunction.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@ public function toMapped(ProtoDAO $dao, JoinCapableQuery $query)
$sqlFunction = call_user_func_array(array('self', 'create'), $mapped);

$sqlFunction->aggregate = $this->aggregate;
$sqlFunction->alias = $this->alias;

$sqlFunction->castTo($this->cast);

Expand Down
6 changes: 6 additions & 0 deletions doc/ChangeLog
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
2012-03-26 Alexey S. Denisov
* core/Logic/BinaryExpression.class.php,
core/Logic/PostfixUnaryExpression.class.php,
core/Logic/PrefixUnaryExpression.class.php,
core/OSQL/SQLFunction.class.php: Added option noBrackets for binary/unary expressions

2012-04-27 Georgiy T. Kutsurua

* core/Form/Primitives/PrimitiveString.class.php:
Expand Down
33 changes: 33 additions & 0 deletions test/main/CriteriaTest.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,39 @@ public function testDialectStringObjects()
);
}

public function testSqlFunction()
{
$criteria = Criteria::create(TestCity::dao())->
addProjection(
Projection::property(
SQLFunction::create(
'count',
SQLFunction::create(
'substring',
BinaryExpression::create(
'name',
BinaryExpression::create(
DBValue::create('M....w'),
DBValue::create('#'),
'for'
)->
noBrackets(),
'from'
)->
noBrackets()
)
)->
setAggregateDistinct()->
setAlias('my_alias')
)
);

$this->assertEquals(
$criteria->toDialectString(PostgresDialect::me()),
'SELECT count(DISTINCT substring("custom_table"."name" from \'M....w\' for \'#\')) AS "my_alias" FROM "custom_table"'
);
}

public function testSleepWithEmptyDao()
{
$baseCriteria =
Expand Down
15 changes: 14 additions & 1 deletion test/main/OsqlSelectTest.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,19 @@ public function testSelectGet()
SQLFunction::create(
'count', DBField::create('field5', 'test_table')
)->
setAggregateDistinct()->
setAlias('alias5')
)->
get(
SQLFunction::create(
'substring',
BinaryExpression::create(
DBField::create('field6', 'test_table'),
DBValue::create('a..b'),
'from'
)->
noBrackets()
)
);

$this->assertEquals(
Expand All @@ -26,7 +38,8 @@ public function testSelectGet()
.'"test_table"."field2", '
.'"test_table"."field3" AS "alias3", '
.'"test_table"."field4", '
.'count("test_table"."field5") AS "alias5" '
.'count(DISTINCT "test_table"."field5") AS "alias5", '
.'substring("test_table"."field6" from \'a..b\') '
.'FROM "test_table"'
);
}
Expand Down