mirror of
https://code.mensbeam.com/MensBeam/Arsse.git
synced 2024-12-22 13:12:41 +00:00
Prototype for nesting query filters
This commit is contained in:
parent
630536d789
commit
a44fe103d8
2 changed files with 75 additions and 32 deletions
|
@ -6,16 +6,10 @@
|
||||||
declare(strict_types=1);
|
declare(strict_types=1);
|
||||||
namespace JKingWeb\Arsse\Misc;
|
namespace JKingWeb\Arsse\Misc;
|
||||||
|
|
||||||
class Query {
|
class Query extends QueryFilter {
|
||||||
protected $qBody = ""; // main query body
|
protected $qBody = ""; // main query body
|
||||||
protected $tBody = []; // main query parameter types
|
protected $tBody = []; // main query parameter types
|
||||||
protected $vBody = []; // main query parameter values
|
protected $vBody = []; // main query parameter values
|
||||||
protected $qWhere = []; // WHERE clause components
|
|
||||||
protected $tWhere = []; // WHERE clause type bindings
|
|
||||||
protected $vWhere = []; // WHERE clause binding values
|
|
||||||
protected $qWhereNot = []; // WHERE NOT clause components
|
|
||||||
protected $tWhereNot = []; // WHERE NOT clause type bindings
|
|
||||||
protected $vWhereNot = []; // WHERE NOT clause binding values
|
|
||||||
protected $group = []; // GROUP BY clause components
|
protected $group = []; // GROUP BY clause components
|
||||||
protected $order = []; // ORDER BY clause components
|
protected $order = []; // ORDER BY clause components
|
||||||
protected $limit = 0;
|
protected $limit = 0;
|
||||||
|
@ -34,24 +28,6 @@ class Query {
|
||||||
return $this;
|
return $this;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function setWhere(string $where, $types = null, $values = null): self {
|
|
||||||
$this->qWhere[] = $where;
|
|
||||||
if (!is_null($types)) {
|
|
||||||
$this->tWhere[] = $types;
|
|
||||||
$this->vWhere[] = $values;
|
|
||||||
}
|
|
||||||
return $this;
|
|
||||||
}
|
|
||||||
|
|
||||||
public function setWhereNot(string $where, $types = null, $values = null): self {
|
|
||||||
$this->qWhereNot[] = $where;
|
|
||||||
if (!is_null($types)) {
|
|
||||||
$this->tWhereNot[] = $types;
|
|
||||||
$this->vWhereNot[] = $values;
|
|
||||||
}
|
|
||||||
return $this;
|
|
||||||
}
|
|
||||||
|
|
||||||
public function setGroup(string ...$column): self {
|
public function setGroup(string ...$column): self {
|
||||||
foreach ($column as $col) {
|
foreach ($column as $col) {
|
||||||
$this->group[] = $col;
|
$this->group[] = $col;
|
||||||
|
@ -81,11 +57,11 @@ class Query {
|
||||||
}
|
}
|
||||||
|
|
||||||
public function getTypes(): array {
|
public function getTypes(): array {
|
||||||
return ValueInfo::flatten([$this->tBody, $this->tWhere, $this->tWhereNot]);
|
return ValueInfo::flatten([$this->tBody, $this->getWhereTypes()]);
|
||||||
}
|
}
|
||||||
|
|
||||||
public function getValues(): array {
|
public function getValues(): array {
|
||||||
return ValueInfo::flatten([$this->vBody, $this->vWhere, $this->vWhereNot]);
|
return ValueInfo::flatten([$this->vBody, $this->getWhereValues()]);
|
||||||
}
|
}
|
||||||
|
|
||||||
protected function buildQueryBody(): string {
|
protected function buildQueryBody(): string {
|
||||||
|
@ -94,11 +70,7 @@ class Query {
|
||||||
$out .= $this->qBody;
|
$out .= $this->qBody;
|
||||||
// add any WHERE terms
|
// add any WHERE terms
|
||||||
if (sizeof($this->qWhere) || sizeof($this->qWhereNot)) {
|
if (sizeof($this->qWhere) || sizeof($this->qWhereNot)) {
|
||||||
$where = implode(" AND ", $this->qWhere);
|
$out .= " WHERE ".$this->buildWhereBody();
|
||||||
$whereNot = implode(" OR ", $this->qWhereNot);
|
|
||||||
$whereNot = strlen($whereNot) ? "NOT ($whereNot)" : "";
|
|
||||||
$where = implode(" AND ", array_filter([$where, $whereNot]));
|
|
||||||
$out .= " WHERE $where";
|
|
||||||
}
|
}
|
||||||
// add any GROUP BY terms
|
// add any GROUP BY terms
|
||||||
if (sizeof($this->group)) {
|
if (sizeof($this->group)) {
|
||||||
|
|
71
lib/Misc/QueryFilter.php
Normal file
71
lib/Misc/QueryFilter.php
Normal file
|
@ -0,0 +1,71 @@
|
||||||
|
<?php
|
||||||
|
/** @license MIT
|
||||||
|
* Copyright 2017 J. King, Dustin Wilson et al.
|
||||||
|
* See LICENSE and AUTHORS files for details */
|
||||||
|
|
||||||
|
declare(strict_types=1);
|
||||||
|
namespace JKingWeb\Arsse\Misc;
|
||||||
|
|
||||||
|
class QueryFilter {
|
||||||
|
protected $qWhere = []; // WHERE clause components
|
||||||
|
protected $tWhere = []; // WHERE clause type bindings
|
||||||
|
protected $vWhere = []; // WHERE clause binding values
|
||||||
|
protected $qWhereNot = []; // WHERE NOT clause components
|
||||||
|
protected $tWhereNot = []; // WHERE NOT clause type bindings
|
||||||
|
protected $vWhereNot = []; // WHERE NOT clause binding values
|
||||||
|
|
||||||
|
public $filterRestrictive = true;
|
||||||
|
|
||||||
|
public function setWhere(string $where, $types = null, $values = null): self {
|
||||||
|
$this->qWhere[] = $where;
|
||||||
|
if (!is_null($types)) {
|
||||||
|
$this->tWhere[] = $types ?? [];
|
||||||
|
$this->vWhere[] = $values;
|
||||||
|
}
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function setWhereNot(string $where, $types = null, $values = null): self {
|
||||||
|
$this->qWhereNot[] = $where;
|
||||||
|
if (!is_null($types)) {
|
||||||
|
$this->tWhereNot[] = $types;
|
||||||
|
$this->vWhereNot[] = $values;
|
||||||
|
}
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function setFilter(self $filter): self {
|
||||||
|
$this->qWhere[] = "(".$filter->buildWhereBody().")";
|
||||||
|
$this->tWhere[] = $filter->getWhereTypes();
|
||||||
|
$this->vWhere[] = $filter->getWhereValues();
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
|
||||||
|
protected function getWhereTypes(): array {
|
||||||
|
return ValueInfo::flatten([$this->tWhere, $this->tWhereNot]);
|
||||||
|
}
|
||||||
|
|
||||||
|
protected function getWhereValues(): array {
|
||||||
|
return ValueInfo::flatten([$this->vWhere, $this->vWhereNot]);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function getTypes(): array {
|
||||||
|
return $this->getWhereTypes();
|
||||||
|
}
|
||||||
|
|
||||||
|
public function getValues(): array {
|
||||||
|
return $this->getWhereValues();
|
||||||
|
}
|
||||||
|
|
||||||
|
protected function buildWhereBody(): string {
|
||||||
|
$glue = $this->filterRestrictive ? " AND " : " OR ";
|
||||||
|
$where = implode($glue, $this->qWhere);
|
||||||
|
$whereNot = implode(" OR ", $this->qWhereNot);
|
||||||
|
$whereNot = strlen($whereNot) ? "NOT ($whereNot)" : "";
|
||||||
|
return implode($glue, array_filter([$where, $whereNot]));
|
||||||
|
}
|
||||||
|
|
||||||
|
public function __toString() {
|
||||||
|
return $this->buildWhereBody();
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in a new issue