flush
authorMichael Wallner <mike@php.net>
Thu, 7 Mar 2013 09:10:44 +0000 (10:10 +0100)
committerMichael Wallner <mike@php.net>
Thu, 7 Mar 2013 09:10:44 +0000 (10:10 +0100)
lib/pq/Gateway/Row.php
lib/pq/Gateway/Rowset.php
lib/pq/Gateway/Table.php
lib/pq/Query/Writer.php
lib/pq/Query/WriterInterface.php [new file with mode: 0644]

index a8b879370f347b4ad494edd78d99a25d7b079328..0a2820aa612b5462fd537a214d0c3bff63ef4e83 100644 (file)
@@ -26,7 +26,7 @@ class Row implements \JsonSerializable
         */
        function __construct(Table $table, array $data = null, $prime = false) {
                $this->table = $table;
-               $this->data = $data;
+               $this->data = (array) $data;
                
                if ($prime) {
                        $this->prime();
index d6134caaa314de1bfee7ba4a620fd98322cacd26..7c0a3c23395f56428d9c1506dfdc5f86ef089459 100644 (file)
@@ -38,7 +38,7 @@ class Rowset implements \SeekableIterator, \Countable, \JsonSerializable
         * @param \pq\Result $result
         * @return \pq\Gateway\Rowset
         */
-       function __invoke(\pq\Result $result) {
+       function __invoke(\pq\Result $result = null) {
                $that = clone $this;
                $that->hydrate($result);
                return $that;
@@ -54,7 +54,7 @@ class Rowset implements \SeekableIterator, \Countable, \JsonSerializable
                $this->rows  = array();
                
                if ($result) {
-                       $row = $this->row;
+                       $row = $this->getRowPrototype();
 
                        if (is_callable($row)) {
                                while (($data = $result->fetchRow(\pq\Result::FETCH_ASSOC))) {
@@ -82,6 +82,14 @@ class Rowset implements \SeekableIterator, \Countable, \JsonSerializable
                return $this;
        }
        
+       /**
+        * Get the row prototype
+        * @return mixed
+        */
+       function getRowPrototype() {
+               return $this->row;
+       }
+       
        /**
         * @return \pq\Gateway\Table
         */
@@ -176,12 +184,14 @@ class Rowset implements \SeekableIterator, \Countable, \JsonSerializable
        function rewind() {
                $this->index = 0;
        }
+       
        /**
         * @implements \Iterator
         */
        function next() {
                ++$this->index;
        }
+       
        /**
         * @implements \Iterator
         * @return bool
@@ -189,13 +199,18 @@ class Rowset implements \SeekableIterator, \Countable, \JsonSerializable
        function valid() {
                return $this->index < count($this->rows);
        }
+       
        /**
         * @implements \Iterator
         * @return \pq\Gateway\Row
         */
        function current() {
+               if (!$this->valid()) {
+                       throw new OutOfBoundsException("Invalid row index {$this->index}");
+               }
                return $this->rows[$this->index];
        }
+       
        /**
         * @implements \Iterator
         * @return int
@@ -252,6 +267,7 @@ class Rowset implements \SeekableIterator, \Countable, \JsonSerializable
         */
        function filter(callable $cb) {
                $rowset = clone $this;
+               $rowset->index = 0;
                $rowset->rows = array_filter($this->rows, $cb);
                return $rowset;
        }
index ba8094ab0fba87d766b55120df7282f87ac24a46..ebab838bf672b8c01f2229720644a617038a17f0 100644 (file)
@@ -25,6 +25,11 @@ class Table
         * @var string
         */
        protected $rowset = "\\pq\\Gateway\\Rowset";
+       
+       /**
+        * @var \pq\Query\Writer
+        */
+       protected $query;
 
        /**
         * @param string $name
@@ -45,6 +50,32 @@ class Table
                return $this;
        }
        
+       /**
+        * Get the rowset prototype
+        * @return mixed
+        */
+       function getRowsetPrototype() {
+               return $this->rowset;
+       }
+       
+       /**
+        * Set the query writer
+        * @param \pq\Query\WriterInterface $query
+        * @return \pq\Gateway\Table
+        */
+       function setQueryWriter(\pq\Query\WriterInterface $query) {
+               $this->query = $query;
+               return $this;
+       }
+       
+       /**
+        * Get the query writer
+        * @return \pq\Query\WriterInterface
+        */
+       function getQueryWriter() {
+               return $this->query ?: new QueryWriter;
+       }
+       
        /**
         * @return \pq\Connection
         */
@@ -71,12 +102,10 @@ class Table
                        return $result;
                }
                
-               if (is_callable($this->rowset)) {
-                       return call_user_func($this->rowset, $result);
-               }
-               
-               if ($this->rowset) {
-                       $rowset = $this->rowset;
+               $rowset = $this->getRowsetPrototype();
+               if (is_callable($rowset)) {
+                       return $rowset($result);
+               } elseif ($rowset) {
                        return new $rowset($this, $result);
                }
                
@@ -92,7 +121,8 @@ class Table
         * @return \pq\Result
         */
        function find(array $where = null, $order = null, $limit = 0, $offset = 0) {
-               $query = new QueryWriter("SELECT * FROM ". $this->conn->quoteName($this->name));
+               $query = $this->getQueryWriter()->reset();
+               $query->write("SELECT * FROM", $this->conn->quoteName($this->name));
                if ($where) {
                        $query->write("WHERE")->criteria($where);
                }
@@ -113,7 +143,8 @@ class Table
         * @return \pq\Result
         */
        function create(array $data = null, $returning = "*") {
-               $query = new QueryWriter("INSERT INTO ".$this->conn->quoteName($this->name));
+               $query = $this->getQueryWriter()->reset();
+               $query->write("INSERT INTO", $this->conn->quoteName($this->name));
                if ($data) {
                        $first = true;
                        $params = array();
@@ -141,7 +172,8 @@ class Table
         * @retunr \pq\Result
         */
        function update(array $where, array $data, $returning = "*") {
-               $query = new QueryWriter("UPDATE ".$this->conn->quoteName($this->name));
+               $query = $this->getQueryWriter()->reset();
+               $query->write("UPDATE", $this->conn->quoteName($this->name));
                $first = true;
                foreach ($data as $key => $val) {
                        $query->write($first ? "SET" : ",", $key, "=", $query->param($val));
@@ -161,7 +193,8 @@ class Table
         * @return pq\Result
         */
        function delete(array $where, $returning = null) {
-               $query = new QueryWriter("DELETE FROM ".$this->conn->quoteName($this->name));
+               $query = $this->getQueryWriter()->reset();
+               $query->write("DELETE FROM", $this->conn->quoteName($this->name));
                $query->write("WHERE")->criteria($where);
                if (strlen($returning)) {
                        $query->write("RETURNING", $returning);
index f326f1ebc86a4e3d4d6808e04f4da3a08dcff66b..fd5e51aab692e42ec3995869b9321e863a645053 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
diff --git a/lib/pq/Query/WriterInterface.php b/lib/pq/Query/WriterInterface.php
new file mode 100644 (file)
index 0000000..32e133b
--- /dev/null
@@ -0,0 +1,15 @@
+<?php
+
+namespace pq\Query;
+
+interface WriterInterface
+{
+       function __toString();
+       function getParams();
+       function getTypes();
+       function write(/*...*/);
+       function param($param, $type = null);
+       function criteria(array $criteria);
+       function reset();
+       function exec(\pq\Connection $c);
+}