Skip to content

Commit

Permalink
[Cache] Remove database sever version detection
Browse files Browse the repository at this point in the history
  • Loading branch information
derrabus committed Nov 24, 2023
1 parent c9ac1b9 commit bc3a79f
Showing 1 changed file with 6 additions and 22 deletions.
28 changes: 6 additions & 22 deletions src/Symfony/Component/Cache/Adapter/DoctrineDbalAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ class DoctrineDbalAdapter extends AbstractAdapter implements PruneableInterface
private MarshallerInterface $marshaller;
private Connection $conn;
private string $platformName;
private string $serverVersion;
private string $table = 'cache_items';
private string $idCol = 'item_id';
private string $dataCol = 'item_data';
Expand Down Expand Up @@ -239,27 +238,27 @@ protected function doSave(array $values, int $lifetime): array|bool
$platformName = $this->getPlatformName();
$insertSql = "INSERT INTO $this->table ($this->idCol, $this->dataCol, $this->lifetimeCol, $this->timeCol) VALUES (?, ?, ?, ?)";

switch (true) {
case 'mysql' === $platformName:
switch ($platformName) {
case 'mysql':
$sql = $insertSql." ON DUPLICATE KEY UPDATE $this->dataCol = VALUES($this->dataCol), $this->lifetimeCol = VALUES($this->lifetimeCol), $this->timeCol = VALUES($this->timeCol)";
break;
case 'oci' === $platformName:
case 'oci':
// DUAL is Oracle specific dummy table
$sql = "MERGE INTO $this->table USING DUAL ON ($this->idCol = ?) ".
"WHEN NOT MATCHED THEN INSERT ($this->idCol, $this->dataCol, $this->lifetimeCol, $this->timeCol) VALUES (?, ?, ?, ?) ".
"WHEN MATCHED THEN UPDATE SET $this->dataCol = ?, $this->lifetimeCol = ?, $this->timeCol = ?";
break;
case 'sqlsrv' === $platformName && version_compare($this->getServerVersion(), '10', '>='):
case 'sqlsrv':
// MERGE is only available since SQL Server 2008 and must be terminated by semicolon
// It also requires HOLDLOCK according to http://weblogs.sqlteam.com/dang/archive/2009/01/31/UPSERT-Race-Condition-With-MERGE.aspx
$sql = "MERGE INTO $this->table WITH (HOLDLOCK) USING (SELECT 1 AS dummy) AS src ON ($this->idCol = ?) ".
"WHEN NOT MATCHED THEN INSERT ($this->idCol, $this->dataCol, $this->lifetimeCol, $this->timeCol) VALUES (?, ?, ?, ?) ".
"WHEN MATCHED THEN UPDATE SET $this->dataCol = ?, $this->lifetimeCol = ?, $this->timeCol = ?;";
break;
case 'sqlite' === $platformName:
case 'sqlite':
$sql = 'INSERT OR REPLACE'.substr($insertSql, 6);
break;
case 'pgsql' === $platformName && version_compare($this->getServerVersion(), '9.5', '>='):
case 'pgsql':
$sql = $insertSql." ON CONFLICT ($this->idCol) DO UPDATE SET ($this->dataCol, $this->lifetimeCol, $this->timeCol) = (EXCLUDED.$this->dataCol, EXCLUDED.$this->lifetimeCol, EXCLUDED.$this->timeCol)";
break;
default:
Expand Down Expand Up @@ -369,21 +368,6 @@ private function getPlatformName(): string
};
}

private function getServerVersion(): string
{
if (isset($this->serverVersion)) {
return $this->serverVersion;
}

// The condition should be removed once support for DBAL <3.3 is dropped
$conn = method_exists($this->conn, 'getNativeConnection') ? $this->conn->getNativeConnection() : $this->conn->getWrappedConnection();
if ($conn instanceof ServerInfoAwareConnection) {
return $this->serverVersion = $conn->getServerVersion();
}

return $this->serverVersion = '0';
}

private function addTableToSchema(Schema $schema): void
{
$types = [
Expand Down

0 comments on commit bc3a79f

Please sign in to comment.