Completed
Push — master ( 58d53e...73f4ae )
by Lukas
13:40
created

MDB2SchemaWriter   A

Complexity

Total Complexity 34

Size/Duplication

Total Lines 152
Duplicated Lines 7.89 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 0
Metric Value
dl 12
loc 152
rs 9.2
c 0
b 0
f 0
wmc 34
lcom 1
cbo 4

5 Methods

Rating   Name   Duplication   Size   Complexity  
B saveSchemaToFile() 0 27 5
B saveTable() 0 21 7
C saveColumn() 12 62 16
A saveIndex() 0 15 4
A toBool() 0 3 2

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
/**
3
 * @copyright Copyright (c) 2016, ownCloud, Inc.
4
 *
5
 * @author Bart Visscher <[email protected]>
6
 * @author Jörn Friedrich Dreyer <[email protected]>
7
 * @author Morris Jobke <[email protected]>
8
 * @author tbelau666 <[email protected]>
9
 * @author Thomas Müller <[email protected]>
10
 *
11
 * @license AGPL-3.0
12
 *
13
 * This code is free software: you can redistribute it and/or modify
14
 * it under the terms of the GNU Affero General Public License, version 3,
15
 * as published by the Free Software Foundation.
16
 *
17
 * This program is distributed in the hope that it will be useful,
18
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20
 * GNU Affero General Public License for more details.
21
 *
22
 * You should have received a copy of the GNU Affero General Public License, version 3,
23
 * along with this program.  If not, see <http://www.gnu.org/licenses/>
24
 *
25
 */
26
27
namespace OC\DB;
28
29
use Doctrine\DBAL\Schema\Column;
30
use Doctrine\DBAL\Schema\Index;
31
32
class MDB2SchemaWriter {
33
34
	/**
35
	 * @param string $file
36
	 * @param \OC\DB\Connection $conn
37
	 * @return bool
38
	 */
39
	static public function saveSchemaToFile($file, \OC\DB\Connection $conn) {
40
		$config = \OC::$server->getConfig();
41
42
		$xml = new \SimpleXMLElement('<database/>');
43
		$xml->addChild('name', $config->getSystemValue('dbname', 'owncloud'));
44
		$xml->addChild('create', 'true');
45
		$xml->addChild('overwrite', 'false');
46
		if($config->getSystemValue('dbtype', 'sqlite') === 'mysql' && $config->getSystemValue('mysql.utf8mb4', false)) {
47
			$xml->addChild('charset', 'utf8mb4');
48
		} else {
49
			$xml->addChild('charset', 'utf8');
50
		}
51
52
		// FIX ME: bloody work around
53
		if ($config->getSystemValue('dbtype', 'sqlite') === 'oci') {
54
			$filterExpression = '/^"' . preg_quote($conn->getPrefix()) . '/';
55
		} else {
56
			$filterExpression = '/^' . preg_quote($conn->getPrefix()) . '/';
57
		}
58
		$conn->getConfiguration()->setFilterSchemaAssetsExpression($filterExpression);
59
60
		foreach ($conn->getSchemaManager()->listTables() as $table) {
61
			self::saveTable($table, $xml->addChild('table'));
62
		}
63
		file_put_contents($file, $xml->asXML());
64
		return true;
65
	}
66
67
	/**
68
	 * @param \Doctrine\DBAL\Schema\Table $table
69
	 * @param \SimpleXMLElement $xml
70
	 */
71
	private static function saveTable($table, $xml) {
72
		$xml->addChild('name', $table->getName());
73
		$declaration = $xml->addChild('declaration');
74
		foreach($table->getColumns() as $column) {
75
			self::saveColumn($column, $declaration->addChild('field'));
76
		}
77
		foreach($table->getIndexes() as $index) {
78
			if ($index->getName() == 'PRIMARY') {
79
				$autoincrement = false;
80
				foreach($index->getColumns() as $column) {
81
					if ($table->getColumn($column)->getAutoincrement()) {
82
						$autoincrement = true;
83
					}
84
				}
85
				if ($autoincrement) {
86
					continue;
87
				}
88
			}
89
			self::saveIndex($index, $declaration->addChild('index'));
90
		}
91
	}
92
93
	/**
94
	 * @param Column $column
95
	 * @param \SimpleXMLElement $xml
96
	 */
97
	private static function saveColumn($column, $xml) {
98
		$xml->addChild('name', $column->getName());
99
		switch($column->getType()) {
100
			case 'SmallInt':
101
			case 'Integer':
102
			case 'BigInt':
103
				$xml->addChild('type', 'integer');
104
				$default = $column->getDefault();
105
				if (is_null($default) && $column->getAutoincrement()) {
106
					$default = '0';
107
				}
108
				$xml->addChild('default', $default);
109
				$xml->addChild('notnull', self::toBool($column->getNotnull()));
110
				if ($column->getAutoincrement()) {
111
					$xml->addChild('autoincrement', '1');
112
				}
113
				if ($column->getUnsigned()) {
114
					$xml->addChild('unsigned', 'true');
115
				}
116
				$length = '4';
117
				if ($column->getType() == 'SmallInt') {
118
					$length = '2';
119
				}
120
				elseif ($column->getType() == 'BigInt') {
121
					$length = '8';
122
				}
123
				$xml->addChild('length', $length);
124
				break;
125
			case 'String':
126
				$xml->addChild('type', 'text');
127
				$default = trim($column->getDefault());
128
				if ($default === '') {
129
					$default = false;
130
				}
131
				$xml->addChild('default', $default);
132
				$xml->addChild('notnull', self::toBool($column->getNotnull()));
133
				$xml->addChild('length', $column->getLength());
134
				break;
135
			case 'Text':
136
				$xml->addChild('type', 'clob');
137
				$xml->addChild('notnull', self::toBool($column->getNotnull()));
138
				break;
139 View Code Duplication
			case 'Decimal':
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
140
				$xml->addChild('type', 'decimal');
141
				$xml->addChild('default', $column->getDefault());
142
				$xml->addChild('notnull', self::toBool($column->getNotnull()));
143
				$xml->addChild('length', '15');
144
				break;
145 View Code Duplication
			case 'Boolean':
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
146
				$xml->addChild('type', 'integer');
147
				$xml->addChild('default', $column->getDefault());
148
				$xml->addChild('notnull', self::toBool($column->getNotnull()));
149
				$xml->addChild('length', '1');
150
				break;
151
			case 'DateTime':
152
				$xml->addChild('type', 'timestamp');
153
				$xml->addChild('default', $column->getDefault());
154
				$xml->addChild('notnull', self::toBool($column->getNotnull()));
155
				break;
156
157
		}
158
	}
159
160
	/**
161
	 * @param Index $index
162
	 * @param \SimpleXMLElement $xml
163
	 */
164
	private static function saveIndex($index, $xml) {
165
		$xml->addChild('name', $index->getName());
166
		if ($index->isPrimary()) {
167
			$xml->addChild('primary', 'true');
168
		}
169
		elseif ($index->isUnique()) {
170
			$xml->addChild('unique', 'true');
171
		}
172
		foreach($index->getColumns() as $column) {
173
			$field = $xml->addChild('field');
174
			$field->addChild('name', $column);
175
			$field->addChild('sorting', 'ascending');
176
			
177
		}
178
	}
179
180
	private static function toBool($bool) {
181
		return $bool ? 'true' : 'false';
182
	}
183
}
184