Skip to content

Commit 50768ee

Browse files
committed
Prepare-based SQL decoration, simplify Platform to abstract registry
Replace PlatformDecoratorInterface wrapping model with SqlDecoratorInterface prepare-based model: decorators mutate SQL objects via public API before the standard rendering pipeline runs unchanged. Platform becomes AbstractPlatform — a flat decorator registry without platform-name keying or adapter resolution. Sql92Platform is the concrete implementation. SqlProcessor gains prepare() which runs decorator then built-in Table::prepare(). Sql::prepareStatementForSqlObject() calls buildSqlString() directly, absorbing ParameterContainer setup. Signed-off-by: Simon Mundy <simon.mundy@peptolab.com>
1 parent 1d75ea3 commit 50768ee

25 files changed

+147
-524
lines changed

src/Adapter/Platform/PlatformInterface.php

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
namespace PhpDb\Adapter\Platform;
66

7-
use PhpDb\Sql\Platform\PlatformDecoratorInterface;
7+
use PhpDb\Sql\Platform\AbstractPlatform as SqlPlatform;
88

99
interface PlatformInterface
1010
{
@@ -14,12 +14,9 @@ interface PlatformInterface
1414
public function getName(): string;
1515

1616
/**
17-
* Get Sql platform decorator
18-
*
19-
* Returns a PhpDb\Sql\Platform\* instance
20-
* PhpDb\Sql\Platform\AbstractPlatform implements this interface
17+
* Get Sql platform decorator registry
2118
*/
22-
public function getSqlPlatformDecorator(): PlatformDecoratorInterface;
19+
public function getSqlPlatformDecorator(): SqlPlatform;
2320

2421
/**
2522
* Get quote identifier symbol

src/Adapter/Platform/Sql92.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@
66

77
use Override;
88
use PhpDb\Adapter\Exception\VunerablePlatformQuoteException;
9-
use PhpDb\Sql\Platform\Platform;
10-
use PhpDb\Sql\Platform\PlatformDecoratorInterface;
9+
use PhpDb\Sql\Platform\AbstractPlatform as SqlPlatform;
10+
use PhpDb\Sql\Platform\Sql92Platform;
1111

1212
use function addcslashes;
1313

@@ -43,8 +43,8 @@ public function quoteValue(string $value): string
4343
* {@inheritDoc}
4444
*/
4545
#[Override]
46-
public function getSqlPlatformDecorator(): PlatformDecoratorInterface
46+
public function getSqlPlatformDecorator(): SqlPlatform
4747
{
48-
return new Platform($this);
48+
return new Sql92Platform();
4949
}
5050
}

src/Sql/AbstractSql.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
use PhpDb\Adapter\ParameterContainer;
1010
use PhpDb\Adapter\Platform\PlatformInterface;
1111
use PhpDb\Adapter\Platform\Sql92 as DefaultAdapterPlatform;
12-
use PhpDb\Sql\Platform\PlatformDecoratorInterface;
12+
use PhpDb\Sql\Platform\AbstractPlatform as SqlPlatform;
1313

1414
abstract class AbstractSql implements SqlInterface
1515
{
@@ -35,7 +35,7 @@ public function buildSqlString(
3535
PlatformInterface $platform,
3636
?DriverInterface $driver = null,
3737
?ParameterContainer $parameterContainer = null,
38-
?PlatformDecoratorInterface $decorator = null,
38+
?SqlPlatform $sqlPlatform = null,
3939
): string {
4040
return '';
4141
}

src/Sql/Combine.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
use PhpDb\Adapter\ParameterContainer;
1010
use PhpDb\Adapter\Platform\PlatformInterface;
1111
use PhpDb\Sql\Part\SqlProcessor;
12-
use PhpDb\Sql\Platform\PlatformDecoratorInterface;
12+
use PhpDb\Sql\Platform\AbstractPlatform as SqlPlatform;
1313

1414
use function array_key_exists;
1515
use function array_keys;
@@ -108,13 +108,13 @@ public function buildSqlString(
108108
PlatformInterface $platform,
109109
?DriverInterface $driver = null,
110110
?ParameterContainer $parameterContainer = null,
111-
?PlatformDecoratorInterface $decorator = null,
111+
?SqlPlatform $sqlPlatform = null,
112112
): string {
113113
if (! $this->combine) {
114114
return '';
115115
}
116116

117-
$processor = new SqlProcessor($platform, $driver, $parameterContainer, $decorator);
117+
$processor = new SqlProcessor($platform, $driver, $parameterContainer, $sqlPlatform);
118118
$processor->setParamPrefix($this->processInfo['paramPrefix']);
119119

120120
$parts = [];

src/Sql/Ddl/AlterTable.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
use PhpDb\Adapter\ParameterContainer;
99
use PhpDb\Adapter\Platform\PlatformInterface;
1010
use PhpDb\Sql\Part\SqlProcessor;
11-
use PhpDb\Sql\Platform\PlatformDecoratorInterface;
11+
use PhpDb\Sql\Platform\AbstractPlatform as SqlPlatform;
1212
use PhpDb\Sql\TableIdentifier;
1313

1414
use function array_key_exists;
@@ -123,9 +123,9 @@ public function buildSqlString(
123123
PlatformInterface $platform,
124124
?DriverInterface $driver = null,
125125
?ParameterContainer $parameterContainer = null,
126-
?PlatformDecoratorInterface $decorator = null,
126+
?SqlPlatform $sqlPlatform = null,
127127
): string {
128-
$processor = new SqlProcessor($platform, $driver, $parameterContainer, $decorator);
128+
$processor = new SqlProcessor($platform, $driver, $parameterContainer, $sqlPlatform);
129129
$processor->setParamPrefix($this->processInfo['paramPrefix']);
130130

131131
$sql = "ALTER TABLE " . $processor->resolveTable($this->table) . "\n";

src/Sql/Ddl/CreateTable.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
use PhpDb\Adapter\ParameterContainer;
99
use PhpDb\Adapter\Platform\PlatformInterface;
1010
use PhpDb\Sql\Part\SqlProcessor;
11-
use PhpDb\Sql\Platform\PlatformDecoratorInterface;
11+
use PhpDb\Sql\Platform\AbstractPlatform as SqlPlatform;
1212
use PhpDb\Sql\TableIdentifier;
1313

1414
use function array_key_exists;
@@ -84,9 +84,9 @@ public function buildSqlString(
8484
PlatformInterface $platform,
8585
?DriverInterface $driver = null,
8686
?ParameterContainer $parameterContainer = null,
87-
?PlatformDecoratorInterface $decorator = null,
87+
?SqlPlatform $sqlPlatform = null,
8888
): string {
89-
$processor = new SqlProcessor($platform, $driver, $parameterContainer, $decorator);
89+
$processor = new SqlProcessor($platform, $driver, $parameterContainer, $sqlPlatform);
9090
$processor->setParamPrefix($this->processInfo['paramPrefix']);
9191

9292
// CREATE [TEMPORARY] TABLE "name" (

src/Sql/Ddl/DropTable.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
use PhpDb\Adapter\ParameterContainer;
99
use PhpDb\Adapter\Platform\PlatformInterface;
1010
use PhpDb\Sql\Part\SqlProcessor;
11-
use PhpDb\Sql\Platform\PlatformDecoratorInterface;
11+
use PhpDb\Sql\Platform\AbstractPlatform as SqlPlatform;
1212
use PhpDb\Sql\TableIdentifier;
1313

1414
class DropTable extends AbstractDdl
@@ -26,9 +26,9 @@ public function buildSqlString(
2626
PlatformInterface $platform,
2727
?DriverInterface $driver = null,
2828
?ParameterContainer $parameterContainer = null,
29-
?PlatformDecoratorInterface $decorator = null,
29+
?SqlPlatform $sqlPlatform = null,
3030
): string {
31-
$processor = new SqlProcessor($platform, $driver, $parameterContainer, $decorator);
31+
$processor = new SqlProcessor($platform, $driver, $parameterContainer, $sqlPlatform);
3232

3333
return 'DROP TABLE ' . $processor->resolveTable($this->table);
3434
}

src/Sql/Delete.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
use PhpDb\Sql\Part\SqlProcessor;
1313
use PhpDb\Sql\Part\From;
1414
use PhpDb\Sql\Part\Where as WherePart;
15-
use PhpDb\Sql\Platform\PlatformDecoratorInterface;
15+
use PhpDb\Sql\Platform\AbstractPlatform as SqlPlatform;
1616
use PhpDb\Sql\Predicate\PredicateInterface;
1717

1818
use function array_key_exists;
@@ -84,10 +84,11 @@ public function buildSqlString(
8484
PlatformInterface $platform,
8585
?DriverInterface $driver = null,
8686
?ParameterContainer $parameterContainer = null,
87-
?PlatformDecoratorInterface $decorator = null,
87+
?SqlPlatform $sqlPlatform = null,
8888
): string {
89-
$processor = new SqlProcessor($platform, $driver, $parameterContainer, $decorator);
89+
$processor = new SqlProcessor($platform, $driver, $parameterContainer, $sqlPlatform);
9090
$processor->setParamPrefix($this->processInfo['paramPrefix']);
91+
$processor->prepare($this);
9192

9293
return (string) SqlFragment::of($this->getStatementKeyword())
9394
->part($this->table->toSql($processor))

src/Sql/Insert.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
use PhpDb\Adapter\Platform\PlatformInterface;
1111
use PhpDb\Sql\Part\SqlProcessor;
1212
use PhpDb\Sql\Part\From;
13-
use PhpDb\Sql\Platform\PlatformDecoratorInterface;
13+
use PhpDb\Sql\Platform\AbstractPlatform as SqlPlatform;
1414

1515
use function array_flip;
1616
use function array_key_exists;
@@ -152,9 +152,9 @@ public function buildSqlString(
152152
PlatformInterface $platform,
153153
?DriverInterface $driver = null,
154154
?ParameterContainer $parameterContainer = null,
155-
?PlatformDecoratorInterface $decorator = null,
155+
?SqlPlatform $sqlPlatform = null,
156156
): string {
157-
$processor = new SqlProcessor($platform, $driver, $parameterContainer, $decorator);
157+
$processor = new SqlProcessor($platform, $driver, $parameterContainer, $sqlPlatform);
158158
$processor->setParamPrefix($this->processInfo['paramPrefix']);
159159

160160
$keyword = $this->getStatementKeyword();

src/Sql/Part/SqlProcessor.php

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
use PhpDb\Sql\Argument\Parameter;
1111
use PhpDb\Sql\ArgumentInterface;
1212
use PhpDb\Sql\ExpressionInterface;
13-
use PhpDb\Sql\Platform\PlatformDecoratorInterface;
13+
use PhpDb\Sql\Platform\AbstractPlatform as SqlPlatform;
1414
use PhpDb\Sql\Select;
1515
use PhpDb\Sql\TableIdentifier;
1616

@@ -33,7 +33,7 @@ public function __construct(
3333
public readonly PlatformInterface $platform,
3434
public readonly ?DriverInterface $driver = null,
3535
public readonly ?ParameterContainer $parameterContainer = null,
36-
private ?PlatformDecoratorInterface $decorator = null,
36+
private ?SqlPlatform $sqlPlatform = null,
3737
) {
3838
$this->identifierSeparator = $platform->getIdentifierSeparator();
3939
}
@@ -48,6 +48,15 @@ public function setParamPrefix(string $prefix): void
4848
$this->paramPrefix = $prefix;
4949
}
5050

51+
public function prepare(object $subject): void
52+
{
53+
$this->sqlPlatform?->getTypeDecorator($subject)?->prepare($subject, $this);
54+
55+
if ($subject instanceof Select) {
56+
$subject->tablePart()->prepare($this);
57+
}
58+
}
59+
5160
public function renderParameter(Parameter $param, ?string $nameOverride = null): string
5261
{
5362
if ($this->parameterContainer instanceof ParameterContainer) {
@@ -62,12 +71,6 @@ public function renderParameter(Parameter $param, ?string $nameOverride = null):
6271

6372
public function processSubSelect(Select $subselect): string
6473
{
65-
$decorator = null;
66-
if ($this->decorator !== null) {
67-
$decorator = clone $this->decorator;
68-
$decorator->setSubject($subselect);
69-
}
70-
7174
if ($this->parameterContainer instanceof ParameterContainer) {
7275
$this->subselectCount++;
7376
$subselect->processInfo['subselectCount'] = $this->subselectCount;
@@ -78,7 +81,7 @@ public function processSubSelect(Select $subselect): string
7881
$this->platform,
7982
$this->driver,
8083
$this->parameterContainer,
81-
$decorator,
84+
$this->sqlPlatform,
8285
);
8386
$this->subselectCount = $subselect->processInfo['subselectCount'];
8487

@@ -89,7 +92,7 @@ public function processSubSelect(Select $subselect): string
8992
$this->platform,
9093
$this->driver,
9194
$this->parameterContainer,
92-
$decorator,
95+
$this->sqlPlatform,
9396
);
9497
}
9598

0 commit comments

Comments
 (0)