Skip to content

Commit

Permalink
Fix DDC-1686
Browse files Browse the repository at this point in the history
  • Loading branch information
FabioBatSilva committed Mar 25, 2012
1 parent ab15528 commit 0f9afbd
Show file tree
Hide file tree
Showing 14 changed files with 547 additions and 123 deletions.
22 changes: 17 additions & 5 deletions lib/Doctrine/ORM/Query/Expr/Andx.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
<?php
/*
* $Id$
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
Expand All @@ -27,18 +25,32 @@
* @license https://www.opensource.org/licenses/lgpl-license.php LGPL
* @link www.doctrine-project.org
* @since 2.0
* @version $Revision$
* @author Guilherme Blanco <[email protected]>
* @author Jonathan Wage <[email protected]>
* @author Roman Borschel <[email protected]>
*/
class Andx extends Composite
{
protected $_separator = ' AND ';
protected $_allowedClasses = array(
/**
* @var string
*/
protected $separator = ' AND ';

/**
* @var array
*/
protected $allowedClasses = array(
'Doctrine\ORM\Query\Expr\Comparison',
'Doctrine\ORM\Query\Expr\Func',
'Doctrine\ORM\Query\Expr\Orx',
'Doctrine\ORM\Query\Expr\Andx',
);

/**
* @return array
*/
public function getParts()
{
return $this->parts;
}
}
58 changes: 45 additions & 13 deletions lib/Doctrine/ORM/Query/Expr/Base.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
<?php
/*
* $Id$
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
Expand All @@ -27,25 +25,49 @@
* @license https://www.opensource.org/licenses/lgpl-license.php LGPL
* @link www.doctrine-project.org
* @since 2.0
* @version $Revision$
* @author Guilherme Blanco <[email protected]>
* @author Jonathan Wage <[email protected]>
* @author Roman Borschel <[email protected]>
*/
abstract class Base
{
protected $_preSeparator = '(';
protected $_separator = ', ';
protected $_postSeparator = ')';
protected $_allowedClasses = array();
/**
* @var string
*/
protected $preSeparator = '(';

/**
* @var string
*/
protected $separator = ', ';

/**
* @var string
*/
protected $postSeparator = ')';

/**
* @var array
*/
protected $allowedClasses = array();

protected $_parts = array();
/**
* @var array
*/
protected $parts = array();

/**
* @param array $args
*/
public function __construct($args = array())
{
$this->addMultiple($args);
}

/**
* @param array $args
* @return Base
*/
public function addMultiple($args = array())
{
foreach ((array) $args as $arg) {
Expand All @@ -55,35 +77,45 @@ public function addMultiple($args = array())
return $this;
}

/**
* @param mixed $arg
* @return Base
*/
public function add($arg)
{
if ( $arg !== null || ($arg instanceof self && $arg->count() > 0) ) {
// If we decide to keep Expr\Base instances, we can use this check
if ( ! is_string($arg)) {
$class = get_class($arg);

if ( ! in_array($class, $this->_allowedClasses)) {
if ( ! in_array($class, $this->allowedClasses)) {
throw new \InvalidArgumentException("Expression of type '$class' not allowed in this context.");
}
}

$this->_parts[] = $arg;
$this->parts[] = $arg;
}

return $this;
}

/**
* @return integer
*/
public function count()
{
return count($this->_parts);
return count($this->parts);
}

/**
* @return string
*/
public function __toString()
{
if ($this->count() == 1) {
return (string) $this->_parts[0];
return (string) $this->parts[0];
}

return $this->_preSeparator . implode($this->_separator, $this->_parts) . $this->_postSeparator;
return $this->preSeparator . implode($this->separator, $this->parts) . $this->postSeparator;
}
}
62 changes: 52 additions & 10 deletions lib/Doctrine/ORM/Query/Expr/Comparison.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
<?php
/*
* $Id$
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
Expand All @@ -27,7 +25,6 @@
* @license https://www.opensource.org/licenses/lgpl-license.php LGPL
* @link www.doctrine-project.org
* @since 2.0
* @version $Revision$
* @author Guilherme Blanco <[email protected]>
* @author Jonathan Wage <[email protected]>
* @author Roman Borschel <[email protected]>
Expand All @@ -41,19 +38,64 @@ class Comparison
const GT = '>';
const GTE = '>=';

private $_leftExpr;
private $_operator;
private $_rightExpr;
/**
* @var mixed
*/
private $leftExpr;

/**
* @var string
*/
private $operator;

/**
* @var mixed
*/
private $rightExpr;

/**
* Creates a comparison expression with the given arguments.
*
* @param mixed $leftExpr
* @param string $operator
* @param mixed $rightExpr
*/
public function __construct($leftExpr, $operator, $rightExpr)
{
$this->_leftExpr = $leftExpr;
$this->_operator = $operator;
$this->_rightExpr = $rightExpr;
$this->leftExpr = $leftExpr;
$this->operator = $operator;
$this->rightExpr = $rightExpr;
}

/**
* @return mixed
*/
public function getLeftExpr()
{
return $this->leftExpr;
}

/**
* @return string
*/
public function getOperator()
{
return $this->operator;
}

/**
* @return mixed
*/
public function getRightExpr()
{
return $this->rightExpr;
}

/**
* @return string
*/
public function __toString()
{
return $this->_leftExpr . ' ' . $this->_operator . ' ' . $this->_rightExpr;
return $this->leftExpr . ' ' . $this->operator . ' ' . $this->rightExpr;
}
}
20 changes: 12 additions & 8 deletions lib/Doctrine/ORM/Query/Expr/Composite.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
<?php
/*
* $Id$
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
Expand All @@ -27,40 +25,46 @@
* @license https://www.opensource.org/licenses/lgpl-license.php LGPL
* @link www.doctrine-project.org
* @since 2.0
* @version $Revision$
* @author Guilherme Blanco <[email protected]>
* @author Jonathan Wage <[email protected]>
* @author Roman Borschel <[email protected]>
*/
class Composite extends Base
{
/**
* @return string
*/
public function __toString()
{
if ($this->count() === 1) {
return (string) $this->_parts[0];
return (string) $this->parts[0];
}

$components = array();

foreach ($this->_parts as $part) {
foreach ($this->parts as $part) {
$components[] = $this->processQueryPart($part);
}

return implode($this->_separator, $components);
return implode($this->separator, $components);
}


/**
* @param string $part
* @return string
*/
private function processQueryPart($part)
{
$queryPart = (string) $part;

if (is_object($part) && $part instanceof self && $part->count() > 1) {
return $this->_preSeparator . $queryPart . $this->_postSeparator;
return $this->preSeparator . $queryPart . $this->postSeparator;
}

// Fixes DDC-1237: User may have added a where item containing nested expression (with "OR" or "AND")
if (stripos($queryPart, ' OR ') !== false || stripos($queryPart, ' AND ') !== false) {
return $this->_preSeparator . $queryPart . $this->_postSeparator;
return $this->preSeparator . $queryPart . $this->postSeparator;
}

return $queryPart;
Expand Down
31 changes: 18 additions & 13 deletions lib/Doctrine/ORM/Query/Expr/From.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
<?php
/*
* $Id$
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
Expand All @@ -27,7 +25,6 @@
* @license https://www.opensource.org/licenses/lgpl-license.php LGPL
* @link www.doctrine-project.org
* @since 2.0
* @version $Revision$
* @author Guilherme Blanco <[email protected]>
* @author Jonathan Wage <[email protected]>
* @author Roman Borschel <[email protected]>
Expand All @@ -37,17 +34,17 @@ class From
/**
* @var string
*/
private $_from;
private $from;

/**
* @var string
*/
private $_alias;
private $alias;

/**
* @var string
*/
private $_indexBy;
private $indexBy;

/**
* @param string $from The class name.
Expand All @@ -56,33 +53,41 @@ class From
*/
public function __construct($from, $alias, $indexBy = null)
{
$this->_from = $from;
$this->_alias = $alias;
$this->_indexBy = $indexBy;
$this->from = $from;
$this->alias = $alias;
$this->indexBy = $indexBy;
}

/**
* @return string
*/
public function getFrom()
{
return $this->_from;
return $this->from;
}

/**
* @return string
*/
public function getAlias()
{
return $this->_alias;
return $this->alias;
}

/**
* @return string
*/
public function getIndexBy()
{
return $this->indexBy;
}

/**
* @return string
*/
public function __toString()
{
return $this->_from . ' ' . $this->_alias .
($this->_indexBy ? ' INDEX BY ' . $this->_indexBy : '');
return $this->from . ' ' . $this->alias .
($this->indexBy ? ' INDEX BY ' . $this->indexBy : '');
}
}
Loading

0 comments on commit 0f9afbd

Please sign in to comment.