added Table::with()
[m6w6/pq-gateway] / lib / pq / Gateway / Table.php
index af84e7f5eb59ed5c0e59c054a48c86bcb6f52620..a25b2ad3fd6e07008bf59f27caa56239293756ac 100644 (file)
@@ -2,6 +2,7 @@
 
 namespace pq\Gateway;
 
+use \pq\Query\Expr as QueryExpr;
 use \pq\Query\Writer as QueryWriter;
 use \pq\Query\Executor as QueryExecutor;
 
@@ -17,6 +18,11 @@ class Table
         */
        public static $defaultResolver;
        
+       /**
+        * @var \pq\Gateway\Table\CacheInterface
+        */
+       public static $defaultMetadataCache;
+       
        /**
         * @var \pq\Connection
         */
@@ -42,11 +48,26 @@ class Table
         */
        protected $exec;
        
+       /**
+        * @var \pq\Gateway\Table\Identity
+        */
+       protected $identity;
+       
        /**
         * @var \pq\Gateway\Table\Relations
         */
        protected $relations;
        
+       /**
+        * @var \pq\Gateway\Table\CacheInterface
+        */
+       protected $metadataCache;
+       
+       /**
+        * @var \pq\Gateway\Table\LockInterface
+        */
+       protected $lock;
+       
        /**
         * @param string $table
         * @return \pq\Gateway\Table
@@ -68,11 +89,31 @@ class Table
         * @param \pq\Connection $conn
         * @param array $dependents
         */
-       function __construct($name, \pq\Connection $conn = null) {
-               $this->name = $name;
+       function __construct($name = null, \pq\Connection $conn = null) {
+               if (isset($name)) {
+                       $this->name = $name;
+               } elseif (!isset($this->name)) {
+                       throw new \InvalidArgumentException("Table must have a name");
+               }
                $this->conn = $conn ?: static::$defaultConnection ?: new \pq\Connection;
        }
        
+       /**
+        * Get the complete PostgreSQL connection string
+        * @return string
+        */
+       function __toString() {
+               return sprintf("postgresql://%s:%s@%s:%d/%s?%s#%s",
+                       $this->conn->user,
+                       $this->conn->pass,
+                       $this->conn->host,
+                       $this->conn->port,
+                       $this->conn->db,
+                       $this->conn->options,
+                       $this->getName()
+               );
+       }
+       
        /**
         * Set the rowset prototype
         * @param mixed $rowset
@@ -133,6 +174,37 @@ class Table
                return $this->exec;
        }
        
+       /**
+        * Get the metadata cache
+        * @return \pq\Gateway\Table\CacheInterface
+        */
+       function getMetadataCache() {
+               if (!isset($this->metadatCache)) {
+                       $this->metadataCache = static::$defaultMetadataCache ?: new Table\StaticCache;
+               }
+               return $this->metadataCache;
+       }
+       
+       /**
+        * Set the metadata cache
+        * @param \pq\Gateway\Table\CacheInterface $cache
+        */
+       function setMetadataCache(Table\CacheInterface $cache) {
+               $this->metadataCache = $cache;
+               return $this;
+       }
+       
+       /**
+        * Get the primary key
+        * @return \pq\Gateway\Table\Identity
+        */
+       function getIdentity() {
+               if (!isset($this->identity)) {
+                       $this->identity = new Table\Identity($this);
+               }
+               return $this->identity;
+       }
+       
        /**
         * Get foreign key relations
         * @param string $to fkey
@@ -181,13 +253,30 @@ class Table
                return $this->name;
        }
 
+       /**
+        * Set a lock provider
+        * @param \pq\Gateway\Table\LockInterface $lock
+        * @return \pq\Gateway\Table
+        */
+       function setLock(Table\LockInterface $lock) {
+               $this->lock = $lock;
+               return $this;
+       }
+       
+       /**
+        * Get any set lock provider
+        * @return \pq\Gateway\Table\LockIntferace
+        */
+       function getLock() {
+               return $this->lock;
+       }
+       
        /**
         * Execute the query
         * @param \pq\Query\WriterInterface $query
         * @return mixed
         */
        protected function execute(QueryWriter $query) {
-               echo $query,"\n",json_encode($query->getParams()),"\n";
                return $this->getQueryExecutor()->execute($query, array($this, "onResult"));
        }
 
@@ -217,9 +306,10 @@ class Table
         * @param array|string $order
         * @param int $limit
         * @param int $offset
+        * @param string $lock
         * @return mixed
         */
-       function find(array $where = null, $order = null, $limit = 0, $offset = 0) {
+       function find(array $where = null, $order = null, $limit = 0, $offset = 0, $lock = null) {
                $query = $this->getQueryWriter()->reset();
                $query->write("SELECT * FROM", $this->conn->quoteName($this->name));
                if ($where) {
@@ -231,7 +321,12 @@ class Table
                if ($limit) {
                        $query->write("LIMIT", $limit);
                }
-               $query->write("OFFSET", $offset);
+               if ($offset) {
+                       $query->write("OFFSET", $offset);
+               }
+               if ($lock) {
+                       $query->write("FOR", $lock);
+               }
                return $this->execute($query);
        }
        
@@ -248,7 +343,7 @@ class Table
                // select * from $this where $this->$foreignColumn = $foreign->$referencedColumn
                
                if (!isset($name)) {
-                       $name = $this->getName();
+                       $name = $foreign->getTable()->getName();
                }
                
                if (!$foreign->getTable()->hasRelation($name, $this->getName())) {
@@ -284,6 +379,42 @@ class Table
                        $order, $limit, $offset
                );
        }
+       
+       /**
+        * Get rows dependent on other rows by foreign keys
+        * @param array $relations
+        * @param array $where
+        * @param string $order
+        * @param int $limit
+        * @param int $offset
+        * @return mixed
+        */
+       function with(array $relations, array $where = null, $order = null, $limit = 0, $offset = 0) {
+               $qthis = $this->conn->quoteName($this->getName());
+               $query = $this->getQueryWriter()->reset();
+               $query->write("SELECT", "$qthis.*", "FROM", $qthis);
+               foreach ($relations as $relation) {
+                       $query->write("JOIN", $relation->foreignTable)->write("ON")->criteria(
+                               array(
+                                       "{$relation->referencedTable}.{$relation->referencedColumn}=" => 
+                                               new QueryExpr("{$relation->foreignTable}.{$relation->foreignColumn}")
+                               )
+                       );
+               }
+               if ($where) {
+                       $query->write("WHERE")->criteria($where);
+               }
+               if ($order) {
+                       $query->write("ORDER BY", $order);
+               }
+               if ($limit) {
+                       $query->write("LIMIT", $limit);
+               }
+               if ($offset) {
+                       $query->write("OFFSET", $offset);
+               }
+               return $this->execute($query);
+       }
 
        /**
         * Insert a row into the table