3 * Zend Framework (http://framework.zend.com/)
5 * @link http://github.com/zendframework/zf2 for the canonical source repository
6 * @copyright Copyright (c) 2005-2013 Zend Technologies USA Inc. (http://www.zend.com)
7 * @license http://framework.zend.com/license/new-bsd New BSD License
10 namespace Zend\Db\Sql
;
12 use Zend\Db\Adapter\Driver\DriverInterface
;
13 use Zend\Db\Adapter\ParameterContainer
;
14 use Zend\Db\Adapter\Platform\PlatformInterface
;
15 use Zend\Db\Adapter\StatementContainer
;
17 abstract class AbstractSql
22 protected $specifications = array();
27 protected $processInfo = array('paramPrefix' => '', 'subselectCount' => 0);
32 protected $instanceParameterIndex = array();
34 protected function processExpression(ExpressionInterface
$expression, PlatformInterface
$platform, DriverInterface
$driver = null, $namedParameterPrefix = null)
36 // static counter for the number of times this method was invoked across the PHP runtime
37 static $runtimeExpressionPrefix = 0;
39 if ($driver && ((!is_string($namedParameterPrefix) ||
$namedParameterPrefix == ''))) {
40 $namedParameterPrefix = sprintf('expr%04dParam', ++
$runtimeExpressionPrefix);
44 $statementContainer = new StatementContainer
;
45 $parameterContainer = $statementContainer->getParameterContainer();
47 // initialize variables
48 $parts = $expression->getExpressionData();
50 if (!isset($this->instanceParameterIndex
[$namedParameterPrefix])) {
51 $this->instanceParameterIndex
[$namedParameterPrefix] = 1;
54 $expressionParamIndex = &$this->instanceParameterIndex
[$namedParameterPrefix];
56 foreach ($parts as $part) {
58 // if it is a string, simply tack it onto the return sql "specification" string
59 if (is_string($part)) {
64 if (!is_array($part)) {
65 throw new Exception\
RuntimeException('Elements returned from getExpressionData() array must be a string or array.');
68 // process values and types (the middle and last position of the expression data)
70 $types = (isset($part[2])) ?
$part[2] : array();
71 foreach ($values as $vIndex => $value) {
72 if (isset($types[$vIndex]) && $types[$vIndex] == ExpressionInterface
::TYPE_IDENTIFIER
) {
73 $values[$vIndex] = $platform->quoteIdentifierInFragment($value);
74 } elseif (isset($types[$vIndex]) && $types[$vIndex] == ExpressionInterface
::TYPE_VALUE
&& $value instanceof Select
) {
77 $values[$vIndex] = '(' . $this->processSubSelect($value, $platform, $driver, $parameterContainer) . ')';
79 $values[$vIndex] = '(' . $this->processSubSelect($value, $platform) . ')';
81 } elseif (isset($types[$vIndex]) && $types[$vIndex] == ExpressionInterface
::TYPE_VALUE
&& $value instanceof ExpressionInterface
) {
82 // recursive call to satisfy nested expressions
83 $innerStatementContainer = $this->processExpression($value, $platform, $driver, $namedParameterPrefix . $vIndex . 'subpart');
84 $values[$vIndex] = $innerStatementContainer->getSql();
86 $parameterContainer->merge($innerStatementContainer->getParameterContainer());
88 } elseif (isset($types[$vIndex]) && $types[$vIndex] == ExpressionInterface
::TYPE_VALUE
) {
90 // if prepareType is set, it means that this particular value must be
91 // passed back to the statement in a way it can be used as a placeholder value
93 $name = $namedParameterPrefix . $expressionParamIndex++
;
94 $parameterContainer->offsetSet($name, $value);
95 $values[$vIndex] = $driver->formatParameterName($name);
99 // if not a preparable statement, simply quote the value and move on
100 $values[$vIndex] = $platform->quoteValue($value);
101 } elseif (isset($types[$vIndex]) && $types[$vIndex] == ExpressionInterface
::TYPE_LITERAL
) {
102 $values[$vIndex] = $value;
106 // after looping the values, interpolate them into the sql string (they might be placeholder names, or values)
107 $sql .= vsprintf($part[0], $values);
110 $statementContainer->setSql($sql);
111 return $statementContainer;
115 * @param $specifications
118 * @throws Exception\RuntimeException
120 protected function createSqlFromSpecificationAndParameters($specifications, $parameters)
122 if (is_string($specifications)) {
123 return vsprintf($specifications, $parameters);
126 $parametersCount = count($parameters);
127 foreach ($specifications as $specificationString => $paramSpecs) {
128 if ($parametersCount == count($paramSpecs)) {
131 unset($specificationString, $paramSpecs);
134 if (!isset($specificationString)) {
135 throw new Exception\
RuntimeException(
136 'A number of parameters was found that is not supported by this specification'
140 $topParameters = array();
141 foreach ($parameters as $position => $paramsForPosition) {
142 if (isset($paramSpecs[$position]['combinedby'])) {
143 $multiParamValues = array();
144 foreach ($paramsForPosition as $multiParamsForPosition) {
145 $ppCount = count($multiParamsForPosition);
146 if (!isset($paramSpecs[$position][$ppCount])) {
147 throw new Exception\
RuntimeException('A number of parameters (' . $ppCount . ') was found that is not supported by this specification');
149 $multiParamValues[] = vsprintf($paramSpecs[$position][$ppCount], $multiParamsForPosition);
151 $topParameters[] = implode($paramSpecs[$position]['combinedby'], $multiParamValues);
152 } elseif ($paramSpecs[$position] !== null) {
153 $ppCount = count($paramsForPosition);
154 if (!isset($paramSpecs[$position][$ppCount])) {
155 throw new Exception\
RuntimeException('A number of parameters (' . $ppCount . ') was found that is not supported by this specification');
157 $topParameters[] = vsprintf($paramSpecs[$position][$ppCount], $paramsForPosition);
159 $topParameters[] = $paramsForPosition;
162 return vsprintf($specificationString, $topParameters);
165 protected function processSubSelect(Select
$subselect, PlatformInterface
$platform, DriverInterface
$driver = null, ParameterContainer
$parameterContainer = null)
168 $stmtContainer = new StatementContainer
;
170 // Track subselect prefix and count for parameters
171 $this->processInfo
['subselectCount']++
;
172 $subselect->processInfo
['subselectCount'] = $this->processInfo
['subselectCount'];
173 $subselect->processInfo
['paramPrefix'] = 'subselect' . $subselect->processInfo
['subselectCount'];
176 $subselect->prepareStatement(new \Zend\Db\Adapter\
Adapter($driver, $platform), $stmtContainer);
179 $this->processInfo
['subselectCount'] = $subselect->processInfo
['subselectCount'];
181 $parameterContainer->merge($stmtContainer->getParameterContainer()->getNamedArray());
182 $sql = $stmtContainer->getSql();
184 $sql = $subselect->getSqlString($platform);