読者です 読者をやめる 読者になる 読者になる

謎言語使いの徒然

適当に気になった技術や言語を流すブログ。

ScalaActiveRecoard の 0.2.2 を弄る

scala OSS

ってもそんなに真面目にやる気ないですけどね。

sbt の build.sbt に

libraryDependencies ++= Seq(
  "org.scala-lang" % "scala-library" % "2.10.0" % "provided",
  "org.scala-lang" % "scala-compiler" % "2.10.0",
  "com.github.aselab" % "scala-activerecord_2.10" % "0.2.2",
  "mysql" % "mysql-connector-java" % "5.1.22",
  "ch.qos.logback" % "logback-classic" % "1.0.9"
)

てな感じで指定したら

package ar

import com.github.aselab.activerecord._
import dsl._
import java.util.Date

/*
CREATE TABLE token(
  id INT(11) NOT NULL PRIMARY KEY AUTO_INCREMENT,
  word VARCHAR(255) NOT NULL,
  appear INT(11) NOT NULL,
  saved  DATETIME NOT NULL
);

CREATE TABLE token_link(
  id INT(11) NOT NULL PRIMARY KEY AUTO_INCREMENT,
  lid INT(11) NOT NULL,
  rid INT(11) NOT NULL,
  appear INT(11) NOT NULL,
  saved  DATETIME NOT NULL
);
*/

case class Token(word:String, appear:Long, saved:Date) extends ActiveRecord
case class TokenLink(lid:Long, rid:Long, appear:Long, saved:Date) extends ActiveRecord

object Tables extends ActiveRecordTables {
  val token = table[Token]
  val link  = table[TokenLink]

  on(token)(t => declare(
    t.word is(dbType("varchar(255)"))
  ))
}

object Token extends ActiveRecordCompanion[Token]
object TokenLink extends ActiveRecordCompanion[TokenLink]

object Sample extends App {
  Tables.initialize

  println(Token.all.size)

  Tables.cleanup
}

見ればわかりますよね?