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.

MDB2SchemaReader.php 9.0KB

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