use standard SplObserver
[m6w6/pq-gateway] / lib / pq / Gateway / Table.php
index fe5efbdd454309b2efa15fbdddb21d1f9a1a7a3e..42977dc0d6c9d7b555afe9c42e5283a2246b5319 100644 (file)
@@ -2,10 +2,11 @@
 
 namespace pq\Gateway;
 
+use \pq\Query\Expr as QueryExpr;
 use \pq\Query\Writer as QueryWriter;
 use \pq\Query\Executor as QueryExecutor;
 
-class Table
+class Table implements \SplSubject
 {
        /**
         * @var \pq\Connection
@@ -52,6 +53,11 @@ class Table
         */
        protected $identity;
        
+       /**
+        * @var \pq\Gateway\Table\Attributes
+        */
+       protected $attributes;
+       
        /**
         * @var \pq\Gateway\Table\Relations
         */
@@ -63,9 +69,9 @@ class Table
        protected $metadataCache;
        
        /**
-        * @var \pq\Gateway\Table\LockInterface
+        * @var \SplObjectStorage
         */
-       protected $lock;
+       protected $observers;
        
        /**
         * @param string $table
@@ -88,9 +94,14 @@ 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;
+               $this->observers = new \SplObjectStorage;
        }
        
        /**
@@ -200,6 +211,13 @@ class Table
                return $this->identity;
        }
        
+       function getAttributes() {
+               if (!isset($this->attributes)) {
+                       $this->attributes = new Table\Attributes($this);
+               }
+               return $this->attributes;
+       }
+       
        /**
         * Get foreign key relations
         * @param string $to fkey
@@ -249,21 +267,32 @@ class Table
        }
 
        /**
-        * Set a lock provider
-        * @param \pq\Gateway\Table\LockInterface $lock
+        * Attach an observer
+        * @param \SplObserver
         * @return \pq\Gateway\Table
         */
-       function setLock(Table\LockInterface $lock) {
-               $this->lock = $lock;
+       function attach(\SplObserver $observer) {
+               $this->observers->attach($observer);
                return $this;
        }
        
        /**
-        * Get any set lock provider
-        * @return \pq\Gateway\Table\LockIntferace
+        * Detach an observer
+        * @param \SplObserver
+        * @return \pq\Gateway\Table
         */
-       function getLock() {
-               return $this->lock;
+       function detach(\SplObserver $observer) {
+               $this->observers->attach($observer);
+               return $this;
+       }
+
+       /**
+        * Implements \SplSubject
+        */
+       function notify(\pq\Gateway\Row $row = null, $event = null, array &$where = null) {
+               foreach ($this->observers as $observer) {
+                       $observer->update($this, $row, $event, $where);
+               }
        }
        
        /**
@@ -374,6 +403,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
@@ -389,7 +454,7 @@ class Table
                        $params = array();
                        foreach ($data as $key => $val) {
                                $query->write($first ? "(" : ",", $key);
-                               $params[] = $query->param($val);
+                               $params[] = $query->param($val, $this->getAttributes()->getColumn($key)->type);
                                $first and $first = false;
                        }
                        $query->write(") VALUES (", $params, ")");
@@ -415,7 +480,8 @@ class Table
                $query->write("UPDATE", $this->conn->quoteName($this->name));
                $first = true;
                foreach ($data as $key => $val) {
-                       $query->write($first ? "SET" : ",", $key, "=", $query->param($val));
+                       $query->write($first ? "SET" : ",", $key, "=", 
+                               $query->param($val, $this->getAttributes()->getColumn($key)->type));
                        $first and $first = false;
                }
                $query->write("WHERE")->criteria($where);