Commit a9d739a7 authored by beberlei's avatar beberlei

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

parent 26a2ec2e
...@@ -209,12 +209,12 @@ final class Query extends AbstractQuery ...@@ -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)) {
......
...@@ -43,10 +43,7 @@ class InputParameter extends Node ...@@ -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);
......
...@@ -34,12 +34,11 @@ namespace Doctrine\ORM\Query; ...@@ -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 ...@@ -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.");
}
} }
\ No newline at end of file
...@@ -130,7 +130,7 @@ class QueryTest extends \Doctrine\Tests\OrmFunctionalTestCase ...@@ -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');
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment