diff --git a/src/QueryBuilder.php b/src/QueryBuilder.php index d14c480..8d8e64f 100644 --- a/src/QueryBuilder.php +++ b/src/QueryBuilder.php @@ -156,7 +156,8 @@ public function assembleSelect(Select $select, array &$values = []) $this->buildGroupBy($select->getGroupBy(), $values), $this->buildHaving($select->getHaving(), $values), $this->buildOrderBy($select->getOrderBy(), $values), - $this->buildLimitOffset($select->getLimit(), $select->getOffset()) + $this->buildLimitOffset($select->getLimit(), $select->getOffset()), + $this->buildFor($select->getFor()) ]); $sql = implode($this->separator, $sql); @@ -684,6 +685,24 @@ public function buildLimitOffset($limit = null, $offset = null) return implode($this->separator, $sql); } + /** + * Build the locking clause of a query + * + * @param ?string $lockStrength + * + * @return string + */ + public function buildFor($lockStrength) + { + $sql = []; + + if (! empty($lockStrength)) { + $sql[] = "FOR $lockStrength"; + } + + return implode($this->separator, $sql); + } + /** * Build the UNION parts of a query * diff --git a/src/Select.php b/src/Select.php index 77a50ee..0e578e7 100644 --- a/src/Select.php +++ b/src/Select.php @@ -39,6 +39,9 @@ class Select implements CommonTableExpressionInterface, LimitOffsetInterface, Or /** @var array|null Internal representation for the HAVING part of the query */ protected $having; + /** @var string The lock strength applied by the query */ + protected $lockStrength; + /** * The queries to UNION * @@ -352,6 +355,30 @@ public function orNotHaving($condition, $operator = Sql::ALL) return $this; } + /** + * Get the lock strength applied by the query + * + * @return string + */ + public function getFor() + { + return $this->lockStrength; + } + + /** + * Set a lock strength to be applied by the query + * + * @param string $lockStrength + * + * @return self + */ + public function for($lockStrength) + { + $this->lockStrength = $lockStrength; + + return $this; + } + /** * Get the UNION parts of the query * diff --git a/tests/SelectTest.php b/tests/SelectTest.php index f81a3b5..f910552 100644 --- a/tests/SelectTest.php +++ b/tests/SelectTest.php @@ -600,6 +600,17 @@ public function testOffsetWithoutLimit() ); } + public function testFor() + { + $this->setupTest(); + + $this->query->from('b')->columns('a')->for('UPDATE'); + $this->assertCorrectStatementAndValues( + 'SELECT a FROM b FOR UPDATE', + [] + ); + } + public function testUnion() { $this->setupTest();