1
0
mirror of synced 2024-12-14 23:26:04 +03:00

Merge pull request #309 from FabioBatSilva/DDC-1686

Fix DDC-1686
This commit is contained in:
Guilherme Blanco 2012-03-25 21:33:43 -07:00
commit 2811d161bb
14 changed files with 553 additions and 123 deletions

View File

@ -1,7 +1,5 @@
<?php <?php
/* /*
* $Id$
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
@ -27,18 +25,32 @@ namespace Doctrine\ORM\Query\Expr;
* @license http://www.opensource.org/licenses/lgpl-license.php LGPL * @license http://www.opensource.org/licenses/lgpl-license.php LGPL
* @link www.doctrine-project.org * @link www.doctrine-project.org
* @since 2.0 * @since 2.0
* @version $Revision$
* @author Guilherme Blanco <guilhermeblanco@hotmail.com> * @author Guilherme Blanco <guilhermeblanco@hotmail.com>
* @author Jonathan Wage <jonwage@gmail.com> * @author Jonathan Wage <jonwage@gmail.com>
* @author Roman Borschel <roman@code-factory.org> * @author Roman Borschel <roman@code-factory.org>
*/ */
class Andx extends Composite 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\Comparison',
'Doctrine\ORM\Query\Expr\Func', 'Doctrine\ORM\Query\Expr\Func',
'Doctrine\ORM\Query\Expr\Orx', 'Doctrine\ORM\Query\Expr\Orx',
'Doctrine\ORM\Query\Expr\Andx', 'Doctrine\ORM\Query\Expr\Andx',
); );
/**
* @return array
*/
public function getParts()
{
return $this->parts;
}
} }

View File

@ -1,7 +1,5 @@
<?php <?php
/* /*
* $Id$
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
@ -27,25 +25,49 @@ namespace Doctrine\ORM\Query\Expr;
* @license http://www.opensource.org/licenses/lgpl-license.php LGPL * @license http://www.opensource.org/licenses/lgpl-license.php LGPL
* @link www.doctrine-project.org * @link www.doctrine-project.org
* @since 2.0 * @since 2.0
* @version $Revision$
* @author Guilherme Blanco <guilhermeblanco@hotmail.com> * @author Guilherme Blanco <guilhermeblanco@hotmail.com>
* @author Jonathan Wage <jonwage@gmail.com> * @author Jonathan Wage <jonwage@gmail.com>
* @author Roman Borschel <roman@code-factory.org> * @author Roman Borschel <roman@code-factory.org>
*/ */
abstract class Base abstract class Base
{ {
protected $_preSeparator = '('; /**
protected $_separator = ', '; * @var string
protected $_postSeparator = ')'; */
protected $_allowedClasses = array(); protected $preSeparator = '(';
protected $_parts = array(); /**
* @var string
*/
protected $separator = ', ';
/**
* @var string
*/
protected $postSeparator = ')';
/**
* @var array
*/
protected $allowedClasses = array();
/**
* @var array
*/
protected $parts = array();
/**
* @param array $args
*/
public function __construct($args = array()) public function __construct($args = array())
{ {
$this->addMultiple($args); $this->addMultiple($args);
} }
/**
* @param array $args
* @return Base
*/
public function addMultiple($args = array()) public function addMultiple($args = array())
{ {
foreach ((array) $args as $arg) { foreach ((array) $args as $arg) {
@ -55,6 +77,10 @@ abstract class Base
return $this; return $this;
} }
/**
* @param mixed $arg
* @return Base
*/
public function add($arg) public function add($arg)
{ {
if ( $arg !== null || ($arg instanceof self && $arg->count() > 0) ) { if ( $arg !== null || ($arg instanceof self && $arg->count() > 0) ) {
@ -62,28 +88,34 @@ abstract class Base
if ( ! is_string($arg)) { if ( ! is_string($arg)) {
$class = get_class($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."); throw new \InvalidArgumentException("Expression of type '$class' not allowed in this context.");
} }
} }
$this->_parts[] = $arg; $this->parts[] = $arg;
} }
return $this; return $this;
} }
/**
* @return integer
*/
public function count() public function count()
{ {
return count($this->_parts); return count($this->parts);
} }
/**
* @return string
*/
public function __toString() public function __toString()
{ {
if ($this->count() == 1) { 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;
} }
} }

View File

@ -1,7 +1,5 @@
<?php <?php
/* /*
* $Id$
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
@ -27,7 +25,6 @@ namespace Doctrine\ORM\Query\Expr;
* @license http://www.opensource.org/licenses/lgpl-license.php LGPL * @license http://www.opensource.org/licenses/lgpl-license.php LGPL
* @link www.doctrine-project.org * @link www.doctrine-project.org
* @since 2.0 * @since 2.0
* @version $Revision$
* @author Guilherme Blanco <guilhermeblanco@hotmail.com> * @author Guilherme Blanco <guilhermeblanco@hotmail.com>
* @author Jonathan Wage <jonwage@gmail.com> * @author Jonathan Wage <jonwage@gmail.com>
* @author Roman Borschel <roman@code-factory.org> * @author Roman Borschel <roman@code-factory.org>
@ -41,19 +38,64 @@ class Comparison
const GT = '>'; const GT = '>';
const GTE = '>='; const GTE = '>=';
private $_leftExpr; /**
private $_operator; * @var mixed
private $_rightExpr; */
protected $leftExpr;
/**
* @var string
*/
protected $operator;
/**
* @var mixed
*/
protected $rightExpr;
/**
* Creates a comparison expression with the given arguments.
*
* @param mixed $leftExpr
* @param string $operator
* @param mixed $rightExpr
*/
public function __construct($leftExpr, $operator, $rightExpr) public function __construct($leftExpr, $operator, $rightExpr)
{ {
$this->_leftExpr = $leftExpr; $this->leftExpr = $leftExpr;
$this->_operator = $operator; $this->operator = $operator;
$this->_rightExpr = $rightExpr; $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() public function __toString()
{ {
return $this->_leftExpr . ' ' . $this->_operator . ' ' . $this->_rightExpr; return $this->leftExpr . ' ' . $this->operator . ' ' . $this->rightExpr;
} }
} }

View File

@ -1,7 +1,5 @@
<?php <?php
/* /*
* $Id$
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
@ -27,40 +25,46 @@ namespace Doctrine\ORM\Query\Expr;
* @license http://www.opensource.org/licenses/lgpl-license.php LGPL * @license http://www.opensource.org/licenses/lgpl-license.php LGPL
* @link www.doctrine-project.org * @link www.doctrine-project.org
* @since 2.0 * @since 2.0
* @version $Revision$
* @author Guilherme Blanco <guilhermeblanco@hotmail.com> * @author Guilherme Blanco <guilhermeblanco@hotmail.com>
* @author Jonathan Wage <jonwage@gmail.com> * @author Jonathan Wage <jonwage@gmail.com>
* @author Roman Borschel <roman@code-factory.org> * @author Roman Borschel <roman@code-factory.org>
*/ */
class Composite extends Base class Composite extends Base
{ {
/**
* @return string
*/
public function __toString() public function __toString()
{ {
if ($this->count() === 1) { if ($this->count() === 1) {
return (string) $this->_parts[0]; return (string) $this->parts[0];
} }
$components = array(); $components = array();
foreach ($this->_parts as $part) { foreach ($this->parts as $part) {
$components[] = $this->processQueryPart($part); $components[] = $this->processQueryPart($part);
} }
return implode($this->_separator, $components); return implode($this->separator, $components);
} }
/**
* @param string $part
* @return string
*/
private function processQueryPart($part) private function processQueryPart($part)
{ {
$queryPart = (string) $part; $queryPart = (string) $part;
if (is_object($part) && $part instanceof self && $part->count() > 1) { 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") // 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) { if (stripos($queryPart, ' OR ') !== false || stripos($queryPart, ' AND ') !== false) {
return $this->_preSeparator . $queryPart . $this->_postSeparator; return $this->preSeparator . $queryPart . $this->postSeparator;
} }
return $queryPart; return $queryPart;

View File

@ -1,7 +1,5 @@
<?php <?php
/* /*
* $Id$
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
@ -27,7 +25,6 @@ namespace Doctrine\ORM\Query\Expr;
* @license http://www.opensource.org/licenses/lgpl-license.php LGPL * @license http://www.opensource.org/licenses/lgpl-license.php LGPL
* @link www.doctrine-project.org * @link www.doctrine-project.org
* @since 2.0 * @since 2.0
* @version $Revision$
* @author Guilherme Blanco <guilhermeblanco@hotmail.com> * @author Guilherme Blanco <guilhermeblanco@hotmail.com>
* @author Jonathan Wage <jonwage@gmail.com> * @author Jonathan Wage <jonwage@gmail.com>
* @author Roman Borschel <roman@code-factory.org> * @author Roman Borschel <roman@code-factory.org>
@ -37,17 +34,17 @@ class From
/** /**
* @var string * @var string
*/ */
private $_from; protected $from;
/** /**
* @var string * @var string
*/ */
private $_alias; protected $alias;
/** /**
* @var string * @var string
*/ */
private $_indexBy; protected $indexBy;
/** /**
* @param string $from The class name. * @param string $from The class name.
@ -56,9 +53,9 @@ class From
*/ */
public function __construct($from, $alias, $indexBy = null) public function __construct($from, $alias, $indexBy = null)
{ {
$this->_from = $from; $this->from = $from;
$this->_alias = $alias; $this->alias = $alias;
$this->_indexBy = $indexBy; $this->indexBy = $indexBy;
} }
/** /**
@ -66,7 +63,7 @@ class From
*/ */
public function getFrom() public function getFrom()
{ {
return $this->_from; return $this->from;
} }
/** /**
@ -74,7 +71,15 @@ class From
*/ */
public function getAlias() public function getAlias()
{ {
return $this->_alias; return $this->alias;
}
/**
* @return string
*/
public function getIndexBy()
{
return $this->indexBy;
} }
/** /**
@ -82,7 +87,7 @@ class From
*/ */
public function __toString() public function __toString()
{ {
return $this->_from . ' ' . $this->_alias . return $this->from . ' ' . $this->alias .
($this->_indexBy ? ' INDEX BY ' . $this->_indexBy : ''); ($this->indexBy ? ' INDEX BY ' . $this->indexBy : '');
} }
} }

View File

@ -1,7 +1,5 @@
<?php <?php
/* /*
* $Id$
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
@ -27,24 +25,55 @@ namespace Doctrine\ORM\Query\Expr;
* @license http://www.opensource.org/licenses/lgpl-license.php LGPL * @license http://www.opensource.org/licenses/lgpl-license.php LGPL
* @link www.doctrine-project.org * @link www.doctrine-project.org
* @since 2.0 * @since 2.0
* @version $Revision$
* @author Guilherme Blanco <guilhermeblanco@hotmail.com> * @author Guilherme Blanco <guilhermeblanco@hotmail.com>
* @author Jonathan Wage <jonwage@gmail.com> * @author Jonathan Wage <jonwage@gmail.com>
* @author Roman Borschel <roman@code-factory.org> * @author Roman Borschel <roman@code-factory.org>
*/ */
class Func class Func
{ {
private $_name; /**
private $_arguments; * @var string
*/
protected $name;
/**
* @var array
*/
protected $arguments;
/**
* Creates a function, with the given argument.
*
* @param string $name
* @param array $arguments
*/
public function __construct($name, $arguments) public function __construct($name, $arguments)
{ {
$this->_name = $name; $this->name = $name;
$this->_arguments = (array) $arguments; $this->arguments = (array) $arguments;
} }
/**
* @return string
*/
public function getName()
{
return $this->name;
}
/**
* @return array
*/
public function getArguments()
{
return $this->arguments;
}
/**
* @return string
*/
public function __toString() public function __toString()
{ {
return $this->_name . '(' . implode(', ', $this->_arguments) . ')'; return $this->name . '(' . implode(', ', $this->arguments) . ')';
} }
} }

View File

@ -1,7 +1,5 @@
<?php <?php
/* /*
* $Id$
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
@ -27,13 +25,27 @@ namespace Doctrine\ORM\Query\Expr;
* @license http://www.opensource.org/licenses/lgpl-license.php LGPL * @license http://www.opensource.org/licenses/lgpl-license.php LGPL
* @link www.doctrine-project.org * @link www.doctrine-project.org
* @since 2.0 * @since 2.0
* @version $Revision$
* @author Guilherme Blanco <guilhermeblanco@hotmail.com> * @author Guilherme Blanco <guilhermeblanco@hotmail.com>
* @author Jonathan Wage <jonwage@gmail.com> * @author Jonathan Wage <jonwage@gmail.com>
* @author Roman Borschel <roman@code-factory.org> * @author Roman Borschel <roman@code-factory.org>
*/ */
class GroupBy extends Base class GroupBy extends Base
{ {
protected $_preSeparator = ''; /**
protected $_postSeparator = ''; * @var string
*/
protected $preSeparator = '';
/**
* @var string
*/
protected $postSeparator = '';
/**
* @return array
*/
public function getParts()
{
return $this->parts;
}
} }

View File

@ -1,7 +1,5 @@
<?php <?php
/* /*
* $Id$
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
@ -27,7 +25,6 @@ namespace Doctrine\ORM\Query\Expr;
* @license http://www.opensource.org/licenses/lgpl-license.php LGPL * @license http://www.opensource.org/licenses/lgpl-license.php LGPL
* @link www.doctrine-project.org * @link www.doctrine-project.org
* @since 2.0 * @since 2.0
* @version $Revision$
* @author Guilherme Blanco <guilhermeblanco@hotmail.com> * @author Guilherme Blanco <guilhermeblanco@hotmail.com>
* @author Jonathan Wage <jonwage@gmail.com> * @author Jonathan Wage <jonwage@gmail.com>
* @author Roman Borschel <roman@code-factory.org> * @author Roman Borschel <roman@code-factory.org>
@ -40,28 +37,111 @@ class Join
const ON = 'ON'; const ON = 'ON';
const WITH = 'WITH'; const WITH = 'WITH';
private $_joinType; /**
private $_join; * @var string
private $_alias; */
private $_conditionType; protected $joinType;
private $_condition;
private $_indexBy;
/**
* @var string
*/
protected $join;
/**
* @var string
*/
protected $alias;
/**
* @var string
*/
protected $conditionType;
/**
* @var string
*/
protected $condition;
/**
* @var string
*/
protected $indexBy;
/**
* @param string $joinType The condition type constant. Either INNER_JOIN or LEFT_JOIN.
* @param string $join The relationship to join
* @param string $alias The alias of the join
* @param string $conditionType The condition type constant. Either ON or WITH.
* @param string $condition The condition for the join
* @param string $indexBy The index for the join
*/
public function __construct($joinType, $join, $alias = null, $conditionType = null, $condition = null, $indexBy = null) public function __construct($joinType, $join, $alias = null, $conditionType = null, $condition = null, $indexBy = null)
{ {
$this->_joinType = $joinType; $this->joinType = $joinType;
$this->_join = $join; $this->join = $join;
$this->_alias = $alias; $this->alias = $alias;
$this->_conditionType = $conditionType; $this->conditionType = $conditionType;
$this->_condition = $condition; $this->condition = $condition;
$this->_indexBy = $indexBy; $this->indexBy = $indexBy;
} }
/**
* @return string
*/
public function getJoinType()
{
return $this->joinType;
}
/**
* @return string
*/
public function getJoin()
{
return $this->join;
}
/**
* @return string
*/
public function getAlias()
{
return $this->alias;
}
/**
* @return string
*/
public function getConditionType()
{
return $this->conditionType;
}
/**
* @return string
*/
public function getCondition()
{
return $this->condition;
}
/**
* @return string
*/
public function getIndexBy()
{
return $this->indexBy;
}
/**
* @return string
*/
public function __toString() public function __toString()
{ {
return strtoupper($this->_joinType) . ' JOIN ' . $this->_join return strtoupper($this->joinType) . ' JOIN ' . $this->join
. ($this->_alias ? ' ' . $this->_alias : '') . ($this->alias ? ' ' . $this->alias : '')
. ($this->_condition ? ' ' . strtoupper($this->_conditionType) . ' ' . $this->_condition : '') . ($this->condition ? ' ' . strtoupper($this->conditionType) . ' ' . $this->condition : '')
. ($this->_indexBy ? ' INDEX BY ' . $this->_indexBy : ''); . ($this->indexBy ? ' INDEX BY ' . $this->indexBy : '');
} }
} }

View File

@ -1,9 +1,52 @@
<?php <?php
/*
* 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
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* This software consists of voluntary contributions made by many individuals
* and is licensed under the LGPL. For more information, see
* <http://www.doctrine-project.org>.
*/
namespace Doctrine\ORM\Query\Expr; namespace Doctrine\ORM\Query\Expr;
/**
* Expression class for generating DQL functions
*
* @license http://www.opensource.org/licenses/lgpl-license.php LGPL
* @link www.doctrine-project.org
* @since 2.0
* @author Guilherme Blanco <guilhermeblanco@hotmail.com>
* @author Jonathan Wage <jonwage@gmail.com>
* @author Roman Borschel <roman@code-factory.org>
*/
class Literal extends Base class Literal extends Base
{ {
protected $_preSeparator = ''; /**
protected $_postSeparator = ''; * @var string
*/
protected $preSeparator = '';
/**
* @var string
*/
protected $postSeparator = '';
/**
* @return array
*/
public function getParts()
{
return $this->parts;
}
} }

View File

@ -1,7 +1,5 @@
<?php <?php
/* /*
* $Id$
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
@ -27,40 +25,84 @@ namespace Doctrine\ORM\Query\Expr;
* @license http://www.opensource.org/licenses/lgpl-license.php LGPL * @license http://www.opensource.org/licenses/lgpl-license.php LGPL
* @link www.doctrine-project.org * @link www.doctrine-project.org
* @since 2.0 * @since 2.0
* @version $Revision$
* @author Guilherme Blanco <guilhermeblanco@hotmail.com> * @author Guilherme Blanco <guilhermeblanco@hotmail.com>
* @author Jonathan Wage <jonwage@gmail.com> * @author Jonathan Wage <jonwage@gmail.com>
* @author Roman Borschel <roman@code-factory.org> * @author Roman Borschel <roman@code-factory.org>
*/ */
class Math class Math
{ {
private $_leftExpr; /**
private $_operator; * @var mixed
private $_rightExpr; */
protected $leftExpr;
/**
* @var string
*/
protected $operator;
/**
* @var mixed
*/
protected $rightExpr;
/**
* Creates a mathematical expression with the given arguments.
*
* @param mixed $leftExpr
* @param string $operator
* @param mixed $rightExpr
*/
public function __construct($leftExpr, $operator, $rightExpr) public function __construct($leftExpr, $operator, $rightExpr)
{ {
$this->_leftExpr = $leftExpr; $this->leftExpr = $leftExpr;
$this->_operator = $operator; $this->operator = $operator;
$this->_rightExpr = $rightExpr; $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() public function __toString()
{ {
// Adjusting Left Expression // Adjusting Left Expression
$leftExpr = (string) $this->_leftExpr; $leftExpr = (string) $this->leftExpr;
if ($this->_leftExpr instanceof Math) { if ($this->leftExpr instanceof Math) {
$leftExpr = '(' . $leftExpr . ')'; $leftExpr = '(' . $leftExpr . ')';
} }
// Adjusting Right Expression // Adjusting Right Expression
$rightExpr = (string) $this->_rightExpr; $rightExpr = (string) $this->rightExpr;
if ($this->_rightExpr instanceof Math) { if ($this->rightExpr instanceof Math) {
$rightExpr = '(' . $rightExpr . ')'; $rightExpr = '(' . $rightExpr . ')';
} }
return $leftExpr . ' ' . $this->_operator . ' ' . $rightExpr; return $leftExpr . ' ' . $this->operator . ' ' . $rightExpr;
} }
} }

View File

@ -1,7 +1,5 @@
<?php <?php
/* /*
* $Id$
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
@ -27,20 +25,41 @@ namespace Doctrine\ORM\Query\Expr;
* @license http://www.opensource.org/licenses/lgpl-license.php LGPL * @license http://www.opensource.org/licenses/lgpl-license.php LGPL
* @link www.doctrine-project.org * @link www.doctrine-project.org
* @since 2.0 * @since 2.0
* @version $Revision$
* @author Guilherme Blanco <guilhermeblanco@hotmail.com> * @author Guilherme Blanco <guilhermeblanco@hotmail.com>
* @author Jonathan Wage <jonwage@gmail.com> * @author Jonathan Wage <jonwage@gmail.com>
* @author Roman Borschel <roman@code-factory.org> * @author Roman Borschel <roman@code-factory.org>
*/ */
class OrderBy class OrderBy
{ {
protected $_preSeparator = ''; /**
protected $_separator = ', '; * @var string
protected $_postSeparator = ''; */
protected $_allowedClasses = array(); protected $preSeparator = '';
private $_parts = array(); /**
* @var string
*/
protected $separator = ', ';
/**
* @var string
*/
protected $postSeparator = '';
/**
* @var array
*/
protected $allowedClasses = array();
/**
* @var array
*/
protected $parts = array();
/**
* @param string $sort
* @param string $order
*/
public function __construct($sort = null, $order = null) public function __construct($sort = null, $order = null)
{ {
if ($sort) { if ($sort) {
@ -48,19 +67,37 @@ class OrderBy
} }
} }
/**
* @param string $sort
* @param string $order
*/
public function add($sort, $order = null) public function add($sort, $order = null)
{ {
$order = ! $order ? 'ASC' : $order; $order = ! $order ? 'ASC' : $order;
$this->_parts[] = $sort . ' '. $order; $this->parts[] = $sort . ' '. $order;
} }
/**
* @return integer
*/
public function count() public function count()
{ {
return count($this->_parts); return count($this->parts);
} }
/**
* @return array
*/
public function getParts()
{
return $this->parts;
}
/**
* @return string
*/
public function __tostring() public function __tostring()
{ {
return $this->_preSeparator . implode($this->_separator, $this->_parts) . $this->_postSeparator; return $this->preSeparator . implode($this->separator, $this->parts) . $this->postSeparator;
} }
} }

View File

@ -1,7 +1,5 @@
<?php <?php
/* /*
* $Id$
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
@ -27,18 +25,32 @@ namespace Doctrine\ORM\Query\Expr;
* @license http://www.opensource.org/licenses/lgpl-license.php LGPL * @license http://www.opensource.org/licenses/lgpl-license.php LGPL
* @link www.doctrine-project.org * @link www.doctrine-project.org
* @since 2.0 * @since 2.0
* @version $Revision$
* @author Guilherme Blanco <guilhermeblanco@hotmail.com> * @author Guilherme Blanco <guilhermeblanco@hotmail.com>
* @author Jonathan Wage <jonwage@gmail.com> * @author Jonathan Wage <jonwage@gmail.com>
* @author Roman Borschel <roman@code-factory.org> * @author Roman Borschel <roman@code-factory.org>
*/ */
class Orx extends Composite class Orx extends Composite
{ {
protected $_separator = ' OR '; /**
protected $_allowedClasses = array( * @var string
*/
protected $separator = ' OR ';
/**
* @var array
*/
protected $allowedClasses = array(
'Doctrine\ORM\Query\Expr\Comparison', 'Doctrine\ORM\Query\Expr\Comparison',
'Doctrine\ORM\Query\Expr\Func', 'Doctrine\ORM\Query\Expr\Func',
'Doctrine\ORM\Query\Expr\Andx', 'Doctrine\ORM\Query\Expr\Andx',
'Doctrine\ORM\Query\Expr\Orx', 'Doctrine\ORM\Query\Expr\Orx',
); );
/**
* @return array
*/
public function getParts()
{
return $this->parts;
}
} }

View File

@ -1,7 +1,5 @@
<?php <?php
/* /*
* $Id$
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
@ -27,16 +25,34 @@ namespace Doctrine\ORM\Query\Expr;
* @license http://www.opensource.org/licenses/lgpl-license.php LGPL * @license http://www.opensource.org/licenses/lgpl-license.php LGPL
* @link www.doctrine-project.org * @link www.doctrine-project.org
* @since 2.0 * @since 2.0
* @version $Revision$
* @author Guilherme Blanco <guilhermeblanco@hotmail.com> * @author Guilherme Blanco <guilhermeblanco@hotmail.com>
* @author Jonathan Wage <jonwage@gmail.com> * @author Jonathan Wage <jonwage@gmail.com>
* @author Roman Borschel <roman@code-factory.org> * @author Roman Borschel <roman@code-factory.org>
*/ */
class Select extends Base class Select extends Base
{ {
protected $_preSeparator = ''; /**
protected $_postSeparator = ''; * @var string
protected $_allowedClasses = array( */
protected $preSeparator = '';
/**
* @var string
*/
protected $postSeparator = '';
/**
* @var array
*/
protected $allowedClasses = array(
'Doctrine\ORM\Query\Expr\Func' 'Doctrine\ORM\Query\Expr\Func'
); );
/**
* @return array
*/
public function getParts()
{
return $this->parts;
}
} }

View File

@ -345,4 +345,68 @@ class ExprTest extends \Doctrine\Tests\OrmTestCase
$this->assertEquals('true', $this->_expr->literal(true)); $this->assertEquals('true', $this->_expr->literal(true));
$this->assertEquals('false', $this->_expr->literal(false)); $this->assertEquals('false', $this->_expr->literal(false));
} }
/**
* @group DDC-1686
*/
public function testExpressionGetter()
{
// Andx
$andx = new Expr\Andx(array('1 = 1', '2 = 2'));
$this->assertEquals(array('1 = 1', '2 = 2'), $andx->getParts());
// Comparison
$comparison = new Expr\Comparison('foo', Expr\Comparison::EQ, 'bar');
$this->assertEquals('foo', $comparison->getLeftExpr());
$this->assertEquals('bar', $comparison->getRightExpr());
$this->assertEquals(Expr\Comparison::EQ, $comparison->getOperator());
// From
$from = new Expr\From('Foo', 'f', 'f.id');
$this->assertEquals('f', $from->getAlias());
$this->assertEquals('Foo', $from->getFrom());
$this->assertEquals('f.id', $from->getIndexBy());
// Func
$func = new Expr\Func('MAX', array('f.id'));
$this->assertEquals('MAX', $func->getName());
$this->assertEquals(array('f.id'), $func->getArguments());
// GroupBy
$group = new Expr\GroupBy(array('foo DESC', 'bar ASC'));
$this->assertEquals(array('foo DESC', 'bar ASC'), $group->getParts());
// Join
$join = new Expr\Join(Expr\Join::INNER_JOIN, 'f.bar', 'b', Expr\Join::ON, 'b.bar_id = 1', 'b.bar_id');
$this->assertEquals(Expr\Join::INNER_JOIN, $join->getJoinType());
$this->assertEquals(Expr\Join::ON, $join->getConditionType());
$this->assertEquals('b.bar_id = 1', $join->getCondition());
$this->assertEquals('b.bar_id', $join->getIndexBy());
$this->assertEquals('f.bar', $join->getJoin());
$this->assertEquals('b', $join->getAlias());
// Literal
$literal = new Expr\Literal(array('foo'));
$this->assertEquals(array('foo'), $literal->getParts());
// Math
$math = new Expr\Math(10, '+', 20);
$this->assertEquals(10, $math->getLeftExpr());
$this->assertEquals(20, $math->getRightExpr());
$this->assertEquals('+', $math->getOperator());
// OrderBy
$order = new Expr\OrderBy('foo', 'DESC');
$this->assertEquals(array('foo DESC'), $order->getParts());
// Andx
$orx = new Expr\Orx(array('foo = 1', 'bar = 2'));
$this->assertEquals(array('foo = 1', 'bar = 2'), $orx->getParts());
// Select
$select = new Expr\Select(array('foo', 'bar'));
$this->assertEquals(array('foo', 'bar'), $select->getParts());
}
} }