You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

136 lines
3.3 KiB

3 years ago
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2017 Joas Schilling <coding@schilljs.com>
  4. *
  5. * @author Christoph Wurst <christoph@winzerhof-wurst.at>
  6. * @author Joas Schilling <coding@schilljs.com>
  7. *
  8. * @license GNU AGPL version 3 or any later version
  9. *
  10. * This program is free software: you can redistribute it and/or modify
  11. * it under the terms of the GNU Affero General Public License as
  12. * published by the Free Software Foundation, either version 3 of the
  13. * License, or (at your option) any later version.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU Affero General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU Affero General Public License
  21. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  22. *
  23. */
  24. namespace OC\DB;
  25. use Doctrine\DBAL\Schema\Schema;
  26. use OCP\DB\ISchemaWrapper;
  27. use OCP\IDBConnection;
  28. class SchemaWrapper implements ISchemaWrapper {
  29. /** @var IDBConnection|Connection */
  30. protected $connection;
  31. /** @var Schema */
  32. protected $schema;
  33. /** @var array */
  34. protected $tablesToDelete = [];
  35. /**
  36. * @param IDBConnection $connection
  37. */
  38. public function __construct(IDBConnection $connection) {
  39. $this->connection = $connection;
  40. $this->schema = $this->connection->createSchema();
  41. }
  42. public function getWrappedSchema() {
  43. return $this->schema;
  44. }
  45. public function performDropTableCalls() {
  46. foreach ($this->tablesToDelete as $tableName => $true) {
  47. $this->connection->dropTable($tableName);
  48. unset($this->tablesToDelete[$tableName]);
  49. }
  50. }
  51. /**
  52. * Gets all table names
  53. *
  54. * @return array
  55. */
  56. public function getTableNamesWithoutPrefix() {
  57. $tableNames = $this->schema->getTableNames();
  58. return array_map(function ($tableName) {
  59. if (strpos($tableName, $this->connection->getPrefix()) === 0) {
  60. return substr($tableName, strlen($this->connection->getPrefix()));
  61. }
  62. return $tableName;
  63. }, $tableNames);
  64. }
  65. // Overwritten methods
  66. /**
  67. * @return array
  68. */
  69. public function getTableNames() {
  70. return $this->schema->getTableNames();
  71. }
  72. /**
  73. * @param string $tableName
  74. *
  75. * @return \Doctrine\DBAL\Schema\Table
  76. * @throws \Doctrine\DBAL\Schema\SchemaException
  77. */
  78. public function getTable($tableName) {
  79. return $this->schema->getTable($this->connection->getPrefix() . $tableName);
  80. }
  81. /**
  82. * Does this schema have a table with the given name?
  83. *
  84. * @param string $tableName
  85. *
  86. * @return boolean
  87. */
  88. public function hasTable($tableName) {
  89. return $this->schema->hasTable($this->connection->getPrefix() . $tableName);
  90. }
  91. /**
  92. * Creates a new table.
  93. *
  94. * @param string $tableName
  95. * @return \Doctrine\DBAL\Schema\Table
  96. */
  97. public function createTable($tableName) {
  98. return $this->schema->createTable($this->connection->getPrefix() . $tableName);
  99. }
  100. /**
  101. * Drops a table from the schema.
  102. *
  103. * @param string $tableName
  104. * @return \Doctrine\DBAL\Schema\Schema
  105. */
  106. public function dropTable($tableName) {
  107. $this->tablesToDelete[$tableName] = true;
  108. return $this->schema->dropTable($this->connection->getPrefix() . $tableName);
  109. }
  110. /**
  111. * Gets all tables of this schema.
  112. *
  113. * @return \Doctrine\DBAL\Schema\Table[]
  114. */
  115. public function getTables() {
  116. return $this->schema->getTables();
  117. }
  118. }