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.6KB

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