1

i try to add my custom sql function.

my function is like this :

DISTANCE( 15.154454, 5.121444, -15.321111, 15.12111)

I add this parser for parse it :

use Doctrine\ORM\Query\Lexer;

class Distance extends \Doctrine\ORM\Query\AST\Functions\FunctionNode
{
    public $lat_a = null;
    public $lat_b = null;
    public $lon_a = null;
    public $lon_b = null;

    public function parse(\Doctrine\ORM\Query\Parser $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(\Doctrine\ORM\Query\SqlWalker $sqlWalker)
    {
        return 'DISTANCE(' .
        $this->lat_a->dispatch($sqlWalker) . ', ' .
        $this->lon_a->dispatch($sqlWalker) . ', ' .
        $this->lat_b->dispatch($sqlWalker) . ', ' .
        $this->lon_b->dispatch($sqlWalker) .
        ')';
    }
}

It's ok, this work fine but when i use negative var like "-15.321111", i get an error 500... With positive var, all it's ok.

Anyone can help me for parse negative var ?

1 Answer 1

3

Try this:

public function parse(\Doctrine\ORM\Query\Parser $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(\Doctrine\ORM\Query\SqlWalker $sqlWalker)
{
    return 'DISTANCE(' .
    $sqlWalker->walkArithmeticExpression($this->lat_a) . ', ' .
    $sqlWalker->walkArithmeticExpression($this->lon_a) . ', ' .
    $sqlWalker->walkArithmeticExpression($this->lat_b) . ', ' .
    $sqlWalker->walkArithmeticExpression($this->lon_b) .
    ')';
}
Sign up to request clarification or add additional context in comments.

1 Comment

It's ok thx for help. I just use ArithmeticExpression in parse and all work ok.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.