use standard SplObserver
[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 \SplObserver
11 {
12 /**
13 * @param \pq\Gateway\Table $table
14 * @param \pq\Gateway\Row $row
15 * @param string $event create/update/delete
16 * @param array $where reference to the criteria
17 * @throws \UnexpectedValueException if the row has already been modified
18 */
19 function update(\SplSubject $table, Row $row = null, $event = null, array &$where = null) {
20 if ($event === "update") {
21 if (1 != count($rowset = $table->find($where, null, 0, 0, "update nowait"))) {
22 throw new \UnexpectedValueException("Failed to select a single row");
23 }
24 if ($rowset->current()->getData() != $row->getData()) {
25 throw new \UnexpectedValueException("Row has already been modified");
26 }
27 }
28 }
29 }