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.

181 lines
5.4 KiB

3 years ago
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. *
  5. * @author Bart Visscher <bartv@thisnet.nl>
  6. * @author Christoph Wurst <christoph@winzerhof-wurst.at>
  7. * @author Jörn Friedrich Dreyer <jfd@butonic.de>
  8. * @author Morris Jobke <hey@morrisjobke.de>
  9. * @author tbelau666 <thomas.belau@gmx.de>
  10. * @author Thomas Müller <thomas.mueller@tmit.eu>
  11. *
  12. * @license AGPL-3.0
  13. *
  14. * This code is free software: you can redistribute it and/or modify
  15. * it under the terms of the GNU Affero General Public License, version 3,
  16. * as published by the Free Software Foundation.
  17. *
  18. * This program is distributed in the hope that it will be useful,
  19. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  20. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  21. * GNU Affero General Public License for more details.
  22. *
  23. * You should have received a copy of the GNU Affero General Public License, version 3,
  24. * along with this program. If not, see <http://www.gnu.org/licenses/>
  25. *
  26. */
  27. namespace OC\DB;
  28. use Doctrine\DBAL\Schema\Column;
  29. use Doctrine\DBAL\Schema\Index;
  30. class MDB2SchemaWriter {
  31. /**
  32. * @param string $file
  33. * @param \OC\DB\Connection $conn
  34. * @return bool
  35. */
  36. public static function saveSchemaToFile($file, \OC\DB\Connection $conn) {
  37. $config = \OC::$server->getConfig();
  38. $xml = new \SimpleXMLElement('<database/>');
  39. $xml->addChild('name', $config->getSystemValue('dbname', 'owncloud'));
  40. $xml->addChild('create', 'true');
  41. $xml->addChild('overwrite', 'false');
  42. if ($config->getSystemValue('dbtype', 'sqlite') === 'mysql' && $config->getSystemValue('mysql.utf8mb4', false)) {
  43. $xml->addChild('charset', 'utf8mb4');
  44. } else {
  45. $xml->addChild('charset', 'utf8');
  46. }
  47. // FIX ME: bloody work around
  48. if ($config->getSystemValue('dbtype', 'sqlite') === 'oci') {
  49. $filterExpression = '/^"' . preg_quote($conn->getPrefix()) . '/';
  50. } else {
  51. $filterExpression = '/^' . preg_quote($conn->getPrefix()) . '/';
  52. }
  53. $conn->getConfiguration()->setFilterSchemaAssetsExpression($filterExpression);
  54. foreach ($conn->getSchemaManager()->listTables() as $table) {
  55. self::saveTable($table, $xml->addChild('table'));
  56. }
  57. file_put_contents($file, $xml->asXML());
  58. return true;
  59. }
  60. /**
  61. * @param \Doctrine\DBAL\Schema\Table $table
  62. * @param \SimpleXMLElement $xml
  63. */
  64. private static function saveTable($table, $xml) {
  65. $xml->addChild('name', $table->getName());
  66. $declaration = $xml->addChild('declaration');
  67. foreach ($table->getColumns() as $column) {
  68. self::saveColumn($column, $declaration->addChild('field'));
  69. }
  70. foreach ($table->getIndexes() as $index) {
  71. if ($index->getName() == 'PRIMARY') {
  72. $autoincrement = false;
  73. foreach ($index->getColumns() as $column) {
  74. if ($table->getColumn($column)->getAutoincrement()) {
  75. $autoincrement = true;
  76. }
  77. }
  78. if ($autoincrement) {
  79. continue;
  80. }
  81. }
  82. self::saveIndex($index, $declaration->addChild('index'));
  83. }
  84. }
  85. /**
  86. * @param Column $column
  87. * @param \SimpleXMLElement $xml
  88. */
  89. private static function saveColumn($column, $xml) {
  90. $xml->addChild('name', $column->getName());
  91. switch ($column->getType()) {
  92. case 'SmallInt':
  93. case 'Integer':
  94. case 'BigInt':
  95. $xml->addChild('type', 'integer');
  96. $default = $column->getDefault();
  97. if (is_null($default) && $column->getAutoincrement()) {
  98. $default = '0';
  99. }
  100. $xml->addChild('default', $default);
  101. $xml->addChild('notnull', self::toBool($column->getNotnull()));
  102. if ($column->getAutoincrement()) {
  103. $xml->addChild('autoincrement', '1');
  104. }
  105. if ($column->getUnsigned()) {
  106. $xml->addChild('unsigned', 'true');
  107. }
  108. $length = '4';
  109. if ($column->getType() == 'SmallInt') {
  110. $length = '2';
  111. } elseif ($column->getType() == 'BigInt') {
  112. $length = '8';
  113. }
  114. $xml->addChild('length', $length);
  115. break;
  116. case 'String':
  117. $xml->addChild('type', 'text');
  118. $default = trim($column->getDefault());
  119. if ($default === '') {
  120. $default = false;
  121. }
  122. $xml->addChild('default', $default);
  123. $xml->addChild('notnull', self::toBool($column->getNotnull()));
  124. $xml->addChild('length', $column->getLength());
  125. break;
  126. case 'Text':
  127. $xml->addChild('type', 'clob');
  128. $xml->addChild('notnull', self::toBool($column->getNotnull()));
  129. break;
  130. case 'Decimal':
  131. $xml->addChild('type', 'decimal');
  132. $xml->addChild('default', $column->getDefault());
  133. $xml->addChild('notnull', self::toBool($column->getNotnull()));
  134. $xml->addChild('length', '15');
  135. break;
  136. case 'Boolean':
  137. $xml->addChild('type', 'integer');
  138. $xml->addChild('default', $column->getDefault());
  139. $xml->addChild('notnull', self::toBool($column->getNotnull()));
  140. $xml->addChild('length', '1');
  141. break;
  142. case 'DateTime':
  143. $xml->addChild('type', 'timestamp');
  144. $xml->addChild('default', $column->getDefault());
  145. $xml->addChild('notnull', self::toBool($column->getNotnull()));
  146. break;
  147. }
  148. }
  149. /**
  150. * @param Index $index
  151. * @param \SimpleXMLElement $xml
  152. */
  153. private static function saveIndex($index, $xml) {
  154. $xml->addChild('name', $index->getName());
  155. if ($index->isPrimary()) {
  156. $xml->addChild('primary', 'true');
  157. } elseif ($index->isUnique()) {
  158. $xml->addChild('unique', 'true');
  159. }
  160. foreach ($index->getColumns() as $column) {
  161. $field = $xml->addChild('field');
  162. $field->addChild('name', $column);
  163. $field->addChild('sorting', 'ascending');
  164. }
  165. }
  166. private static function toBool($bool) {
  167. return $bool ? 'true' : 'false';
  168. }
  169. }