Tommy Adamski
Tommy Adamski

Reputation: 577

Scala Slick Lifted Date GroupBy

I'm using Scala 2.10 with Slick 1.0.0 and trying to do a lifted query.

I have a table, "Logins", where I'm attempting to do a load, and groupBy on a Timestamp column. However, when I attempt to groupBy, I am running into an issue when I try and format the Timestamp field to extract only the day portion, to group the objects by the same day.

Given the objects:

id | requestTimestamp
1  | Jan 1, 2013 01:02:003
2  | Jan 1, 2013 03:04:005
3  | Jan 1, 2013 05:06:007
4  | Jan 2, 2013 01:01:001

I'd like to return a grouping out of the database by similar days, where, for the sake of brevity, the the following Formatted timestamp to id relationship happens, where the id's would actually be a list of objects

Jan 1, 2013 -> (1, 2, 3)
Jan 2, 2013  (4)

I've got the following slick table object:

private implicit object Logins extends Table[(Int, Timestamp)]("LOGINS") {
    def id = column[Int]("ID", O.PrimaryKey)
    def requestTimeStamp = column[Timestamp]("REQUESTTIMESTAMP", O.NotNull)
    def * = logId ~ requestTimeStamp
}

The following Query method:

val q = for {
    l <- Logins if (l.id >= 1 && l.id <= 4)
} yield l

val dayGroupBy = new java.text.SimpleDateFormat("MM/dd/yyyy")
val q1 = q.groupBy(l => dayGroupBy.format(l.requestTimeStamp))

db.withSession {
    q1.list
}

However, instead of getting the expected grouping, I get an exception on the line where I attempt the groupBy:

java.lang.IllegalArgumentException: Cannot format given Object as a Date

Does anyone have any suggestions on properly grouping by Timestamps out of the database?

Upvotes: 1

Views: 1518

Answers (1)

korefn
korefn

Reputation: 955

Timestamp and Date are not the same thing! Try to convert Timestamp to Human understandable text using calendar or SimpleDateTime.

Not so sure about the second one though!

Upvotes: 0

Related Questions