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: implement in for sqlite databases #276

Merged
merged 2 commits into from
Nov 14, 2024

Conversation

jiazheng2
Copy link
Contributor

@jiazheng2 jiazheng2 commented Oct 18, 2024

Checks

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

What did this pull request do?

This PR ensures that the implementation for JSONArrayQuery in the SQLite dialect is now complete. That is,
the JSONArrayQuery.In method now works for SQLite.

This should resolve issue #151 partially.

Use case description

Previously, we implemented JSONArrayQuery.In for MySQL. Now we can do the same for SQLite.

| id | json_column |
|----|-------------|
| 1  | [1, 2]      |
| 2  | {"a": 1}    |

If we wish to check if the element at key "a" is in the set $\{1, 2\}$, we can do

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

This gets translated to

CASE 
    WHEN json_type("json_column", "$.a") = 'array' 
        THEN NOT EXISTS(SELECT 1 FROM json_each("json_column", "$.a") WHERE value NOT IN (1, 2))
    ELSE json_extract("json_column", "$.a") IN (1, 2)
END

This query returns row 2.

The reason for differentiating arrays and other JSON objects is because MySQL's JSON_CONTAINS function
is able to check if an array's elements are all within another array. If we wish to achieve feature parity for the
different dialects, the SQLite implementation should also support such queries.

For arrays, we check if there is an element that does not exist in the target array. For other json objects, we use
IN directly.

@jiazheng2 jiazheng2 marked this pull request as ready for review October 18, 2024 06:14
@jiazheng2
Copy link
Contributor Author

@jinzhu 麻烦看一下 🙏

@jinzhu jinzhu merged commit e503f10 into go-gorm:master Nov 14, 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