File: ARC2_StoreConstructQueryHandler.php

package info (click to toggle)
php-arc 2~20100706-1
  • links: PTS, VCS
  • area: main
  • in suites: squeeze
  • size: 696 kB
  • ctags: 3,493
  • sloc: php: 12,283; makefile: 31
file content (119 lines) | stat: -rw-r--r-- 3,238 bytes parent folder | download | duplicates (5)
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
<?php
/*
homepage: http://arc.semsol.org/
license:  http://arc.semsol.org/license

class:    ARC2 RDF Store CONSTRUCT Query Handler
author:   Benjamin Nowack
version:  2008-02-11 (Fix: auto-adding DISTINCT to avoid unnecessary duplicates)
*/

ARC2::inc('StoreSelectQueryHandler');

class ARC2_StoreConstructQueryHandler extends ARC2_StoreSelectQueryHandler {

  function __construct($a = '', &$caller) {/* caller has to be a store */
    parent::__construct($a, $caller);
  }
  
  function ARC2_StoreConstructQueryHandler($a = '', &$caller) {
    $this->__construct($a, $caller);
  }

  function __init() {/* db_con */
    parent::__init();
    $this->store =& $this->caller;
  }

  /*  */
  
  function runQuery($infos) {
    $this->infos = $infos;
    $this->buildResultVars();
    $this->infos['query']['distinct'] = 1;
    $sub_r = parent::runQuery($this->infos);
    $rf = $this->v('result_format', '', $infos);
    if (in_array($rf, array('sql', 'structure', 'index'))) {
      return $sub_r;
    }
    return $this->getResultIndex($sub_r);
  }
  
  /*  */
  
  function buildResultVars() {
    $r = array();
    foreach ($this->infos['query']['construct_triples'] as $t) {
      foreach (array('s', 'p', 'o') as $term) {
        if ($t[$term . '_type'] == 'var') {
          if (!in_array($t[$term], $r)) {
            $r[] = array('var' => $t[$term], 'aggregate' => '', 'alias' => '');
          }
        }
      }
    }
    $this->infos['query']['result_vars'] = $r;
  }

  /*  */

  function getResultIndex($qr) {
    $r = array();
    $added = array();
    $rows = $this->v('rows', array(), $qr);
    $cts = $this->infos['query']['construct_triples'];
    $bnc = 0;
    foreach ($rows as $row) {
      $bnc++;
      foreach ($cts as $ct) {
        $skip_t = 0;
        $t = array();
        foreach (array('s', 'p', 'o') as $term) {
          $val = $ct[$term];
          $type = $ct[$term . '_type'];
          $val = ($type == 'bnode') ? $val . $bnc : $val;
          if ($type == 'var') {
            $skip_t = !isset($row[$val]) ? 1 : $skip_t;
            $type = !$skip_t ? $row[$val . ' type'] : '';
            $val = (!$skip_t) ? $row[$val] : '';
          }
          $t[$term] = $val;
          $t[$term . '_type'] = $type;
          if (isset($row[$term . ' lang'])) {
            $t[$term . '_lang'] = $row[$term . ' lang'];
          }
          if (isset($row[$term . ' datatype'])) {
            $t[$term . '_datatype'] = $row[$term . ' datatype'];
          }
        }
        if (!$skip_t) {
          $s = $t['s'];
          $p = $t['p'];
          $o = $t['o'];
          if (!isset($r[$s])) {
            $r[$s] = array();
          }
          if (!isset($r[$s][$p])) {
            $r[$s][$p] = array();
          }
          $o = array('value' => $o);
          foreach (array('lang', 'type', 'datatype') as $suffix) {
            if (isset($t['o_' . $suffix]) && $t['o_' . $suffix]) {
              $o[$suffix] = $t['o_' . $suffix];
            }
          }
          if (!isset($added[md5($s . ' ' . $p . ' ' . serialize($o))])) {
            $r[$s][$p][] = $o;
            $added[md5($s . ' ' . $p . ' ' . serialize($o))] = 1;
          }
        }
      }
    }
    return $r;
  }
  
  /*  */

}