3 homepage: http://arc.semsol.org/
4 license: http://arc.semsol.org/license
6 class: ARC2 RDF Store CONSTRUCT Query Handler
7 author: Benjamin Nowack
8 version: 2008-02-11 (Fix: auto-adding DISTINCT to avoid unnecessary duplicates)
11 ARC2::inc('StoreSelectQueryHandler');
13 class ARC2_StoreConstructQueryHandler extends ARC2_StoreSelectQueryHandler {
15 function __construct($a = '', &$caller) {/* caller has to be a store */
16 parent::__construct($a, $caller);
19 function ARC2_StoreConstructQueryHandler($a = '', &$caller) {
20 $this->__construct($a, $caller);
23 function __init() {/* db_con */
25 $this->store =& $this->caller;
30 function runQuery($infos) {
31 $this->infos = $infos;
32 $this->buildResultVars();
33 $this->infos['query']['distinct'] = 1;
34 $sub_r = parent::runQuery($this->infos);
35 $rf = $this->v('result_format', '', $infos);
36 if (in_array($rf, array('sql', 'structure', 'index'))) {
39 return $this->getResultIndex($sub_r);
44 function buildResultVars() {
46 foreach ($this->infos['query']['construct_triples'] as $t) {
47 foreach (array('s', 'p', 'o') as $term) {
48 if ($t[$term . '_type'] == 'var') {
49 if (!in_array($t[$term], $r)) {
50 $r[] = array('var' => $t[$term], 'aggregate' => '', 'alias' => '');
55 $this->infos['query']['result_vars'] = $r;
60 function getResultIndex($qr) {
63 $rows = $this->v('rows', array(), $qr);
64 $cts = $this->infos['query']['construct_triples'];
66 foreach ($rows as $row) {
68 foreach ($cts as $ct) {
71 foreach (array('s', 'p', 'o') as $term) {
73 $type = $ct[$term . '_type'];
74 $val = ($type == 'bnode') ? $val . $bnc : $val;
76 $skip_t = !isset($row[$val]) ? 1 : $skip_t;
77 $type = !$skip_t ? $row[$val . ' type'] : '';
78 $val = (!$skip_t) ? $row[$val] : '';
81 $t[$term . '_type'] = $type;
82 if (isset($row[$term . ' lang'])) {
83 $t[$term . '_lang'] = $row[$term . ' lang'];
85 if (isset($row[$term . ' datatype'])) {
86 $t[$term . '_datatype'] = $row[$term . ' datatype'];
96 if (!isset($r[$s][$p])) {
99 $o = array('value' => $o);
100 foreach (array('lang', 'type', 'datatype') as $suffix) {
101 if (isset($t['o_' . $suffix]) && $t['o_' . $suffix]) {
102 $o[$suffix] = $t['o_' . $suffix];
105 if (!isset($added[md5($s . ' ' . $p . ' ' . serialize($o))])) {
107 $added[md5($s . ' ' . $p . ' ' . serialize($o))] = 1;