-
Notifications
You must be signed in to change notification settings - Fork 1
/
rdfdb.query_virtuoso.inc
133 lines (117 loc) · 3.92 KB
/
rdfdb.query_virtuoso.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
<?php
/**
* @file
* Query code for Virtuoso engine.
*/
class RdfdbInsertDataQuery_virtuoso extends RdfdbInsertDataQuery {
/**
* Executes the insert data query.
*
* @return
* The number of inserted triples, or FALSE is there were errors.
*/
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->update_query($query, $this->queryOptions);
}
public function toString() {
// Serializes prefixes in prologue if they are given. Otherwise ARC2 will
// use the prefixes which were defined during the remote endpoint setup.
$prologue = $this->preparePrefixes();
//return 'SELECT * WHERE { GRAPH ?g { ?s ?p ?o . } } LIMIT 10';
if (!empty($this->graph)) {
// ARC2 requires a graph no matter what.
return $prologue . 'INSERT INTO <' . $this->graph . '> { ' . $this->triples . ' }';
}
else {
return $prologue . 'INSERT INTO <http://example.org/default_graph> { ' . $this->triples . ' }';
}
}
/**
* Generic preparation and validation for an INSERT query.
*
* @return
* TRUE if the validation was successful, FALSE if not.
*/
public function preExecute() {
return TRUE;
}
}
class RdfdbDeleteDataQuery_virtuoso extends RdfdbDeleteDataQuery {
/**
* Executes the delete data query.
*
* @return
* The number of deleted triples, or FALSE is there were errors.
*/
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->update_query($query, $this->queryOptions);
}
public function toString() {
// Serializes prefixes in prologue if they are given. Otherwise ARC2 will
// use the prefixes which were defined during the remote endpoint setup.
$prologue = $this->preparePrefixes();
//return 'SELECT * WHERE { GRAPH ?g { ?s ?p ?o . } } LIMIT 10';
if (!empty($this->graph)) {
return $prologue . 'DELETE FROM <' . $this->graph . '> { ' . $this->triples . ' }';
}
else {
return $prologue . 'DELETE { ' . $this->triples . ' }';
}
}
/**
* Generic preparation and validation for an INSERT query.
*
* @return
* TRUE if the validation was successful, FALSE if not.
*/
public function preExecute() {
return TRUE;
}
}
class RdfdbClearQuery_virtuoso extends RdfdbClearQuery {
// This method only exist because of the zombie triples bug in ARC2
// @todo remove once this bug has been fixed.
public function execute() {
// If validation fails, simply return NULL.
// Note that validation routines in preExecute() may throw exceptions instead.
if (!$this->preExecute()) {
return NULL;
}
// The zombie bug in ARC2 causes the triples to reappear after attempting
// to clear the whole store with DELETE { ?s ?p ?o . } WHERE { ?s ?p ?o . }
if (empty($this->graph)) {
$rs = rdfdb_select('DISTINCT ?g')->where('GRAPH ?g { ?s ?p ?o . } ')->execute();
foreach ($rs['result']['rows'] as $row) {
rdfdb_clear($row['g'])->execute();
}
return;
}
$query = $this->toString();
//var_dump($query);
return $this->connection->update_query($query, $this->queryOptions);
}
public function toString() {
// Serializes prefixes in prologue if they are given. Otherwise ARC2 will
// use the prefixes which were defined during the remote endpoint setup.
$prologue = $this->preparePrefixes();
if (!empty($this->graph)) {
return $prologue . ' CLEAR GRAPH <' . $this->graph . '>';
}
else {
throw new Exception('The graph to clear must be provided');
}
}
}