autoload; cache; tests;
[m6w6/pq-gateway] / lib / pq / Gateway / Table.php
index af84e7f5eb59ed5c0e59c054a48c86bcb6f52620..8fc85d7ad748ff68d816aa607afdf9fdfbc8cec1 100644 (file)
@@ -17,6 +17,11 @@ class Table
         */
        public static $defaultResolver;
        
+       /**
+        * @var \pq\Gateway\Table\CacheInterface
+        */
+       public static $defaultMetadataCache;
+       
        /**
         * @var \pq\Connection
         */
@@ -47,6 +52,11 @@ class Table
         */
        protected $relations;
        
+       /**
+        * @var \pq\Gateway\Table\CacheInterface
+        */
+       protected $metadataCache;
+       
        /**
         * @param string $table
         * @return \pq\Gateway\Table
@@ -73,6 +83,22 @@ class Table
                $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 +159,26 @@ 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 foreign key relations
         * @param string $to fkey
@@ -187,7 +233,6 @@ class Table
         * @return mixed
         */
        protected function execute(QueryWriter $query) {
-               echo $query,"\n",json_encode($query->getParams()),"\n";
                return $this->getQueryExecutor()->execute($query, array($this, "onResult"));
        }