Source for file DatasetDb.php

Documentation is available at DatasetDb.php

  1. <?php
  2. // ----------------------------------------------------------------------------------
  3. // Class: DatasetDb
  4. // ----------------------------------------------------------------------------------
  5.  
  6. /**
  7. * Persistent implementation of a Dataset in a database.
  8. * A RDF dataset is a collection of named RDF graphs.
  9. *
  10. @version  $Id: fsource_dataset__datasetDatasetDb.php.html 310 2006-06-26 12:34:22Z tgauss $
  11. @author Daniel Westphal (http://www.d-westphal.de)
  12. @author Chris Bizer <chris@bizer.de>
  13. *
  14. @package     dataset
  15. @access    public
  16. ***/
  17. require_once(RDFAPI_INCLUDE_DIR.PACKAGE_DBASE);
  18.  
  19. class DatasetDb extends Dataset 
  20. {
  21.         
  22.     /**
  23.     * Reference to databse connection.
  24.     *
  25.     * @var        resource dbConnection
  26.     * @access    private
  27.     */
  28.     var $dbConnection;
  29.     
  30.     /**
  31.     * Reference to the dbStore Object.
  32.     *
  33.     * @var        $dbStore dbStore
  34.     * @access    private
  35.     */
  36.     var $dbStore;
  37.     
  38.     
  39.     /**
  40.     * Name of the Dataset
  41.     *
  42.     * @var        string 
  43.     * @access    private
  44.     */
  45.     var $setName;
  46.     
  47.     
  48.     /**
  49.     * Constructor
  50.     * You can supply a Dataset name.
  51.     *
  52.     * @param  ADODBConnection 
  53.     * @param  DbStore 
  54.     * @param  string 
  55.     * @access    public
  56.     */        
  57.     function DatasetDb(&$dbConnection,&$dbStore,$datasetName)
  58.     {
  59.         $this->dbConnection=$dbConnection;
  60.         $this->dbStore=&$dbStore;
  61.         $this->setName$datasetName;
  62.         $this->_initialize();
  63.     }
  64.     
  65.     /**
  66.     * Initialize
  67.     * Read all needed data into the set.
  68.     *
  69.     * 
  70.     * @access    private
  71.     */        
  72.     function _initialize()
  73.     {
  74.         $recordSet =$this->dbConnection->execute("SELECT defaultModelUri
  75.                                          FROM datasets where datasetName='".$this->setName."'");
  76.         
  77.            $this->defaultGraph=$this->dbStore->getModel($recordSet->fields[0]);
  78.     }
  79.  
  80.  
  81.     
  82. //    === Graph level methods ========================
  83.     
  84.         
  85.     /**
  86.     * Sets the Dataset name. Return true on success, false otherwise.
  87.     *
  88.     * @param  string 
  89.     * @access    public
  90.     */    
  91.     function setDatasetName($datasetName)
  92.     {
  93.         if ($this->dbStore->datasetExists($datasetName))
  94.             return false;
  95.             
  96.         $this->dbConnection->execute("UPDATE datasets SET datasetName='".$datasetName."'
  97.                                       where datasetName='".$this->setName."'");
  98.         
  99.         $this->dbConnection->execute("UPDATE dataset_model SET datasetName='".$datasetName."'
  100.                                       where datasetName='".$this->setName."'");
  101.         $this->setName=$datasetName;
  102.         return true;
  103.     }
  104.     
  105.     /**
  106.     * Returns the Datasets name.
  107.     *
  108.     * @return string 
  109.     * @access    public
  110.     */
  111.     function getDatasetName()
  112.     {
  113.         return $this->setName;
  114.     }
  115.     
  116.     /**
  117.      * Adds a NamedGraph to the set.
  118.      *
  119.      * @param NamedGraphDb 
  120.      */
  121.     function addNamedGraph(&$graph)
  122.     {
  123.         $graphNameURI=$graph->getGraphName();
  124.         $this->removeNamedGraph($graphNameURI);
  125.         $this->dbConnection->execute('INSERT INTO dataset_model VALUES("'.$this->setName.'",'.$graph->modelID.',"'.$graphNameURI.'")');
  126.     }
  127.     
  128.         
  129.     /**
  130.      * Overwrites the existting default graph.
  131.      *
  132.      * @param DbModel 
  133.      */
  134.     function setDefaultGraph(&$graph)
  135.     {
  136.         $this->dbConnection->execute('UPDATE datasets SET defaultModelUri ="'.$graph->modelURI.'"  WHERE datasetName ="'.$this->setName.'"');
  137.     }
  138.     
  139.     /**
  140.      * Returns a reference to the defaultGraph.
  141.      *     
  142.      * @return NamedGraphDb 
  143.      */
  144.     function getDefaultGraph()
  145.     {
  146.         $defaultGraphURI $this->dbConnection->GetOne("SELECT defaultModelUri FROM datasets WHERE datasetName ='".$this->setName."'");
  147.         return ($this->dbStore->getNamedGraphDb($defaultGraphURI,'http://rdfapi-php/dataset_defaultGraph_'.$this->setName));
  148.     }
  149.     
  150.     /**
  151.      * Returns true, if a defaultGraph exists. False otherwise.
  152.      *     
  153.      * @return boolean 
  154.      */    
  155.     function hasDefaultGraph()
  156.     {
  157.         return true;
  158.     }
  159.     
  160.     /**
  161.      * Removes a NamedGraph from the set. Nothing happens
  162.      * if no graph with that name is contained in the set.
  163.      *
  164.      * @param string 
  165.      */
  166.     function removeNamedGraph($graphName)
  167.     {
  168.         $this->dbConnection->execute('DELETE FROM dataset_model WHERE datasetName="'.$this->setName.'"  AND graphURI ="'.$graphName.'"');
  169.     }
  170.     
  171.     /**
  172.      * Tells wether the Dataset contains a NamedGraph.
  173.      *
  174.      * @param  string 
  175.      * @return boolean 
  176.      */
  177.     function containsNamedGraph($graphName)
  178.     {
  179.         $count$this->dbConnection->GetOne('SELECT count(*) FROM dataset_model WHERE datasetName="'.$this->setName.'"  AND graphURI ="'.$graphName.'"');
  180.         return ($count>0);    
  181.     }
  182.     
  183.     /**
  184.      * Returns the NamedGraph with a specific name from the Dataset.
  185.      * Changes to the graph will be reflected in the set.
  186.      *
  187.      * @param string 
  188.      * @return NamedGraphDb or null
  189.      */
  190.     function &getNamedGraph($graphName)
  191.     {
  192.         if(!$this->containsNamedGraph($graphName))
  193.             return null;
  194.             
  195.         $modelVars =$this->dbConnection->execute("SELECT models.modelURI, models.modelID, models.baseURI
  196.                                                     FROM models, dataset_model
  197.                                                     WHERE dataset_model.graphURI ='" .$graphName ."' AND dataset_model.modelId= models.modelID");
  198.     
  199.         return new NamedGraphDb($this->dbConnection$modelVars->fields[0],
  200.                                  $modelVars->fields[1]$graphName ,$modelVars->fields[2]);
  201.     }
  202.     
  203.     /**
  204.      * Returns the names of the namedGraphs in this set as strings in an array.
  205.      *
  206.      * @return Array 
  207.      */
  208.     function listGraphNames()
  209.     {
  210.         $recordSet =$this->dbConnection->execute("SELECT graphURI FROM dataset_model WHERE datasetName ='".$this->setName."'");
  211.  
  212.         $return=array();
  213.         while (!$recordSet->EOF
  214.         {
  215.           $return[$recordSet->fields[0];
  216.           $recordSet->moveNext();
  217.         }
  218.         return $return;
  219.     }
  220.  
  221.     /**
  222.      * Creates a new NamedGraph and adds it to the set. An existing graph with the same name will be replaced. But the old namedGraph remains in the database.
  223.      *
  224.      * @param  string 
  225.      * @param  string 
  226.      * @return NamedGraphDb 
  227.      */
  228.     function &createGraph($graphName,$baseURI null)
  229.     {
  230.         $graph =$this->dbStore->getNewNamedGraphDb(uniqid('http://rdfapi-php/namedGraph_'),$graphName,$baseURI);
  231.         $this->addNamedGraph(&$graph);
  232.         
  233.         return $graph;
  234.     }
  235.     
  236.     /**
  237.      * Deletes all NamedGraphs from the set.
  238.      */
  239.     function clear()
  240.     {
  241.         $this->dbConnection->execute("DELETE FROM dataset_model WHERE datasetName ='".$this->setName."'");    
  242.     }
  243.  
  244.     /** 
  245.      * Returns the number of NamedGraphs in the set. Empty graphs are counted.
  246.      *
  247.      * @return int 
  248.      */
  249.     function countGraphs(
  250.     {
  251.         return ($this->dbConnection->GetOne("SELECT count(*) FROM dataset_model WHERE datasetName ='".$this->setName."'"));    
  252.     }
  253.     
  254.     /**
  255.      * Returns an iterator over all {@link NamedGraph}s in the set.
  256.      *
  257.      * @return IteratorAllGraphsDb 
  258.      */
  259.     function &listNamedGraphs()
  260.     {
  261.         $recordSet =$this->dbConnection->execute("SELECT graphURI FROM dataset_model WHERE datasetName ='".$this->setName."'");
  262.         return (new IteratorAllGraphsDb(&$recordSet,&$this));
  263.     }
  264.     
  265.     /**
  266.      * Tells wether the set contains any NamedGraphs.
  267.      *
  268.      * @return boolean 
  269.      */
  270.     function isEmpty()
  271.     {
  272.         return ($this->countGraphs()==0);    
  273.     }
  274.  
  275.     /**
  276.      * Add all named graphs of the other dataset to this dataset.
  277.      *
  278.      * @param Dataset 
  279.      */
  280.     function addAll($otherDataset)
  281.     {
  282.         for($iterator $otherDataset->listNamedGraphs()$iterator->valid()$iterator->next()) 
  283.         {
  284.             $this->addNamedGraph($iterator->current());
  285.          };
  286.          
  287.          if ($otherDataset->hasDefaultGraph())
  288.          {
  289.              $this->defaultGraph $this->defaultGraph->unite($otherDataset->getDefaultGraph());    
  290.          }
  291.     }
  292.     
  293. //    === Quad level methods ========================
  294.  
  295.     
  296.     /**
  297.      * Adds a quad to the Dataset. The argument must not contain any
  298.      * wildcards. If the quad is already present, nothing happens. A new
  299.      * named graph will automatically be created if necessary.
  300.      *
  301.      * @param Quad 
  302.      */
  303.     function addQuad(&$quad)
  304.     {
  305.         $graphName=$quad->getGraphName();
  306.         $graphName=$graphName->getLabel();
  307.         
  308.         $graph=$this->getNamedGraph($graphName);
  309.         
  310.         if ($graph===null)
  311.             $graph=$this->createGraph($graphName);
  312.         
  313.         $statement=$quad->getStatement();
  314.         $graph->add($statement);
  315.     }
  316.     
  317.     /**
  318.      * Tells wether the Dataset contains a quad or
  319.      * quads matching a pattern.
  320.      * 
  321.      * @param Resource 
  322.      * @param Resource 
  323.      * @param Resource 
  324.      * @param Resource 
  325.      * @return boolean 
  326.      */
  327.     function containsQuad($graphName,$subject,$predicate,$object)
  328.     {
  329.         // static part of the sql statement
  330.         $sql "SELECT count(*)
  331.                   FROM statements, dataset_model
  332.                    WHERE datasetName ='".$this->setName."' AND statements.modelID=dataset_model.modelId ";
  333.  
  334.         if($graphName!=null)
  335.         {
  336.             $sql.= " AND graphURI ='".$graphName->getLabel()."'";
  337.         }
  338.         
  339.         // dynamic part of the sql statement
  340.         $sql .= DbModel::_createDynSqlPart_SPO($subject$predicate$object);
  341.  
  342.         return (($this->dbConnection->GetOne($sql))>0);
  343.     
  344.     
  345.     /**
  346.      * Deletes a Quad from the RDF dataset.
  347.      *
  348.      * @param Quad 
  349.      */
  350.     function removeQuad($quad)
  351.     {
  352.         $graphName=$quad->getGraphName();$graphName=$graphName->getLabel();
  353.         //find namedGraph IDs
  354.         $graphID $this->dbConnection->GetOne("SELECT modelId FROM dataset_model WHERE graphURI ='$graphName'");
  355.  
  356.         // static part of the sql statement
  357.         $sql "DELETE FROM statements WHERE modelID = $graphID";
  358.  
  359.         // dynamic part of the sql statement
  360.         $sql .= DbModel::_createDynSqlPart_SPO($quad->getSubject()$quad->getPredicate()$quad->getObject());
  361.  
  362.         // execute the query
  363.         if($graphID)
  364.             $recordSet =$this->dbConnection->execute($sql);
  365.     }
  366.     
  367.     /**
  368.      * Counts the Quads in the RDF dataset. Identical Triples in
  369.      * different NamedGraphs are counted individually.
  370.      *
  371.      * @return int 
  372.      */
  373.     function countQuads()
  374.     {
  375.         $sql "SELECT count(*)
  376.                   FROM statements, dataset_model
  377.                    WHERE datasetName ='".$this->setName."' AND statements.modelID=dataset_model.modelId ";
  378.         
  379.         return ((int)$this->dbConnection->GetOne($sql));
  380.     }
  381.     
  382.     /**
  383.      * Finds Statements that match a quad pattern. The argument may contain
  384.      * wildcards.
  385.      *
  386.      * @param Resource or null
  387.      * @param Resource or null
  388.      * @param Resource or null
  389.      * @param Resource or null
  390.      * @return IteratorFindQuadsDb 
  391.      */
  392.     function &findInNamedGraphs($graphName,$subject,$predicate,$object,$returnAsTriples =false )
  393.     {
  394.         // static part of the sql statement
  395.         $sql "SELECT subject, predicate, object, l_language, l_datatype, subject_is, object_is, dataset_model.graphURI
  396.                   FROM statements, dataset_model
  397.                    WHERE datasetName ='".$this->setName."' AND statements.modelID=dataset_model.modelId ";
  398.  
  399.         if($graphName!=null)
  400.         {
  401.             $sql.= " AND graphURI ='".$graphName->getLabel()."'";
  402.         }
  403.         
  404.         // dynamic part of the sql statement
  405.         $sql .= DbModel::_createDynSqlPart_SPO($subject$predicate$object);
  406.  
  407.         // execute the query
  408.         $recordSet =$this->dbConnection->execute($sql);
  409.         
  410.  
  411.         return new IteratorFindQuadsDb(&$recordSet,&$this,$returnAsTriples);
  412.     }
  413.     
  414.     /**
  415.      * Finds Statements that match a pattern in the default Graph. The argument may contain
  416.      * wildcards.
  417.      *
  418.      * @param Resource or null
  419.      * @param Resource or null
  420.      * @param Resource or null
  421.      * @return IteratorFindQuadsDb 
  422.      */
  423.     function &findInDefaultGraph($subject,$predicate,$object)
  424.     {
  425.         $defaultGraphID = (int)$this->dbConnection->GetOne("SELECT models.modelID FROM datasets, models WHERE datasets.datasetName ='".$this->setName."' AND datasets.defaultModelUri = models.modelURI");
  426.         // static part of the sql statement
  427.         $sql "SELECT subjectpredicateobjectl_languagel_datatypesubject_isobject_is
  428.                   FROM statements
  429.                    WHERE modelID ='$defaultGraphID'";
  430.         
  431.         // dynamic part of the sql statement
  432.         $sql .= DbModel::_createDynSqlPart_SPO($subject$predicate$object);
  433.  
  434.         // execute the query
  435.         $recordSet =$this->dbConnection->execute($sql);
  436.         
  437.         return new IteratorFindQuadsDb(&$recordSet,&$this,true);
  438.     }
  439. }
  440. ?>

Documentation generated on Mon, 26 Jun 2006 14:25:20 +0200 by phpDocumentor 1.3.0RC6