Skip to content
/ jdub Public
forked from SimpleFinance/jdub

A damn simple JDBC wrapper. Y'know. For databases.

License

Notifications You must be signed in to change notification settings

vasa-chi/jdub

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

jdub

A damn simple JDBC wrapper. Y'know. For databases.

Requirements

  • Java SE 6
  • Scala 2.8.1 or 2.9.0-1 or 2.9.1
  • Metrics 2.0.0-BETA16
  • Logula 2.1.3
  • Tomcat DBCP (not the app server)

How To Use

First, specify Jdub as a dependency:

<repositories>
  <repository>
    <id>repo.codahale.com</id>
    <url>http://repo.codahale.com</url>
  </repository>
</repositories>

<dependencies>
  <dependency>
    <groupId>com.codahale</groupId>
    <artifactId>jdub_${scala.version}</artifactId>
    <version>0.0.5</version>
  </dependency>
</dependencies>

(Don't forget to include your JDBC driver!)

Second, connect to a database:

val db = Database.connect("jdbc:postgresql://localhost/wait_what", "myaccount", "mypassword")

Third, run some queries:

case class GetUser(userId: Long) extends Query[Option[User]] {
  val sql = trim("""
SELECT id, email, name
  FROM users
 WHERE id = ?
""")

  val values = userId :: Nil
  
  def reduce(results: Iterator[Row]) = {
    for (row <- results;
         id <- row.long("id");
         email <- row.string("email");
         name <- row.string("name"))
      yield User(id, email, name)
  }.toStream.headOption
}

// this'll print the user record for user #4002
println(db(GetUser(4002)))

Fourth, execute some statements:

case class UpdateUserEmail(userId: Long, oldEmail: String, newEmail: String) extends Statement {
  val sql = trim("""
UPDATE users
   SET email = ?
 WHERE userId = ? AND email = ?
""")

  val values = userId :: oldEmail :: newEmail :: Nil
}

// execute the statement
db.execute(UpdateUserEmail(4002, "[email protected]", "[email protected]"))

// or return the number of rows updated
db.update(UpdateUserEmail(4002, "[email protected]", "[email protected]"))

License

Copyright (c) 2011 Coda Hale

Published under The MIT License, see LICENSE

About

A damn simple JDBC wrapper. Y'know. For databases.

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • Scala 100.0%