【scala】【playframework】slick 2.0.0-M3 を使ってみる
新しいことにチャレンジするということはなかなか大変だ・・・
細かい点は気にせずに、おまかに。。。
まずはSQLの設定(application.conf)
db.default.driver=org.h2.Driver
db.default.url="jdbc:h2:file:play;MODE=PostgreSQL"
# --- !Ups CREATE TABLE EVENTS ( ID BIGINT(6) GENERATED BY DEFAULT AS IDENTITY(START WITH 1) NOT NULL PRIMARY KEY, -- 10万件 EVENT_NAME VARCHAR(100) NOT NULL, EVENT_DETAIL TEXT NOT NULL, START_DATE DATE, END_DATE DATE, LOCATION VARCHAR(200), AMOUNT NUMERIC, MAXIMUM_NUMBER_OF_PEOPLE INT(3), IS_PRIVATE BOOLEAN, PASSWORD BYTEA ); # --- !Downs DROP TABLE EVENTS;
んで、models
package models.events import play.api.db.DB import play.api.Play.current import scala.slick.driver.H2Driver.simple._ import scala.slick.lifted.Tag import java.sql.Date import org.joda.time.DateTime import scala.slick.ast.BaseTypedType import java.sql.Timestamp case class Event(id: Option[Int], eventName: String, eventDetail: String, startDate: Date, endDate: Date, location: String, amount: BigDecimal, maximumNumberOfPeople: Int, isPrivate: Boolean, password: String) class Events(tag: Tag) extends Table[Event](tag, "EVENTS") { implicit val dateTime: BaseTypedType[DateTime] = MappedColumnType.base[DateTime, Timestamp]( dt => new Timestamp(dt.getMillis), ts => new DateTime(ts.getTime) ) def id = column[Int]("ID", O PrimaryKey, O AutoInc) def eventName = column[String]("EVENT_NAME", O NotNull) def eventDetail = column[String]("EVENT_DETAIL") def startDate = column[Date]("START_DATE") def endDate = column[Date]("END_DATE") def location = column[String]("LOCATION") def amount = column[BigDecimal]("AMOUNT") def maximum_number_of_people = column[Int]("MAXIMUM_NUMBER_OF_PEOPLE") def isPrivate = column[Boolean]("IS_PRIVATE") def password = column[String]("PASSWORD") def * = (id.?, eventName, eventDetail, startDate, endDate, location, amount, maximum_number_of_people, isPrivate, password) <> (Event.tupled, Event.unapply) }
テスト
import org.specs2.mutable._ import org.specs2.specification.AllExpectations import org.specs2.specification.AutoExamples import scala.slick.lifted.TableQuery import scala.slick.driver.H2Driver.simple._ import play.api.db.DB import play.api.Play.current import play.api.test._ import play.api.test.Helpers._ import java.sql.Date class EventSpec extends Specification { isolated "Registers ".p "xxx".br eg { lazy val db = Database.forDataSource(DB.getDataSource()) val events = TableQuery[Events] running(FakeApplication()) { db.withSession { implicit session: Session => events += Event(None, "eventName", "eventDetail", new Date(System.currentTimeMillis()), new Date(System.currentTimeMillis()), "location", 100, 10, false, "") events.list.size === 1 } } } }
とりあえずやってみたけど、1.0.0 とは書き方が微妙に変わってますね。
あと、いつもEclipseの補完を頼りにコード書いてるけど
今回いろいろと補完が出てきてくれなくて困った(;´д`)トホ…
list とか出てきてくれなかったし
- アーティスト: 高垣彩陽
- 出版社/メーカー: ミュージックレイン
- 発売日: 2013/12/11
- メディア: CD
- この商品を含むブログ (2件) を見る