66
77use Closure ;
88use Override ;
9- use PhpDb \Adapter \ParameterContainer ;
109use PhpDb \Sql \Argument \Literal ;
1110use PhpDb \Sql \Argument \Select as SelectArgument ;
12- use PhpDb \Sql \Argument \Value ;
1311use PhpDb \Sql \Part \Combine as CombinePart ;
1412use PhpDb \Sql \Part \GroupBy ;
13+ use PhpDb \Sql \Part \Limit ;
14+ use PhpDb \Sql \Part \Offset ;
1515use PhpDb \Sql \Part \OrderBy ;
16+ use PhpDb \Sql \Part \SqlFragment ;
1617use PhpDb \Sql \Part \Table ;
1718use PhpDb \Sql \Platform \AbstractSqlRenderer ;
1819use PhpDb \Sql \Predicate \PredicateInterface ;
@@ -107,9 +108,9 @@ class Select extends AbstractPreparableSql
107108
108109 protected ?OrderBy $ orderBy = null ;
109110
110- protected ?Value $ limitValue = null ;
111+ protected ?Limit $ limit = null ;
111112
112- protected ?Value $ offsetValue = null ;
113+ protected ?Offset $ offset = null ;
113114
114115 protected ?CombinePart $ combine = null ;
115116
@@ -259,7 +260,7 @@ public function limit(int|string $limit): static
259260 ));
260261 }
261262
262- $ this ->limitValue = new Value ( $ limit, ParameterContainer:: TYPE_INTEGER );
263+ ( $ this ->limit ?? = new Limit ())-> set ( $ limit );
263264 return $ this ;
264265 }
265266
@@ -276,7 +277,7 @@ public function offset(int|string $offset): static
276277 ));
277278 }
278279
279- $ this ->offsetValue = new Value ( $ offset, ParameterContainer:: TYPE_INTEGER );
280+ ( $ this ->offset ?? = new Offset ())-> set ( $ offset );
280281 return $ this ;
281282 }
282283
@@ -329,10 +330,10 @@ public function reset(string $part): static
329330 $ this ->having = null ;
330331 break ;
331332 case self ::LIMIT :
332- $ this ->limitValue = null ;
333+ $ this ->limit = null ;
333334 break ;
334335 case self ::OFFSET :
335- $ this ->offsetValue = null ;
336+ $ this ->offset = null ;
336337 break ;
337338 case self ::ORDER :
338339 $ this ->orderBy = null ;
@@ -356,8 +357,8 @@ public function getRawState(?string $key = null): mixed
356357 self ::ORDER => $ this ->orderBy ?->get() ?? [],
357358 self ::GROUP => $ this ->groupBy ?->get() ?? [],
358359 self ::HAVING => $ this ->having ??= new Having (),
359- self ::LIMIT => $ this ->limitValue ?->value ,
360- self ::OFFSET => $ this ->offsetValue ?->value ,
360+ self ::LIMIT => $ this ->limit ?->get() ,
361+ self ::OFFSET => $ this ->offset ?->get() ,
361362 self ::COMBINE => $ this ->combine ?->get() ?? [],
362363 ];
363364 return $ key !== null && array_key_exists ($ key , $ rawState ) ? $ rawState [$ key ] : $ rawState ;
@@ -381,57 +382,26 @@ public function buildSqlString(AbstractSqlRenderer $renderer): string
381382 {
382383 $ renderer ->getTypeDecorator ($ this )?->prepare($ this , $ renderer );
383384
384- $ sql = 'SELECT ' ;
385-
386- if ($ this ->quantifier !== null ) {
387- $ sql .= $ this ->quantifier instanceof Literal
388- ? ' ' . $ this ->quantifier ->literal
389- : ' ' . $ renderer ->render ($ this ->quantifier ->getValue (), 'quantifier ' );
390- }
391-
392- $ sql .= ' ' . $ this ->table ->columns ()->toSql ($ renderer );
393-
394- if (($ part = $ this ->table ->from ()->toSql ($ renderer )) !== null ) {
395- $ sql .= " $ part " ;
396- }
397- if (($ part = $ this ->table ->joins ()?->toSql($ renderer )) !== null ) {
398- $ sql .= " $ part " ;
399- }
400- if (($ part = $ this ->where ?->toSql($ renderer )) !== null ) {
401- $ sql .= " $ part " ;
402- }
403- if (($ part = $ this ->groupBy ?->toSql($ renderer )) !== null ) {
404- $ sql .= " $ part " ;
405- }
406- if (($ part = $ this ->having ?->toSql($ renderer )) !== null ) {
407- $ sql .= " $ part " ;
408- }
409- if (($ part = $ this ->orderBy ?->toSql($ renderer )) !== null ) {
410- $ sql .= " $ part " ;
411- }
412- if ($ this ->limitValue !== null ) {
413- $ pi = 0 ;
414- $ sql .= ' LIMIT ' . $ renderer ->renderArgument (
415- $ this ->limitValue ,
416- $ renderer ->getParamPrefix () . 'limit ' ,
417- $ pi ,
418- );
419- }
420- if ($ this ->offsetValue !== null ) {
421- $ pi = 0 ;
422- $ sql .= ' OFFSET ' . $ renderer ->renderArgument (
423- $ this ->offsetValue ,
424- $ renderer ->getParamPrefix () . 'offset ' ,
425- $ pi ,
426- );
427- }
428-
429- $ combine = $ this ->combine ?->toSql($ renderer );
430- if ($ combine !== null ) {
431- return "( $ sql ) $ combine " ;
432- }
433-
434- return $ sql ;
385+ $ quantifierSql = $ this ->quantifier !== null
386+ ? ($ this ->quantifier instanceof Literal
387+ ? $ this ->quantifier ->literal
388+ : $ renderer ->render ($ this ->quantifier ->getValue (), 'quantifier ' ))
389+ : null ;
390+
391+ $ fragment = SqlFragment::of ('SELECT ' )
392+ ->part ($ quantifierSql )
393+ ->part ($ this ->table ->columns ()->toSql ($ renderer ))
394+ ->part ($ this ->table ->from ()->toSql ($ renderer ))
395+ ->part ($ this ->table ->joins ()?->toSql($ renderer ))
396+ ->part ($ this ->where ?->toSql($ renderer ))
397+ ->part ($ this ->groupBy ?->toSql($ renderer ))
398+ ->part ($ this ->having ?->toSql($ renderer ))
399+ ->part ($ this ->orderBy ?->toSql($ renderer ))
400+ ->part ($ this ->limit ?->toSql($ renderer ))
401+ ->part ($ this ->offset ?->toSql($ renderer ))
402+ ->wrap ($ this ->combine ?->toSql($ renderer ));
403+
404+ return (string ) $ fragment ;
435405 }
436406
437407 /**
0 commit comments