telescope icon indicating copy to clipboard operation
telescope copied to clipboard

query with an escaped question mark does not reflect correct bindings

Open WaqasIbrahim opened this issue 1 year ago • 1 comments

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.

WaqasIbrahim avatar Feb 05 '24 06:02 WaqasIbrahim

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!

github-actions[bot] avatar Feb 06 '24 09:02 github-actions[bot]

A fix will be released today. Thanks!

driesvints avatar Jun 18 '24 14:06 driesvints