use standard SplObserver
[m6w6/pq-gateway] / lib / pq / Gateway / Table.php
index a25b2ad3fd6e07008bf59f27caa56239293756ac..42977dc0d6c9d7b555afe9c42e5283a2246b5319 100644 (file)
@@ -6,7 +6,7 @@ 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
@@ -53,6 +53,11 @@ class Table
         */
        protected $identity;
        
+       /**
+        * @var \pq\Gateway\Table\Attributes
+        */
+       protected $attributes;
+       
        /**
         * @var \pq\Gateway\Table\Relations
         */
@@ -64,9 +69,9 @@ class Table
        protected $metadataCache;
        
        /**
-        * @var \pq\Gateway\Table\LockInterface
+        * @var \SplObjectStorage
         */
-       protected $lock;
+       protected $observers;
        
        /**
         * @param string $table
@@ -96,6 +101,7 @@ class Table
                        throw new \InvalidArgumentException("Table must have a name");
                }
                $this->conn = $conn ?: static::$defaultConnection ?: new \pq\Connection;
+               $this->observers = new \SplObjectStorage;
        }
        
        /**
@@ -205,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
@@ -254,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);
+               }
        }
        
        /**
@@ -430,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, ")");
@@ -456,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);