postgresqlhaskellcontravariancehasql

Encoding a Parameter List for a Hasql query


I'm trying to get Hasql to encode a list for a "select ... where in" query. It typechecks if I use contramany from contravariant-extras, but I get a syntax error at runtime.

import qualified Database.Encoders as E
import Contravariant.Extras

getTeamMembership :: Query [TeamId] [(TeamId, EmployeeId)]
getTeamMembership = statement q enc def True
  where
    enc = contramany (E.value E.teamId)
    q = "select workteam, employee \
        \from workteam_employee where workteam in $1"

Is it not possible to encode a parameter list?


Solution

  • The "IN" operator does not support that. You can only specify the individual values with it (e.g., IN ($1, $2, $3)). However what you want is easily achievable with the Array encoder and any and all Postgres functions, as per the docs.

    There are some Hasql tests showing it in action.

    There's also been a discussion on that subject on the issue tracker.