|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* This file is part of the sweetrdf/InMemoryStoreSqlite package and licensed under |
|
5
|
|
|
* the terms of the GPL-3 license. |
|
6
|
|
|
* |
|
7
|
|
|
* (c) Konrad Abicht <[email protected]> |
|
8
|
|
|
* (c) Benjamin Nowack |
|
9
|
|
|
* |
|
10
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
11
|
|
|
* file that was distributed with this source code. |
|
12
|
|
|
*/ |
|
13
|
|
|
|
|
14
|
|
|
namespace sweetrdf\InMemoryStoreSqlite\Store\QueryHandler; |
|
15
|
|
|
|
|
16
|
|
|
class ConstructQueryHandler extends SelectQueryHandler |
|
17
|
|
|
{ |
|
18
|
5 |
|
public function runQuery($infos) |
|
19
|
|
|
{ |
|
20
|
5 |
|
$this->infos = $infos; |
|
|
|
|
|
|
21
|
5 |
|
$this->buildResultVars(); |
|
22
|
5 |
|
$this->infos['query']['distinct'] = 1; |
|
23
|
5 |
|
$sub_r = parent::runQuery($this->infos); |
|
24
|
5 |
|
$rf = $this->v('result_format', '', $infos); |
|
25
|
5 |
|
if (\in_array($rf, ['sql', 'structure', 'index'])) { |
|
26
|
|
|
return $sub_r; |
|
27
|
|
|
} |
|
28
|
|
|
|
|
29
|
5 |
|
return $this->getResultIndex($sub_r); |
|
30
|
|
|
} |
|
31
|
|
|
|
|
32
|
5 |
|
public function buildResultVars() |
|
33
|
|
|
{ |
|
34
|
5 |
|
$r = []; |
|
35
|
5 |
|
foreach ($this->infos['query']['construct_triples'] as $t) { |
|
36
|
5 |
|
foreach (['s', 'p', 'o'] as $term) { |
|
37
|
5 |
|
if ('var' == $t[$term.'_type']) { |
|
38
|
3 |
|
if (!\in_array($t[$term], $r)) { |
|
39
|
3 |
|
$r[] = ['var' => $t[$term], 'aggregate' => '', 'alias' => '']; |
|
40
|
|
|
} |
|
41
|
|
|
} |
|
42
|
|
|
} |
|
43
|
|
|
} |
|
44
|
5 |
|
$this->infos['query']['result_vars'] = $r; |
|
45
|
5 |
|
} |
|
46
|
|
|
|
|
47
|
5 |
|
public function getResultIndex($qr) |
|
48
|
|
|
{ |
|
49
|
5 |
|
$r = []; |
|
50
|
5 |
|
$added = []; |
|
51
|
5 |
|
$rows = $this->v('rows', [], $qr); |
|
52
|
5 |
|
$cts = $this->infos['query']['construct_triples']; |
|
53
|
5 |
|
$bnc = 0; |
|
54
|
5 |
|
foreach ($rows as $row) { |
|
55
|
5 |
|
++$bnc; |
|
56
|
5 |
|
foreach ($cts as $ct) { |
|
57
|
5 |
|
$skip_t = 0; |
|
58
|
5 |
|
$t = []; |
|
59
|
5 |
|
foreach (['s', 'p', 'o'] as $term) { |
|
60
|
5 |
|
$val = $ct[$term]; |
|
61
|
5 |
|
$type = $ct[$term.'_type']; |
|
62
|
5 |
|
$val = ('bnode' == $type) ? $val.$bnc : $val; |
|
63
|
5 |
|
if ('var' == $type) { |
|
64
|
3 |
|
$skip_t = !isset($row[$val]) ? 1 : $skip_t; |
|
65
|
3 |
|
$type = !$skip_t ? $row[$val.' type'] : ''; |
|
66
|
3 |
|
$val = (!$skip_t) ? $row[$val] : ''; |
|
67
|
|
|
} |
|
68
|
5 |
|
$t[$term] = $val; |
|
69
|
5 |
|
$t[$term.'_type'] = $type; |
|
70
|
5 |
|
if (isset($row[$ct[$term].' lang'])) { |
|
71
|
|
|
$t[$term.'_lang'] = $row[$ct[$term].' lang']; |
|
72
|
|
|
} |
|
73
|
5 |
|
if (isset($row[$ct[$term].' datatype'])) { |
|
74
|
|
|
$t[$term.'_datatype'] = $row[$ct[$term].' datatype']; |
|
75
|
|
|
} |
|
76
|
|
|
} |
|
77
|
5 |
|
if (!$skip_t) { |
|
78
|
5 |
|
$s = $t['s']; |
|
79
|
5 |
|
$p = $t['p']; |
|
80
|
5 |
|
$o = $t['o']; |
|
81
|
5 |
|
if (!isset($r[$s])) { |
|
82
|
5 |
|
$r[$s] = []; |
|
83
|
|
|
} |
|
84
|
5 |
|
if (!isset($r[$s][$p])) { |
|
85
|
5 |
|
$r[$s][$p] = []; |
|
86
|
|
|
} |
|
87
|
5 |
|
$o = ['value' => $o]; |
|
88
|
5 |
|
foreach (['lang', 'type', 'datatype'] as $suffix) { |
|
89
|
5 |
|
if (isset($t['o_'.$suffix]) && $t['o_'.$suffix]) { |
|
90
|
5 |
|
$o[$suffix] = $t['o_'.$suffix]; |
|
91
|
|
|
} |
|
92
|
|
|
} |
|
93
|
5 |
|
if (!isset($added[md5($s.' '.$p.' '.serialize($o))])) { |
|
94
|
5 |
|
$r[$s][$p][] = $o; |
|
95
|
5 |
|
$added[md5($s.' '.$p.' '.serialize($o))] = 1; |
|
96
|
|
|
} |
|
97
|
|
|
} |
|
98
|
|
|
} |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
5 |
|
return $r; |
|
102
|
|
|
} |
|
103
|
|
|
} |
|
104
|
|
|
|