1 | <?php
|
---|
2 | require_once RDFAPI_INCLUDE_DIR . 'dataset/Dataset.php';
|
---|
3 | require_once RDFAPI_INCLUDE_DIR . 'model/DbModel.php';
|
---|
4 | require_once RDFAPI_INCLUDE_DIR . 'dataset/IteratorFindQuadsDb.php';
|
---|
5 | // ----------------------------------------------------------------------------------
|
---|
6 | // Class: DatasetDb
|
---|
7 | // ----------------------------------------------------------------------------------
|
---|
8 |
|
---|
9 | /**
|
---|
10 | * Persistent implementation of a Dataset in a database.
|
---|
11 | * A RDF dataset is a collection of named RDF graphs.
|
---|
12 | *
|
---|
13 | * @version $Id$
|
---|
14 | * @author Daniel Westphal (http://www.d-westphal.de)
|
---|
15 | * @author Chris Bizer <chris@bizer.de>
|
---|
16 | *
|
---|
17 | * @package dataset
|
---|
18 | * @access public
|
---|
19 | **/
|
---|
20 | require_once(RDFAPI_INCLUDE_DIR.PACKAGE_DBASE);
|
---|
21 |
|
---|
22 | class DatasetDb extends Dataset
|
---|
23 | {
|
---|
24 |
|
---|
25 | /**
|
---|
26 | * Reference to databse connection.
|
---|
27 | *
|
---|
28 | * @var resource dbConnection
|
---|
29 | * @access private
|
---|
30 | */
|
---|
31 | var $dbConnection;
|
---|
32 |
|
---|
33 | /**
|
---|
34 | * Reference to the dbStore Object.
|
---|
35 | *
|
---|
36 | * @var $dbStore dbStore
|
---|
37 | * @access private
|
---|
38 | */
|
---|
39 | var $dbStore;
|
---|
40 |
|
---|
41 |
|
---|
42 | /**
|
---|
43 | * Name of the Dataset
|
---|
44 | *
|
---|
45 | * @var string
|
---|
46 | * @access private
|
---|
47 | */
|
---|
48 | var $setName;
|
---|
49 |
|
---|
50 |
|
---|
51 | /**
|
---|
52 | * Constructor
|
---|
53 | * You can supply a Dataset name.
|
---|
54 | *
|
---|
55 | * @param ADODBConnection
|
---|
56 | * @param DbStore
|
---|
57 | * @param string
|
---|
58 | * @access public
|
---|
59 | */
|
---|
60 | function DatasetDb(&$dbConnection,&$dbStore,$datasetName)
|
---|
61 | {
|
---|
62 | $this->dbConnection=& $dbConnection;
|
---|
63 | $this->dbStore=&$dbStore;
|
---|
64 | $this->setName= $datasetName;
|
---|
65 | $this->_initialize();
|
---|
66 | }
|
---|
67 |
|
---|
68 | /**
|
---|
69 | * Initialize
|
---|
70 | * Read all needed data into the set.
|
---|
71 | *
|
---|
72 | *
|
---|
73 | * @access private
|
---|
74 | */
|
---|
75 | function _initialize()
|
---|
76 | {
|
---|
77 | $recordSet =& $this->dbConnection->execute("SELECT defaultModelUri
|
---|
78 | FROM datasets where datasetName='".$this->setName."'");
|
---|
79 |
|
---|
80 | $this->defaultGraph=& $this->dbStore->getModel($recordSet->fields[0]);
|
---|
81 | }
|
---|
82 |
|
---|
83 |
|
---|
84 |
|
---|
85 | // === Graph level methods ========================
|
---|
86 |
|
---|
87 | /**
|
---|
88 | * Sets the Dataset name. Return true on success, false otherwise.
|
---|
89 | *
|
---|
90 | * @param string
|
---|
91 | * @access public
|
---|
92 | */
|
---|
93 | function setDatasetName($datasetName)
|
---|
94 | {
|
---|
95 | if ($this->dbStore->datasetExists($datasetName))
|
---|
96 | return false;
|
---|
97 |
|
---|
98 | $this->dbConnection->execute("UPDATE datasets SET datasetName='".$datasetName."'
|
---|
99 | where datasetName='".$this->setName."'");
|
---|
100 |
|
---|
101 | $this->dbConnection->execute("UPDATE dataset_model SET datasetName='".$datasetName."'
|
---|
102 | where datasetName='".$this->setName."'");
|
---|
103 | $this->setName=$datasetName;
|
---|
104 | return true;
|
---|
105 | }
|
---|
106 |
|
---|
107 | /**
|
---|
108 | * Returns the Datasets name.
|
---|
109 | *
|
---|
110 | * @return string
|
---|
111 | * @access public
|
---|
112 | */
|
---|
113 | function getDatasetName()
|
---|
114 | {
|
---|
115 | return $this->setName;
|
---|
116 | }
|
---|
117 |
|
---|
118 | /**
|
---|
119 | * Adds a NamedGraph to the set.
|
---|
120 | *
|
---|
121 | * @param NamedGraphDb
|
---|
122 | */
|
---|
123 | function addNamedGraph(&$graph)
|
---|
124 | {
|
---|
125 | $graphNameURI=$graph->getGraphName();
|
---|
126 | $this->removeNamedGraph($graphNameURI);
|
---|
127 | $this->dbConnection->execute('INSERT INTO dataset_model VALUES('
|
---|
128 | . $this->dbConnection->qstr($this->setName) . ','
|
---|
129 | . $this->dbConnection->qstr($graph->modelID) . ','
|
---|
130 | . $this->dbConnection->qstr($graphNameURI) .')');
|
---|
131 | }
|
---|
132 |
|
---|
133 |
|
---|
134 | /**
|
---|
135 | * Overwrites the existting default graph.
|
---|
136 | *
|
---|
137 | * @param DbModel
|
---|
138 | */
|
---|
139 | function setDefaultGraph(&$graph)
|
---|
140 | {
|
---|
141 | $this->dbConnection->execute('UPDATE datasets SET defaultModelUri ='
|
---|
142 | . $this->dbConnection->qstr($graph->modelURI) . ' WHERE datasetName ='
|
---|
143 | . $this->dbConnection->qstr($this->setName));
|
---|
144 | }
|
---|
145 |
|
---|
146 | /**
|
---|
147 | * Returns a reference to the defaultGraph.
|
---|
148 | *
|
---|
149 | * @return NamedGraphDb
|
---|
150 | */
|
---|
151 | function & getDefaultGraph()
|
---|
152 | {
|
---|
153 | $defaultGraphURI = $this->dbConnection->GetOne("SELECT defaultModelUri FROM datasets WHERE datasetName ='".$this->setName."'");
|
---|
154 | return ($this->dbStore->getNamedGraphDb($defaultGraphURI,'http://rdfapi-php/dataset_defaultGraph_'.$this->setName));
|
---|
155 | }
|
---|
156 |
|
---|
157 | /**
|
---|
158 | * Returns true, if a defaultGraph exists. False otherwise.
|
---|
159 | *
|
---|
160 | * @return boolean
|
---|
161 | */
|
---|
162 | function hasDefaultGraph()
|
---|
163 | {
|
---|
164 | return true;
|
---|
165 | }
|
---|
166 |
|
---|
167 | /**
|
---|
168 | * Removes a NamedGraph from the set. Nothing happens
|
---|
169 | * if no graph with that name is contained in the set.
|
---|
170 | *
|
---|
171 | * @param string
|
---|
172 | */
|
---|
173 | function removeNamedGraph($graphName)
|
---|
174 | {
|
---|
175 | $this->dbConnection->execute('DELETE FROM dataset_model WHERE datasetName="'
|
---|
176 | . $this->dbConnection->qstr($this->setName) . '" AND graphURI ="'
|
---|
177 | . $this->dbConnection->qstr($graphName) . '"');
|
---|
178 | }
|
---|
179 |
|
---|
180 | /**
|
---|
181 | * Tells wether the Dataset contains a NamedGraph.
|
---|
182 | *
|
---|
183 | * @param string
|
---|
184 | * @return boolean
|
---|
185 | */
|
---|
186 | function containsNamedGraph($graphName)
|
---|
187 | {
|
---|
188 | $count= $this->dbConnection->GetOne('SELECT count(*) FROM dataset_model WHERE datasetName="'.$this->setName.'" AND graphURI ="'.$graphName.'"');
|
---|
189 | return ($count>0);
|
---|
190 | }
|
---|
191 |
|
---|
192 | /**
|
---|
193 | * Returns the NamedGraph with a specific name from the Dataset.
|
---|
194 | * Changes to the graph will be reflected in the set.
|
---|
195 | *
|
---|
196 | * @param string
|
---|
197 | * @return NamedGraphDb or null
|
---|
198 | */
|
---|
199 | function &getNamedGraph($graphName)
|
---|
200 | {
|
---|
201 | if(!$this->containsNamedGraph($graphName))
|
---|
202 | return null;
|
---|
203 |
|
---|
204 | $modelVars =& $this->dbConnection->execute("SELECT models.modelURI, models.modelID, models.baseURI
|
---|
205 | FROM models, dataset_model
|
---|
206 | WHERE dataset_model.graphURI ='" .$graphName ."' AND dataset_model.modelId= models.modelID");
|
---|
207 |
|
---|
208 | return new NamedGraphDb($this->dbConnection, $modelVars->fields[0],
|
---|
209 | $modelVars->fields[1], $graphName ,$modelVars->fields[2]);
|
---|
210 | }
|
---|
211 |
|
---|
212 | /**
|
---|
213 | * Returns the names of the namedGraphs in this set as strings in an array.
|
---|
214 | *
|
---|
215 | * @return Array
|
---|
216 | */
|
---|
217 | function listGraphNames()
|
---|
218 | {
|
---|
219 | $recordSet =& $this->dbConnection->execute("SELECT graphURI FROM dataset_model WHERE datasetName ='".$this->setName."'");
|
---|
220 |
|
---|
221 | $return=array();
|
---|
222 | while (!$recordSet->EOF)
|
---|
223 | {
|
---|
224 | $return[] = $recordSet->fields[0];
|
---|
225 | $recordSet->moveNext();
|
---|
226 | }
|
---|
227 | return $return;
|
---|
228 | }
|
---|
229 |
|
---|
230 | /**
|
---|
231 | * Creates a new NamedGraph and adds it to the set. An existing graph with the same name will be replaced. But the old namedGraph remains in the database.
|
---|
232 | *
|
---|
233 | * @param string
|
---|
234 | * @param string
|
---|
235 | * @return NamedGraphDb
|
---|
236 | */
|
---|
237 | function &createGraph($graphName,$baseURI = null)
|
---|
238 | {
|
---|
239 | $graph =& $this->dbStore->getNewNamedGraphDb(uniqid('http://rdfapi-php/namedGraph_'),$graphName,$baseURI);
|
---|
240 | $this->addNamedGraph($graph);
|
---|
241 |
|
---|
242 | return $graph;
|
---|
243 | }
|
---|
244 |
|
---|
245 | /**
|
---|
246 | * Deletes all NamedGraphs from the set.
|
---|
247 | */
|
---|
248 | function clear()
|
---|
249 | {
|
---|
250 | $this->dbConnection->execute("DELETE FROM dataset_model WHERE datasetName ='".$this->setName."'");
|
---|
251 | }
|
---|
252 |
|
---|
253 | /**
|
---|
254 | * Returns the number of NamedGraphs in the set. Empty graphs are counted.
|
---|
255 | *
|
---|
256 | * @return int
|
---|
257 | */
|
---|
258 | function countGraphs()
|
---|
259 | {
|
---|
260 | return ($this->dbConnection->GetOne("SELECT count(*) FROM dataset_model WHERE datasetName ='".$this->setName."'"));
|
---|
261 | }
|
---|
262 |
|
---|
263 | /**
|
---|
264 | * Returns an iterator over all {@link NamedGraph}s in the set.
|
---|
265 | *
|
---|
266 | * @return IteratorAllGraphsDb
|
---|
267 | */
|
---|
268 | function &listNamedGraphs()
|
---|
269 | {
|
---|
270 | $recordSet =& $this->dbConnection->execute("SELECT graphURI FROM dataset_model WHERE datasetName ='".$this->setName."'");
|
---|
271 | $it = new IteratorAllGraphsDb($recordSet, $this);
|
---|
272 | return $it;
|
---|
273 | }
|
---|
274 |
|
---|
275 | /**
|
---|
276 | * Tells wether the set contains any NamedGraphs.
|
---|
277 | *
|
---|
278 | * @return boolean
|
---|
279 | */
|
---|
280 | function isEmpty()
|
---|
281 | {
|
---|
282 | return ($this->countGraphs()==0);
|
---|
283 | }
|
---|
284 |
|
---|
285 | /**
|
---|
286 | * Add all named graphs of the other dataset to this dataset.
|
---|
287 | *
|
---|
288 | * @param Dataset
|
---|
289 | */
|
---|
290 | function addAll($otherDataset)
|
---|
291 | {
|
---|
292 | for($iterator = $otherDataset->listNamedGraphs(); $iterator->valid(); $iterator->next())
|
---|
293 | {
|
---|
294 | $this->addNamedGraph($iterator->current());
|
---|
295 | };
|
---|
296 |
|
---|
297 | if ($otherDataset->hasDefaultGraph())
|
---|
298 | {
|
---|
299 | $this->defaultGraph = $this->defaultGraph->unite($otherDataset->getDefaultGraph());
|
---|
300 | }
|
---|
301 | }
|
---|
302 |
|
---|
303 | // === Quad level methods ========================
|
---|
304 |
|
---|
305 | /**
|
---|
306 | * Adds a quad to the Dataset. The argument must not contain any
|
---|
307 | * wildcards. If the quad is already present, nothing happens. A new
|
---|
308 | * named graph will automatically be created if necessary.
|
---|
309 | *
|
---|
310 | * @param Quad
|
---|
311 | */
|
---|
312 | function addQuad(&$quad)
|
---|
313 | {
|
---|
314 | $graphName=$quad->getGraphName();
|
---|
315 | $graphName=$graphName->getLabel();
|
---|
316 |
|
---|
317 | $graph=& $this->getNamedGraph($graphName);
|
---|
318 |
|
---|
319 | if ($graph===null)
|
---|
320 | $graph=& $this->createGraph($graphName);
|
---|
321 |
|
---|
322 | $statement=$quad->getStatement();
|
---|
323 | $graph->add($statement);
|
---|
324 | }
|
---|
325 |
|
---|
326 | /**
|
---|
327 | * Tells wether the Dataset contains a quad or
|
---|
328 | * quads matching a pattern.
|
---|
329 | *
|
---|
330 | * @param Resource
|
---|
331 | * @param Resource
|
---|
332 | * @param Resource
|
---|
333 | * @param Resource
|
---|
334 | * @return boolean
|
---|
335 | */
|
---|
336 | function containsQuad($graphName,$subject,$predicate,$object)
|
---|
337 | {
|
---|
338 | // static part of the sql statement
|
---|
339 | $sql = "SELECT count(*)
|
---|
340 | FROM statements, dataset_model
|
---|
341 | WHERE datasetName ='".$this->setName."' AND statements.modelID=dataset_model.modelId ";
|
---|
342 |
|
---|
343 | if($graphName!=null)
|
---|
344 | {
|
---|
345 | $sql.= " AND graphURI ='".$graphName->getLabel()."'";
|
---|
346 | }
|
---|
347 |
|
---|
348 | // dynamic part of the sql statement
|
---|
349 | $sql .= DbModel::_createDynSqlPart_SPO($subject, $predicate, $object);
|
---|
350 |
|
---|
351 | return (($this->dbConnection->GetOne($sql))>0);
|
---|
352 | }
|
---|
353 |
|
---|
354 | /**
|
---|
355 | * Deletes a Quad from the RDF dataset.
|
---|
356 | *
|
---|
357 | * @param Quad
|
---|
358 | */
|
---|
359 | function removeQuad($quad)
|
---|
360 | {
|
---|
361 | $graphName=$quad->getGraphName();$graphName=$graphName->getLabel();
|
---|
362 | //find namedGraph IDs
|
---|
363 | $graphID = $this->dbConnection->GetOne("SELECT modelId FROM dataset_model WHERE graphURI ='$graphName'");
|
---|
364 |
|
---|
365 | // static part of the sql statement
|
---|
366 | $sql = "DELETE FROM statements WHERE modelID = $graphID";
|
---|
367 |
|
---|
368 | // dynamic part of the sql statement
|
---|
369 | $sql .= DbModel::_createDynSqlPart_SPO($quad->getSubject(), $quad->getPredicate(), $quad->getObject());
|
---|
370 |
|
---|
371 | // execute the query
|
---|
372 | if($graphID)
|
---|
373 | $recordSet =& $this->dbConnection->execute($sql);
|
---|
374 | }
|
---|
375 |
|
---|
376 | /**
|
---|
377 | * Counts the Quads in the RDF dataset. Identical Triples in
|
---|
378 | * different NamedGraphs are counted individually.
|
---|
379 | *
|
---|
380 | * @return int
|
---|
381 | */
|
---|
382 | function countQuads()
|
---|
383 | {
|
---|
384 | $sql = "SELECT count(*)
|
---|
385 | FROM statements, dataset_model
|
---|
386 | WHERE datasetName ='".$this->setName."' AND statements.modelID=dataset_model.modelId ";
|
---|
387 |
|
---|
388 | return ((int)$this->dbConnection->GetOne($sql));
|
---|
389 | }
|
---|
390 |
|
---|
391 | /**
|
---|
392 | * Finds Statements that match a quad pattern. The argument may contain
|
---|
393 | * wildcards.
|
---|
394 | *
|
---|
395 | * @param Resource or null
|
---|
396 | * @param Resource or null
|
---|
397 | * @param Resource or null
|
---|
398 | * @param Resource or null
|
---|
399 | * @return IteratorFindQuadsDb
|
---|
400 | */
|
---|
401 | function &findInNamedGraphs($graphName,$subject,$predicate,$object,$returnAsTriples =false )
|
---|
402 | {
|
---|
403 | // static part of the sql statement
|
---|
404 | $sql = "SELECT subject, predicate, object, l_language, l_datatype, subject_is, object_is, dataset_model.graphURI
|
---|
405 | FROM statements, dataset_model
|
---|
406 | WHERE datasetName ='".$this->setName."' AND statements.modelID=dataset_model.modelId ";
|
---|
407 |
|
---|
408 | if($graphName!=null)
|
---|
409 | {
|
---|
410 | $sql.= " AND graphURI ='".$graphName->getLabel()."'";
|
---|
411 | }
|
---|
412 |
|
---|
413 | // dynamic part of the sql statement
|
---|
414 | $sql .= DbModel::_createDynSqlPart_SPO($subject, $predicate, $object);
|
---|
415 |
|
---|
416 | // execute the query
|
---|
417 | $recordSet =& $this->dbConnection->execute($sql);
|
---|
418 |
|
---|
419 |
|
---|
420 | $it = new IteratorFindQuadsDb($recordSet, $this, $returnAsTriples);
|
---|
421 | return $it;
|
---|
422 | }
|
---|
423 |
|
---|
424 | /**
|
---|
425 | * Finds Statements that match a pattern in the default Graph. The argument may contain
|
---|
426 | * wildcards.
|
---|
427 | *
|
---|
428 | * @param Resource or null
|
---|
429 | * @param Resource or null
|
---|
430 | * @param Resource or null
|
---|
431 | * @return IteratorFindQuadsDb
|
---|
432 | */
|
---|
433 | function &findInDefaultGraph($subject,$predicate,$object)
|
---|
434 | {
|
---|
435 | $defaultGraphID = (int)$this->dbConnection->GetOne("SELECT models.modelID FROM datasets, models WHERE datasets.datasetName ='".$this->setName."' AND datasets.defaultModelUri = models.modelURI");
|
---|
436 | // static part of the sql statement
|
---|
437 | $sql = "SELECT subject, predicate, object, l_language, l_datatype, subject_is, object_is
|
---|
438 | FROM statements
|
---|
439 | WHERE modelID ='$defaultGraphID'";
|
---|
440 |
|
---|
441 | // dynamic part of the sql statement
|
---|
442 | $sql .= DbModel::_createDynSqlPart_SPO($subject, $predicate, $object);
|
---|
443 |
|
---|
444 | // execute the query
|
---|
445 | $recordSet =& $this->dbConnection->execute($sql);
|
---|
446 |
|
---|
447 | $it = new IteratorFindQuadsDb($recordSet, $this, true);
|
---|
448 | return $it;
|
---|
449 | }
|
---|
450 | }
|
---|
451 | ?> |
---|