-
-
Notifications
You must be signed in to change notification settings - Fork 85
Expand file tree
/
Copy pathQuerySegmentListProcessor.php
More file actions
390 lines (326 loc) · 11.3 KB
/
QuerySegmentListProcessor.php
File metadata and controls
390 lines (326 loc) · 11.3 KB
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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
<?php
/**
* adds $this->joinConditions and $this->fromTables
* to the original QuerySegmentListProcessor for the
* use with SearchPanes
*/
namespace SRF\DataTables;
use RuntimeException;
use SMW\MediaWiki\Database;
use SMW\SQLStore\QueryEngine\QuerySegment;
use SMW\SQLStore\TableBuilder\TemporaryTableBuilder;
use SMWQuery as Query;
class QuerySegmentListProcessor {
/* @var array */
public $joinConditions = [];
/* @var array */
public $fromTables = [];
/**
* @var Database
*/
private $connection;
/**
* @var TemporaryTableBuilder
*/
private $temporaryTableBuilder;
/**
* @var HierarchyTempTableBuilder
*/
private $hierarchyTempTableBuilder;
/**
* Array of arrays of executed queries, indexed by the temporary table names
* results were fed into.
*
* @var array
*/
private $executedQueries = [];
/**
* Query mode copied from given query. Some submethods act differently when
* in Query::MODE_DEBUG.
*
* @var int
*/
private $queryMode;
/**
* @var array
*/
private $querySegmentList = [];
/**
* @param Database $connection
* @param TemporaryTableBuilder $temporaryTableBuilder
* @param HierarchyTempTableBuilder $hierarchyTempTableBuilder
*/
public function __construct( $connection, TemporaryTableBuilder $temporaryTableBuilder, $hierarchyTempTableBuilder ) {
$this->connection = $connection;
$this->temporaryTableBuilder = $temporaryTableBuilder;
$this->hierarchyTempTableBuilder = $hierarchyTempTableBuilder;
}
/**
* @since 2.2
*
* @return array
*/
public function getExecutedQueries() {
return $this->executedQueries;
}
/**
* @since 2.2
*
* @param &$querySegmentList
*/
public function setQuerySegmentList( &$querySegmentList ) {
$this->querySegmentList =& $querySegmentList;
}
/**
* @since 2.2
*
* @param int $queryMode
*/
public function setQueryMode( $queryMode ) {
$this->queryMode = $queryMode;
}
/**
* Process stored queries and change store accordingly. The query obj is modified
* so that it contains non-recursive description of a select to execute for getting
* the actual result.
*
* @param int $id
*
* @throws RuntimeException
*/
public function process( $id ) {
$this->hierarchyTempTableBuilder->emptyHierarchyCache();
$this->executedQueries = [];
// Should never happen
if ( !isset( $this->querySegmentList[$id] ) ) {
throw new RuntimeException( "$id doesn't exist" );
}
$this->segment( $this->querySegmentList[$id] );
}
private function segment( QuerySegment &$query ) {
switch ( $query->type ) {
case QuerySegment::Q_TABLE: // .
$this->table( $query );
break;
case QuerySegment::Q_CONJUNCTION:
$this->conjunction( $query );
break;
case QuerySegment::Q_DISJUNCTION:
$this->disjunction( $query );
break;
case QuerySegment::Q_PROP_HIERARCHY:
case QuerySegment::Q_CLASS_HIERARCHY: // make a saturated hierarchy
$this->hierarchy( $query );
break;
case QuerySegment::Q_VALUE:
break; // nothing to do
}
}
/**
* Resolves normal queries with possible conjunctive subconditions
*/
private function table( QuerySegment &$query ) {
foreach ( $query->components as $qid => $joinField ) {
$subQuery = $this->querySegmentList[$qid];
$this->segment( $subQuery );
$alias = $subQuery->alias;
if ( $subQuery->joinTable !== '' ) { // Join with jointable.joinfield
$op = $subQuery->not ? '!' : '';
$joinType = $subQuery->joinType ? $subQuery->joinType : 'INNER';
$t = $this->connection->tableName( $subQuery->joinTable ) . " AS $subQuery->alias";
// If the alias is the same as the table name and if there is a prefix, MediaWiki does not declare the unprefixed alias
$joinTable = $subQuery->joinTable === $subQuery->alias ? $this->connection->tableName( $subQuery->joinTable ) : $subQuery->joinTable;
if ( $subQuery->from ) {
$t = "($t $subQuery->from)";
$alias = 'nested' . $subQuery->alias;
$query->fromTables[$alias] = array_merge( (array)$subQuery->fromTables, [ $subQuery->alias => $joinTable ] );
$query->joinConditions = array_merge( (array)$query->joinConditions, (array)$subQuery->joinConditions );
} else {
$query->fromTables[$alias] = $joinTable;
}
$query->joinConditions[$alias] = [ $joinType . ' JOIN', "$joinField$op=" . $subQuery->joinfield ];
$this->fromTables[$subQuery->alias] = $joinTable;
ksort( $this->fromTables );
$this->joinConditions[$subQuery->alias] = [ $joinType . ' JOIN', "$joinField$op=" . $subQuery->joinfield ];
$query->from .= " $joinType JOIN $t ON $joinField$op=" . $subQuery->joinfield;
if ( $joinType === 'LEFT' ) {
$query->where .= ( ( $query->where === '' ) ? '' : ' AND ' ) . '(' . $subQuery->joinfield . ' IS NULL)';
}
} elseif ( $subQuery->joinfield !== '' ) { // Require joinfield as "value" via WHERE.
$condition = '';
if ( $subQuery->null === true ) {
$condition .= ( $condition ? ' OR ' : '' ) . "$joinField IS NULL";
} else {
foreach ( $subQuery->joinfield as $value ) {
$op = $subQuery->not ? '!' : '';
$condition .= ( $condition ? ' OR ' : '' ) . "$joinField$op=" . $this->connection->addQuotes( $value );
}
}
if ( count( $subQuery->joinfield ) > 1 ) {
$condition = "($condition)";
}
$query->where .= ( ( $query->where === '' || $subQuery->where === null ) ? '' : ' AND ' ) . $condition;
$query->from .= $subQuery->from;
$query->fromTables = array_merge( (array)$query->fromTables, (array)$subQuery->fromTables );
$query->joinConditions = array_merge( (array)$query->joinConditions, (array)$subQuery->joinConditions );
} else { // interpret empty joinfields as impossible condition (empty result)
$query->joinfield = ''; // make whole query false
$query->joinTable = '';
$query->where = '';
$query->from = '';
$query->fromTables = [];
$query->joinConditions = [];
break;
}
if ( $subQuery->where !== '' && $subQuery->where !== null ) {
if ( $subQuery->joinType === 'LEFT' || $subQuery->joinType == 'LEFT OUTER' ) {
$query->from .= ' AND (' . $subQuery->where . ')';
$query->joinConditions[$alias][1] .= ' AND (' . $subQuery->where . ')';
} else {
$query->where .= ( ( $query->where === '' ) ? '' : ' AND ' ) . '(' . $subQuery->where . ')';
}
}
}
$query->components = [];
}
private function conjunction( QuerySegment &$query ) {
reset( $query->components );
$key = false;
// Pick one subquery as anchor point ...
foreach ( $query->components as $qkey => $qid ) {
$key = $qkey;
if ( $this->querySegmentList[$qkey]->joinTable !== '' ) {
break;
}
}
$result = $this->querySegmentList[$key];
unset( $query->components[$key] );
// Execute it first (may change jointable and joinfield, e.g. when
// making temporary tables)
$this->segment( $result );
// ... and append to this query the remaining queries.
foreach ( $query->components as $qid => $joinfield ) {
$result->components[$qid] = $result->joinfield;
}
// Second execute, now incorporating remaining conditions.
$this->segment( $result );
$query = $result;
}
private function disjunction( QuerySegment &$query ) {
if ( $this->queryMode !== Query::MODE_NONE ) {
$this->temporaryTableBuilder->create( $this->connection->tableName( $query->alias ) );
}
$this->executedQueries[$query->alias] = [];
foreach ( $query->components as $qid => $joinField ) {
$subQuery = $this->querySegmentList[$qid];
$this->segment( $subQuery );
$sql = '';
if ( $subQuery->joinTable !== '' ) {
$sql = 'INSERT ' . 'IGNORE ' . 'INTO ' .
$this->connection->tableName( $query->alias ) .
" SELECT DISTINCT $subQuery->joinfield FROM " . $this->connection->tableName( $subQuery->joinTable ) .
" AS $subQuery->alias $subQuery->from" . ( $subQuery->where ? " WHERE $subQuery->where" : '' );
} elseif ( $subQuery->joinfield !== '' ) {
// NOTE: this works only for single "unconditional" values without further
// WHERE or FROM. The execution must take care of not creating any others.
$values = '';
// This produces an error on postgres with
// pg_query(): Query failed: ERROR: duplicate key value violates
// unique constraint "sunittest_t3_pkey" DETAIL: Key (id)=(274) already exists.
foreach ( $subQuery->joinfield as $value ) {
$values .= ( $values ? ',' : '' ) . '(' . $this->connection->addQuotes( $value ) . ')';
}
$sql = 'INSERT ' . 'IGNORE ' . 'INTO ' . $this->connection->tableName( $query->alias ) . " (id) VALUES $values";
} // else: // interpret empty joinfields as impossible condition (empty result), ignore
if ( $sql ) {
$this->executedQueries[$query->alias][] = $sql;
if ( $this->queryMode !== Query::MODE_NONE ) {
$this->connection->query(
$sql,
__METHOD__,
ISQLPlatform::QUERY_CHANGE_ROWS
);
}
}
}
$query->type = QuerySegment::Q_TABLE;
$query->where = '';
$query->components = [];
$query->joinTable = $query->alias;
$query->joinfield = "$query->alias.id";
$query->sortfields = []; // Make sure we got no sortfields.
// TODO: currently this eliminates sortkeys, possibly keep them (needs
// different temp table format though, maybe not such a good thing to do)
}
/**
* Find subproperties or subcategories. This may require iterative computation,
* and temporary tables are used in many cases.
*
* @param QuerySegment &$query
*/
private function hierarchy( QuerySegment &$query ) {
switch ( $query->type ) {
case QuerySegment::Q_PROP_HIERARCHY:
$type = 'property';
break;
case QuerySegment::Q_CLASS_HIERARCHY:
$type = 'class';
break;
}
[ $smwtable, $depth ] = $this->hierarchyTempTableBuilder->getTableDefinitionByType(
$type
);
// An individual depth was annotated as part of the query
if ( $query->depth !== null ) {
$depth = $query->depth;
}
if ( $depth <= 0 ) { // treat as value, no recursion
$query->type = QuerySegment::Q_VALUE;
return;
}
$values = '';
$valuecond = '';
foreach ( $query->joinfield as $value ) {
$values .= ( $values ? ',' : '' ) . '(' . $this->connection->addQuotes( $value ) . ')';
$valuecond .= ( $valuecond ? ' OR ' : '' ) . 'o_id=' . $this->connection->addQuotes( $value );
}
// Try to safe time (SELECT is cheaper than creating/dropping 3 temp tables):
$res = $this->connection->select(
$smwtable,
's_id',
$valuecond,
__METHOD__,
[ 'LIMIT' => 1 ]
);
if ( !$res->fetchObject() ) { // no subobjects, we are done!
$res->free();
$query->type = QuerySegment::Q_VALUE;
return;
}
$res->free();
$tablename = $this->connection->tableName( $query->alias );
$this->executedQueries[$query->alias] = [
"Recursively computed hierarchy for element(s) $values.",
"SELECT s_id FROM $smwtable WHERE $valuecond LIMIT 1"
];
$query->joinTable = $query->alias;
$query->joinfield = "$query->alias.id";
$this->hierarchyTempTableBuilder->fillTempTable(
$type,
$tablename,
$values,
$depth
);
}
/**
* After querying, make sure no temporary database tables are left.
*
* @todo I might be better to keep the tables and possibly reuse them later
* on. Being temporary, the tables will vanish with the session anyway.
*/
public function cleanUp() {
foreach ( $this->executedQueries as $table => $log ) {
$this->temporaryTableBuilder->drop( $this->connection->tableName( $table ) );
}
}
}