Skip to content

Instantly share code, notes, and snippets.

@tototoshi
Created July 26, 2012 11:40
Show Gist options
  • Save tototoshi/3181590 to your computer and use it in GitHub Desktop.
Save tototoshi/3181590 to your computer and use it in GitHub Desktop.
O/R Broker examples
import org.orbroker._
import org.orbroker.config._
object HelloWorld {
def main(args: Array[String]) {
val jdbcURL = args(0)
val config = new BrokerConfig(jdbcURL)
val broker = Broker(config)
val count = broker.readOnly() { session =>
session.selectOne("SELECT COUNT(*) FROM MYTABLE").get
}
printf("Hello World! (oh, and %d rows were counted)%n", count)
}
}
import org.orbroker._
import org.orbroker.config._
object HelloWorld {
def main(args: Array[String]) {
val jdbcURL = args(0)
val config = new BrokerConfig(jdbcURL)
val broker = Broker(config)
val count = broker.readOnly() { session =>
session.selectOne("SELECT COUNT(*) FROM MYTABLE").get
}
printf("Hello World! (oh, and %d rows were counted)%n", count)
}
}
case class Item (id: Option[Int], name: String, price: BigDecimal)
val InsertItem = Token[Int]('insertItem, BigDecimalConv) // Int refers to the generated key type
val newItem = Item(None, "Cheese", 7.95)
val savedItem = broker.transaction() { txn =>
val newId = txn.executeForKey(InsertItem, "item"->newItem)
item.copy(id = newId)
}
package models
import play.api.test._
import play.api.test.Helpers._
import org.orbroker._
import org.orbroker.config._
import org.specs2.mutable._
import play.api.Play.current
import java.io.File
class ProjectSpec extends Specification {
"Project" should {
"should find a project by id" in {
running (FakeApplication()) {
val sqlDir = new File("conf")
val brokerConfig = new BrokerConfig(current.configuration.getString("db.default.url").get)
FileSystemRegistrant(sqlDir).register(brokerConfig)
val broker = Broker(brokerConfig)
broker.transaction() { session =>
session.execute('insertProject, "project" -> Project(Some(1), "foo", "bar"))
}
}
success
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment