@@ -110,6 +110,13 @@ class BaseBuilder
110110 */
111111 protected $ QBOffset = false ;
112112
113+ /**
114+ * QB FOR UPDATE flag
115+ *
116+ * @var bool
117+ */
118+ protected $ QBLockForUpdate = false ;
119+
113120 /**
114121 * QB ORDER BY data
115122 *
@@ -1620,6 +1627,16 @@ public function limit(?int $value = null, ?int $offset = 0)
16201627 return $ this ;
16211628 }
16221629
1630+ /**
1631+ * Locks the selected rows for update.
1632+ */
1633+ public function lockForUpdate (): static
1634+ {
1635+ $ this ->QBLockForUpdate = true ;
1636+
1637+ return $ this ;
1638+ }
1639+
16231640 /**
16241641 * Sets the OFFSET value
16251642 *
@@ -1801,20 +1818,26 @@ public function countAllResults(bool $reset = true)
18011818 }
18021819
18031820 // We cannot use a LIMIT when getting the single row COUNT(*) result
1804- $ limit = $ this ->QBLimit ;
1821+ $ limit = $ this ->QBLimit ;
1822+ $ lockForUpdate = $ this ->QBLockForUpdate ;
18051823
1806- $ this ->QBLimit = false ;
1824+ $ this ->QBLimit = false ;
1825+ $ this ->QBLockForUpdate = false ;
18071826
1808- if ($ this ->QBDistinct === true || ! empty ($ this ->QBGroupBy )) {
1809- // We need to backup the original SELECT in case DBPrefix is used
1810- $ select = $ this ->QBSelect ;
1811- $ sql = $ this ->countString . $ this ->db ->protectIdentifiers ('numrows ' ) . "\nFROM ( \n" . $ this ->compileSelect () . "\n) CI_count_all_results " ;
1827+ try {
1828+ if ($ this ->QBDistinct === true || ! empty ($ this ->QBGroupBy )) {
1829+ // We need to backup the original SELECT in case DBPrefix is used
1830+ $ select = $ this ->QBSelect ;
1831+ $ sql = $ this ->countString . $ this ->db ->protectIdentifiers ('numrows ' ) . "\nFROM ( \n" . $ this ->compileSelect () . "\n) CI_count_all_results " ;
18121832
1813- // Restore SELECT part
1814- $ this ->QBSelect = $ select ;
1815- unset($ select );
1816- } else {
1817- $ sql = $ this ->compileSelect ($ this ->countString . $ this ->db ->protectIdentifiers ('numrows ' ));
1833+ // Restore SELECT part
1834+ $ this ->QBSelect = $ select ;
1835+ unset($ select );
1836+ } else {
1837+ $ sql = $ this ->compileSelect ($ this ->countString . $ this ->db ->protectIdentifiers ('numrows ' ));
1838+ }
1839+ } finally {
1840+ $ this ->QBLockForUpdate = $ lockForUpdate ;
18181841 }
18191842
18201843 if ($ this ->testMode ) {
@@ -3223,9 +3246,19 @@ protected function compileSelect($selectOverride = false): string
32233246 $ sql = $ this ->_limit ($ sql . "\n" );
32243247 }
32253248
3249+ $ sql .= $ this ->compileLockForUpdate ();
3250+
32263251 return $ this ->unionInjection ($ sql );
32273252 }
32283253
3254+ /**
3255+ * Compile the SELECT lock clause.
3256+ */
3257+ protected function compileLockForUpdate (): string
3258+ {
3259+ return $ this ->QBLockForUpdate ? "\nFOR UPDATE " : '' ;
3260+ }
3261+
32293262 /**
32303263 * Checks if the ignore option is supported by
32313264 * the Database Driver for the specific statement.
@@ -3533,17 +3566,18 @@ protected function resetRun(array $qbResetItems)
35333566 protected function resetSelect ()
35343567 {
35353568 $ this ->resetRun ([
3536- 'QBSelect ' => [],
3537- 'QBJoin ' => [],
3538- 'QBWhere ' => [],
3539- 'QBGroupBy ' => [],
3540- 'QBHaving ' => [],
3541- 'QBOrderBy ' => [],
3542- 'QBNoEscape ' => [],
3543- 'QBDistinct ' => false ,
3544- 'QBLimit ' => false ,
3545- 'QBOffset ' => false ,
3546- 'QBUnion ' => [],
3569+ 'QBSelect ' => [],
3570+ 'QBJoin ' => [],
3571+ 'QBWhere ' => [],
3572+ 'QBGroupBy ' => [],
3573+ 'QBHaving ' => [],
3574+ 'QBOrderBy ' => [],
3575+ 'QBNoEscape ' => [],
3576+ 'QBDistinct ' => false ,
3577+ 'QBLimit ' => false ,
3578+ 'QBOffset ' => false ,
3579+ 'QBLockForUpdate ' => false ,
3580+ 'QBUnion ' => [],
35473581 ]);
35483582
35493583 if ($ this ->db instanceof BaseConnection) {
0 commit comments