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

Support for JSONB where clauses in PostgreSQL #134

Closed
dapapko opened this issue Oct 28, 2020 · 11 comments · Fixed by #445
Closed

Support for JSONB where clauses in PostgreSQL #134

dapapko opened this issue Oct 28, 2020 · 11 comments · Fixed by #445
Assignees
Labels
type:enhancement Enhancement.

Comments

@dapapko
Copy link

dapapko commented Oct 28, 2020

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)

@wolfy-j
Copy link
Contributor

wolfy-j commented Oct 29, 2020

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

@floor12
Copy link

floor12 commented Jan 19, 2021

Yes, that would be great...

@wolfy-j
Copy link
Contributor

wolfy-j commented Jan 19, 2021

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

@floor12
Copy link

floor12 commented Jan 19, 2021

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
Copy link

floor12 commented Feb 1, 2021

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
Copy link

floor12 commented Feb 1, 2021

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...

@wolfy-j
Copy link
Contributor

wolfy-j commented Feb 5, 2021

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

@floor12
Copy link

floor12 commented Feb 5, 2021

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.

@wolfy-j
Copy link
Contributor

wolfy-j commented Feb 12, 2021

So I assume this issue is not an issue anymore?

@floor12
Copy link

floor12 commented Feb 16, 2021

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

@floor12
Copy link

floor12 commented Feb 23, 2021

@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.

@roxblnfk roxblnfk added this to Cycle Jan 10, 2022
@roxblnfk roxblnfk moved this to Backlog in Cycle Jan 10, 2022
@msmakouz msmakouz moved this from Backlog to In Progress in Cycle Oct 30, 2023
@github-project-automation github-project-automation bot moved this from In Progress to Done in Cycle Nov 25, 2023
@roxblnfk roxblnfk moved this from Done to Released in Cycle Nov 27, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type:enhancement Enhancement.
Projects
Status: Released
Development

Successfully merging a pull request may close this issue.

5 participants