squirrel icon indicating copy to clipboard operation
squirrel copied to clipboard

Null array in where clause argument causes an invalid where clause (1=0)

Open gogrisohil opened this issue 4 years ago • 1 comments

Minimal working example

package main

import (
	"fmt"
	sq "github.com/Masterminds/squirrel"
)

func main() {
	var ids []uint64
	query, args, err := sq.Select("sub_account_id").From("someTable").Where(sq.Eq{"id": ids}).ToSql()
	fmt.Println(query, args, err)
}

Ideally I would expect the WHERE clause to be something like WHERE id IS NULL or WHERE id IN (NULL) and not (1=0) because that breaks the entire query.

gogrisohil avatar Feb 10 '21 20:02 gogrisohil

Squirrel translates e.g. Eq{"x": []int{1,2,3}} into x IN (1,2,3). Lots of Go code treats nil slices as equivalent to empty slices, and Squirrel does the same, so Eq{"x": []int(nil)} would be translated into x IN (). Unfortunately, x IN () is not valid syntax in all databases (MySQL, iirc), so instead it translates it into an "always false" expression (1=0).

WHERE x IS NULL would be true if x is NULL, which is a different semantic. WHERE id IN (NULL) evaluates to NULL, not "false", which can cause problems in more complex expressions.

In any case, any major change to this would be a breaking change.

lann avatar Feb 12 '21 13:02 lann