2017-06-04 18:00:18 -04:00
|
|
|
<?php
|
|
|
|
declare(strict_types=1);
|
2017-06-18 10:23:37 -04:00
|
|
|
namespace JKingWeb\Arsse\Misc;
|
2017-06-04 18:00:18 -04:00
|
|
|
|
|
|
|
class Query {
|
2017-07-07 11:49:54 -04:00
|
|
|
protected $qBody = ""; // main query body
|
|
|
|
protected $tBody = []; // main query parameter types
|
|
|
|
protected $vBody = []; // main query parameter values
|
2017-06-04 18:00:18 -04:00
|
|
|
protected $qCTE = []; // Common table expression query components
|
|
|
|
protected $tCTE = []; // Common table expression type bindings
|
|
|
|
protected $vCTE = []; // Common table expression binding values
|
|
|
|
protected $jCTE = []; // Common Table Expression joins
|
|
|
|
protected $qWhere = []; // WHERE clause components
|
|
|
|
protected $tWhere = []; // WHERE clause type bindings
|
|
|
|
protected $vWhere = []; // WHERE clause binding values
|
|
|
|
protected $order = []; // ORDER BY clause components
|
|
|
|
protected $limit = 0;
|
|
|
|
protected $offset = 0;
|
|
|
|
|
|
|
|
|
2017-08-29 10:50:31 -04:00
|
|
|
public function __construct(string $body = "", $types = null, $values = null) {
|
2017-07-07 11:49:54 -04:00
|
|
|
$this->setBody($body, $types, $values);
|
2017-06-04 18:00:18 -04:00
|
|
|
}
|
|
|
|
|
2017-08-29 10:50:31 -04:00
|
|
|
public function setBody(string $body = "", $types = null, $values = null): bool {
|
2017-07-07 11:49:54 -04:00
|
|
|
$this->qBody = $body;
|
2017-08-29 10:50:31 -04:00
|
|
|
if (!is_null($types)) {
|
2017-07-07 11:49:54 -04:00
|
|
|
$this->tBody[] = $types;
|
|
|
|
$this->vBody[] = $values;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2017-08-29 10:50:31 -04:00
|
|
|
public function setCTE(string $tableSpec, string $body, $types = null, $values = null, string $join = ''): bool {
|
2017-07-07 11:49:54 -04:00
|
|
|
$this->qCTE[] = "$tableSpec as ($body)";
|
2017-08-29 10:50:31 -04:00
|
|
|
if (!is_null($types)) {
|
2017-06-04 18:00:18 -04:00
|
|
|
$this->tCTE[] = $types;
|
|
|
|
$this->vCTE[] = $values;
|
|
|
|
}
|
2017-08-29 10:50:31 -04:00
|
|
|
if (strlen($join)) { // the CTE might only participate in subqueries rather than a join on the main query
|
2017-07-20 22:40:09 -04:00
|
|
|
$this->jCTE[] = $join;
|
|
|
|
}
|
2017-06-04 18:00:18 -04:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2017-08-29 10:50:31 -04:00
|
|
|
public function setWhere(string $where, $types = null, $values = null): bool {
|
2017-06-04 18:00:18 -04:00
|
|
|
$this->qWhere[] = $where;
|
2017-08-29 10:50:31 -04:00
|
|
|
if (!is_null($types)) {
|
2017-06-04 18:00:18 -04:00
|
|
|
$this->tWhere[] = $types;
|
|
|
|
$this->vWhere[] = $values;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2017-08-29 10:50:31 -04:00
|
|
|
public function setOrder(string $order, bool $prepend = false): bool {
|
|
|
|
if ($prepend) {
|
2017-06-04 18:00:18 -04:00
|
|
|
array_unshift($this->order, $order);
|
|
|
|
} else {
|
|
|
|
$this->order[] = $order;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2017-08-29 10:50:31 -04:00
|
|
|
public function setLimit(int $limit, int $offset = 0): bool {
|
2017-07-07 11:49:54 -04:00
|
|
|
$this->limit = $limit;
|
|
|
|
$this->offset = $offset;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2017-08-29 10:50:31 -04:00
|
|
|
public function pushCTE(string $tableSpec, string $join = ''): bool {
|
2017-07-07 11:49:54 -04:00
|
|
|
// this function takes the query body and converts it to a common table expression, putting it at the bottom of the existing CTE stack
|
|
|
|
// all WHERE, ORDER BY, and LIMIT parts belong to the new CTE and are removed from the main query
|
|
|
|
$this->setCTE($tableSpec, $this->buildQueryBody(), [$this->tBody, $this->tWhere], [$this->vBody, $this->vWhere]);
|
|
|
|
$this->jCTE = [];
|
|
|
|
$this->tBody = [];
|
|
|
|
$this->vBody = [];
|
|
|
|
$this->qWhere = [];
|
|
|
|
$this->tWhere = [];
|
|
|
|
$this->vWhere = [];
|
|
|
|
$this->order = [];
|
2017-08-29 10:50:31 -04:00
|
|
|
$this->setLimit(0, 0);
|
|
|
|
if (strlen($join)) {
|
2017-07-20 22:40:09 -04:00
|
|
|
$this->jCTE[] = $join;
|
|
|
|
}
|
2017-07-07 11:49:54 -04:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2017-08-29 10:50:31 -04:00
|
|
|
public function __toString(): string {
|
2017-06-04 18:00:18 -04:00
|
|
|
$out = "";
|
2017-08-29 10:50:31 -04:00
|
|
|
if (sizeof($this->qCTE)) {
|
2017-06-04 18:00:18 -04:00
|
|
|
// start with common table expressions
|
|
|
|
$out .= "WITH RECURSIVE ".implode(", ", $this->qCTE)." ";
|
|
|
|
}
|
|
|
|
// add the body
|
2017-06-18 10:23:37 -04:00
|
|
|
$out .= $this->buildQueryBody();
|
|
|
|
return $out;
|
|
|
|
}
|
|
|
|
|
2017-08-29 10:50:31 -04:00
|
|
|
public function getQuery(): string {
|
2017-07-07 11:49:54 -04:00
|
|
|
return $this->__toString();
|
|
|
|
}
|
|
|
|
|
2017-08-29 10:50:31 -04:00
|
|
|
public function getTypes(): array {
|
2017-07-07 11:49:54 -04:00
|
|
|
return [$this->tCTE, $this->tBody, $this->tWhere];
|
|
|
|
}
|
|
|
|
|
2017-08-29 10:50:31 -04:00
|
|
|
public function getValues(): array {
|
2017-07-07 11:49:54 -04:00
|
|
|
return [$this->vCTE, $this->vBody, $this->vWhere];
|
2017-06-18 10:23:37 -04:00
|
|
|
}
|
|
|
|
|
2017-08-29 10:50:31 -04:00
|
|
|
public function getWhereTypes(): array {
|
2017-06-18 10:23:37 -04:00
|
|
|
return $this->tWhere;
|
|
|
|
}
|
|
|
|
|
2017-08-29 10:50:31 -04:00
|
|
|
public function getWhereValues(): array {
|
2017-06-18 10:23:37 -04:00
|
|
|
return $this->vWhere;
|
|
|
|
}
|
|
|
|
|
2017-08-29 10:50:31 -04:00
|
|
|
public function getCTETypes(): array {
|
2017-06-18 10:23:37 -04:00
|
|
|
return $this->tCTE;
|
|
|
|
}
|
|
|
|
|
2017-08-29 10:50:31 -04:00
|
|
|
public function getCTEValues(): array {
|
2017-06-18 10:23:37 -04:00
|
|
|
return $this->vCTE;
|
|
|
|
}
|
|
|
|
|
|
|
|
protected function buildQueryBody(): string {
|
|
|
|
$out = "";
|
|
|
|
// add the body
|
2017-07-07 11:49:54 -04:00
|
|
|
$out .= $this->qBody;
|
2017-08-29 10:50:31 -04:00
|
|
|
if (sizeof($this->qCTE)) {
|
2017-06-04 18:00:18 -04:00
|
|
|
// add any joins against CTEs
|
|
|
|
$out .= " ".implode(" ", $this->jCTE);
|
|
|
|
}
|
|
|
|
// add any WHERE terms
|
2017-08-29 10:50:31 -04:00
|
|
|
if (sizeof($this->qWhere)) {
|
2017-06-04 18:00:18 -04:00
|
|
|
$out .= " WHERE ".implode(" AND ", $this->qWhere);
|
|
|
|
}
|
|
|
|
// add any ORDER BY terms
|
2017-08-29 10:50:31 -04:00
|
|
|
if (sizeof($this->order)) {
|
2017-06-04 18:00:18 -04:00
|
|
|
$out .= " ORDER BY ".implode(", ", $this->order);
|
|
|
|
}
|
|
|
|
// add LIMIT and OFFSET if the former is specified
|
2017-08-29 10:50:31 -04:00
|
|
|
if ($this->limit > 0) {
|
2017-06-04 18:00:18 -04:00
|
|
|
$out .= " LIMIT ".$this->limit;
|
2017-08-29 10:50:31 -04:00
|
|
|
if ($this->offset > 0) {
|
2017-06-04 18:00:18 -04:00
|
|
|
$out .= " OFFSET ".$this->offset;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return $out;
|
|
|
|
}
|
2017-08-29 10:50:31 -04:00
|
|
|
}
|