- Bugfixes done linked to SearchRequest and Searchable features.

- Fixed a bug introduced into PdoObject exception on latest commit.
- Corrected the open() close() enclosure of query condition.
This commit is contained in:
Dave M. 2020-03-31 13:24:15 -04:00
parent f98cb2ded0
commit 84da4dbb7b
9 changed files with 111 additions and 20 deletions

View File

@ -28,7 +28,11 @@ class Relation implements \Ulmus\Annotation\Annotation {
} }
public function entity() { public function entity() {
try {
$e = $this->entity; $e = $this->entity;
} catch (\Throwable $ex) {
throw new \Exception("Your @Relation annotation seems to be missing an `entity` entry.");
}
return new $e(); return new $e();
} }

View File

@ -16,9 +16,15 @@ class PdoObject extends PDO {
return $statement; return $statement;
} }
} catch (\PDOException $e) { } catch (\PDOException $e) {
switch ( $e->getCode() ) {
case 42000:
throw new \PdoException($e->getMessage() . " `$sql` with data:" . json_encode($parameters));
default:
throw $e; throw $e;
} }
} }
}
public function runQuery(string $sql, array $parameters = []): ? PDOStatement { public function runQuery(string $sql, array $parameters = []): ? PDOStatement {
try { try {
@ -26,9 +32,16 @@ class PdoObject extends PDO {
return $this->execute($statement, $parameters, true); return $this->execute($statement, $parameters, true);
} }
} catch (\PDOException $e) { } catch (\PDOException $e) {
switch ( $e->getCode() ) {
case 42000:
throw new \PdoException($e->getMessage() . " `$sql` with data:" . json_encode($parameters));
default:
throw $e; throw $e;
} }
}
return null; return null;
} }

View File

@ -19,8 +19,12 @@ class Limit extends Fragment {
public function render() : string public function render() : string
{ {
if ( $this->limit < 0 ) {
throw new \Exception("An error occured trying to render the LIMIT fragment ; given value has to be > 0. Received {$this->limit}");
}
return $this->renderSegments([ return $this->renderSegments([
sprintf($this->keyword, $this->limit) sprintf($this->keyword, abs($this->limit))
]); ]);
} }
} }

View File

@ -17,6 +17,10 @@ class Offset extends Fragment {
public function render() : string public function render() : string
{ {
if ( $this->offset < 0 ) {
throw new \Exception("An error occured trying to render the OFFSET fragment ; given value has to be > 0. Received {$this->offset}");
}
return $this->renderSegments([ return $this->renderSegments([
'OFFSET', $this->offset, 'OFFSET', $this->offset,
]); ]);

View File

@ -139,7 +139,13 @@ class QueryBuilder
public function open(string $condition = Query\Where::CONDITION_AND) : self public function open(string $condition = Query\Where::CONDITION_AND) : self
{ {
if ( null !== ($this->where ?? false) ) { if ( null !== ($this->where ?? null) ) {
$this->where->conditionList[] = $new = new Query\Where($this, $condition);
$this->where = $new;
}
else {
$this->where = new Query\Where($this, $condition);
$this->push($this->where);
$this->where->conditionList[] = $new = new Query\Where($this, $condition); $this->where->conditionList[] = $new = new Query\Where($this, $condition);
$this->where = $new; $this->where = $new;
} }
@ -149,7 +155,13 @@ class QueryBuilder
public function close() : self public function close() : self
{ {
if ( null !== ($this->where ?? false) && $this->where->parent ) { if ( null !== ($this->where ?? null) && $this->where->parent ) {
# if an enclosure was opened, and nothing done, we must remove the unused node
if ( empty($this->where->conditionList) && (count($this->where->parent->conditionList) === 1) ) {
unset($this->where->parent->conditionList);
}
$this->where = $this->where->parent; $this->where = $this->where->parent;
} }
@ -167,6 +179,7 @@ class QueryBuilder
} }
$this->conditionOperator = $operator; $this->conditionOperator = $operator;
$where->add($field, $value, $operator, $condition, $not); $where->add($field, $value, $operator, $condition, $not);
return $this; return $this;
@ -179,7 +192,6 @@ class QueryBuilder
public function groupBy() : self public function groupBy() : self
{ {
//$this->queryBuilder->groupBy();
return $this; return $this;
} }

View File

@ -217,8 +217,25 @@ class Repository
public function wheres(array $fieldValues, string $operator = Query\Where::OPERATOR_EQUAL) : self public function wheres(array $fieldValues, string $operator = Query\Where::OPERATOR_EQUAL) : self
{ {
foreach($fieldValues as $field => $value) { foreach($fieldValues as $field => $value) {
if ( is_array($value) ) {
switch ($value[1]) {
case Query\Where::CONDITION_AND:
$this->where($field, $value[0], $operator);
break;
case Query\Where::CONDITION_OR:
$this->or($field, $value[0], $operator);
break;
case Query\Where::CONDITION_NOT:
$this->notWhere($field, $value[0], $operator);
break;
}
}
else {
$this->where($field, $value, $operator); $this->where($field, $value, $operator);
} }
}
return $this; return $this;
} }
@ -287,24 +304,47 @@ class Repository
public function like($field, $value) : self public function like($field, $value) : self
{ {
$this->queryBuilder->where($field, $value, Query\Where::OPERATOR_LIKE, Query\Where::CONDITION_AND); $this->where($field, $value, Query\Where::OPERATOR_LIKE);
return $this;
}
public function orLike($field, $value) : self
{
$this->or($field, $value, Query\Where::OPERATOR_LIKE);
return $this; return $this;
} }
public function notLike($field, $value) : self public function notLike($field, $value) : self
{ {
$this->queryBuilder->notWhere($field, $value, Query\Where::OPERATOR_LIKE, Query\Where::CONDITION_AND, true); $this->notWhere($field, $value, Query\Where::OPERATOR_LIKE);
return $this; return $this;
} }
public function likes(array $fieldValues, string $condition = Query\Where::CONDITION_AND) : self
public function likes(array $fieldValues) : self
{ {
foreach($fieldValues as $field => $value) { foreach($fieldValues as $field => $value) {
if ( is_array($value) ) {
switch ($value[1]) {
case Query\Where::CONDITION_AND:
$this->like($field, $value[0]);
break;
case Query\Where::CONDITION_OR:
$this->orLike($field, $value[0]);
break;
case Query\Where::CONDITION_NOT:
$this->notLike($field, $value[0]);
break;
}
}
else {
$this->like($field, $value); $this->like($field, $value);
} }
}
return $this; return $this;
} }
@ -394,14 +434,16 @@ class Repository
public function filterServerRequest(SearchRequest\SearchRequestInterface $searchRequest) : self public function filterServerRequest(SearchRequest\SearchRequestInterface $searchRequest) : self
{ {
$searchRequest->count = (clone $this)->wheres($searchRequest->wheres()) $searchRequest->count = $searchRequest->filter( clone $this )
->likes($searchRequest->likes()) ->wheres($searchRequest->wheres(), Query\Where::OPERATOR_EQUAL, Query\Where::CONDITION_AND)
->likes($searchRequest->likes(), Query\Where::CONDITION_OR)
->orders($searchRequest->orders()) ->orders($searchRequest->orders())
->groups($searchRequest->groups()) ->groups($searchRequest->groups())
->count(); ->count();
return $this->wheres($searchRequest->wheres()) return $searchRequest->filter($this)
->likes($searchRequest->likes()) ->wheres($searchRequest->wheres(), Query\Where::OPERATOR_EQUAL, Query\Where::CONDITION_AND)
->likes($searchRequest->likes(), Query\Where::CONDITION_OR)
->orders($searchRequest->orders()) ->orders($searchRequest->orders())
->groups($searchRequest->groups()) ->groups($searchRequest->groups())
->offset($searchRequest->offset()) ->offset($searchRequest->offset())

View File

@ -10,6 +10,18 @@ trait SearchRequestPaginationTrait {
public int $pageCount = 0; public int $pageCount = 0;
public int $limit = 25;
public function limit(): int
{
return $this->limit;
}
public function offset(): int
{
return abs( ( $this->page - 1 ) * $this->limit() );
}
public function pagination(int $page, int $itemCount) : void public function pagination(int $page, int $itemCount) : void
{ {
$this->count = $itemCount; $this->count = $itemCount;

View File

@ -6,6 +6,6 @@ use \Psr\Http\Message\ServerRequestInterface;
interface SearchableInterface { interface SearchableInterface {
public static function searchRequest(ServerRequestInterface $request) : SearchRequestInterface; public static function searchRequest() : SearchRequestInterface;
} }