-
Notifications
You must be signed in to change notification settings - Fork 1
/
rdfdb.query.inc
480 lines (413 loc) · 11.6 KB
/
rdfdb.query.inc
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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
<?php
/**
* @file
* Non-specific Database query code. Used by all engines.
*/
/**
* Interface for a query that can be manipulated via an alter hook.
*/
interface RdfdbQueryAlterableInterface {
/**
* Adds a tag to a query.
*
* Tags are strings that identify a query. A query may have any number of
* tags. Tags are used to mark a query so that alter hooks may decide if they
* wish to take action. Tags should be all lower-case and contain only letters,
* numbers, and underscore, and start with a letter. That is, they should
* follow the same rules as PHP identifiers in general.
*
* @param $tag
* The tag to add.
* @return QueryAlterableInterface
* The called object.
*/
public function addTag($tag);
/**
* Determines if a given query has a given tag.
*
* @param $tag
* The tag to check.
* @return
* TRUE if this query has been marked with this tag, FALSE otherwise.
*/
public function hasTag($tag);
/**
* Determines if a given query has all specified tags.
*
* @param $tags
* A variable number of arguments, one for each tag to check.
* @return
* TRUE if this query has been marked with all specified tags, FALSE otherwise.
*/
public function hasAllTags();
/**
* Determines if a given query has any specified tag.
*
* @param $tags
* A variable number of arguments, one for each tag to check.
* @return
* TRUE if this query has been marked with at least one of the specified
* tags, FALSE otherwise.
*/
public function hasAnyTag();
/**
* Adds additional metadata to the query.
*
* Often, a query may need to provide additional contextual data to alter
* hooks. Alter hooks may then use that information to decide if and how
* to take action.
*
* @param $key
* The unique identifier for this piece of metadata. Must be a string that
* follows the same rules as any other PHP identifier.
* @param $object
* The additional data to add to the query. May be any valid PHP variable.
* @return QueryAlterableInterface
* The called object.
*/
public function addMetaData($key, $object);
/**
* Retrieves a given piece of metadata.
*
* @param $key
* The unique identifier for the piece of metadata to retrieve.
* @return
* The previously attached metadata object, or NULL if one doesn't exist.
*/
public function getMetaData($key);
}
/**
* Base class for the query builders.
*
* All query builders inherit from a common base class.
*/
abstract class RdfdbQuery implements RdfdbQueryAlterableInterface {
/**
* The connection object on which to run this query.
*
* @var DatabaseConnection
*/
protected $connection;
/**
* The query options to pass on to the connection object.
*
* @var array
*/
protected $queryOptions;
public function __construct(RdfdbConnection $connection, $options) {
$this->connection = $connection;
$this->queryOptions = $options;
//var_dump($options);
}
/**
* Run the query against the database.
*/
abstract protected function execute();
public function preparePrefixes() {
$prefixes = '';
if (!empty($this->queryOptions['namespaces'])) {
foreach ($this->queryOptions['namespaces'] as $p => $ns) {
$prefixes .= 'PREFIX ' . $p . ': <' . $ns . ">\n";
}
}
return $prefixes;
}
/**
* Generic preparation and validation for a query.
*
* @return
* TRUE if the validation was successful, FALSE if not.
*/
public function preExecute() {
// @todo
return TRUE;
}
/* Implementations of QueryAlterableInterface. */
public function addTag($tag) {
$this->query->addTag($tag);
return $this;
}
public function hasTag($tag) {
return $this->query->hasTag($tag);
}
public function hasAllTags() {
return call_user_func_array(array($this->query, 'hasAllTags', func_get_args()));
}
public function hasAnyTag() {
return call_user_func_array(array($this->query, 'hasAnyTags', func_get_args()));
}
public function addMetaData($key, $object) {
$this->query->addMetaData($key, $object);
return $this;
}
public function getMetaData($key) {
return $this->query->getMetaData($key);
}
}
/**
* General class for a raw SPARQL query.
*/
class RdfdbSelectCustomQuery extends RdfdbQuery {
/**
* The SPARQL query.
*
* @query string
*/
protected $query;
public function __construct($connection, $query, array $options = array()) {
parent::__construct($connection, $options);
//var_dump($this);
$this->query = $query;
}
/**
* Executes the select query.
*/
public function execute() {
// If validation fails, simply return NULL.
// Note that validation routines in preExecute() may throw exceptions instead.
if (!$this->preExecute()) {
return NULL;
}
$query = $this->toString();
//var_dump($query);
return $this->connection->query($query, $this->queryOptions);
}
public function toString() {
return $this->query;
}
public function preparePrefixes() {
$prefixes = '';
if (!empty($this->queryOptions['namespaces'])) {
foreach ($this->queryOptions['namespaces'] as $p => $ns) {
$prefixes .= 'PREFIX ' . $p . ': <' . $ns . ">\n";
}
}
return $prefixes;
}
}
/**
* General class for an abstracted SELECT query.
*/
class RdfdbSelectQuery extends RdfdbQuery {
/**
* The variables of the SPARQL query.
*
* @var string
*/
protected $vars;
/**
* The Group Graph Patterns of the SPARQL query.
*
* @var array
*/
protected $ggps = array();
/**
* The limit of the SPARQL query.
*
* @var int
*/
protected $limit = 0;
public function __construct($connection, $vars, array $options = array()) {
parent::__construct($connection, $options);
//var_dump($this);
$this->vars = $vars;
}
public function where($snippet, $args = array()) {
// Add a Group Graph Pattern to the list of ggps.
$this->ggps[] = $snippet;
return $this;
}
public function limit($limit, $args = array()) {
$this->limit = $limit;
return $this;
}
/**
* Executes the select query.
*/
public function execute() {
// If validation fails, simply return NULL.
// Note that validation routines in preExecute() may throw exceptions instead.
if (!$this->preExecute()) {
return NULL;
}
$query = $this->toString();
//var_dump($query);
return $this->connection->query($query, $this->queryOptions);
}
public function toString() {
$prologue = $this->preparePrefixes();
$limit = $this->limit ? ' LIMIT ' . $this->limit : '';
if (count($this->ggps) > 1) {
$where = '{ ' . implode(" } \n { ", $this->ggps) . ' }';
}
else {
$where = $this->ggps[0];
}
return $prologue . "\n" . 'SELECT ' . $this->vars . ' WHERE { ' . $where . ' } ' . $limit;
}
public function preparePrefixes() {
$prefixes = '';
if (!empty($this->queryOptions['namespaces'])) {
foreach ($this->queryOptions['namespaces'] as $p => $ns) {
$prefixes .= 'PREFIX ' . $p . ': <' . $ns . ">\n";
}
}
return $prefixes;
}
}
/**
* General class for an abstracted INSERT DATA operation.
*/
class RdfdbInsertDataQuery extends RdfdbQuery {
/**
* The graph in which to insert the data.
*
* @var string
*/
protected $graph;
/**
* The triples to be inserted.
*
* @var string
*/
protected $triples;
public function __construct($connection, $graph, $triples, array $options = array()) {
parent::__construct($connection, $options);
$this->graph = $graph;
$this->triples = $triples;
}
/**
* Executes the insert data query.
*
* @return
* The last insert ID of the query, if one exists. If the query
* was given multiple sets of values to insert, the return value is
* undefined. If the query is flagged "delayed", then the insert ID
* won't be created until later when the query actually runs so the
* return value is also undefined. If no fields are specified, this
* method will do nothing and return NULL. That makes it safe to use
* in multi-insert loops.
*/
public function execute() {
// If validation fails, simply return NULL.
// Note that validation routines in preExecute() may throw exceptions instead.
if (!$this->preExecute()) {
return NULL;
}
$query = $this->toString();
return $this->connection->query($query, $this->queryOptions);
// return $this->toString();
}
public function toString() {
if (!empty($this->graph)) {
return 'INSERT DATA { GRAPH <' . $this->graph . '> { ' . $this->triples . ' } }';
}
else {
return 'INSERT DATA { ' . $this->triples . ' }';
}
}
public function preparePrefixes() {
$prefixes = '';
if (!empty($this->queryOptions['namespaces'])) {
foreach ($this->queryOptions['namespaces'] as $p => $ns) {
$prefixes .= 'PREFIX ' . $p . ': <' . $ns . ">\n";
}
}
return $prefixes;
}
}
/**
* General class for an abstracted DELETE DATA operation.
*/
class RdfdbDeleteDataQuery extends RdfdbQuery {
/**
* The graph in which to delete the data.
*
* @var string
*/
protected $graph;
/**
* The triples to be deleted.
*
* @var string
*/
protected $triples;
public function __construct($connection, $graph, $triples, array $options = array()) {
parent::__construct($connection, $options);
$this->graph = $graph;
$this->triples = $triples;
}
/**
* Executes the delete data query.
*/
public function execute() {
// If validation fails, simply return NULL.
// Note that validation routines in preExecute() may throw exceptions instead.
if (!$this->preExecute()) {
return NULL;
}
return $this->toString();
}
public function toString() {
if (!empty($this->graph)) {
return 'DELETE DATA { GRAPH <' . $this->graph . '> { ' . $this->triples . ' } }';
}
else {
return 'DELETE DATA { ' . $this->triples . ' }';
}
}
public function preparePrefixes() {
$prefixes = '';
if (!empty($this->queryOptions['namespaces'])) {
foreach ($this->queryOptions['namespaces'] as $p => $ns) {
$prefixes .= 'PREFIX ' . $p . ': <' . $ns . ">\n";
}
}
return $prefixes;
}
}
/**
* General class for an abstracted CLEAR operation.
*/
class RdfdbClearQuery extends RdfdbQuery {
/**
* The graph clear.
*
* @var string
*/
protected $graph;
public function __construct($connection, $graph, array $options = array()) {
parent::__construct($connection, $options);
$this->graph = $graph;
}
/**
* Executes the clear data query.
*
* @return
* The last delete ID of the query, if one exists. If the query
* was given multiple sets of values to delete, the return value is
* undefined. If the query is flagged "delayed", then the delete ID
* won't be created until later when the query actually runs so the
* return value is also undefined. If no fields are specified, this
* method will do nothing and return NULL. That makes it safe to use
* in multi-delete loops.
*/
public function execute() {
// If validation fails, simply return NULL.
// Note that validation routines in preExecute() may throw exceptions instead.
if (!$this->preExecute()) {
return NULL;
}
$query = $this->toString();
var_dump($query);
return $this->connection->query($query, $this->queryOptions);
}
public function toString() {
if (!empty($this->graph)) {
return 'CLEAR GRAPH <' . $this->graph . '>';
}
else {
return 'CLEAR GRAPH DEFAULT';
}
}
}