@ -13,10 +13,6 @@ class Query {
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 $qJoin = []; // JOIN clause components
protected $tJoin = []; // JOIN clause type bindings
protected $vJoin = []; // JOIN clause binding values
protected $qWhere = []; // WHERE clause components
protected $tWhere = []; // WHERE clause type bindings
protected $vWhere = []; // WHERE clause binding values
@ -42,24 +38,12 @@ class Query {
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)";
if (!is_null($types)) {
$this->tCTE[] = $types;
$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;
}
@ -88,12 +72,8 @@ class Query {
return true;
}
public function setOrder(string $order, bool $prepend = false): bool {
if ($prepend) {
array_unshift($this->order, $order);
} else {
$this->order[] = $order;
}
public function setOrder(string $order): bool {
$this->order[] = $order;
return true;
}
@ -103,11 +83,10 @@ class Query {
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
// 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->jCTE = [];
$this->tBody = [];
$this->vBody = [];
$this->qWhere = [];
@ -116,15 +95,9 @@ class Query {
$this->qWhereNot = [];
$this->tWhereNot = [];
$this->vWhereNot = [];
$this->qJoin = [];
$this->tJoin = [];
$this->vJoin = [];
$this->order = [];
$this->group = [];
$this->setLimit(0, 0);
if (strlen($join)) {
$this->jCTE[] = $join;
}
return true;
}
@ -144,49 +117,17 @@ class Query {
}
public function getTypes(): array {
return [$this->tCTE, $this->tBody, $this->tJoin, $this->t Where, $this->tWhereNot];
return [$this->tCTE, $this->tBody, $this->tWhere, $this->tWhereNot];
}
public function getValues(): array {
return [$this->vCTE, $this->vBody, $this->vJoin, $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;
return [$this->vCTE, $this->vBody, $this->vWhere, $this->vWhereNot];
}
protected function buildQueryBody(): string {
$out = "";
// add the body
$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
if (sizeof($this->qWhere) || sizeof($this->qWhereNot)) {
$where = implode(" AND ", $this->qWhere);