ent
ent copied to clipboard
How to query with bitwise op?
Hi, I have some problem to write query:
There is a field tag with integer type. How to write query like raw sql select * from table_name where tag & 16 = 16 ?
update: find a way:
func(s *sql.Selector) {
s.Where(sql.ExprP("tag & ? = ?", 16, 16))
}
any better answer?
same question
That way doesn't work for me for Postgres :( Any suggestions? @a8m
After investigation I should say that this version works for Postgres:
// to check that flag is up
s.Where(sql.P(func(b *sql.Builder) {
b.WriteString("flags & ").Arg(flag).WriteOp(sql.OpEQ).Arg(flag)
}))
// to check that flag is down
s.Where(sql.P(func(b *sql.Builder) {
b.WriteString("flags & ").Arg(flag).WriteOp(sql.OpEQ).Arg(0)
}))