Skip to content

Commit

Permalink
Add where OR statement support; which is technical dept at its start.
Browse files Browse the repository at this point in the history
  • Loading branch information
BowlOfSoup committed Mar 26, 2019
1 parent e4887ca commit 5c89572
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 4 deletions.
12 changes: 12 additions & 0 deletions src/Builder/QueryBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,18 @@ public function where(string $where): self
return $this;
}

/**
* @param string $where
*
* @return \BowlOfSoup\CouchbaseAccessLayer\Builder\QueryBuilder
*/
public function whereOr(string $where): self
{
$this->query->addWhereOr($where);

return $this;
}

/**
* @param string $orderBy
* @param string|null $direction
Expand Down
21 changes: 17 additions & 4 deletions src/Model/Query.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,10 @@ class Query
private $useIndex;

/** @var array */
private $where = [];
private $whereAnd = [];

/** @var array */
private $whereOr = [];

/** @var array */
private $orderBy = [];
Expand Down Expand Up @@ -108,7 +111,15 @@ public function setUseIndex(string $index)
*/
public function addWhere(string $where)
{
$this->where[] = $where;
$this->whereAnd[] = $where;
}

/**
* @param string $where
*/
public function addWhereOr(string $where)
{
$this->whereOr[] = $where;
}

/**
Expand Down Expand Up @@ -194,8 +205,10 @@ public function build(): string
$query .= sprintf('USE INDEX (%s USING GSI) ', $this->useIndex);
}

if (!empty($this->where)) {
$query .= 'WHERE ' . implode(' AND ', $this->where);
if (!empty($this->whereAnd) || !empty($this->whereOr)) {
$whereOrStatement = !empty($this->whereOr) ? ' OR ' : '';

$query .= 'WHERE ' . implode(' AND ', $this->whereAnd) . $whereOrStatement . implode(' OR ', $this->whereOr);
}

if (!empty($this->groupBy)) {
Expand Down
42 changes: 42 additions & 0 deletions tests/Builder/QueryBuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,48 @@ public function testGetQuery()
], $queryBuilder->getParameters());
}

/**
* @throws \BowlOfSoup\CouchbaseAccessLayer\Exception\CouchbaseQueryException
*/
public function testGetQueryWithWhereOr()
{
$queryBuilder = new QueryBuilder('default_bucket');

$queryBuilder
->where('someField = $someField')
->whereOr('`key` IN [\'foo\', \'bar\']');

$queryBuilder->setParameters([
'someField' => '12346782',
'anotherValue' => 'ThisIsADocument',
'type' => 'SomeDifferentTypeOfDocument',
]);

$queryBuilder
->where('data.foo > $dataFoo')
->setParameter('dataFoo', 'bar');

$queryBuilder
->groupBy('someField')
->orderBy('data.someOrderingField', Query::ORDER_DESC)
->limit(10)
->offset(5);

$queryBuilder->select('someField');
$queryBuilder->selectMultiple(['foo', 'COUNT(bar) AS bar_counted']);

$this->assertSame(
'SELECT someField, foo, COUNT(bar) AS bar_counted FROM `default_bucket` WHERE someField = $someField AND data.foo > $dataFoo OR `key` IN [\'foo\', \'bar\'] GROUP BY someField ORDER BY data.someOrderingField DESC LIMIT 10 OFFSET 5',
$queryBuilder->getQuery()
);
$this->assertSame([
'someField' => '12346782',
'anotherValue' => 'ThisIsADocument',
'type' => 'SomeDifferentTypeOfDocument',
'dataFoo' => 'bar',
], $queryBuilder->getParameters());
}

/**
* @throws \BowlOfSoup\CouchbaseAccessLayer\Exception\CouchbaseQueryException
*/
Expand Down

0 comments on commit 5c89572

Please sign in to comment.