Anorm, Postgres and Dates

810 views
Skip to first unread message

jprudent

unread,
Sep 19, 2011, 12:11:56 AM9/19/11
to play-framework
hi!

Anorm Magic handles java.util.Date nicely but doesn't support
specifically java.sql.Date. However, the postgres driver can only
handle java.sql.Date

org.postgresql.util.PSQLException: Can't infer the SQL type to use
for an instance of java.util.Date. Use setObject() with an explicit
Types value to specify the type to use.
at
org.postgresql.jdbc2.AbstractJdbc2Statement.setObject(AbstractJdbc2Statement.java:
1801)
at
org.postgresql.jdbc3g.AbstractJdbc3gStatement.setObject(AbstractJdbc3gStatement.java:
37)
at
org.postgresql.jdbc4.AbstractJdbc4Statement.setObject(AbstractJdbc4Statement.java:
46)
at play.db.anorm.Sql$class.setAny(Anorm.scala:912)
at play.db.anorm.SimpleSql.setAny(Anorm.scala:829)

Can Anorm support java.sql.Date since most of drivers support this
type rather than java.util.Date? What would be the problem supporting
java.sql.Date?

Thanks!

Xuefeng Wu

unread,
Sep 19, 2011, 12:55:12 AM9/19/11
to play-fr...@googlegroups.com
The Anorm has a path that could use custom type, but it don''t release because play-scala 0.9.2 do not.

Sadache Aldrobi

unread,
Sep 19, 2011, 3:14:30 AM9/19/11
to play-fr...@googlegroups.com
What play-scala version are you using?


--
You received this message because you are subscribed to the Google Groups "play-framework" group.
To post to this group, send email to play-fr...@googlegroups.com.
To unsubscribe from this group, send email to play-framewor...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/play-framework?hl=en.




--
www.sadekdrobi.com
ʎdoɹʇuǝ

jprudent

unread,
Sep 20, 2011, 5:12:34 AM9/20/11
to play-framework
Hi!

I'm using 0.9.1

using java.util.Date calls jdbc setObject method and pg driver doesn't
like this

Thomas Dy

unread,
Sep 20, 2011, 5:27:57 AM9/20/11
to play-framework
You might want to use the java.sql.Timestamp class which the Postgres
driver supports.

It's also possible to add support for Timestamp to your companion
object by adding these methods:

import play.db.anorm._
import play.db.anorm.defaults._
import scala.reflect.Manifest

import java.sql.{Timestamp}


override def extendExtractor[C](f:(Manifest[C] =>
Option[ColumnTo[C]]), ma:Manifest[C]):Option[ColumnTo[C]] = (ma match
{
case m if m == Manifest.classType(classOf[Timestamp]) =>
Some(rowToTimestamp)
case _ => None
}).asInstanceOf[Option[ColumnTo[C]]]

def rowToTimestamp: Column[Timestamp] = {
Column[Timestamp](transformer = { (value, meta) =>
val MetaDataItem(qualified,nullable,clazz) = meta
value match {
case time:java.sql.Timestamp => Right(time)
case _ => Left(TypeDoesNotMatch("Cannot convert " + value + " to
Timestamp for column " + qualified))
}
})
}

The mechanism for this changed in the latest git release, though.

jprudent

unread,
Sep 20, 2011, 5:41:53 AM9/20/11
to play-framework
I'll try this, thanks Thomas.
I'll also try to adapt to java.sql.Date since I only want a date.

Sadache Aldrobi

unread,
Sep 20, 2011, 4:32:43 PM9/20/11
to play-fr...@googlegroups.com
By the way, java.sql.Date is supported on master.

jprudent

unread,
Sep 21, 2011, 11:34:57 AM9/21/11
to play-framework
Oh great news thanks!
Reply all
Reply to author
Forward
0 new messages