2012-10-10 22:49:47 +04: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 18:33:00 +04:00
|
|
|
namespace OC\DB;
|
2012-10-10 22:49:47 +04:00
|
|
|
|
2013-07-29 18:33:00 +04:00
|
|
|
class MDB2SchemaReader {
|
2013-03-19 21:52:54 +04:00
|
|
|
/**
|
2013-07-29 18:33:00 +04: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
|
2013-03-19 21:52:54 +04:00
|
|
|
* @return \Doctrine\DBAL\Schema\Schema
|
2013-07-29 18:33:00 +04:00
|
|
|
* @throws \DomainException
|
2013-03-19 21:52:54 +04:00
|
|
|
*/
|
2013-07-29 18:33:00 +04:00
|
|
|
public function loadSchemaFromFile($file) {
|
2012-10-10 22:49:47 +04:00
|
|
|
$schema = new \Doctrine\DBAL\Schema\Schema();
|
|
|
|
$xml = simplexml_load_file($file);
|
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;
|
|
|
|
}
|
|
|
|
|
2013-03-19 21:52:54 +04:00
|
|
|
/**
|
|
|
|
* @param\Doctrine\DBAL\Schema\Schema $schema
|
2013-07-29 18:33:00 +04:00
|
|
|
* @param \SimpleXMLElement $xml
|
|
|
|
* @throws \DomainException
|
2013-03-19 21:52:54 +04:00
|
|
|
*/
|
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
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-03-19 21:52:54 +04:00
|
|
|
/**
|
|
|
|
* @param \Doctrine\DBAL\Schema\Table $table
|
2013-07-29 18:33:00 +04:00
|
|
|
* @param \SimpleXMLElement $xml
|
|
|
|
* @throws \DomainException
|
2013-03-19 21:52:54 +04:00
|
|
|
*/
|
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) {
|
2012-10-10 22:49:47 +04:00
|
|
|
$options = 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;
|
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;
|
2013-07-22 18:25:07 +04:00
|
|
|
case 'primary':
|
2013-07-29 18:33:00 +04:00
|
|
|
$primary = $this->asBool($child);
|
2013-07-22 18:25:07 +04:00
|
|
|
$options['primary'] = $primary;
|
|
|
|
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)) {
|
2013-11-11 20:58:21 +04:00
|
|
|
if (isset($options['default']) && empty($options['default'])) {
|
2013-03-17 18:25:41 +04:00
|
|
|
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-03-17 18:25:41 +04:00
|
|
|
}
|
2013-11-12 16:47:47 +04:00
|
|
|
if ($type == 'integer' || $type == 'decimal') {
|
2013-03-17 18:25:41 +04:00
|
|
|
$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
|
|
|
}
|
2013-03-17 18:25:41 +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'];
|
2013-03-17 20:34:35 +04:00
|
|
|
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']);
|
2013-07-22 18:24:46 +04:00
|
|
|
}
|
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;
|
|
|
|
}
|
|
|
|
$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) {
|
|
|
|
$table->setPrimaryKey($fields, $name);
|
|
|
|
} else
|
2013-07-29 18:33:00 +04:00
|
|
|
if (isset($unique) && $unique) {
|
|
|
|
$table->addUniqueIndex($fields, $name);
|
|
|
|
} else {
|
|
|
|
$table->addIndex($fields, $name);
|
|
|
|
}
|
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
|
|
|
/**
|
|
|
|
* @param \SimpleXMLElement | string $xml
|
|
|
|
* @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;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|