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

Auto-parenthesis for Where() #380

Open
dany74q opened this issue Jul 21, 2024 · 1 comment
Open

Auto-parenthesis for Where() #380

dany74q opened this issue Jul 21, 2024 · 1 comment

Comments

@dany74q
Copy link

dany74q commented Jul 21, 2024

Greets !

Quick suggestion to hear the community's thoughts before opening a PR:

I recently noticed that if one would use .Where() and pass it multiple OR clauses, say, without using proper sq.Or,
the statement would not be parenthesized:

	sql, _, _ := sq.Select("*").
		From("table").
		Where("a = ?", 1).
		Where("b = ? OR c = ?", 1, 2, 3).
		ToSql()

	println(sql)

---
SELECT * FROM table WHERE a = ? AND b = ? OR c = ?

This is unlike some other query builders, for instance in gorm, the same usage is parenthesized:

	db, _ := gorm.Open("postgres", "...")

	expr := db.Select("*").
		Table("table").
		Where("a = ?", 1).
		Where("b = ? OR c = ?", 2, 3).
		QueryExpr()

	field := reflect.ValueOf(expr).Elem().FieldByName("expr")
	fieldPtr := unsafe.Pointer(field.UnsafeAddr())

	fieldRef := reflect.NewAt(field.Type(), fieldPtr)
	println(fieldRef.Elem().Interface().(string))
---
SELECT * FROM "table"  WHERE (a = ?) AND (b = ? OR c = ?)

Even though sq providers a proper Or primitive, the above can still be mistakenly used, which could lead to insidious bugs where the latter part of the OR statement overrides predicates before it.

I was wondering if, maybe via an opt-in flag to be backwards compatible, it would make sense to auto-parenthesize statements inside Where() - it could be done for all statements, or only for those that contains "OR" statements.

Let me know the sentiment if this is something we're willing to push, and I'll open a PR.

@dany74q
Copy link
Author

dany74q commented Oct 13, 2024

Ping, maybe @MartyHub

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