ent icon indicating copy to clipboard operation
ent copied to clipboard

How to query with bitwise op?

Open mapleray opened this issue 2 years ago • 2 comments

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?

mapleray avatar Nov 22 '23 01:11 mapleray

same question

JK-97 avatar Dec 19 '23 02:12 JK-97

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)
}))

nesymno avatar Jan 25 '24 14:01 nesymno