1<?php
2/**
3 * DokuWiki Plugin stratastorage (Helper Component)
4 *
5 * @license GPL 2 http://www.gnu.org/licenses/gpl-2.0.html
6 * @author  Brend Wanders <b.wanders@utwente.nl>
7 */
8
9// must be run within Dokuwiki
10if (!defined('DOKU_INC')) die('Meh.');
11
12class strata_querytree_visitor {
13    /**
14     * Visits a triple pattern.
15     */
16    function visit_tp(&$tp) {
17    }
18
19    /**
20     * Visit a filter pattern.
21     */
22    function visit_fp(&$fp) {
23    }
24
25    /**
26     * Visit an optional operation.
27     */
28    function visit_opt(&$query) {
29        $this->dispatch($query['lhs']);
30        $this->dispatch($query['rhs']);
31    }
32
33    /**
34     * Visit an and operation.
35     */
36    function visit_and(&$query) {
37        $this->dispatch($query['lhs']);
38        $this->dispatch($query['rhs']);
39    }
40
41    /**
42     * Visit a filter operation.
43     */
44    function visit_filter(&$query) {
45        $this->dispatch($query['lhs']);
46        foreach($query['rhs'] as &$filter) {
47            $this->visit_fp($filter);
48        }
49    }
50
51    /**
52     * Visit minus operation.
53     */
54    function visit_minus(&$query) {
55        $this->dispatch($query['lhs']);
56        $this->dispatch($query['rhs']);
57
58    }
59
60    /**
61     * Visit union operation.
62     */
63    function visit_union(&$query) {
64        $this->dispatch($query['lhs']);
65        $this->dispatch($query['rhs']);
66
67    }
68
69    /**
70     * Visit projection and ordering.
71     */
72    function visit_select(&$query) {
73        $this->dispatch($query['group']);
74    }
75
76    function dispatch(&$query) {
77        switch($query['type']) {
78            case 'select':
79                return $this->visit_select($query);
80            case 'union':
81                return $this->visit_union($query);
82            case 'minus':
83                return $this->visit_minus($query);
84            case 'optional':
85                return $this->visit_opt($query);
86            case 'filter':
87                return $this->visit_filter($query);
88            case 'triple':
89                return $this->visit_tp($query);
90            case 'and':
91                return $this->visit_and($query);
92            default:
93        }
94    }
95
96    /**
97     * Visits an abstract query tree to SQL.
98     */
99    function visit(&$query) {
100        $this->dispatch($query);
101    }
102}
103