@@ -47,14 +47,19 @@ class Select extends Statement {
4747 */
4848 private $ calcFoundRows = false ;
4949
50+ /**
51+ * @var bool
52+ */
53+ private $ forUpdate = false ;
54+
5055 /**
5156 * @param string $expression
5257 * @param string $alias
5358 * @return $this
5459 */
5560 public function field ($ expression , $ alias = null ) {
5661 if (is_object ($ expression )) {
57- $ expression = (string ) $ expression ;
62+ $ expression = (string )$ expression ;
5863 }
5964 if ($ alias === null ) {
6065 $ this ->fields [] = $ expression ;
@@ -197,6 +202,15 @@ public function offset($offset) {
197202 return $ this ;
198203 }
199204
205+ /**
206+ * @param bool $enabled
207+ * @return $this
208+ */
209+ public function forUpdate ($ enabled = true ) {
210+ $ this ->forUpdate = $ enabled ;
211+ return $ this ;
212+ }
213+
200214 /**
201215 * @return bool
202216 */
@@ -234,6 +248,7 @@ public function __toString() {
234248 $ query = $ this ->buildConditions ('HAVING ' , $ this ->having , $ query );
235249 $ query = $ this ->buildOrder ($ query );
236250 $ query = $ this ->buildLimit ($ query );
251+ $ query = $ this ->buildForUpdate ($ query );
237252 $ query .= "; \n" ;
238253 return $ query ;
239254 }
@@ -274,7 +289,7 @@ private function buildFields($query) {
274289 } else {
275290 $ fields [] = "\t* " ;
276291 }
277- return $ query . join (", \n" , $ fields ) . "\n" ;
292+ return $ query. join (", \n" , $ fields ). "\n" ;
278293 }
279294
280295 /**
@@ -285,12 +300,12 @@ private function buildFrom($query) {
285300 $ arr = array ();
286301 foreach ($ this ->tables as $ table ) {
287302 if ($ table ['type ' ] == 'FROM ' ) {
288- $ arr [] = "\t" . $ this ->buildTableName ($ table ['alias ' ], $ table ['name ' ]);
303+ $ arr [] = "\t" . $ this ->buildTableName ($ table ['alias ' ], $ table ['name ' ]);
289304 }
290305 }
291306 if (count ($ arr )) {
292307 $ query .= "FROM \n" ;
293- $ query .= join (", \n" , $ arr ) . "\n" ;
308+ $ query .= join (", \n" , $ arr ). "\n" ;
294309 }
295310 return $ query ;
296311 }
@@ -303,16 +318,16 @@ private function buildJoins($query) {
303318 $ arr = array ();
304319 foreach ($ this ->tables as $ table ) {
305320 if ($ table ['type ' ] != 'FROM ' ) {
306- $ join = $ table ['type ' ] . " JOIN \n" ;
307- $ join .= "\t" . $ this ->buildTableName ($ table ['alias ' ], $ table ['name ' ]);
321+ $ join = $ table ['type ' ]. " JOIN \n" ;
322+ $ join .= "\t" . $ this ->buildTableName ($ table ['alias ' ], $ table ['name ' ]);
308323 if ($ table ['expression ' ]) {
309- $ join .= " ON " . $ this ->buildExpression ($ table ['expression ' ], $ table ['arguments ' ]);
324+ $ join .= " ON " . $ this ->buildExpression ($ table ['expression ' ], $ table ['arguments ' ]);
310325 }
311326 $ arr [] = $ join ;
312327 }
313328 }
314329 if (count ($ arr )) {
315- $ query .= join ("\n" , $ arr ) . "\n" ;
330+ $ query .= join ("\n" , $ arr ). "\n" ;
316331 }
317332 return $ query ;
318333 }
@@ -335,7 +350,7 @@ private function buildConditions($type, array $conditions, $query) {
335350 $ arr [] = "\t{$ expr }" ;
336351 }
337352 $ query .= join ("\n\tAND \n" , $ arr );
338- return $ query . "\n" ;
353+ return $ query. "\n" ;
339354 }
340355
341356 /**
@@ -352,7 +367,7 @@ private function buildOrder($query) {
352367 list ($ expression , $ direction ) = $ order ;
353368 $ arr [] = sprintf ("\t%s %s " , $ expression , strtoupper ($ direction ));
354369 }
355- return $ query . join (", \n" , $ arr ) . "\n" ;
370+ return $ query. join (", \n" , $ arr ). "\n" ;
356371 }
357372
358373 /**
@@ -368,7 +383,7 @@ private function buildGroups($query) {
368383 foreach ($ this ->groupBy as $ expression ) {
369384 $ arr [] = "\t{$ expression }" ;
370385 }
371- return $ query . join (", \n" , $ arr ) . "\n" ;
386+ return $ query. join (", \n" , $ arr ). "\n" ;
372387 }
373388
374389 /**
@@ -386,6 +401,17 @@ private function buildLimit($query) {
386401 return $ query ;
387402 }
388403
404+ /**
405+ * @param string $query
406+ * @return string
407+ */
408+ private function buildForUpdate ($ query ) {
409+ if ($ this ->forUpdate ) {
410+ $ query .= "FOR UPDATE \n" ;
411+ }
412+ return $ query ;
413+ }
414+
389415 /**
390416 * @param string $alias
391417 * @param string $name
0 commit comments