Source for file DbStore.php

Documentation is available at DbStore.php

  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: fsource_model__modelDbStore.php.html 443 2007-06-01 16:25:38Z 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.  * Database connection object
  33.  *
  34.  * @var     object ADOConnection 
  35.  * @access    private
  36.  */
  37.  var $dbConn;
  38.  
  39.  
  40. /**
  41.  * Constructor:
  42.  * Set the database connection with the given parameters.
  43.  *
  44.  * @param   string   $dbDriver 
  45.  * @param   string   $host 
  46.  * @param   string   $dbName 
  47.  * @param   string   $user 
  48.  * @param   string   $password 
  49.  * @access    public
  50.  */
  51.  function DbStore ($dbDriver=ADODB_DB_DRIVER$host=ADODB_DB_HOST$dbName=ADODB_DB_NAME,
  52.                    $user=ADODB_DB_USER$password=ADODB_DB_PASSWORD{
  53.  
  54.    // include DBase Package
  55.    require_once(RDFAPI_INCLUDE_DIR.PACKAGE_DBASE);
  56.  
  57.    // create a new connection object
  58.    $this->dbConn =ADONewConnection($dbDriver);
  59.  
  60.    //activate the ADOdb DEBUG mode
  61.    if (ADODB_DEBUG_MODE == '1')
  62.         $this->dbConn->debug true;
  63.  
  64.    // connect to database
  65.    $r $this->dbConn->connect($host$user$password$dbName);
  66.    if ($r !== true{
  67.       throw new Exception('Could not connect to database');
  68.    }
  69.  
  70.    // optimized for speed
  71.    $this->dbConn->setFetchMode(ADODB_FETCH_NUM);
  72.    //$ADODB_COUNTRECS = FALSE;
  73.  }
  74.  
  75.  
  76. /**
  77.  * Create tables and indexes for the given database type.
  78.  * Currently supported: MsAccess and MySQL.
  79.  * If you want to use other databases, you will have to create tables by yourself
  80.  * according to the abstract <a href="database_schema.html">database schema</a>
  81.  * described in the API documentation.
  82.  *
  83.  * @param   string  $databaseType 
  84.  * @throws    PhpError
  85.  * @access    public
  86.  */
  87.  function createTables($databaseType{
  88.  
  89.    if (!strcasecmp($databaseType'MsAccess'))
  90.    $this->_createTables_MsAccess();
  91.    elseif (!strcasecmp($databaseType'MySQL'))
  92.        $this->_createTables_MySql();
  93.    elseif (!strcasecmp($databaseType'MSSQL'))
  94.            $this->_createTables_mssql();
  95.    else {
  96.        $errmsg RDFAPI_ERROR "(classDbStoremethodcreateTables('$databaseType')):
  97.                                   Currently only MsAccesMySQL and MSSQL supported.";
  98.        trigger_error($errmsgE_USER_ERROR);
  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.  * Create a new instance of DbModel with the given $modelURI and
  160.  * load the corresponding values of modelID and baseURI from the database.
  161.  * Return FALSE if the DbModel does not exist.
  162.  *
  163.  * @param   string   $modelURI 
  164.  * @return  object DbModel 
  165.  * @access    public
  166.  */
  167.  function getModel($modelURI{
  168.  
  169.    if (!$this->modelExists($modelURI))
  170.       return FALSE;
  171.    else {
  172.       $modelVars =$this->dbConn->execute("SELECT modelURI, modelID, baseURI
  173.                                             FROM models
  174.                                             WHERE modelURI='" .$modelURI ."'");
  175.  
  176.       return new DbModel($this->dbConn$modelVars->fields[0],
  177.                          $modelVars->fields[1]$modelVars->fields[2]);
  178.    }
  179.  }
  180.  
  181.  
  182. /**
  183.  * Create a new instance of DbModel with the given $modelURI
  184.  * and insert the DbModel variables into the database.
  185.  * Return FALSE if there is already a model with the given URI.
  186.  *
  187.  * @param   string   $modelURI 
  188.  * @param   string   $baseURI 
  189.  * @return  object DbModel 
  190.  * @throws  SqlError
  191.  * @access    public
  192.  */
  193.  function getNewModel($modelURI$baseURI=NULL{
  194.  
  195.    if ($this->modelExists($modelURI))
  196.       return FALSE;
  197.    else {
  198.       $modelID $this->_createUniqueModelID();
  199.  
  200.       $rs =$this->dbConn->execute("INSERT INTO models
  201.                                             (modelID, modelURI, baseURI)
  202.                                             VALUES ('" .$modelID ."',
  203.                                                     '" .$modelURI ."',
  204.                                                     '" .$baseURI ."')");
  205.       if (!$rs)
  206.          $this->dbConn->errorMsg();
  207.       else
  208.          return new DbModel($this->dbConn$modelURI$modelID$baseURI);
  209.    }
  210.  }
  211.  
  212.  
  213. /**
  214.  * Store a MemModel or another DbModel from a different DbStore in the database.
  215.  * Return FALSE if there is already a model with modelURI matching the modelURI
  216.  * of the given model.
  217.  *
  218.  * @param   object Model  &$model 
  219.  * @param   string $modelURI 
  220.  * @return  boolean 
  221.  * @access    public
  222.  */
  223.  function putModel(&$model$modelURI=NULL{
  224.  
  225.    if (!$modelURI{
  226.       if (is_a($model'MemModel'))
  227.          $modelURI 'DbModel-' .$this->_createUniqueModelID();
  228.       else
  229.          $modelURI $model->modelURI;
  230.    }else
  231.       if ($this->modelExists($modelURI))
  232.          return FALSE;
  233.  
  234.  
  235.    $newDbModel $this->getNewModel($modelURI$model->getBaseURI());
  236.    $newDbModel->addModel($model);
  237.  }
  238.  
  239.  
  240. /**
  241.  * Close the DbStore.
  242.  * !!! Warning: If you close the DbStore all active instances of DbModel from this
  243.  * !!!          DbStore will lose their database connection !!!
  244.  *
  245.  * @access    public
  246.  */
  247.  function close({
  248.  
  249.    $this->dbConn->close();
  250.    unset($this);
  251.  }
  252.  
  253.  
  254. // =============================================================================
  255. // **************************** private methods ********************************
  256. // =============================================================================
  257.  
  258.  
  259. /**
  260.  * Create a unique ID for the DbModel to be insert into the models table.
  261.  * This method was implemented because some databases do not support auto-increment.
  262.  *
  263.  * @return  integer 
  264.  * @access    private
  265.  */
  266.  function _createUniqueModelID({
  267.  
  268.    $maxModelID =$this->dbConn->GetOne('SELECT MAX(modelID) FROM models');
  269.    return ++$maxModelID;
  270.  }
  271.  
  272.  /**
  273.  * Create a unique ID for the dataset to be insert into the datasets table.
  274.  * This method was implemented because some databases do not support auto-increment.
  275.  *
  276.  * @return  integer 
  277.  * @access    private
  278.  */
  279.  function _createUniqueDatasetID({
  280.  
  281.    $maxDatasetID =$this->dbConn->GetOne('SELECT MAX(datasetId) FROM datasets');
  282.    return ++$maxDatasetID;
  283.  }
  284.  
  285.  
  286. /**
  287.  * Create tables and indexes for MsAccess database
  288.  *
  289.  * @throws  SqlError
  290.  * @access    private
  291.  */
  292.  function _createTables_MsAccess({
  293.  
  294.    $this->dbConn->startTrans();
  295.  
  296.    $this->dbConn->execute('CREATE TABLE models
  297.                            (modelID long primary key,
  298.                             modelURI varchar not null,
  299.                             baseURI varchar)');
  300.  
  301.    $this->dbConn->execute('CREATE UNIQUE INDEX m_modURI_idx ON models (modelURI)');
  302.  
  303.    $this->dbConn->execute('CREATE TABLE statements
  304.                            (modelID long,
  305.                             subject varchar,
  306.                             predicate varchar,
  307.                             object Memo,
  308.                             l_language varchar,
  309.                             l_datatype varchar,
  310.                             subject_is varchar(1),
  311.                             object_is varchar(1),
  312.                             primary key (modelID, subject, predicate, object,
  313.                                          l_language, l_datatype))');
  314.  
  315.    $this->dbConn->execute('CREATE INDEX s_mod_idx ON statements (modelID)');
  316.    $this->dbConn->execute('CREATE INDEX s_sub_idx ON statements (subject)');
  317.    $this->dbConn->execute('CREATE INDEX s_pred_idx ON statements (predicate)');
  318.    $this->dbConn->execute('CREATE INDEX s_obj_idx ON statements (object)');
  319.  
  320.      $this->dbConn->execute('CREATE TABLE namespaces
  321.                            (modelID long,
  322.                             namespace varchar,
  323.                             prefix varchar,
  324.                             primary key (modelID, namespace, prefix))');
  325.  
  326.    $this->dbConn->execute('CREATE INDEX n_name_idx ON namespaces (namespace)');
  327.    $this->dbConn->execute('CREATE INDEX n_pref_idx ON namespaces (prefix)');
  328.  
  329.      $this->dbConn->execute("CREATE TABLE datasets
  330.                            (datasetName varchar,
  331.                               defaultModelUri varchar,
  332.                             primary key (datasetName))");
  333.  
  334.    $this->dbConn->execute('CREATE INDEX nGS_idx1 ON datasets (datasetName)');
  335.  
  336.  
  337.    $this->dbConn->execute("CREATE TABLE `dataset_model` (
  338.                               datasetName varchar,
  339.                               modelId long,
  340.                             graphURI varchar,
  341.                             PRIMARY KEY  (modelId,datasetName))");
  342.  
  343.  
  344.    if (!$this->dbConn->completeTrans())
  345.       echo $this->dbConn->errorMsg();
  346.  }
  347.  
  348.  
  349. /**
  350.  * Create tables and indexes for MySQL database
  351.  *
  352.  * @throws  SqlError
  353.  * @access    private
  354.  */
  355.  function _createTables_MySql({
  356.  
  357.    $this->dbConn->startTrans();
  358.  
  359.    $this->dbConn->execute("CREATE TABLE models
  360.                            (modelID bigint NOT NULL,
  361.                             modelURI varchar(255) NOT NULL,
  362.                             baseURI varchar(255) DEFAULT '',
  363.                             primary key (modelID))");
  364.  
  365.    $this->dbConn->execute('CREATE UNIQUE INDEX m_modURI_idx ON models (modelURI)');
  366.  
  367.    $this->dbConn->execute("CREATE TABLE statements
  368.                            (modelID bigint NOT NULL,
  369.                             subject varchar(255) NOT NULL,
  370.                             predicate varchar(255) NOT NULL,
  371.                             object text,
  372.                             l_language varchar(255) DEFAULT '',
  373.                             l_datatype varchar(255) DEFAULT '',
  374.                             subject_is varchar(1) NOT NULL,
  375.                             object_is varchar(1) NOT NULL)");
  376.  
  377.    $this->dbConn->execute("CREATE TABLE namespaces
  378.                            (modelID bigint NOT NULL,
  379.                             namespace varchar(255) NOT NULL,
  380.                             prefix varchar(255) NOT NULL,
  381.                                primary key (modelID,namespace))");
  382.  
  383.   $this->dbConn->execute("CREATE TABLE `dataset_model` (
  384.                               `datasetName` varchar(255) NOT NULL default '0',
  385.                               `modelId` bigint(20) NOT NULL default '0',
  386.                             `graphURI` varchar(255) NOT NULL default '',
  387.                              PRIMARY KEY  (`modelId`,`datasetName`))");
  388.  
  389.   $this->dbConn->execute("CREATE TABLE `datasets` (
  390.                               `datasetName` varchar(255) NOT NULL default '',
  391.                             `defaultModelUri` varchar(255) NOT NULL default '0',
  392.                              PRIMARY KEY  (`datasetName`),
  393.                              KEY `datasetName` (`datasetName`))");
  394.  
  395.    $this->dbConn->execute('CREATE INDEX s_mod_idx ON statements (modelID)');
  396.    $this->dbConn->execute('CREATE INDEX n_mod_idx ON namespaces (modelID)');
  397.  
  398.    $this->dbConn->execute('CREATE INDEX s_sub_pred_idx ON statements
  399.                           (subject(200),predicate(200))');
  400.  
  401.    $this->dbConn->execute('CREATE INDEX s_sub_idx ON statements (subject(200))');
  402.    $this->dbConn->execute('CREATE INDEX s_pred_idx ON statements (predicate(200))');
  403.    $this->dbConn->execute('CREATE INDEX s_obj_idx ON statements (object(250))');
  404.  
  405.    $this->dbConn->execute('CREATE FULLTEXT INDEX s_obj_ftidx ON statements (object)');
  406.  
  407.    if (!$this->dbConn->completeTrans())
  408.       echo $this->dbConn->errorMsg();
  409.  }
  410.  
  411.  /**
  412.  * Create tables and indexes for MSSQL database
  413.  *
  414.  * @throws  SqlError
  415.  * @access    private
  416.  */
  417.  function _createTables_mssql(){
  418.  
  419.    $this->dbConn->startTrans();
  420.  
  421.    $this->dbConn->execute("CREATE TABLE [dbo].[models] (
  422.                             [modelID] [int] NOT NULL ,
  423.                             [modelURI] [nvarchar] (200) COLLATE SQL_Latin1_General_CP1_CI_AS NULL ,
  424.                             [baseURI] [nvarchar] (200) COLLATE SQL_Latin1_General_CP1_CI_AS NULL
  425.                             ) ON [PRIMARY]");
  426.  
  427.    $this->dbConn->execute("CREATE TABLE [dbo].[statements] (
  428.                             [modelID] [int] NOT NULL ,
  429.                             [subject] [nvarchar] (200) COLLATE SQL_Latin1_General_CP1_CI_AS NULL ,
  430.                             [predicate] [nvarchar] (200) COLLATE SQL_Latin1_General_CP1_CI_AS NULL ,
  431.                             [object] [text] COLLATE SQL_Latin1_General_CP1_CI_AS NULL ,
  432.                             [l_language] [nvarchar] (50) COLLATE SQL_Latin1_General_CP1_CI_AS NULL ,
  433.                             [l_datatype] [nvarchar] (50) COLLATE SQL_Latin1_General_CP1_CI_AS NULL ,
  434.                             [subject_is] [nchar] (1) COLLATE SQL_Latin1_General_CP1_CI_AS NULL ,
  435.                             [object_is] [nchar] (1) COLLATE SQL_Latin1_General_CP1_CI_AS NULL
  436.                             ) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]");
  437.  
  438.  
  439.     $this->dbConn->execute("CREATE TABLE [dbo].[namespaces] (
  440.                             [modelID] [int] NOT NULL ,
  441.                             [namespace] [nvarchar] (200) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL ,
  442.                             [prefix] [nvarchar] (200) COLLATE SQL_Latin1_General_CP1_CI_AS NULL ,
  443.                             ) ON [PRIMARY]");
  444.  
  445.    $this->dbConn->execute("ALTER TABLE [dbo].[models] WITH NOCHECK ADD
  446.                             CONSTRAINT [PK_models] PRIMARY KEY  CLUSTERED
  447.                             (
  448.                                 [modelID]
  449.                             )  ON [PRIMARY] ");
  450.    $this->dbConn->execute("ALTER TABLE [dbo].[namespaces] WITH NOCHECK ADD
  451.                             CONSTRAINT [PK_namespaces] PRIMARY KEY  CLUSTERED
  452.                             (
  453.                                 [modelID],[namespace]
  454.                             )  ON [PRIMARY] ");
  455.  
  456.    $this->dbConn->execute("CREATE  INDEX [joint index on subject and predicate] ON [dbo].[statements]([subject], [predicate]) ON [PRIMARY]");
  457.  
  458.  
  459.    if (!$this->dbConn->completeTrans())
  460.        echo $this->dbConn->errorMsg();
  461.  
  462.  }
  463.  
  464.  
  465.   /**
  466.   * Checks if tables are setup for RAP
  467.   *
  468.   * @param   string  $databaseType 
  469.   * @throws SqlError
  470.   * @access public
  471.   ***/
  472.  function isSetup($databaseType="MySQL"{
  473.    if ($databaseType=="MySQL")
  474.      return $this->_isSetup_MySql();
  475.     if ($databaseType=="MSSQL")
  476.      return $this->_isSetup_MSSQL();
  477.    else {
  478.        if ($databaseType=='MsAccess'){
  479.            return $this->_isSetup_MsAccess();
  480.        }else{
  481.      $errmsg=RDFAPI_ERROR."(classDbStoremethod isSetup('$databaseType')):\nCurrently only MySQLMsAccess and MSSQL are supported!";
  482.      trigger_error($errmsgE_USER_ERROR);}
  483.    }
  484.  }
  485.  
  486.  /**
  487.   * Checks if tables are setup for RAP (MySql)
  488.   *
  489.   * @throws SqlError
  490.   * @access private
  491.   ***/
  492.  function _isSetup_MySql({
  493.    $recordSet =$this->dbConn->execute("SHOW TABLES");
  494.    if (!$recordSet)
  495.      echo $this->dbConn->errorMsg();
  496.    else {
  497.      $tables array();
  498.      while (!$recordSet->EOF{
  499.  
  500.        $tables[]$recordSet->fields[0];
  501.  
  502.        if(isset($i)){++$i;}
  503.        $recordSet->moveNext();
  504.      }
  505.      if (in_array("models",$tables&& in_array("statements",$tables)&& in_array("namespaces",$tables)) return true;
  506.    }
  507.    return false;
  508.  
  509.  }
  510.  
  511.  
  512.   /**
  513.   * Checks if tables are setup for RAP (MsAccess)
  514.   *
  515.   * @throws SqlError
  516.   * @access private
  517.   ***/
  518.  function _isSetup_MsAccess({
  519.        $tables =$this->dbConn->MetaTables();
  520.        if (!$tables)
  521.      echo $this->dbConn->errorMsg();
  522.    if (count($tables)==0){
  523.      return false;}
  524.    else {
  525.            if (in_array("models",$tables&& in_array("statements",$tables&& in_array("namespaces",$tables))return true;
  526.            }else{return false;}
  527.    }
  528.  }
  529.  
  530.   /**
  531.   * Checks if tables are setup for RAP (MSSQL)
  532.   *
  533.   * @throws SqlError
  534.   * @access private
  535.   ***/
  536.  function _isSetup_MSSQL({
  537.        $tables =$this->dbConn->MetaTables();
  538.        if (!$tables)
  539.      echo $this->dbConn->errorMsg();
  540.    if (count($tables)==0){
  541.      return false;}
  542.    else {
  543.            if (in_array("models",$tables&& in_array("statements",$tables&& in_array("namespaces",$tables))return true;
  544.            }else{return false;}
  545.    }
  546.  }
  547.  
  548.  
  549.  /**
  550.  * Create a new instance of DatasetDb with the given $datasetName
  551.  * and insert the DatasetDb variables into the database.
  552.  * Return FALSE if there is already a model with the given URI.
  553.  *
  554.  * @param   $datasetName string
  555.  * @return  object DatasetDB 
  556.  * @throws  SqlError
  557.  * @access    public
  558.  */
  559.  function getNewDatasetDb($datasetName)
  560.  {
  561.  
  562.      require_once(RDFAPI_INCLUDE_DIR PACKAGE_DATASET);
  563.  
  564.    if ($this->datasetExists($datasetName))
  565.       return FALSE;
  566.    else
  567.    {
  568.            $defaultModelUri=uniqid('http://rdfapi-php/dataset_defaultmodel_');
  569.            $defaultModel=$this->getNewModel($defaultModelUri);
  570.  
  571.           $rs =$this->dbConn->execute("INSERT INTO datasets
  572.                                             VALUES ('" .$datasetName ."',
  573.                                                     '" .$defaultModelUri."')");
  574.  
  575.       if (!$rs)
  576.          $this->dbConn->errorMsg();
  577.       else
  578.         $return=new DatasetDb($this->dbConn$this$datasetName);
  579.            return ($return);
  580.    }
  581.  }
  582.  
  583.  /**
  584.  * Check if the Dataset with the given $datasetName is already stored in the database
  585.  *
  586.  * @param   $datasetName string
  587.  * @return  boolean 
  588.  * @throws    SqlError
  589.  * @access    public
  590.  */
  591. function datasetExists($datasetName{
  592.  
  593.    $res =$this->dbConn->execute("SELECT COUNT(*) FROM datasets
  594.                                    WHERE datasetName = '" .$datasetName ."'");
  595.    if (!$res)
  596.       echo $this->dbConn->errorMsg();
  597.    else {
  598.       if (!$res->fields[0])
  599.          return FALSE;
  600.       return TRUE;
  601.    }
  602.  }
  603.  
  604.  
  605.  /**
  606.  * Create a new instance of DatasetDb with the given $datasetName and
  607.  * load the corresponding values from the database.
  608.  * Return FALSE if the DbModel does not exist.
  609.  *
  610.  * @param   $datasetId string
  611.  * @return  object DatasetDb 
  612.  * @access    public
  613.  */
  614.  function &getDatasetDb($datasetName{
  615.     require_once(RDFAPI_INCLUDE_DIR PACKAGE_DATASET);
  616.  
  617.     if (!$this->datasetExists($datasetName)) {
  618.         return FALSE;
  619.     else {
  620.         $return new DatasetDb($this->dbConn$this$datasetName);
  621.         return ($return);
  622.     }
  623.  }
  624.  
  625.  /**
  626.  * Create a new instance of namedGraphDb with the given $modelURI and graphName and
  627.  * load the corresponding values of modelID and baseURI from the database.
  628.  * Return FALSE if the DbModel does not exist.
  629.  *
  630.  * @param   $modelURI string
  631.  * @param   $graphName string
  632.  * @return  object NamedGraphMem 
  633.  * @access    public
  634.  */
  635.  function getNamedGraphDb($modelURI$graphName)
  636.  {
  637.     require_once(RDFAPI_INCLUDE_DIR PACKAGE_DATASET);
  638.  
  639.    if (!$this->modelExists($modelURI))
  640.       return FALSE;
  641.    else {
  642.       $modelVars =$this->dbConn->execute("SELECT modelURI, modelID, baseURI
  643.                                             FROM models
  644.                                             WHERE modelURI='" .$modelURI ."'");
  645.  
  646.       return new NamedGraphDb($this->dbConn$modelVars->fields[0],
  647.                          $modelVars->fields[1]$graphName ,$modelVars->fields[2]);
  648.    }
  649.  }
  650.  
  651.  /**
  652.  * Create a new instance of namedGraphDb with the given $modelURI and graphName
  653.  * and insert the DbModel variables into the database (not the graphName. This
  654.  * is only stored persistently, when added to dataset).
  655.  * Return FALSE if there is already a model with the given URI.
  656.  *
  657.  * @param   $modelURI string
  658.  * @param      $graphName string
  659.  * @param   $baseURI string
  660.  * @return  object namedGraphDb 
  661.  * @throws  SqlError
  662.  * @access    public
  663.  */
  664.  function getNewNamedGraphDb($modelURI$graphName$baseURI=NULL{
  665.  
  666.    if ($this->modelExists($modelURI))
  667.       return FALSE;
  668.    else {
  669.       $modelID $this->_createUniqueModelID();
  670.  
  671.       $rs =$this->dbConn->execute("INSERT INTO models
  672.                                             (modelID, modelURI, baseURI)
  673.                                             VALUES ('" .$modelID ."',
  674.                                                     '" .$modelURI ."',
  675.                                                     '" .$baseURI ."')");
  676.       if (!$rs)
  677.          $this->dbConn->errorMsg();
  678.       else
  679.          return new NamedGraphDb($this->dbConn$modelURI$modelID$graphName$baseURI);
  680.    }
  681.  }
  682.  
  683.  /**
  684.  * Removes the graph with all statements from the database.
  685.  * Warning: A single namedGraph can be added to several datasets. So it'll be
  686.  * removed from all datasets.
  687.  *
  688.  * @param   $modelURI string
  689.  * @return  boolean 
  690.  * @throws  SqlError
  691.  * @access    public
  692.  */
  693.  function removeNamedGraphDb($modelURI)
  694.  {
  695.     if (!$this->modelExists($modelURI))
  696.         return FALSE;
  697.  
  698.     $modelID $this->dbConn->GetOne("SELECT modelID FROM models WHERE modelURI='".$modelURI."'");
  699.  
  700.     $this->dbConn->execute("DELETE FROM models WHERE modelID=".$modelID);
  701.     $this->dbConn->execute("DELETE FROM dataset_model WHERE modelId=".$modelID);
  702.     $this->dbConn->execute("DELETE FROM statements WHERE modelID=".$modelID);
  703.  
  704.     return true;
  705.  }
  706.  
  707. // end: Class DbStore
  708. ?>

Documentation generated on Fri, 1 Jun 2007 16:48:52 +0200 by phpDocumentor 1.3.2