added Table::with()
[m6w6/pq-gateway] / lib / pq / Gateway / Table / PessimisticLock.php
1 <?php
2
3 namespace pq\Gateway\Table;
4
5 use \pq\Gateway\Row;
6
7 /**
8 * A pessimistic row lock implementation using an additional SELECT FOR UPDATE
9 */
10 class PessimisticLock implements LockInterface
11 {
12 /**
13 * @inheritdoc
14 * @param \pq\Gateway\Row $row
15 * @param array $ignore
16 * @throws \UnexpectedValueException if the row has already been modified
17 */
18 function onUpdate(Row $row, array &$ignore) {
19 $where = array();
20 foreach ($row->getIdentity() as $col => $val) {
21 if (isset($val)) {
22 $where["$col="] = $val;
23 } else {
24 $where["$col IS"] = new QueryExpr("NULL");
25 }
26 }
27
28 if (1 != count($rowset = $row->getTable()->find($where, null, 0, 0, "update nowait"))) {
29 throw new \UnexpectedValueException("Failed to select a single row");
30 }
31 if ($rowset->current()->getData() != $row->getData()) {
32 throw new \UnexpectedValueException("Row has already been modified");
33 }
34 }
35 }