StoreSchema   A
last analyzed

Complexity

Total Complexity 12

Size/Duplication

Total Lines 112
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
wmc 12
lcom 1
cbo 3
dl 0
loc 112
rs 10
c 0
b 0
f 0

8 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 12 3
A getDataValueHandlers() 0 3 1
A getTables() 0 8 1
A getDvTables() 0 13 3
A getTableFromDvHandler() 0 14 1
A addCommonColumnsToTable() 0 15 1
A addCommonIndexesToTable() 0 5 1
A getValuelessSnaksTable() 0 10 1
1
<?php
2
3
namespace Wikibase\QueryEngine\SQLStore;
4
5
use Doctrine\DBAL\Schema\Table;
6
use Doctrine\DBAL\Types\Type;
7
8
/**
9
 * Contains the tables and table interactors for a given SQLStore configuration.
10
 *
11
 * @since 0.1
12
 *
13
 * @licence GNU GPL v2+
14
 * @author Jeroen De Dauw < [email protected] >
15
 */
16
class StoreSchema {
17
18
	private $tablePrefix;
19
	private $dvHandlers;
20
21
	/**
22
	 * @param string $tablePrefix
23
	 * @param DataValueHandlers $dataValueHandlers
24
	 */
25
	public function __construct( $tablePrefix, DataValueHandlers $dataValueHandlers ) {
26
		$this->tablePrefix = $tablePrefix;
27
		$this->dvHandlers = $dataValueHandlers;
28
29
		foreach ( $this->dvHandlers->getMainSnakHandlers() as $dvHandler ) {
30
			$dvHandler->setTablePrefix( $this->tablePrefix . 'mainsnak_' );
31
		}
32
33
		foreach ( $this->dvHandlers->getQualifierHandlers() as $dvHandler ) {
34
			$dvHandler->setTablePrefix( $this->tablePrefix . 'qualifier_' );
35
		}
36
	}
37
38
	/**
39
	 * @return DataValueHandlers
40
	 */
41
	public function getDataValueHandlers() {
42
		return $this->dvHandlers;
43
	}
44
45
	/**
46
	 * Returns all tables part of the stores schema.
47
	 *
48
	 * @return Table[]
49
	 */
50
	public function getTables() {
51
		return array_merge(
52
			$this->getDvTables(),
53
			array(
54
				$this->getValuelessSnaksTable()
55
			)
56
		);
57
	}
58
59
	/**
60
	 * @return Table[]
61
	 */
62
	private function getDvTables() {
63
		$tables = array();
64
65
		foreach ( $this->dvHandlers->getMainSnakHandlers() as $dvHandler ) {
66
			$tables[] = $this->getTableFromDvHandler( $dvHandler );
67
		}
68
69
		foreach ( $this->dvHandlers->getQualifierHandlers() as $dvHandler ) {
70
			$tables[] = $this->getTableFromDvHandler( $dvHandler );
71
		}
72
73
		return $tables;
74
	}
75
76
	private function getTableFromDvHandler( DataValueHandler $handler ) {
77
		$table = $handler->constructTable();
78
79
		$this->addCommonColumnsToTable( $table );
80
		$this->addCommonIndexesToTable( $table );
81
82
		$table->addUniqueIndex( array(
83
			$handler->getEqualityFieldName(),
84
			'property_id',
85
			'subject_id',
86
		) );
87
88
		return $table;
89
	}
90
91
	private function addCommonColumnsToTable( Table $table ) {
92
		$table->addColumn(
93
			'row_id',
94
			Type::INTEGER,
95
			array(
96
				'autoincrement' => true,
97
				'unsigned' => true,
98
			)
99
		);
100
101
		$table->addColumn( 'subject_id', Type::STRING, array( 'length' => 16 ) );
102
		$table->addColumn( 'subject_type', Type::STRING, array( 'length' => 8 ) );
103
		$table->addColumn( 'property_id', Type::STRING, array( 'length' => 16 ) );
104
		$table->addColumn( 'statement_rank', Type::SMALLINT );
105
	}
106
107
	private function addCommonIndexesToTable( Table $table ) {
108
		$table->setPrimaryKey( array( 'row_id' ) );
109
		$table->addIndex( array( 'subject_id' ) );
110
		$table->addIndex( array( 'property_id' ) );
111
	}
112
113
	/**
114
	 * @return Table
115
	 */
116
	public function getValuelessSnaksTable() {
117
		$table = new Table( $this->tablePrefix . 'valueless_snaks' );
118
119
		$table->addColumn( 'snak_type', Type::INTEGER, array( 'unsigned' => true ) );
120
121
		$this->addCommonColumnsToTable( $table );
122
		$this->addCommonIndexesToTable( $table );
123
124
		return $table;
125
	}
126
127
}
128