为什么我使用SQLite3和Scala Slick&获得SQLITE_ERROR Joda DateTime?

时间:2014-03-28 03:25:25

标签: sqlite scala slick typesafe

所以我从这里使用光滑的joda-mapper助手: https://github.com/tototoshi/slick-joda-mapper 当使用joda DateTime比较运行查询时,我收到错误。

  val today = new DateTime().withTimeAtStartOfDay()
  val yesterday = today.minusDays(1)
val yesterdaysRun = ReportRuns.forReport(report).filter {run => 
  run.runDate >= yesterday && run.runDate < today
}.sortBy(_.runDate.desc).firstOption

如果省略过滤器,此代码运行正常,但使用过滤器我得到以下错误:

java.sql.SQLException: [SQLITE_ERROR] SQL error or missing database (unrecognized token: "{")
    at org.sqlite.DB.newSQLException(DB.java:383)
    at org.sqlite.DB.newSQLException(DB.java:387)
    at org.sqlite.DB.throwex(DB.java:374)
    at org.sqlite.NativeDB.prepare(Native Method)
    at org.sqlite.DB.prepare(DB.java:123)
    at org.sqlite.PrepStmt.<init>(PrepStmt.java:42)
    at org.sqlite.Conn.prepareStatement(Conn.java:404)
    at org.sqlite.Conn.prepareStatement(Conn.java:399)
    at com.mchange.v2.c3p0.impl.NewProxyConnection.prepareStatement(NewProxyConnection.java:275)
    at scala.slick.jdbc.JdbcBackend$SessionDef$class.prepareStatement(JdbcBackend.scala:123)
    at scala.slick.jdbc.JdbcBackend$BaseSession.prepareStatement(JdbcBackend.scala:297)
    at scala.slick.jdbc.StatementInvoker.results(StatementInvoker.scala:28)
    at scala.slick.jdbc.StatementInvoker.iteratorTo(StatementInvoker.scala:16)
    at scala.slick.jdbc.Invoker$class.foreach(Invoker.scala:97)
    at scala.slick.jdbc.StatementInvoker.foreach(StatementInvoker.scala:9)
    at scala.slick.jdbc.Invoker$class.firstOption(Invoker.scala:44)
    at scala.slick.jdbc.StatementInvoker.firstOption(StatementInvoker.scala:9)
    at scala.slick.jdbc.UnitInvoker$class.firstOption(Invoker.scala:155)
    at scala.slick.driver.JdbcInvokerComponent$UnitQueryInvoker.firstOption(JdbcInvokerComponent.scala:50)
    at service.QueryRunnerService$$anonfun$scheduleReports$1$$anonfun$apply$1.apply(QueryRunnerService.scala:57)
...

想知道是否有人可以指出我正确的解决方案,这可能是joda mapper中的一个sql-escaping错误?还是别的什么?

以下是此生成(加上格式化)的SQL查询:

SELECT x2."id", 
       x2."reportid", 
       x2."query", 
       x2."run_date", 
       x2."status", 
       x2."start_time", 
       x2."end_time", 
       x2."error_message", 
       x2."error_details", 
       x2."created_at", 
       x2."updated_at" 
FROM   "report_runs" x2 
WHERE  ( x2."reportid" = 1 ) 
       AND ( ( x2."run_date" >= {ts '2014-03-28 00:00:00.0'} ) 
             AND ( x2."run_date" < {ts '2014-03-29 00:00:00.0'} ) ) 
ORDER  BY x2."run_date" DESC 

1 个答案:

答案 0 :(得分:0)

这是Joda Mapper中的一个错误(https://github.com/tototoshi/slick-joda-mapper)。

在该映射器中实际上有很多代码可以做非常简单的事情。要解决这个问题,最好为joda DateTime定义一个简单的自定义列类型,如下所示:

    import java.sql.Timestamp
    import org.joda.time.DateTime
    import org.joda.time.DateTimeZone.UTC

    object CustomColumnTypes {
      implicit lazy val jodaType = MappedColumnType.base[DateTime, Timestamp](
         {d => new Timestamp(d.getMillis)} ,
         {d => new DateTime(d.getTime, UTC)}
      )
    }

确保从您选择的驱动程序中导入相应的.simple._

例如:

import scala.slick.driver.SQLiteDriver.simple._