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.

MDB2SchemaWriter.php 5.4KB

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