Skip to content

Commit

Permalink
no issue - various code cleanups
Browse files Browse the repository at this point in the history
  • Loading branch information
pounard committed Apr 18, 2024
1 parent cde05fc commit 6364fac
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 60 deletions.
3 changes: 2 additions & 1 deletion phpstan.neon.dist
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
parameters:
phpVersion: 80300
phpVersion: 80100
level: 3
paths:
- src
- tests
92 changes: 45 additions & 47 deletions src/Bridge/AbstractBridge.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -97,56 +98,50 @@ 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
{
if ($this->serverNameLookedUp) {
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;
}

Expand Down Expand Up @@ -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)) {
Expand Down Expand Up @@ -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
Expand All @@ -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();
}

/**
Expand Down Expand Up @@ -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();
}

/**
Expand Down
15 changes: 6 additions & 9 deletions src/Bridge/Bridge.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
3 changes: 2 additions & 1 deletion src/Bridge/Doctrine/DoctrineQueryBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand All @@ -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
Expand Down
6 changes: 4 additions & 2 deletions src/Vendor.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

namespace MakinaCorpus\QueryBuilder;

use MakinaCorpus\QueryBuilder\Error\QueryBuilderError;

/**
* RDMBS identification.
*
Expand All @@ -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));
}

/**
Expand All @@ -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 '<', '<=', '=', '>=', '>'"),
};
}

Expand Down

0 comments on commit 6364fac

Please sign in to comment.