PageRenderTime 24ms CodeModel.GetById 29ms RepoModel.GetById 0ms app.codeStats 0ms

/lib/private/db/mdb2schemawriter.php

https://gitlab.com/Red54/core
PHP | 171 lines | 121 code | 11 blank | 39 comment | 15 complexity | d8307a857f1ad3bf88fb1332af16e868 MD5 | raw file
  1. <?php
  2. /**
  3. * @author Bart Visscher <bartv@thisnet.nl>
  4. * @author Jörn Friedrich Dreyer <jfd@butonic.de>
  5. * @author Morris Jobke <hey@morrisjobke.de>
  6. * @author Scrutinizer Auto-Fixer <auto-fixer@scrutinizer-ci.com>
  7. * @author tbelau666 <thomas.belau@gmx.de>
  8. * @author Thomas Müller <thomas.mueller@tmit.eu>
  9. *
  10. * @copyright Copyright (c) 2015, ownCloud, Inc.
  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. class OC_DB_MDB2SchemaWriter {
  27. /**
  28. * @param string $file
  29. * @param \OC\DB\Connection $conn
  30. * @return bool
  31. */
  32. static public function saveSchemaToFile($file, \OC\DB\Connection $conn) {
  33. $config = \OC::$server->getConfig();
  34. $xml = new SimpleXMLElement('<database/>');
  35. $xml->addChild('name', $config->getSystemValue('dbname', 'owncloud'));
  36. $xml->addChild('create', 'true');
  37. $xml->addChild('overwrite', 'false');
  38. $xml->addChild('charset', 'utf8');
  39. // FIX ME: bloody work around
  40. if ($config->getSystemValue('dbtype', 'sqlite') === 'oci') {
  41. $filterExpression = '/^"' . preg_quote($conn->getPrefix()) . '/';
  42. } else {
  43. $filterExpression = '/^' . preg_quote($conn->getPrefix()) . '/';
  44. }
  45. $conn->getConfiguration()->setFilterSchemaAssetsExpression($filterExpression);
  46. foreach ($conn->getSchemaManager()->listTables() as $table) {
  47. self::saveTable($table, $xml->addChild('table'));
  48. }
  49. file_put_contents($file, $xml->asXML());
  50. return true;
  51. }
  52. /**
  53. * @param SimpleXMLElement $xml
  54. */
  55. private static function saveTable($table, $xml) {
  56. $xml->addChild('name', $table->getName());
  57. $declaration = $xml->addChild('declaration');
  58. foreach($table->getColumns() as $column) {
  59. self::saveColumn($column, $declaration->addChild('field'));
  60. }
  61. foreach($table->getIndexes() as $index) {
  62. if ($index->getName() == 'PRIMARY') {
  63. $autoincrement = false;
  64. foreach($index->getColumns() as $column) {
  65. if ($table->getColumn($column)->getAutoincrement()) {
  66. $autoincrement = true;
  67. }
  68. }
  69. if ($autoincrement) {
  70. continue;
  71. }
  72. }
  73. self::saveIndex($index, $declaration->addChild('index'));
  74. }
  75. }
  76. /**
  77. * @param SimpleXMLElement $xml
  78. */
  79. private static function saveColumn($column, $xml) {
  80. $xml->addChild('name', $column->getName());
  81. switch($column->getType()) {
  82. case 'SmallInt':
  83. case 'Integer':
  84. case 'BigInt':
  85. $xml->addChild('type', 'integer');
  86. $default = $column->getDefault();
  87. if (is_null($default) && $column->getAutoincrement()) {
  88. $default = '0';
  89. }
  90. $xml->addChild('default', $default);
  91. $xml->addChild('notnull', self::toBool($column->getNotnull()));
  92. if ($column->getAutoincrement()) {
  93. $xml->addChild('autoincrement', '1');
  94. }
  95. if ($column->getUnsigned()) {
  96. $xml->addChild('unsigned', 'true');
  97. }
  98. $length = '4';
  99. if ($column->getType() == 'SmallInt') {
  100. $length = '2';
  101. }
  102. elseif ($column->getType() == 'BigInt') {
  103. $length = '8';
  104. }
  105. $xml->addChild('length', $length);
  106. break;
  107. case 'String':
  108. $xml->addChild('type', 'text');
  109. $default = trim($column->getDefault());
  110. if ($default === '') {
  111. $default = false;
  112. }
  113. $xml->addChild('default', $default);
  114. $xml->addChild('notnull', self::toBool($column->getNotnull()));
  115. $xml->addChild('length', $column->getLength());
  116. break;
  117. case 'Text':
  118. $xml->addChild('type', 'clob');
  119. $xml->addChild('notnull', self::toBool($column->getNotnull()));
  120. break;
  121. case 'Decimal':
  122. $xml->addChild('type', 'decimal');
  123. $xml->addChild('default', $column->getDefault());
  124. $xml->addChild('notnull', self::toBool($column->getNotnull()));
  125. $xml->addChild('length', '15');
  126. break;
  127. case 'Boolean':
  128. $xml->addChild('type', 'integer');
  129. $xml->addChild('default', $column->getDefault());
  130. $xml->addChild('notnull', self::toBool($column->getNotnull()));
  131. $xml->addChild('length', '1');
  132. break;
  133. case 'DateTime':
  134. $xml->addChild('type', 'timestamp');
  135. $xml->addChild('default', $column->getDefault());
  136. $xml->addChild('notnull', self::toBool($column->getNotnull()));
  137. break;
  138. }
  139. }
  140. /**
  141. * @param SimpleXMLElement $xml
  142. */
  143. private static function saveIndex($index, $xml) {
  144. $xml->addChild('name', $index->getName());
  145. if ($index->isPrimary()) {
  146. $xml->addChild('primary', 'true');
  147. }
  148. elseif ($index->isUnique()) {
  149. $xml->addChild('unique', 'true');
  150. }
  151. foreach($index->getColumns() as $column) {
  152. $field = $xml->addChild('field');
  153. $field->addChild('name', $column);
  154. $field->addChild('sorting', 'ascending');
  155. }
  156. }
  157. private static function toBool($bool) {
  158. return $bool ? 'true' : 'false';
  159. }
  160. }