7/15/2011

mirage-scala: Scala binding of Mirage

I started working for mirage-scala that is Scala binding of Mirage. mirage-scala works as wrapper of Mirage and provides the best interface to access to the database using Mirage for Scala.

This is a simple example to query using mirage-scala:

// A class that mapped to ResultList
class Book (
  val bookId: Int,
  val bookName: String,
  val author: String,
  val price: Int
)

// get Session and SqlManager
val session: Session = SessionFactory.getSession()
val sqlManager: SqlManager = session.getSqlManager()

// begin transaction
session.begin()

try {
  val books: List[Book] = sqlManager.getResultList(
    classOf[Book], Sql("""
      SELECT BOOK_ID, BOOK_NAME, AUTHOR, PRICE
      FROM BOOK
      /*IF author!=null*/
        WHERE AUTHOR = /*author*/
      /*END*/
    """), Map("author"->"Naoki Takezoe"))
  
  books.foreach { book =>
    println("bookId: " + book.bookId)
    println("bookName: " + book.bookName)
    println("author: " + book.author)
    println("price: " + book.price)
    println("--")
  }
  
  // commit transaction
  session.commit()
} catch {
  // rollback transaction
  ex: Exception => session.rollback()
}

A most important feature of Mirage is dynamic SQL template named 2waySQL. In Mirage, you have to create it as an external file on the classpath. However in mirage-scala, you can write it inline using multi-line string literal. Of course, you can also use an external file as following:

val result: List[Book] = sqlManager.getResultList(
  classOf[Book], 
  SqlFile("META-INF/selectBooks.sql"), 
  Map("author"->"Naoki Takezoe"))

mirage-scala is still under development. I'm evaluating APIs feaibility. So APIs might be changed considerably in the release version.

0 件のコメント: