package models
import scala.slick.driver.H2Driver.simple._
import Database.threadLocalSession
import play.api.db._
import play.api.Play._
import scala.slick.jdbc.{GetResult, StaticQuery => Q}
import Q.interpolation
case class Price(code:String,date:java.sql.Date,open:Int,high:Int,low:Int,close:Int) extends Ordered[Price]{
def compare(that:Price):Int={
date.getTime().compare(that.date.getTime())
}
}
object Prices extends Table[Price]("price") {
val DATE_TERM=180 // 180日分とる
def code=column[String]("code",O.PrimaryKey)
def date=column[java.sql.Date]("date",O.PrimaryKey)
def open=column[Int]("open")
def high=column[Int]("high")
def low=column[Int]("low")
def close=column[Int]("close")
def * = code ~ date ~ open ~ high ~ low ~ close <> (Price,Price.unapply _)
implicit val getPrice=GetResult(rs=>Price(rs<<,rs<<,rs<<,rs<<,rs<<,rs<<))
def select(code:String)= connectDB{
val list=sql"select * from price where code=$code order by date desc limit $DATE_TERM".as[PriceHistAdj].list.sorted
// sort 逆順なので並べ替える、GSON用にArrayList化
val ary=new java.util.ArrayList[Price]
for(l<-list)ary.add(l)
ary
}
def connectDB[Any](f: => Any): Any = {
Database.forDataSource(DB.getDataSource("data")) withSession {
f
}
}
}