-
Notifications
You must be signed in to change notification settings - Fork 0
Implement $exists #17
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -174,6 +174,24 @@ func (c *Converter) convertFilter(filter map[string]any, paramIndex int) (string | |
v[operator] = c.arrayDriver(v[operator]) | ||
} | ||
values = append(values, v[operator]) | ||
case "$exists": | ||
// $exists only works on jsonb columns, so we need to check if the key is in the JSONB data first. | ||
isNestedColumn := c.nestedColumn != "" | ||
for _, exemption := range c.nestedExemptions { | ||
if exemption == key { | ||
isNestedColumn = false | ||
break | ||
} | ||
} | ||
if !isNestedColumn { | ||
// There is no way in Postgres to check if a column exists on a table. | ||
return "", nil, fmt.Errorf("$exists operator not supported on non-nested jsonb columns") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Alternatively we can always return TRUE if it's not a nested column (or this could be an option). There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. But that's not true. The column can also not exist. The only way to actually check is selecting from There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. That's right, good point. 👍 |
||
} | ||
neg := "" | ||
if v[operator] == false { | ||
neg = "NOT " | ||
} | ||
inner = append(inner, fmt.Sprintf("(%sjsonb_path_match(%s, 'exists($.%s)'))", neg, c.nestedColumn, key)) | ||
default: | ||
value := v[operator] | ||
op, ok := basicOperatorMap[operator] | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This code is duplicated in the
case nil:
as well, we could lift it above the switch.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah we can change those once the two pulls are both merged.