Source for file DatasetDb.php

Documentation is available at DatasetDb.php

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

Documentation generated on Fri, 13 Jan 2006 07:48:02 +0100 by phpDocumentor 1.3.0RC4