orm icon indicating copy to clipboard operation
orm copied to clipboard

Support for JSONB where clauses in PostgreSQL

Open dapapko opened this issue 4 years ago • 11 comments

Is there any opportunity to find out if JSONB field contains one value or array of values using Cycle ORM ? Example for Eloquent ORM: $users = DB::table('users') ->whereJsonContains('options->languages', 'en') ->get();

$users = DB::table('users') ->whereJsonContains('options->languages', ['en', 'de']) ->get(); (https://laravel.com/docs/8.x/queries#json-where-clauses)

dapapko avatar Oct 28 '20 18:10 dapapko

Need some help or a bit of time to implement it.

wolfy-j avatar Oct 29 '20 08:10 wolfy-j

Yes, that would be great...

floor12 avatar Jan 19 '21 08:01 floor12

Have you tried new Expression or Fragment syntax? It now supports optional parameters that make SQL composing for some dialects easier.

wolfy-j avatar Jan 19 '21 11:01 wolfy-j

Not yet, I've only been playing with this orm for the first few days. I will look in the near future when I will do some aggregations for jsonb columns en postgres... Thanx.

floor12 avatar Jan 19 '21 17:01 floor12

Have you tried new Expression or Fragment syntax? It now supports optional parameters that make SQL composing for some dialects easier.

@wolfy-j maybe you could help me build query with "WHERE json_column_name ? 'some string'"?

I tried to use expression, but with no success (

->where(new Expression("json_column_name ? 'some_value'"))
or
->where('json_column_name', '?', 'some_value')

get me

SQLSTATE[42601]: Syntax error: 7 ERROR:  syntax error at or near "$1"  
LINE 3: WHERE "output_urls" $1 '"some_value"' 

floor12 avatar Feb 01 '21 12:02 floor12

When i switched driver to sqlite to take a look on the result sql in this case, i got expected error

SQLSTATE[HY000]: General error: 1 near "?": syntax error 

When driver set to sqlite ? sign doesn't replace with $1. But with postgres driver the result sql query contains $1.

Escaping ? with one or few \ doesn't halps: result sql query contains \$1 in this case. Any ideas? ? sign is very helpful to work with json array of strings...

floor12 avatar Feb 01 '21 23:02 floor12

Can you give us the full example and query you are trying to build?

wolfy-j avatar Feb 05 '21 09:02 wolfy-j

Hello! Yesterday, after some hours in the orm and the driver source code, i found that problem was in php-pdo extension. It was fixed in 7.4.

https://bugs.php.net/bug.php?id=71885 https://wiki.php.net/rfc/pdo_escape_placeholders

"Question marks can be escaped by doubling them".

But before i found it, i fixed my case with by replacing ? with jsonb_exists postgres function.

floor12 avatar Feb 05 '21 10:02 floor12

So I assume this issue is not an issue anymore?

wolfy-j avatar Feb 12 '21 12:02 wolfy-j

I well check how the question mark doubling works with ORM query builder.

floor12 avatar Feb 16 '21 23:02 floor12

@wolfy-j

I checked escaping with doubling it - it work great with ORM too. My example is

$this->dbal->database('default')
            ->table('table')
            ->select('name')
            ->where(new Fragment(sprintf("field_name ?? '%s'", $textToFind)))
            ->fetchAll();

Thanks for your work.

floor12 avatar Feb 23 '21 22:02 floor12