|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Wikibase\Database\MediaWiki; |
|
4
|
|
|
|
|
5
|
|
|
use DatabaseBase; |
|
6
|
|
|
use Iterator; |
|
7
|
|
|
use MWException; |
|
8
|
|
|
use Wikibase\Database\Exception\DeleteFailedException; |
|
9
|
|
|
use Wikibase\Database\Exception\InsertFailedException; |
|
10
|
|
|
use Wikibase\Database\Exception\QueryInterfaceException; |
|
11
|
|
|
use Wikibase\Database\Exception\SelectFailedException; |
|
12
|
|
|
use Wikibase\Database\Exception\UpdateFailedException; |
|
13
|
|
|
use Wikibase\Database\QueryInterface; |
|
14
|
|
|
|
|
15
|
|
|
/** |
|
16
|
|
|
* Implementation of the QueryInterface interface using the MediaWiki |
|
17
|
|
|
* database abstraction layer where possible. |
|
18
|
|
|
* |
|
19
|
|
|
* @since 0.1 |
|
20
|
|
|
* @licence GNU GPL v2+ |
|
21
|
|
|
* @author Jeroen De Dauw < [email protected] > |
|
22
|
|
|
* @author Thiemo Kreuz |
|
23
|
|
|
*/ |
|
24
|
|
|
class MediaWikiQueryInterface implements QueryInterface { |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* @var DBConnectionProvider |
|
28
|
|
|
*/ |
|
29
|
|
|
private $connectionProvider; |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* @since 0.1 |
|
33
|
|
|
* |
|
34
|
|
|
* @param DBConnectionProvider $connectionProvider |
|
35
|
|
|
*/ |
|
36
|
|
|
public function __construct( DBConnectionProvider $connectionProvider ) { |
|
37
|
|
|
$this->connectionProvider = $connectionProvider; |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* @return DatabaseBase |
|
42
|
|
|
*/ |
|
43
|
|
|
private function getDB() { |
|
44
|
|
|
return $this->connectionProvider->getConnection(); |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* @see QueryInterface::tableExists |
|
49
|
|
|
* |
|
50
|
|
|
* @since 0.1 |
|
51
|
|
|
* |
|
52
|
|
|
* @param string $tableName |
|
53
|
|
|
* |
|
54
|
|
|
* @return bool |
|
55
|
|
|
*/ |
|
56
|
|
|
public function tableExists( $tableName ) { |
|
57
|
|
|
return $this->getDB()->tableExists( $tableName, __METHOD__ ); |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
/** |
|
61
|
|
|
* @see QueryInterface::insert |
|
62
|
|
|
* |
|
63
|
|
|
* @since 0.1 |
|
64
|
|
|
* |
|
65
|
|
|
* @param string $tableName |
|
66
|
|
|
* @param array $values |
|
67
|
|
|
* |
|
68
|
|
|
* @throws InsertFailedException |
|
69
|
|
|
*/ |
|
70
|
|
|
public function insert( $tableName, array $values ) { |
|
71
|
|
|
try { |
|
72
|
|
|
$result = $this->getDB()->insert( $tableName, $values, __METHOD__ ); |
|
73
|
|
|
|
|
74
|
|
|
if ( $result === false ) { |
|
75
|
|
|
throw new InsertFailedException( $tableName, $values ); |
|
76
|
|
|
} |
|
77
|
|
|
} catch ( MWException $ex ) { |
|
|
|
|
|
|
78
|
|
|
throw new InsertFailedException( $tableName, $values, $ex->getMessage(), $ex ); |
|
79
|
|
|
} |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
/** |
|
83
|
|
|
* @see QueryInterface::update |
|
84
|
|
|
* |
|
85
|
|
|
* @since 0.1 |
|
86
|
|
|
* |
|
87
|
|
|
* @param string $tableName |
|
88
|
|
|
* @param array $values |
|
89
|
|
|
* @param array $conditions |
|
90
|
|
|
* |
|
91
|
|
|
* @throws UpdateFailedException |
|
92
|
|
|
*/ |
|
93
|
|
|
public function update( $tableName, array $values, array $conditions ) { |
|
94
|
|
|
try { |
|
95
|
|
|
$result = $this->getDB()->update( $tableName, $values, $conditions, __METHOD__ ); |
|
96
|
|
|
|
|
97
|
|
|
if ( $result === false ) { |
|
98
|
|
|
throw new UpdateFailedException( $tableName, $values, $conditions ); |
|
99
|
|
|
} |
|
100
|
|
|
} catch ( MWException $ex ) { |
|
|
|
|
|
|
101
|
|
|
throw new UpdateFailedException( $tableName, $values, $conditions, $ex->getMessage(), $ex ); |
|
102
|
|
|
} |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
|
|
/** |
|
106
|
|
|
* @see QueryInterface::delete |
|
107
|
|
|
* |
|
108
|
|
|
* @since 0.1 |
|
109
|
|
|
* |
|
110
|
|
|
* @param string $tableName |
|
111
|
|
|
* @param array $conditions |
|
112
|
|
|
* |
|
113
|
|
|
* @throws DeleteFailedException |
|
114
|
|
|
*/ |
|
115
|
|
|
public function delete( $tableName, array $conditions ) { |
|
116
|
|
|
try { |
|
117
|
|
|
$result = $this->getDB()->delete( $tableName, $conditions, __METHOD__ ); |
|
118
|
|
|
|
|
119
|
|
|
if ( $result === false ) { |
|
120
|
|
|
throw new DeleteFailedException( $tableName, $conditions ); |
|
121
|
|
|
} |
|
122
|
|
|
} catch ( MWException $ex ) { |
|
|
|
|
|
|
123
|
|
|
throw new DeleteFailedException( $tableName, $conditions, $ex->getMessage(), $ex ); |
|
124
|
|
|
} |
|
125
|
|
|
} |
|
126
|
|
|
|
|
127
|
|
|
/** |
|
128
|
|
|
* @see QueryInterface::getInsertId |
|
129
|
|
|
* |
|
130
|
|
|
* @since 0.1 |
|
131
|
|
|
* |
|
132
|
|
|
* @throws QueryInterfaceException |
|
133
|
|
|
* @return int |
|
134
|
|
|
*/ |
|
135
|
|
|
public function getInsertId() { |
|
136
|
|
|
$databaseBase = $this->getDB(); |
|
137
|
|
|
|
|
138
|
|
|
if ( !method_exists( $databaseBase, 'insertId' ) ) { |
|
139
|
|
|
throw new QueryInterfaceException( 'Connection does not support obtain the last inserted ID' ); |
|
140
|
|
|
} |
|
141
|
|
|
|
|
142
|
|
|
return (int)$databaseBase->insertId(); |
|
143
|
|
|
} |
|
144
|
|
|
|
|
145
|
|
|
/** |
|
146
|
|
|
* @see QueryInterface::select |
|
147
|
|
|
* |
|
148
|
|
|
* @since 0.1 |
|
149
|
|
|
* |
|
150
|
|
|
* @param string $tableName |
|
151
|
|
|
* @param string[] $fieldNames |
|
152
|
|
|
* @param array $conditions |
|
153
|
|
|
* @param array $options |
|
154
|
|
|
* |
|
155
|
|
|
* @throws SelectFailedException |
|
156
|
|
|
* @return Iterator |
|
157
|
|
|
*/ |
|
158
|
|
|
public function select( $tableName, array $fieldNames, array $conditions, array $options = array() ) { |
|
159
|
|
|
$selectionResult = $this->getDB()->select( |
|
160
|
|
|
$tableName, |
|
161
|
|
|
$fieldNames, |
|
162
|
|
|
$conditions, |
|
163
|
|
|
__METHOD__, |
|
164
|
|
|
$options |
|
165
|
|
|
); |
|
166
|
|
|
|
|
167
|
|
|
if ( $selectionResult instanceof \ResultWrapper ) { |
|
|
|
|
|
|
168
|
|
|
// TODO: change to return arrays instead of objects |
|
169
|
|
|
return $selectionResult; |
|
|
|
|
|
|
170
|
|
|
} |
|
171
|
|
|
|
|
172
|
|
|
throw new SelectFailedException( $tableName, $fieldNames, $conditions ); |
|
173
|
|
|
} |
|
174
|
|
|
|
|
175
|
|
|
} |
|
176
|
|
|
|