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

feature: support IN query clauses for nested JSON columns #272

Merged
merged 7 commits into from
Oct 15, 2024

Conversation

jiazheng2
Copy link
Contributor

@jiazheng2 jiazheng2 commented Oct 14, 2024

Checks

  • Do only one thing
  • Non breaking API changes
  • Tested

What did this pull request do?

Allow users to extract nested values from a JSON column and use JSON_CONTAINS to check if the
extracted value belongs a referenced array. This PR implements this in MySQL (and its derived dialects only).

Use case description

Suppose we have just a JSON array column

| json_array |
|------------|
| [1, 2]     |

We can now do

JSONArrayQuery("json_array").In([]int{1, 2})

which translates to

JSON_CONTAINS(JSON_ARRAY(1, 2), `json_array`)

Suppose we have a JSON column with a nested array as follows

| json_column |
|-------------|
| {"a": 1}    |

We can now do

JSONArrayQuery("json_column").In([]int{1, 2}, "a")

which translates to

JSON_CONTAINS(JSON_ARRAY(1, 2), JSON_EXTRACT(`json_column`, '$.a'))

We cannot use IN here because older versions of MySQL do not support comparison of JSON values
using the IN operator
.

@jiazheng2 jiazheng2 marked this pull request as ready for review October 15, 2024 06:32
@jinzhu jinzhu merged commit 1399d3c into go-gorm:master Oct 15, 2024
27 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants