1
0

MDB2SchemaReader.php 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. *
  5. * @author Bart Visscher <bartv@thisnet.nl>
  6. * @author Jörn Friedrich Dreyer <jfd@butonic.de>
  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 <pvince81@owncloud.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 = array( '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. } else if ($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. $table->addColumn($name, $type, $options);
  248. if (!empty($options['primary']) && $options['primary']) {
  249. $table->setPrimaryKey(array($name));
  250. }
  251. }
  252. }
  253. /**
  254. * @param \Doctrine\DBAL\Schema\Table $table
  255. * @param \SimpleXMLElement $xml
  256. * @throws \DomainException
  257. */
  258. private function loadIndex($table, $xml) {
  259. $name = null;
  260. $fields = array();
  261. foreach ($xml->children() as $child) {
  262. /**
  263. * @var \SimpleXMLElement $child
  264. */
  265. switch ($child->getName()) {
  266. case 'name':
  267. $name = (string)$child;
  268. break;
  269. case 'primary':
  270. $primary = $this->asBool($child);
  271. break;
  272. case 'unique':
  273. $unique = $this->asBool($child);
  274. break;
  275. case 'field':
  276. foreach ($child->children() as $field) {
  277. /**
  278. * @var \SimpleXMLElement $field
  279. */
  280. switch ($field->getName()) {
  281. case 'name':
  282. $field_name = (string)$field;
  283. $field_name = $this->platform->quoteIdentifier($field_name);
  284. $fields[] = $field_name;
  285. break;
  286. case 'sorting':
  287. break;
  288. default:
  289. throw new \DomainException('Unknown element: ' . $field->getName());
  290. }
  291. }
  292. break;
  293. default:
  294. throw new \DomainException('Unknown element: ' . $child->getName());
  295. }
  296. }
  297. if (!empty($fields)) {
  298. if (isset($primary) && $primary) {
  299. if ($table->hasPrimaryKey()) {
  300. return;
  301. }
  302. $table->setPrimaryKey($fields, $name);
  303. } else {
  304. if (isset($unique) && $unique) {
  305. $table->addUniqueIndex($fields, $name);
  306. } else {
  307. $table->addIndex($fields, $name);
  308. }
  309. }
  310. } else {
  311. throw new \DomainException('Empty index definition: ' . $name . ' options:' . print_r($fields, true));
  312. }
  313. }
  314. /**
  315. * @param \SimpleXMLElement|string $xml
  316. * @return bool
  317. */
  318. private function asBool($xml) {
  319. $result = (string)$xml;
  320. if ($result == 'true') {
  321. $result = true;
  322. } elseif ($result == 'false') {
  323. $result = false;
  324. }
  325. return (bool)$result;
  326. }
  327. }