cphalcon icon indicating copy to clipboard operation
cphalcon copied to clipboard

[NFR] PHQL: User-Defined Functions

Open igorgolovanov opened this issue 13 years ago • 10 comments

I think this functional (Custom functions) is very important because all RDBMS have a lot of custom functions/extensions which help decide a lot of issues and give very good performance.

Simple way

This case uses simple way to define a user query function. Function registered as pattern.

$di->modelsManager->registerSimpleQueryFunction('GET_MONTH', 'MONTH(?0)');

How to use?

$phql = "SELECT GET_MONTH(p.createdAt) month_created FROM Store\Products p WHERE p.status = :status:"

Expected SQL:

SELECT MONTH(p.createdAt) AS month_created FROM products AS p WHERE p.status = :status:

Class way

When uses class then you can parse functions with more heavy logic by your logic.

class TimestampDiffClassName
{
    public $firstDatetimeExpression = null;
    public $secondDatetimeExpression = null;
    public $unit = null;

    public function parse(Parser $parser)
    {
        $parser->match(Lexer::T_IDENTIFIER);
        $parser->match(Lexer::T_OPEN_PARENTHESIS);
        $parser->match(Lexer::T_IDENTIFIER);
        $lexer = $parser->getLexer();
        $this->unit = $lexer->token['value'];
        $parser->match(Lexer::T_COMMA);
        $this->firstDatetimeExpression = $parser->getArithmetic();
        $parser->match(Lexer::T_COMMA);
        $this->secondDatetimeExpression = $parser->getArithmetic();
        $parser->match(Lexer::T_CLOSE_PARENTHESIS);
    }

    public function getSql()
    {
        return sprintf('TIMESTAMPDIFF(%s, %s, %s)',
            $this->unit,
            $this->firstDatetimeExpression->dispatch(),
            $this->secondDatetimeExpression->dispatch()
        );
    }
}

$di->modelsManager->registerQueryFunction('GET_MYSQL_TIMESTAMPDIFF', 'TimestampDiffClassName');

Usage:

$phql = "SELECT GET_MYSQL_TIMESTAMPDIFF(DAY, p.createdAt, p.updatedAt) day_diff FROM Store\Products p WHERE p.status = :status:";

Expected SQL:

SELECT TIMESTAMPDIFF(DAY, p.createdAt, p.updatedAt) AS day_diff FROM products AS p WHERE p.status = :status:

Whete it implemented?

Doctrine: http://www.doctrine-project.org/blog/doctrine2-custom-dql-udfs.html and many examples for mysql + doctine: https://github.com/beberlei/DoctrineExtensions/tree/master/lib/DoctrineExtensions/Query/Mysql

Want to back this issue? Post a bounty on it! We accept bounties via Bountysource.

igorgolovanov avatar May 20 '13 14:05 igorgolovanov

+++ Good idea!

alvassin avatar May 21 '13 14:05 alvassin

+1

beermeat avatar Aug 06 '13 19:08 beermeat

+1

tmihalik avatar May 13 '15 11:05 tmihalik

:+1:

Green-Cat avatar May 13 '15 13:05 Green-Cat

@golovanov This is now a feature in phalcon 2.0.3. https://github.com/phalcon/cphalcon/commit/081331ab210e7568c3a6570bd5c6b612a4dc696a

@phalcon @andresgutierrez Please close.

Green-Cat avatar Jun 09 '15 20:06 Green-Cat

@Green-Cat The implemented feature is not what he asked here

andresgutierrez avatar Jun 09 '15 20:06 andresgutierrez

Oups, my bad. What does it do?

Green-Cat avatar Jun 09 '15 20:06 Green-Cat

This: https://gist.github.com/andresgutierrez/2a729903f4b189e3ce75

andresgutierrez avatar Jun 09 '15 20:06 andresgutierrez

Isn't that what this NFR asks for? I mean the "Simple Way" from the OP.

Green-Cat avatar Jun 09 '15 20:06 Green-Cat

Yeah, it's the same, I thought he was asking the Hibernate style of doing this

andresgutierrez avatar Jun 09 '15 20:06 andresgutierrez