Welcome to mirror list, hosted at ThFree Co, Russian Federation.

mdb2schemawriter.php « db « private « lib - github.com/nextcloud/server.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 1fb71ab398bf217f6b372f9aa9a3693182ef0c7a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
<?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.
 */

class OC_DB_MDB2SchemaWriter {

	/**
	 * @param string $file
	 * @param \OC\DB\Connection $conn
	 * @return bool
	 */
	static public function saveSchemaToFile($file, \OC\DB\Connection $conn) {
		$config = \OC::$server->getConfig();

		$xml = new SimpleXMLElement('<database/>');
		$xml->addChild('name', $config->getSystemValue('dbname', 'owncloud'));
		$xml->addChild('create', 'true');
		$xml->addChild('overwrite', 'false');
		$xml->addChild('charset', 'utf8');

		// 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);

		foreach ($conn->getSchemaManager()->listTables() as $table) {
			self::saveTable($table, $xml->addChild('table'));
		}
		file_put_contents($file, $xml->asXML());
		return true;
	}

	/**
	 * @param SimpleXMLElement $xml
	 */
	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'));
		}
	}

	/**
	 * @param SimpleXMLElement $xml
	 */
	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;

		}
	}

	/**
	 * @param SimpleXMLElement $xml
	 */
	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';
	}
}