Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Select: Add support for locking clauses #55

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 20 additions & 1 deletion src/QueryBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not just string here?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I couldn't find the ANSI-SQL definition, but read that this is defined in ANSI-SQL (to an extent, of course). Here is what I found about the implementation in our supported databases:

Because of the different implementations, I would say locking clause instead of lock strength everywhere. And don't bother with MSSQL for now.

*
* @return string
*/
public function buildFor($lockStrength)
{
$sql = [];
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not just return ! empty($lockStrength) ? "FOR $lockStrength" : ""?


if (! empty($lockStrength)) {
$sql[] = "FOR $lockStrength";
}

return implode($this->separator, $sql);
}

/**
* Build the UNION parts of a query
*
Expand Down
27 changes: 27 additions & 0 deletions src/Select.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
*
Expand Down Expand Up @@ -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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should be @return $this.

*/
public function for($lockStrength)
{
$this->lockStrength = $lockStrength;

return $this;
}

/**
* Get the UNION parts of the query
*
Expand Down
11 changes: 11 additions & 0 deletions tests/SelectTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down