add identity and lock
[m6w6/pq-gateway] / lib / pq / Query / Writer.php
index bfd7094715d76a175a99b8fc7a32783a287ebd8d..b5f3ab2176d8d9fe4d0999060fc01ee206bc30d2 100644 (file)
@@ -5,7 +5,7 @@ namespace pq\Query;
 /**
  * A very simple query writer used by \pq\Gateway
  */
-class Writer
+class Writer implements WriterInterface
 {
        /**
         * @var string
@@ -48,7 +48,13 @@ class Writer
         * @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;
        }
 
        /**
@@ -83,6 +89,9 @@ class Writer
         * @return \pq\Query\Writer
         */
        function write() {
+               if (strlen($this->query)) {
+                       $this->query .= " ";
+               }
                $this->query .= array_reduce(func_get_args(), array($this, "reduce"));
                return $this;
        }
@@ -94,10 +103,11 @@ class Writer
         * @return string
         */
        function param($param, $type = null) {
+               if ($param instanceof ExpressibleInterface) {
+                       $param = $param->get();
+               }
                if ($param instanceof Expr) {
                        return (string) $param;
-               } else {
-                       var_dump($param);
                }
                
                $this->params[] = $param;
@@ -113,14 +123,13 @@ class 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) {
+                       while ((list($left, $right) = each($criteria))) {
                                $this->write(is_int($left) && is_array($right) ? "OR" : "AND");
                                if (is_array($right)) {
                                        $this->criteria($right);
@@ -132,15 +141,4 @@ class Writer
                }
                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);
-       }
 }