tests
[m6w6/pq-gateway] / lib / pq / Gateway / Table.php
index 295e7a2c60f5db4aa7c5dccd607d07c401c36e5c..ba8094ab0fba87d766b55120df7282f87ac24a46 100644 (file)
@@ -9,7 +9,12 @@ class Table
        /**
         * @var \pq\Connection
         */
-       protected $connection;
+       public static $defaultConnection;
+       
+       /**
+        * @var \pq\Connection
+        */
+       protected $conn;
 
        /**
         * @var string
@@ -19,52 +24,63 @@ class Table
        /**
         * @var string
         */
-       protected $rowset;
+       protected $rowset = "\\pq\\Gateway\\Rowset";
 
        /**
-        * @param \pq\Connection $c
         * @param string $name
+        * @param \pq\Connection $conn
         */
-       function __construct(\pq\Connection $c, $name, $rowset = "\\pq\\Gateway\\Rowset") {
-               $this->connection = $c;
+       function __construct($name, \pq\Connection $conn = null) {
                $this->name = $name;
+               $this->conn = $conn ?: static::$defaultConnection ?: new \pq\Connection;
+       }
+       
+       /**
+        * Set the rowset prototype
+        * @param mixed $rowset
+        * @return \pq\Gateway\Table
+        */
+       function setRowsetPrototype($rowset) {
                $this->rowset = $rowset;
+               return $this;
        }
        
        /**
-        * Accessor to read-only properties
-        * @param string $p
+        * @return \pq\Connection
         */
-       function __get($p) {
-               return $this->$p;
+       function getConnection() {
+               return $this->conn;
+       }
+       
+       /**
+        * @return string
+        */
+       function getName() {
+               return $this->name;
        }
 
        /**
+        * Execute the query
         * @param \pq\Query\Writer $query
-        * @param array $criteria
-        * @param string $join
+        * @return mixed
         */
-       protected function criteria(QueryWriter $query, array $criteria, $join = "AND") {
-               $joinable = false;
-               $query->write("(");
-               foreach ($criteria as $lop => $rop) {
-                       if (is_array($rop)) {
-                               if ($joinable) {
-                                       $query->write(")", $join, "(");
-                               }
-                               $this->criteria($query, $rop, is_int($lop) ? "AND" : $lop);
-                       } else {
-                               if ($joinable) {
-                                       $query->write(")", $join, "(");
-                               }
-                               if (!is_int($lop)) {
-                                       $query->write($lop);
-                               }
-                               $query->write($query->param($rop));
-                       }
-                       $joinable or $joinable = true;
+       protected function execute(QueryWriter $query) {
+               $result = $query->exec($this->conn);
+               
+               if ($result->status != \pq\Result::TUPLES_OK) {
+                       return $result;
+               }
+               
+               if (is_callable($this->rowset)) {
+                       return call_user_func($this->rowset, $result);
+               }
+               
+               if ($this->rowset) {
+                       $rowset = $this->rowset;
+                       return new $rowset($this, $result);
                }
-               $query->write(")");
+               
+               return $result;
        }
 
        /**
@@ -76,9 +92,9 @@ class Table
         * @return \pq\Result
         */
        function find(array $where = null, $order = null, $limit = 0, $offset = 0) {
-               $query = new QueryWriter("SELECT * FROM ". $this->connection->quoteName($this->name));
+               $query = new QueryWriter("SELECT * FROM ". $this->conn->quoteName($this->name));
                if ($where) {
-                       $this->criteria($query->write("WHERE"), $where);
+                       $query->write("WHERE")->criteria($where);
                }
                if ($order) {
                        $query->write("ORDER BY", $order);
@@ -87,7 +103,7 @@ class Table
                        $query->write("LIMIT", $limit);
                }
                $query->write("OFFSET", $offset);
-               return new Rowset($this, $query->exec($this->connection));
+               return $this->execute($query);
        }
 
        /**
@@ -96,23 +112,25 @@ class Table
         * @param string $returning
         * @return \pq\Result
         */
-       function create(array $data, $returning = "*") {
-               $params = array();
-               $query = new QueryWriter("INSERT INTO ".$this->connection->quoteName($this->name)." (");
-               foreach ($data as $key => $val) {
-                       $query->write($key);
-                       $params[] = $query->param($val);
+       function create(array $data = null, $returning = "*") {
+               $query = new QueryWriter("INSERT INTO ".$this->conn->quoteName($this->name));
+               if ($data) {
+                       $first = true;
+                       $params = array();
+                       foreach ($data as $key => $val) {
+                               $query->write($first ? "(" : ",", $key);
+                               $params[] = $query->param($val);
+                               $first and $first = false;
+                       }
+                       $query->write(") VALUES (", $params, ")");
+               } else {
+                       $query->write("DEFAULT VALUES");
                }
-               $query->write(") VALUES (", $params, ")");
+               
                if (strlen($returning)) {
                        $query->write("RETURNING", $returning);
                }
-               $result = $query->exec($this->connection);
-               if ($result->status == \pq\Result::TUPLES_OK) {
-                       $rowset = $this->rowset;
-                       return new $rowset($this, $result);
-               }
-               return $result;
+               return $this->execute($query);
        }
 
        /**
@@ -123,20 +141,17 @@ class Table
         * @retunr \pq\Result
         */
        function update(array $where, array $data, $returning = "*") {
-               $query = new QueryWriter("UPDATE ".$this->connection->quoteName($this->name)." SET");
+               $query = new QueryWriter("UPDATE ".$this->conn->quoteName($this->name));
+               $first = true;
                foreach ($data as $key => $val) {
-                       $query->write($key, "=", $query->param($val));
+                       $query->write($first ? "SET" : ",", $key, "=", $query->param($val));
+                       $first and $first = false;
                }
-               $this->criteria($query->write("WHERE"), $where);
+               $query->write("WHERE")->criteria($where);
                if (strlen($returning)) {
                        $query->write("RETURNING", $returning);
                }
-               $result = $query->exec($this->connection);
-               if ($result->status == \pq\Result::TUPLES_OK) {
-                       $rowset = $this->rowset;
-                       return new $rowset($this, $result);
-               }
-               return $result;
+               return $this->execute($query);
        }
 
        /**
@@ -146,17 +161,11 @@ class Table
         * @return pq\Result
         */
        function delete(array $where, $returning = null) {
-               $query = new QueryWriter("DELETE FROM ".$this->connection->quoteName($this->name));
-               $this->criteria($query->write("WHERE"), $where);
+               $query = new QueryWriter("DELETE FROM ".$this->conn->quoteName($this->name));
+               $query->write("WHERE")->criteria($where);
                if (strlen($returning)) {
                        $query->write("RETURNING", $returning);
                }
-               $result = $query->exec($this->connection);
-               if ($result->status == \pq\Result::TUPLES_OK) {
-                       $rowset = $this->rowset;
-                       return new $rowset($this, $result);
-               }
-               return $result;
+               return $this->execute($query);
        }
 }
-