DataValueHandler   A
last analyzed

Complexity

Total Complexity 10

Size/Duplication

Total Lines 149
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 5

Importance

Changes 0
Metric Value
wmc 10
lcom 2
cbo 5
dl 0
loc 149
rs 10
c 0
b 0
f 0

10 Methods

Rating   Name   Duplication   Size   Complexity  
A getTableName() 0 9 2
A setTablePrefix() 0 7 2
A constructTable() 0 7 1
getBaseTableName() 0 1 ?
completeTable() 0 1 ?
A getEqualityFieldName() 0 3 1
A getSortFieldNames() 0 3 1
getInsertValues() 0 1 ?
A getEqualityFieldValue() 0 3 1
A addMatchConditions() 0 9 2
1
<?php
2
3
namespace Wikibase\QueryEngine\SQLStore;
4
5
use Ask\Language\Description\ValueDescription;
6
use DataValues\DataValue;
7
use Doctrine\DBAL\Query\QueryBuilder;
8
use Doctrine\DBAL\Schema\Table;
9
use InvalidArgumentException;
10
use RuntimeException;
11
use Wikibase\QueryEngine\QueryNotSupportedException;
12
13
/**
14
 * Represents the mapping between a DataValue type and the
15
 * associated implementation in the store.
16
 *
17
 * @since 0.1
18
 *
19
 * @licence GNU GPL v2+
20
 * @author Jeroen De Dauw < [email protected] >
21
 */
22
abstract class DataValueHandler {
23
24
	/**
25
	 * Needs to be set by the constructor.
26
	 *
27
	 * @var string|null
28
	 */
29
	protected $tableName = null;
30
31
	/**
32
	 * Returns the full name of the table.
33
	 * This is the same value as ->constructTable()->getName().
34
	 *
35
	 * @return string
36
	 *
37
	 * @throws RuntimeException
38
	 */
39
	public function getTableName() {
40
		if ( $this->tableName === null ) {
41
			throw new RuntimeException(
42
				'Cannot get the table name when the table name prefix has not been set yet'
43
			);
44
		}
45
46
		return $this->tableName;
47
	}
48
49
	/**
50
	 * Prefixes the table name. This needs to be called once, and only once,
51
	 * before getTableName or constructTable are called.
52
	 *
53
	 * @param string $tablePrefix
54
	 *
55
	 * @throws RuntimeException
56
	 */
57
	public function setTablePrefix( $tablePrefix ) {
58
		if ( $this->tableName !== null ) {
59
			throw new RuntimeException( 'Cannot set the table name prefix more than once' );
60
		}
61
62
		$this->tableName = $tablePrefix . $this->getBaseTableName();
63
	}
64
65
	/**
66
	 * Returns a Table object that represents the schema of the data value table.
67
	 *
68
	 * @return Table
69
	 */
70
	public function constructTable() {
71
		$table = new Table( $this->getTableName() );
72
73
		$this->completeTable( $table );
74
75
		return $table;
76
	}
77
78
	/**
79
	 * Returns the base name of the table.
80
	 * This does not contain any prefixes indicating which store it
81
	 * belongs to or what the role of the data value it handles is.
82
	 *
83
	 * @return string
84
	 */
85
	abstract protected function getBaseTableName();
86
87
	/**
88
	 * @param Table $table
89
	 */
90
	abstract protected function completeTable( Table $table );
91
92
	/**
93
	 * Returns the name of the field that holds a value suitable for equality checks.
94
	 *
95
	 * This field should not exceed 255 chars index space equivalent.
96
	 *
97
	 * @since 0.1
98
	 *
99
	 * @return string
100
	 */
101
	public function getEqualityFieldName() {
102
		return 'hash';
103
	}
104
105
	/**
106
	 * Returns the names of the fields used to order this type of DataValue.
107
	 * Usually a single field. Must not be an empty array.
108
	 *
109
	 * @since 0.1
110
	 *
111
	 * @return string[]
112
	 */
113
	public function getSortFieldNames() {
114
		return array( $this->getEqualityFieldName() );
115
	}
116
117
	/**
118
	 * Return an array of fields=>values that is to be inserted when
119
	 * writing the given DataValue to the database. Values should be set
120
	 * for all columns, even if NULL. This array is used to perform all
121
	 * insert operations into the DB.
122
	 *
123
	 * The passed DataValue needs to be of a type supported by the DataValueHandler.
124
	 * If it is not supported, an InvalidArgumentException might be thrown.
125
	 *
126
	 * @since 0.1
127
	 *
128
	 * @param DataValue $value
129
	 *
130
	 * @return array
131
	 * @throws InvalidArgumentException
132
	 */
133
	abstract public function getInsertValues( DataValue $value );
134
135
	/**
136
	 * Returns the equality field value for a given data value.
137
	 * This value is needed for constructing equality checking
138
	 * queries.
139
	 *
140
	 * @since 0.1
141
	 *
142
	 * @param DataValue $value
143
	 *
144
	 * @return mixed
145
	 * @throws InvalidArgumentException
146
	 */
147
	public function getEqualityFieldValue( DataValue $value ) {
148
		return $value->getHash();
149
	}
150
151
	/**
152
	 * @since 0.2
153
	 *
154
	 * @param QueryBuilder $builder
155
	 * @param ValueDescription $description
156
	 *
157
	 * @throws InvalidArgumentException
158
	 * @throws QueryNotSupportedException
159
	 */
160
	public function addMatchConditions( QueryBuilder $builder, ValueDescription $description ) {
161
		if ( $description->getComparator() === ValueDescription::COMP_EQUAL ) {
162
			$builder->andWhere( $this->getEqualityFieldName() . '= :equality' );
163
			$builder->setParameter( ':equality', $this->getEqualityFieldValue( $description->getValue() ) );
164
		}
165
		else {
166
			throw new QueryNotSupportedException( $description, 'Only equality is supported' );
167
		}
168
	}
169
170
}
171