2012-10-10 18:49:47 +00:00
|
|
|
<?php
|
|
|
|
/**
|
2016-07-21 15:07:57 +00:00
|
|
|
* @copyright Copyright (c) 2016, ownCloud, Inc.
|
|
|
|
*
|
2015-03-26 10:44:34 +00:00
|
|
|
* @author Bart Visscher <bartv@thisnet.nl>
|
|
|
|
* @author Jörn Friedrich Dreyer <jfd@butonic.de>
|
|
|
|
* @author Morris Jobke <hey@morrisjobke.de>
|
|
|
|
* @author Oliver Gasser <oliver.gasser@gmail.com>
|
2016-07-21 16:13:36 +00:00
|
|
|
* @author Robin Appelman <robin@icewind.nl>
|
2016-01-12 14:02:16 +00:00
|
|
|
* @author Robin McCorkell <robin@mccorkell.me.uk>
|
2015-03-26 10:44:34 +00:00
|
|
|
* @author Thomas Müller <thomas.mueller@tmit.eu>
|
|
|
|
* @author Victor Dubiniuk <dubiniuk@owncloud.com>
|
|
|
|
* @author Vincent Petry <pvince81@owncloud.com>
|
|
|
|
*
|
|
|
|
* @license AGPL-3.0
|
|
|
|
*
|
|
|
|
* This code is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU Affero General Public License, version 3,
|
|
|
|
* as published by the Free Software Foundation.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU Affero General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Affero General Public License, version 3,
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>
|
|
|
|
*
|
2012-10-10 18:49:47 +00:00
|
|
|
*/
|
2015-02-26 10:37:37 +00:00
|
|
|
|
2013-07-29 14:33:00 +00:00
|
|
|
namespace OC\DB;
|
2012-10-10 18:49:47 +00:00
|
|
|
|
2014-11-18 23:25:26 +00:00
|
|
|
use Doctrine\DBAL\Platforms\AbstractPlatform;
|
2017-03-19 20:52:54 +00:00
|
|
|
use Doctrine\DBAL\Schema\Schema;
|
2014-11-18 23:25:26 +00:00
|
|
|
use OCP\IConfig;
|
|
|
|
|
2013-07-29 14:33:00 +00:00
|
|
|
class MDB2SchemaReader {
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var string $DBTABLEPREFIX
|
|
|
|
*/
|
|
|
|
protected $DBTABLEPREFIX;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var \Doctrine\DBAL\Platforms\AbstractPlatform $platform
|
|
|
|
*/
|
|
|
|
protected $platform;
|
|
|
|
|
2015-07-30 11:57:04 +00:00
|
|
|
/** @var IConfig */
|
|
|
|
protected $config;
|
|
|
|
|
2013-07-29 14:33:00 +00:00
|
|
|
/**
|
2014-11-18 23:25:26 +00:00
|
|
|
* @param \OCP\IConfig $config
|
2013-07-29 14:33:00 +00:00
|
|
|
* @param \Doctrine\DBAL\Platforms\AbstractPlatform $platform
|
|
|
|
*/
|
2014-11-18 23:25:26 +00:00
|
|
|
public function __construct(IConfig $config, AbstractPlatform $platform) {
|
2013-07-29 14:33:00 +00:00
|
|
|
$this->platform = $platform;
|
2015-07-30 11:57:04 +00:00
|
|
|
$this->config = $config;
|
2014-11-18 23:25:26 +00:00
|
|
|
$this->DBTABLEPREFIX = $config->getSystemValue('dbtableprefix', 'oc_');
|
2013-07-29 14:33:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param string $file
|
2015-07-30 08:57:16 +00:00
|
|
|
* @param Schema $schema
|
2017-03-19 20:52:54 +00:00
|
|
|
* @return Schema
|
2013-07-29 14:33:00 +00:00
|
|
|
* @throws \DomainException
|
2013-03-19 17:52:54 +00:00
|
|
|
*/
|
2015-07-30 08:57:16 +00:00
|
|
|
public function loadSchemaFromFile($file, Schema $schema) {
|
2014-03-03 10:31:46 +00:00
|
|
|
$loadEntities = libxml_disable_entity_loader(false);
|
2012-10-10 18:49:47 +00:00
|
|
|
$xml = simplexml_load_file($file);
|
2014-03-03 10:31:46 +00:00
|
|
|
libxml_disable_entity_loader($loadEntities);
|
2013-07-29 14:33:00 +00:00
|
|
|
foreach ($xml->children() as $child) {
|
|
|
|
/**
|
|
|
|
* @var \SimpleXMLElement $child
|
|
|
|
*/
|
|
|
|
switch ($child->getName()) {
|
2012-10-10 18:49:47 +00:00
|
|
|
case 'name':
|
|
|
|
case 'create':
|
|
|
|
case 'overwrite':
|
|
|
|
case 'charset':
|
|
|
|
break;
|
|
|
|
case 'table':
|
2013-07-29 14:33:00 +00:00
|
|
|
$this->loadTable($schema, $child);
|
2012-10-10 18:49:47 +00:00
|
|
|
break;
|
|
|
|
default:
|
2013-07-29 14:33:00 +00:00
|
|
|
throw new \DomainException('Unknown element: ' . $child->getName());
|
2012-10-10 18:49:47 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return $schema;
|
|
|
|
}
|
|
|
|
|
2013-03-19 17:52:54 +00:00
|
|
|
/**
|
2014-05-11 20:51:30 +00:00
|
|
|
* @param \Doctrine\DBAL\Schema\Schema $schema
|
2013-07-29 14:33:00 +00:00
|
|
|
* @param \SimpleXMLElement $xml
|
|
|
|
* @throws \DomainException
|
2013-03-19 17:52:54 +00:00
|
|
|
*/
|
2013-07-29 14:33:00 +00:00
|
|
|
private function loadTable($schema, $xml) {
|
|
|
|
$table = null;
|
|
|
|
foreach ($xml->children() as $child) {
|
|
|
|
/**
|
|
|
|
* @var \SimpleXMLElement $child
|
|
|
|
*/
|
|
|
|
switch ($child->getName()) {
|
2012-10-10 18:49:47 +00:00
|
|
|
case 'name':
|
|
|
|
$name = (string)$child;
|
2013-07-29 14:33:00 +00:00
|
|
|
$name = str_replace('*dbprefix*', $this->DBTABLEPREFIX, $name);
|
|
|
|
$name = $this->platform->quoteIdentifier($name);
|
2012-10-10 18:49:47 +00:00
|
|
|
$table = $schema->createTable($name);
|
|
|
|
break;
|
|
|
|
case 'create':
|
|
|
|
case 'overwrite':
|
|
|
|
case 'charset':
|
|
|
|
break;
|
|
|
|
case 'declaration':
|
2013-07-29 14:33:00 +00:00
|
|
|
if (is_null($table)) {
|
|
|
|
throw new \DomainException('Table declaration before table name');
|
|
|
|
}
|
|
|
|
$this->loadDeclaration($table, $child);
|
2012-10-10 18:49:47 +00:00
|
|
|
break;
|
|
|
|
default:
|
2013-07-29 14:33:00 +00:00
|
|
|
throw new \DomainException('Unknown element: ' . $child->getName());
|
2012-10-10 18:49:47 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-03-19 17:52:54 +00:00
|
|
|
/**
|
|
|
|
* @param \Doctrine\DBAL\Schema\Table $table
|
2013-07-29 14:33:00 +00:00
|
|
|
* @param \SimpleXMLElement $xml
|
|
|
|
* @throws \DomainException
|
2013-03-19 17:52:54 +00:00
|
|
|
*/
|
2013-07-29 14:33:00 +00:00
|
|
|
private function loadDeclaration($table, $xml) {
|
|
|
|
foreach ($xml->children() as $child) {
|
|
|
|
/**
|
|
|
|
* @var \SimpleXMLElement $child
|
|
|
|
*/
|
|
|
|
switch ($child->getName()) {
|
2012-10-10 18:49:47 +00:00
|
|
|
case 'field':
|
2013-07-29 14:33:00 +00:00
|
|
|
$this->loadField($table, $child);
|
2012-10-10 18:49:47 +00:00
|
|
|
break;
|
|
|
|
case 'index':
|
2013-07-29 14:33:00 +00:00
|
|
|
$this->loadIndex($table, $child);
|
2012-10-10 18:49:47 +00:00
|
|
|
break;
|
|
|
|
default:
|
2013-07-29 14:33:00 +00:00
|
|
|
throw new \DomainException('Unknown element: ' . $child->getName());
|
2012-10-10 18:49:47 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-07-29 14:33:00 +00:00
|
|
|
/**
|
|
|
|
* @param \Doctrine\DBAL\Schema\Table $table
|
|
|
|
* @param \SimpleXMLElement $xml
|
|
|
|
* @throws \DomainException
|
|
|
|
*/
|
|
|
|
private function loadField($table, $xml) {
|
2014-05-01 21:03:45 +00:00
|
|
|
$options = array( 'notnull' => false );
|
2013-07-29 14:33:00 +00:00
|
|
|
foreach ($xml->children() as $child) {
|
|
|
|
/**
|
|
|
|
* @var \SimpleXMLElement $child
|
|
|
|
*/
|
|
|
|
switch ($child->getName()) {
|
2012-10-10 18:49:47 +00:00
|
|
|
case 'name':
|
|
|
|
$name = (string)$child;
|
2013-07-29 14:33:00 +00:00
|
|
|
$name = $this->platform->quoteIdentifier($name);
|
2012-10-10 18:49:47 +00:00
|
|
|
break;
|
|
|
|
case 'type':
|
|
|
|
$type = (string)$child;
|
2013-07-29 14:33:00 +00:00
|
|
|
switch ($type) {
|
2012-10-10 18:49:47 +00:00
|
|
|
case 'text':
|
|
|
|
$type = 'string';
|
|
|
|
break;
|
|
|
|
case 'clob':
|
|
|
|
$type = 'text';
|
|
|
|
break;
|
|
|
|
case 'timestamp':
|
|
|
|
$type = 'datetime';
|
|
|
|
break;
|
2013-11-12 12:55:06 +00:00
|
|
|
case 'numeric':
|
|
|
|
$type = 'decimal';
|
|
|
|
break;
|
2012-10-10 18:49:47 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 'length':
|
|
|
|
$length = (string)$child;
|
|
|
|
$options['length'] = $length;
|
|
|
|
break;
|
|
|
|
case 'unsigned':
|
2013-07-29 14:33:00 +00:00
|
|
|
$unsigned = $this->asBool($child);
|
2012-10-10 18:49:47 +00:00
|
|
|
$options['unsigned'] = $unsigned;
|
|
|
|
break;
|
|
|
|
case 'notnull':
|
2013-07-29 14:33:00 +00:00
|
|
|
$notnull = $this->asBool($child);
|
2012-10-10 18:49:47 +00:00
|
|
|
$options['notnull'] = $notnull;
|
|
|
|
break;
|
|
|
|
case 'autoincrement':
|
2013-07-29 14:33:00 +00:00
|
|
|
$autoincrement = $this->asBool($child);
|
2012-10-10 18:49:47 +00:00
|
|
|
$options['autoincrement'] = $autoincrement;
|
|
|
|
break;
|
|
|
|
case 'default':
|
|
|
|
$default = (string)$child;
|
|
|
|
$options['default'] = $default;
|
|
|
|
break;
|
2013-07-05 19:42:37 +00:00
|
|
|
case 'comments':
|
|
|
|
$comment = (string)$child;
|
|
|
|
$options['comment'] = $comment;
|
|
|
|
break;
|
2013-07-22 14:25:07 +00:00
|
|
|
case 'primary':
|
2013-07-29 14:33:00 +00:00
|
|
|
$primary = $this->asBool($child);
|
2013-07-22 14:25:07 +00:00
|
|
|
$options['primary'] = $primary;
|
|
|
|
break;
|
2013-12-17 21:46:45 +00:00
|
|
|
case 'precision':
|
|
|
|
$precision = (string)$child;
|
|
|
|
$options['precision'] = $precision;
|
|
|
|
break;
|
|
|
|
case 'scale':
|
|
|
|
$scale = (string)$child;
|
|
|
|
$options['scale'] = $scale;
|
|
|
|
break;
|
2012-10-10 18:49:47 +00:00
|
|
|
default:
|
2013-07-29 14:33:00 +00:00
|
|
|
throw new \DomainException('Unknown element: ' . $child->getName());
|
2012-10-10 18:49:47 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (isset($name) && isset($type)) {
|
2013-11-11 16:58:21 +00:00
|
|
|
if (isset($options['default']) && empty($options['default'])) {
|
2013-03-17 14:25:41 +00:00
|
|
|
if (empty($options['notnull']) || !$options['notnull']) {
|
|
|
|
unset($options['default']);
|
2013-06-24 20:37:07 +00:00
|
|
|
$options['notnull'] = false;
|
2013-07-18 18:28:57 +00:00
|
|
|
} else {
|
|
|
|
$options['default'] = '';
|
2013-03-17 14:25:41 +00:00
|
|
|
}
|
2013-11-12 12:47:47 +00:00
|
|
|
if ($type == 'integer' || $type == 'decimal') {
|
2013-03-17 14:25:41 +00:00
|
|
|
$options['default'] = 0;
|
2013-07-29 14:33:00 +00:00
|
|
|
} elseif ($type == 'boolean') {
|
|
|
|
$options['default'] = false;
|
2012-10-10 18:49:47 +00:00
|
|
|
}
|
2013-03-17 14:25:41 +00:00
|
|
|
if (!empty($options['autoincrement']) && $options['autoincrement']) {
|
2012-10-10 18:49:47 +00:00
|
|
|
unset($options['default']);
|
|
|
|
}
|
|
|
|
}
|
2013-07-29 14:33:00 +00:00
|
|
|
if ($type === 'integer' && isset($options['default'])) {
|
|
|
|
$options['default'] = (int)$options['default'];
|
|
|
|
}
|
|
|
|
if ($type === 'integer' && isset($options['length'])) {
|
2012-10-10 18:49:47 +00:00
|
|
|
$length = $options['length'];
|
2013-03-17 16:34:35 +00:00
|
|
|
if ($length < 4) {
|
2012-10-10 18:49:47 +00:00
|
|
|
$type = 'smallint';
|
2013-07-29 14:33:00 +00:00
|
|
|
} else if ($length > 4) {
|
2012-10-10 18:49:47 +00:00
|
|
|
$type = 'bigint';
|
|
|
|
}
|
|
|
|
}
|
2013-07-29 14:33:00 +00:00
|
|
|
if ($type === 'boolean' && isset($options['default'])) {
|
|
|
|
$options['default'] = $this->asBool($options['default']);
|
2013-07-22 14:24:46 +00:00
|
|
|
}
|
2012-10-10 18:49:47 +00:00
|
|
|
if (!empty($options['autoincrement'])
|
2013-07-29 14:33:00 +00:00
|
|
|
&& !empty($options['notnull'])
|
|
|
|
) {
|
2013-07-18 18:28:57 +00:00
|
|
|
$options['primary'] = true;
|
|
|
|
}
|
2017-03-19 20:52:54 +00:00
|
|
|
|
2013-07-18 18:28:57 +00:00
|
|
|
$table->addColumn($name, $type, $options);
|
|
|
|
if (!empty($options['primary']) && $options['primary']) {
|
2012-10-10 18:49:47 +00:00
|
|
|
$table->setPrimaryKey(array($name));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-07-29 14:33:00 +00:00
|
|
|
/**
|
|
|
|
* @param \Doctrine\DBAL\Schema\Table $table
|
|
|
|
* @param \SimpleXMLElement $xml
|
|
|
|
* @throws \DomainException
|
|
|
|
*/
|
|
|
|
private function loadIndex($table, $xml) {
|
2012-10-10 18:49:47 +00:00
|
|
|
$name = null;
|
|
|
|
$fields = array();
|
2013-07-29 14:33:00 +00:00
|
|
|
foreach ($xml->children() as $child) {
|
|
|
|
/**
|
|
|
|
* @var \SimpleXMLElement $child
|
|
|
|
*/
|
|
|
|
switch ($child->getName()) {
|
2012-10-10 18:49:47 +00:00
|
|
|
case 'name':
|
|
|
|
$name = (string)$child;
|
|
|
|
break;
|
|
|
|
case 'primary':
|
2013-07-29 14:33:00 +00:00
|
|
|
$primary = $this->asBool($child);
|
2012-10-10 18:49:47 +00:00
|
|
|
break;
|
|
|
|
case 'unique':
|
2013-07-29 14:33:00 +00:00
|
|
|
$unique = $this->asBool($child);
|
2012-10-10 18:49:47 +00:00
|
|
|
break;
|
|
|
|
case 'field':
|
2013-07-29 14:33:00 +00:00
|
|
|
foreach ($child->children() as $field) {
|
|
|
|
/**
|
|
|
|
* @var \SimpleXMLElement $field
|
|
|
|
*/
|
|
|
|
switch ($field->getName()) {
|
2012-10-10 18:49:47 +00:00
|
|
|
case 'name':
|
|
|
|
$field_name = (string)$field;
|
2013-07-29 14:33:00 +00:00
|
|
|
$field_name = $this->platform->quoteIdentifier($field_name);
|
2012-10-10 18:49:47 +00:00
|
|
|
$fields[] = $field_name;
|
|
|
|
break;
|
|
|
|
case 'sorting':
|
|
|
|
break;
|
|
|
|
default:
|
2013-07-29 14:33:00 +00:00
|
|
|
throw new \DomainException('Unknown element: ' . $field->getName());
|
2012-10-10 18:49:47 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
2013-07-29 14:33:00 +00:00
|
|
|
throw new \DomainException('Unknown element: ' . $child->getName());
|
2012-10-10 18:49:47 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!empty($fields)) {
|
|
|
|
if (isset($primary) && $primary) {
|
2015-02-11 16:35:46 +00:00
|
|
|
if ($table->hasPrimaryKey()) {
|
|
|
|
return;
|
|
|
|
}
|
2012-10-10 18:49:47 +00:00
|
|
|
$table->setPrimaryKey($fields, $name);
|
2013-09-13 15:45:27 +00:00
|
|
|
} else {
|
2013-07-29 14:33:00 +00:00
|
|
|
if (isset($unique) && $unique) {
|
|
|
|
$table->addUniqueIndex($fields, $name);
|
|
|
|
} else {
|
|
|
|
$table->addIndex($fields, $name);
|
|
|
|
}
|
2013-09-13 15:45:27 +00:00
|
|
|
}
|
2012-10-10 18:49:47 +00:00
|
|
|
} else {
|
2013-07-29 14:33:00 +00:00
|
|
|
throw new \DomainException('Empty index definition: ' . $name . ' options:' . print_r($fields, true));
|
2012-10-10 18:49:47 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-07-29 14:33:00 +00:00
|
|
|
/**
|
2014-05-11 17:28:45 +00:00
|
|
|
* @param \SimpleXMLElement|string $xml
|
2013-07-29 14:33:00 +00:00
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
private function asBool($xml) {
|
2012-10-10 18:49:47 +00:00
|
|
|
$result = (string)$xml;
|
|
|
|
if ($result == 'true') {
|
|
|
|
$result = true;
|
2013-07-29 14:33:00 +00:00
|
|
|
} elseif ($result == 'false') {
|
2012-10-10 18:49:47 +00:00
|
|
|
$result = false;
|
|
|
|
}
|
|
|
|
return (bool)$result;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|