feat: add Query Builder lockForUpdate()#10171
Open
memleakd wants to merge 3 commits intocodeigniter4:4.8from
Open
feat: add Query Builder lockForUpdate()#10171memleakd wants to merge 3 commits intocodeigniter4:4.8from
memleakd wants to merge 3 commits intocodeigniter4:4.8from
Conversation
datamweb
reviewed
May 7, 2026
Signed-off-by: memleakd <121398829+memleakd@users.noreply.github.com>
Signed-off-by: memleakd <121398829+memleakd@users.noreply.github.com>
Signed-off-by: memleakd <121398829+memleakd@users.noreply.github.com>
2945994 to
b7737be
Compare
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.
Description
This PR proposes adding
lockForUpdate()to the Query Builder for pessimistic write locking on supported database drivers.The goal is to provide a small framework-native API for code that needs to read rows and then safely update them inside the same transaction.
Example:
Why
Some application workflows need to prevent concurrent writes around the same row while a transaction is deciding what to do next.
Common examples include:
Without a Query Builder method, users either need raw SQL per database driver or custom helper code around otherwise builder-based queries.
Behavior
For drivers that support a trailing
FOR UPDATEclause,lockForUpdate()appends it to the compiledSELECT.SQLSRV is handled separately because SQL Server does not use trailing
FOR UPDATE. Instead, it appliesWITH (UPDLOCK, ROWLOCK)table hints to table references in theFROMclause.Supported drivers in this PR:
Unsupported or constrained behavior:
DatabaseExceptionbecause SQLite does not support row-levelSELECT ... FOR UPDATE.DatabaseExceptionwhenlockForUpdate()is combined withlimit()oroffset(), because Oracle does not allowFOR UPDATEwith row limiting.DatabaseExceptionwhen used without aFROMtable or on subqueries.Notes
This PR intentionally keeps the first implementation narrow. It does not add
sharedLock(),skipLocked(),nowait()or SQLSRV joined-table hinting.Those could be considered later, but this PR focuses only on the most common pessimistic write-lock use case.
Testing
Added Query Builder coverage for:
FOR UPDATEcompilation$reset = falsebehaviorcountAllResults()suppressing lock clausesFROMqueriesChecklist