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
*/
protected $identity;
+ /**
+ * @var \pq\Gateway\Table\Attributes
+ */
+ protected $attributes;
+
/**
* @var \pq\Gateway\Table\Relations
*/
protected $metadataCache;
/**
- * @var \pq\Gateway\Table\LockInterface
+ * @var \SplObjectStorage
*/
- protected $lock;
+ protected $observers;
/**
* @param string $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;
}
/**
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
}
/**
- * 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);
+ }
}
/**
$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
$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, ")");
$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);