server/lib/private/db/mdb2schemareader.php

320 lines
7.6 KiB
PHP
Raw Normal View History

2012-10-10 18:49:47 +00:00
<?php
/**
* Copyright (c) 2012 Bart Visscher <bartv@thisnet.nl>
* This file is licensed under the Affero General Public License version 3 or
* later.
* See the COPYING-README file.
*/
2013-07-29 14:33:00 +00:00
namespace OC\DB;
2012-10-10 18:49:47 +00:00
2013-07-29 14:33:00 +00:00
class MDB2SchemaReader {
/**
2013-07-29 14:33:00 +00:00
* @var string $DBNAME
*/
protected $DBNAME;
/**
* @var string $DBTABLEPREFIX
*/
protected $DBTABLEPREFIX;
/**
* @var \Doctrine\DBAL\Platforms\AbstractPlatform $platform
*/
protected $platform;
/**
* @param \OC\Config $config
* @param \Doctrine\DBAL\Platforms\AbstractPlatform $platform
*/
public function __construct($config, $platform) {
$this->platform = $platform;
$this->DBNAME = $config->getValue('dbname', 'owncloud');
$this->DBTABLEPREFIX = $config->getValue('dbtableprefix', 'oc_');
}
/**
* @param string $file
* @return \Doctrine\DBAL\Schema\Schema
2013-07-29 14:33:00 +00:00
* @throws \DomainException
*/
2013-07-29 14:33:00 +00:00
public function loadSchemaFromFile($file) {
2012-10-10 18:49:47 +00:00
$schema = new \Doctrine\DBAL\Schema\Schema();
$loadEntities = libxml_disable_entity_loader(false);
2012-10-10 18:49:47 +00:00
$xml = simplexml_load_file($file);
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;
}
/**
* @param\Doctrine\DBAL\Schema\Schema $schema
2013-07-29 14:33:00 +00:00
* @param \SimpleXMLElement $xml
* @throws \DomainException
*/
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
}
}
}
/**
* @param \Doctrine\DBAL\Schema\Table $table
2013-07-29 14:33:00 +00:00
* @param \SimpleXMLElement $xml
* @throws \DomainException
*/
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;
case 'primary':
2013-07-29 14:33:00 +00: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 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)) {
if (isset($options['default']) && empty($options['default'])) {
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-11-12 12:47:47 +00:00
if ($type == 'integer' || $type == 'decimal') {
$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
}
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'];
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']);
}
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;
}
$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) {
$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
/**
* @param \SimpleXMLElement | string $xml
* @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;
}
}