SchemaWrapper.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. <?php
  2. /**
  3. * SPDX-FileCopyrightText: 2017 Nextcloud GmbH and Nextcloud contributors
  4. * SPDX-License-Identifier: AGPL-3.0-or-later
  5. */
  6. namespace OC\DB;
  7. use Doctrine\DBAL\Exception;
  8. use Doctrine\DBAL\Platforms\AbstractPlatform;
  9. use Doctrine\DBAL\Schema\Schema;
  10. use OCP\DB\ISchemaWrapper;
  11. class SchemaWrapper implements ISchemaWrapper {
  12. /** @var Connection */
  13. protected $connection;
  14. /** @var Schema */
  15. protected $schema;
  16. /** @var array */
  17. protected $tablesToDelete = [];
  18. public function __construct(Connection $connection, ?Schema $schema = null) {
  19. $this->connection = $connection;
  20. if ($schema) {
  21. $this->schema = $schema;
  22. } else {
  23. $this->schema = $this->connection->createSchema();
  24. }
  25. }
  26. public function getWrappedSchema() {
  27. return $this->schema;
  28. }
  29. public function performDropTableCalls() {
  30. foreach ($this->tablesToDelete as $tableName => $true) {
  31. $this->connection->dropTable($tableName);
  32. foreach ($this->connection->getShardConnections() as $shardConnection) {
  33. $shardConnection->dropTable($tableName);
  34. }
  35. unset($this->tablesToDelete[$tableName]);
  36. }
  37. }
  38. /**
  39. * Gets all table names
  40. *
  41. * @return array
  42. */
  43. public function getTableNamesWithoutPrefix() {
  44. $tableNames = $this->schema->getTableNames();
  45. return array_map(function ($tableName) {
  46. if (str_starts_with($tableName, $this->connection->getPrefix())) {
  47. return substr($tableName, strlen($this->connection->getPrefix()));
  48. }
  49. return $tableName;
  50. }, $tableNames);
  51. }
  52. // Overwritten methods
  53. /**
  54. * @return array
  55. */
  56. public function getTableNames() {
  57. return $this->schema->getTableNames();
  58. }
  59. /**
  60. * @param string $tableName
  61. *
  62. * @return \Doctrine\DBAL\Schema\Table
  63. * @throws \Doctrine\DBAL\Schema\SchemaException
  64. */
  65. public function getTable($tableName) {
  66. return $this->schema->getTable($this->connection->getPrefix() . $tableName);
  67. }
  68. /**
  69. * Does this schema have a table with the given name?
  70. *
  71. * @param string $tableName
  72. *
  73. * @return boolean
  74. */
  75. public function hasTable($tableName) {
  76. return $this->schema->hasTable($this->connection->getPrefix() . $tableName);
  77. }
  78. /**
  79. * Creates a new table.
  80. *
  81. * @param string $tableName
  82. * @return \Doctrine\DBAL\Schema\Table
  83. */
  84. public function createTable($tableName) {
  85. unset($this->tablesToDelete[$tableName]);
  86. return $this->schema->createTable($this->connection->getPrefix() . $tableName);
  87. }
  88. /**
  89. * Drops a table from the schema.
  90. *
  91. * @param string $tableName
  92. * @return \Doctrine\DBAL\Schema\Schema
  93. */
  94. public function dropTable($tableName) {
  95. $this->tablesToDelete[$tableName] = true;
  96. return $this->schema->dropTable($this->connection->getPrefix() . $tableName);
  97. }
  98. /**
  99. * Gets all tables of this schema.
  100. *
  101. * @return \Doctrine\DBAL\Schema\Table[]
  102. */
  103. public function getTables() {
  104. return $this->schema->getTables();
  105. }
  106. /**
  107. * Gets the DatabasePlatform for the database.
  108. *
  109. * @return AbstractPlatform
  110. *
  111. * @throws Exception
  112. */
  113. public function getDatabasePlatform() {
  114. return $this->connection->getDatabasePlatform();
  115. }
  116. }