2012-10-26 08:51:21 +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 tbelau666 <thomas.belau@gmx.de>
|
|
|
|
* @author Thomas Müller <thomas.mueller@tmit.eu>
|
|
|
|
*
|
|
|
|
* @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-26 08:51:21 +00:00
|
|
|
*/
|
2015-02-26 10:37:37 +00:00
|
|
|
|
2016-01-07 08:29:21 +00:00
|
|
|
namespace OC\DB;
|
|
|
|
|
|
|
|
use Doctrine\DBAL\Schema\Column;
|
|
|
|
use Doctrine\DBAL\Schema\Index;
|
|
|
|
|
|
|
|
class MDB2SchemaWriter {
|
2013-06-27 21:34:36 +00:00
|
|
|
|
|
|
|
/**
|
2014-02-06 15:30:58 +00:00
|
|
|
* @param string $file
|
2014-12-22 09:44:30 +00:00
|
|
|
* @param \OC\DB\Connection $conn
|
2013-06-27 21:34:36 +00:00
|
|
|
* @return bool
|
|
|
|
*/
|
2014-12-22 09:44:30 +00:00
|
|
|
static public function saveSchemaToFile($file, \OC\DB\Connection $conn) {
|
|
|
|
$config = \OC::$server->getConfig();
|
|
|
|
|
2016-01-07 08:29:21 +00:00
|
|
|
$xml = new \SimpleXMLElement('<database/>');
|
2014-12-22 09:44:30 +00:00
|
|
|
$xml->addChild('name', $config->getSystemValue('dbname', 'owncloud'));
|
2012-10-26 08:51:21 +00:00
|
|
|
$xml->addChild('create', 'true');
|
|
|
|
$xml->addChild('overwrite', 'false');
|
2015-07-30 11:57:04 +00:00
|
|
|
if($config->getSystemValue('dbtype', 'sqlite') === 'mysql' && $config->getSystemValue('mysql.utf8mb4', false)) {
|
|
|
|
$xml->addChild('charset', 'utf8mb4');
|
|
|
|
} else {
|
|
|
|
$xml->addChild('charset', 'utf8');
|
|
|
|
}
|
2014-12-22 09:44:30 +00:00
|
|
|
|
2015-01-12 12:57:46 +00:00
|
|
|
// FIX ME: bloody work around
|
|
|
|
if ($config->getSystemValue('dbtype', 'sqlite') === 'oci') {
|
|
|
|
$filterExpression = '/^"' . preg_quote($conn->getPrefix()) . '/';
|
|
|
|
} else {
|
|
|
|
$filterExpression = '/^' . preg_quote($conn->getPrefix()) . '/';
|
|
|
|
}
|
|
|
|
$conn->getConfiguration()->setFilterSchemaAssetsExpression($filterExpression);
|
2014-12-22 09:44:30 +00:00
|
|
|
|
2014-11-30 22:17:09 +00:00
|
|
|
foreach ($conn->getSchemaManager()->listTables() as $table) {
|
2012-10-26 08:51:21 +00:00
|
|
|
self::saveTable($table, $xml->addChild('table'));
|
|
|
|
}
|
|
|
|
file_put_contents($file, $xml->asXML());
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2014-02-06 15:30:58 +00:00
|
|
|
/**
|
2016-01-07 08:29:21 +00:00
|
|
|
* @param \Doctrine\DBAL\Schema\Table $table
|
|
|
|
* @param \SimpleXMLElement $xml
|
2014-02-06 15:30:58 +00:00
|
|
|
*/
|
2012-10-26 08:51:21 +00:00
|
|
|
private static function saveTable($table, $xml) {
|
|
|
|
$xml->addChild('name', $table->getName());
|
|
|
|
$declaration = $xml->addChild('declaration');
|
|
|
|
foreach($table->getColumns() as $column) {
|
|
|
|
self::saveColumn($column, $declaration->addChild('field'));
|
|
|
|
}
|
|
|
|
foreach($table->getIndexes() as $index) {
|
|
|
|
if ($index->getName() == 'PRIMARY') {
|
|
|
|
$autoincrement = false;
|
|
|
|
foreach($index->getColumns() as $column) {
|
|
|
|
if ($table->getColumn($column)->getAutoincrement()) {
|
|
|
|
$autoincrement = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if ($autoincrement) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
self::saveIndex($index, $declaration->addChild('index'));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-02-19 08:31:54 +00:00
|
|
|
/**
|
2016-01-07 08:29:21 +00:00
|
|
|
* @param Column $column
|
|
|
|
* @param \SimpleXMLElement $xml
|
2014-02-19 08:31:54 +00:00
|
|
|
*/
|
2012-10-26 08:51:21 +00:00
|
|
|
private static function saveColumn($column, $xml) {
|
|
|
|
$xml->addChild('name', $column->getName());
|
|
|
|
switch($column->getType()) {
|
|
|
|
case 'SmallInt':
|
|
|
|
case 'Integer':
|
|
|
|
case 'BigInt':
|
|
|
|
$xml->addChild('type', 'integer');
|
|
|
|
$default = $column->getDefault();
|
|
|
|
if (is_null($default) && $column->getAutoincrement()) {
|
|
|
|
$default = '0';
|
|
|
|
}
|
|
|
|
$xml->addChild('default', $default);
|
|
|
|
$xml->addChild('notnull', self::toBool($column->getNotnull()));
|
|
|
|
if ($column->getAutoincrement()) {
|
|
|
|
$xml->addChild('autoincrement', '1');
|
|
|
|
}
|
|
|
|
if ($column->getUnsigned()) {
|
|
|
|
$xml->addChild('unsigned', 'true');
|
|
|
|
}
|
|
|
|
$length = '4';
|
|
|
|
if ($column->getType() == 'SmallInt') {
|
|
|
|
$length = '2';
|
|
|
|
}
|
|
|
|
elseif ($column->getType() == 'BigInt') {
|
|
|
|
$length = '8';
|
|
|
|
}
|
|
|
|
$xml->addChild('length', $length);
|
|
|
|
break;
|
|
|
|
case 'String':
|
|
|
|
$xml->addChild('type', 'text');
|
|
|
|
$default = trim($column->getDefault());
|
|
|
|
if ($default === '') {
|
|
|
|
$default = false;
|
|
|
|
}
|
|
|
|
$xml->addChild('default', $default);
|
|
|
|
$xml->addChild('notnull', self::toBool($column->getNotnull()));
|
|
|
|
$xml->addChild('length', $column->getLength());
|
|
|
|
break;
|
|
|
|
case 'Text':
|
|
|
|
$xml->addChild('type', 'clob');
|
|
|
|
$xml->addChild('notnull', self::toBool($column->getNotnull()));
|
|
|
|
break;
|
|
|
|
case 'Decimal':
|
|
|
|
$xml->addChild('type', 'decimal');
|
|
|
|
$xml->addChild('default', $column->getDefault());
|
|
|
|
$xml->addChild('notnull', self::toBool($column->getNotnull()));
|
|
|
|
$xml->addChild('length', '15');
|
|
|
|
break;
|
|
|
|
case 'Boolean':
|
|
|
|
$xml->addChild('type', 'integer');
|
|
|
|
$xml->addChild('default', $column->getDefault());
|
|
|
|
$xml->addChild('notnull', self::toBool($column->getNotnull()));
|
|
|
|
$xml->addChild('length', '1');
|
|
|
|
break;
|
|
|
|
case 'DateTime':
|
|
|
|
$xml->addChild('type', 'timestamp');
|
|
|
|
$xml->addChild('default', $column->getDefault());
|
|
|
|
$xml->addChild('notnull', self::toBool($column->getNotnull()));
|
|
|
|
break;
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-02-19 08:31:54 +00:00
|
|
|
/**
|
2016-01-07 08:29:21 +00:00
|
|
|
* @param Index $index
|
|
|
|
* @param \SimpleXMLElement $xml
|
2014-02-19 08:31:54 +00:00
|
|
|
*/
|
2012-10-26 08:51:21 +00:00
|
|
|
private static function saveIndex($index, $xml) {
|
|
|
|
$xml->addChild('name', $index->getName());
|
|
|
|
if ($index->isPrimary()) {
|
|
|
|
$xml->addChild('primary', 'true');
|
|
|
|
}
|
|
|
|
elseif ($index->isUnique()) {
|
|
|
|
$xml->addChild('unique', 'true');
|
|
|
|
}
|
|
|
|
foreach($index->getColumns() as $column) {
|
|
|
|
$field = $xml->addChild('field');
|
|
|
|
$field->addChild('name', $column);
|
|
|
|
$field->addChild('sorting', 'ascending');
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private static function toBool($bool) {
|
|
|
|
return $bool ? 'true' : 'false';
|
|
|
|
}
|
|
|
|
}
|