Created
July 26, 2012 11:40
-
-
Save tototoshi/3181590 to your computer and use it in GitHub Desktop.
O/R Broker examples
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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