SchemaWrapper.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  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\Exception;
  26. use Doctrine\DBAL\Platforms\AbstractPlatform;
  27. use Doctrine\DBAL\Schema\Schema;
  28. use OCP\DB\ISchemaWrapper;
  29. class SchemaWrapper implements ISchemaWrapper {
  30. /** @var Connection */
  31. protected $connection;
  32. /** @var Schema */
  33. protected $schema;
  34. /** @var array */
  35. protected $tablesToDelete = [];
  36. public function __construct(Connection $connection) {
  37. $this->connection = $connection;
  38. $this->schema = $this->connection->createSchema();
  39. }
  40. public function getWrappedSchema() {
  41. return $this->schema;
  42. }
  43. public function performDropTableCalls() {
  44. foreach ($this->tablesToDelete as $tableName => $true) {
  45. $this->connection->dropTable($tableName);
  46. unset($this->tablesToDelete[$tableName]);
  47. }
  48. }
  49. /**
  50. * Gets all table names
  51. *
  52. * @return array
  53. */
  54. public function getTableNamesWithoutPrefix() {
  55. $tableNames = $this->schema->getTableNames();
  56. return array_map(function ($tableName) {
  57. if (strpos($tableName, $this->connection->getPrefix()) === 0) {
  58. return substr($tableName, strlen($this->connection->getPrefix()));
  59. }
  60. return $tableName;
  61. }, $tableNames);
  62. }
  63. // Overwritten methods
  64. /**
  65. * @return array
  66. */
  67. public function getTableNames() {
  68. return $this->schema->getTableNames();
  69. }
  70. /**
  71. * @param string $tableName
  72. *
  73. * @return \Doctrine\DBAL\Schema\Table
  74. * @throws \Doctrine\DBAL\Schema\SchemaException
  75. */
  76. public function getTable($tableName) {
  77. return $this->schema->getTable($this->connection->getPrefix() . $tableName);
  78. }
  79. /**
  80. * Does this schema have a table with the given name?
  81. *
  82. * @param string $tableName
  83. *
  84. * @return boolean
  85. */
  86. public function hasTable($tableName) {
  87. return $this->schema->hasTable($this->connection->getPrefix() . $tableName);
  88. }
  89. /**
  90. * Creates a new table.
  91. *
  92. * @param string $tableName
  93. * @return \Doctrine\DBAL\Schema\Table
  94. */
  95. public function createTable($tableName) {
  96. unset($this->tablesToDelete[$tableName]);
  97. return $this->schema->createTable($this->connection->getPrefix() . $tableName);
  98. }
  99. /**
  100. * Drops a table from the schema.
  101. *
  102. * @param string $tableName
  103. * @return \Doctrine\DBAL\Schema\Schema
  104. */
  105. public function dropTable($tableName) {
  106. $this->tablesToDelete[$tableName] = true;
  107. return $this->schema->dropTable($this->connection->getPrefix() . $tableName);
  108. }
  109. /**
  110. * Gets all tables of this schema.
  111. *
  112. * @return \Doctrine\DBAL\Schema\Table[]
  113. */
  114. public function getTables() {
  115. return $this->schema->getTables();
  116. }
  117. /**
  118. * Gets the DatabasePlatform for the database.
  119. *
  120. * @return AbstractPlatform
  121. *
  122. * @throws Exception
  123. */
  124. public function getDatabasePlatform() {
  125. return $this->connection->getDatabasePlatform();
  126. }
  127. }