2017-06-07 09:15:53 -04:00
|
|
|
<?php
|
|
|
|
|
/**
|
2024-05-23 03:26:56 -04:00
|
|
|
* SPDX-FileCopyrightText: 2017 Nextcloud GmbH and Nextcloud contributors
|
|
|
|
|
* SPDX-License-Identifier: AGPL-3.0-or-later
|
2017-06-07 09:15:53 -04:00
|
|
|
*/
|
|
|
|
|
namespace OC\DB;
|
|
|
|
|
|
|
|
|
|
use Doctrine\DBAL\Schema\Schema;
|
2018-01-17 05:35:20 -05:00
|
|
|
use OCP\DB\ISchemaWrapper;
|
2017-06-07 09:15:53 -04:00
|
|
|
|
2018-01-17 05:35:20 -05:00
|
|
|
class SchemaWrapper implements ISchemaWrapper {
|
2021-01-03 09:28:31 -05:00
|
|
|
/** @var Connection */
|
2017-06-07 09:15:53 -04:00
|
|
|
protected $connection;
|
|
|
|
|
|
|
|
|
|
/** @var Schema */
|
|
|
|
|
protected $schema;
|
|
|
|
|
|
|
|
|
|
/** @var array */
|
2017-07-19 07:15:32 -04:00
|
|
|
protected $tablesToDelete = [];
|
2017-06-07 09:15:53 -04:00
|
|
|
|
2024-06-28 11:08:46 -04:00
|
|
|
public function __construct(Connection $connection, ?Schema $schema = null) {
|
2017-06-07 09:15:53 -04:00
|
|
|
$this->connection = $connection;
|
2024-06-28 11:08:46 -04:00
|
|
|
if ($schema) {
|
|
|
|
|
$this->schema = $schema;
|
|
|
|
|
} else {
|
|
|
|
|
$this->schema = $this->connection->createSchema();
|
|
|
|
|
}
|
2017-06-07 09:15:53 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function getWrappedSchema() {
|
|
|
|
|
return $this->schema;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function performDropTableCalls() {
|
|
|
|
|
foreach ($this->tablesToDelete as $tableName => $true) {
|
|
|
|
|
$this->connection->dropTable($tableName);
|
2024-07-31 12:41:11 -04:00
|
|
|
foreach ($this->connection->getShardConnections() as $shardConnection) {
|
|
|
|
|
$shardConnection->dropTable($tableName);
|
|
|
|
|
}
|
2017-06-07 09:15:53 -04:00
|
|
|
unset($this->tablesToDelete[$tableName]);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function getTableNamesWithoutPrefix() {
|
|
|
|
|
$tableNames = $this->schema->getTableNames();
|
2020-04-09 07:53:40 -04:00
|
|
|
return array_map(function ($tableName) {
|
2023-05-15 07:47:19 -04:00
|
|
|
if (str_starts_with($tableName, $this->connection->getPrefix())) {
|
2017-06-07 09:15:53 -04:00
|
|
|
return substr($tableName, strlen($this->connection->getPrefix()));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return $tableName;
|
|
|
|
|
}, $tableNames);
|
|
|
|
|
}
|
|
|
|
|
|
2018-01-17 05:35:20 -05:00
|
|
|
public function getTableNames() {
|
|
|
|
|
return $this->schema->getTableNames();
|
|
|
|
|
}
|
|
|
|
|
|
2017-06-07 09:15:53 -04:00
|
|
|
public function getTable($tableName) {
|
|
|
|
|
return $this->schema->getTable($this->connection->getPrefix() . $tableName);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function hasTable($tableName) {
|
|
|
|
|
return $this->schema->hasTable($this->connection->getPrefix() . $tableName);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function createTable($tableName) {
|
2020-11-16 13:34:38 -05:00
|
|
|
unset($this->tablesToDelete[$tableName]);
|
2017-06-07 09:15:53 -04:00
|
|
|
return $this->schema->createTable($this->connection->getPrefix() . $tableName);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function dropTable($tableName) {
|
|
|
|
|
$this->tablesToDelete[$tableName] = true;
|
|
|
|
|
return $this->schema->dropTable($this->connection->getPrefix() . $tableName);
|
|
|
|
|
}
|
|
|
|
|
|
2018-01-17 05:35:20 -05:00
|
|
|
public function getTables() {
|
|
|
|
|
return $this->schema->getTables();
|
2017-06-07 09:15:53 -04:00
|
|
|
}
|
2021-08-26 08:25:17 -04:00
|
|
|
|
|
|
|
|
public function getDatabasePlatform() {
|
|
|
|
|
return $this->connection->getDatabasePlatform();
|
|
|
|
|
}
|
2017-06-07 09:15:53 -04:00
|
|
|
}
|