nextcloud/lib/private/DB/MDB2SchemaReader.php

349 lines
8.7 KiB
PHP
Raw Normal View History

2012-10-10 22:49:47 +04:00
<?php
/**
2016-07-21 18:07:57 +03:00
* @copyright Copyright (c) 2016, ownCloud, Inc.
*
2015-03-26 13:44:34 +03: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 19:13:36 +03:00
* @author Robin Appelman <robin@icewind.nl>
2016-01-12 17:02:16 +03:00
* @author Robin McCorkell <robin@mccorkell.me.uk>
2015-03-26 13:44:34 +03: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/>
2015-03-26 13:44:34 +03:00
*
2012-10-10 22:49:47 +04:00
*/
2013-07-29 18:33:00 +04:00
namespace OC\DB;
2012-10-10 22:49:47 +04:00
use Doctrine\DBAL\Platforms\AbstractPlatform;
use Doctrine\DBAL\Schema\Schema;
use OCP\IConfig;
2013-07-29 18:33:00 +04:00
class MDB2SchemaReader {
/**
* @var string $DBTABLEPREFIX
*/
protected $DBTABLEPREFIX;
/**
* @var \Doctrine\DBAL\Platforms\AbstractPlatform $platform
*/
protected $platform;
/** @var IConfig */
protected $config;
2013-07-29 18:33:00 +04:00
/**
* @param \OCP\IConfig $config
2013-07-29 18:33:00 +04:00
* @param \Doctrine\DBAL\Platforms\AbstractPlatform $platform
*/
public function __construct(IConfig $config, AbstractPlatform $platform) {
2013-07-29 18:33:00 +04:00
$this->platform = $platform;
$this->config = $config;
$this->DBTABLEPREFIX = $config->getSystemValue('dbtableprefix', 'oc_');
2013-07-29 18:33:00 +04:00
}
/**
* @param string $file
* @param Schema $schema
* @return Schema
2013-07-29 18:33:00 +04:00
* @throws \DomainException
*/
public function loadSchemaFromFile($file, Schema $schema) {
$loadEntities = libxml_disable_entity_loader(false);
2012-10-10 22:49:47 +04:00
$xml = simplexml_load_file($file);
libxml_disable_entity_loader($loadEntities);
2013-07-29 18:33:00 +04:00
foreach ($xml->children() as $child) {
/**
* @var \SimpleXMLElement $child
*/
switch ($child->getName()) {
2012-10-10 22:49:47 +04:00
case 'name':
case 'create':
case 'overwrite':
case 'charset':
break;
case 'table':
2013-07-29 18:33:00 +04:00
$this->loadTable($schema, $child);
2012-10-10 22:49:47 +04:00
break;
default:
2013-07-29 18:33:00 +04:00
throw new \DomainException('Unknown element: ' . $child->getName());
2012-10-10 22:49:47 +04:00
}
}
return $schema;
}
/**
* @param \Doctrine\DBAL\Schema\Schema $schema
2013-07-29 18:33:00 +04:00
* @param \SimpleXMLElement $xml
* @throws \DomainException
*/
2013-07-29 18:33:00 +04:00
private function loadTable($schema, $xml) {
$table = null;
foreach ($xml->children() as $child) {
/**
* @var \SimpleXMLElement $child
*/
switch ($child->getName()) {
2012-10-10 22:49:47 +04:00
case 'name':
$name = (string)$child;
2013-07-29 18:33:00 +04:00
$name = str_replace('*dbprefix*', $this->DBTABLEPREFIX, $name);
$name = $this->platform->quoteIdentifier($name);
2012-10-10 22:49:47 +04:00
$table = $schema->createTable($name);
break;
case 'create':
case 'overwrite':
case 'charset':
break;
case 'declaration':
2013-07-29 18:33:00 +04:00
if (is_null($table)) {
throw new \DomainException('Table declaration before table name');
}
$this->loadDeclaration($table, $child);
2012-10-10 22:49:47 +04:00
break;
default:
2013-07-29 18:33:00 +04:00
throw new \DomainException('Unknown element: ' . $child->getName());
2012-10-10 22:49:47 +04:00
}
}
}
/**
* @param \Doctrine\DBAL\Schema\Table $table
2013-07-29 18:33:00 +04:00
* @param \SimpleXMLElement $xml
* @throws \DomainException
*/
2013-07-29 18:33:00 +04:00
private function loadDeclaration($table, $xml) {
foreach ($xml->children() as $child) {
/**
* @var \SimpleXMLElement $child
*/
switch ($child->getName()) {
2012-10-10 22:49:47 +04:00
case 'field':
2013-07-29 18:33:00 +04:00
$this->loadField($table, $child);
2012-10-10 22:49:47 +04:00
break;
case 'index':
2013-07-29 18:33:00 +04:00
$this->loadIndex($table, $child);
2012-10-10 22:49:47 +04:00
break;
default:
2013-07-29 18:33:00 +04:00
throw new \DomainException('Unknown element: ' . $child->getName());
2012-10-10 22:49:47 +04:00
}
}
}
2013-07-29 18:33:00 +04:00
/**
* @param \Doctrine\DBAL\Schema\Table $table
* @param \SimpleXMLElement $xml
* @throws \DomainException
*/
private function loadField($table, $xml) {
2014-05-02 01:03:45 +04:00
$options = array( 'notnull' => false );
2013-07-29 18:33:00 +04:00
foreach ($xml->children() as $child) {
/**
* @var \SimpleXMLElement $child
*/
switch ($child->getName()) {
2012-10-10 22:49:47 +04:00
case 'name':
$name = (string)$child;
2013-07-29 18:33:00 +04:00
$name = $this->platform->quoteIdentifier($name);
2012-10-10 22:49:47 +04:00
break;
case 'type':
$type = (string)$child;
2013-07-29 18:33:00 +04:00
switch ($type) {
2012-10-10 22:49:47 +04:00
case 'text':
$type = 'string';
break;
case 'clob':
$type = 'text';
break;
case 'timestamp':
$type = 'datetime';
break;
2013-11-12 16:55:06 +04:00
case 'numeric':
$type = 'decimal';
break;
2012-10-10 22:49:47 +04:00
}
break;
case 'length':
$length = (string)$child;
$options['length'] = $length;
break;
case 'unsigned':
2013-07-29 18:33:00 +04:00
$unsigned = $this->asBool($child);
2012-10-10 22:49:47 +04:00
$options['unsigned'] = $unsigned;
break;
case 'notnull':
2013-07-29 18:33:00 +04:00
$notnull = $this->asBool($child);
2012-10-10 22:49:47 +04:00
$options['notnull'] = $notnull;
break;
case 'autoincrement':
2013-07-29 18:33:00 +04:00
$autoincrement = $this->asBool($child);
2012-10-10 22:49:47 +04:00
$options['autoincrement'] = $autoincrement;
break;
case 'default':
$default = (string)$child;
$options['default'] = $default;
break;
2013-07-05 23:42:37 +04:00
case 'comments':
$comment = (string)$child;
$options['comment'] = $comment;
break;
case 'primary':
2013-07-29 18:33:00 +04:00
$primary = $this->asBool($child);
$options['primary'] = $primary;
break;
case 'precision':
$precision = (string)$child;
$options['precision'] = $precision;
break;
case 'scale':
$scale = (string)$child;
$options['scale'] = $scale;
break;
2012-10-10 22:49:47 +04:00
default:
2013-07-29 18:33:00 +04:00
throw new \DomainException('Unknown element: ' . $child->getName());
2012-10-10 22:49:47 +04:00
}
}
if (isset($name) && isset($type)) {
if (isset($options['default']) && empty($options['default'])) {
if (empty($options['notnull']) || !$options['notnull']) {
unset($options['default']);
2013-06-25 00:37:07 +04:00
$options['notnull'] = false;
2013-07-18 22:28:57 +04:00
} else {
$options['default'] = '';
}
2013-11-12 16:47:47 +04:00
if ($type == 'integer' || $type == 'decimal') {
$options['default'] = 0;
2013-07-29 18:33:00 +04:00
} elseif ($type == 'boolean') {
$options['default'] = false;
2012-10-10 22:49:47 +04:00
}
if (!empty($options['autoincrement']) && $options['autoincrement']) {
2012-10-10 22:49:47 +04:00
unset($options['default']);
}
}
2013-07-29 18:33:00 +04:00
if ($type === 'integer' && isset($options['default'])) {
$options['default'] = (int)$options['default'];
}
if ($type === 'integer' && isset($options['length'])) {
2012-10-10 22:49:47 +04:00
$length = $options['length'];
if ($length < 4) {
2012-10-10 22:49:47 +04:00
$type = 'smallint';
2013-07-29 18:33:00 +04:00
} else if ($length > 4) {
2012-10-10 22:49:47 +04:00
$type = 'bigint';
}
}
2013-07-29 18:33:00 +04:00
if ($type === 'boolean' && isset($options['default'])) {
$options['default'] = $this->asBool($options['default']);
}
2012-10-10 22:49:47 +04:00
if (!empty($options['autoincrement'])
2013-07-29 18:33:00 +04:00
&& !empty($options['notnull'])
) {
2013-07-18 22:28:57 +04:00
$options['primary'] = true;
}
2013-07-18 22:28:57 +04:00
$table->addColumn($name, $type, $options);
if (!empty($options['primary']) && $options['primary']) {
2012-10-10 22:49:47 +04:00
$table->setPrimaryKey(array($name));
}
}
}
2013-07-29 18:33:00 +04:00
/**
* @param \Doctrine\DBAL\Schema\Table $table
* @param \SimpleXMLElement $xml
* @throws \DomainException
*/
private function loadIndex($table, $xml) {
2012-10-10 22:49:47 +04:00
$name = null;
$fields = array();
2013-07-29 18:33:00 +04:00
foreach ($xml->children() as $child) {
/**
* @var \SimpleXMLElement $child
*/
switch ($child->getName()) {
2012-10-10 22:49:47 +04:00
case 'name':
$name = (string)$child;
break;
case 'primary':
2013-07-29 18:33:00 +04:00
$primary = $this->asBool($child);
2012-10-10 22:49:47 +04:00
break;
case 'unique':
2013-07-29 18:33:00 +04:00
$unique = $this->asBool($child);
2012-10-10 22:49:47 +04:00
break;
case 'field':
2013-07-29 18:33:00 +04:00
foreach ($child->children() as $field) {
/**
* @var \SimpleXMLElement $field
*/
switch ($field->getName()) {
2012-10-10 22:49:47 +04:00
case 'name':
$field_name = (string)$field;
2013-07-29 18:33:00 +04:00
$field_name = $this->platform->quoteIdentifier($field_name);
2012-10-10 22:49:47 +04:00
$fields[] = $field_name;
break;
case 'sorting':
break;
default:
2013-07-29 18:33:00 +04:00
throw new \DomainException('Unknown element: ' . $field->getName());
2012-10-10 22:49:47 +04:00
}
}
break;
default:
2013-07-29 18:33:00 +04:00
throw new \DomainException('Unknown element: ' . $child->getName());
2012-10-10 22:49:47 +04:00
}
}
if (!empty($fields)) {
if (isset($primary) && $primary) {
if ($table->hasPrimaryKey()) {
return;
}
2012-10-10 22:49:47 +04:00
$table->setPrimaryKey($fields, $name);
2013-09-13 19:45:27 +04:00
} else {
2013-07-29 18:33:00 +04:00
if (isset($unique) && $unique) {
$table->addUniqueIndex($fields, $name);
} else {
$table->addIndex($fields, $name);
}
2013-09-13 19:45:27 +04:00
}
2012-10-10 22:49:47 +04:00
} else {
2013-07-29 18:33:00 +04:00
throw new \DomainException('Empty index definition: ' . $name . ' options:' . print_r($fields, true));
2012-10-10 22:49:47 +04:00
}
}
2013-07-29 18:33:00 +04:00
/**
2014-05-11 21:28:45 +04:00
* @param \SimpleXMLElement|string $xml
2013-07-29 18:33:00 +04:00
* @return bool
*/
private function asBool($xml) {
2012-10-10 22:49:47 +04:00
$result = (string)$xml;
if ($result == 'true') {
$result = true;
2013-07-29 18:33:00 +04:00
} elseif ($result == 'false') {
2012-10-10 22:49:47 +04:00
$result = false;
}
return (bool)$result;
}
}