Skip to content

Commit c9dbd06

Browse files
committed
Restore SqlFragment and thin Limit/Offset Part classes
Reintroduce SqlFragment fluent builder for cleaner buildSqlString composition. Re-wrap Limit and Offset as thin Part classes using Value+renderArgument internally instead of the old Parameter approach. Quantifier remains inlined in Select.
1 parent 6949364 commit c9dbd06

File tree

4 files changed

+123
-67
lines changed

4 files changed

+123
-67
lines changed

src/Sql/Part/Limit.php

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace PhpDb\Sql\Part;
6+
7+
use PhpDb\Adapter\ParameterContainer;
8+
use PhpDb\Sql\Argument\Value;
9+
use PhpDb\Sql\Platform\AbstractSqlRenderer;
10+
11+
class Limit extends AbstractPart
12+
{
13+
private ?Value $limit = null;
14+
15+
public function toSql(AbstractSqlRenderer $renderer, string $paramPrefix = '', int &$paramIndex = 0): ?string
16+
{
17+
if ($this->limit === null) {
18+
return null;
19+
}
20+
21+
$pi = 0;
22+
23+
return 'LIMIT ' . $renderer->renderArgument(
24+
$this->limit,
25+
$renderer->getParamPrefix() . 'limit',
26+
$pi,
27+
);
28+
}
29+
30+
public function isEmpty(): bool
31+
{
32+
return $this->limit === null;
33+
}
34+
35+
public function set(string|int $limit): static
36+
{
37+
$this->limit = new Value($limit, ParameterContainer::TYPE_INTEGER);
38+
return $this;
39+
}
40+
41+
public function get(): string|int|null
42+
{
43+
return $this->limit?->value;
44+
}
45+
}

src/Sql/Part/Offset.php

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace PhpDb\Sql\Part;
6+
7+
use PhpDb\Adapter\ParameterContainer;
8+
use PhpDb\Sql\Argument\Value;
9+
use PhpDb\Sql\Platform\AbstractSqlRenderer;
10+
11+
class Offset extends AbstractPart
12+
{
13+
private ?Value $offset = null;
14+
15+
public function toSql(AbstractSqlRenderer $renderer, string $paramPrefix = '', int &$paramIndex = 0): ?string
16+
{
17+
if ($this->offset === null) {
18+
return null;
19+
}
20+
21+
$pi = 0;
22+
23+
return 'OFFSET ' . $renderer->renderArgument(
24+
$this->offset,
25+
$renderer->getParamPrefix() . 'offset',
26+
$pi,
27+
);
28+
}
29+
30+
public function isEmpty(): bool
31+
{
32+
return $this->offset === null;
33+
}
34+
35+
public function set(string|int $offset): static
36+
{
37+
$this->offset = new Value($offset, ParameterContainer::TYPE_INTEGER);
38+
return $this;
39+
}
40+
41+
public function get(): string|int|null
42+
{
43+
return $this->offset?->value;
44+
}
45+
}

src/Sql/Part/SqlFragment.php

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,14 +25,10 @@ public function part(?string $part): self
2525
return $this;
2626
}
2727

28-
public function wrap(?string $suffix = null): self
28+
public function wrap(?string $suffix): self
2929
{
30-
if ($this->parts === []) {
31-
return $this;
32-
}
33-
$this->parts = ['( ' . implode(' ', $this->parts) . ' )'];
3430
if ($suffix !== null) {
35-
$this->parts[] = $suffix;
31+
$this->parts = ['( ' . implode(' ', $this->parts) . ' )', $suffix];
3632
}
3733
return $this;
3834
}

src/Sql/Select.php

Lines changed: 31 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,14 @@
66

77
use Closure;
88
use Override;
9-
use PhpDb\Adapter\ParameterContainer;
109
use PhpDb\Sql\Argument\Literal;
1110
use PhpDb\Sql\Argument\Select as SelectArgument;
12-
use PhpDb\Sql\Argument\Value;
1311
use PhpDb\Sql\Part\Combine as CombinePart;
1412
use PhpDb\Sql\Part\GroupBy;
13+
use PhpDb\Sql\Part\Limit;
14+
use PhpDb\Sql\Part\Offset;
1515
use PhpDb\Sql\Part\OrderBy;
16+
use PhpDb\Sql\Part\SqlFragment;
1617
use PhpDb\Sql\Part\Table;
1718
use PhpDb\Sql\Platform\AbstractSqlRenderer;
1819
use 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

Comments
 (0)