diff --git a/src/Sql/Platform/AbstractPlatform.php b/src/Sql/Platform/AbstractPlatform.php index 195cc118..cc8d5d67 100644 --- a/src/Sql/Platform/AbstractPlatform.php +++ b/src/Sql/Platform/AbstractPlatform.php @@ -83,7 +83,7 @@ public function getSqlString(?PlatformInterface $adapterPlatform = null): string if (! $this->subject instanceof SqlInterface) { throw new Exception\RuntimeException( 'The subject does not appear to implement PhpDb\Sql\SqlInterface, thus calling ' - . 'prepareStatement() has no effect' + . 'getSqlString() has no effect' ); } diff --git a/src/Sql/Platform/Platform.php b/src/Sql/Platform/Platform.php index 9d522024..6ce2f62b 100644 --- a/src/Sql/Platform/Platform.php +++ b/src/Sql/Platform/Platform.php @@ -84,6 +84,7 @@ public function getTypeDecorator( public function getDecorators(): array { $platformName = $this->resolvePlatformName($this->getDefaultPlatform()); + return $this->decorators[$platformName]; } diff --git a/src/Sql/Sql.php b/src/Sql/Sql.php index 50b45e48..525c4efb 100644 --- a/src/Sql/Sql.php +++ b/src/Sql/Sql.php @@ -15,15 +15,15 @@ class Sql protected TableIdentifier|string|array|null $table; - protected Platform\Platform $sqlPlatform; + protected Platform\PlatformDecoratorInterface $sqlPlatform; public function __construct( AdapterInterface $adapter, array|string|TableIdentifier|null $table = null ) { - $this->adapter = $adapter; $this->table = $table; - $this->sqlPlatform = new Platform\Platform($adapter->getPlatform()); + $this->adapter = $adapter; + $this->sqlPlatform = $adapter->getPlatform()->getSqlPlatformDecorator(); } public function getAdapter(): ?AdapterInterface @@ -51,7 +51,7 @@ public function getTable(): array|string|TableIdentifier|null return $this->table; } - public function getSqlPlatform(): ?Platform\Platform + public function getSqlPlatform(): ?Platform\PlatformDecoratorInterface { return $this->sqlPlatform; } @@ -109,10 +109,17 @@ public function prepareStatementForSqlObject( ?StatementInterface $statement = null, ?AdapterInterface $adapter = null ): StatementInterface { + if (! $this->sqlPlatform instanceof PreparableSqlInterface) { + throw new Exception\RuntimeException( + 'The subject does not implement PreparableSqlInterface' + ); + } + $adapter ??= $this->adapter; $statement ??= $adapter->getDriver()->createStatement(); - $this->sqlPlatform->setSubject($sqlObject)->prepareStatement($adapter, $statement); + $this->sqlPlatform->setSubject($sqlObject); + $this->sqlPlatform->prepareStatement($adapter, $statement); return $statement; } @@ -122,11 +129,16 @@ public function prepareStatementForSqlObject( */ public function buildSqlString(SqlInterface $sqlObject, ?AdapterInterface $adapter = null): string { - return $this - ->sqlPlatform - ->setSubject($sqlObject) - ->getSqlString( - $adapter instanceof AdapterInterface ? $adapter->getPlatform() : $this->adapter->getPlatform() + if (! $this->sqlPlatform instanceof SqlInterface) { + throw new Exception\RuntimeException( + 'The subject does not implement SqlInterface' ); + } + + $this->sqlPlatform->setSubject($sqlObject); + + return $this->sqlPlatform->getSqlString( + $adapter instanceof AdapterInterface ? $adapter->getPlatform() : $this->adapter->getPlatform() + ); } } diff --git a/test/unit/RowGateway/AbstractRowGatewayTest.php b/test/unit/RowGateway/AbstractRowGatewayTest.php index af6f1243..aa63b7bd 100644 --- a/test/unit/RowGateway/AbstractRowGatewayTest.php +++ b/test/unit/RowGateway/AbstractRowGatewayTest.php @@ -10,7 +10,6 @@ use PhpDb\Adapter\Driver\DriverInterface; use PhpDb\Adapter\Driver\ResultInterface; use PhpDb\Adapter\Driver\StatementInterface; -use PhpDb\Adapter\Platform\PlatformInterface; use PhpDb\RowGateway\AbstractRowGateway; use PhpDb\RowGateway\Exception\InvalidArgumentException; use PhpDb\RowGateway\Exception\RuntimeException; @@ -18,6 +17,7 @@ use PhpDb\RowGateway\RowGateway; use PhpDb\Sql\Select; use PhpDb\Sql\Sql; +use PhpDbTest\TestAsset\TrustingSql92Platform; use PHPUnit\Framework\Attributes\CoversMethod; use PHPUnit\Framework\Attributes\IgnoreDeprecations; use PHPUnit\Framework\Attributes\RequiresPhp; @@ -77,7 +77,7 @@ protected function setUp(): void ->setConstructorArgs( [ $mockDriver, - $this->getMockBuilder(PlatformInterface::class)->getMock(), + new TrustingSql92Platform(), ] )->getMock(); diff --git a/test/unit/Sql/AbstractSqlFunctionalTestCase.php b/test/unit/Sql/AbstractSqlFunctionalTestCase.php index 227b18f0..c93ddaae 100644 --- a/test/unit/Sql/AbstractSqlFunctionalTestCase.php +++ b/test/unit/Sql/AbstractSqlFunctionalTestCase.php @@ -265,6 +265,7 @@ public function test(PreparableSqlInterface|SqlInterface $sqlObject, string $pla $platform = $sql->getSqlPlatform(); $this->assertNotNull($platform); + $this->assertInstanceOf(Sql\Platform\AbstractPlatform::class, $platform); $platform->setTypeDecorator($type, $decorator); } } diff --git a/test/unit/TableGateway/AbstractTableGatewayTest.php b/test/unit/TableGateway/AbstractTableGatewayTest.php index b6c14f79..a53bd1ce 100644 --- a/test/unit/TableGateway/AbstractTableGatewayTest.php +++ b/test/unit/TableGateway/AbstractTableGatewayTest.php @@ -79,6 +79,10 @@ protected function setUp(): void $mockResult->expects($this->any())->method('getAffectedRows')->willReturn(5); $mockPlatform = $this->getMockBuilder(PlatformInterface::class)->getMock(); + $mockPlatform->expects($this->any())->method('getName')->willReturn('sql92'); + $mockPlatform->expects($this->any()) + ->method('getSqlPlatformDecorator') + ->willReturn(new Sql\Platform\Platform($mockPlatform)); $mockResultSet = $this->getMockBuilder(ResultSetInterface::class)->getMock();