model
[ class tree: model ] [ index: model ] [ all elements ]

Source for file MDB.php

Documentation is available at MDB.php

  1. <?php
  2. // ----------------------------------------------------------------------------------
  3. // Class: RDF_Store_MDB
  4. // ----------------------------------------------------------------------------------
  5. /**
  6.  * Store_MDB is a persistent store of RDF data using relational database technology.
  7.  * Store_MDB uses the MDB Library for PHP (http://pear.php.net/MDB),
  8.  * which allows to connect to multiple databases in a portable manner.
  9.  *
  10.  * @version V0.7
  11.  * @author Radoslaw Oldakowski <radol@gmx.de>
  12.  * @package model
  13.  * @access public
  14.  */
  15.  
  16. class RDF_Store_MDB extends RDF_Object
  17. {
  18.     /**
  19.      * Database connection object
  20.      *
  21.      * @var object ADOConnection 
  22.      * @access protected
  23.      */
  24.     var $dbConn;
  25.  
  26.     /**
  27.      * Set the database connection with the given parameters.
  28.      *
  29.      * @param string $dsn 
  30.      * @param string $options 
  31.      * @access public
  32.      */
  33.     function RDF_Store_MDB($dsn$options = null)
  34.     {
  35.         require_once 'MDB.php';
  36.         // create a new connection object
  37.         $this->dbConn =MDB::connect($dsn$options);
  38.     }
  39.  
  40.     /**
  41.      * Create tables and indexes for the given database type.
  42.      *
  43.      * @param string $filename 
  44.      * @throws PhpError
  45.      * @access public
  46.      */
  47.     function createTables($filename)
  48.     {
  49.         MDB::loadFile('Manager');
  50.         $manager =new MDB_Manager;
  51.         $err $manager->connect($this->dbConn);
  52.         if(PEAR::isError($err)) {
  53.             return $err;
  54.         }
  55.         $err $manager->updateDatabase(
  56.             $filename,
  57.             $filename.'.old',
  58.             array('database' => $this->dbConn->database_name)
  59.         );
  60.         if(PEAR::isError($err)) {
  61.             return $err;
  62.         }
  63.         $dsn $this->dbConn->getDSN();
  64.         // cant we remove this ugly hack?
  65.         if (isset($dsn['phptype']&& $dsn['phptype'== 'mysql'{
  66.             $this->dbConn->query('CREATE INDEX s_mod_idx ON statements (modelID)');
  67.             $sql 'CREATE INDEX s_sub_pred_idx ON statements (subject(200),predicate(200))';
  68.             $this->dbConn->query($sql);
  69.             $this->dbConn->query('CREATE INDEX s_obj_idx ON statements (object(250))');
  70.         }
  71.         return true;
  72.     }
  73.  
  74.     /**
  75.      * List all Model_MDBs stored in the database.
  76.      *
  77.      * @return array 
  78.      * @throws SqlError
  79.      * @access public
  80.      */
  81.     function listModels()
  82.     {
  83.         $sql 'SELECT modelURI, baseURI FROM models';
  84.         return $this->dbConn->queryAll($sqlMDB_FETCHMODE_ASSOC);
  85.     }
  86.  
  87.     /**
  88.      * Check if the Model_MDB with the given modelURI is already stored in the database
  89.      *
  90.      * @param string $modelURI 
  91.      * @return boolean 
  92.      * @throws SqlError
  93.      * @access public
  94.      */
  95.     function modelExists($modelURI)
  96.     {
  97.         $sql 'SELECT COUNT(*)
  98.             FROM models
  99.             WHERE modelURI = ' $this->dbConn->getValue('text'$modelURI);
  100.         $result $this->dbConn->queryOne($sql);
  101.  
  102.         if (PEAR::isError($result)) {
  103.             return $result;
  104.         }
  105.  
  106.         return (bool)$result;
  107.     }
  108.  
  109.     /**
  110.      * Create a new instance of Model_MDB with the given $modelURI and
  111.      * load the corresponding values of modelID and baseURI from the database.
  112.      * Return FALSE if the Model_MDB does not exist.
  113.      *
  114.      * @param string $modelURI 
  115.      * @return object Model_MDB 
  116.      * @access public
  117.      */
  118.     function getModel($modelURI)
  119.     {
  120.         if (!$this->modelExists($modelURI)) {
  121.             return false;
  122.         }
  123.  
  124.         $sql 'SELECT modelURI, modelID, baseURI FROM models
  125.             WHERE modelURI=' $this->dbConn->getValue('text'$modelURI);
  126.         $modelVars $this->dbConn->queryRow($sql);
  127.  
  128.         return new RDF_Model_MDB($this->dbConn$modelVars[0],
  129.             $modelVars[1]$modelVars[2]);
  130.     }
  131.  
  132.     /**
  133.      * Create a new instance of Model_MDB with the given $modelURI
  134.      * and insert the Model_MDB variables into the database.
  135.      * Return FALSE if there is already a model with the given URI.
  136.      *
  137.      * @param string $modelURI 
  138.      * @param string $baseURI 
  139.      * @return object Model_MDB 
  140.      * @throws SqlError
  141.      * @access public
  142.      */
  143.     function getNewModel($modelURI$baseURI = null)
  144.     {
  145.         if ($this->modelExists($modelURI)) {
  146.             return false;
  147.         }
  148.  
  149.         $this->dbConn->autoCommit(false);
  150.  
  151.         $modelID $this->_createUniqueModelID();
  152.  
  153.         $sql 'INSERT INTO models VALUES (' .
  154.             $this->dbConn->getValue('text'$modelID.',' .
  155.             $this->dbConn->getValue('text'$modelURI.',' .
  156.             $this->dbConn->getValue('text'$baseURI.')';
  157.         $result =$this->dbConn->query($sql);
  158.  
  159.         $this->dbConn->autoCommit(true);
  160.  
  161.         if (PEAR::isError($result)) {
  162.             return $result;
  163.         }
  164.  
  165.         return new RDF_Model_MDB($this->dbConn$modelURI$modelID$baseURI);
  166.     }
  167.  
  168.     /**
  169.      * Store a Model_Memory or another Model_MDB from a different Store_MDB in the database.
  170.      * Return FALSE if there is already a model with modelURI matching the modelURI
  171.      * of the given model.
  172.      *
  173.      * @param object Model  &$model 
  174.      * @param string $modelURI 
  175.      * @return boolean 
  176.      * @access public
  177.      */
  178.     function putModel(&$model$modelURI = null)
  179.     {
  180.         if (!$modelURI{
  181.             if (is_a($model'RDF_Model_Memory')) {
  182.                 $modelURI 'Model_MDB-' $this->_createUniqueModelID();
  183.             else {
  184.                 $modelURI $model->modelURI;
  185.             }
  186.         elseif ($this->modelExists($modelURI)) {
  187.             return false;
  188.         }
  189.  
  190.         $newmodel $this->getNewModel($modelURI$model->getBaseURI());
  191.         return $newmodel->addModel($model);
  192.     }
  193.  
  194.     /**
  195.      * Close the Store_MDB.
  196.      * !!! Warning: If you close the Store_MDB all active instances of Model_MDB from this
  197.      * !!!          Store_MDB will lose their database connection !!!
  198.      *
  199.      * @access public
  200.      */
  201.     function close()
  202.     {
  203.         $this->dbConn->disconnect();
  204.     }
  205.  
  206.     // =============================================================================
  207.     // **************************** protected methods ********************************
  208.     // =============================================================================
  209.     /**
  210.      * Create a unique ID for the Model_MDB to be insert into the models table.
  211.      * This method was implemented because some databases do not support auto-increment.
  212.      *
  213.      * @return integer 
  214.      * @access protected
  215.      */
  216.     function _createUniqueModelID()
  217.     {
  218.         // move to a sequence?
  219.         $sql 'SELECT MAX(modelID) FROM models';
  220.         $maxModelID $this->dbConn->queryOne($sql);
  221.         ++$maxModelID;
  222.         return $maxModelID;
  223.     }
  224. // end: Class Store_MDB
  225. ?>

Documentation generated on Mon, 11 Mar 2019 15:39:28 -0400 by phpDocumentor 1.4.4. PEAR Logo Copyright © PHP Group 2004.