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

query with an escaped question mark does not reflect correct bindings #1435

Open
WaqasIbrahim opened this issue Feb 5, 2024 · 1 comment

Comments

@WaqasIbrahim
Copy link

Telescope Version

4.17.5

Laravel Version

10.43.0

PHP Version

8.1.21

Database Driver & Version

PostgreSQL 15.3 (Debian 15.3-1.pgdg120+1)

Description

Hello, I have a Query builder macro in my application that checks if a JSON column contains any of the provided values. There is no built-in laravel function for this so I use a raw statement for this.

Builder::macro('whereJsonContainsAny', function (string $column, array $values) {
    /** @var Builder $this */
    $placeholders = implode(',', array_map(fn () => '?', $values));

    return $this->whereRaw("{$column} ??| array[{$placeholders}]", $values);
});

?| operators is used checks if any of the specified values exist in jsonb array.
??| in this is used to escape ? so query builder does not view this as a binding placeholder.

The query is executed successfully but telescope does not report the correct query.

Telescope result:

select
  "id"
from
  "movies"
where
  "release_date" between '2023-02-05 00:00:00'
  and '2024-02-05 00:00:00'
  and ("categories") :: jsonb @ > '["action"]'
  and categories 'thriller''crime' | array ['adventure','one-man-army','one-person-army-action']
  and keywords 'spy''terrorist' | array ['mission','secret-agent','action-hero','assasin','intelligence-service',?,?,?,?]
order by
  "rank" desc
limit
  21 offset 0

Expected result:

select
  "id"
from
  "movies"
where
  "release_date" between '2023-02-05 00:00:00'
  and '2024-02-05 00:00:00'
  and ("categories") :: jsonb @ > '["action"]'
  and categories ?| array ['thriller', 'crime', 'adventure']
  and keywords ?| array ['one-man-army','one-person-army-action','spy','terrorist','mission','secret-agent','action-hero','assasin','intelligence-service']
order by
  "rank" desc
limit
  21 offset 0

Another minor issue is the space between @> which throws an error if you copy and execute the query directly.

Steps To Reproduce

Wrtie a raw statement using an ?| operator comparing a jsonb array column for postgresql.

Copy link

github-actions bot commented Feb 6, 2024

Thank you for reporting this issue!

As Laravel is an open source project, we rely on the community to help us diagnose and fix issues as it is not possible to research and fix every issue reported to us via GitHub.

If possible, please make a pull request fixing the issue you have described, along with corresponding tests. All pull requests are promptly reviewed by the Laravel team.

Thank you!

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

No branches or pull requests

2 participants