Source for file Preparator.php

Documentation is available at Preparator.php

  1. <?php
  2. require_once RDFAPI_INCLUDE_DIR 'sparql/SparqlEngineDb/Offsetter.php';
  3.  
  4. /**
  5. *   This class takes care of prepared statements:
  6. *   Preparing them in the database, replacing
  7. *
  8. *   @author Christian Weiske <cweiske@cweiske.de>
  9. *
  10. *   @package sparql
  11. */
  12. {
  13.  
  14.  
  15.  
  16.     public function __construct(Query $queryADOConnection $dbConn)
  17.     {
  18.         $this->query        $query;
  19.         $this->dbConn       $dbConn;
  20.         $this->arPrefixes   $this->query->getPrefixes();
  21.     }//public function __construct(Query $query, ADOConnection $dbConn)
  22.  
  23.  
  24.  
  25.     /**
  26.     *   Converts the given queries into sql prepared statments,
  27.     *   calls the prepare command in the database and returns
  28.     *   an array consisting of subarrays. They contain
  29.     *   the db's prepared statement as first value, and an array
  30.     *   of variable positions as second value (key is the position,
  31.     *   the sparql variable is the value).
  32.     *
  33.     *   @param array $arQueries      Array of sql queries part arrays
  34.     *   @param array $arPlaceholders Array of sparql (variable name =>
  35.     *                                 placeholder name) pairs
  36.     *   @return array  Array of (prepared statment, variable positions) pairs
  37.     */
  38.     public function prepareInDb($arQueries$arPlaceholders)
  39.     {
  40.         $arDbStatements array();
  41.  
  42.         foreach ($arQueries as $arQuery{
  43.             list($strPrepared$arVariablePositions$this->replacePlaceholders(
  44.                 implode(''$arQuery),
  45.                 $arPlaceholders
  46.             );
  47.             if (count($arQueries== 1{
  48.                 //I currently haven't seens one case in which the count was > 1
  49.                 //if that happens, we will need to add a fix
  50.                 $strPrepared .= SparqlEngineDb_Offsetter::getLimitSql(
  51.                     $this->query,
  52.                     $this->dbConn
  53.                 );
  54.             }
  55.             $stmt $this->dbConn->Prepare($strPrepared);
  56.             $arDbStatements[array(
  57.                 $stmt,
  58.                 $arVariablePositions
  59.             );
  60.         }
  61.  
  62.         return $arDbStatements;
  63.     }//public function prepareInDb($arQueries, $arPlaceholders)
  64.  
  65.  
  66.  
  67.     /**
  68.     *   Replaces the placeholders in the given SQL statement with real
  69.     *   SQL prepared statements placeholders.
  70.     *
  71.     *   @param string $strQuery SQL query with placeholders
  72.     *   @param array $arPlaceholders Array of sparql (variable name =>
  73.     *                                 placeholder name) pairs
  74.     *   @return array (prepared sql query string, variable positions) pair
  75.     */
  76.     protected function replacePlaceholders($strQuery$arPlaceholders)
  77.     {
  78.         $arVariablePositions array();
  79.         $this->arTmpVariablePositions array();
  80.         $this->arTmpPlaceholders      $arPlaceholders;
  81.         $strQuery preg_replace_callback(
  82.             '/@\\$%_PLACEHOLDER_[0-9]+_%\\$@/',
  83.             array($this'replacePlaceholdersCb'),
  84.             $strQuery
  85.         );
  86.         return array($strQuery$this->arTmpVariablePositions);
  87.     }//protected function replacePlaceholders($strQuery, $arPlaceholders)
  88.  
  89.  
  90.  
  91.     /**
  92.     *   Callback method internally used by replacePlaceholders() method.
  93.     */
  94.     protected function replacePlaceholdersCb($matches)
  95.     {
  96.         $strPlaceholder     $matches[0];
  97.         $strSparqlVariable  array_search(
  98.             $strPlaceholder,
  99.             $this->arTmpPlaceholders
  100.         );
  101.         $strDbPlaceholder   $this->dbConn->Param($strSparqlVariable);
  102.         $this->arTmpVariablePositions[$strSparqlVariable;
  103.  
  104.         return $strDbPlaceholder;
  105.     }//protected function replacePlaceholdersCb($matches)
  106.  
  107.  
  108.  
  109.     /**
  110.     *   Executes the given prepared statments, filling the placeholders
  111.     *   with the given values.
  112.     *
  113.     *   @param array $arDbStatements    Return value of prepareInDb()
  114.     *   @param array $arVariableValues      Array of (variable name, value) pairs
  115.     *
  116.     *   @return array Array of database results as returned by Execute()
  117.     */
  118.     public function execute($arDbStatements$arVariableValues)
  119.     {
  120.         $arResults array();
  121.         $oldmode $this->dbConn->SetFetchMode(ADODB_FETCH_ASSOC);
  122.  
  123.         foreach ($arDbStatements as $arStatement{
  124.             list($stmt$arVariablePositions$arStatement;
  125.             $arVariables $this->createVariableArray(
  126.                 $arVariablePositions,
  127.                 $arVariableValues
  128.             );
  129.             $arResults[$this->dbConn->Execute($stmt$arVariables);
  130.         }
  131.  
  132.         $this->dbConn->SetFetchMode($oldmode);
  133.  
  134.         return $arResults;
  135.     }//public function execute($arDbStatements, $arVariableValues)
  136.  
  137.  
  138.  
  139.     /**
  140.     *   Creates an array full of variables to be passed to the Execute() method
  141.     *   of the database connection object.
  142.     *   Uses the variable positions array to get the positions of the variables
  143.     *   in the result array, and the variable value array to get the actual
  144.     *   values for the prepared statement.
  145.     *
  146.     *   @param array $arVariablePositions   Positions of the variables as returned
  147.     *                                        by replacePlaceholders().
  148.     *   @param array $arVariableValues      Array of (variable name, value) pairs
  149.     *
  150.     *   @return array Array of variable values
  151.     */
  152.     protected function createVariableArray($arVariablePositions$arVariableValues)
  153.     {
  154.         $arVariables array();
  155.  
  156.         foreach ($arVariablePositions as $nPos => $strVariable{
  157.             if (!isset($arVariableValues[$strVariable])) {
  158.                 throw new Exception('No value for variable "' $strVariable '" in prepared statement');
  159.             }
  160.  
  161.             $strValue self::replacePrefix(
  162.                 $arVariableValues[$strVariable],
  163.                 $this->arPrefixes
  164.             );
  165.  
  166.             $arVariables[$nPos$strValue;
  167.         }
  168.  
  169.         return $arVariables;
  170.     }//protected function createVariableArray($arVariablePositions, $arVariableValues)
  171.  
  172.  
  173.  
  174.     /**
  175.     *   Replaces all placeholders with their actual values.
  176.     */
  177.     public function replacePlaceholdersWithVariables($strQuery$arPlaceholders$arVariableValues)
  178.     {
  179.         $this->arTmpPlaceholders   $arPlaceholders;
  180.         $this->arTmpVariableValues $arVariableValues;
  181.  
  182.         $strQuery preg_replace_callback(
  183.             '/@\\$%_PLACEHOLDER_[0-9]+_%\\$@/',
  184.             array($this'replacePlaceholdersWithVariablesCb'),
  185.             $strQuery
  186.         );
  187.  
  188.         return $strQuery;
  189.     }//public function replacePlaceholdersWithVariables($strQuery, $arPlaceholders, $arVariableValues)
  190.  
  191.  
  192.  
  193.     /**
  194.     *   Callback method internally used by
  195.     *   replacePlaceholdersWithVariables() method.
  196.     */
  197.     protected function replacePlaceholdersWithVariablesCb($matches)
  198.     {
  199.         $strPlaceholder     $matches[0];
  200.         $strSparqlVariable  array_search(
  201.             $strPlaceholder,
  202.             $this->arTmpPlaceholders
  203.         );
  204.         return $this->dbConn->qstr(
  205.             self::replacePrefix(
  206.                 $this->arTmpVariableValues[$strSparqlVariable],
  207.                 $this->arPrefixes
  208.             )
  209.         );
  210.     }//protected function replacePlaceholdersWithVariablesCb($matches)
  211.  
  212.  
  213.  
  214.     protected static function replacePrefix($strValue$arPrefixes)
  215.     {
  216.         //replace prefixes?
  217.         if (count($arParts explode(':'$strValue2)) == 2{
  218.             if (isset($arPrefixes[$arParts[0]])) {
  219.                 $strValue $arPrefixes[$arParts[0]] $arParts[1];
  220.             }
  221.         }
  222.         return $strValue;
  223.     }//protected static function replacePrefix($strValue, $arPrefixes)
  224.  
  225. }//class SparqlEngineDb_Preparator
  226.  
  227. ?>

Documentation generated on Fri, 1 Jun 2007 16:50:25 +0200 by phpDocumentor 1.3.2