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 3.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. <?php
  2. /**
  3. * Copyright (c) 2012 Bart Visscher <bartv@thisnet.nl>
  4. * This file is licensed under the Affero General Public License version 3 or
  5. * later.
  6. * See the COPYING-README file.
  7. */
  8. class OC_DB_MDB2SchemaWriter {
  9. /**
  10. * @param $file
  11. * @param \Doctrine\DBAL\Schema\AbstractSchemaManager $sm
  12. * @return bool
  13. */
  14. static public function saveSchemaToFile($file, $sm) {
  15. $xml = new SimpleXMLElement('<database/>');
  16. $xml->addChild('name', OC_Config::getValue( "dbname", "owncloud" ));
  17. $xml->addChild('create', 'true');
  18. $xml->addChild('overwrite', 'false');
  19. $xml->addChild('charset', 'utf8');
  20. foreach ($sm->listTables() as $table) {
  21. self::saveTable($table, $xml->addChild('table'));
  22. }
  23. file_put_contents($file, $xml->asXML());
  24. return true;
  25. }
  26. private static function saveTable($table, $xml) {
  27. $xml->addChild('name', $table->getName());
  28. $declaration = $xml->addChild('declaration');
  29. foreach($table->getColumns() as $column) {
  30. self::saveColumn($column, $declaration->addChild('field'));
  31. }
  32. foreach($table->getIndexes() as $index) {
  33. if ($index->getName() == 'PRIMARY') {
  34. $autoincrement = false;
  35. foreach($index->getColumns() as $column) {
  36. if ($table->getColumn($column)->getAutoincrement()) {
  37. $autoincrement = true;
  38. }
  39. }
  40. if ($autoincrement) {
  41. continue;
  42. }
  43. }
  44. self::saveIndex($index, $declaration->addChild('index'));
  45. }
  46. }
  47. private static function saveColumn($column, $xml) {
  48. $xml->addChild('name', $column->getName());
  49. switch($column->getType()) {
  50. case 'SmallInt':
  51. case 'Integer':
  52. case 'BigInt':
  53. $xml->addChild('type', 'integer');
  54. $default = $column->getDefault();
  55. if (is_null($default) && $column->getAutoincrement()) {
  56. $default = '0';
  57. }
  58. $xml->addChild('default', $default);
  59. $xml->addChild('notnull', self::toBool($column->getNotnull()));
  60. if ($column->getAutoincrement()) {
  61. $xml->addChild('autoincrement', '1');
  62. }
  63. if ($column->getUnsigned()) {
  64. $xml->addChild('unsigned', 'true');
  65. }
  66. $length = '4';
  67. if ($column->getType() == 'SmallInt') {
  68. $length = '2';
  69. }
  70. elseif ($column->getType() == 'BigInt') {
  71. $length = '8';
  72. }
  73. $xml->addChild('length', $length);
  74. break;
  75. case 'String':
  76. $xml->addChild('type', 'text');
  77. $default = trim($column->getDefault());
  78. if ($default === '') {
  79. $default = false;
  80. }
  81. $xml->addChild('default', $default);
  82. $xml->addChild('notnull', self::toBool($column->getNotnull()));
  83. $xml->addChild('length', $column->getLength());
  84. break;
  85. case 'Text':
  86. $xml->addChild('type', 'clob');
  87. $xml->addChild('notnull', self::toBool($column->getNotnull()));
  88. break;
  89. case 'Decimal':
  90. $xml->addChild('type', 'decimal');
  91. $xml->addChild('default', $column->getDefault());
  92. $xml->addChild('notnull', self::toBool($column->getNotnull()));
  93. $xml->addChild('length', '15');
  94. break;
  95. case 'Boolean':
  96. $xml->addChild('type', 'integer');
  97. $xml->addChild('default', $column->getDefault());
  98. $xml->addChild('notnull', self::toBool($column->getNotnull()));
  99. $xml->addChild('length', '1');
  100. break;
  101. case 'DateTime':
  102. $xml->addChild('type', 'timestamp');
  103. $xml->addChild('default', $column->getDefault());
  104. $xml->addChild('notnull', self::toBool($column->getNotnull()));
  105. break;
  106. }
  107. }
  108. private static function saveIndex($index, $xml) {
  109. $xml->addChild('name', $index->getName());
  110. if ($index->isPrimary()) {
  111. $xml->addChild('primary', 'true');
  112. }
  113. elseif ($index->isUnique()) {
  114. $xml->addChild('unique', 'true');
  115. }
  116. foreach($index->getColumns() as $column) {
  117. $field = $xml->addChild('field');
  118. $field->addChild('name', $column);
  119. $field->addChild('sorting', 'ascending');
  120. }
  121. }
  122. private static function toBool($bool) {
  123. return $bool ? 'true' : 'false';
  124. }
  125. }