mdb2schemawriter.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  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. $xml->addChild('charset', 'utf8');
  42. // FIX ME: bloody work around
  43. if ($config->getSystemValue('dbtype', 'sqlite') === 'oci') {
  44. $filterExpression = '/^"' . preg_quote($conn->getPrefix()) . '/';
  45. } else {
  46. $filterExpression = '/^' . preg_quote($conn->getPrefix()) . '/';
  47. }
  48. $conn->getConfiguration()->setFilterSchemaAssetsExpression($filterExpression);
  49. foreach ($conn->getSchemaManager()->listTables() as $table) {
  50. self::saveTable($table, $xml->addChild('table'));
  51. }
  52. file_put_contents($file, $xml->asXML());
  53. return true;
  54. }
  55. /**
  56. * @param \Doctrine\DBAL\Schema\Table $table
  57. * @param \SimpleXMLElement $xml
  58. */
  59. private static function saveTable($table, $xml) {
  60. $xml->addChild('name', $table->getName());
  61. $declaration = $xml->addChild('declaration');
  62. foreach($table->getColumns() as $column) {
  63. self::saveColumn($column, $declaration->addChild('field'));
  64. }
  65. foreach($table->getIndexes() as $index) {
  66. if ($index->getName() == 'PRIMARY') {
  67. $autoincrement = false;
  68. foreach($index->getColumns() as $column) {
  69. if ($table->getColumn($column)->getAutoincrement()) {
  70. $autoincrement = true;
  71. }
  72. }
  73. if ($autoincrement) {
  74. continue;
  75. }
  76. }
  77. self::saveIndex($index, $declaration->addChild('index'));
  78. }
  79. }
  80. /**
  81. * @param Column $column
  82. * @param \SimpleXMLElement $xml
  83. */
  84. private static function saveColumn($column, $xml) {
  85. $xml->addChild('name', $column->getName());
  86. switch($column->getType()) {
  87. case 'SmallInt':
  88. case 'Integer':
  89. case 'BigInt':
  90. $xml->addChild('type', 'integer');
  91. $default = $column->getDefault();
  92. if (is_null($default) && $column->getAutoincrement()) {
  93. $default = '0';
  94. }
  95. $xml->addChild('default', $default);
  96. $xml->addChild('notnull', self::toBool($column->getNotnull()));
  97. if ($column->getAutoincrement()) {
  98. $xml->addChild('autoincrement', '1');
  99. }
  100. if ($column->getUnsigned()) {
  101. $xml->addChild('unsigned', 'true');
  102. }
  103. $length = '4';
  104. if ($column->getType() == 'SmallInt') {
  105. $length = '2';
  106. }
  107. elseif ($column->getType() == 'BigInt') {
  108. $length = '8';
  109. }
  110. $xml->addChild('length', $length);
  111. break;
  112. case 'String':
  113. $xml->addChild('type', 'text');
  114. $default = trim($column->getDefault());
  115. if ($default === '') {
  116. $default = false;
  117. }
  118. $xml->addChild('default', $default);
  119. $xml->addChild('notnull', self::toBool($column->getNotnull()));
  120. $xml->addChild('length', $column->getLength());
  121. break;
  122. case 'Text':
  123. $xml->addChild('type', 'clob');
  124. $xml->addChild('notnull', self::toBool($column->getNotnull()));
  125. break;
  126. case 'Decimal':
  127. $xml->addChild('type', 'decimal');
  128. $xml->addChild('default', $column->getDefault());
  129. $xml->addChild('notnull', self::toBool($column->getNotnull()));
  130. $xml->addChild('length', '15');
  131. break;
  132. case 'Boolean':
  133. $xml->addChild('type', 'integer');
  134. $xml->addChild('default', $column->getDefault());
  135. $xml->addChild('notnull', self::toBool($column->getNotnull()));
  136. $xml->addChild('length', '1');
  137. break;
  138. case 'DateTime':
  139. $xml->addChild('type', 'timestamp');
  140. $xml->addChild('default', $column->getDefault());
  141. $xml->addChild('notnull', self::toBool($column->getNotnull()));
  142. break;
  143. }
  144. }
  145. /**
  146. * @param Index $index
  147. * @param \SimpleXMLElement $xml
  148. */
  149. private static function saveIndex($index, $xml) {
  150. $xml->addChild('name', $index->getName());
  151. if ($index->isPrimary()) {
  152. $xml->addChild('primary', 'true');
  153. }
  154. elseif ($index->isUnique()) {
  155. $xml->addChild('unique', 'true');
  156. }
  157. foreach($index->getColumns() as $column) {
  158. $field = $xml->addChild('field');
  159. $field->addChild('name', $column);
  160. $field->addChild('sorting', 'ascending');
  161. }
  162. }
  163. private static function toBool($bool) {
  164. return $bool ? 'true' : 'false';
  165. }
  166. }