shadowhand/latitude

Parameters inside functions

oscarotero opened this issue · 5 comments

Hi.
I'm trying to upgrade a database related library with this latitude (nice work, btw) and have a query built in the following way

$query = $builder->update('post')
    ->set([
        'title' => param('New title'),
        'body' => param('New body'),
        'point' => fn('POINT', 1, 2),
    ])
    ->where(field('id')->eq(2));

The arguments of the POINT function are not converted to params. I have something like this:

UPDATE `post` SET `title` = ?, `body` = ?, `point` = POINT(`1`, `2`) WHERE `id` = ?

but what I want is this.

UPDATE `post` SET `title` = ?, `body` = ?, `point` = POINT(?, ?) WHERE `id` = ?

I've tried to use params inside the function:

fn('POINT', param(1), param(2))

but that throws an exception because fn only accept strings as arguments.
Is any way to archieve this?

Hmm yes I guess this could be considered a bug in fn(), though it was intended for SELECT statements and conditions, such as SUM() or COUNT().

I think you could achieve the same result using:

$query = $builder->update('post')
    ->set([
        'title' => 'New title',
        'body' => 'New body',
        'point' => express('POINT(%d, %d)', ...paramAll(1, 2)),
    ]);

Ok, that works. Thanks!
Note that 'POINT(%d, %d)' returns POINT(0, 0) but 'POINT(%s, %s)' returns POINT(?, ?).

Doh, right... All expressions need to use %s due to placeholders.

Was going to close this, but there's probably no good reason to strictly enforce that fn() only accepts strings. I will make it accept any type, default will assume that the input is column names.

Fixed in version 3.1.0.