comparison sites/all/libraries/ARC2/arc/parsers/ARC2_SPARQLPlusParser.php @ 4:ce11bbd8f642

added modules
author danieleb <danielebarchiesi@me.com>
date Thu, 19 Sep 2013 10:38:44 +0100
parents
children
comparison
equal deleted inserted replaced
3:b28be78d8160 4:ce11bbd8f642
1 <?php
2 /*
3 homepage: http://arc.semsol.org/
4 license: http://arc.semsol.org/license
5
6 class: ARC2 SPARQL+ Parser (SPARQL + Aggregates + LOAD + INSERT + DELETE)
7 author: Benjamin Nowack
8 version: 2010-11-16
9 */
10
11 ARC2::inc('SPARQLParser');
12
13 class ARC2_SPARQLPlusParser extends ARC2_SPARQLParser {
14
15 function __construct($a, &$caller) {
16 parent::__construct($a, $caller);
17 }
18
19 function __init() {
20 parent::__init();
21 }
22
23 /* +1 */
24
25 function xQuery($v) {
26 list($r, $v) = $this->xPrologue($v);
27 foreach (array('Select', 'Construct', 'Describe', 'Ask', 'Insert', 'Delete', 'Load') as $type) {
28 $m = 'x' . $type . 'Query';
29 if ((list($r, $v) = $this->$m($v)) && $r) {
30 return array($r, $v);
31 }
32 }
33 return array(0, $v);
34 }
35
36 /* +3 */
37
38 function xResultVar($v) {
39 $aggregate = '';
40 /* aggregate */
41 if ($sub_r = $this->x('\(?(AVG|COUNT|MAX|MIN|SUM)\s*\(\s*([^\)]+)\)\s+AS\s+([^\s\)]+)\)?', $v)) {
42 $aggregate = $sub_r[1];
43 $result_var = $sub_r[3];
44 $v = $sub_r[2] . $sub_r[4];
45 }
46 if ($sub_r && (list($sub_r, $sub_v) = $this->xVar($result_var)) && $sub_r) {
47 $result_var = $sub_r['value'];
48 }
49 /* * or var */
50 if ((list($sub_r, $sub_v) = $this->x('\*', $v)) && $sub_r) {
51 return array(array('var' => $sub_r['value'], 'aggregate' => $aggregate, 'alias' => $aggregate ? $result_var : ''), $sub_v);
52 }
53 if ((list($sub_r, $sub_v) = $this->xVar($v)) && $sub_r) {
54 return array(array('var' => $sub_r['value'], 'aggregate' => $aggregate, 'alias' => $aggregate ? $result_var : ''), $sub_v);
55 }
56 return array(0, $v);
57 }
58
59 /* +4 */
60
61 function xLoadQuery($v) {
62 if ($sub_r = $this->x('LOAD\s+', $v)) {
63 $sub_v = $sub_r[1];
64 if ((list($sub_r, $sub_v) = $this->xIRIref($sub_v)) && $sub_r) {
65 $r = array('type' => 'load', 'url' => $sub_r, 'target_graph' => '');
66 if ($sub_r = $this->x('INTO\s+', $sub_v)) {
67 $sub_v = $sub_r[1];
68 if ((list($sub_r, $sub_v) = $this->xIRIref($sub_v)) && $sub_r) {
69 $r['target_graph'] = $sub_r;
70 }
71 }
72 return array($r, $sub_v);
73 }
74 }
75 return array(0, $v);
76 }
77
78 /* +5 */
79
80 function xInsertQuery($v) {
81 if ($sub_r = $this->x('INSERT\s+', $v)) {
82 $r = array(
83 'type' => 'insert',
84 'dataset' => array(),
85 );
86 $sub_v = $sub_r[1];
87 /* target */
88 if ($sub_r = $this->x('INTO\s+', $sub_v)) {
89 $sub_v = $sub_r[1];
90 if ((list($sub_r, $sub_v) = $this->xIRIref($sub_v)) && $sub_r) {
91 $r['target_graph'] = $sub_r;
92 /* CONSTRUCT keyword, optional */
93 if ($sub_r = $this->x('CONSTRUCT\s+', $sub_v)) {
94 $sub_v = $sub_r[1];
95 }
96 /* construct template */
97 if ((list($sub_r, $sub_v) = $this->xConstructTemplate($sub_v)) && is_array($sub_r)) {
98 $r['construct_triples'] = $sub_r;
99 }
100 else {
101 $this->addError('Construct Template not found');
102 return array(0, $v);
103 }
104 /* dataset */
105 while ((list($sub_r, $sub_v) = $this->xDatasetClause($sub_v)) && $sub_r) {
106 $r['dataset'][] = $sub_r;
107 }
108 /* where */
109 if ((list($sub_r, $sub_v) = $this->xWhereClause($sub_v)) && $sub_r) {
110 $r['pattern'] = $sub_r;
111 }
112 /* solution modifier */
113 if ((list($sub_r, $sub_v) = $this->xSolutionModifier($sub_v)) && $sub_r) {
114 $r = array_merge($r, $sub_r);
115 }
116 return array($r, $sub_v);
117 }
118 }
119 }
120 return array(0, $v);
121 }
122
123 /* +6 */
124
125 function xDeleteQuery($v) {
126 if ($sub_r = $this->x('DELETE\s+', $v)) {
127 $r = array(
128 'type' => 'delete',
129 'target_graphs' => array()
130 );
131 $sub_v = $sub_r[1];
132 /* target */
133 do {
134 $proceed = false;
135 if ($sub_r = $this->x('FROM\s+', $sub_v)) {
136 $sub_v = $sub_r[1];
137 if ((list($sub_r, $sub_v) = $this->xIRIref($sub_v)) && $sub_r) {
138 $r['target_graphs'][] = $sub_r;
139 $proceed = 1;
140 }
141 }
142 } while ($proceed);
143 /* CONSTRUCT keyword, optional */
144 if ($sub_r = $this->x('CONSTRUCT\s+', $sub_v)) {
145 $sub_v = $sub_r[1];
146 }
147 /* construct template */
148 if ((list($sub_r, $sub_v) = $this->xConstructTemplate($sub_v)) && is_array($sub_r)) {
149 $r['construct_triples'] = $sub_r;
150 /* dataset */
151 while ((list($sub_r, $sub_v) = $this->xDatasetClause($sub_v)) && $sub_r) {
152 $r['dataset'][] = $sub_r;
153 }
154 /* where */
155 if ((list($sub_r, $sub_v) = $this->xWhereClause($sub_v)) && $sub_r) {
156 $r['pattern'] = $sub_r;
157 }
158 /* solution modifier */
159 if ((list($sub_r, $sub_v) = $this->xSolutionModifier($sub_v)) && $sub_r) {
160 $r = array_merge($r, $sub_r);
161 }
162 }
163 return array($r, $sub_v);
164 }
165 return array(0, $v);
166 }
167
168 /* +7 */
169
170 function xSolutionModifier($v) {
171 $r = array();
172 if ((list($sub_r, $sub_v) = $this->xGroupClause($v)) && $sub_r) {
173 $r['group_infos'] = $sub_r;
174 }
175 if ((list($sub_r, $sub_v) = $this->xOrderClause($sub_v)) && $sub_r) {
176 $r['order_infos'] = $sub_r;
177 }
178 while ((list($sub_r, $sub_v) = $this->xLimitOrOffsetClause($sub_v)) && $sub_r) {
179 $r = array_merge($r, $sub_r);
180 }
181 return ($v == $sub_v) ? array(0, $v) : array($r, $sub_v);
182 }
183
184 /* +8 */
185
186 function xGroupClause($v) {
187 if ($sub_r = $this->x('GROUP BY\s+', $v)) {
188 $sub_v = $sub_r[1];
189 $r = array();
190 do {
191 $proceed = 0;
192 if ((list($sub_r, $sub_v) = $this->xVar($sub_v)) && $sub_r) {
193 $r[] = $sub_r;
194 $proceed = 1;
195 if ($sub_r = $this->x('\,', $sub_v)) {
196 $sub_v = $sub_r[1];
197 }
198 }
199 } while ($proceed);
200 if (count($r)) {
201 return array($r, $sub_v);
202 }
203 else {
204 $this->addError('No columns specified in GROUP BY clause.');
205 }
206 }
207 return array(0, $v);
208 }
209
210 }