2/12/2012

Getting Started with mirage-scala

mirage-scala is the simple and powerful library to access RDBMS from Scala. I introduce how to use mirage-scala with sbt.

At first, add the following dependency into your build.sbt:

resolvers += "amateras-repo" at "http://amateras.sourceforge.jp/mvn/"
libraryDependencies += "jp.sf.amateras.mirage" %% "mirage-scala" % "0.0.3" % "compile"
view raw build.sbt hosted with ❤ by GitHub

And create the jdbc.properties and put it on the root of classpath.

jdbc.driver=org.h2.Driver
jdbc.url=jdbc:h2:tcp://localhost:9092/test
jdbc.user=sa
jdbc.password=
view raw jdbc.properties hosted with ❤ by GitHub

Next, you have to create the entity class. mirage-scala supports both of mutable and immutable style as entity class. This is the example of immutable style entity class:

case class Book(
@(PrimaryKey @field)(generationType = IDENTITY) bookId: Option[Long],
bookName: String,
author: String,
price: Option[Int]) {
// default constructor
def this() = this(None, null, null, None)
}
view raw Book.scala hosted with ❤ by GitHub

Ready for mirage-scala. Let's try to use it!

import jp.sf.amateras.mirage.scala._
Session.withTransaction { session =>
val sqlManager = session.sqlManager
// insert
sqlManager.insertEntity(Book(None, "Mirage in Action", "Naoki Takezoe", Some(20)))
// select by the primary key
val key: Long = 1
val book: Option[Book] = sqlManager.findEntity(classOf[book], key)
...
}

You can insert, update and delete a record using SqlManager and entity classes. And also you can select a record by the primary key.

When you must select records by the more complex query, you can do it using 2way-SQL which is the powerful feature of mirage-scala. I would like to write about it in the next entiry.

mirage-scala 0.0.3 is now available!

mirage-scala is the wrapper of Mirage for Scala. It provides the best solution to access RDBMS from Scala.

New features in mirage-scala 0.0.3:

  • Case class (immutable model) is available as entity.
  • Option[T] is available as property type.

By these new features, mirage-scala became the more suitable library for Scala to access RDBMS.

mirage-scala is still under preview release. So any request or feedback welcome!

Mirage 1.1.4 is now available!

Mirage is a simple SQL centric database access library. See the following URL to know about Mirage:

New features in Mirage 1.1.4:

  • Added RailsLikeNameConverter as an optional NameConverter.
  • Added FieldPropertyExtractor as an optional implementation of PropertyExtractor.
  • BugFix: Avoid depending on JDBC 4.0 (Java6) API in DefaultValueType
  • Chopping semicolon from the end of the SQL file.
  • Improve: Avoid using type Exception/RuntimeException to make interfaces intension revealing
  • Added SqlManagerImpl#setValueTypes to make it easier to configure valueTypes using Spring framework
  • BugFix: PropertyExtractor extracts static or final field
  • Improvement: DefaultResultEntityCreator just reqires no-argument constructor. It really doesn't matter if the constructor is public.
  • Improvement: Make ValueType parameterized. If you implement custom ValueTypes, these is not compatible in this version.
  • Added BreakIterationException to discontinue iteration search.
  • BugFix: @Column is available for the select query.

Since this version, we provide a source code cross reference generated by Sorcerer. It generates so rich HTML based cross reference which provide references search like Eclipse. See the follwing URL:

I hope Mirage helps your development. Enjoy!