PageRenderTime 29ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

/s3db3.5.10/pearlib/rdfapi-php/api/model/DbStore.php

https://code.google.com/p/s3db/
PHP | 784 lines | 391 code | 130 blank | 263 comment | 59 complexity | 35460b7935a2e58a8e8c1f7df2f09ad1 MD5 | raw file
  1. <?php
  2. require_once RDFAPI_INCLUDE_DIR . 'constants.php';
  3. require_once RDFAPI_INCLUDE_DIR . 'util/Object.php';
  4. // ----------------------------------------------------------------------------------
  5. // Class: DbStore
  6. // ----------------------------------------------------------------------------------
  7. /**
  8. * DbStore is a persistent store of RDF data using relational database technology.
  9. * DbStore uses ADOdb Library for PHP V3.60 (http://php.weblogs.com/ADODB),
  10. * which allows to connect to multiple databases in a portable manner.
  11. * This class also provides methods for creating tables for MsAccess, MySQL, and MS SQL Server.
  12. * If you want to use other databases, you will have to create tables by yourself
  13. * according to the abstract database schema described in the API documentation.
  14. *
  15. * You can activate debug mode by defining ADODB_DEBUG_MODE to "1".
  16. *
  17. *
  18. * @version $Id: DbStore.php,v 1.29 2007/05/31 00:23:47 cweiske Exp $
  19. * @author Radoslaw Oldakowski <radol@gmx.de>
  20. * @author Daniel Westphal (http://www.d-westphal.de)
  21. *
  22. * @package model
  23. * @access public
  24. */
  25. class DbStore extends Object{
  26. /**
  27. * Database connection object
  28. *
  29. * @var object ADOConnection
  30. * @access private
  31. */
  32. var $dbConn;
  33. /**
  34. * SparqlParser so we can re-use it
  35. * @var Parser
  36. */
  37. var $queryParser = null;
  38. /**
  39. * Constructor:
  40. * Set the database connection with the given parameters.
  41. *
  42. * @param string $dbDriver
  43. * @param string $host
  44. * @param string $dbName
  45. * @param string $user
  46. * @param string $password
  47. * @access public
  48. */
  49. function DbStore ($dbDriver=ADODB_DB_DRIVER, $host=ADODB_DB_HOST, $dbName=ADODB_DB_NAME,
  50. $user=ADODB_DB_USER, $password=ADODB_DB_PASSWORD) {
  51. // include DBase Package
  52. require_once(RDFAPI_INCLUDE_DIR.PACKAGE_DBASE);
  53. // create a new connection object
  54. $this->dbConn =& ADONewConnection($dbDriver);
  55. //activate the ADOdb DEBUG mode
  56. if (ADODB_DEBUG_MODE == '1')
  57. $this->dbConn->debug = true;
  58. // connect to database
  59. $r = $this->dbConn->connect($host, $user, $password, $dbName);
  60. if ($r !== true) {
  61. throw new Exception('Could not connect to database');
  62. }
  63. // optimized for speed
  64. $this->dbConn->setFetchMode(ADODB_FETCH_NUM);
  65. //$ADODB_COUNTRECS = FALSE;
  66. }
  67. /**
  68. * Create tables and indexes for the given database type.
  69. * Currently supported: MsAccess and MySQL.
  70. * If you want to use other databases, you will have to create tables by yourself
  71. * according to the abstract <a href="database_schema.html">database schema</a>
  72. * described in the API documentation.
  73. *
  74. * @param string $databaseType
  75. * @throws PhpError
  76. * @access public
  77. */
  78. function createTables($databaseType) {
  79. if (!strcasecmp($databaseType, 'MsAccess'))
  80. $this->_createTables_MsAccess();
  81. elseif (!strcasecmp($databaseType, 'MySQL'))
  82. $this->_createTables_MySql();
  83. elseif (!strcasecmp($databaseType, 'MSSQL'))
  84. $this->_createTables_mssql();
  85. else {
  86. $errmsg = RDFAPI_ERROR . "(class: DbStore; method: createTables('$databaseType')):
  87. Currently only MsAcces, MySQL and MSSQL supported.";
  88. trigger_error($errmsg, E_USER_ERROR);
  89. }
  90. }
  91. /**
  92. * List all DbModels stored in the database.
  93. *
  94. * @return array
  95. * @throws SqlError
  96. * @access public
  97. */
  98. function listModels() {
  99. $recordSet =& $this->dbConn->execute("SELECT modelURI, baseURI
  100. FROM models");
  101. if (!$recordSet)
  102. echo $this->dbConn->errorMsg();
  103. else {
  104. $models = array();
  105. $i=0;
  106. while (!$recordSet->EOF) {
  107. $models[$i]['modelURI'] = $recordSet->fields[0];
  108. $models[$i]['baseURI'] = $recordSet->fields[1];
  109. ++$i;
  110. $recordSet->moveNext();
  111. }
  112. return $models;
  113. }
  114. }
  115. /**
  116. * Check if the DbModel with the given modelURI is already stored in the database
  117. *
  118. * @param string $modelURI
  119. * @return boolean
  120. * @throws SqlError
  121. * @access public
  122. */
  123. function modelExists($modelURI) {
  124. $res =& $this->dbConn->execute("SELECT COUNT(*) FROM models
  125. WHERE modelURI = '" .$modelURI ."'");
  126. if (!$res)
  127. echo $this->dbConn->errorMsg();
  128. else {
  129. if (!$res->fields[0]) {
  130. $res->Close();
  131. return FALSE;
  132. } else {
  133. $res->Close();
  134. return TRUE;
  135. }
  136. }
  137. }
  138. /**
  139. * Returns the database connection object
  140. *
  141. * @return ADOdb Database object
  142. * @access public
  143. */
  144. function &getDbConn()
  145. {
  146. return $this->dbConn;
  147. }
  148. /**
  149. * Create a new instance of DbModel with the given $modelURI and
  150. * load the corresponding values of modelID and baseURI from the database.
  151. * Return FALSE if the DbModel does not exist.
  152. *
  153. * @param string $modelURI
  154. * @return object DbModel
  155. * @access public
  156. */
  157. function getModel($modelURI) {
  158. if (!$this->modelExists($modelURI))
  159. return FALSE;
  160. else {
  161. $modelVars =& $this->dbConn->execute("SELECT modelURI, modelID, baseURI
  162. FROM models
  163. WHERE modelURI='" .$modelURI ."'");
  164. return new DbModel($this->dbConn, $modelVars->fields[0],
  165. $modelVars->fields[1], $modelVars->fields[2]);
  166. }
  167. }
  168. /**
  169. * Create a new instance of DbModel with the given $modelURI
  170. * and insert the DbModel variables into the database.
  171. * Return FALSE if there is already a model with the given URI.
  172. *
  173. * @param string $modelURI
  174. * @param string $baseURI
  175. * @return object DbModel
  176. * @throws SqlError
  177. * @access public
  178. */
  179. function getNewModel($modelURI, $baseURI=NULL) {
  180. if ($this->modelExists($modelURI))
  181. return FALSE;
  182. else {
  183. $modelID = $this->_createUniqueModelID();
  184. $rs =& $this->dbConn->execute("INSERT INTO models
  185. (modelID, modelURI, baseURI)
  186. VALUES ('" .$modelID ."',
  187. '" .$modelURI ."',
  188. '" .$baseURI ."')");
  189. if (!$rs)
  190. $this->dbConn->errorMsg();
  191. else
  192. return new DbModel($this->dbConn, $modelURI, $modelID, $baseURI);
  193. }
  194. }
  195. /**
  196. * Store a MemModel or another DbModel from a different DbStore in the database.
  197. * Return FALSE if there is already a model with modelURI matching the modelURI
  198. * of the given model.
  199. *
  200. * @param object Model &$model
  201. * @param string $modelURI
  202. * @return boolean
  203. * @access public
  204. */
  205. function putModel(&$model, $modelURI=NULL) {
  206. if (!$modelURI) {
  207. if (is_a($model, 'MemModel'))
  208. $modelURI = 'DbModel-' .$this->_createUniqueModelID();
  209. else
  210. $modelURI = $model->modelURI;
  211. }else
  212. if ($this->modelExists($modelURI))
  213. return FALSE;
  214. $newDbModel = $this->getNewModel($modelURI, $model->getBaseURI());
  215. $newDbModel->addModel($model);
  216. }
  217. /**
  218. * Close the DbStore.
  219. * !!! Warning: If you close the DbStore all active instances of DbModel from this
  220. * !!! DbStore will lose their database connection !!!
  221. *
  222. * @access public
  223. */
  224. function close() {
  225. $this->dbConn->close();
  226. unset($this);
  227. }
  228. // =============================================================================
  229. // **************************** private methods ********************************
  230. // =============================================================================
  231. /**
  232. * Create a unique ID for the DbModel to be insert into the models table.
  233. * This method was implemented because some databases do not support auto-increment.
  234. *
  235. * @return integer
  236. * @access private
  237. */
  238. function _createUniqueModelID() {
  239. $maxModelID =& $this->dbConn->GetOne('SELECT MAX(modelID) FROM models');
  240. return ++$maxModelID;
  241. }
  242. /**
  243. * Create a unique ID for the dataset to be insert into the datasets table.
  244. * This method was implemented because some databases do not support auto-increment.
  245. *
  246. * @return integer
  247. * @access private
  248. */
  249. function _createUniqueDatasetID() {
  250. $maxDatasetID =& $this->dbConn->GetOne('SELECT MAX(datasetId) FROM datasets');
  251. return ++$maxDatasetID;
  252. }
  253. /**
  254. * Create tables and indexes for MsAccess database
  255. *
  256. * @throws SqlError
  257. * @access private
  258. */
  259. function _createTables_MsAccess() {
  260. $this->dbConn->startTrans();
  261. $this->dbConn->execute('CREATE TABLE models
  262. (modelID long primary key,
  263. modelURI varchar not null,
  264. baseURI varchar)');
  265. $this->dbConn->execute('CREATE UNIQUE INDEX m_modURI_idx ON models (modelURI)');
  266. $this->dbConn->execute('CREATE TABLE statements
  267. (modelID long,
  268. subject varchar,
  269. predicate varchar,
  270. object Memo,
  271. l_language varchar,
  272. l_datatype varchar,
  273. subject_is varchar(1),
  274. object_is varchar(1),
  275. primary key (modelID, subject, predicate, object,
  276. l_language, l_datatype))');
  277. $this->dbConn->execute('CREATE INDEX s_mod_idx ON statements (modelID)');
  278. $this->dbConn->execute('CREATE INDEX s_sub_idx ON statements (subject)');
  279. $this->dbConn->execute('CREATE INDEX s_pred_idx ON statements (predicate)');
  280. $this->dbConn->execute('CREATE INDEX s_obj_idx ON statements (object)');
  281. $this->dbConn->execute('CREATE TABLE namespaces
  282. (modelID long,
  283. namespace varchar,
  284. prefix varchar,
  285. primary key (modelID, namespace, prefix))');
  286. $this->dbConn->execute('CREATE INDEX n_name_idx ON namespaces (namespace)');
  287. $this->dbConn->execute('CREATE INDEX n_pref_idx ON namespaces (prefix)');
  288. $this->dbConn->execute("CREATE TABLE datasets
  289. (datasetName varchar,
  290. defaultModelUri varchar,
  291. primary key (datasetName))");
  292. $this->dbConn->execute('CREATE INDEX nGS_idx1 ON datasets (datasetName)');
  293. $this->dbConn->execute("CREATE TABLE `dataset_model` (
  294. datasetName varchar,
  295. modelId long,
  296. graphURI varchar,
  297. PRIMARY KEY (modelId,datasetName))");
  298. if (!$this->dbConn->completeTrans())
  299. echo $this->dbConn->errorMsg();
  300. }
  301. /**
  302. * Create tables and indexes for MySQL database
  303. *
  304. * @throws SqlError
  305. * @access private
  306. */
  307. function _createTables_MySql() {
  308. $this->dbConn->startTrans();
  309. $this->dbConn->execute("CREATE TABLE models
  310. (modelID bigint NOT NULL,
  311. modelURI varchar(255) NOT NULL,
  312. baseURI varchar(255) DEFAULT '',
  313. primary key (modelID))");
  314. $this->dbConn->execute('CREATE UNIQUE INDEX m_modURI_idx ON models (modelURI)');
  315. $this->dbConn->execute("CREATE TABLE statements
  316. (modelID bigint NOT NULL,
  317. subject varchar(255) NOT NULL,
  318. predicate varchar(255) NOT NULL,
  319. object text,
  320. l_language varchar(255) DEFAULT '',
  321. l_datatype varchar(255) DEFAULT '',
  322. subject_is varchar(1) NOT NULL,
  323. object_is varchar(1) NOT NULL)");
  324. $this->dbConn->execute("CREATE TABLE namespaces
  325. (modelID bigint NOT NULL,
  326. namespace varchar(255) NOT NULL,
  327. prefix varchar(255) NOT NULL,
  328. primary key (modelID,namespace))");
  329. $this->dbConn->execute("CREATE TABLE `dataset_model` (
  330. `datasetName` varchar(255) NOT NULL default '0',
  331. `modelId` bigint(20) NOT NULL default '0',
  332. `graphURI` varchar(255) NOT NULL default '',
  333. PRIMARY KEY (`modelId`,`datasetName`))");
  334. $this->dbConn->execute("CREATE TABLE `datasets` (
  335. `datasetName` varchar(255) NOT NULL default '',
  336. `defaultModelUri` varchar(255) NOT NULL default '0',
  337. PRIMARY KEY (`datasetName`),
  338. KEY `datasetName` (`datasetName`))");
  339. $this->dbConn->execute('CREATE INDEX s_mod_idx ON statements (modelID)');
  340. $this->dbConn->execute('CREATE INDEX n_mod_idx ON namespaces (modelID)');
  341. $this->dbConn->execute('CREATE INDEX s_sub_pred_idx ON statements
  342. (subject(200),predicate(200))');
  343. $this->dbConn->execute('CREATE INDEX s_sub_idx ON statements (subject(200))');
  344. $this->dbConn->execute('CREATE INDEX s_pred_idx ON statements (predicate(200))');
  345. $this->dbConn->execute('CREATE INDEX s_obj_idx ON statements (object(250))');
  346. $this->dbConn->execute('CREATE FULLTEXT INDEX s_obj_ftidx ON statements (object)');
  347. if (!$this->dbConn->completeTrans())
  348. echo $this->dbConn->errorMsg();
  349. }
  350. /**
  351. * Create tables and indexes for MSSQL database
  352. *
  353. * @throws SqlError
  354. * @access private
  355. */
  356. function _createTables_mssql(){
  357. $this->dbConn->startTrans();
  358. $this->dbConn->execute("CREATE TABLE [dbo].[models] (
  359. [modelID] [int] NOT NULL ,
  360. [modelURI] [nvarchar] (200) COLLATE SQL_Latin1_General_CP1_CI_AS NULL ,
  361. [baseURI] [nvarchar] (200) COLLATE SQL_Latin1_General_CP1_CI_AS NULL
  362. ) ON [PRIMARY]");
  363. $this->dbConn->execute("CREATE TABLE [dbo].[statements] (
  364. [modelID] [int] NOT NULL ,
  365. [subject] [nvarchar] (200) COLLATE SQL_Latin1_General_CP1_CI_AS NULL ,
  366. [predicate] [nvarchar] (200) COLLATE SQL_Latin1_General_CP1_CI_AS NULL ,
  367. [object] [text] COLLATE SQL_Latin1_General_CP1_CI_AS NULL ,
  368. [l_language] [nvarchar] (50) COLLATE SQL_Latin1_General_CP1_CI_AS NULL ,
  369. [l_datatype] [nvarchar] (50) COLLATE SQL_Latin1_General_CP1_CI_AS NULL ,
  370. [subject_is] [nchar] (1) COLLATE SQL_Latin1_General_CP1_CI_AS NULL ,
  371. [object_is] [nchar] (1) COLLATE SQL_Latin1_General_CP1_CI_AS NULL
  372. ) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]");
  373. $this->dbConn->execute("CREATE TABLE [dbo].[namespaces] (
  374. [modelID] [int] NOT NULL ,
  375. [namespace] [nvarchar] (200) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL ,
  376. [prefix] [nvarchar] (200) COLLATE SQL_Latin1_General_CP1_CI_AS NULL ,
  377. ) ON [PRIMARY]");
  378. $this->dbConn->execute("ALTER TABLE [dbo].[models] WITH NOCHECK ADD
  379. CONSTRAINT [PK_models] PRIMARY KEY CLUSTERED
  380. (
  381. [modelID]
  382. ) ON [PRIMARY] ");
  383. $this->dbConn->execute("ALTER TABLE [dbo].[namespaces] WITH NOCHECK ADD
  384. CONSTRAINT [PK_namespaces] PRIMARY KEY CLUSTERED
  385. (
  386. [modelID],[namespace]
  387. ) ON [PRIMARY] ");
  388. $this->dbConn->execute("CREATE INDEX [joint index on subject and predicate] ON [dbo].[statements]([subject], [predicate]) ON [PRIMARY]");
  389. if (!$this->dbConn->completeTrans())
  390. echo $this->dbConn->errorMsg();
  391. }
  392. /**
  393. * Checks if tables are setup for RAP
  394. *
  395. * @param string $databaseType
  396. * @throws SqlError
  397. * @access public
  398. **/
  399. function isSetup($databaseType="MySQL") {
  400. if ($databaseType=="MySQL")
  401. return $this->_isSetup_MySql();
  402. if ($databaseType=="MSSQL")
  403. return $this->_isSetup_MSSQL();
  404. else {
  405. if ($databaseType=='MsAccess'){
  406. return $this->_isSetup_MsAccess();
  407. }else{
  408. $errmsg=RDFAPI_ERROR."(class: DbStore; method isSetup('$databaseType')):\nCurrently only MySQL, MsAccess and MSSQL are supported!";
  409. trigger_error($errmsg, E_USER_ERROR);}
  410. }
  411. }
  412. /**
  413. * Checks if tables are setup for RAP (MySql)
  414. *
  415. * @throws SqlError
  416. * @access private
  417. **/
  418. function _isSetup_MySql() {
  419. $recordSet =& $this->dbConn->execute("SHOW TABLES");
  420. if (!$recordSet)
  421. echo $this->dbConn->errorMsg();
  422. else {
  423. $tables = array();
  424. while (!$recordSet->EOF) {
  425. $tables[]= $recordSet->fields[0];
  426. if(isset($i)){++$i;}
  427. $recordSet->moveNext();
  428. }
  429. if (in_array("models",$tables) && in_array("statements",$tables)&& in_array("namespaces",$tables)) return true;
  430. }
  431. return false;
  432. }
  433. /**
  434. * Checks if tables are setup for RAP (MsAccess)
  435. *
  436. * @throws SqlError
  437. * @access private
  438. **/
  439. function _isSetup_MsAccess() {
  440. $tables =& $this->dbConn->MetaTables();
  441. if (!$tables)
  442. echo $this->dbConn->errorMsg();
  443. if (count($tables)==0){
  444. return false;}
  445. else {
  446. if (in_array("models",$tables) && in_array("statements",$tables) && in_array("namespaces",$tables)){ return true;
  447. }else{return false;}
  448. }
  449. }
  450. /**
  451. * Checks if tables are setup for RAP (MSSQL)
  452. *
  453. * @throws SqlError
  454. * @access private
  455. **/
  456. function _isSetup_MSSQL() {
  457. $tables =& $this->dbConn->MetaTables();
  458. if (!$tables)
  459. echo $this->dbConn->errorMsg();
  460. if (count($tables)==0){
  461. return false;}
  462. else {
  463. if (in_array("models",$tables) && in_array("statements",$tables) && in_array("namespaces",$tables)){ return true;
  464. }else{return false;}
  465. }
  466. }
  467. /**
  468. * Create a new instance of DatasetDb with the given $datasetName
  469. * and insert the DatasetDb variables into the database.
  470. * Return FALSE if there is already a model with the given URI.
  471. *
  472. * @param $datasetName string
  473. * @return object DatasetDB
  474. * @throws SqlError
  475. * @access public
  476. */
  477. function & getNewDatasetDb($datasetName)
  478. {
  479. require_once(RDFAPI_INCLUDE_DIR . PACKAGE_DATASET);
  480. if ($this->datasetExists($datasetName))
  481. return FALSE;
  482. else
  483. {
  484. $defaultModelUri=uniqid('http://rdfapi-php/dataset_defaultmodel_');
  485. $defaultModel=$this->getNewModel($defaultModelUri);
  486. $rs =& $this->dbConn->execute("INSERT INTO datasets
  487. VALUES ('" .$datasetName ."',
  488. '" .$defaultModelUri."')");
  489. if (!$rs)
  490. $this->dbConn->errorMsg();
  491. else
  492. $return=new DatasetDb($this->dbConn, $this, $datasetName);
  493. return ($return);
  494. }
  495. }
  496. /**
  497. * Check if the Dataset with the given $datasetName is already stored in the database
  498. *
  499. * @param $datasetName string
  500. * @return boolean
  501. * @throws SqlError
  502. * @access public
  503. */
  504. function datasetExists($datasetName) {
  505. $res =& $this->dbConn->execute("SELECT COUNT(*) FROM datasets
  506. WHERE datasetName = '" .$datasetName ."'");
  507. if (!$res)
  508. echo $this->dbConn->errorMsg();
  509. else {
  510. if (!$res->fields[0])
  511. return FALSE;
  512. return TRUE;
  513. }
  514. }
  515. /**
  516. * Create a new instance of DatasetDb with the given $datasetName and
  517. * load the corresponding values from the database.
  518. * Return FALSE if the DbModel does not exist.
  519. *
  520. * @param $datasetId string
  521. * @return object DatasetDb
  522. * @access public
  523. */
  524. function &getDatasetDb($datasetName) {
  525. require_once(RDFAPI_INCLUDE_DIR . PACKAGE_DATASET);
  526. if (!$this->datasetExists($datasetName)) {
  527. return FALSE;
  528. } else {
  529. $return = new DatasetDb($this->dbConn, $this, $datasetName);
  530. return ($return);
  531. }
  532. }
  533. /**
  534. * Create a new instance of namedGraphDb with the given $modelURI and graphName and
  535. * load the corresponding values of modelID and baseURI from the database.
  536. * Return FALSE if the DbModel does not exist.
  537. *
  538. * @param $modelURI string
  539. * @param $graphName string
  540. * @return object NamedGraphMem
  541. * @access public
  542. */
  543. function getNamedGraphDb($modelURI, $graphName)
  544. {
  545. require_once(RDFAPI_INCLUDE_DIR . PACKAGE_DATASET);
  546. if (!$this->modelExists($modelURI))
  547. return FALSE;
  548. else {
  549. $modelVars =& $this->dbConn->execute("SELECT modelURI, modelID, baseURI
  550. FROM models
  551. WHERE modelURI='" .$modelURI ."'");
  552. return new NamedGraphDb($this->dbConn, $modelVars->fields[0],
  553. $modelVars->fields[1], $graphName ,$modelVars->fields[2]);
  554. }
  555. }
  556. /**
  557. * Create a new instance of namedGraphDb with the given $modelURI and graphName
  558. * and insert the DbModel variables into the database (not the graphName. This
  559. * is only stored persistently, when added to dataset).
  560. * Return FALSE if there is already a model with the given URI.
  561. *
  562. * @param $modelURI string
  563. * @param $graphName string
  564. * @param $baseURI string
  565. * @return object namedGraphDb
  566. * @throws SqlError
  567. * @access public
  568. */
  569. function getNewNamedGraphDb($modelURI, $graphName, $baseURI=NULL) {
  570. if ($this->modelExists($modelURI))
  571. return FALSE;
  572. else {
  573. $modelID = $this->_createUniqueModelID();
  574. $rs =& $this->dbConn->execute("INSERT INTO models
  575. (modelID, modelURI, baseURI)
  576. VALUES ('" .$modelID ."',
  577. '" .$modelURI ."',
  578. '" .$baseURI ."')");
  579. if (!$rs)
  580. $this->dbConn->errorMsg();
  581. else
  582. return new NamedGraphDb($this->dbConn, $modelURI, $modelID, $graphName, $baseURI);
  583. }
  584. }
  585. /**
  586. * Removes the graph with all statements from the database.
  587. * Warning: A single namedGraph can be added to several datasets. So it'll be
  588. * removed from all datasets.
  589. *
  590. * @param $modelURI string
  591. * @return boolean
  592. * @throws SqlError
  593. * @access public
  594. */
  595. function removeNamedGraphDb($modelURI)
  596. {
  597. if (!$this->modelExists($modelURI))
  598. return FALSE;
  599. $modelID = $this->dbConn->GetOne("SELECT modelID FROM models WHERE modelURI='".$modelURI."'");
  600. $this->dbConn->execute("DELETE FROM models WHERE modelID=".$modelID);
  601. $this->dbConn->execute("DELETE FROM dataset_model WHERE modelId=".$modelID);
  602. $this->dbConn->execute("DELETE FROM statements WHERE modelID=".$modelID);
  603. return true;
  604. }
  605. /**
  606. * Performs a SPARQL query against a model. The model is converted to
  607. * an RDF Dataset. The result can be retrived in SPARQL Query Results XML Format or
  608. * as an array containing the variables an their bindings.
  609. *
  610. * @param string $query Sparql query string
  611. * @param mixed $arModelIds Array of modelIDs, or NULL to use all models
  612. * @param string $resultform Result form ('xml' for SPARQL Query Results XML Format)
  613. * @return string/array
  614. */
  615. function sparqlQuery($query, $arModelIds = null, $resultform = false)
  616. {
  617. $engine = $this->_prepareSparql($arModelIds);
  618. return $engine->queryModel(
  619. null,
  620. $this->_parseSparqlQuery($query),
  621. $resultform
  622. );
  623. }//function sparqlQuery($query,$resultform = false)
  624. /**
  625. * Prepares everything for SparqlEngine-usage
  626. * Loads the files, creates instances for SparqlEngine and
  627. * Dataset...
  628. *
  629. * @return SparqlEngineDb
  630. */
  631. function _prepareSparql($arModelIds)
  632. {
  633. require_once RDFAPI_INCLUDE_DIR . 'sparql/SparqlEngineDb.php';
  634. return new SparqlEngineDb($this, $arModelIds);
  635. }//function _prepareSparql()
  636. /**
  637. * Parses an query and returns the parsed form.
  638. * If the query is not a string but a Query object,
  639. * it will just be returned.
  640. *
  641. * @param $query mixed String or Query object
  642. * @return Query query object
  643. * @throws Exception If $query is no string and no Query object
  644. */
  645. function _parseSparqlQuery($query)
  646. {
  647. if ($this->queryParser === null) {
  648. require_once RDFAPI_INCLUDE_DIR . 'sparql/SparqlParser.php';
  649. $this->queryParser = new SparqlParser();
  650. }
  651. return $this->queryParser->parse($query);
  652. }//function _parseSparqlQuery($query)
  653. } // end: Class DbStore
  654. ?>