Source for file SparqlEngineDb.php

Documentation is available at SparqlEngineDb.php

  1. <?php
  2. require_once RDFAPI_INCLUDE_DIR 'sparql/SparqlEngine.php';
  3. require_once RDFAPI_INCLUDE_DIR 'sparql/SparqlEngineDb/Offsetter.php';
  4. require_once RDFAPI_INCLUDE_DIR 'sparql/SparqlEngineDb/ResultConverter.php';
  5. require_once RDFAPI_INCLUDE_DIR 'sparql/SparqlEngineDb/SqlGenerator.php';
  6. require_once RDFAPI_INCLUDE_DIR 'sparql/SparqlEngineDb/SqlMerger.php';
  7.  
  8. /**
  9. *   SPARQL engine optimized for databases.
  10. *   Generates SQL statements to directly query the database,
  11. *   letting the database system do all the hard work like
  12. *   selecting, joining, filtering and ordering results.
  13. *
  14. *   @author Christian Weiske <cweiske@cweiske.de>
  15. *
  16. *   @package sparql
  17. */
  18. {
  19.     /**
  20.     *   Sparql Query object.
  21.     *
  22.     *   @var Query 
  23.     */
  24.     protected $query;
  25.  
  26.     /**
  27.     *   RDF dataset object.
  28.     *   @var Dataset 
  29.     */
  30.     protected $dataset;
  31.  
  32.     /**
  33.     *   Database connection object.
  34.     *   @var ADOConnection 
  35.     */
  36.     protected $dbConn;
  37.  
  38.     /**
  39.     *   Internal ID for our graph model.
  40.     *   Stored in the database along the statements.
  41.     *   @var int 
  42.     */
  43.     protected $modelID;
  44.  
  45.     /**
  46.     *   Prepared SQL statements are stored in here.
  47.     *   @var array 
  48.     */
  49.     protected $arPrepared    = null;
  50.  
  51.     /**
  52.     *   If the prepared statement is really prepared, or if we just emulate it.
  53.     *   @var boolean 
  54.     */
  55.     protected $bRealPrepared = false;
  56.  
  57.     /**
  58.     *   SQL generator instance
  59.     *   @var SparqlEngineDb_SqlGenerator 
  60.     */
  61.     protected $sg = null;
  62.  
  63.     /**
  64.     *   Type sorting instance
  65.     *   @var SparqlEngineDb_TypeSorter 
  66.     */
  67.     protected $ts = null;
  68.  
  69.     /**
  70.     *   Prepared statments preparator instance
  71.     *   @var SparqlEngineDb_Preparator 
  72.     */
  73.     protected $pr = null;
  74.  
  75.  
  76.  
  77.     /**
  78.     *   Use SparqlEngine::factory() to create the instance
  79.     */
  80.     public function __construct(DbModel $model)
  81.     {
  82.         //parent::__construct();
  83.         $this->dbConn  = $model->getDbConn();
  84.         $this->modelID = $model->getModelID();
  85.     }//public function __construct(DbModel $model)
  86.  
  87.  
  88.  
  89.     /**
  90.     *   Query the database with the given SPARQL query.
  91.     *
  92.     *
  93.     *   @param  Dataset       $dataset    RDF Dataset
  94.     *   @param  Query         $query      Parsed SPARQL query
  95.     *   @param  string        $resultform Result form. If set to 'xml' the result will be
  96.     *                                    SPARQL Query Results XML Format as described in http://www.w3.org/TR/rdf-sparql-XMLres/ .
  97.     *
  98.     *   @return array/string  array of triple arrays, or XML. Format depends on
  99.     *                                    $resultform parameter.
  100.     */
  101.     public function queryModel(Dataset $datasetQuery $query$resultform false)
  102.     {
  103.         $this->query   = $query;
  104.         $this->dataset = $dataset;
  105.         $this->sg = new SparqlEngineDb_SqlGenerator   ($this->query$this->dbConn$this->modelID);
  106.         $this->rc new SparqlEngineDb_ResultConverter($this->query$this->sg$this);
  107.         $this->ts = new SparqlEngineDb_TypeSorter     ($this->query$this->dbConn);
  108.  
  109.         if($this->query->isEmpty()){
  110.             $vartable[0]['patternResult'null;
  111.             return $this->returnResult($vartable$resultform);
  112.         }
  113.  
  114.  
  115.         $arSqls $this->sg->createSql();
  116.         $this->ts->setData($this->sg);
  117.  
  118.         return
  119.                 $this->queryMultiple(
  120.                     $this->ts->getOrderifiedSqls(
  121.                         $arSqls
  122.                     )
  123.                 ),
  124.                 $this,
  125.                 $resultform
  126.             );
  127.     }//public function queryModel(Dataset $dataset, Query $query, $resultform = false)
  128.  
  129.  
  130.  
  131.     /**
  132.     *   Create a prepared statement that can be executed later.
  133.     *
  134.     *   @param  Dataset       $dataset    RDF Dataset
  135.     *   @param  Query         $query      Parsed SPARQL query
  136.     *
  137.     *   @return SparqlEngineDb_PreparedStatement Prepared statment that can
  138.     *            be execute()d later.
  139.     */
  140.     public function prepare(Dataset $datasetQuery $query)
  141.     {
  142.         require_once RDFAPI_INCLUDE_DIR 'sparql/SparqlEngineDb/PreparedStatement.php';
  143.         require_once RDFAPI_INCLUDE_DIR 'sparql/SparqlEngineDb/Preparator.php';
  144.  
  145.         $this->query   = $query;
  146.         $this->dataset = $dataset;
  147.         $this->sg = new SparqlEngineDb_SqlGenerator   ($this->query$this->dbConn$this->modelID);
  148.         $this->rc new SparqlEngineDb_ResultConverter($this->query$this->sg$this);
  149.         $this->ts = new SparqlEngineDb_TypeSorter     ($this->query$this->dbConn);
  150.         $this->pr = new SparqlEngineDb_Preparator     ($this->query$this->dbConn);
  151.  
  152.         $this->arPrepared = $this->sg->createSql();
  153.         $this->ts->setData($this->sg);
  154.  
  155.         if ($this->ts->willBeDataDependent()) {
  156.             $this->bRealPrepared = false;
  157.         else {
  158.             $this->bRealPrepared     = true;
  159.             list($strSelect$strFrom$strWhere$this->arPrepared;
  160.             $this->arPreparedQueries $this->ts->getOrderifiedSqls(
  161.                 $strSelect,
  162.                 $strFrom,
  163.                 $strWhere
  164.             );
  165.             $this->arDbStatements    $this->pr->prepareInDb(
  166.                 $this->arPreparedQueries,
  167.                 $this->sg->getPlaceholders()
  168.             );
  169.         }
  170.  
  171.  
  172.         return new SparqlEngineDb_PreparedStatement(
  173.             $this
  174.         );
  175.     }//public function prepare(Dataset $dataset, Query $query)
  176.  
  177.  
  178.  
  179.     /**
  180.     *   Execute a prepared statement by filling it with variables
  181.     *
  182.     *   @param array $arVariables   Array with (variable name => value) pairs
  183.     *   @param string $resultform   Which form the result should have
  184.     *
  185.     *   @return mixed   Result according to $resultform
  186.     */
  187.     public function execute($arVariables$resultform false)
  188.     {
  189.         if ($this->arPrepared === null{
  190.             throw new Exception('You need to prepare() the query first.');
  191.         }
  192.  
  193.         if ($this->bRealPrepared{
  194.             return
  195.                 SparqlEngineDb_ResultConverter::convertFromDbResults(
  196.                     $this->pr->execute(
  197.                         $this->arDbStatements,
  198.                         $arVariables
  199.                     ),
  200.                     $this,
  201.                     $resultform
  202.                 );
  203.         else {
  204.             list($strSelect$strFrom$strWhere$this->arPrepared;
  205.  
  206.             return SparqlEngineDb_ResultConverter::convertFromDbResults(
  207.                 $this->queryMultiple(
  208.                     $this->ts->getOrderifiedSqls(
  209.                         $strSelect,
  210.                         $strFrom,
  211.                         $this->pr->replacePlaceholdersWithVariables(
  212.                             $strWhere,
  213.                             $this->sg->getPlaceholders(),
  214.                             $arVariables
  215.                         )
  216.                     )
  217.                 ),
  218.                 $this,
  219.                 $resultform
  220.             );
  221.         }
  222.     }//public function execute($arVariables, $resultform)
  223.  
  224.  
  225.  
  226.     /**
  227.     *   Executes multiple SQL queries and returns an array
  228.     *   of results.
  229.     *
  230.     *   @param array $arSqls     Array of SQL queries
  231.     *   @return array   Array of query results
  232.     */
  233.     protected function queryMultiple($arSqls)
  234.     {
  235.         $arSM $this->query->getSolutionModifier();
  236.         if ($arSM['limit'=== null && $arSM['offset'=== null{
  237.             $nOffset 0;
  238.             $nLimit  null;
  239.             $nSql    0;
  240.         else {
  241.             $offsetter new SparqlEngineDb_Offsetter($this->dbConn$this->query);
  242.             list($nSql$nOffset$offsetter->determineOffset($arSqls);
  243.             $nLimit    $arSM['limit'];
  244.         }
  245.  
  246.         $nCount    0;
  247.         $arResults array();
  248.         foreach ($arSqls as $nId => $arSql{
  249.             if ($nId $nSqlcontinue}
  250.  
  251.             if ($nLimit != null{
  252.                 $nCurrentLimit $nLimit $nCount;
  253.             else {
  254.                 $nCurrentLimit null;
  255.             }
  256.  
  257.             $dbResult    $this->queryDb($arSql$nOffset$nCurrentLimit);
  258.             $nCount     += $dbResult->RowCount();
  259.             $arResults[$dbResult;
  260.             $nOffset 0;
  261.             if ($nLimit !== null && $nCount >= $nLimit{
  262.                 break;
  263.             }
  264.         }
  265.  
  266.         return $arResults;
  267.         //return array_map(array($this, 'queryDb'), $arSql);
  268.     }//protected function queryMultiple($arSql)
  269.  
  270.  
  271.  
  272.     /**
  273.     *   Sends the sql to the database and returns the results.
  274.     *
  275.     *   @internal Switches between ADOConnection::Execute() and
  276.     *     ADOConnection::SelectLimit() depending on the $query parameter's
  277.     *     $solutionModifier "limit" and "offset" settings.
  278.     *    Uses $query variable.
  279.     *
  280.     *   @param array $arSql     Array that gets a SQL query string once imploded
  281.     *
  282.     *   @return mixed           Anything ADOConnection::Execute() may return
  283.     *   @throws Exception       If Database query does not work
  284.     */
  285.     function queryDb($arSql$nOffset$nLimit)
  286.     {
  287.         $strSql SparqlEngineDb_SqlMerger::getSelect($this->query$arSql);
  288.  
  289.         if ($strSql == '()'{
  290.             return new ADORecordSet(false);
  291.         }
  292.  
  293.         // I want associative arrays.
  294.         $oldmode $this->dbConn->SetFetchMode(ADODB_FETCH_ASSOC);
  295.  
  296. //var_dump($strSql);
  297.         if ($nLimit === null && $nOffset == 0{
  298.             $ret $this->dbConn->execute($strSql);
  299.         else if ($nLimit === null{
  300.             $ret $this->dbConn->SelectLimit($strSql-1$nOffset);
  301.         else {
  302.             $ret $this->dbConn->SelectLimit($strSql$nLimit$nOffset);
  303.         }
  304.  
  305.         //... but others maybe not
  306.         $this->dbConn->SetFetchMode($oldmode);
  307.  
  308.         if (!$ret{
  309.             //Error occured
  310.             throw new Exception(
  311.                 'ADOdb error: ' $this->dbConn->ErrorMsg("\n"
  312.                 . $strSql
  313.             );
  314.         }
  315.  
  316.         return $ret;
  317.     }//function queryDb($sql)
  318.  
  319.  
  320.  
  321.     /*
  322.     *   Dumb getters
  323.     */
  324.  
  325.  
  326.  
  327.     public function getQuery()
  328.     {
  329.         return $this->query;
  330.     }//public function getQuery()
  331.  
  332.  
  333.  
  334.     public function getSqlGenerator()
  335.     {
  336.         return $this->sg;
  337.     }//public function getSqlGenerator()
  338.  
  339.  
  340.  
  341.     public function getTypeSorter()
  342.     {
  343.         return $this->ts;
  344.     }//public function getTypeSorter()
  345.  
  346. }//class SparqlEngineDb
  347. ?>

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