Source for file DatasetDb.php

Documentation is available at DatasetDb.php

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

Documentation generated on Thu, 7 Jul 2005 13:41:11 +0200 by phpDocumentor 1.3.0RC3