Jdub for Scala 2.9.2

Jdub is a Scala wrapper over JDBC.

License

License

The MIT License
GroupId

GroupId

com.simple
ArtifactId

ArtifactId

jdub_2.9.2
Last Version

Last Version

0.4.1
Release Date

Release Date

Type

Type

jar
Description

Description

Jdub for Scala 2.9.2
Jdub is a Scala wrapper over JDBC.
Project URL

Project URL

https://github.com/SimpleFinance/jdub
Source Code Management

Source Code Management

http://github.com/SimpleFinance/jdub/

Download jdub_2.9.2

How to add to project

<!-- https://jarcasting.com/artifacts/com.simple/jdub_2.9.2/ -->
<dependency>
    <groupId>com.simple</groupId>
    <artifactId>jdub_2.9.2</artifactId>
    <version>0.4.1</version>
</dependency>
// https://jarcasting.com/artifacts/com.simple/jdub_2.9.2/
implementation 'com.simple:jdub_2.9.2:0.4.1'
// https://jarcasting.com/artifacts/com.simple/jdub_2.9.2/
implementation ("com.simple:jdub_2.9.2:0.4.1")
'com.simple:jdub_2.9.2:jar:0.4.1'
<dependency org="com.simple" name="jdub_2.9.2" rev="0.4.1">
  <artifact name="jdub_2.9.2" type="jar" />
</dependency>
@Grapes(
@Grab(group='com.simple', module='jdub_2.9.2', version='0.4.1')
)
libraryDependencies += "com.simple" % "jdub_2.9.2" % "0.4.1"
[com.simple/jdub_2.9.2 "0.4.1"]

Dependencies

compile (6)

Group / Artifact Type Version
org.scala-lang : scala-library jar 2.9.2
nl.grons : metrics-scala_2.9.2 jar 2.2.0
org.clapper : grizzled-slf4j_2.9.2 jar 0.6.10
org.apache.tomcat : tomcat-dbcp jar 7.0.42
joda-time : joda-time jar 2.3
org.joda : joda-convert jar 1.5

test (3)

Group / Artifact Type Version
com.simple : simplespec_2.9.2 jar 0.7.0
org.hsqldb : hsqldb jar 2.2.8
ch.qos.logback : logback-classic jar 1.0.13

Project Modules

There are no modules declared in this project.

jdub

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

Requirements

  • Java SE 8 or above
  • Scala 2.12.x

How To Use

First, specify Jdub as a dependency:

<dependencies>
  <dependency>
    <groupId>com.simple</groupId>
    <artifactId>jdub_${scala.major.version}</artifactId>
    <version>${jdub.version}</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:

// Query returning an optional single result.
case class GetAge(name: String) extends FlatSingleRowQuery[Int] {

  val sql = trim("""
      SELECT age /* Use C-style comments in trimmed queries */
      FROM people
      WHERE name = ?
      """)

  val values = Seq(name)

  def flatMap(row: Row) = {
    // Returns Option[Int]
    row.int(0) // 0 gets the first column
  }

}

val age = db(GetAge("Old Guy")).getOrElse(-1) // 402
// Query returning a Person object for each row.
case object GetPeople extends CollectionQuery[Seq, Person] {

  val sql = trim("""
      SELECT name, email, age
      FROM people
      """)

  val values = Seq()

  def map(row: Row) = {
    val name = row.string("name").get
    val email = row.string("email").getOrElse("")
    val age = row.int("age").getOrElse(0)
    Person(name, email, age)
  }

}

val person = db(GetPeople).head // Person(Coda Hale,[email protected],29)

You can also use the sql string interpolator:

class PersonQueries(val database: Database) {
  // Query returning an optional single result.
  def getAge(name: String): Option[Int] = database {
    sql"""
      SELECT age
      FROM people
      WHERE name = ${name}
    """.map { row =>
      row.int("age")
    }
  }.headOption
}

val personQueries = new PersonQueries(database)
val age = personQueries.getAge("Old Guy").getOrElse(-1) // 402
object PersonQueries {
  def mapper(row: Row): String = {
    Person(
      name = row.string("name").get
      email = row.string("email").getOrElse("")
      age = row.int("age").getOrElse(0)
    )
  }
}

class PersonQueries(val database: Database) {
  // Query returning Person objects for each row.
  def getPeople(): Seq[Person] = database {
    sql"""
      SELECT name, email, age
      FROM people
    """.map(PersonQueries.mapper)
  }
}

val personQueries = new PersonQueries(database)
val people = personQueries.getPeople.head // Person(Coda Hale,[email protected],29)

Fourth, execute some statements:

case class UpdateEmail(name: String, newEmail: String) extends Statement {
  val sql = trim("""
      UPDATE people
      SET email = ?
      WHERE name = ?
      """)
  val values = Seq(newEmail, name)
}

// Execute the statement.
db.execute(UpdateEmail("Old Guy", "[email protected]"))

// Or return the number of rows updated.
val count = db.update(UpdateEmail("Old Guy", "[email protected]")) // 1

You can also use the sql string interpolator:

object PersonQueries {
  def mapper(row: Row): String = {
    Person(
      name = row.string("name").get
      email = row.string("email").getOrElse("")
      age = row.int("age").getOrElse(0)
    )
  }
}

class PersonQueries(val database: Database) {
  // Update a Person's email
  def updateEmail(name: String, newEmail: String): Option[Person] = database {
    sql"""
      UPDATE people
      SET email = ${newEmail}
      WHERE name = ${name}
      RETURNING people.*
    """.map(PersonQueries.mapper)
  }.headOption
}


val personQueries = new PersonQueries(database)
val updatedPerson = personQueries.updateEmail("Old Guy", "[email protected]")

Fifth, read up on all the details in the Jdub tour.

License

Copyright (c) 2011-2012 Coda Hale Copyright (c) 2012-2017 Simple Finance Technology Corp. All rights reserved.

Published under The MIT License, see LICENSE.md

com.simple

Simple

Versions

Version
0.4.1
0.4.0
0.3.3
0.3.2
0.3.1
0.3.0
0.2.0
0.1.0