mdb2schemareader.php 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  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. namespace OC\DB;
  9. class MDB2SchemaReader {
  10. /**
  11. * @var string $DBNAME
  12. */
  13. protected $DBNAME;
  14. /**
  15. * @var string $DBTABLEPREFIX
  16. */
  17. protected $DBTABLEPREFIX;
  18. /**
  19. * @var \Doctrine\DBAL\Platforms\AbstractPlatform $platform
  20. */
  21. protected $platform;
  22. /**
  23. * @param \OC\Config $config
  24. * @param \Doctrine\DBAL\Platforms\AbstractPlatform $platform
  25. */
  26. public function __construct($config, $platform) {
  27. $this->platform = $platform;
  28. $this->DBNAME = $config->getValue('dbname', 'owncloud');
  29. $this->DBTABLEPREFIX = $config->getValue('dbtableprefix', 'oc_');
  30. }
  31. /**
  32. * @param string $file
  33. * @return \Doctrine\DBAL\Schema\Schema
  34. * @throws \DomainException
  35. */
  36. public function loadSchemaFromFile($file) {
  37. $schema = new \Doctrine\DBAL\Schema\Schema();
  38. $xml = simplexml_load_file($file);
  39. foreach ($xml->children() as $child) {
  40. /**
  41. * @var \SimpleXMLElement $child
  42. */
  43. switch ($child->getName()) {
  44. case 'name':
  45. case 'create':
  46. case 'overwrite':
  47. case 'charset':
  48. break;
  49. case 'table':
  50. $this->loadTable($schema, $child);
  51. break;
  52. default:
  53. throw new \DomainException('Unknown element: ' . $child->getName());
  54. }
  55. }
  56. return $schema;
  57. }
  58. /**
  59. * @param\Doctrine\DBAL\Schema\Schema $schema
  60. * @param \SimpleXMLElement $xml
  61. * @throws \DomainException
  62. */
  63. private function loadTable($schema, $xml) {
  64. $table = null;
  65. foreach ($xml->children() as $child) {
  66. /**
  67. * @var \SimpleXMLElement $child
  68. */
  69. switch ($child->getName()) {
  70. case 'name':
  71. $name = (string)$child;
  72. $name = str_replace('*dbprefix*', $this->DBTABLEPREFIX, $name);
  73. $name = $this->platform->quoteIdentifier($name);
  74. $table = $schema->createTable($name);
  75. break;
  76. case 'create':
  77. case 'overwrite':
  78. case 'charset':
  79. break;
  80. case 'declaration':
  81. if (is_null($table)) {
  82. throw new \DomainException('Table declaration before table name');
  83. }
  84. $this->loadDeclaration($table, $child);
  85. break;
  86. default:
  87. throw new \DomainException('Unknown element: ' . $child->getName());
  88. }
  89. }
  90. }
  91. /**
  92. * @param \Doctrine\DBAL\Schema\Table $table
  93. * @param \SimpleXMLElement $xml
  94. * @throws \DomainException
  95. */
  96. private function loadDeclaration($table, $xml) {
  97. foreach ($xml->children() as $child) {
  98. /**
  99. * @var \SimpleXMLElement $child
  100. */
  101. switch ($child->getName()) {
  102. case 'field':
  103. $this->loadField($table, $child);
  104. break;
  105. case 'index':
  106. $this->loadIndex($table, $child);
  107. break;
  108. default:
  109. throw new \DomainException('Unknown element: ' . $child->getName());
  110. }
  111. }
  112. }
  113. /**
  114. * @param \Doctrine\DBAL\Schema\Table $table
  115. * @param \SimpleXMLElement $xml
  116. * @throws \DomainException
  117. */
  118. private function loadField($table, $xml) {
  119. $options = array();
  120. foreach ($xml->children() as $child) {
  121. /**
  122. * @var \SimpleXMLElement $child
  123. */
  124. switch ($child->getName()) {
  125. case 'name':
  126. $name = (string)$child;
  127. $name = $this->platform->quoteIdentifier($name);
  128. break;
  129. case 'type':
  130. $type = (string)$child;
  131. switch ($type) {
  132. case 'text':
  133. $type = 'string';
  134. break;
  135. case 'clob':
  136. $type = 'text';
  137. break;
  138. case 'timestamp':
  139. $type = 'datetime';
  140. break;
  141. }
  142. break;
  143. case 'length':
  144. $length = (string)$child;
  145. $options['length'] = $length;
  146. break;
  147. case 'unsigned':
  148. $unsigned = $this->asBool($child);
  149. $options['unsigned'] = $unsigned;
  150. break;
  151. case 'notnull':
  152. $notnull = $this->asBool($child);
  153. $options['notnull'] = $notnull;
  154. break;
  155. case 'autoincrement':
  156. $autoincrement = $this->asBool($child);
  157. $options['autoincrement'] = $autoincrement;
  158. break;
  159. case 'default':
  160. $default = (string)$child;
  161. $options['default'] = $default;
  162. break;
  163. case 'comments':
  164. $comment = (string)$child;
  165. $options['comment'] = $comment;
  166. break;
  167. case 'primary':
  168. $primary = $this->asBool($child);
  169. $options['primary'] = $primary;
  170. break;
  171. default:
  172. throw new \DomainException('Unknown element: ' . $child->getName());
  173. }
  174. }
  175. if (isset($name) && isset($type)) {
  176. if (empty($options['default'])) {
  177. if (empty($options['notnull']) || !$options['notnull']) {
  178. unset($options['default']);
  179. $options['notnull'] = false;
  180. } else {
  181. $options['default'] = '';
  182. }
  183. if ($type == 'integer') {
  184. $options['default'] = 0;
  185. } elseif ($type == 'boolean') {
  186. $options['default'] = false;
  187. }
  188. if (!empty($options['autoincrement']) && $options['autoincrement']) {
  189. unset($options['default']);
  190. }
  191. }
  192. if ($type === 'integer' && isset($options['default'])) {
  193. $options['default'] = (int)$options['default'];
  194. }
  195. if ($type === 'integer' && isset($options['length'])) {
  196. $length = $options['length'];
  197. if ($length < 4) {
  198. $type = 'smallint';
  199. } else if ($length > 4) {
  200. $type = 'bigint';
  201. }
  202. }
  203. if ($type === 'boolean' && isset($options['default'])) {
  204. $options['default'] = $this->asBool($options['default']);
  205. }
  206. if (!empty($options['autoincrement'])
  207. && !empty($options['notnull'])
  208. ) {
  209. $options['primary'] = true;
  210. }
  211. $table->addColumn($name, $type, $options);
  212. if (!empty($options['primary']) && $options['primary']) {
  213. $table->setPrimaryKey(array($name));
  214. }
  215. }
  216. }
  217. /**
  218. * @param \Doctrine\DBAL\Schema\Table $table
  219. * @param \SimpleXMLElement $xml
  220. * @throws \DomainException
  221. */
  222. private function loadIndex($table, $xml) {
  223. $name = null;
  224. $fields = array();
  225. foreach ($xml->children() as $child) {
  226. /**
  227. * @var \SimpleXMLElement $child
  228. */
  229. switch ($child->getName()) {
  230. case 'name':
  231. $name = (string)$child;
  232. break;
  233. case 'primary':
  234. $primary = $this->asBool($child);
  235. break;
  236. case 'unique':
  237. $unique = $this->asBool($child);
  238. break;
  239. case 'field':
  240. foreach ($child->children() as $field) {
  241. /**
  242. * @var \SimpleXMLElement $field
  243. */
  244. switch ($field->getName()) {
  245. case 'name':
  246. $field_name = (string)$field;
  247. $field_name = $this->platform->quoteIdentifier($field_name);
  248. $fields[] = $field_name;
  249. break;
  250. case 'sorting':
  251. break;
  252. default:
  253. throw new \DomainException('Unknown element: ' . $field->getName());
  254. }
  255. }
  256. break;
  257. default:
  258. throw new \DomainException('Unknown element: ' . $child->getName());
  259. }
  260. }
  261. if (!empty($fields)) {
  262. if (isset($primary) && $primary) {
  263. $table->setPrimaryKey($fields, $name);
  264. } else
  265. if (isset($unique) && $unique) {
  266. $table->addUniqueIndex($fields, $name);
  267. } else {
  268. $table->addIndex($fields, $name);
  269. }
  270. } else {
  271. throw new \DomainException('Empty index definition: ' . $name . ' options:' . print_r($fields, true));
  272. }
  273. }
  274. /**
  275. * @param \SimpleXMLElement | string $xml
  276. * @return bool
  277. */
  278. private function asBool($xml) {
  279. $result = (string)$xml;
  280. if ($result == 'true') {
  281. $result = true;
  282. } elseif ($result == 'false') {
  283. $result = false;
  284. }
  285. return (bool)$result;
  286. }
  287. }