Skip to content

Instantly share code, notes, and snippets.

@tpolecat
Created January 9, 2018 07:53
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tpolecat/83fa997be85ddcaa7558f8df748abb0b to your computer and use it in GitHub Desktop.
Save tpolecat/83fa997be85ddcaa7558f8df748abb0b to your computer and use it in GitHub Desktop.
Meta for citext Postgres type.

Ok, so to map CITEXT, which is not a standard type JDBC knows about, we need a wrapper class and need to move the value back and forth via the generic PGobject data type.

import org.postgresql.util.PGobject

case class CIText(s: String)

object CIText {
  
  implicit val CITextMeta: Meta[CIText] = 
    Meta.other[PGobject]("citext").xmap[CIText](
      o => CIText(o.getValue),
      a => {
        val o = new PGobject
        o.setType("citext")
        o.setValue(a.s)
        o
      }
    )

} 

It works both as a parameter and return type.

@ sql"select cit from test where cit = ${CIText("foo")}".query[CIText].quick.unsafeRunSync 
  CIText(foo)
  CIText(FOO)

And statement checking works now.

@ sql"select cit from test where cit = ${CIText("foo")}".query[CIText].check.unsafeRunSync 

  select cit from test where cit = ?SQL Compiles and TypechecksP01 CIText    OTHER (citext)
  ✓ C01 cit OTHER (citext) NOT NULL    CIText

@
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment