SchemaWrapper.php 2.8 KB

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