Skip to content
Browse files

Merge branch 'hotfix/db-sql-nested-expressions' of https://github.com…

…/ralphschindler/zf2 into hotfix/2687
  • Loading branch information...
2 parents 7cdfa42 + 734e9b1 commit 4b5d39c6904bb83413cef5a94fbff20a30f3dbd2 @weierophinney weierophinney committed
View
21 library/Zend/Db/Sql/AbstractSql.php
@@ -62,6 +62,20 @@ protected function processExpression(ExpressionInterface $expression, PlatformIn
foreach ($values as $vIndex => $value) {
if (isset($types[$vIndex]) && $types[$vIndex] == ExpressionInterface::TYPE_IDENTIFIER) {
$values[$vIndex] = $platform->quoteIdentifierInFragment($value);
+ } elseif (isset($types[$vIndex]) && $types[$vIndex] == ExpressionInterface::TYPE_VALUE && $value instanceof Select) {
+ // process sub-select
+ if ($adapter) {
+ $values[$vIndex] = '(' . $this->processSubSelect($value, $platform, $adapter, $parameterContainer) . ')';
+ } else {
+ $values[$vIndex] = '(' . $this->processSubSelect($value, $platform) . ')';
+ }
+ } elseif (isset($types[$vIndex]) && $types[$vIndex] == ExpressionInterface::TYPE_VALUE && $value instanceof ExpressionInterface) {
+ // recursive call to satisfy nested expressions
+ $innerStatementContainer = $this->processExpression($value, $platform, $adapter, $namedParameterPrefix . $vIndex . 'subpart');
+ $values[$vIndex] = $innerStatementContainer->getSql();
+ if ($adapter) {
+ $parameterContainer->merge($innerStatementContainer->getParameterContainer());
+ }
} elseif (isset($types[$vIndex]) && $types[$vIndex] == ExpressionInterface::TYPE_VALUE) {
// if prepareType is set, it means that this particular value must be
@@ -77,13 +91,6 @@ protected function processExpression(ExpressionInterface $expression, PlatformIn
$values[$vIndex] = $platform->quoteValue($value);
} elseif (isset($types[$vIndex]) && $types[$vIndex] == ExpressionInterface::TYPE_LITERAL) {
$values[$vIndex] = $value;
- } elseif (isset($types[$vIndex]) && $types[$vIndex] == ExpressionInterface::TYPE_SELECT) {
- // process sub-select
- if ($adapter) {
- $values[$vIndex] = '(' . $this->processSubSelect($value, $platform, $adapter, $statementContainer->getParameterContainer()) . ')';
- } else {
- $values[$vIndex] = '(' . $this->processSubSelect($value, $platform) . ')';
- }
}
}
View
6 library/Zend/Db/Sql/Expression.php
@@ -79,8 +79,8 @@ public function getExpression()
*/
public function setParameters($parameters)
{
- if (!is_string($parameters) && !is_array($parameters)) {
- throw new Exception\InvalidArgumentException('Expression parameters must be a string or array.');
+ if (!is_scalar($parameters) && !is_array($parameters)) {
+ throw new Exception\InvalidArgumentException('Expression parameters must be a scalar or array.');
}
$this->parameters = $parameters;
return $this;
@@ -118,7 +118,7 @@ public function getTypes()
*/
public function getExpressionData()
{
- $parameters = (is_string($this->parameters)) ? array($this->parameters) : $this->parameters;
+ $parameters = (is_scalar($this->parameters)) ? array($this->parameters) : $this->parameters;
$types = array();
for ($i = 0; $i < count($parameters); $i++) {
View
4 library/Zend/Db/Sql/ExpressionInterface.php
@@ -15,6 +15,10 @@
const TYPE_IDENTIFIER = 'identifier';
const TYPE_VALUE = 'value';
const TYPE_LITERAL = 'literal';
+
+ /**
+ * @deprecated This will go away in 2.1
+ */
const TYPE_SELECT = 'select';
/**
View
2 library/Zend/Db/Sql/Predicate/In.php
@@ -94,7 +94,7 @@ public function getExpressionData()
$values = $this->getValueSet();
if ($values instanceof Select) {
$specification = '%s IN %s';
- $types = array(self::TYPE_SELECT);
+ $types = array(self::TYPE_VALUE);
$values = array($values);
} else {
$specification = '%s IN (' . implode(', ', array_fill(0, count($values), '%s')) . ')';
View
11 tests/ZendTest/Db/Sql/AbstractSqlTest.php
@@ -115,6 +115,17 @@ public function testProcessExpressionWorksWithExpressionContainingSelectObject()
$this->assertEquals(0, $sqlAndParams->getParameterContainer()->count());
}
+ public function testProcessExpressionWorksWithExpressionContainingExpressionObject()
+ {
+ $expression = new Predicate\Operator(
+ 'release_date',
+ '=',
+ new Expression('FROM_UNIXTIME(?)', 100000000)
+ );
+
+ $sqlAndParams = $this->invokeProcessExpressionMethod($expression);
+ $this->assertEquals('"release_date" = FROM_UNIXTIME(\'100000000\')', $sqlAndParams->getSql());
+ }
/**
* @param \Zend\Db\Sql\ExpressionInterface $expression
View
2 tests/ZendTest/Db/Sql/ExpressionTest.php
@@ -70,7 +70,7 @@ public function testSetParametersException()
{
$expression = new Expression('','foo');
- $this->setExpectedException('Zend\Db\Sql\Exception\InvalidArgumentException', 'Expression parameters must be a string or array.');
+ $this->setExpectedException('Zend\Db\Sql\Exception\InvalidArgumentException', 'Expression parameters must be a scalar or array.');
$return = $expression->setParameters(null);
}
View
2 tests/ZendTest/Db/Sql/Predicate/InTest.php
@@ -65,7 +65,7 @@ public function testGetExpressionDataWithSubselect()
$expected = array(array(
'%s IN %s',
array('foo', $select),
- array($in::TYPE_IDENTIFIER, $in::TYPE_SELECT)
+ array($in::TYPE_IDENTIFIER, $in::TYPE_VALUE)
));
$this->assertEquals($expected, $in->getExpressionData());
}

0 comments on commit 4b5d39c

Please sign in to comment.
Something went wrong with that request. Please try again.