How Query.Or() works?
yaxinr opened this issue · comments
Gilfanov Ramil commented
This do not work how i assume
_, found := db.Query("goal").
WhereInt64("id", reindexer.SET, root.Nodes...).
WhereInt64("parent_updated_at", reindexer.GT, root.UpdatedAt).
Or().
WhereInt64("parent", reindexer.SET, root.Nodes...).
WhereInt64("parent_updated_at", reindexer.GT, root.UpdatedAt).
Get()
This work
_, found := db.Query("goal").
WhereInt64("id", reindexer.SET, root.Nodes...).
WhereInt64("parent_updated_at", reindexer.GT, root.UpdatedAt).
Get()
_, found2 := db.Query("goal").
WhereInt64("parent", reindexer.SET, root.Nodes...).
WhereInt64("parent_updated_at", reindexer.GT, root.UpdatedAt).
Get()
if found || found2 {
}
Alexey Torochkov commented
In your case it can be:
_, found := db.Query("goal").
WhereInt64("id", reindexer.SET, root.Nodes...).Or().WhereInt64("parent", reindexer.SET, root.Nodes...).
WhereInt64("parent_updated_at", reindexer.GT, root.UpdatedAt).
Get()
At the moment there is no parentheses implementation like in SQL foo="1" AND (foo="2" OR bar="3"), so the Or()/Not() operator only affects the next operator as far as I understand.
olegator77 commented
Hi.
In version 2.1.0 there are query brackets functional has been implemented.
So, now it is possible to write this query:
OpenBracket().
WhereInt64("id", reindexer.SET, root.Nodes...).
WhereInt64("parent_updated_at", reindexer.GT, root.UpdatedAt).
CloseBracket().
Or().
OpenBracket().
WhereInt64("parent", reindexer.SET, root.Nodes...).
WhereInt64("parent_updated_at", reindexer.GT, root.UpdatedAt).
CloseBracket()