diff --git a/phpstan.neon.dist b/phpstan.neon.dist index a061ba4..e64bf8b 100644 --- a/phpstan.neon.dist +++ b/phpstan.neon.dist @@ -1,5 +1,6 @@ parameters: - phpVersion: 80300 + phpVersion: 80100 level: 3 paths: - src + - tests diff --git a/src/Bridge/AbstractBridge.php b/src/Bridge/AbstractBridge.php index c02edcc..335950f 100644 --- a/src/Bridge/AbstractBridge.php +++ b/src/Bridge/AbstractBridge.php @@ -48,6 +48,7 @@ abstract class AbstractBridge extends DefaultQueryBuilder implements Bridge private ?string $serverVersion = null; private bool $serverVersionLookedUp = false; private ?string $vendorName = null; + private ?string $vendorVersion = null; private ?Transaction $currentTransaction = null; private ?ErrorConverter $errorConverter = null; private ?SchemaManager $schemaManager = null; @@ -97,6 +98,14 @@ public function setServerInfo(?string $serverName = null, ?string $serverVersion $this->serverVersionLookedUp = (null !== ($this->serverVersion = $serverVersion)); } + /** + * Please override. + */ + protected function lookupServerName(): ?string + { + return null; + } + #[\Override] public function getServerName(): ?string { @@ -104,49 +113,35 @@ public function getServerName(): ?string return $this->serverName; } - $this->serverName = $this->lookupServerName(); $this->serverNameLookedUp = true; - return $this->serverName; + return $this->serverName = $this->lookupServerName(); } /** - * Get error converter. + * Please override. */ - protected function getErrorConverter(): ErrorConverter + protected function lookupServerVersion(): ?string { - return $this->errorConverter ??= $this->createErrorConverter(); + return null; } - /** - * @internal - * For dependency injection only. - */ - public function setErrorConverter(ErrorConverter $errorConverter): void + #[\Override] + public function getServerVersion(): ?string { - $this->errorConverter = $errorConverter; - } + if ($this->serverVersionLookedUp) { + return $this->serverVersion; + } - /** - * Please override. - */ - protected function createErrorConverter(): ErrorConverter - { - return new PassthroughErrorConverter(); - } + $this->serverVersionLookedUp = true; - /** - * Please override. - */ - protected function lookupServerName(): ?string - { - return null; + return $this->serverVersion = $this->lookupServerVersion(); } #[\Override] public function getVendorName(): string { - if ($this->vendorName) { + if (null !== $this->vendorName) { return $this->vendorName; } @@ -180,12 +175,11 @@ public function getVendorName(): string #[\Override] public function getVendorVersion(): string { - if ($this->serverVersionLookedUp && $this->serverVersion) { - return $this->serverVersion; + if (null !== $this->vendorVersion) { + return $this->vendorVersion; } - $this->serverVersionLookedUp = true; - $serverVersion = $this->lookupServerVersion(); + $serverVersion = $this->getServerVersion(); $matches = []; if ($serverVersion && \preg_match('/(\d+\.|)\d+\.\d+/ims', $serverVersion, $matches)) { @@ -225,13 +219,6 @@ public function getServerFlavor(): ?string return Vendor::UNKNOWN !== ($value = $this->getVendorName()) ? $value : null; } - /** @deprecated */ - #[\Override] - public function getServerVersion(): ?string - { - return '0.0.0' !== ($value = $this->getVendorVersion()) ? $value : null; - } - /** @deprecated */ #[\Override] public function isVersionLessThan(string $version): bool @@ -249,19 +236,13 @@ public function isVersionGreaterOrEqualThan(string $version): bool #[\Override] public function getCurrentDatabase(): string { - if (null === $this->currentDatabase) { - $this->currentDatabase = (string) $this->raw('SELECT ?', [new CurrentDatabase()])->executeQuery()->fetchOne(); - } - return $this->currentDatabase; + return $this->currentDatabase ??= (string) $this->raw('SELECT ?', [new CurrentDatabase()])->executeQuery()->fetchOne(); } #[\Override] public function getDefaultSchema(): string { - if (null === $this->currentSchema) { - $this->currentSchema = (string) $this->raw('SELECT ?', [new CurrentSchema()])->executeQuery()->fetchOne(); - } - return $this->currentSchema; + return $this->currentSchema ??= (string) $this->raw('SELECT ?', [new CurrentSchema()])->executeQuery()->fetchOne(); } /** @@ -370,12 +351,29 @@ public function beginTransaction(int $isolationLevel = Transaction::REPEATABLE_R return $this->createTransaction($isolationLevel, $allowPending)->start(); } + /** + * Get error converter. + */ + protected function getErrorConverter(): ErrorConverter + { + return $this->errorConverter ??= $this->createErrorConverter(); + } + + /** + * @internal + * For dependency injection only. + */ + public function setErrorConverter(ErrorConverter $errorConverter): void + { + $this->errorConverter = $errorConverter; + } + /** * Please override. */ - protected function lookupServerVersion(): ?string + protected function createErrorConverter(): ErrorConverter { - return null; + return new PassthroughErrorConverter(); } /** diff --git a/src/Bridge/Bridge.php b/src/Bridge/Bridge.php index a71cfe4..4c74d9f 100644 --- a/src/Bridge/Bridge.php +++ b/src/Bridge/Bridge.php @@ -16,25 +16,22 @@ interface Bridge extends DatabaseSession public function disableErrorConverter(): void; /** - * Get server name. + * Get server name as exposed by the server itself, not normalized. */ public function getServerName(): ?string; /** - * Get server product type. - * - * @deprecated - * @see DatabaseSession::getVendorName() + * Get server version as exposed by the server itself, not normalized. */ - public function getServerFlavor(): ?string; + public function getServerVersion(): ?string; /** - * Get server version. + * Get normalized vendor name. * * @deprecated - * @see DatabaseSession::getVendorVersion() + * @see DatabaseSession::getVendorName() */ - public function getServerVersion(): ?string; + public function getServerFlavor(): ?string; /** * Version is less than given. diff --git a/src/Bridge/Doctrine/DoctrineQueryBuilder.php b/src/Bridge/Doctrine/DoctrineQueryBuilder.php index 8bf52ea..9cc2f55 100644 --- a/src/Bridge/Doctrine/DoctrineQueryBuilder.php +++ b/src/Bridge/Doctrine/DoctrineQueryBuilder.php @@ -83,7 +83,7 @@ protected function lookupServerVersion(): ?string return $this->doctrineServerVersion = $this->connection->getServerVersion(); } - return $this->doctrineServerVersion = 'unknown'; + return $this->doctrineServerVersion = '0.0.0'; } #[\Override] @@ -98,6 +98,7 @@ protected function createEscaper(): Escaper }; } + #[\Override] protected function createWriter(Escaper $escaper, Converter $converter): Writer { // @todo Temporary deactivated, needs a way to add converter diff --git a/src/Vendor.php b/src/Vendor.php index 674bd23..018bef4 100644 --- a/src/Vendor.php +++ b/src/Vendor.php @@ -4,6 +4,8 @@ namespace MakinaCorpus\QueryBuilder; +use MakinaCorpus\QueryBuilder\Error\QueryBuilderError; + /** * RDMBS identification. * @@ -30,7 +32,7 @@ public static function versionNormalize(string $version): string if (\preg_match('/(\d+)(\.\d+|)(\.\d+|).*/ims', $version, $matches)) { return $matches[1] . ($matches[2] ?: '.0') . ($matches[3] ?: '.0'); } - throw new \Exception(\sprintf("Version '%s', is not in 'x.y.z' semantic format", $version)); + throw new QueryBuilderError(\sprintf("Version '%s', is not in 'x.y.z' semantic format", $version)); } /** @@ -47,7 +49,7 @@ public static function versionCompare(string $userGiven, string $serverVersion, '=' => 0 === \version_compare($userGiven, $serverVersion), '>=' => 0 <= \version_compare($userGiven, $serverVersion), '>' => 0 < \version_compare($userGiven, $serverVersion), - default => throw new \Exception("Version comparison operator must be one of '<', '<=', '=', '>=', '>'"), + default => throw new QueryBuilderError("Version comparison operator must be one of '<', '<=', '=', '>=', '>'"), }; }