Browse Source

Remove most unused features of the query builder

Experience has proved programmatically setting joins is not useful, and
getting the types and values of query parts was not being maintained.

The programmatic setting of GROUP BY may be useful in future, however.
microsub
J. King 5 years ago
parent
commit
92b1626dba
  1. 71
      lib/Misc/Query.php

71
lib/Misc/Query.php

@ -13,10 +13,6 @@ class Query {
protected $qCTE = []; // Common table expression query components protected $qCTE = []; // Common table expression query components
protected $tCTE = []; // Common table expression type bindings protected $tCTE = []; // Common table expression type bindings
protected $vCTE = []; // Common table expression binding values protected $vCTE = []; // Common table expression binding values
protected $jCTE = []; // Common Table Expression joins
protected $qJoin = []; // JOIN clause components
protected $tJoin = []; // JOIN clause type bindings
protected $vJoin = []; // JOIN clause binding values
protected $qWhere = []; // WHERE clause components protected $qWhere = []; // WHERE clause components
protected $tWhere = []; // WHERE clause type bindings protected $tWhere = []; // WHERE clause type bindings
protected $vWhere = []; // WHERE clause binding values protected $vWhere = []; // WHERE clause binding values
@ -42,24 +38,12 @@ class Query {
return true; return true;
} }
public function setCTE(string $tableSpec, string $body, $types = null, $values = null, string $join = ''): bool { public function setCTE(string $tableSpec, string $body, $types = null, $values = null): bool {
$this->qCTE[] = "$tableSpec as ($body)"; $this->qCTE[] = "$tableSpec as ($body)";
if (!is_null($types)) { if (!is_null($types)) {
$this->tCTE[] = $types; $this->tCTE[] = $types;
$this->vCTE[] = $values; $this->vCTE[] = $values;
} }
if (strlen($join)) { // the CTE might only participate in subqueries rather than a join on the main query
$this->jCTE[] = $join;
}
return true;
}
public function setJoin(string $join, $types = null, $values = null): bool {
$this->qJoin[] = $join;
if (!is_null($types)) {
$this->tJoin[] = $types;
$this->vJoin[] = $values;
}
return true; return true;
} }
@ -88,12 +72,8 @@ class Query {
return true; return true;
} }
public function setOrder(string $order, bool $prepend = false): bool { public function setOrder(string $order): bool {
if ($prepend) { $this->order[] = $order;
array_unshift($this->order, $order);
} else {
$this->order[] = $order;
}
return true; return true;
} }
@ -103,11 +83,10 @@ class Query {
return true; return true;
} }
public function pushCTE(string $tableSpec, string $join = ''): bool { 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 // 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 // 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->setCTE($tableSpec, $this->buildQueryBody(), [$this->tBody, $this->tWhere, $this->tWhereNot], [$this->vBody, $this->vWhere, $this->vWhereNot]);
$this->jCTE = [];
$this->tBody = []; $this->tBody = [];
$this->vBody = []; $this->vBody = [];
$this->qWhere = []; $this->qWhere = [];
@ -116,15 +95,9 @@ class Query {
$this->qWhereNot = []; $this->qWhereNot = [];
$this->tWhereNot = []; $this->tWhereNot = [];
$this->vWhereNot = []; $this->vWhereNot = [];
$this->qJoin = [];
$this->tJoin = [];
$this->vJoin = [];
$this->order = []; $this->order = [];
$this->group = []; $this->group = [];
$this->setLimit(0, 0); $this->setLimit(0, 0);
if (strlen($join)) {
$this->jCTE[] = $join;
}
return true; return true;
} }
@ -144,49 +117,17 @@ class Query {
} }
public function getTypes(): array { public function getTypes(): array {
return [$this->tCTE, $this->tBody, $this->tJoin, $this->tWhere, $this->tWhereNot]; return [$this->tCTE, $this->tBody, $this->tWhere, $this->tWhereNot];
} }
public function getValues(): array { public function getValues(): array {
return [$this->vCTE, $this->vBody, $this->vJoin, $this->vWhere, $this->vWhereNot]; return [$this->vCTE, $this->vBody, $this->vWhere, $this->vWhereNot];
}
public function getJoinTypes(): array {
return $this->tJoin;
}
public function getJoinValues(): array {
return $this->vJoin;
}
public function getWhereTypes(): array {
return $this->tWhere;
}
public function getWhereValues(): array {
return $this->vWhere;
}
public function getCTETypes(): array {
return $this->tCTE;
}
public function getCTEValues(): array {
return $this->vCTE;
} }
protected function buildQueryBody(): string { protected function buildQueryBody(): string {
$out = ""; $out = "";
// add the body // add the body
$out .= $this->qBody; $out .= $this->qBody;
if (sizeof($this->qCTE)) {
// add any joins against CTEs
$out .= " ".implode(" ", $this->jCTE);
}
// add any JOINs
if (sizeof($this->qJoin)) {
$out .= " ".implode(" ", $this->qJoin);
}
// 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); $where = implode(" AND ", $this->qWhere);

Loading…
Cancel
Save