feat: Added 'FOR UPDATE SKIP LOCKED' to SelectQuery#243
Open
feat: Added 'FOR UPDATE SKIP LOCKED' to SelectQuery#243
Conversation
roxblnfk
reviewed
Mar 2, 2026
Member
There was a problem hiding this comment.
I think it's time to increase min PHP version to 8.1 and use enums.
/**
* Row-level lock strength.
* PG-specific modes fallback to Update/Share on other drivers.
*/
enum LockMode
{
/** Exclusive lock. Blocks UPDATE, DELETE, SELECT FOR UPDATE/SHARE. */
case Update;
/** Shared lock. Blocks UPDATE, DELETE, SELECT FOR UPDATE. Allows FOR SHARE. */
case Share;
/**
* PG only. Like Update, but doesn't block FOR KEY SHARE.
* Use when not modifying PK/FK columns.
* Fallback: Update (MySQL, MSSQL), noop (SQLite).
*/
case NoKeyUpdate;
/**
* PG only. Weakest lock — blocks only DELETE and PK/FK updates.
* Fallback: Share (MySQL, MSSQL), noop (SQLite).
*/
case KeyShare;
}
/**
* Lock wait behavior when row is already locked.
*/
enum LockBehavior
{
/** Default. Block until lock is released. */
case Wait;
/**
* Fail immediately if row is locked.
* PG: NOWAIT, MySQL: NOWAIT, MSSQL: SET LOCK_TIMEOUT 0.
* SQLite: NotSupportedException.
*/
case NoWait;
/**
* Skip locked rows, return only unlocked. Useful for job queues.
* PG: SKIP LOCKED, MySQL: SKIP LOCKED (8.0+), MSSQL: READPAST hint.
* SQLite: NotSupportedException.
*/
case SkipLocked;
}
interface SelectQuery
{
// ...
/**
* Add row-level locking clause.
*
* @param LockMode $mode Lock strength. SQLite: ignored, uses BEGIN IMMEDIATE.
* @param LockBehavior $behavior Wait strategy. SQLite: only Wait supported.
*/
public function forUpdate(
LockMode $mode = LockMode::Update,
LockBehavior $behavior = LockBehavior::Wait,
): self;
}
// Usage
$query->forUpdate(LockMode::Update, LockBehavior::SkipLocked);Feel free to use enums in this PR, the PHP version must be bumped separately.
fcab654 to
ca3dbb6
Compare
ca3dbb6 to
1cfa7f8
Compare
Contributor
Author
|
Ready. Maybe we should add method lock($mode, $behavior) and deprecate forUpdate()? And rename field $forUpdate to $lock? |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
🤔 Why?
Issues240