mdb2schemareader.php 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. *
  5. * @author Bart Visscher <bartv@thisnet.nl>
  6. * @author Joas Schilling <coding@schilljs.com>
  7. * @author Jörn Friedrich Dreyer <jfd@butonic.de>
  8. * @author Morris Jobke <hey@morrisjobke.de>
  9. * @author Oliver Gasser <oliver.gasser@gmail.com>
  10. * @author Robin Appelman <robin@icewind.nl>
  11. * @author Robin McCorkell <robin@mccorkell.me.uk>
  12. * @author Thomas Müller <thomas.mueller@tmit.eu>
  13. * @author Victor Dubiniuk <dubiniuk@owncloud.com>
  14. * @author Vincent Petry <pvince81@owncloud.com>
  15. *
  16. * @license AGPL-3.0
  17. *
  18. * This code is free software: you can redistribute it and/or modify
  19. * it under the terms of the GNU Affero General Public License, version 3,
  20. * as published by the Free Software Foundation.
  21. *
  22. * This program is distributed in the hope that it will be useful,
  23. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  24. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  25. * GNU Affero General Public License for more details.
  26. *
  27. * You should have received a copy of the GNU Affero General Public License, version 3,
  28. * along with this program. If not, see <http://www.gnu.org/licenses/>
  29. *
  30. */
  31. namespace OC\DB;
  32. use Doctrine\DBAL\Platforms\AbstractPlatform;
  33. use Doctrine\DBAL\Schema\SchemaConfig;
  34. use OCP\IConfig;
  35. class MDB2SchemaReader {
  36. /**
  37. * @var string $DBNAME
  38. */
  39. protected $DBNAME;
  40. /**
  41. * @var string $DBTABLEPREFIX
  42. */
  43. protected $DBTABLEPREFIX;
  44. /**
  45. * @var \Doctrine\DBAL\Platforms\AbstractPlatform $platform
  46. */
  47. protected $platform;
  48. /** @var \Doctrine\DBAL\Schema\SchemaConfig $schemaConfig */
  49. protected $schemaConfig;
  50. /**
  51. * @param \OCP\IConfig $config
  52. * @param \Doctrine\DBAL\Platforms\AbstractPlatform $platform
  53. */
  54. public function __construct(IConfig $config, AbstractPlatform $platform) {
  55. $this->platform = $platform;
  56. $this->DBNAME = $config->getSystemValue('dbname', 'owncloud');
  57. $this->DBTABLEPREFIX = $config->getSystemValue('dbtableprefix', 'oc_');
  58. // Oracle does not support longer index names then 30 characters.
  59. // We use this limit for all DBs to make sure it does not cause a
  60. // problem.
  61. $this->schemaConfig = new SchemaConfig();
  62. $this->schemaConfig->setMaxIdentifierLength(30);
  63. }
  64. /**
  65. * @param string $file
  66. * @return \Doctrine\DBAL\Schema\Schema
  67. * @throws \DomainException
  68. */
  69. public function loadSchemaFromFile($file) {
  70. $schema = new \Doctrine\DBAL\Schema\Schema();
  71. $loadEntities = libxml_disable_entity_loader(false);
  72. $xml = simplexml_load_file($file);
  73. libxml_disable_entity_loader($loadEntities);
  74. foreach ($xml->children() as $child) {
  75. /**
  76. * @var \SimpleXMLElement $child
  77. */
  78. switch ($child->getName()) {
  79. case 'name':
  80. case 'create':
  81. case 'overwrite':
  82. case 'charset':
  83. break;
  84. case 'table':
  85. $this->loadTable($schema, $child);
  86. break;
  87. default:
  88. throw new \DomainException('Unknown element: ' . $child->getName());
  89. }
  90. }
  91. return $schema;
  92. }
  93. /**
  94. * @param \Doctrine\DBAL\Schema\Schema $schema
  95. * @param \SimpleXMLElement $xml
  96. * @throws \DomainException
  97. */
  98. private function loadTable($schema, $xml) {
  99. $table = null;
  100. foreach ($xml->children() as $child) {
  101. /**
  102. * @var \SimpleXMLElement $child
  103. */
  104. switch ($child->getName()) {
  105. case 'name':
  106. $name = (string)$child;
  107. $name = str_replace('*dbprefix*', $this->DBTABLEPREFIX, $name);
  108. $name = $this->platform->quoteIdentifier($name);
  109. $table = $schema->createTable($name);
  110. $table->addOption('collate', 'utf8_bin');
  111. $table->setSchemaConfig($this->schemaConfig);
  112. break;
  113. case 'create':
  114. case 'overwrite':
  115. case 'charset':
  116. break;
  117. case 'declaration':
  118. if (is_null($table)) {
  119. throw new \DomainException('Table declaration before table name');
  120. }
  121. $this->loadDeclaration($table, $child);
  122. break;
  123. default:
  124. throw new \DomainException('Unknown element: ' . $child->getName());
  125. }
  126. }
  127. }
  128. /**
  129. * @param \Doctrine\DBAL\Schema\Table $table
  130. * @param \SimpleXMLElement $xml
  131. * @throws \DomainException
  132. */
  133. private function loadDeclaration($table, $xml) {
  134. foreach ($xml->children() as $child) {
  135. /**
  136. * @var \SimpleXMLElement $child
  137. */
  138. switch ($child->getName()) {
  139. case 'field':
  140. $this->loadField($table, $child);
  141. break;
  142. case 'index':
  143. $this->loadIndex($table, $child);
  144. break;
  145. default:
  146. throw new \DomainException('Unknown element: ' . $child->getName());
  147. }
  148. }
  149. }
  150. /**
  151. * @param \Doctrine\DBAL\Schema\Table $table
  152. * @param \SimpleXMLElement $xml
  153. * @throws \DomainException
  154. */
  155. private function loadField($table, $xml) {
  156. $options = array( 'notnull' => false );
  157. foreach ($xml->children() as $child) {
  158. /**
  159. * @var \SimpleXMLElement $child
  160. */
  161. switch ($child->getName()) {
  162. case 'name':
  163. $name = (string)$child;
  164. $name = $this->platform->quoteIdentifier($name);
  165. break;
  166. case 'type':
  167. $type = (string)$child;
  168. switch ($type) {
  169. case 'text':
  170. $type = 'string';
  171. break;
  172. case 'clob':
  173. $type = 'text';
  174. break;
  175. case 'timestamp':
  176. $type = 'datetime';
  177. break;
  178. case 'numeric':
  179. $type = 'decimal';
  180. break;
  181. }
  182. break;
  183. case 'length':
  184. $length = (string)$child;
  185. $options['length'] = $length;
  186. break;
  187. case 'unsigned':
  188. $unsigned = $this->asBool($child);
  189. $options['unsigned'] = $unsigned;
  190. break;
  191. case 'notnull':
  192. $notnull = $this->asBool($child);
  193. $options['notnull'] = $notnull;
  194. break;
  195. case 'autoincrement':
  196. $autoincrement = $this->asBool($child);
  197. $options['autoincrement'] = $autoincrement;
  198. break;
  199. case 'default':
  200. $default = (string)$child;
  201. $options['default'] = $default;
  202. break;
  203. case 'comments':
  204. $comment = (string)$child;
  205. $options['comment'] = $comment;
  206. break;
  207. case 'primary':
  208. $primary = $this->asBool($child);
  209. $options['primary'] = $primary;
  210. break;
  211. case 'precision':
  212. $precision = (string)$child;
  213. $options['precision'] = $precision;
  214. break;
  215. case 'scale':
  216. $scale = (string)$child;
  217. $options['scale'] = $scale;
  218. break;
  219. default:
  220. throw new \DomainException('Unknown element: ' . $child->getName());
  221. }
  222. }
  223. if (isset($name) && isset($type)) {
  224. if (isset($options['default']) && empty($options['default'])) {
  225. if (empty($options['notnull']) || !$options['notnull']) {
  226. unset($options['default']);
  227. $options['notnull'] = false;
  228. } else {
  229. $options['default'] = '';
  230. }
  231. if ($type == 'integer' || $type == 'decimal') {
  232. $options['default'] = 0;
  233. } elseif ($type == 'boolean') {
  234. $options['default'] = false;
  235. }
  236. if (!empty($options['autoincrement']) && $options['autoincrement']) {
  237. unset($options['default']);
  238. }
  239. }
  240. if ($type === 'integer' && isset($options['default'])) {
  241. $options['default'] = (int)$options['default'];
  242. }
  243. if ($type === 'integer' && isset($options['length'])) {
  244. $length = $options['length'];
  245. if ($length < 4) {
  246. $type = 'smallint';
  247. } else if ($length > 4) {
  248. $type = 'bigint';
  249. }
  250. }
  251. if ($type === 'boolean' && isset($options['default'])) {
  252. $options['default'] = $this->asBool($options['default']);
  253. }
  254. if (!empty($options['autoincrement'])
  255. && !empty($options['notnull'])
  256. ) {
  257. $options['primary'] = true;
  258. }
  259. $table->addColumn($name, $type, $options);
  260. if (!empty($options['primary']) && $options['primary']) {
  261. $table->setPrimaryKey(array($name));
  262. }
  263. }
  264. }
  265. /**
  266. * @param \Doctrine\DBAL\Schema\Table $table
  267. * @param \SimpleXMLElement $xml
  268. * @throws \DomainException
  269. */
  270. private function loadIndex($table, $xml) {
  271. $name = null;
  272. $fields = array();
  273. foreach ($xml->children() as $child) {
  274. /**
  275. * @var \SimpleXMLElement $child
  276. */
  277. switch ($child->getName()) {
  278. case 'name':
  279. $name = (string)$child;
  280. break;
  281. case 'primary':
  282. $primary = $this->asBool($child);
  283. break;
  284. case 'unique':
  285. $unique = $this->asBool($child);
  286. break;
  287. case 'field':
  288. foreach ($child->children() as $field) {
  289. /**
  290. * @var \SimpleXMLElement $field
  291. */
  292. switch ($field->getName()) {
  293. case 'name':
  294. $field_name = (string)$field;
  295. $field_name = $this->platform->quoteIdentifier($field_name);
  296. $fields[] = $field_name;
  297. break;
  298. case 'sorting':
  299. break;
  300. default:
  301. throw new \DomainException('Unknown element: ' . $field->getName());
  302. }
  303. }
  304. break;
  305. default:
  306. throw new \DomainException('Unknown element: ' . $child->getName());
  307. }
  308. }
  309. if (!empty($fields)) {
  310. if (isset($primary) && $primary) {
  311. if ($table->hasPrimaryKey()) {
  312. return;
  313. }
  314. $table->setPrimaryKey($fields, $name);
  315. } else {
  316. if (isset($unique) && $unique) {
  317. $table->addUniqueIndex($fields, $name);
  318. } else {
  319. $table->addIndex($fields, $name);
  320. }
  321. }
  322. } else {
  323. throw new \DomainException('Empty index definition: ' . $name . ' options:' . print_r($fields, true));
  324. }
  325. }
  326. /**
  327. * @param \SimpleXMLElement|string $xml
  328. * @return bool
  329. */
  330. private function asBool($xml) {
  331. $result = (string)$xml;
  332. if ($result == 'true') {
  333. $result = true;
  334. } elseif ($result == 'false') {
  335. $result = false;
  336. }
  337. return (bool)$result;
  338. }
  339. }