Skip to content

Add support for DistanceFunction #332

@seb-jean

Description

@seb-jean

Hello,

Do you think it would be worth adding DistanceFunction to your repository? :)

Here it is in one of my projects:

<?php

namespace App\Doctrine\Query;

use Doctrine\ORM\Query\AST\Functions\FunctionNode;
use Doctrine\ORM\Query\AST\Node;
use Doctrine\ORM\Query\Parser;
use Doctrine\ORM\Query\SqlWalker;
use Doctrine\ORM\Query\TokenType;

/**
 * "DISTANCE" "(" StringPrimary "," StringPrimary ")".
 */
final class DistanceFunction extends FunctionNode
{
    public Node $entityLocation;
    public Node $latitude;
    public Node $longitude;

    public function getSql(SqlWalker $sqlWalker): string
    {
        $entityLocation = $this->entityLocation->dispatch($sqlWalker);

        return \sprintf(
            'earth_distance(ll_to_earth(%s[1], %s[0]),ll_to_earth(%s, %s))',
            $entityLocation,
            $entityLocation,
            $this->latitude->dispatch($sqlWalker),
            $this->longitude->dispatch($sqlWalker),
        );
    }

    public function parse(Parser $parser): void
    {
        $parser->match(TokenType::T_IDENTIFIER);
        $parser->match(TokenType::T_OPEN_PARENTHESIS);

        $this->entityLocation = $parser->ArithmeticPrimary();
        $parser->match(TokenType::T_COMMA);
        $this->latitude = $parser->ArithmeticPrimary();
        $parser->match(TokenType::T_COMMA);
        $this->longitude = $parser->ArithmeticPrimary();

        $parser->match(TokenType::T_CLOSE_PARENTHESIS);
    }
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions