1
0
mirror of synced 2025-01-31 04:21:44 +03:00

[2.0] Refactor Exceptions from Query and AST\InputParameter into QueryException class.

This commit is contained in:
beberlei 2009-11-21 17:04:17 +00:00
parent 26a2ec2e63
commit a9d739a743
4 changed files with 20 additions and 9 deletions

View File

@ -209,12 +209,12 @@ final class Query extends AbstractQuery
$paramMappings = $this->_parserResult->getParameterMappings(); $paramMappings = $this->_parserResult->getParameterMappings();
if(count($paramMappings) != count($params)) { if(count($paramMappings) != count($params)) {
throw new QueryException("Invalid parameter number: number of bound variables does not match number of tokens"); throw QueryException::invalidParameterNumber();
} }
foreach ($params as $key => $value) { foreach ($params as $key => $value) {
if(!isset($paramMappings[$key])) { if(!isset($paramMappings[$key])) {
throw new QueryException("Invalid parameter: token ".$key." is not defined in the query."); throw QueryException::unknownParameter($key);
} }
if (is_object($value)) { if (is_object($value)) {

View File

@ -43,10 +43,7 @@ class InputParameter extends Node
public function __construct($value) public function __construct($value)
{ {
if (strlen($value) == 1) { if (strlen($value) == 1) {
throw new \InvalidArgumentException( throw \Doctrine\ORM\Query\QueryException::invalidParameterFormat($value);
"Invalid parameter format, '".$value."' given, ".
"but :<name> or ?<num> expected."
);
} }
$param = substr($value, 1); $param = substr($value, 1);

View File

@ -34,12 +34,11 @@ namespace Doctrine\ORM\Query;
*/ */
class QueryException extends \Doctrine\Common\DoctrineException class QueryException extends \Doctrine\Common\DoctrineException
{ {
public static function syntaxError($message) public static function syntaxError($message)
{ {
return new self('[Syntax Error] ' . $message); return new self('[Syntax Error] ' . $message);
} }
public static function semanticalError($message) public static function semanticalError($message)
{ {
return new self('[Semantical Error] ' . $message); return new self('[Semantical Error] ' . $message);
@ -49,4 +48,19 @@ class QueryException extends \Doctrine\Common\DoctrineException
{ {
return new self('Invalid parameter position: ' . $pos); return new self('Invalid parameter position: ' . $pos);
} }
public static function invalidParameterNumber()
{
return new self("Invalid parameter number: number of bound variables does not match number of tokens");
}
public static function invalidParameterFormat($value)
{
return new self('Invalid parameter format, '.$value.' given, but :<name> or ?<num> expected.');
}
public static function unknownParameter($key)
{
return new self("Invalid parameter: token ".$key." is not defined in the query.");
}
} }

View File

@ -130,7 +130,7 @@ class QueryTest extends \Doctrine\Tests\OrmFunctionalTestCase
public function testInvalidInputParameterThrowsException() public function testInvalidInputParameterThrowsException()
{ {
$this->setExpectedException("InvalidArgumentException"); $this->setExpectedException("Doctrine\ORM\Query\QueryException");
$q = $this->_em->createQuery('SELECT u FROM Doctrine\Tests\Models\CMS\CmsUser u WHERE u.name = ?'); $q = $this->_em->createQuery('SELECT u FROM Doctrine\Tests\Models\CMS\CmsUser u WHERE u.name = ?');
$q->setParameter(1, 'jwage'); $q->setParameter(1, 'jwage');