Skip to content

Commit 9e3ed29

Browse files
authored
Merge pull request #133 from simon-mundy/platform-decorator-reinstate
- Reintroduced Adapter-specific Platform Decorators
2 parents b40e849 + 2492690 commit 9e3ed29

6 files changed

Lines changed: 31 additions & 13 deletions

File tree

src/Sql/Platform/AbstractPlatform.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ public function getSqlString(?PlatformInterface $adapterPlatform = null): string
8383
if (! $this->subject instanceof SqlInterface) {
8484
throw new Exception\RuntimeException(
8585
'The subject does not appear to implement PhpDb\Sql\SqlInterface, thus calling '
86-
. 'prepareStatement() has no effect'
86+
. 'getSqlString() has no effect'
8787
);
8888
}
8989

src/Sql/Platform/Platform.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@ public function getTypeDecorator(
8484
public function getDecorators(): array
8585
{
8686
$platformName = $this->resolvePlatformName($this->getDefaultPlatform());
87+
8788
return $this->decorators[$platformName];
8889
}
8990

src/Sql/Sql.php

Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -15,15 +15,15 @@ class Sql
1515

1616
protected TableIdentifier|string|array|null $table;
1717

18-
protected Platform\Platform $sqlPlatform;
18+
protected Platform\PlatformDecoratorInterface $sqlPlatform;
1919

2020
public function __construct(
2121
AdapterInterface $adapter,
2222
array|string|TableIdentifier|null $table = null
2323
) {
24-
$this->adapter = $adapter;
2524
$this->table = $table;
26-
$this->sqlPlatform = new Platform\Platform($adapter->getPlatform());
25+
$this->adapter = $adapter;
26+
$this->sqlPlatform = $adapter->getPlatform()->getSqlPlatformDecorator();
2727
}
2828

2929
public function getAdapter(): ?AdapterInterface
@@ -51,7 +51,7 @@ public function getTable(): array|string|TableIdentifier|null
5151
return $this->table;
5252
}
5353

54-
public function getSqlPlatform(): ?Platform\Platform
54+
public function getSqlPlatform(): ?Platform\PlatformDecoratorInterface
5555
{
5656
return $this->sqlPlatform;
5757
}
@@ -109,10 +109,17 @@ public function prepareStatementForSqlObject(
109109
?StatementInterface $statement = null,
110110
?AdapterInterface $adapter = null
111111
): StatementInterface {
112+
if (! $this->sqlPlatform instanceof PreparableSqlInterface) {
113+
throw new Exception\RuntimeException(
114+
'The subject does not implement PreparableSqlInterface'
115+
);
116+
}
117+
112118
$adapter ??= $this->adapter;
113119
$statement ??= $adapter->getDriver()->createStatement();
114120

115-
$this->sqlPlatform->setSubject($sqlObject)->prepareStatement($adapter, $statement);
121+
$this->sqlPlatform->setSubject($sqlObject);
122+
$this->sqlPlatform->prepareStatement($adapter, $statement);
116123

117124
return $statement;
118125
}
@@ -122,11 +129,16 @@ public function prepareStatementForSqlObject(
122129
*/
123130
public function buildSqlString(SqlInterface $sqlObject, ?AdapterInterface $adapter = null): string
124131
{
125-
return $this
126-
->sqlPlatform
127-
->setSubject($sqlObject)
128-
->getSqlString(
129-
$adapter instanceof AdapterInterface ? $adapter->getPlatform() : $this->adapter->getPlatform()
132+
if (! $this->sqlPlatform instanceof SqlInterface) {
133+
throw new Exception\RuntimeException(
134+
'The subject does not implement SqlInterface'
130135
);
136+
}
137+
138+
$this->sqlPlatform->setSubject($sqlObject);
139+
140+
return $this->sqlPlatform->getSqlString(
141+
$adapter instanceof AdapterInterface ? $adapter->getPlatform() : $this->adapter->getPlatform()
142+
);
131143
}
132144
}

test/unit/RowGateway/AbstractRowGatewayTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,14 @@
1010
use PhpDb\Adapter\Driver\DriverInterface;
1111
use PhpDb\Adapter\Driver\ResultInterface;
1212
use PhpDb\Adapter\Driver\StatementInterface;
13-
use PhpDb\Adapter\Platform\PlatformInterface;
1413
use PhpDb\RowGateway\AbstractRowGateway;
1514
use PhpDb\RowGateway\Exception\InvalidArgumentException;
1615
use PhpDb\RowGateway\Exception\RuntimeException;
1716
use PhpDb\RowGateway\Feature\FeatureSet;
1817
use PhpDb\RowGateway\RowGateway;
1918
use PhpDb\Sql\Select;
2019
use PhpDb\Sql\Sql;
20+
use PhpDbTest\TestAsset\TrustingSql92Platform;
2121
use PHPUnit\Framework\Attributes\CoversMethod;
2222
use PHPUnit\Framework\Attributes\IgnoreDeprecations;
2323
use PHPUnit\Framework\Attributes\RequiresPhp;
@@ -77,7 +77,7 @@ protected function setUp(): void
7777
->setConstructorArgs(
7878
[
7979
$mockDriver,
80-
$this->getMockBuilder(PlatformInterface::class)->getMock(),
80+
new TrustingSql92Platform(),
8181
]
8282
)->getMock();
8383

test/unit/Sql/AbstractSqlFunctionalTestCase.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -265,6 +265,7 @@ public function test(PreparableSqlInterface|SqlInterface $sqlObject, string $pla
265265

266266
$platform = $sql->getSqlPlatform();
267267
$this->assertNotNull($platform);
268+
$this->assertInstanceOf(Sql\Platform\AbstractPlatform::class, $platform);
268269
$platform->setTypeDecorator($type, $decorator);
269270
}
270271
}

test/unit/TableGateway/AbstractTableGatewayTest.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,10 @@ protected function setUp(): void
7979
$mockResult->expects($this->any())->method('getAffectedRows')->willReturn(5);
8080

8181
$mockPlatform = $this->getMockBuilder(PlatformInterface::class)->getMock();
82+
$mockPlatform->expects($this->any())->method('getName')->willReturn('sql92');
83+
$mockPlatform->expects($this->any())
84+
->method('getSqlPlatformDecorator')
85+
->willReturn(new Sql\Platform\Platform($mockPlatform));
8286

8387
$mockResultSet = $this->getMockBuilder(ResultSetInterface::class)->getMock();
8488

0 commit comments

Comments
 (0)