Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ARRAY with multiple arguments #95

Open
flaushi opened this issue Oct 5, 2021 · 2 comments
Open

ARRAY with multiple arguments #95

flaushi opened this issue Oct 5, 2021 · 2 comments

Comments

@flaushi
Copy link

flaushi commented Oct 5, 2021

I'd like to use this kind of postgres code Array_to_string(ARRAY['a', 'b', 'c'], ';') which gives a;b;c

but this does not parse in dql :

ARRAY_TO_STRING( ARRAY( 'a', 'b', 'c' ), ';')
-> [Syntax Error] line 0, col 70: Error: Expected Doctrine\ORM\Query\Lexer::T_CLOSE_PARENTHESIS, got ','
@LeoAdamek
Copy link

Looking at this package I've noticed too that the ARRAY function doesn't work correctly, it seems to accept only a single argument, rather than the variable number of arguments of the pg function.

My understanding is that the DQL ARRAY( 'foo', 'bar', 'baz' ) should produce the SQL ARRAY['foo','bar','baz'] , but instead throws a parser error as it's expecting only a single argument?

@Mashishe
Copy link

Mashishe commented Jun 20, 2022

Here a rewrite that permit to have array with multiple arg

<?php

namespace App\Dql;

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

/**
 * array with multiple element
 * ex: OVERLAPS(ARRAY('anru', 'tva'), l.fiscalites) = true
 */
class Arr extends FunctionNode
{
    private ?Node $field = null;

    private array $values = [];

    public function __construct()
    {
        parent::__construct("ARRAY");
    }

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

        $this->field = $parser->ArithmeticPrimary();

        $lexer = $parser->getLexer();

        while (count($this->values) < 1 || $lexer->lookahead['type'] !== Lexer::T_CLOSE_PARENTHESIS) {
            $parser->match(Lexer::T_COMMA);
            $this->values[] = $parser->ArithmeticPrimary(); // renvoi le node
        }

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

    public function getSql(SqlWalker $sqlWalker): string
    {
        $query = 'ARRAY[';

        $query .= $this->field->dispatch($sqlWalker);

        $query .= ', ';

        foreach ($this->values as $i => $iValue) {
            if ($i > 0) {
                $query .= ', ';
            }

            $query .= $iValue->dispatch($sqlWalker);
        }

        $query .= ']';

        return $query;
    }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants