2011年5月26日木曜日

Lift Mapper How To

Lift Mapperでテーブル設計をする際の最小限の知識のまとめ
詳しいことはここを見てください。
主にMySQLを使用しているため、DBの用語等はMySQLのものとなります。

よく使われるフィールド

object DefaultFieldModel extends DefaultFieldModel with LongKeyedMetaMapper[DefaultFieldModel]

class DefaultFieldModel extends LongKeyedMapper[DefaultFieldModel]{
  def getSingleton = DefaultFieldModel 

  object idField extends MappedLongIndex(this) 

  object intField extends MappedInt(this)
  object longField extends MappedLong(this)
  object varcharField extends MappedString(this,100)// VARCHAR(100)
  object textField extends MappedText(this) // TEXT
  object dateField extends MappedDate(this) // DATE
  object dateTimeField extends MappedDateTime(this) // DATETIME

}

//usage

val newObject = DefaultFieldModel.createInstance
newObject.idField(1)
newObject.textField("hoge")
...
newObject.save()


val read = DefaultFieldModel.findByKey(1L).open_!
println("ID:" + read.idField.is)




AUTO INCREMENTなIDの設定 + CRUD画面の簡単生成

object AutoIncrement extends AutoIncrement with LongKeyedMetaMapper[AutoIncrement] with CRUDify[Long,AutoIncrement]{
  /*
  def menus : List[Menu]
  がCRUDifyにより追加されます
  これをSiteMenuMapに追加しておくことでCRUD画面が作成されます。
  */

}

class AutoIncrement extends LongKeyedMapper[AutoIncrement] widh IdPK{

  def getSingleton = AutoIncrement

  /**
   IdPKにて
   object id extends MappedLongIndex(this)
   のフィールドが作成されます。
  */

}



インデックス、複合インデックスの設定のやりかた

object IndexSample extends IndexSample with LongKeyedMetaMapper[IndexSample]{

  //複合インデックス設定
  override def dbIndexes = {
    List(
      new Index(unionIndex1 , unionIndex2 , unionIndex3),
      new Index(unionIndex3 , unionIndex2)
    )
  }

}

class IndexSample extends LongKeyedMapper[IndexSample] widh IdPK{

  def getSingleton = AutoIncrement

  //フィールドのインデックス設定
  object indexForSingleField extends MappedInt(this){
    override def dbIndexed_? = true
  }
  object unionIndex1 extends MappedInt(this)
  object unionIndex2 extends MappedInt(this)
  object unionIndex3 extends MappedInt(this)

}



OneToOne , OneToMany


object MailBox ...

class MailBox extends LongKeyedMapper[MailBox] with IdPK with OneToMany[Long, MailBox]{
  
  def getSingleton = MailBox

  //one to many
  object mails extends MappedOneToMany(Mail , Mail.mailBox, OrderBy(Mail.id, Ascending))
  
}

object Mail ...

class Mail extends LongKeyedMapper[MailBox] with IdPK{
  
  def getSingleton = Mail
  //one to one(many to one)
  object mailBox extends MappedLongForeignKey(this,MailBox)

}




ManyToMany

object User ...

class User extends LongKeyedMapper[User] with IdPK with ManyToMany{

  def getSingleton = User
  
  //many to many
  object mails extends MappedManyToMany(Mail , Mail.mailBox, OrderBy(Mail.id, Ascending))
  
}
object Role...

class Role extends LongKeyedMapper[Role] with IdPK{

  def getSingleton = Role 
}

object UesrHasRole ...

class UesrHasRole extends Mapper[UserHasRole]{
  
  def getSingleton = UserHasRole
  
  object user extends LongMappedMapper(this,User)
  object role extends LongMappedMapper(this,Role)

}

0 件のコメント:

コメントを投稿