Source for file DatasetDb.php

Documentation is available at DatasetDb.php

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

Documentation generated on Tue, 01 May 2007 16:43:29 +0200 by phpDocumentor 1.3.2