Jdub for Scala 2.11.8

Jdub is a Scala wrapper over JDBC.

License

License

The MIT License
GroupId

GroupId

com.simple
ArtifactId

ArtifactId

jdub_2.11
Last Version

Last Version

1.5.0
Release Date

Release Date

Type

Type

jar
Description

Description

Jdub for Scala 2.11.8
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.11

How to add to project

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

Dependencies

compile (6)

Group / Artifact Type Version
io.dropwizard.metrics : metrics-core jar 3.1.2
io.dropwizard.metrics : metrics-healthchecks jar 3.1.2
org.clapper : grizzled-slf4j_2.11 jar 1.3.0
com.zaxxer : HikariCP jar 2.6.1
joda-time : joda-time jar 2.9.9
org.joda : joda-convert jar 1.8.1

test (4)

Group / Artifact Type Version
org.scalatest : scalatest_2.11 jar 3.0.1
org.mockito : mockito-all jar 1.10.19
org.hsqldb : hsqldb jar 2.3.4
ch.qos.logback : logback-classic jar 1.2.3

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
1.5.0
1.4.0
1.3.4
1.3.3
1.3.2
1.3.1
1.3.0
1.1.1
1.1.0
1.0.0
0.11.0
0.10.0
0.9.0