SchemaWrapper.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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. unset($this->tablesToDelete[$tableName]);
  33. }
  34. }
  35. /**
  36. * Gets all table names
  37. *
  38. * @return array
  39. */
  40. public function getTableNamesWithoutPrefix() {
  41. $tableNames = $this->schema->getTableNames();
  42. return array_map(function ($tableName) {
  43. if (str_starts_with($tableName, $this->connection->getPrefix())) {
  44. return substr($tableName, strlen($this->connection->getPrefix()));
  45. }
  46. return $tableName;
  47. }, $tableNames);
  48. }
  49. // Overwritten methods
  50. /**
  51. * @return array
  52. */
  53. public function getTableNames() {
  54. return $this->schema->getTableNames();
  55. }
  56. /**
  57. * @param string $tableName
  58. *
  59. * @return \Doctrine\DBAL\Schema\Table
  60. * @throws \Doctrine\DBAL\Schema\SchemaException
  61. */
  62. public function getTable($tableName) {
  63. return $this->schema->getTable($this->connection->getPrefix() . $tableName);
  64. }
  65. /**
  66. * Does this schema have a table with the given name?
  67. *
  68. * @param string $tableName
  69. *
  70. * @return boolean
  71. */
  72. public function hasTable($tableName) {
  73. return $this->schema->hasTable($this->connection->getPrefix() . $tableName);
  74. }
  75. /**
  76. * Creates a new table.
  77. *
  78. * @param string $tableName
  79. * @return \Doctrine\DBAL\Schema\Table
  80. */
  81. public function createTable($tableName) {
  82. unset($this->tablesToDelete[$tableName]);
  83. return $this->schema->createTable($this->connection->getPrefix() . $tableName);
  84. }
  85. /**
  86. * Drops a table from the schema.
  87. *
  88. * @param string $tableName
  89. * @return \Doctrine\DBAL\Schema\Schema
  90. */
  91. public function dropTable($tableName) {
  92. $this->tablesToDelete[$tableName] = true;
  93. return $this->schema->dropTable($this->connection->getPrefix() . $tableName);
  94. }
  95. /**
  96. * Gets all tables of this schema.
  97. *
  98. * @return \Doctrine\DBAL\Schema\Table[]
  99. */
  100. public function getTables() {
  101. return $this->schema->getTables();
  102. }
  103. /**
  104. * Gets the DatabasePlatform for the database.
  105. *
  106. * @return AbstractPlatform
  107. *
  108. * @throws Exception
  109. */
  110. public function getDatabasePlatform() {
  111. return $this->connection->getDatabasePlatform();
  112. }
  113. }