1
0

MDB2SchemaReader.php 8.7 KB

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