Skip to content

Commit

Permalink
Add SQL Expression object
Browse files Browse the repository at this point in the history
  • Loading branch information
cundd committed Dec 21, 2018
1 parent 7d1648b commit 5f156f5
Show file tree
Hide file tree
Showing 5 changed files with 150 additions and 122 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
<?php
declare(strict_types=1);

namespace Cundd\Rest\VirtualObject\Persistence\Backend;

use Cundd\Rest\VirtualObject\Persistence\QueryInterface;

abstract class AbstractSqlExpression implements SqlExpressionInterface
{
private $expression = '';
private $boundVariables = [];

/**
* Expression constructor
*
* @param string $sqlExpression
* @param array $boundVariables
*/
public function __construct(string $sqlExpression = '', array $boundVariables = [])
{
$this->expression = $sqlExpression;
$this->boundVariables = $boundVariables;
}

public function setExpression(string $expression): SqlExpressionInterface
{
$this->expression = $expression;

return $this;
}

public function appendSql(
string $expression,
string $combinator = QueryInterface::COMBINATOR_AND
): SqlExpressionInterface {
$this->assertCombinator($combinator);

if ($this->expression) {
$this->expression .= ' ' . strtoupper($combinator) . ' ' . $expression;
} else {
$this->expression = $expression;
}

return $this;
}

public function getExpression(): string
{
return $this->expression;
}

public function getBoundVariables(): array
{
return $this->boundVariables;
}

public function bindVariable(string $key, $value): SqlExpressionInterface
{
$this->boundVariables[$key] = $value;

return $this;
}

public function __toString()
{
return $this->getExpression();
}

/**
* @param $combinator
*/
public static function assertCombinator($combinator)
{
if (!is_string($combinator)) {
throw new \InvalidArgumentException(
sprintf(
'Logical combinator must be of type string, \'%s\' given',
is_object($combinator) ? get_class($combinator) : gettype($combinator)
)
);
}
if (!in_array(strtoupper($combinator), [QueryInterface::COMBINATOR_AND, QueryInterface::COMBINATOR_OR])) {
throw new \InvalidArgumentException(
sprintf(
'Logical combinator must be either \'%s\' or \'%s\'',
QueryInterface::COMBINATOR_AND,
QueryInterface::COMBINATOR_OR
)
);
}
}
}
4 changes: 2 additions & 2 deletions Classes/VirtualObject/Persistence/Backend/DoctrineBackend.php
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ public function getObjectCountByQuery($tableName, QueryInterface $query)

try {
$statement = $this->getConnection($tableName)->executeQuery(
$baseQuery . " WHERE " . $whereClause->getClause(),
$baseQuery . " WHERE " . $whereClause->getExpression(),
$whereClause->getBoundVariables()
);
} catch (DBALException $exception) {
Expand Down Expand Up @@ -121,7 +121,7 @@ public function getObjectDataByQuery($tableName, QueryInterface $query)
$this->whereClauseBuilder->build($query);
$whereClause = $this->whereClauseBuilder->getWhere();

$sql = $baseSql . " WHERE " . $whereClause->getClause();
$sql = $baseSql . " WHERE " . $whereClause->getExpression();
if ($query instanceof QueryInterface) {
$sql = $this->addOrderingAndLimit($sql, $query);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?php
declare(strict_types=1);

namespace Cundd\Rest\VirtualObject\Persistence\Backend;

use Cundd\Rest\VirtualObject\Persistence\QueryInterface;

interface SqlExpressionInterface
{
/**
* Set the SQL expression string
*
* @param string $expression
* @return self
*/
public function setExpression(string $expression): self;

/**
* Append the string to the SQL expression
*
* @param string $expression
* @param string $combinator
* @return self
*/
public function appendSql(string $expression, string $combinator = QueryInterface::COMBINATOR_AND): self;

/**
* @return string
*/
public function getExpression(): string;

/**
* @return array
*/
public function getBoundVariables(): array;

/**
* Bind the variable to the SQL WHERE-clause
*
* @param string $key
* @param string|int|float $value
* @return self
*/
public function bindVariable(string $key, $value): self;
}
111 changes: 1 addition & 110 deletions Classes/VirtualObject/Persistence/Backend/WhereClause.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,115 +3,6 @@

namespace Cundd\Rest\VirtualObject\Persistence\Backend;

use Cundd\Rest\VirtualObject\Persistence\QueryInterface;

class WhereClause
class WhereClause extends AbstractSqlExpression
{
private $clause = '';
private $boundVariables = [];

/**
* WHERE-clause constructor
*
* @param string $clause
* @param array $boundVariables
*/
public function __construct($clause = '', array $boundVariables = [])
{
$this->setClause($clause);
$this->boundVariables = $boundVariables;
}

/**
* Set the SQL WHERE-clause
*
* @param string $clause
* @return $this
*/
public function setClause($clause)
{
if (!is_string($clause)) {
throw new \InvalidArgumentException();
}
$this->clause = $clause;

return $this;
}

/**
* Append the string to the SQL WHERE-clause
*
* @param string $clause
* @param string $combinator
* @return $this
*/
public function appendSql($clause, $combinator = QueryInterface::COMBINATOR_AND)
{
if (!is_string($clause)) {
throw new \InvalidArgumentException();
}
$this->assertCombinator($combinator);

if ($this->clause) {
$this->clause .= ' ' . strtoupper($combinator) . ' ' . $clause;
} else {
$this->clause = $clause;
}

return $this;
}

/**
* @return string
*/
public function getClause()
{
return $this->clause;
}

/**
* @return array
*/
public function getBoundVariables()
{
return $this->boundVariables;
}

/**
* Bind the variable to the SQL WHERE-clause
*
* @param string $key
* @param string|int|float $value
* @return $this
*/
public function bindVariable($key, $value)
{
$this->boundVariables[$key] = $value;

return $this;
}

/**
* @param $combinator
*/
public static function assertCombinator($combinator)
{
if (!is_string($combinator)) {
throw new \InvalidArgumentException(
sprintf(
'Logical combinator must be of type string, \'%s\' given',
is_object($combinator) ? get_class($combinator) : gettype($combinator)
)
);
}
if (!in_array(strtoupper($combinator), [QueryInterface::COMBINATOR_AND, QueryInterface::COMBINATOR_OR])) {
throw new \InvalidArgumentException(
sprintf(
'Logical combinator must be either \'%s\' or \'%s\'',
QueryInterface::COMBINATOR_AND,
QueryInterface::COMBINATOR_OR
)
);
}
}
}
20 changes: 10 additions & 10 deletions Tests/Unit/VirtualObject/Backend/WhereClauseBuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public function testAddConstraint()
$where = $this->fixture->getWhere();
$this->assertEquals(
'`property` = :property',
$where->getClause()
$where->getExpression()
);
$this->assertEquals(
['property' => 'value'],
Expand All @@ -35,7 +35,7 @@ public function testAddConstraintOr()
$where = $this->fixture->getWhere();
$this->assertEquals(
'`property` = :property OR `another_property` = :another_property',
$where->getClause()
$where->getExpression()
);
$this->assertEquals(
['property' => 'value', 'another_property' => 200],
Expand All @@ -56,7 +56,7 @@ function ($v) {
$where = $this->fixture->getWhere();
$this->assertEquals(
'`property` = :property',
$where->getClause()
$where->getExpression()
);
$this->assertEquals(
['property' => '--value--'],
Expand All @@ -78,7 +78,7 @@ function ($cn) {
$where = $this->fixture->getWhere();
$this->assertEquals(
'@property@ = :property',
$where->getClause()
$where->getExpression()
);
$this->assertEquals(
['property' => 'value'],
Expand All @@ -99,7 +99,7 @@ public function testAddConstraintWithBindingPrefix()
$where = $this->fixture->getWhere();
$this->assertEquals(
'`property` = :pp_property',
$where->getClause()
$where->getExpression()
);
$this->assertEquals(
['pp_property' => 'value'],
Expand All @@ -116,7 +116,7 @@ private function assertEmptyWhere()
{
$where = $this->fixture->getWhere();
$this->assertInstanceOf(WhereClause::class, $where);
$this->assertEquals('', $where->getClause());
$this->assertEquals('', $where->getExpression());
$this->assertEmpty($where->getBoundVariables());
}

Expand All @@ -129,7 +129,7 @@ public function testBuild()
$where = $this->fixture->getWhere();
$this->assertEquals(
'`property` = :property AND `another_property` = :another_property',
$where->getClause()
$where->getExpression()
);
$this->assertEquals(
['property' => 'value', 'another_property' => 200],
Expand All @@ -149,7 +149,7 @@ public function testAddConstraints()
$where = $this->fixture->getWhere();
$this->assertEquals(
'`property` = :property AND `another_property` = :another_property',
$where->getClause()
$where->getExpression()
);
$this->assertEquals(
['property' => 'value', 'another_property' => 200],
Expand All @@ -172,7 +172,7 @@ function ($cn) {
$where = $this->fixture->getWhere();
$this->assertEquals(
'@property@ = :property AND @another_property@ = :another_property',
$where->getClause()
$where->getExpression()
);
$this->assertEquals(
['property' => '--value--', 'another_property' => '--200--'],
Expand All @@ -192,7 +192,7 @@ public function testAddConstraintsWithBindingPrefix()
$where = $this->fixture->getWhere();
$this->assertEquals(
'`property` = :pp_property AND `another_property` = :pp_another_property',
$where->getClause()
$where->getExpression()
);
$this->assertEquals(
['pp_property' => 'value', 'pp_another_property' => 200],
Expand Down

0 comments on commit 5f156f5

Please sign in to comment.