Revision 7 as of 2011-10-23 05:54:54

Clear message
Locked History Actions

Circumflex_ORM

Circumflex ORM

参考

とにかく動かしてみる

Circumflex ORMはCircumflexというウェブフレームワークの一部だが、ここでは単体で使用する。 DBはH2を用いた。

テーブルの定義とデータベース生成

Country, Cityはサンプルそのまま

import ru.circumflex._
import ru.circumflex.orm._
import ru.circumflex.core._

class Country extends Record[String, Country] {
  val code = "code".VARCHAR(2).NOT_NULL.DEFAULT("'ch'")
  val name = "name".TEXT.NOT_NULL

  def cities = inverseMany(City.country)
  def relation = Country
  def PRIMARY_KEY = code
}

object Country extends Country with Table[String, Country]

class City extends Record[Long, City] with SequenceGenerator[Long, City] {
  val id = "id".BIGINT.NOT_NULL.AUTO_INCREMENT
  val name = "name".TEXT
  val country = "country_code".TEXT.NOT_NULL
          .REFERENCES(Country)
          .ON_DELETE(CASCADE)
          .ON_UPDATE(CASCADE)

  def relation = City
  def PRIMARY_KEY = id
}

object City extends City with Table[Long, City]

object Creation {

  def main(args: Array[String]) {
    
    val cx = Circumflex
    cx("orm.connection.driver") = "org.h2.Driver"
    cx("orm.connection.url") = "jdbc:h2:sample"
    cx("orm.connection.username") = "sa"
    cx("orm.connection.password") = ""
    
    val unit = new DDLUnit(Country, City)
    unit.CREATE()
  }
}

どんなDDLが生成されているのか?

  println(Country.sqlCreate)
  println(City.sqlCreate)

レコードの挿入

      val country = new Country
      country.code := "jp"
      country.name := "japan"
      try {
        country.INSERT_!()
        COMMIT()
      } catch {
        case e:Exception => { e.printStackTrace(); }
      }

バグ

dialect.scalaの207行目付近

  def columnDefinition[R <: Record[_, R]](field: Field[_, R]): String = {
    var result = field.name + " " + field.sqlType
    if (field.notNull_?) result += " NOT NULL"
    if (field.unique_?) result += " UNIQUE" // <-- これが抜けてる
    result += defaultExpression(field)
    return result
  }

ドキュメント翻訳

- 本家ドキュメント翻訳