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

Incorrect SQL query when nil Or clause #382

Open
MartyHub opened this issue Oct 7, 2024 · 1 comment
Open

Incorrect SQL query when nil Or clause #382

MartyHub opened this issue Oct 7, 2024 · 1 comment

Comments

@MartyHub
Copy link

MartyHub commented Oct 7, 2024

Hello,

not sure if this is expected?

https://go.dev/play/p/yMEuRKDSOSI

package main

import (
	"fmt"

	sq "github.com/Masterminds/squirrel"
)

func getFilter() sq.Or {
	return nil
}

func main() {
	users := sq.Select("*").From("users").Where(getFilter())

	sql, _, err := users.ToSql()
	if err != nil {
		panic(err)
	}

	fmt.Println(sql)
}

=> SELECT * FROM users WHERE (1=0)

IMHO this is really tricky and error-prone

@MartyHub
Copy link
Author

MartyHub commented Oct 7, 2024

Workaround: return sq.Sqlizer instead of sq.Or
https://go.dev/play/p/l0fZoUdUxXA

func getFilter() sq.Sqlizer {
	return nil
}

=> SELECT * FROM users

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

No branches or pull requests

1 participant