Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Specifications for date column #2434

Merged
merged 1 commit into from
Mar 5, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions framework/src/anorm/src/main/scala/anorm/Column.scala
Original file line number Diff line number Diff line change
Expand Up @@ -159,10 +159,22 @@ object Column {
nonNull((value, meta) =>
anyToBigDecimal(value, meta).map(BigDecimal(_)))

/**
* Parses column as Java Date.
* Time zone offset is the one of default JVM time zone
* (see [[java.util.TimeZone.getDefault]]).
*
* {{{
* import java.util.Date
*
* val d: Date = SQL("SELECT last_mod FROM tbl").as(scalar[Date].single)
* }}}
*/
implicit val columnToDate: Column[Date] = nonNull { (value, meta) =>
val MetaDataItem(qualified, nullable, clazz) = meta
value match {
case date: Date => Right(date)
case time: Long => Right(new Date(time))
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Raw time to date parsing

case _ => Left(TypeDoesNotMatch("Cannot convert $value: ${value.asInstanceOf[AnyRef].getClass} to Date for column $qualified"))
}
}
Expand Down
39 changes: 38 additions & 1 deletion framework/src/anorm/src/test/scala/anorm/ColumnSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,20 @@ package anorm
import javax.sql.rowset.serial.SerialClob

import acolyte.QueryResult
import acolyte.RowLists._
import acolyte.RowLists.{
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

More explicit imports

bigDecimalList,
byteList,
dateList,
doubleList,
floatList,
intList,
stringList,
longList,
rowList1,
shortList,
timeList,
timestampList
}
import acolyte.Acolyte.{ connection, handleQuery }
import acolyte.Implicits._

Expand Down Expand Up @@ -248,6 +261,30 @@ object ColumnSpec extends org.specs2.mutable.Specification {
}
}

"Column mapped as date" should {
val time = System.currentTimeMillis

"be parsed from date" in withQueryResult(
dateList :+ new java.sql.Date(time)) { implicit con =>
SQL("SELECT d").as(scalar[java.util.Date].single).
aka("parsed date") must_== new java.util.Date(time)
}

"be parsed from timestamp" in withQueryResult(
timestampList :+ new java.sql.Timestamp(time)) { implicit con =>
SQL("SELECT ts").as(scalar[java.util.Date].single).
aka("parsed date") must beLike {
case d => d.getTime aka "time" must_== time
}
}

"be parsed from time" in withQueryResult(longList :+ time) { implicit con =>
SQL("SELECT time").as(scalar[java.util.Date].single).
aka("parsed date") must_== new java.util.Date(time)

}
}

def withQueryResult[A](r: QueryResult)(f: java.sql.Connection => A): A =
f(connection(handleQuery { _ => r }))

Expand Down