X-Git-Url: https://git.m6w6.name/?p=m6w6%2Fpq-gateway;a=blobdiff_plain;f=lib%2Fpq%2FGateway%2FRow.php;h=cda8d4af67a0ba857a6854b4f8c262f016f5e34d;hp=249337334d68e48deee130f672d02786e54e20a3;hb=b39e14404cfeac177d41b152690b6adbb2b1e4bf;hpb=7045a269b4993d414337fcd7ee1b789b15185af4 diff --git a/lib/pq/Gateway/Row.php b/lib/pq/Gateway/Row.php index 2493373..cda8d4a 100644 --- a/lib/pq/Gateway/Row.php +++ b/lib/pq/Gateway/Row.php @@ -2,6 +2,8 @@ namespace pq\Gateway; +use \pq\Query\Expr as QueryExpr; + class Row implements \JsonSerializable { /** @@ -63,8 +65,8 @@ class Row implements \JsonSerializable } /** - * Export current state with security sensitive data removed. You should override that, just - * calls export() by default. + * Export current state with security sensitive data removed. You should override that. + * Just calls export() by default. * @return array */ function exportPublic() { @@ -93,6 +95,30 @@ class Row implements \JsonSerializable return $this->data; } + /** + * Get all column/value pairs to possibly uniquely identify this row + * @return array + * @throws \OutOfBoundsException if any primary key column is not present in the row + */ + function getIdentity() { + $cols = array(); + if (count($identity = $this->getTable()->getIdentity())) { + foreach ($identity as $col) { + if (!array_key_exists($col, $this->data)) { + throw new \OutOfBoundsException( + sprintf("Column '%s' does not exist in row of table '%s'", + $col, $this->getTable()->getName() + ) + ); + } + $cols[$col] = $this->data[$col]; + } + } else { + $cols = $this->data; + } + return $cols; + } + /** * Check whether the row contains modifications * @return boolean @@ -106,6 +132,10 @@ class Row implements \JsonSerializable return false; } + /** + * Refresh the rows data + * @return \pq\Gateway\Row + */ function refresh() { $this->data = $this->table->find($this->criteria(), null, 1, 0)->current()->data; $this->cell = array(); @@ -125,13 +155,17 @@ class Row implements \JsonSerializable } /** - * Transform data array to where criteria + * Transform the row's identity to where criteria * @return array */ protected function criteria() { $where = array(); - foreach($this->data as $k => $v) { - $where["$k="] = $v; + foreach ($this->getIdentity() as $col => $val) { + if (isset($val)) { + $where["$col="] = $val; + } else { + $where["$col IS"] = new QueryExpr("NULL"); + } } return $where; } @@ -150,6 +184,18 @@ class Row implements \JsonSerializable return $changes; } + /** + * Cell accessor + * @param string $p column name + * @return \pq\Gateway\Cell + */ + protected function cell($p) { + if (!isset($this->cell[$p])) { + $this->cell[$p] = new Cell($this, $p, isset($this->data[$p]) ? $this->data[$p] : null); + } + return $this->cell[$p]; + } + /** * Get a cell or parent rows * @param string $p @@ -159,10 +205,7 @@ class Row implements \JsonSerializable if ($this->table->hasRelation($p)) { return $this->table->by($this, $p); } - if (!isset($this->cell[$p])) { - $this->cell[$p] = new Cell($this, $p, isset($this->data[$p]) ? $this->data[$p] : null); - } - return $this->cell[$p]; + return $this->cell($p); } /** @@ -171,7 +214,7 @@ class Row implements \JsonSerializable * @param mixed $v */ function __set($p, $v) { - $this->__get($p)->set($v); + $this->cell($p)->set($v); } /** @@ -210,7 +253,11 @@ class Row implements \JsonSerializable * @return \pq\Gateway\Row */ function create() { - $this->data = $this->table->create($this->changes())->current()->data; + $rowset = $this->table->create($this->changes()); + if (!count($rowset)) { + throw new \UnexpectedValueException("No row created"); + } + $this->data = $rowset->current()->data; $this->cell = array(); return $this; } @@ -220,7 +267,15 @@ class Row implements \JsonSerializable * @return \pq\Gateway\Row */ function update() { - $this->data = $this->table->update($this->criteria(), $this->changes())->current()->data; + $criteria = $this->criteria(); + if (($lock = $this->getTable()->getLock())) { + $lock->onUpdate($this, $criteria); + } + $rowset = $this->table->update($criteria, $this->changes()); + if (!count($rowset)) { + throw new \UnexpectedValueException("No row updated"); + } + $this->data = $rowset->current()->data; $this->cell = array(); return $this; } @@ -230,7 +285,11 @@ class Row implements \JsonSerializable * @return \pq\Gateway\Row */ function delete() { - $this->data = $this->table->delete($this->criteria(), "*")->current()->data; + $rowset = $this->table->delete($this->criteria(), "*"); + if (!count($rowset)) { + throw new \UnexpectedValueException("No row deleted"); + } + $this->data = $rowset->current()->data; return $this->prime(); } }