1
1
Fork 0
mirror of https://code.mensbeam.com/MensBeam/Arsse.git synced 2024-12-23 17:12:41 +00:00
Arsse/lib/Misc/Query.php

157 lines
5.2 KiB
PHP
Raw Normal View History

<?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 Query {
protected $qBody = ""; // main query body
protected $tBody = []; // main query parameter types
protected $vBody = []; // main query parameter values
protected $qCTE = []; // Common table expression query components
protected $tCTE = []; // Common table expression type bindings
protected $vCTE = []; // Common table expression binding 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 $order = []; // ORDER BY clause components
protected $limit = 0;
protected $offset = 0;
2017-08-29 14:50:31 +00:00
public function __construct(string $body = "", $types = null, $values = null) {
$this->setBody($body, $types, $values);
}
2017-08-29 14:50:31 +00:00
public function setBody(string $body = "", $types = null, $values = null): bool {
$this->qBody = $body;
2017-08-29 14:50:31 +00:00
if (!is_null($types)) {
$this->tBody[] = $types;
$this->vBody[] = $values;
}
return true;
}
public function setCTE(string $tableSpec, string $body, $types = null, $values = null): bool {
$this->qCTE[] = "$tableSpec as ($body)";
2017-08-29 14:50:31 +00:00
if (!is_null($types)) {
$this->tCTE[] = $types;
$this->vCTE[] = $values;
}
return true;
}
2017-08-29 14:50:31 +00:00
public function setWhere(string $where, $types = null, $values = null): bool {
$this->qWhere[] = $where;
2017-08-29 14:50:31 +00:00
if (!is_null($types)) {
$this->tWhere[] = $types;
$this->vWhere[] = $values;
}
return true;
}
public function setWhereNot(string $where, $types = null, $values = null): bool {
$this->qWhereNot[] = $where;
if (!is_null($types)) {
$this->tWhereNot[] = $types;
$this->vWhereNot[] = $values;
}
return true;
}
public function setGroup(string ...$column): bool {
foreach ($column as $col) {
$this->group[] = $col;
}
return true;
}
public function setOrder(string $order): bool {
$this->order[] = $order;
return true;
}
2017-08-29 14:50:31 +00:00
public function setLimit(int $limit, int $offset = 0): bool {
$this->limit = $limit;
$this->offset = $offset;
return true;
}
public function pushCTE(string $tableSpec): bool {
// 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->tWhereNot], [$this->vBody, $this->vWhere, $this->vWhereNot]);
$this->tBody = [];
$this->vBody = [];
$this->qWhere = [];
$this->tWhere = [];
$this->vWhere = [];
2019-02-26 16:11:42 +00:00
$this->qWhereNot = [];
$this->tWhereNot = [];
$this->vWhereNot = [];
$this->order = [];
$this->group = [];
2017-08-29 14:50:31 +00:00
$this->setLimit(0, 0);
return true;
}
2017-08-29 14:50:31 +00:00
public function __toString(): string {
$out = "";
2017-08-29 14:50:31 +00:00
if (sizeof($this->qCTE)) {
// start with common table expressions
$out .= "WITH RECURSIVE ".implode(", ", $this->qCTE)." ";
}
// add the body
$out .= $this->buildQueryBody();
return $out;
}
2017-08-29 14:50:31 +00:00
public function getQuery(): string {
return $this->__toString();
}
2017-08-29 14:50:31 +00:00
public function getTypes(): array {
return [$this->tCTE, $this->tBody, $this->tWhere, $this->tWhereNot];
}
2017-08-29 14:50:31 +00:00
public function getValues(): array {
return [$this->vCTE, $this->vBody, $this->vWhere, $this->vWhereNot];
}
protected function buildQueryBody(): string {
$out = "";
// add the body
$out .= $this->qBody;
// add any WHERE terms
if (sizeof($this->qWhere) || sizeof($this->qWhereNot)) {
$where = implode(" AND ", $this->qWhere);
$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
if (sizeof($this->group)) {
$out .= " GROUP BY ".implode(", ", $this->group);
}
// add any ORDER BY terms
2017-08-29 14:50:31 +00:00
if (sizeof($this->order)) {
$out .= " ORDER BY ".implode(", ", $this->order);
}
// add LIMIT and OFFSET if the former is specified
2017-08-29 14:50:31 +00:00
if ($this->limit > 0) {
$out .= " LIMIT ".$this->limit;
2017-08-29 14:50:31 +00:00
if ($this->offset > 0) {
$out .= " OFFSET ".$this->offset;
}
}
return $out;
}
2017-08-29 14:50:31 +00:00
}