Closed bentranter closed 2 years ago
This would be a breaking change to the API. Though the behavior may be confusing I don't think it is worth potentially breaking dependent projects over.
Sounds good @lann. I was hoping that despite it being a breaking change, it would be a rare enough case that it could be considered a bug fix, but I understand. Thanks for the response.
TL;DR: Adds a check to protect against accidental
WHERE (1=1)
clauses when executing an UPDATE or DELETE query with a nil or emptyEq
map.Background
In #277, a user asked why passing a nil array in a
WHERE
clause creates a statement that containsWHERE (1=0)
. The explanation there was that for compatibility between databases, that needs to evaluate to an "always false" statement. I think this is a good trade off, since it prevents potentially unexpected behaviour.Motivation
Unlike nil arrays, passing a nil or empty
Eq
map evaluates to an "always true"WHERE
clause:https://github.com/Masterminds/squirrel/blob/49f26833fcb82e3a493770a5a29d66d155289889/expr.go#L139-L144
While this is probably fine for
SELECT
orINSERT
statements, I feel like this can cause unexpected and potentially dangerousUPDATE
orDELETE
statements.For example, consider a
DELETE
query where theEq
map is populated conditionally:If this method is called as:
Squirrel currently generates the following MySQL query:
Which will delete every row in that table.
While the above example might not seem like something that would realistically happen (and should be caught easily with tests), for more complex predicate building logic, combined with some bad error handling, I would argue it's too easy to pass values that would end up creating a nil
Eq
map. It happened to me 😅Proposed Solution
For
UPDATE
andDELETE
queries, I've:Eq
map passed as the only argument to theWHERE
clause,I've tried to keep this PR as small as possible – this doesn't change the behaviour for passing
nil
to the where clause, or in any case where the predicate isn't amap[string]interface{}
(passed using theEq
helper), and doesn't affect the behaviour forINSERT
orSELECT
queries.