1
0

SchemaWrapper.php 3.3 KB

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