matching / literal value of byte array using Postgres driver

262 views
Skip to first unread message

Adam Mackler

unread,
Jan 11, 2013, 8:06:23 AM1/11/13
to scala...@googlegroups.com
Hi:

I am wanting to use the filter() method of Query to match on the value of a column of type bytea in PosgreSQL.  I get a SlickException message telling me that
Array[Byte] does not have a literal representation

I see in driver/PostgresDriver.scala line 103 that ByteArrayTypeMapperDelegate is defined, extending a superclass' member class of the same name, and overriding value member sqlTypeName but not overriding the valueToSQLLiteral() method, which is what I think would be necessary for matching on a bytea-type column without getting the mentioned error.

So I made a first attempt to write such a method.  Here is the diff of PostgresDriver.scala with the change:
a103 2
      override def valueToSQLLiteral(value: Array[Byte]) =
        "E'\\\\x" + value.map( b => (new scala.runtime.StringFormat(b)).formatted("%02X")).mkString + "'"


I put this modified PostgresDriver.scala file into my sbt project along with the rest of my sources, changed the package name to my project, and changed the
import scala.slick.driver.PostgresDriver.simple._
line to refer to my project package instead of scala.slick.driver.

Now I get this error here:
[error] /usr/home/mackler/myproject/src/main/scala/PostgresDriver.scala:107: type mismatch;
[error]  found   : String
[error]  required: Nothing
[error]         "E'\\\\x" + value.map( b => (new scala.runtime.StringFormat(b)).formatted("%02X")).mkString + "'"
[error]                                                                                                     ^
[error] one error found
[error] (compile:compile) Compilation failed
[error] Total time: 6 s, completed Jan 11, 2013 5:54:19 AM


This is a little beyond me, but I get the impression that this definition of valueToSQLLiteral() from line 77 of driver/BasicTypeMapperDelegatesComponent.scala
      override def valueToSQLLiteral(value: Array[Byte]) =
        throw new SlickException("Array[Byte] does not have a literal representation")

is a problem because it throws an exception but doesn't return a String, even though it's overriding the definition on line 164 of lifted/TypeMapper.scala that has type String, and somehow it's changing the type of that method from String to Nothing.

So I've tried a few things, but none worked and it was getting messy.  I tried copying the BasicTypeMapperDelegatesComponent.scala file into my project and changing the definition of valueToSQLLiteral() so it returns a String.  I tried changing the definition of valueToSQLLiteral() in PostgresDriver.scala so it extends DriverTypeMapperDelegate[Array[Byte]].

My gut tells me there must be a simpler way to get this to work.  Obviously the best thing would be if the distributed version of PostgresDriver.scala were changed to produce byte array literals.  But unless and until that happens, can anyone give me a suggestion about how I can get the Query.filter() method to work with bytea-type columns in a PostgreSQL database?

Thanks very much,
--
Adam Mackler

Stig Brautaset

unread,
Aug 26, 2013, 4:55:46 PM8/26/13
to scala...@googlegroups.com
Hi Adam,

Did you manage to solve this? I am running into the same issue, trying to use filter on a BYTEA field.

"scala.slick.SlickException: Array[Byte] does not have a literal representation"

I am trying to store the result of SHA-256 passwords, and this returns an Array[Byte] instance.  My current work-around is to convert it to a hex string, but it feels like this should not be necessary since Pg can store binary data directly. Although I haven't actually checked whether the issue here is Slick or Pg... can Pg store, but not compare, binary data? Or is it just slick not able to do that yet?

Stig

Barry Kaplan

unread,
Nov 6, 2013, 6:33:42 PM11/6/13
to scala...@googlegroups.com
I too now have this issue with HSQL. MySQL seems to work though. Any solution?

Christopher Vogt

unread,
Nov 7, 2013, 3:55:25 PM11/7/13
to scala...@googlegroups.com
This is fixed in Slick 2.0-M3, which is expected to be release tomorrow.
No fix for 1.0.1, please upgrade.

Chris
Reply all
Reply to author
Forward
0 new messages