com.github.njeuk.dbmapper

DbAsync

object DbAsync

DbAsync is the main entry point for executing queries.

All queries are executed asynchronously

Example:

import scala.async.Async._
import com.github.njeuk.dbmapper.Implicits._

implicit def rowToBook: RowData => Book = (r) => DbCodeGenerator.rowToClass[Book](r)
implicit def rowToAuthor: RowData => Author = (r) => DbCodeGenerator.rowToClass[Author](r)

def getBooks(authorName: String): Future[List[Book]] {
async {
  val author = await(DbAsync.execOne[Author](q"select * author where name = $authorName"))
  DbAsync.exec[Book](q"select * from book where author_id = ${author.authorId}")
}

The public methods expect two implicit parameters: param f This is a function to convert from the returned database row, of type RowData to the entity object T. DbCodeGenerator.rowToClass will generate a function at compile time using a Scala Macros. Or you can hand craft the function. param config This contains the configuration used by dbmapper to connect to the database, and details of what to log. import com.github.njeuk.dbmapper.Implicits._ will place a default config in scope, which reads the db connection string from the application.conf settings. Or you can create your own specific settings as can be seen in the sample's attached to this project

Linear Supertypes
AnyRef, Any
Ordering
  1. Alphabetic
  2. By inheritance
Inherited
  1. DbAsync
  2. AnyRef
  3. Any
  1. Hide All
  2. Show all
Learn more about member selection
Visibility
  1. Public
  2. All

Value Members

  1. final def !=(arg0: Any): Boolean

    Definition Classes
    AnyRef → Any
  2. final def ##(): Int

    Definition Classes
    AnyRef → Any
  3. final def ==(arg0: Any): Boolean

    Definition Classes
    AnyRef → Any
  4. final def asInstanceOf[T0]: T0

    Definition Classes
    Any
  5. def clone(): AnyRef

    Attributes
    protected[java.lang]
    Definition Classes
    AnyRef
    Annotations
    @throws( ... )
  6. final def eq(arg0: AnyRef): Boolean

    Definition Classes
    AnyRef
  7. def equals(arg0: Any): Boolean

    Definition Classes
    AnyRef → Any
  8. def exec[T](q: SqlAndArgs)(implicit f: (RowData) ⇒ T, config: DbAsyncConfig): Future[List[T]]

    Returns a list of entity objects (T's) created from executing the sql asynchronously

    Returns a list of entity objects (T's) created from executing the sql asynchronously

    q

    the sql and arguments to execute, formed from SqlInterpolation

    f

    implicit function to transform the RowData to a T

    config

    implicit configuration settings

    Example:

    val price = 33.99
    val books = exec[Book](q"select * from book where retail_price > $price")
    val aBook = execOne[Book](q"select * from '$table where '$column = $price")
    val prices = List(33.99, 14.52)
    val moreBooks = exec[Book](q"select * from book where retail_price in ($prices) order by book_id")
  9. def exec[T](sql: String, values: Seq[Any] = List())(implicit f: (RowData) ⇒ T, config: DbAsyncConfig): Future[List[T]]

    Returns a list of T's created from executing the sql asynchronously

    Returns a list of T's created from executing the sql asynchronously

    sql

    the sql to execute

    values

    the arguments for the query

    f

    implicit function to transform the RowData to a T

    config

    implicit configuration settings

    Example:

    val books = exec[Book]("select * from book")
    val allTitles = DbAsync.exec[String]("select title from book order by book_id")(r => r(0).toString, dbAsyncConfig)
  10. def execNonQuery(q: SqlAndArgs)(implicit config: DbAsyncConfig): Future[QueryResult]

    Version of execNonQuery which takes a query interpolation generated query

    Version of execNonQuery which takes a query interpolation generated query

    Example:

    val id = 123
    execNonQuery[Book](q"delete from book where book_id = $id")
  11. def execNonQuery(sql: String, values: Seq[Any] = List())(implicit config: DbAsyncConfig): Future[QueryResult]

    Asynchronously executes query, no results are expected

    Asynchronously executes query, no results are expected

    A Future[QueryResult] is returned, this is so that you can wait on the completion of the query if required.

    sql

    the sql to execute

    values

    the arguments for the query

    config

    implicit configuration settings

    returns

    returns a QueryResult, don't expect callers will use this apart from to wait on the result if they need to wait until the query has executed

    Example:

    execNonQuery[Book]("delete from book where book_id = 123")
    
    // example of stalling until query executes
    async {
      await(execNonQuery[Book]("delete from book where book_id = 123"))
      // code here won't continue until delete has been executed
    }
  12. def execOne[T](q: SqlAndArgs)(implicit f: (RowData) ⇒ T, config: DbAsyncConfig): Future[T]

    Version of execOne which expects a query generated from the query interpolation

    Version of execOne which expects a query generated from the query interpolation

    Example:

    val bookId = 123
    val noBookId = -1
    val book = execOne[Book](q"select * from book where book_id = $bookId") // Book
    val book = execOne[Book](q"select * from book where book_id = $noBookId") // Exception -- no result
    val book = execOne[Book](q"select * from book") // Exception -- too many results
  13. def execOne[T](sql: String, values: Seq[Any] = List())(implicit f: (RowData) ⇒ T, config: DbAsyncConfig): Future[T]

    Asynchronously returns T containing single result from the query

    Asynchronously returns T containing single result from the query

    If more than one result or no result then throws exception.

    sql

    the sql to execute

    values

    the arguments for the query

    f

    implicit function to transform the RowData to a T

    config

    implicit configuration settings

    Example:

    val book = execOne[Book]("select * from book where book_id = 123") // Book
    val book = execOne[Book]("select * from book where book_id = -1") // Exception -- no result
    val book = execOne[Book]("select * from book") // Exception -- too many results
  14. def execOneOrNone[T](q: SqlAndArgs)(implicit f: (RowData) ⇒ T, config: DbAsyncConfig): Future[Option[T]]

    Returns one or zero results from executing the query.

    Returns one or zero results from executing the query. Designed to be called from a queryInterpolated string

    T

    type of the entity objects expected to be returned

    q

    result from a querty Interpolated string i.e. q"some query"

    f

    implicit function to convert sql RowData into the entity object T

    config

    implicit configuration settings

    returns

    Some(Entity Object) or None -- throws exception if more than one row is found

    Example:

    val price = 33.44
    val book = execOneOrNone[Book](q"select * from book where retail_price = $price") // Some()
    val book = execOneOrNone[Book](q"select * from book where book_id = -1") // None
    val book = execOneOrNone[Book](q"select * from book") // Exception -- too many results
  15. def execOneOrNone[T](sql: String, values: Seq[Any] = List())(implicit f: (RowData) ⇒ T, config: DbAsyncConfig): Future[Option[T]]

    Asynchronously returns Option[T] containing Some single result from the query or None if there were no results.

    Asynchronously returns Option[T] containing Some single result from the query or None if there were no results.

    If more than one result then throws exception.

    T

    type of the entity objects expected to be returned

    sql

    the sql to execute

    values

    the arguments for the query

    f

    implicit function to convert sql RowData into the entity object T

    config

    implicit configuration settings

    returns

    Some(Entity Object) or None -- throws exception if more than one row is found

    Example:

    val book = execOneOrNone[Book]("select * from book where book_id = 123") // Some()
    val book = execOneOrNone[Book]("select * from book where book_id = -1") // None
    val book = execOneOrNone[Book]("select * from book") // Exception -- too many results
  16. var factory: Option[PostgreSQLConnectionFactory]

  17. def finalize(): Unit

    Attributes
    protected[java.lang]
    Definition Classes
    AnyRef
    Annotations
    @throws( classOf[java.lang.Throwable] )
  18. final def getClass(): Class[_]

    Definition Classes
    AnyRef → Any
  19. def hashCode(): Int

    Definition Classes
    AnyRef → Any
  20. final def isInstanceOf[T0]: Boolean

    Definition Classes
    Any
  21. final def ne(arg0: AnyRef): Boolean

    Definition Classes
    AnyRef
  22. final def notify(): Unit

    Definition Classes
    AnyRef
  23. final def notifyAll(): Unit

    Definition Classes
    AnyRef
  24. var pool: Option[ConnectionPool[_]]

  25. def shutdown: Unit

    Explicitly close down the database connections, releasing any resources.

    Explicitly close down the database connections, releasing any resources.

    Not a big deal to do, will happen automatically in most instances when app closes.

  26. final def synchronized[T0](arg0: ⇒ T0): T0

    Definition Classes
    AnyRef
  27. def toString(): String

    Definition Classes
    AnyRef → Any
  28. final def wait(): Unit

    Definition Classes
    AnyRef
    Annotations
    @throws( ... )
  29. final def wait(arg0: Long, arg1: Int): Unit

    Definition Classes
    AnyRef
    Annotations
    @throws( ... )
  30. final def wait(arg0: Long): Unit

    Definition Classes
    AnyRef
    Annotations
    @throws( ... )

Inherited from AnyRef

Inherited from Any

Ungrouped