Thanks to visit codestin.com
Credit goes to github.com

Skip to content

feat: add Query Builder sharedLock()#10315

Open
memleakd wants to merge 2 commits into
codeigniter4:4.8from
memleakd:feat/query-builder-shared-lock
Open

feat: add Query Builder sharedLock()#10315
memleakd wants to merge 2 commits into
codeigniter4:4.8from
memleakd:feat/query-builder-shared-lock

Conversation

@memleakd

Copy link
Copy Markdown
Contributor

Description

This PR adds sharedLock() to Query Builder.

It is the read-lock counterpart to lockForUpdate() and can be used when rows should be protected from concurrent updates while the current transaction reads them:

$db->transaction(static function ($db) use ($accountId): void {
    $account = $db->table('accounts')
        ->where('id', $accountId)
        ->sharedLock()
        ->get()
        ->getRow();
});

Supported drivers:

  • MySQLi: LOCK IN SHARE MODE
  • Postgre: FOR SHARE
  • SQLSRV: table hints

SQLite3 and OCI8 throw a DatabaseException.

The implementation reuses the same internal select-lock path as lockForUpdate(). This keeps reset behavior consistent and ensures helper queries like exists() and countAllResults() do not include select locks while preserving the original builder state.

Docs and tests were added for supported SQL, unsupported drivers, reset behavior, and driver-specific limitations.

Checklist:

  • Securely signed commits
  • Component(s) with PHPDoc blocks, only if necessary or adds value (without duplication)
  • Unit testing, with >80% coverage
  • User guide updated
  • Conforms to style guide

- Add Query Builder sharedLock() for pessimistic read locks.
- Compile driver-specific shared-lock SQL for supported drivers.
- Preserve SELECT lock state around exists() and countAllResults().
- Document driver support and limitations.
- Cover generated SQL, reset behavior, unsupported drivers, and edge cases.

Signed-off-by: memleakd <[email protected]>
@github-actions github-actions Bot added the 4.8 PRs that target the `4.8` branch. label Jun 16, 2026
Signed-off-by: memleakd <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

4.8 PRs that target the `4.8` branch.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant