[12] | 1 | <?php
|
---|
| 2 | require_once RDFAPI_INCLUDE_DIR . 'constants.php';
|
---|
| 3 | require_once RDFAPI_INCLUDE_DIR . 'util/Object.php';
|
---|
| 4 |
|
---|
| 5 | // ----------------------------------------------------------------------------------
|
---|
| 6 | // Class: DbStore
|
---|
| 7 | // ----------------------------------------------------------------------------------
|
---|
| 8 |
|
---|
| 9 | /**
|
---|
| 10 | * DbStore is a persistent store of RDF data using relational database technology.
|
---|
| 11 | * DbStore uses ADOdb Library for PHP V3.60 (http://php.weblogs.com/ADODB),
|
---|
| 12 | * which allows to connect to multiple databases in a portable manner.
|
---|
| 13 | * This class also provides methods for creating tables for MsAccess, MySQL, and MS SQL Server.
|
---|
| 14 | * If you want to use other databases, you will have to create tables by yourself
|
---|
| 15 | * according to the abstract database schema described in the API documentation.
|
---|
| 16 | *
|
---|
| 17 | * You can activate debug mode by defining ADODB_DEBUG_MODE to "1".
|
---|
| 18 | *
|
---|
| 19 | *
|
---|
| 20 | * @version $Id: DbStore.php 560 2008-02-29 15:24:20Z cax $
|
---|
| 21 | * @author Radoslaw Oldakowski <radol@gmx.de>
|
---|
| 22 | * @author Daniel Westphal (http://www.d-westphal.de)
|
---|
| 23 | *
|
---|
| 24 | * @package model
|
---|
| 25 | * @access public
|
---|
| 26 | */
|
---|
| 27 |
|
---|
| 28 |
|
---|
| 29 | class DbStore extends Object
|
---|
| 30 | {
|
---|
| 31 | /**
|
---|
| 32 | * Array with all supported database types
|
---|
| 33 | *
|
---|
| 34 | * @var array
|
---|
| 35 | */
|
---|
| 36 | public static $arSupportedDbTypes = array(
|
---|
| 37 | "MySQL",
|
---|
| 38 | "MySQLi",
|
---|
| 39 | "MSSQL",
|
---|
| 40 | 'MsAccess'
|
---|
| 41 | );
|
---|
| 42 |
|
---|
| 43 | /**
|
---|
| 44 | * Database connection object
|
---|
| 45 | *
|
---|
| 46 | * @var object ADOConnection
|
---|
| 47 | * @access private
|
---|
| 48 | */
|
---|
| 49 | var $dbConn;
|
---|
| 50 |
|
---|
| 51 | /**
|
---|
| 52 | * Database driver name
|
---|
| 53 | *
|
---|
| 54 | * @var string
|
---|
| 55 | */
|
---|
| 56 | protected $driver = null;
|
---|
| 57 |
|
---|
| 58 | /**
|
---|
| 59 | * SparqlParser so we can re-use it
|
---|
| 60 | * @var Parser
|
---|
| 61 | */
|
---|
| 62 | var $queryParser = null;
|
---|
| 63 |
|
---|
| 64 |
|
---|
| 65 | /**
|
---|
| 66 | * Constructor:
|
---|
| 67 | * Set the database connection with the given parameters.
|
---|
| 68 | *
|
---|
| 69 | * @param string $dbDriver
|
---|
| 70 | * @param string $host
|
---|
| 71 | * @param string $dbName
|
---|
| 72 | * @param string $user
|
---|
| 73 | * @param string $password
|
---|
| 74 | * @access public
|
---|
| 75 | */
|
---|
| 76 | function DbStore ($dbDriver=ADODB_DB_DRIVER, $host=ADODB_DB_HOST, $dbName=ADODB_DB_NAME,
|
---|
| 77 | $user=ADODB_DB_USER, $password=ADODB_DB_PASSWORD) {
|
---|
| 78 |
|
---|
| 79 | // include DBase Package
|
---|
| 80 | require_once(RDFAPI_INCLUDE_DIR.PACKAGE_DBASE);
|
---|
| 81 |
|
---|
| 82 | // create a new connection object
|
---|
| 83 | $this->dbConn =& ADONewConnection($dbDriver);
|
---|
| 84 | $this->driver = $dbDriver;
|
---|
| 85 |
|
---|
| 86 | //activate the ADOdb DEBUG mode
|
---|
| 87 | if (ADODB_DEBUG_MODE == '1')
|
---|
| 88 | $this->dbConn->debug = true;
|
---|
| 89 |
|
---|
| 90 | // connect to database
|
---|
| 91 | $r = $this->dbConn->NConnect($host, $user, $password, $dbName);
|
---|
| 92 | if ($r !== true) {
|
---|
| 93 | throw new Exception('Could not connect to database');
|
---|
| 94 | }
|
---|
| 95 |
|
---|
| 96 | // optimized for speed
|
---|
| 97 | $this->dbConn->setFetchMode(ADODB_FETCH_NUM);
|
---|
| 98 | //$ADODB_COUNTRECS = FALSE;
|
---|
| 99 | }
|
---|
| 100 |
|
---|
| 101 |
|
---|
| 102 |
|
---|
| 103 | /**
|
---|
| 104 | * List all DbModels stored in the database.
|
---|
| 105 | *
|
---|
| 106 | * @return array
|
---|
| 107 | * @throws SqlError
|
---|
| 108 | * @access public
|
---|
| 109 | */
|
---|
| 110 | function listModels() {
|
---|
| 111 |
|
---|
| 112 | $recordSet =& $this->dbConn->execute("SELECT modelURI, baseURI
|
---|
| 113 | FROM models");
|
---|
| 114 | if (!$recordSet)
|
---|
| 115 | echo $this->dbConn->errorMsg();
|
---|
| 116 | else {
|
---|
| 117 | $models = array();
|
---|
| 118 | $i=0;
|
---|
| 119 | while (!$recordSet->EOF) {
|
---|
| 120 |
|
---|
| 121 | $models[$i]['modelURI'] = $recordSet->fields[0];
|
---|
| 122 | $models[$i]['baseURI'] = $recordSet->fields[1];
|
---|
| 123 |
|
---|
| 124 | ++$i;
|
---|
| 125 | $recordSet->moveNext();
|
---|
| 126 | }
|
---|
| 127 | return $models;
|
---|
| 128 | }
|
---|
| 129 | }
|
---|
| 130 |
|
---|
| 131 |
|
---|
| 132 | /**
|
---|
| 133 | * Check if the DbModel with the given modelURI is already stored in the database
|
---|
| 134 | *
|
---|
| 135 | * @param string $modelURI
|
---|
| 136 | * @return boolean
|
---|
| 137 | * @throws SqlError
|
---|
| 138 | * @access public
|
---|
| 139 | */
|
---|
| 140 | function modelExists($modelURI) {
|
---|
| 141 |
|
---|
| 142 | $res =& $this->dbConn->execute("SELECT COUNT(*) FROM models
|
---|
| 143 | WHERE modelURI = '" .$modelURI ."'");
|
---|
| 144 | if (!$res)
|
---|
| 145 | echo $this->dbConn->errorMsg();
|
---|
| 146 | else {
|
---|
| 147 | if (!$res->fields[0]) {
|
---|
| 148 | $res->Close();
|
---|
| 149 | return FALSE;
|
---|
| 150 | } else {
|
---|
| 151 | $res->Close();
|
---|
| 152 | return TRUE;
|
---|
| 153 | }
|
---|
| 154 | }
|
---|
| 155 | }
|
---|
| 156 |
|
---|
| 157 |
|
---|
| 158 |
|
---|
| 159 | /**
|
---|
| 160 | * Returns the database connection object
|
---|
| 161 | *
|
---|
| 162 | * @return ADOdb Database object
|
---|
| 163 | * @access public
|
---|
| 164 | */
|
---|
| 165 | function &getDbConn()
|
---|
| 166 | {
|
---|
| 167 | return $this->dbConn;
|
---|
| 168 | }
|
---|
| 169 |
|
---|
| 170 |
|
---|
| 171 | /**
|
---|
| 172 | * Create a new instance of DbModel with the given $modelURI and
|
---|
| 173 | * load the corresponding values of modelID and baseURI from the database.
|
---|
| 174 | * Return FALSE if the DbModel does not exist.
|
---|
| 175 | *
|
---|
| 176 | * @param string $modelURI
|
---|
| 177 | * @return object DbModel
|
---|
| 178 | * @access public
|
---|
| 179 | */
|
---|
| 180 | function getModel($modelURI) {
|
---|
| 181 |
|
---|
| 182 | if (!$this->modelExists($modelURI))
|
---|
| 183 | return FALSE;
|
---|
| 184 | else {
|
---|
| 185 | $modelVars =& $this->dbConn->execute("SELECT modelURI, modelID, baseURI
|
---|
| 186 | FROM models
|
---|
| 187 | WHERE modelURI='" .$modelURI ."'");
|
---|
| 188 |
|
---|
| 189 | return new DbModel($this->dbConn, $modelVars->fields[0],
|
---|
| 190 | $modelVars->fields[1], $modelVars->fields[2]);
|
---|
| 191 | }
|
---|
| 192 | }
|
---|
| 193 |
|
---|
| 194 |
|
---|
| 195 | /**
|
---|
| 196 | * Create a new instance of DbModel with the given $modelURI
|
---|
| 197 | * and insert the DbModel variables into the database.
|
---|
| 198 | * Return FALSE if there is already a model with the given URI.
|
---|
| 199 | *
|
---|
| 200 | * @param string $modelURI
|
---|
| 201 | * @param string $baseURI
|
---|
| 202 | * @return object DbModel
|
---|
| 203 | * @throws SqlError
|
---|
| 204 | * @access public
|
---|
| 205 | */
|
---|
| 206 | function getNewModel($modelURI, $baseURI=NULL) {
|
---|
| 207 |
|
---|
| 208 | if ($this->modelExists($modelURI))
|
---|
| 209 | return FALSE;
|
---|
| 210 | else {
|
---|
| 211 | $modelID = $this->_createUniqueModelID();
|
---|
| 212 |
|
---|
| 213 | $rs =& $this->dbConn->execute("INSERT INTO models
|
---|
| 214 | (modelID, modelURI, baseURI)
|
---|
| 215 | VALUES (" .$modelID .",
|
---|
| 216 | " .$this->dbConn->qstr($modelURI) .",
|
---|
| 217 | " .$this->dbConn->qstr($baseURI) .")");
|
---|
| 218 |
|
---|
| 219 | if (!$rs)
|
---|
| 220 | return $this->dbConn->errorMsg();
|
---|
| 221 | else
|
---|
| 222 | return new DbModel($this->dbConn, $modelURI, $modelID, $baseURI);
|
---|
| 223 | }
|
---|
| 224 | }
|
---|
| 225 |
|
---|
| 226 |
|
---|
| 227 | /**
|
---|
| 228 | * Store a MemModel or another DbModel from a different DbStore in the database.
|
---|
| 229 | * Return FALSE if there is already a model with modelURI matching the modelURI
|
---|
| 230 | * of the given model.
|
---|
| 231 | *
|
---|
| 232 | * @param object Model &$model
|
---|
| 233 | * @param string $modelURI
|
---|
| 234 | * @return boolean
|
---|
| 235 | * @access public
|
---|
| 236 | */
|
---|
| 237 | function putModel(&$model, $modelURI=NULL) {
|
---|
| 238 |
|
---|
| 239 | if (!$modelURI) {
|
---|
| 240 | if (is_a($model, 'MemModel'))
|
---|
| 241 | $modelURI = 'DbModel-' .$this->_createUniqueModelID();
|
---|
| 242 | else
|
---|
| 243 | $modelURI = $model->modelURI;
|
---|
| 244 | }else
|
---|
| 245 | if ($this->modelExists($modelURI))
|
---|
| 246 | return FALSE;
|
---|
| 247 |
|
---|
| 248 |
|
---|
| 249 | $newDbModel = $this->getNewModel($modelURI, $model->getBaseURI());
|
---|
| 250 | $newDbModel->addModel($model);
|
---|
| 251 | }
|
---|
| 252 |
|
---|
| 253 |
|
---|
| 254 | /**
|
---|
| 255 | * Close the DbStore.
|
---|
| 256 | * !!! Warning: If you close the DbStore all active instances of DbModel from this
|
---|
| 257 | * !!! DbStore will lose their database connection !!!
|
---|
| 258 | *
|
---|
| 259 | * @access public
|
---|
| 260 | */
|
---|
| 261 | function close() {
|
---|
| 262 |
|
---|
| 263 | $this->dbConn->close();
|
---|
| 264 | unset($this);
|
---|
| 265 | }
|
---|
| 266 |
|
---|
| 267 |
|
---|
| 268 | // =============================================================================
|
---|
| 269 | // **************************** private methods ********************************
|
---|
| 270 | // =============================================================================
|
---|
| 271 |
|
---|
| 272 |
|
---|
| 273 | /**
|
---|
| 274 | * Create a unique ID for the DbModel to be insert into the models table.
|
---|
| 275 | * This method was implemented because some databases do not support auto-increment.
|
---|
| 276 | *
|
---|
| 277 | * @return integer
|
---|
| 278 | * @access private
|
---|
| 279 | */
|
---|
| 280 | function _createUniqueModelID() {
|
---|
| 281 |
|
---|
| 282 | $maxModelID =& $this->dbConn->GetOne('SELECT MAX(modelID) FROM models');
|
---|
| 283 | return ++$maxModelID;
|
---|
| 284 | }
|
---|
| 285 |
|
---|
| 286 | /**
|
---|
| 287 | * Create a unique ID for the dataset to be insert into the datasets table.
|
---|
| 288 | * This method was implemented because some databases do not support auto-increment.
|
---|
| 289 | *
|
---|
| 290 | * @return integer
|
---|
| 291 | * @access private
|
---|
| 292 | */
|
---|
| 293 | function _createUniqueDatasetID() {
|
---|
| 294 |
|
---|
| 295 | $maxDatasetID =& $this->dbConn->GetOne('SELECT MAX(datasetId) FROM datasets');
|
---|
| 296 | return ++$maxDatasetID;
|
---|
| 297 | }
|
---|
| 298 |
|
---|
| 299 |
|
---|
| 300 |
|
---|
| 301 | /**
|
---|
| 302 | * Sets up tables for RAP.
|
---|
| 303 | * DOES NOT CHECK IF TABLES ALREADY EXIST
|
---|
| 304 | *
|
---|
| 305 | * @param string $databaseType Database driver name (e.g. MySQL)
|
---|
| 306 | *
|
---|
| 307 | * @throws Exception If database type is unsupported
|
---|
| 308 | * @access public
|
---|
| 309 | **/
|
---|
| 310 | public function createTables($databaseType = null)
|
---|
| 311 | {
|
---|
| 312 | $driver = $this->getDriver($databaseType);
|
---|
| 313 | self::assertDriverSupported($driver);
|
---|
| 314 |
|
---|
| 315 | $createFunc = '_createTables_' . $driver;
|
---|
| 316 | return $this->$createFunc();
|
---|
| 317 | }//public function createTables($databaseType="MySQL")
|
---|
| 318 |
|
---|
| 319 |
|
---|
| 320 |
|
---|
| 321 | /**
|
---|
| 322 | * Create tables and indexes for MsAccess database
|
---|
| 323 | *
|
---|
| 324 | * @return boolean true If all is ok
|
---|
| 325 | *
|
---|
| 326 | * @throws Exception
|
---|
| 327 | */
|
---|
| 328 | protected function _createTables_MsAccess()
|
---|
| 329 | {
|
---|
| 330 | $this->dbConn->startTrans();
|
---|
| 331 |
|
---|
| 332 | $this->dbConn->execute('CREATE TABLE models
|
---|
| 333 | (modelID long primary key,
|
---|
| 334 | modelURI varchar not null,
|
---|
| 335 | baseURI varchar)');
|
---|
| 336 |
|
---|
| 337 | $this->dbConn->execute('CREATE UNIQUE INDEX m_modURI_idx ON models (modelURI)');
|
---|
| 338 |
|
---|
| 339 | $this->dbConn->execute('CREATE TABLE statements
|
---|
| 340 | (modelID long,
|
---|
| 341 | subject varchar,
|
---|
| 342 | predicate varchar,
|
---|
| 343 | object Memo,
|
---|
| 344 | l_language varchar,
|
---|
| 345 | l_datatype varchar,
|
---|
| 346 | subject_is varchar(1),
|
---|
| 347 | object_is varchar(1),
|
---|
| 348 | primary key (modelID, subject, predicate, object,
|
---|
| 349 | l_language, l_datatype))');
|
---|
| 350 |
|
---|
| 351 | $this->dbConn->execute('CREATE INDEX s_mod_idx ON statements (modelID)');
|
---|
| 352 | $this->dbConn->execute('CREATE INDEX s_sub_idx ON statements (subject)');
|
---|
| 353 | $this->dbConn->execute('CREATE INDEX s_pred_idx ON statements (predicate)');
|
---|
| 354 | $this->dbConn->execute('CREATE INDEX s_obj_idx ON statements (object)');
|
---|
| 355 |
|
---|
| 356 | $this->dbConn->execute('CREATE TABLE namespaces
|
---|
| 357 | (modelID long,
|
---|
| 358 | namespace varchar,
|
---|
| 359 | prefix varchar,
|
---|
| 360 | primary key (modelID, namespace, prefix))');
|
---|
| 361 |
|
---|
| 362 | $this->dbConn->execute('CREATE INDEX n_name_idx ON namespaces (namespace)');
|
---|
| 363 | $this->dbConn->execute('CREATE INDEX n_pref_idx ON namespaces (prefix)');
|
---|
| 364 |
|
---|
| 365 | $this->dbConn->execute("CREATE TABLE datasets
|
---|
| 366 | (datasetName varchar,
|
---|
| 367 | defaultModelUri varchar,
|
---|
| 368 | primary key (datasetName))");
|
---|
| 369 |
|
---|
| 370 | $this->dbConn->execute('CREATE INDEX nGS_idx1 ON datasets (datasetName)');
|
---|
| 371 |
|
---|
| 372 |
|
---|
| 373 | $this->dbConn->execute("CREATE TABLE `dataset_model` (
|
---|
| 374 | datasetName varchar,
|
---|
| 375 | modelId long,
|
---|
| 376 | graphURI varchar,
|
---|
| 377 | PRIMARY KEY (modelId,datasetName))");
|
---|
| 378 |
|
---|
| 379 |
|
---|
| 380 | if (!$this->dbConn->completeTrans()) {
|
---|
| 381 | throw new Exception($this->dbConn->errorMsg());
|
---|
| 382 | }
|
---|
| 383 | return true;
|
---|
| 384 | }
|
---|
| 385 |
|
---|
| 386 |
|
---|
| 387 |
|
---|
| 388 | /**
|
---|
| 389 | * Create tables and indexes for MySQL database
|
---|
| 390 | *
|
---|
| 391 | * @return boolean true If all is ok
|
---|
| 392 | *
|
---|
| 393 | * @throws Exception
|
---|
| 394 | */
|
---|
| 395 | function _createTables_MySQL()
|
---|
| 396 | {
|
---|
| 397 |
|
---|
| 398 | $this->dbConn->startTrans();
|
---|
| 399 |
|
---|
| 400 | $this->dbConn->execute("CREATE TABLE models
|
---|
| 401 | (modelID bigint NOT NULL,
|
---|
| 402 | modelURI varchar(255) NOT NULL,
|
---|
| 403 | baseURI varchar(255) DEFAULT '',
|
---|
| 404 | primary key (modelID))");
|
---|
| 405 |
|
---|
| 406 | $this->dbConn->execute('CREATE UNIQUE INDEX m_modURI_idx ON models (modelURI)');
|
---|
| 407 |
|
---|
| 408 | $this->dbConn->execute("CREATE TABLE statements
|
---|
| 409 | (modelID bigint NOT NULL,
|
---|
| 410 | subject varchar(255) NOT NULL,
|
---|
| 411 | predicate varchar(255) NOT NULL,
|
---|
| 412 | object text,
|
---|
| 413 | l_language varchar(255) DEFAULT '',
|
---|
| 414 | l_datatype varchar(255) DEFAULT '',
|
---|
| 415 | subject_is varchar(1) NOT NULL,
|
---|
| 416 | object_is varchar(1) NOT NULL)");
|
---|
| 417 |
|
---|
| 418 | $this->dbConn->execute("CREATE TABLE namespaces
|
---|
| 419 | (modelID bigint NOT NULL,
|
---|
| 420 | namespace varchar(255) NOT NULL,
|
---|
| 421 | prefix varchar(255) NOT NULL,
|
---|
| 422 | primary key (modelID,namespace))");
|
---|
| 423 |
|
---|
| 424 | $this->dbConn->execute("CREATE TABLE `dataset_model` (
|
---|
| 425 | `datasetName` varchar(255) NOT NULL default '0',
|
---|
| 426 | `modelId` bigint(20) NOT NULL default '0',
|
---|
| 427 | `graphURI` varchar(255) NOT NULL default '',
|
---|
| 428 | PRIMARY KEY (`modelId`,`datasetName`))");
|
---|
| 429 |
|
---|
| 430 | $this->dbConn->execute("CREATE TABLE `datasets` (
|
---|
| 431 | `datasetName` varchar(255) NOT NULL default '',
|
---|
| 432 | `defaultModelUri` varchar(255) NOT NULL default '0',
|
---|
| 433 | PRIMARY KEY (`datasetName`),
|
---|
| 434 | KEY `datasetName` (`datasetName`))");
|
---|
| 435 |
|
---|
| 436 | $this->dbConn->execute('CREATE INDEX s_mod_idx ON statements (modelID)');
|
---|
| 437 | $this->dbConn->execute('CREATE INDEX n_mod_idx ON namespaces (modelID)');
|
---|
| 438 |
|
---|
| 439 | $this->dbConn->execute('CREATE INDEX s_sub_pred_idx ON statements
|
---|
| 440 | (subject(200),predicate(200))');
|
---|
| 441 |
|
---|
| 442 | $this->dbConn->execute('CREATE INDEX s_sub_idx ON statements (subject(200))');
|
---|
| 443 | $this->dbConn->execute('CREATE INDEX s_pred_idx ON statements (predicate(200))');
|
---|
| 444 | $this->dbConn->execute('CREATE INDEX s_obj_idx ON statements (object(250))');
|
---|
| 445 |
|
---|
| 446 | $this->dbConn->execute('CREATE FULLTEXT INDEX s_obj_ftidx ON statements (object)');
|
---|
| 447 |
|
---|
| 448 | if (!$this->dbConn->completeTrans()) {
|
---|
| 449 | throw new Exception($this->dbConn->errorMsg());
|
---|
| 450 | }
|
---|
| 451 | return true;
|
---|
| 452 | }
|
---|
| 453 |
|
---|
| 454 |
|
---|
| 455 |
|
---|
| 456 | /**
|
---|
| 457 | * Creates tables on a MySQLi database
|
---|
| 458 | */
|
---|
| 459 | function _createTables_MySQLi()
|
---|
| 460 | {
|
---|
| 461 | return $this->_createTables_MySQL();
|
---|
| 462 | }//function _createTables_MySQLi()
|
---|
| 463 |
|
---|
| 464 |
|
---|
| 465 |
|
---|
| 466 | /**
|
---|
| 467 | * Create tables and indexes for MSSQL database
|
---|
| 468 | *
|
---|
| 469 | * @return boolean true If all is ok
|
---|
| 470 | *
|
---|
| 471 | * @throws Exception
|
---|
| 472 | */
|
---|
| 473 | function _createTables_MSSQL()
|
---|
| 474 | {
|
---|
| 475 | $this->dbConn->startTrans();
|
---|
| 476 |
|
---|
| 477 | $this->dbConn->execute("CREATE TABLE [dbo].[models] (
|
---|
| 478 | [modelID] [int] NOT NULL ,
|
---|
| 479 | [modelURI] [nvarchar] (200) COLLATE SQL_Latin1_General_CP1_CI_AS NULL ,
|
---|
| 480 | [baseURI] [nvarchar] (200) COLLATE SQL_Latin1_General_CP1_CI_AS NULL
|
---|
| 481 | ) ON [PRIMARY]");
|
---|
| 482 |
|
---|
| 483 | $this->dbConn->execute("CREATE TABLE [dbo].[statements] (
|
---|
| 484 | [modelID] [int] NOT NULL ,
|
---|
| 485 | [subject] [nvarchar] (200) COLLATE SQL_Latin1_General_CP1_CI_AS NULL ,
|
---|
| 486 | [predicate] [nvarchar] (200) COLLATE SQL_Latin1_General_CP1_CI_AS NULL ,
|
---|
| 487 | [object] [text] COLLATE SQL_Latin1_General_CP1_CI_AS NULL ,
|
---|
| 488 | [l_language] [nvarchar] (50) COLLATE SQL_Latin1_General_CP1_CI_AS NULL ,
|
---|
| 489 | [l_datatype] [nvarchar] (50) COLLATE SQL_Latin1_General_CP1_CI_AS NULL ,
|
---|
| 490 | [subject_is] [nchar] (1) COLLATE SQL_Latin1_General_CP1_CI_AS NULL ,
|
---|
| 491 | [object_is] [nchar] (1) COLLATE SQL_Latin1_General_CP1_CI_AS NULL
|
---|
| 492 | ) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]");
|
---|
| 493 |
|
---|
| 494 |
|
---|
| 495 | $this->dbConn->execute("CREATE TABLE [dbo].[namespaces] (
|
---|
| 496 | [modelID] [int] NOT NULL ,
|
---|
| 497 | [namespace] [nvarchar] (200) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL ,
|
---|
| 498 | [prefix] [nvarchar] (200) COLLATE SQL_Latin1_General_CP1_CI_AS NULL ,
|
---|
| 499 | ) ON [PRIMARY]");
|
---|
| 500 |
|
---|
| 501 | $this->dbConn->execute("ALTER TABLE [dbo].[models] WITH NOCHECK ADD
|
---|
| 502 | CONSTRAINT [PK_models] PRIMARY KEY CLUSTERED
|
---|
| 503 | (
|
---|
| 504 | [modelID]
|
---|
| 505 | ) ON [PRIMARY] ");
|
---|
| 506 | $this->dbConn->execute("ALTER TABLE [dbo].[namespaces] WITH NOCHECK ADD
|
---|
| 507 | CONSTRAINT [PK_namespaces] PRIMARY KEY CLUSTERED
|
---|
| 508 | (
|
---|
| 509 | [modelID],[namespace]
|
---|
| 510 | ) ON [PRIMARY] ");
|
---|
| 511 |
|
---|
| 512 | $this->dbConn->execute("CREATE INDEX [joint index on subject and predicate] ON [dbo].[statements]([subject], [predicate]) ON [PRIMARY]");
|
---|
| 513 |
|
---|
| 514 |
|
---|
| 515 | if (!$this->dbConn->completeTrans()) {
|
---|
| 516 | throw new Exception($this->dbConn->errorMsg());
|
---|
| 517 | }
|
---|
| 518 | return true;
|
---|
| 519 | }
|
---|
| 520 |
|
---|
| 521 |
|
---|
| 522 |
|
---|
| 523 | /**
|
---|
| 524 | * Checks if tables are setup for RAP
|
---|
| 525 | *
|
---|
| 526 | * @param string $databaseType
|
---|
| 527 | * @throws Exception If database type is unsupported
|
---|
| 528 | * @access public
|
---|
| 529 | **/
|
---|
| 530 | public function isSetup($databaseType = null)
|
---|
| 531 | {
|
---|
| 532 | $driver = $this->getDriver($databaseType);
|
---|
| 533 | self::assertDriverSupported($driver);
|
---|
| 534 |
|
---|
| 535 | $issetupFunc = '_isSetup_' . $driver;
|
---|
| 536 | return $this->$issetupFunc();
|
---|
| 537 | }//public function isSetup($databaseType="MySQL")
|
---|
| 538 |
|
---|
| 539 |
|
---|
| 540 |
|
---|
| 541 | /**
|
---|
| 542 | * Returns the driver for the database type.
|
---|
| 543 | * You can pass NULL or omit the parameter to
|
---|
| 544 | * use the parameter from the dbstore constructor
|
---|
| 545 | *
|
---|
| 546 | * @param string $databaseType Database driver name (e.g. MySQL)
|
---|
| 547 | *
|
---|
| 548 | * @return string Database driver string (e.g. MySQL)
|
---|
| 549 | */
|
---|
| 550 | public function getDriver($databaseType = null)
|
---|
| 551 | {
|
---|
| 552 | if ($databaseType === null) {
|
---|
| 553 | if ($this->driver === null) {
|
---|
| 554 | //backward compatibility
|
---|
| 555 | $databaseType = 'MySQL';
|
---|
| 556 | } else {
|
---|
| 557 | $databaseType = $this->driver;
|
---|
| 558 | }
|
---|
| 559 | }
|
---|
| 560 | if (!self::isDriverSupported($databaseType)) {
|
---|
| 561 | //check if it is a known driver in wrong case
|
---|
| 562 | $arLowercases = array_map('strtolower', self::$arSupportedDbTypes);
|
---|
| 563 | $arMapping = array_combine($arLowercases, self::$arSupportedDbTypes);
|
---|
| 564 | if (isset($arMapping[strtolower($databaseType)])) {
|
---|
| 565 | $databaseType = $arMapping[strtolower($databaseType)];
|
---|
| 566 | }
|
---|
| 567 | }
|
---|
| 568 | return $databaseType;
|
---|
| 569 | }//public function getDriver($databaseType = null)
|
---|
| 570 |
|
---|
| 571 |
|
---|
| 572 |
|
---|
| 573 | /**
|
---|
| 574 | * Returns if the given driver is supported
|
---|
| 575 | *
|
---|
| 576 | * @return boolean True if it supported, false if not
|
---|
| 577 | */
|
---|
| 578 | public static function isDriverSupported($databaseType)
|
---|
| 579 | {
|
---|
| 580 | return in_array($databaseType, self::$arSupportedDbTypes);
|
---|
| 581 | }//public static function isDriverSupported($databaseType)
|
---|
| 582 |
|
---|
| 583 |
|
---|
| 584 |
|
---|
| 585 | /**
|
---|
| 586 | * Checks if the given driver is supported and throws an
|
---|
| 587 | * Exception if not.
|
---|
| 588 | *
|
---|
| 589 | * @param string $databaseType Database driver name (e.g. MySQL)
|
---|
| 590 | *
|
---|
| 591 | * @return true If it does not fail
|
---|
| 592 | *
|
---|
| 593 | * @throws Exception If the driver is not supported
|
---|
| 594 | */
|
---|
| 595 | public static function assertDriverSupported($databaseType)
|
---|
| 596 | {
|
---|
| 597 | if (!self::isDriverSupported($databaseType)) {
|
---|
| 598 | throw new Exception(
|
---|
| 599 | 'Unsupported database type, only supported: '
|
---|
| 600 | . implode(', ', self::$arSupportedDbTypes)
|
---|
| 601 | );
|
---|
| 602 | }
|
---|
| 603 | return true;
|
---|
| 604 | }//public static function assertDriverSupported($databaseType)
|
---|
| 605 |
|
---|
| 606 |
|
---|
| 607 |
|
---|
| 608 | /**
|
---|
| 609 | * Checks if tables are setup for RAP (MySql)
|
---|
| 610 | *
|
---|
| 611 | * @throws SqlError
|
---|
| 612 | * @access private
|
---|
| 613 | **/
|
---|
| 614 | function _isSetup_MySQL()
|
---|
| 615 | {
|
---|
| 616 | $recordSet =& $this->dbConn->execute("SHOW TABLES");
|
---|
| 617 | if (!$recordSet) {
|
---|
| 618 | throw new Exception($this->dbConn->errorMsg());
|
---|
| 619 | } else {
|
---|
| 620 | $tables = array();
|
---|
| 621 | while (!$recordSet->EOF) {
|
---|
| 622 | $tables[]= $recordSet->fields[0];
|
---|
| 623 | if (isset($i)) {
|
---|
| 624 | ++$i;
|
---|
| 625 | }
|
---|
| 626 | $recordSet->moveNext();
|
---|
| 627 | }
|
---|
| 628 | if (in_array("models",$tables) && in_array("statements",$tables)
|
---|
| 629 | && in_array("namespaces",$tables)) {
|
---|
| 630 | return true;
|
---|
| 631 | }
|
---|
| 632 | }
|
---|
| 633 | return false;
|
---|
| 634 | }//function _isSetup_MySQL()
|
---|
| 635 |
|
---|
| 636 |
|
---|
| 637 |
|
---|
| 638 | /**
|
---|
| 639 | * Checks if tables are setup for RAP (MySQLi)
|
---|
| 640 | *
|
---|
| 641 | * @see _isSetup_MySQL()
|
---|
| 642 | */
|
---|
| 643 | function _isSetup_MySQLi()
|
---|
| 644 | {
|
---|
| 645 | return $this->_isSetup_MySQL();
|
---|
| 646 | }//function _isSetup_MySQLi()
|
---|
| 647 |
|
---|
| 648 |
|
---|
| 649 |
|
---|
| 650 | /**
|
---|
| 651 | * Checks if tables are setup for RAP (MsAccess)
|
---|
| 652 | *
|
---|
| 653 | * @throws SqlError
|
---|
| 654 | * @access private
|
---|
| 655 | **/
|
---|
| 656 | function _isSetup_MsAccess()
|
---|
| 657 | {
|
---|
| 658 | $tables =& $this->dbConn->MetaTables();
|
---|
| 659 | if (!$tables) {
|
---|
| 660 | throw new Exception($this->dbConn->errorMsg());
|
---|
| 661 | }
|
---|
| 662 | if (count($tables) == 0) {
|
---|
| 663 | return false;
|
---|
| 664 | } else {
|
---|
| 665 | if (in_array("models",$tables) && in_array("statements",$tables)
|
---|
| 666 | && in_array("namespaces",$tables)) {
|
---|
| 667 | return true;
|
---|
| 668 | } else {
|
---|
| 669 | return false;
|
---|
| 670 | }
|
---|
| 671 | }
|
---|
| 672 | }//function _isSetup_MsAccess()
|
---|
| 673 |
|
---|
| 674 |
|
---|
| 675 |
|
---|
| 676 | /**
|
---|
| 677 | * Checks if tables are setup for RAP (MSSQL)
|
---|
| 678 | *
|
---|
| 679 | * @throws SqlError
|
---|
| 680 | * @access private
|
---|
| 681 | **/
|
---|
| 682 | function _isSetup_MSSQL()
|
---|
| 683 | {
|
---|
| 684 | $tables =& $this->dbConn->MetaTables();
|
---|
| 685 | if (!$tables) {
|
---|
| 686 | throw new Exception($this->dbConn->errorMsg());
|
---|
| 687 | }
|
---|
| 688 | if (count($tables) == 0) {
|
---|
| 689 | return false;
|
---|
| 690 | } else {
|
---|
| 691 | if (in_array("models",$tables) && in_array("statements",$tables)
|
---|
| 692 | && in_array("namespaces",$tables)){
|
---|
| 693 | return true;
|
---|
| 694 | } else {
|
---|
| 695 | return false;
|
---|
| 696 | }
|
---|
| 697 | }
|
---|
| 698 | }//function _isSetup_MSSQL()
|
---|
| 699 |
|
---|
| 700 |
|
---|
| 701 | /**
|
---|
| 702 | * Create a new instance of DatasetDb with the given $datasetName
|
---|
| 703 | * and insert the DatasetDb variables into the database.
|
---|
| 704 | * Return FALSE if there is already a model with the given URI.
|
---|
| 705 | *
|
---|
| 706 | * @param $datasetName string
|
---|
| 707 | * @return object DatasetDB
|
---|
| 708 | * @throws SqlError
|
---|
| 709 | * @access public
|
---|
| 710 | */
|
---|
| 711 | function & getNewDatasetDb($datasetName)
|
---|
| 712 | {
|
---|
| 713 |
|
---|
| 714 | require_once(RDFAPI_INCLUDE_DIR . PACKAGE_DATASET);
|
---|
| 715 |
|
---|
| 716 | if ($this->datasetExists($datasetName))
|
---|
| 717 | return FALSE;
|
---|
| 718 | else
|
---|
| 719 | {
|
---|
| 720 | $defaultModelUri=uniqid('http://rdfapi-php/dataset_defaultmodel_');
|
---|
| 721 | $defaultModel=$this->getNewModel($defaultModelUri);
|
---|
| 722 |
|
---|
| 723 | $rs =& $this->dbConn->execute("INSERT INTO datasets
|
---|
| 724 | VALUES ('" .$this->dbConn->qstr($datasetName) ."',
|
---|
| 725 | '" .$this->dbConn->qstr($defaultModelUri)."')");
|
---|
| 726 |
|
---|
| 727 | if (!$rs)
|
---|
| 728 | $this->dbConn->errorMsg();
|
---|
| 729 | else
|
---|
| 730 | $return=new DatasetDb($this->dbConn, $this, $datasetName);
|
---|
| 731 | return ($return);
|
---|
| 732 | }
|
---|
| 733 | }
|
---|
| 734 |
|
---|
| 735 | /**
|
---|
| 736 | * Check if the Dataset with the given $datasetName is already stored in the database
|
---|
| 737 | *
|
---|
| 738 | * @param $datasetName string
|
---|
| 739 | * @return boolean
|
---|
| 740 | * @throws SqlError
|
---|
| 741 | * @access public
|
---|
| 742 | */
|
---|
| 743 | function datasetExists($datasetName) {
|
---|
| 744 |
|
---|
| 745 | $res =& $this->dbConn->execute("SELECT COUNT(*) FROM datasets
|
---|
| 746 | WHERE datasetName = '" .$datasetName ."'");
|
---|
| 747 | if (!$res)
|
---|
| 748 | echo $this->dbConn->errorMsg();
|
---|
| 749 | else {
|
---|
| 750 | if (!$res->fields[0])
|
---|
| 751 | return FALSE;
|
---|
| 752 | return TRUE;
|
---|
| 753 | }
|
---|
| 754 | }
|
---|
| 755 |
|
---|
| 756 |
|
---|
| 757 | /**
|
---|
| 758 | * Create a new instance of DatasetDb with the given $datasetName and
|
---|
| 759 | * load the corresponding values from the database.
|
---|
| 760 | * Return FALSE if the DbModel does not exist.
|
---|
| 761 | *
|
---|
| 762 | * @param $datasetId string
|
---|
| 763 | * @return object DatasetDb
|
---|
| 764 | * @access public
|
---|
| 765 | */
|
---|
| 766 | function &getDatasetDb($datasetName) {
|
---|
| 767 | require_once(RDFAPI_INCLUDE_DIR . PACKAGE_DATASET);
|
---|
| 768 |
|
---|
| 769 | if (!$this->datasetExists($datasetName)) {
|
---|
| 770 | return FALSE;
|
---|
| 771 | } else {
|
---|
| 772 | $return = new DatasetDb($this->dbConn, $this, $datasetName);
|
---|
| 773 | return ($return);
|
---|
| 774 | }
|
---|
| 775 | }
|
---|
| 776 |
|
---|
| 777 | /**
|
---|
| 778 | * Create a new instance of namedGraphDb with the given $modelURI and graphName and
|
---|
| 779 | * load the corresponding values of modelID and baseURI from the database.
|
---|
| 780 | * Return FALSE if the DbModel does not exist.
|
---|
| 781 | *
|
---|
| 782 | * @param $modelURI string
|
---|
| 783 | * @param $graphName string
|
---|
| 784 | * @return object NamedGraphMem
|
---|
| 785 | * @access public
|
---|
| 786 | */
|
---|
| 787 | function getNamedGraphDb($modelURI, $graphName)
|
---|
| 788 | {
|
---|
| 789 | require_once(RDFAPI_INCLUDE_DIR . PACKAGE_DATASET);
|
---|
| 790 |
|
---|
| 791 | if (!$this->modelExists($modelURI))
|
---|
| 792 | return FALSE;
|
---|
| 793 | else {
|
---|
| 794 | $modelVars =& $this->dbConn->execute("SELECT modelURI, modelID, baseURI
|
---|
| 795 | FROM models
|
---|
| 796 | WHERE modelURI='" .$modelURI ."'");
|
---|
| 797 |
|
---|
| 798 | return new NamedGraphDb($this->dbConn, $modelVars->fields[0],
|
---|
| 799 | $modelVars->fields[1], $graphName ,$modelVars->fields[2]);
|
---|
| 800 | }
|
---|
| 801 | }
|
---|
| 802 |
|
---|
| 803 | /**
|
---|
| 804 | * Create a new instance of namedGraphDb with the given $modelURI and graphName
|
---|
| 805 | * and insert the DbModel variables into the database (not the graphName. This
|
---|
| 806 | * is only stored persistently, when added to dataset).
|
---|
| 807 | * Return FALSE if there is already a model with the given URI.
|
---|
| 808 | *
|
---|
| 809 | * @param $modelURI string
|
---|
| 810 | * @param $graphName string
|
---|
| 811 | * @param $baseURI string
|
---|
| 812 | * @return object namedGraphDb
|
---|
| 813 | * @throws SqlError
|
---|
| 814 | * @access public
|
---|
| 815 | */
|
---|
| 816 | function getNewNamedGraphDb($modelURI, $graphName, $baseURI=NULL) {
|
---|
| 817 |
|
---|
| 818 | if ($this->modelExists($modelURI))
|
---|
| 819 | return FALSE;
|
---|
| 820 | else {
|
---|
| 821 | $modelID = $this->_createUniqueModelID();
|
---|
| 822 |
|
---|
| 823 | $rs =& $this->dbConn->execute("INSERT INTO models
|
---|
| 824 | (modelID, modelURI, baseURI)
|
---|
| 825 | VALUES (" .$modelID ."',
|
---|
| 826 | " .$this->dbConn->qstr($modelURI) ."',
|
---|
| 827 | " .$this->dbConn->qstr($baseURI) .")");
|
---|
| 828 | if (!$rs)
|
---|
| 829 | $this->dbConn->errorMsg();
|
---|
| 830 | else
|
---|
| 831 | return new NamedGraphDb($this->dbConn, $modelURI, $modelID, $graphName, $baseURI);
|
---|
| 832 | }
|
---|
| 833 | }
|
---|
| 834 |
|
---|
| 835 | /**
|
---|
| 836 | * Removes the graph with all statements from the database.
|
---|
| 837 | * Warning: A single namedGraph can be added to several datasets. So it'll be
|
---|
| 838 | * removed from all datasets.
|
---|
| 839 | *
|
---|
| 840 | * @param $modelURI string
|
---|
| 841 | * @return boolean
|
---|
| 842 | * @throws SqlError
|
---|
| 843 | * @access public
|
---|
| 844 | */
|
---|
| 845 | function removeNamedGraphDb($modelURI)
|
---|
| 846 | {
|
---|
| 847 | if (!$this->modelExists($modelURI))
|
---|
| 848 | return FALSE;
|
---|
| 849 |
|
---|
| 850 | $modelID = $this->dbConn->GetOne("SELECT modelID FROM models WHERE modelURI='".$modelURI."'");
|
---|
| 851 |
|
---|
| 852 | $this->dbConn->execute("DELETE FROM models WHERE modelID=".$modelID);
|
---|
| 853 | $this->dbConn->execute("DELETE FROM dataset_model WHERE modelId=".$modelID);
|
---|
| 854 | $this->dbConn->execute("DELETE FROM statements WHERE modelID=".$modelID);
|
---|
| 855 |
|
---|
| 856 | return true;
|
---|
| 857 | }
|
---|
| 858 |
|
---|
| 859 |
|
---|
| 860 |
|
---|
| 861 | /**
|
---|
| 862 | * Performs a SPARQL query against a model. The model is converted to
|
---|
| 863 | * an RDF Dataset. The result can be retrived in SPARQL Query Results XML Format or
|
---|
| 864 | * as an array containing the variables an their bindings.
|
---|
| 865 | *
|
---|
| 866 | * @param string $query Sparql query string
|
---|
| 867 | * @param mixed $arModelIds Array of modelIDs, or NULL to use all models
|
---|
| 868 | * @param string $resultform Result form ('xml' for SPARQL Query Results XML Format)
|
---|
| 869 | * @return string/array
|
---|
| 870 | */
|
---|
| 871 | function sparqlQuery($query, $arModelIds = null, $resultform = false)
|
---|
| 872 | {
|
---|
| 873 | $engine = $this->_prepareSparql($arModelIds);
|
---|
| 874 | return $engine->queryModel(
|
---|
| 875 | null,
|
---|
| 876 | $this->_parseSparqlQuery($query),
|
---|
| 877 | $resultform
|
---|
| 878 | );
|
---|
| 879 | }//function sparqlQuery($query,$resultform = false)
|
---|
| 880 |
|
---|
| 881 |
|
---|
| 882 |
|
---|
| 883 | /**
|
---|
| 884 | * Prepares everything for SparqlEngine-usage
|
---|
| 885 | * Loads the files, creates instances for SparqlEngine and
|
---|
| 886 | * Dataset...
|
---|
| 887 | *
|
---|
| 888 | * @return SparqlEngineDb
|
---|
| 889 | */
|
---|
| 890 | function _prepareSparql($arModelIds)
|
---|
| 891 | {
|
---|
| 892 | require_once RDFAPI_INCLUDE_DIR . 'sparql/SparqlEngineDb.php';
|
---|
| 893 | return new SparqlEngineDb($this, $arModelIds);
|
---|
| 894 | }//function _prepareSparql()
|
---|
| 895 |
|
---|
| 896 |
|
---|
| 897 |
|
---|
| 898 | /**
|
---|
| 899 | * Parses an query and returns the parsed form.
|
---|
| 900 | * If the query is not a string but a Query object,
|
---|
| 901 | * it will just be returned.
|
---|
| 902 | *
|
---|
| 903 | * @param $query mixed String or Query object
|
---|
| 904 | * @return Query query object
|
---|
| 905 | * @throws Exception If $query is no string and no Query object
|
---|
| 906 | */
|
---|
| 907 | function _parseSparqlQuery($query)
|
---|
| 908 | {
|
---|
| 909 | if ($this->queryParser === null) {
|
---|
| 910 | require_once RDFAPI_INCLUDE_DIR . 'sparql/SparqlParser.php';
|
---|
| 911 | $this->queryParser = new SparqlParser();
|
---|
| 912 | }
|
---|
| 913 | return $this->queryParser->parse($query);
|
---|
| 914 | }//function _parseSparqlQuery($query)
|
---|
| 915 |
|
---|
| 916 | } // end: Class DbStore
|
---|
| 917 | ?> |
---|