Permalink
Browse files

Improve SQLite timestamp parsing

  • Loading branch information...
trevorsibanda committed Jun 11, 2017
1 parent 58ba04e commit bc823febc152a760ffbccf8a13dc3ca9d5385192
@@ -171,6 +171,7 @@ class $name(_tableTag: Tag) extends profile.api.Table[$elementType](_tableTag, $
}
def defaultCode = {
case Some(v) => s"Some(${defaultCode(v)})"
case s: String if rawType == "java.sql.Timestamp" => s
case s:String => "\""+s.replaceAll("\"", """\\"""")+"\""
case None => s"None"
case v:Byte => s"$v"
@@ -182,6 +183,7 @@ class $name(_tableTag: Tag) extends profile.api.Table[$elementType](_tableTag, $
case v:Short => s"$v"
case v:Char => s"'$v'"
case v:BigDecimal => s"""scala.math.BigDecimal(\"$v\")"""
case v: java.sql.Timestamp => s"""java.sql.Timestamp.valueOf("${v}")"""
case v => throw new SlickException( s"Dont' know how to generate code for default value $v of ${v.getClass}. Override def defaultCode to render the value." )
}
// Explicit type to allow overloading existing Slick method names.
@@ -1,3 +1,4 @@
CREATE TABLE "SUPPLIERS" ("SUP_ID" INTEGER NOT NULL,"SUP_NAME" VARCHAR(254) NOT NULL,"STREET" VARCHAR(254) NOT NULL,"CITY" VARCHAR(254) NOT NULL,"STATE" VARCHAR(254) NOT NULL,"ZIP" VARCHAR(254) NOT NULL);
CREATE TABLE "COFFEES" ("COF_NAME" VARCHAR(254) NOT NULL,"SUP_ID" INTEGER NOT NULL,"PRICE" DOUBLE NOT NULL,"SALES" INTEGER NOT NULL,"TOTAL" INTEGER NOT NULL,constraint "SUP_FK" foreign key("SUP_ID") references "SUPPLIERS"("SUP_ID") on update NO ACTION on delete NO ACTION);
CREATE TABLE models (modelKey PRIMARY KEY, devKey, name, lang, version, code);
CREATE TABLE "TIMESTAMPS"("TS" TIMESTAMP NOT NULL DEFAULT "2016-07-29 09:14:56" , "TS2" TIMESTAMP NOT NULL DEFAULT "2016-01-01" , "TS3" TIMESTAMP NOT NULL DEFAULT 817466400000, "TS4" TIMESTAMP NOT NULL DEFAULT "21:00:00" , "TS5" TIMESTAMP NOT NULL DEFAULT "2013-10-07T08:23:19.120Z", TS6 TIMESTAMP NOT NULL DEFAULT "now", TS7 TIMESTAMP NOT NULL DEFAULT "2013-10-07 04:23:19.120-04:00");
@@ -89,6 +89,14 @@ object GeneratedCodeTest {
schema.create,
Suppliers += s,
Models += m,
Timestamps += new Timestamp(),
Timestamps.result.head.map{r =>
val c = new Timestamp()
val diff = 10*1000 //10 seconds
(r.ts, r.ts2, r.ts3, r.ts4, r.ts5, r.ts6, r.ts7) match{
case (c.ts, c.ts2, c.ts3, c.ts4, c.ts5, now, c.ts7) => assertTrue(c.ts6.compareTo(r.ts6) <= diff)
}
},
Suppliers.result.map(assertEquals(List(s), _)),
Models.result.map(assertEquals(List(m), _))
)
@@ -103,6 +103,24 @@ trait SQLiteProfile extends JdbcProfile {
override def varying = dbType == Some("VARCHAR")
override def default = meta.columnDef.map((_,tpe)).collect{
case ("null",_) => Some(None) // 3.7.15-M1
case (v , "java.sql.Timestamp") => {
import scala.util.{Try, Success}
val convertors = Seq((s: String) => new java.sql.Timestamp(s.toLong),
(s: String) => java.sql.Timestamp.valueOf(s),
(s: String) => new java.sql.Timestamp(javax.xml.bind.DatatypeConverter.parseDateTime(s).getTime.getTime),
(s: String) => new java.sql.Timestamp(javax.xml.bind.DatatypeConverter.parseDateTime(s.replaceAll(" ","T")).getTime.getTime),
(s: String) => {
if(s == "now")
"new java.sql.Timestamp(java.util.Calendar.getInstance().getTime().getTime())"
else
throw new Exception(s"Failed to parse timestamp - $s")
}
)
val v2 = v.replaceAll("\"", "")
convertors.collectFirst(fn => Try(fn(v2)) match{
case Success(v) => Some(v)
})
}
}.getOrElse{super.default}
override def tpe = dbType match {
case Some("DOUBLE") => "Double"

0 comments on commit bc823fe

Please sign in to comment.