Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

Hotfix/db sql nested expressions #2687

Closed
wants to merge 2 commits into from

2 participants

@ralphschindler
Collaborator

Fix for #2521.

  • This now allows expressions within expressions:
$expression = new Predicate\Operator('release_date', '=', new Expression('FROM_UNIXTIME(?)', 100000000));
  • Previous internal demarcation of the type being TYPE_SELECT is deprecated in favor of TYPE_VALUE. (This is an undocumented and internal only API change).
  • Expression objects now accept scalar|array instead of string|array
ralphschindler added some commits
@ralphschindler ralphschindler Zend\Db\Sql:
* Fixed AbstractSql to handle nested expressions
* deprecated the internal feature of tagging value types as SELECT
7750d86
@ralphschindler ralphschindler Zend\Db\Sql\Expression & Zend\Db\Sql\Predicate\*
* Expressions now take scalar or array (as opposed to string or array)
* Test added for expression within expression handling
734e9b1
@weierophinney weierophinney was assigned
@weierophinney weierophinney referenced this pull request from a commit
@weierophinney weierophinney Merge branch 'hotfix/2687' into develop
Forward port #2687
17fe5ab
@ghost Unknown referenced this pull request from a commit
@weierophinney weierophinney Merge branch 'hotfix/2687'
Close #2687
1a6f199
@ghost Unknown referenced this pull request from a commit
@weierophinney weierophinney Merge branch 'hotfix/2687' into develop
Forward port #2687
8661b93
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Oct 3, 2012
  1. @ralphschindler

    Zend\Db\Sql:

    ralphschindler authored
    * Fixed AbstractSql to handle nested expressions
    * deprecated the internal feature of tagging value types as SELECT
Commits on Oct 5, 2012
  1. @ralphschindler

    Zend\Db\Sql\Expression & Zend\Db\Sql\Predicate\*

    ralphschindler authored
    * Expressions now take scalar or array (as opposed to string or array)
    * Test added for expression within expression handling
This page is out of date. Refresh to see the latest.
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());
}
Something went wrong with that request. Please try again.