Skip to content

Ignore a nil Or in Where, matching nil predicates - #401

Open
AshSgDe29071999 wants to merge 1 commit into
Masterminds:masterfrom
AshSgDe29071999:fix/nil-or-where-clause
Open

Ignore a nil Or in Where, matching nil predicates#401
AshSgDe29071999 wants to merge 1 commit into
Masterminds:masterfrom
AshSgDe29071999:fix/nil-or-where-clause

Conversation

@AshSgDe29071999

Copy link
Copy Markdown

Fixes #382

Where(nil) is documented as a no-op, but a helper typed as sq.Or that returns nil is a typed-nil interface. That is not == nil, so it was compiled as WHERE (1=0) and inverted optional filters.

func getFilter() sq.Or { return nil }
Select("*").From("users").Where(getFilter())
// before: SELECT * FROM users WHERE (1=0)
// after:  SELECT * FROM users

An empty Or{} is unchanged (1=0). The same ignore is applied on Update and Delete Where for consistency.

Where(nil) is a no-op, but a typed-nil Or (return nil from a helper
typed as sq.Or) is a non-nil interface and became WHERE (1=0).

Treat a nil Or like a nil predicate so optional filters do not invert
the query. An empty Or{} is unchanged.

See Masterminds#382
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.

Incorrect SQL query when nil Or clause

1 participant