Pergunta

so I'm using the slick joda-mapper helper from here: https://github.com/tototoshi/slick-joda-mapper and when running a query with a joda DateTime comparison I get an error.

  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

This code runs fine if I omit the filter, but with the filter I get the below error:

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)
...

Was wondering if someone could point me in the right direction for a solution, is this potentially a sql-escaping bug in the joda mapper? Or is it something else?

Below is the SQL query that this generating (plus formatting):

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 
Foi útil?

Solução

This is a bug in the Joda Mapper (https://github.com/tototoshi/slick-joda-mapper ).

There is actually a lot of code in that mapper to do something very simple. To get around this problem it's best to just define a simple custom column type for joda DateTime like this:

    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)}
      )
    }

Make sure to import the respective .simple._ from your driver of choice.

eg:

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

For latest versions of slick, scala and joda Packages to import:

  "org.joda" % "joda-convert" % "2.2.1", // for time convert
  "com.github.tototoshi" %% "slick-joda-mapper" % "2.4.2", // 2.4 doesn't work
  "joda-time" % "joda-time" % "2.7",
  "org.xerial" % "sqlite-jdbc" % "3.30.1", // sqlite driver

Slick packages:

"com.typesafe.slick" %% "slick" % "3.3.2",
"com.typesafe.slick" %% "slick-codegen" % "3.3.2", // for generating Table schema for sqlite db

Imports for the code:

import slick.jdbc.SQLiteProfile.api._
Database.forURL(url = absoluteDatabaseUrl)
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top