mdb2schemareader.php 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  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. case 'numeric':
  142. $type = 'decimal';
  143. break;
  144. }
  145. break;
  146. case 'length':
  147. $length = (string)$child;
  148. $options['length'] = $length;
  149. break;
  150. case 'unsigned':
  151. $unsigned = $this->asBool($child);
  152. $options['unsigned'] = $unsigned;
  153. break;
  154. case 'notnull':
  155. $notnull = $this->asBool($child);
  156. $options['notnull'] = $notnull;
  157. break;
  158. case 'autoincrement':
  159. $autoincrement = $this->asBool($child);
  160. $options['autoincrement'] = $autoincrement;
  161. break;
  162. case 'default':
  163. $default = (string)$child;
  164. $options['default'] = $default;
  165. break;
  166. case 'comments':
  167. $comment = (string)$child;
  168. $options['comment'] = $comment;
  169. break;
  170. case 'primary':
  171. $primary = $this->asBool($child);
  172. $options['primary'] = $primary;
  173. break;
  174. default:
  175. throw new \DomainException('Unknown element: ' . $child->getName());
  176. }
  177. }
  178. if (isset($name) && isset($type)) {
  179. if (isset($options['default']) && empty($options['default'])) {
  180. if (empty($options['notnull']) || !$options['notnull']) {
  181. unset($options['default']);
  182. $options['notnull'] = false;
  183. } else {
  184. $options['default'] = '';
  185. }
  186. if ($type == 'integer' || $type == 'decimal') {
  187. $options['default'] = 0;
  188. } elseif ($type == 'boolean') {
  189. $options['default'] = false;
  190. }
  191. if (!empty($options['autoincrement']) && $options['autoincrement']) {
  192. unset($options['default']);
  193. }
  194. }
  195. if ($type === 'integer' && isset($options['default'])) {
  196. $options['default'] = (int)$options['default'];
  197. }
  198. if ($type === 'integer' && isset($options['length'])) {
  199. $length = $options['length'];
  200. if ($length < 4) {
  201. $type = 'smallint';
  202. } else if ($length > 4) {
  203. $type = 'bigint';
  204. }
  205. }
  206. if ($type === 'boolean' && isset($options['default'])) {
  207. $options['default'] = $this->asBool($options['default']);
  208. }
  209. if (!empty($options['autoincrement'])
  210. && !empty($options['notnull'])
  211. ) {
  212. $options['primary'] = true;
  213. }
  214. $table->addColumn($name, $type, $options);
  215. if (!empty($options['primary']) && $options['primary']) {
  216. $table->setPrimaryKey(array($name));
  217. }
  218. }
  219. }
  220. /**
  221. * @param \Doctrine\DBAL\Schema\Table $table
  222. * @param \SimpleXMLElement $xml
  223. * @throws \DomainException
  224. */
  225. private function loadIndex($table, $xml) {
  226. $name = null;
  227. $fields = array();
  228. foreach ($xml->children() as $child) {
  229. /**
  230. * @var \SimpleXMLElement $child
  231. */
  232. switch ($child->getName()) {
  233. case 'name':
  234. $name = (string)$child;
  235. break;
  236. case 'primary':
  237. $primary = $this->asBool($child);
  238. break;
  239. case 'unique':
  240. $unique = $this->asBool($child);
  241. break;
  242. case 'field':
  243. foreach ($child->children() as $field) {
  244. /**
  245. * @var \SimpleXMLElement $field
  246. */
  247. switch ($field->getName()) {
  248. case 'name':
  249. $field_name = (string)$field;
  250. $field_name = $this->platform->quoteIdentifier($field_name);
  251. $fields[] = $field_name;
  252. break;
  253. case 'sorting':
  254. break;
  255. default:
  256. throw new \DomainException('Unknown element: ' . $field->getName());
  257. }
  258. }
  259. break;
  260. default:
  261. throw new \DomainException('Unknown element: ' . $child->getName());
  262. }
  263. }
  264. if (!empty($fields)) {
  265. if (isset($primary) && $primary) {
  266. $table->setPrimaryKey($fields, $name);
  267. } else
  268. if (isset($unique) && $unique) {
  269. $table->addUniqueIndex($fields, $name);
  270. } else {
  271. $table->addIndex($fields, $name);
  272. }
  273. } else {
  274. throw new \DomainException('Empty index definition: ' . $name . ' options:' . print_r($fields, true));
  275. }
  276. }
  277. /**
  278. * @param \SimpleXMLElement | string $xml
  279. * @return bool
  280. */
  281. private function asBool($xml) {
  282. $result = (string)$xml;
  283. if ($result == 'true') {
  284. $result = true;
  285. } elseif ($result == 'false') {
  286. $result = false;
  287. }
  288. return (bool)$result;
  289. }
  290. }