Home > Blockchain >  Go boltDB query using bolthold 3 conditions
Go boltDB query using bolthold 3 conditions

Time:05-13

So I had this question before and I had an answer below the question which worked, but I just realized that the query I came out with does not work as planned.

Basically now if it works like this

(if the roleskey contains any of the roles in slice) and (if the tenantID is an empty string) or (if tenantIDKey is equal to tenantID)

But what I need is

(if the roleskey contains any of the roles in slice) AND (if the tenantID is an empty string OR if tenantIDKey is equal to tenantID)

Here is my current query:

query := bolthold.Where(roleskey).ContainsAny(bolthold.Slice(roles)...).And(tenantIDKey).Eq("").Or(bolthold.Where(tenantIDKey).Eq(tenantID))

Does anyone know how to solve this?

CodePudding user response:

You just need to move the emptiness check inside the OR condition.

query := bolthold.
  Where(roleskey).
  ContainsAny(bolthold.Slice(roles)...).
  And(
    bolthold.
      Where(tenantIDKey).
      Eq(tenantID).
      Or(tenantIDKey).
      Eq("")
  )

CodePudding user response:

Try:

query := bolthold.
         Where(tenantIDKey).Eq("").
         Or(
           bolthold.
           Where(tenantIDKey).
           Eq(tenantID)
         ).
         And(roleskey).
         ContainsAny(bolthold.Slice(roles)...)

Or

query := bolthold.
         Where(tenantIDKey).ContainsAny("", tenantID).
         And(roleskey).
         ContainsAny(bolthold.Slice(roles)...)

Or

query := bolthold.
         Where(tenantIDKey).In("", tenantID).
         And(roleskey).
         ContainsAny(bolthold.Slice(roles)...)
  • Related