我尝试添加我的自定义SQL函数。
我的函数是这样的:
DISTANCE( 15.154454, 5.121444, -15.321111, 15.12111)
我添加这个解析器来解析它:
use DoctrineORMQueryLexer;
class Distance extends DoctrineORMQueryASTFunctionsFunctionNode
{
public $lat_a = null;
public $lat_b = null;
public $lon_a = null;
public $lon_b = null;
public function parse(DoctrineORMQueryParser $parser)
{
$parser->match(Lexer::T_IDENTIFIER);
$parser->match(Lexer::T_OPEN_PARENTHESIS);
$this->lat_a = $parser->ArithmeticPrimary();
$parser->match(Lexer::T_COMMA);
$this->lon_a = $parser->ArithmeticPrimary();
$parser->match(Lexer::T_COMMA);
$this->lat_b = $parser->ArithmeticPrimary();
$parser->match(Lexer::T_COMMA);
$this->lon_b = $parser->ArithmeticPrimary();
$parser->match(Lexer::T_CLOSE_PARENTHESIS);
}
public function getSql(DoctrineORMQuerySqlWalker $sqlWalker)
{
return 'DISTANCE(' .
$this->lat_a->dispatch($sqlWalker) . ', ' .
$this->lon_a->dispatch($sqlWalker) . ', ' .
$this->lat_b->dispatch($sqlWalker) . ', ' .
$this->lon_b->dispatch($sqlWalker) .
')';
}
}
没关系,这工作正常,但是当我使用像"-15.321111"这样的负变量时,我得到一个错误 500...有了积极的变量,一切都没关系。
任何人都可以帮助我解析负变量?
试试这个:
public function parse(DoctrineORMQueryParser $parser)
{
$parser->match(Lexer::T_IDENTIFIER);
$parser->match(Lexer::T_OPEN_PARENTHESIS);
$this->lat_a = $parser->ArithmeticExpression();
$parser->match(Lexer::T_COMMA);
$this->lon_a = $parser->ArithmeticExpression();
$parser->match(Lexer::T_COMMA);
$this->lat_b = $parser->ArithmeticExpression();
$parser->match(Lexer::T_COMMA);
$this->lon_b = $parser->ArithmeticExpression();
$parser->match(Lexer::T_CLOSE_PARENTHESIS);
}
public function getSql(DoctrineORMQuerySqlWalker $sqlWalker)
{
return 'DISTANCE(' .
$sqlWalker->walkArithmeticExpression($this->lat_a) . ', ' .
$sqlWalker->walkArithmeticExpression($this->lon_a) . ', ' .
$sqlWalker->walkArithmeticExpression($this->lat_b) . ', ' .
$sqlWalker->walkArithmeticExpression($this->lon_b) .
')';
}