torrentpier-lts/library/Zend/Db/Sql/AbstractSql.php

382 lines
14 KiB
PHP
Raw Normal View History

<?php
/**
* Zend Framework (http://framework.zend.com/)
*
* @link http://github.com/zendframework/zf2 for the canonical source repository
2023-04-01 09:03:34 +03:00
* @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
*/
namespace Zend\Db\Sql;
use Zend\Db\Adapter\Driver\DriverInterface;
use Zend\Db\Adapter\ParameterContainer;
use Zend\Db\Adapter\Platform\PlatformInterface;
use Zend\Db\Sql\Platform\PlatformDecoratorInterface;
2023-04-01 09:03:34 +03:00
use Zend\Db\Adapter\Platform\Sql92 as DefaultAdapterPlatform;
2023-04-01 09:03:34 +03:00
abstract class AbstractSql implements SqlInterface
{
/**
2023-04-01 09:03:34 +03:00
* Specifications for Sql String generation
*
* @var string[]|array[]
*/
protected $specifications = array();
/**
* @var string
*/
protected $processInfo = array('paramPrefix' => '', 'subselectCount' => 0);
/**
* @var array
*/
protected $instanceParameterIndex = array();
2023-04-01 09:03:34 +03:00
/**
* {@inheritDoc}
*/
public function getSqlString(PlatformInterface $adapterPlatform = null)
{
2023-04-01 09:03:34 +03:00
$adapterPlatform = ($adapterPlatform) ?: new DefaultAdapterPlatform;
return $this->buildSqlString($adapterPlatform);
}
/**
* @param PlatformInterface $platform
* @param null|DriverInterface $driver
* @param null|ParameterContainer $parameterContainer
* @return string
*/
protected function buildSqlString(
PlatformInterface $platform,
DriverInterface $driver = null,
ParameterContainer $parameterContainer = null
) {
$this->localizeVariables();
$sqls = array();
$parameters = array();
foreach ($this->specifications as $name => $specification) {
$parameters[$name] = $this->{'process' . $name}(
$platform,
$driver,
$parameterContainer,
$sqls,
$parameters
);
if ($specification && is_array($parameters[$name])) {
$sqls[$name] = $this->createSqlFromSpecificationAndParameters($specification, $parameters[$name]);
continue;
}
if (is_string($parameters[$name])) {
$sqls[$name] = $parameters[$name];
}
}
return rtrim(implode(' ', $sqls), "\n ,");
}
/**
* @staticvar int $runtimeExpressionPrefix
* @param ExpressionInterface $expression
* @param PlatformInterface $platform
* @param null|DriverInterface $driver
* @param null|ParameterContainer $parameterContainer
* @param null|string $namedParameterPrefix
* @return string
* @throws Exception\RuntimeException
*/
protected function processExpression(
ExpressionInterface $expression,
PlatformInterface $platform,
DriverInterface $driver = null,
ParameterContainer $parameterContainer = null,
$namedParameterPrefix = null
) {
$namedParameterPrefix = ! $namedParameterPrefix
? $namedParameterPrefix
: $this->processInfo['paramPrefix'] . $namedParameterPrefix;
// static counter for the number of times this method was invoked across the PHP runtime
static $runtimeExpressionPrefix = 0;
2023-04-01 09:03:34 +03:00
if ($parameterContainer && ((!is_string($namedParameterPrefix) || $namedParameterPrefix == ''))) {
$namedParameterPrefix = sprintf('expr%04dParam', ++$runtimeExpressionPrefix);
2023-04-01 09:03:34 +03:00
} else {
$namedParameterPrefix = preg_replace('/\s/', '__', $namedParameterPrefix);
}
$sql = '';
// initialize variables
$parts = $expression->getExpressionData();
2023-04-01 09:03:34 +03:00
if (! isset($this->instanceParameterIndex[$namedParameterPrefix])) {
$this->instanceParameterIndex[$namedParameterPrefix] = 1;
}
$expressionParamIndex = &$this->instanceParameterIndex[$namedParameterPrefix];
foreach ($parts as $part) {
2023-04-01 09:03:34 +03:00
// #7407: use $expression->getExpression() to get the unescaped
// version of the expression
if (is_string($part) && $expression instanceof Expression) {
$sql .= $expression->getExpression();
continue;
}
// If it is a string, simply tack it onto the return sql
// "specification" string
if (is_string($part)) {
$sql .= $part;
continue;
}
2023-04-01 09:03:34 +03:00
if (! is_array($part)) {
throw new Exception\RuntimeException(
'Elements returned from getExpressionData() array must be a string or array.'
);
}
2023-04-01 09:03:34 +03:00
// Process values and types (the middle and last position of the
// expression data)
$values = $part[1];
2023-04-01 09:03:34 +03:00
$types = isset($part[2]) ? $part[2] : array();
foreach ($values as $vIndex => $value) {
2023-04-01 09:03:34 +03:00
if (!isset($types[$vIndex])) {
continue;
}
$type = $types[$vIndex];
if ($value instanceof Select) {
// process sub-select
2023-04-01 09:03:34 +03:00
$values[$vIndex] = '('
. $this->processSubSelect($value, $platform, $driver, $parameterContainer)
. ')';
} elseif ($value instanceof ExpressionInterface) {
// recursive call to satisfy nested expressions
2023-04-01 09:03:34 +03:00
$values[$vIndex] = $this->processExpression(
$value,
$platform,
$driver,
$parameterContainer,
$namedParameterPrefix . $vIndex . 'subpart'
);
} elseif ($type == ExpressionInterface::TYPE_IDENTIFIER) {
$values[$vIndex] = $platform->quoteIdentifierInFragment($value);
} elseif ($type == ExpressionInterface::TYPE_VALUE) {
// if prepareType is set, it means that this particular value must be
// passed back to the statement in a way it can be used as a placeholder value
2023-04-01 09:03:34 +03:00
if ($parameterContainer) {
$name = $namedParameterPrefix . $expressionParamIndex++;
$parameterContainer->offsetSet($name, $value);
$values[$vIndex] = $driver->formatParameterName($name);
continue;
}
// if not a preparable statement, simply quote the value and move on
$values[$vIndex] = $platform->quoteValue($value);
2023-04-01 09:03:34 +03:00
} elseif ($type == ExpressionInterface::TYPE_LITERAL) {
$values[$vIndex] = $value;
}
}
2023-04-01 09:03:34 +03:00
// After looping the values, interpolate them into the sql string
// (they might be placeholder names, or values)
$sql .= vsprintf($part[0], $values);
}
2023-04-01 09:03:34 +03:00
return $sql;
}
/**
2023-04-01 09:03:34 +03:00
* @param string|array $specifications
* @param string|array $parameters
*
* @return string
2023-04-01 09:03:34 +03:00
*
* @throws Exception\RuntimeException
*/
protected function createSqlFromSpecificationAndParameters($specifications, $parameters)
{
if (is_string($specifications)) {
return vsprintf($specifications, $parameters);
}
$parametersCount = count($parameters);
2023-04-01 09:03:34 +03:00
foreach ($specifications as $specificationString => $paramSpecs) {
if ($parametersCount == count($paramSpecs)) {
break;
}
2023-04-01 09:03:34 +03:00
unset($specificationString, $paramSpecs);
}
if (!isset($specificationString)) {
throw new Exception\RuntimeException(
'A number of parameters was found that is not supported by this specification'
);
}
$topParameters = array();
foreach ($parameters as $position => $paramsForPosition) {
if (isset($paramSpecs[$position]['combinedby'])) {
$multiParamValues = array();
foreach ($paramsForPosition as $multiParamsForPosition) {
$ppCount = count($multiParamsForPosition);
if (!isset($paramSpecs[$position][$ppCount])) {
2023-04-01 09:03:34 +03:00
throw new Exception\RuntimeException(sprintf(
'A number of parameters (%d) was found that is not supported by this specification',
$ppCount
));
}
$multiParamValues[] = vsprintf($paramSpecs[$position][$ppCount], $multiParamsForPosition);
}
$topParameters[] = implode($paramSpecs[$position]['combinedby'], $multiParamValues);
} elseif ($paramSpecs[$position] !== null) {
$ppCount = count($paramsForPosition);
if (!isset($paramSpecs[$position][$ppCount])) {
2023-04-01 09:03:34 +03:00
throw new Exception\RuntimeException(sprintf(
'A number of parameters (%d) was found that is not supported by this specification',
$ppCount
));
}
$topParameters[] = vsprintf($paramSpecs[$position][$ppCount], $paramsForPosition);
} else {
$topParameters[] = $paramsForPosition;
}
}
return vsprintf($specificationString, $topParameters);
}
2023-04-01 09:03:34 +03:00
/**
* @param Select $subselect
* @param PlatformInterface $platform
* @param null|DriverInterface $driver
* @param null|ParameterContainer $parameterContainer
* @return string
*/
protected function processSubSelect(
Select $subselect,
PlatformInterface $platform,
DriverInterface $driver = null,
ParameterContainer $parameterContainer = null
) {
if ($this instanceof PlatformDecoratorInterface) {
$decorator = clone $this;
$decorator->setSubject($subselect);
} else {
$decorator = $subselect;
}
2023-04-01 09:03:34 +03:00
if ($parameterContainer) {
// Track subselect prefix and count for parameters
2023-04-01 09:03:34 +03:00
$processInfoContext = ($decorator instanceof PlatformDecoratorInterface) ? $subselect : $decorator;
$this->processInfo['subselectCount']++;
2023-04-01 09:03:34 +03:00
$processInfoContext->processInfo['subselectCount'] = $this->processInfo['subselectCount'];
$processInfoContext->processInfo['paramPrefix'] = 'subselect'
. $processInfoContext->processInfo['subselectCount'];
$sql = $decorator->buildSqlString($platform, $driver, $parameterContainer);
// copy count
2023-04-01 09:03:34 +03:00
$this->processInfo['subselectCount'] = $decorator->processInfo['subselectCount'];
return $sql;
}
2023-04-01 09:03:34 +03:00
return $decorator->buildSqlString($platform, $driver, $parameterContainer);
}
/**
* @param null|array|ExpressionInterface|Select $column
* @param PlatformInterface $platform
* @param null|DriverInterface $driver
* @param null|string $namedParameterPrefix
* @param null|ParameterContainer $parameterContainer
* @return string
*/
protected function resolveColumnValue(
$column,
PlatformInterface $platform,
DriverInterface $driver = null,
ParameterContainer $parameterContainer = null,
$namedParameterPrefix = null
) {
$namedParameterPrefix = ! $namedParameterPrefix
? $namedParameterPrefix
: $this->processInfo['paramPrefix'] . $namedParameterPrefix;
$isIdentifier = false;
$fromTable = '';
if (is_array($column)) {
if (isset($column['isIdentifier'])) {
$isIdentifier = (bool) $column['isIdentifier'];
}
if (isset($column['fromTable']) && $column['fromTable'] !== null) {
$fromTable = $column['fromTable'];
}
2023-04-01 09:03:34 +03:00
$column = $column['column'];
}
if ($column instanceof ExpressionInterface) {
return $this->processExpression($column, $platform, $driver, $parameterContainer, $namedParameterPrefix);
}
if ($column instanceof Select) {
return '(' . $this->processSubSelect($column, $platform, $driver, $parameterContainer) . ')';
}
if ($column === null) {
return 'NULL';
}
return $isIdentifier
? $fromTable . $platform->quoteIdentifierInFragment($column)
: $platform->quoteValue($column);
}
/**
* @param string|TableIdentifier|Select $table
* @param PlatformInterface $platform
* @param DriverInterface $driver
* @param ParameterContainer $parameterContainer
* @return string
*/
protected function resolveTable(
$table,
PlatformInterface $platform,
DriverInterface $driver = null,
ParameterContainer $parameterContainer = null
) {
$schema = null;
if ($table instanceof TableIdentifier) {
list($table, $schema) = $table->getTableAndSchema();
}
if ($table instanceof Select) {
$table = '(' . $this->processSubselect($table, $platform, $driver, $parameterContainer) . ')';
} elseif ($table) {
$table = $platform->quoteIdentifier($table);
}
if ($schema && $table) {
$table = $platform->quoteIdentifier($schema) . $platform->getIdentifierSeparator() . $table;
}
return $table;
}
/**
* Copy variables from the subject into the local properties
*/
protected function localizeVariables()
{
if (! $this instanceof PlatformDecoratorInterface) {
return;
}
foreach (get_object_vars($this->subject) as $name => $value) {
$this->{$name} = $value;
}
}
}