query = $query; $this->params = $params; $this->types = $types; } /** * Get the query string * @return string */ function __toString() { return $this->query; } /** * Reduce arguments to write() * @param string $q * @param mixed $v * @return string */ protected function reduce($q, $v) { return $q . " " . (is_array($v) ? implode(", ", $v) : $v); } /** * Get the query params * @return array */ function getParams() { return $this->params; } /** * Get the param types * @return array */ function getTypes() { return $this->types; } /** * Reset * @return \pq\Query\Writer */ function reset() { $this->query = ""; $this->params = array(); $this->types = array(); return $this; } /** * Append to the query string * @return \pq\Query\Writer */ function write() { $this->query .= array_reduce(func_get_args(), array($this, "reduce")); return $this; } /** * Write a param placeholder and push the param onto the param list * @param mixed $param * @param string $type * @return string */ function param($param, $type = null) { if ($param instanceof Expr) { return (string) $param; } else { var_dump($param); } $this->params[] = $param; $this->types[] = $type; return "\$".count($this->params); } /** * Write nested AND/OR criteria * @param array $criteria * @return \pq\Query\Writer */ function criteria(array $criteria) { if ((list($left, $right) = each($criteria))) { array_shift($criteria); $this->write("("); if (is_array($right)) { $this->criteria($right); } else { $this->write("(", $left, $this->param($right), ")"); } foreach ($criteria as $left => $right) { $this->write(is_int($left) && is_array($right) ? "OR" : "AND"); if (is_array($right)) { $this->criteria($right); } else { $this->write("(", $left, $this->param($right), ")"); } } $this->write(")"); } return $this; } /** * Execute the query through \pq\Connection::execParams($this, $this->params, $this->types) * @param \pq\Connection $c * @return \pq\Result */ function exec(\pq\Connection $c) { fprintf(STDERR, "Q: %s\n", $this); fprintf(STDERR, "P: %s\n", implode(", ", $this->params)); return $c->execParams($this, $this->params, $this->types); } }