/**
* A very simple query writer used by \pq\Gateway
*/
-class Writer
+class Writer implements WriterInterface
{
/**
* @var string
* @return string
*/
protected function reduce($q, $v) {
- return $q . " " . (is_array($v) ? implode(", ", $v) : $v);
+ if (is_array($v)) {
+ $v = implode(", ", $v);
+ }
+ if (strlen($q)) {
+ $q .= " ";
+ }
+ return $q . $v;
}
/**
* @return \pq\Query\Writer
*/
function write() {
+ if (strlen($this->query)) {
+ $this->query .= " ";
+ }
$this->query .= array_reduce(func_get_args(), array($this, "reduce"));
return $this;
}
* @return string
*/
function param($param, $type = null) {
+ if ($param instanceof ExpressibleInterface) {
+ $param = $param->get();
+ }
if ($param instanceof Expr) {
return (string) $param;
}
*/
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) {
+ while ((list($left, $right) = each($criteria))) {
$this->write(is_int($left) && is_array($right) ? "OR" : "AND");
if (is_array($right)) {
$this->criteria($right);
}
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);
- }
}