@@ -109,6 +109,13 @@ class BaseBuilder
109109 */
110110 protected $ QBOffset = false ;
111111
112+ /**
113+ * QB FOR UPDATE flag
114+ *
115+ * @var bool
116+ */
117+ protected $ QBLockForUpdate = false ;
118+
112119 /**
113120 * QB ORDER BY data
114121 *
@@ -1619,6 +1626,16 @@ public function limit(?int $value = null, ?int $offset = 0)
16191626 return $ this ;
16201627 }
16211628
1629+ /**
1630+ * Locks the selected rows for update.
1631+ */
1632+ public function lockForUpdate (): static
1633+ {
1634+ $ this ->QBLockForUpdate = true ;
1635+
1636+ return $ this ;
1637+ }
1638+
16221639 /**
16231640 * Sets the OFFSET value
16241641 *
@@ -1800,20 +1817,26 @@ public function countAllResults(bool $reset = true)
18001817 }
18011818
18021819 // We cannot use a LIMIT when getting the single row COUNT(*) result
1803- $ limit = $ this ->QBLimit ;
1820+ $ limit = $ this ->QBLimit ;
1821+ $ lockForUpdate = $ this ->QBLockForUpdate ;
18041822
1805- $ this ->QBLimit = false ;
1823+ $ this ->QBLimit = false ;
1824+ $ this ->QBLockForUpdate = false ;
18061825
1807- if ($ this ->QBDistinct === true || ! empty ($ this ->QBGroupBy )) {
1808- // We need to backup the original SELECT in case DBPrefix is used
1809- $ select = $ this ->QBSelect ;
1810- $ sql = $ this ->countString . $ this ->db ->protectIdentifiers ('numrows ' ) . "\nFROM ( \n" . $ this ->compileSelect () . "\n) CI_count_all_results " ;
1826+ try {
1827+ if ($ this ->QBDistinct === true || ! empty ($ this ->QBGroupBy )) {
1828+ // We need to backup the original SELECT in case DBPrefix is used
1829+ $ select = $ this ->QBSelect ;
1830+ $ sql = $ this ->countString . $ this ->db ->protectIdentifiers ('numrows ' ) . "\nFROM ( \n" . $ this ->compileSelect () . "\n) CI_count_all_results " ;
18111831
1812- // Restore SELECT part
1813- $ this ->QBSelect = $ select ;
1814- unset($ select );
1815- } else {
1816- $ sql = $ this ->compileSelect ($ this ->countString . $ this ->db ->protectIdentifiers ('numrows ' ));
1832+ // Restore SELECT part
1833+ $ this ->QBSelect = $ select ;
1834+ unset($ select );
1835+ } else {
1836+ $ sql = $ this ->compileSelect ($ this ->countString . $ this ->db ->protectIdentifiers ('numrows ' ));
1837+ }
1838+ } finally {
1839+ $ this ->QBLockForUpdate = $ lockForUpdate ;
18171840 }
18181841
18191842 if ($ this ->testMode ) {
@@ -3158,9 +3181,19 @@ protected function compileSelect($selectOverride = false): string
31583181 $ sql = $ this ->_limit ($ sql . "\n" );
31593182 }
31603183
3184+ $ sql .= $ this ->compileLockForUpdate ();
3185+
31613186 return $ this ->unionInjection ($ sql );
31623187 }
31633188
3189+ /**
3190+ * Compile the SELECT lock clause.
3191+ */
3192+ protected function compileLockForUpdate (): string
3193+ {
3194+ return $ this ->QBLockForUpdate ? "\nFOR UPDATE " : '' ;
3195+ }
3196+
31643197 /**
31653198 * Checks if the ignore option is supported by
31663199 * the Database Driver for the specific statement.
@@ -3468,17 +3501,18 @@ protected function resetRun(array $qbResetItems)
34683501 protected function resetSelect ()
34693502 {
34703503 $ this ->resetRun ([
3471- 'QBSelect ' => [],
3472- 'QBJoin ' => [],
3473- 'QBWhere ' => [],
3474- 'QBGroupBy ' => [],
3475- 'QBHaving ' => [],
3476- 'QBOrderBy ' => [],
3477- 'QBNoEscape ' => [],
3478- 'QBDistinct ' => false ,
3479- 'QBLimit ' => false ,
3480- 'QBOffset ' => false ,
3481- 'QBUnion ' => [],
3504+ 'QBSelect ' => [],
3505+ 'QBJoin ' => [],
3506+ 'QBWhere ' => [],
3507+ 'QBGroupBy ' => [],
3508+ 'QBHaving ' => [],
3509+ 'QBOrderBy ' => [],
3510+ 'QBNoEscape ' => [],
3511+ 'QBDistinct ' => false ,
3512+ 'QBLimit ' => false ,
3513+ 'QBOffset ' => false ,
3514+ 'QBLockForUpdate ' => false ,
3515+ 'QBUnion ' => [],
34823516 ]);
34833517
34843518 if ($ this ->db instanceof BaseConnection) {
0 commit comments