Need help on comparing DateTime through Slick

亡梦爱人 提交于 2019-12-11 06:49:16

问题


Code:

 def getDatasetStats(startDate: DateTime, endDate: DateTime) = {
    val query = for(created <- datasets) yield created.createdOn
    db.run(query.filter(d => d >= startDate && d <= endDate).size.result)   
  }

Table:

protected class Datasets(tag: Tag) extends Table[SqlDataset](tag, "datasets") {
    // format: OFF
    def id = column[UUID]("id", O.PrimaryKey)

    def name = column[String]("name")

    def createdOn = column[DateTime]("created_on")

    def updatedOn = column[Option[DateTime]]("updated_on")

    def isPublic = column[Boolean]("public")

    def * = (id, name, createdOn, isPublic, updatedOn) <>
      ((SqlDataset.apply _).tupled, SqlDataset.unapply)

    implicit def jodaTimeMapping: BaseColumnType[DateTime] = MappedColumnType.base[DateTime, Timestamp](
      dateTime => new Timestamp(dateTime.getMillis),
      timeStamp => new DateTime(timeStamp.getTime)
    )

    // format: ON
  }

The above solution i have tried is not working properly. Also i could not use isBefore or isAfter since i am getting Rep[DateTime] from DB. I need help on applying the date range filter on top of the result.


回答1:


isBefore and isAfter does not work. You have to use <, <=, and >, >=.

Slick is built on top of JDBC. JDBC understands java.sql.Timestamp only. So provide implicit mapped column type for joda DateTime.

 implicit def jodaTimeMapping: BaseColumnType[DateTime] = MappedColumnType.base[DateTime, Timestamp](
    dateTime => new Timestamp(dateTime.getMillis),
    timeStamp => new DateTime(timeStamp.getTime)
  )

Now you can use <, <=, > and >=

> is equivalent to isAfter and < is equivalent to isBefore.

Have implicit in the scope where ever you are dealing with DateTime

table

protected class Datasets(tag: Tag) extends Table[SqlDataset](tag, "datasets") {

implicit def jodaTimeMapping: BaseColumnType[DateTime] = MappedColumnType.base[DateTime, Timestamp](
      dateTime => new Timestamp(dateTime.getMillis),
      timeStamp => new DateTime(timeStamp.getTime)
    )

def id = column[UUID]("id", O.PrimaryKey)

def name = column[String]("name")

def createdOn = column[DateTime]("created_on")

def updatedOn = column[Option[DateTime]]("updated_on")

def isPublic = column[Boolean]("public")

def * = (id, name, createdOn, isPublic, updatedOn) <>
  ((SqlDataset.apply _).tupled, SqlDataset.unapply)

}

Method

def getDatasetStats(startDate: DateTime, endDate: DateTime) = {

implicit def jodaTimeMapping: BaseColumnType[DateTime] = MappedColumnType.base[DateTime, Timestamp](
          dateTime => new Timestamp(dateTime.getMillis),
          timeStamp => new DateTime(timeStamp.getTime)
        )

  db.run(datasets.map(_.createdOn).filter(d => d >= startDate && d <= endDate).size.result)   
}



回答2:


My bad. The solution which i have tried works actually. The problem is with the pattern conversion which i have used to convert a string to DateTime.

Problematic code:

 val dtf = JDateTimeFormat.forPattern("yyyymmdd") 

Modified:

 val dtf = JDateTimeFormat.forPattern("yyyyMMdd")

mm -> miniutes

MM -> Month

Since i have used mm in lower case to mention the month which yields me improper results. Now i am getting proper results after the change.



来源:https://stackoverflow.com/questions/40082935/need-help-on-comparing-datetime-through-slick

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!