Source for file DbStore.php
Documentation is available at DbStore.php
require_once RDFAPI_INCLUDE_DIR .
'constants.php';
require_once RDFAPI_INCLUDE_DIR .
'util/Object.php';
// ----------------------------------------------------------------------------------
// ----------------------------------------------------------------------------------
* DbStore is a persistent store of RDF data using relational database technology.
* DbStore uses ADOdb Library for PHP V3.60 (http://php.weblogs.com/ADODB),
* which allows to connect to multiple databases in a portable manner.
* This class also provides methods for creating tables for MsAccess, MySQL, and MS SQL Server.
* If you want to use other databases, you will have to create tables by yourself
* according to the abstract database schema described in the API documentation.
* You can activate debug mode by defining ADODB_DEBUG_MODE to "1".
* @version $Id: fsource_model__modelDbStore.php.html 443 2007-06-01 16:25:38Z cax $
* @author Radoslaw Oldakowski <radol@gmx.de>
* @author Daniel Westphal (http://www.d-westphal.de)
* Database connection object
* @var object ADOConnection
* Set the database connection with the given parameters.
* @param string $dbDriver
* @param string $password
function DbStore ($dbDriver=
ADODB_DB_DRIVER, $host=
ADODB_DB_HOST, $dbName=
ADODB_DB_NAME,
$user=
ADODB_DB_USER, $password=
ADODB_DB_PASSWORD) {
// create a new connection object
$this->dbConn =
& ADONewConnection($dbDriver);
//activate the ADOdb DEBUG mode
$this->dbConn->debug =
true;
$r =
$this->dbConn->connect($host, $user, $password, $dbName);
throw
new Exception('Could not connect to database');
$this->dbConn->setFetchMode(ADODB_FETCH_NUM);
//$ADODB_COUNTRECS = FALSE;
* Create tables and indexes for the given database type.
* Currently supported: MsAccess and MySQL.
* If you want to use other databases, you will have to create tables by yourself
* according to the abstract <a href="database_schema.html">database schema</a>
* described in the API documentation.
* @param string $databaseType
$this->_createTables_MsAccess();
$this->_createTables_MySql();
$this->_createTables_mssql();
$errmsg =
RDFAPI_ERROR .
"(class: DbStore; method: createTables('$databaseType')):
Currently only MsAcces, MySQL and MSSQL supported.";
* List all DbModels stored in the database.
$recordSet =
& $this->dbConn->execute("SELECT modelURI, baseURI
echo
$this->dbConn->errorMsg();
while (!$recordSet->EOF) {
$models[$i]['modelURI'] =
$recordSet->fields[0];
$models[$i]['baseURI'] =
$recordSet->fields[1];
* Check if the DbModel with the given modelURI is already stored in the database
* @param string $modelURI
$res =
& $this->dbConn->execute("SELECT COUNT(*) FROM models
WHERE modelURI = '" .
$modelURI .
"'");
echo
$this->dbConn->errorMsg();
* Create a new instance of DbModel with the given $modelURI and
* load the corresponding values of modelID and baseURI from the database.
* Return FALSE if the DbModel does not exist.
* @param string $modelURI
$modelVars =
& $this->dbConn->execute("SELECT modelURI, modelID, baseURI
WHERE modelURI='" .
$modelURI .
"'");
return new DbModel($this->dbConn, $modelVars->fields[0],
$modelVars->fields[1], $modelVars->fields[2]);
* Create a new instance of DbModel with the given $modelURI
* and insert the DbModel variables into the database.
* Return FALSE if there is already a model with the given URI.
* @param string $modelURI
$modelID =
$this->_createUniqueModelID();
$rs =
& $this->dbConn->execute("INSERT INTO models
(modelID, modelURI, baseURI)
VALUES ('" .
$modelID .
"',
$this->dbConn->errorMsg();
return new DbModel($this->dbConn, $modelURI, $modelID, $baseURI);
* Store a MemModel or another DbModel from a different DbStore in the database.
* Return FALSE if there is already a model with modelURI matching the modelURI
* @param object Model &$model
* @param string $modelURI
function putModel(&$model, $modelURI=
NULL) {
if (is_a($model, 'MemModel'))
$modelURI =
'DbModel-' .
$this->_createUniqueModelID();
$modelURI =
$model->modelURI;
$newDbModel =
$this->getNewModel($modelURI, $model->getBaseURI());
$newDbModel->addModel($model);
* !!! Warning: If you close the DbStore all active instances of DbModel from this
* !!! DbStore will lose their database connection !!!
// =============================================================================
// **************************** private methods ********************************
// =============================================================================
* Create a unique ID for the DbModel to be insert into the models table.
* This method was implemented because some databases do not support auto-increment.
function _createUniqueModelID() {
$maxModelID =
& $this->dbConn->GetOne('SELECT MAX(modelID) FROM models');
* Create a unique ID for the dataset to be insert into the datasets table.
* This method was implemented because some databases do not support auto-increment.
function _createUniqueDatasetID() {
$maxDatasetID =
& $this->dbConn->GetOne('SELECT MAX(datasetId) FROM datasets');
* Create tables and indexes for MsAccess database
function _createTables_MsAccess() {
$this->dbConn->startTrans();
$this->dbConn->execute('CREATE TABLE models
(modelID long primary key,
modelURI varchar not null,
$this->dbConn->execute('CREATE UNIQUE INDEX m_modURI_idx ON models (modelURI)');
$this->dbConn->execute('CREATE TABLE statements
primary key (modelID, subject, predicate, object,
l_language, l_datatype))');
$this->dbConn->execute('CREATE INDEX s_mod_idx ON statements (modelID)');
$this->dbConn->execute('CREATE INDEX s_sub_idx ON statements (subject)');
$this->dbConn->execute('CREATE INDEX s_pred_idx ON statements (predicate)');
$this->dbConn->execute('CREATE INDEX s_obj_idx ON statements (object)');
$this->dbConn->execute('CREATE TABLE namespaces
primary key (modelID, namespace, prefix))');
$this->dbConn->execute('CREATE INDEX n_name_idx ON namespaces (namespace)');
$this->dbConn->execute('CREATE INDEX n_pref_idx ON namespaces (prefix)');
$this->dbConn->execute("CREATE TABLE datasets
primary key (datasetName))");
$this->dbConn->execute('CREATE INDEX nGS_idx1 ON datasets (datasetName)');
$this->dbConn->execute("CREATE TABLE `dataset_model` (
PRIMARY KEY (modelId,datasetName))");
if (!$this->dbConn->completeTrans())
echo
$this->dbConn->errorMsg();
* Create tables and indexes for MySQL database
function _createTables_MySql() {
$this->dbConn->startTrans();
$this->dbConn->execute("CREATE TABLE models
(modelID bigint NOT NULL,
modelURI varchar(255) NOT NULL,
baseURI varchar(255) DEFAULT '',
primary key (modelID))");
$this->dbConn->execute('CREATE UNIQUE INDEX m_modURI_idx ON models (modelURI)');
$this->dbConn->execute("CREATE TABLE statements
(modelID bigint NOT NULL,
subject varchar(255) NOT NULL,
predicate varchar(255) NOT NULL,
l_language varchar(255) DEFAULT '',
l_datatype varchar(255) DEFAULT '',
subject_is varchar(1) NOT NULL,
object_is varchar(1) NOT NULL)");
$this->dbConn->execute("CREATE TABLE namespaces
(modelID bigint NOT NULL,
namespace varchar(255) NOT NULL,
prefix varchar(255) NOT NULL,
primary key (modelID,namespace))");
$this->dbConn->execute("CREATE TABLE `dataset_model` (
`datasetName` varchar(255) NOT NULL default '0',
`modelId` bigint(20) NOT NULL default '0',
`graphURI` varchar(255) NOT NULL default '',
PRIMARY KEY (`modelId`,`datasetName`))");
$this->dbConn->execute("CREATE TABLE `datasets` (
`datasetName` varchar(255) NOT NULL default '',
`defaultModelUri` varchar(255) NOT NULL default '0',
PRIMARY KEY (`datasetName`),
KEY `datasetName` (`datasetName`))");
$this->dbConn->execute('CREATE INDEX s_mod_idx ON statements (modelID)');
$this->dbConn->execute('CREATE INDEX n_mod_idx ON namespaces (modelID)');
$this->dbConn->execute('CREATE INDEX s_sub_pred_idx ON statements
(subject(200),predicate(200))');
$this->dbConn->execute('CREATE INDEX s_sub_idx ON statements (subject(200))');
$this->dbConn->execute('CREATE INDEX s_pred_idx ON statements (predicate(200))');
$this->dbConn->execute('CREATE INDEX s_obj_idx ON statements (object(250))');
$this->dbConn->execute('CREATE FULLTEXT INDEX s_obj_ftidx ON statements (object)');
if (!$this->dbConn->completeTrans())
echo
$this->dbConn->errorMsg();
* Create tables and indexes for MSSQL database
function _createTables_mssql(){
$this->dbConn->startTrans();
$this->dbConn->execute("CREATE TABLE [dbo].[models] (
[modelID] [int] NOT NULL ,
[modelURI] [nvarchar] (200) COLLATE SQL_Latin1_General_CP1_CI_AS NULL ,
[baseURI] [nvarchar] (200) COLLATE SQL_Latin1_General_CP1_CI_AS NULL
$this->dbConn->execute("CREATE TABLE [dbo].[statements] (
[modelID] [int] NOT NULL ,
[subject] [nvarchar] (200) COLLATE SQL_Latin1_General_CP1_CI_AS NULL ,
[predicate] [nvarchar] (200) COLLATE SQL_Latin1_General_CP1_CI_AS NULL ,
[object] [text] COLLATE SQL_Latin1_General_CP1_CI_AS NULL ,
[l_language] [nvarchar] (50) COLLATE SQL_Latin1_General_CP1_CI_AS NULL ,
[l_datatype] [nvarchar] (50) COLLATE SQL_Latin1_General_CP1_CI_AS NULL ,
[subject_is] [nchar] (1) COLLATE SQL_Latin1_General_CP1_CI_AS NULL ,
[object_is] [nchar] (1) COLLATE SQL_Latin1_General_CP1_CI_AS NULL
) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]");
$this->dbConn->execute("CREATE TABLE [dbo].[namespaces] (
[modelID] [int] NOT NULL ,
[namespace] [nvarchar] (200) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL ,
[prefix] [nvarchar] (200) COLLATE SQL_Latin1_General_CP1_CI_AS NULL ,
$this->dbConn->execute("ALTER TABLE [dbo].[models] WITH NOCHECK ADD
CONSTRAINT [PK_models] PRIMARY KEY CLUSTERED
$this->dbConn->execute("ALTER TABLE [dbo].[namespaces] WITH NOCHECK ADD
CONSTRAINT [PK_namespaces] PRIMARY KEY CLUSTERED
$this->dbConn->execute("CREATE INDEX [joint index on subject and predicate] ON [dbo].[statements]([subject], [predicate]) ON [PRIMARY]");
if (!$this->dbConn->completeTrans())
echo
$this->dbConn->errorMsg();
* Checks if tables are setup for RAP
* @param string $databaseType
function isSetup($databaseType=
"MySQL") {
if ($databaseType==
"MySQL")
return $this->_isSetup_MySql();
if ($databaseType==
"MSSQL")
return $this->_isSetup_MSSQL();
if ($databaseType==
'MsAccess'){
return $this->_isSetup_MsAccess();
$errmsg=
RDFAPI_ERROR.
"(class: DbStore; method isSetup('$databaseType')):\nCurrently only MySQL, MsAccess and MSSQL are supported!";
* Checks if tables are setup for RAP (MySql)
function _isSetup_MySql() {
$recordSet =
& $this->dbConn->execute("SHOW TABLES");
echo
$this->dbConn->errorMsg();
while (!$recordSet->EOF) {
$tables[]=
$recordSet->fields[0];
* Checks if tables are setup for RAP (MsAccess)
function _isSetup_MsAccess() {
$tables =
& $this->dbConn->MetaTables();
echo
$this->dbConn->errorMsg();
* Checks if tables are setup for RAP (MSSQL)
function _isSetup_MSSQL() {
$tables =
& $this->dbConn->MetaTables();
echo
$this->dbConn->errorMsg();
* Create a new instance of DatasetDb with the given $datasetName
* and insert the DatasetDb variables into the database.
* Return FALSE if there is already a model with the given URI.
* @param $datasetName string
* @return object DatasetDB
$defaultModelUri=
uniqid('http://rdfapi-php/dataset_defaultmodel_');
$rs =
& $this->dbConn->execute("INSERT INTO datasets
VALUES ('" .
$datasetName .
"',
'" .
$defaultModelUri.
"')");
$this->dbConn->errorMsg();
$return=
new DatasetDb($this->dbConn, $this, $datasetName);
* Check if the Dataset with the given $datasetName is already stored in the database
* @param $datasetName string
$res =
& $this->dbConn->execute("SELECT COUNT(*) FROM datasets
WHERE datasetName = '" .
$datasetName .
"'");
echo
$this->dbConn->errorMsg();
* Create a new instance of DatasetDb with the given $datasetName and
* load the corresponding values from the database.
* Return FALSE if the DbModel does not exist.
* @param $datasetId string
* @return object DatasetDb
$return =
new DatasetDb($this->dbConn, $this, $datasetName);
* Create a new instance of namedGraphDb with the given $modelURI and graphName and
* load the corresponding values of modelID and baseURI from the database.
* Return FALSE if the DbModel does not exist.
* @param $modelURI string
* @param $graphName string
* @return object NamedGraphMem
$modelVars =
& $this->dbConn->execute("SELECT modelURI, modelID, baseURI
WHERE modelURI='" .
$modelURI .
"'");
return new NamedGraphDb($this->dbConn, $modelVars->fields[0],
$modelVars->fields[1], $graphName ,$modelVars->fields[2]);
* Create a new instance of namedGraphDb with the given $modelURI and graphName
* and insert the DbModel variables into the database (not the graphName. This
* is only stored persistently, when added to dataset).
* Return FALSE if there is already a model with the given URI.
* @param $modelURI string
* @param $graphName string
* @return object namedGraphDb
$modelID =
$this->_createUniqueModelID();
$rs =
& $this->dbConn->execute("INSERT INTO models
(modelID, modelURI, baseURI)
VALUES ('" .
$modelID .
"',
$this->dbConn->errorMsg();
return new NamedGraphDb($this->dbConn, $modelURI, $modelID, $graphName, $baseURI);
* Removes the graph with all statements from the database.
* Warning: A single namedGraph can be added to several datasets. So it'll be
* removed from all datasets.
* @param $modelURI string
$modelID =
$this->dbConn->GetOne("SELECT modelID FROM models WHERE modelURI='".
$modelURI.
"'");
$this->dbConn->execute("DELETE FROM models WHERE modelID=".
$modelID);
$this->dbConn->execute("DELETE FROM dataset_model WHERE modelId=".
$modelID);
$this->dbConn->execute("DELETE FROM statements WHERE modelID=".
$modelID);
Documentation generated on Fri, 1 Jun 2007 16:48:52 +0200 by phpDocumentor 1.3.2