com.github.njeuk

dbmapper

package dbmapper

Visibility
  1. Public
  2. All

Type Members

  1. case class ColumnName(name: String) extends Annotation with StaticAnnotation with Product with Serializable

    Specifies that the column name that represents the annotated variable.

    Specifies that the column name that represents the annotated variable. Used when the default Scala identifier to database column name conversion is not correct for your table

    name

    the column name to use in the database for this variable

    Example:

    // the identifier 'name' will be mapped to the column 'tag' in the book table
    
    case class Book (
      bookId: Int
      \@(ColumnName @field)("tag") name: String
    )
  2. case class DbAsyncConfig(configuration: Configuration, logQueriesLongerThan: Duration = Duration(500, MILLISECONDS)) extends Product with Serializable

    Configuration information needed to access the database, and control logging.

    Configuration information needed to access the database, and control logging.

    Usually passed implicitly into dbmapper.

    To setup a default implicit DbAsyncConfig add 'import com.github.njeuk.dbmapper.Implicits._'.

    This will access the database connection settings from the application.conf properties in the style of the Play Framework.

    Or you can specify the connection information directly, for example

    implicit val dbAsyncConfig = DbAsyncConfig(URLParser.parse("jdbc:postgresql://localhost/dbmappersamples?user=test&password=test"), Duration("0 ms"))
    configuration

    Configuration information used to setup the db connections

    logQueriesLongerThan

    Any queries which take longer than the specified duration will be logged. Use to identify slow queries.

    Annotations
    @implicitNotFound( ... )
  3. case class Identity() extends Annotation with StaticAnnotation with Product with Serializable

    Specifies that the value is the identifier for the entity.

    Specifies that the value is the identifier for the entity.

    Use when the Scala to Database identifier conversion routine isn't correct for your schema.

    Example:

    // the table 'book' in the database will have a primary key 'book_reference' in the database
    
    case class Book (
      @(Identity @field) bookReference: Int
      name: String
    )

    Only one identity can be specified, compound keys are not supported.

    The key must be an Int, although this restriction maybe lifted one day.

    Really, we expect the schema to have integer surrogate keys on all tables, and although not text book database design, your life will be easier even if you don't use dbmapper...

  4. case class SqlAndArgs(sql: String, args: Seq[Any]) extends Product with Serializable

  5. abstract class TableAccess[T] extends AnyRef

    TableAccess lets you easily implement a 'Table Data Gateway' as per Fowler PoEAA

    TableAccess lets you easily implement a 'Table Data Gateway' as per Fowler PoEAA

    Example:

    class SuperHeroAccess extends TableAccess[SuperHero](
      () => DbCodeGenerator.codeToSql[SuperHero](),
     (r) => DbCodeGenerator.rowToClass[SuperHero](r),
     (u) => DbCodeGenerator.updateSql[SuperHero](u),
     (i) => DbCodeGenerator.insertSql[SuperHero](i),
     (i) => DbCodeGenerator.identityInsertSql[SuperHero](i),
     (t, i) => t.copy(superHeroId = i)) {
    
      def getViaName(name: String)(implicit config: DbAsyncConfig): Future[Option[SuperHero]] = {
      DbAsync.execOneOrNone[SuperHero](q"select * from super_hero where name = $name")
    }
    
    case class SuperHero(
      superHeroId: Int = 0,
      name: String = "",
      wearsTights: Boolean = false,
      partner: Option[String] = None
    )

    The above code creates a Table Data Gateway to access the SuperHeroes.

    Each SuperHero is represented by a row in the table 'super_hero'.

    The DbCodeGenerator is a Scala Macro based class which builds data mappers based on the names of the Entity object SuperHero. The generated code can be replaced by hand crafted SQL where needed.

    Once you have the SuperHeroAccess you can used standard CRUD activities on the SuperHeroes. For example:

    async {
    val hero = await( superHeroAccess.load(1) )
    superHeroAccess.update( hero.copy(wearsTights = true) )
    }

    for a full example, see the sample in the test package of this project.

    Using a Table Data Gateway is great for testing, it makes it very simple to mock out the database access.

    T

    the type of the Entity object the TableAccess is providing access to

  6. case class TableName(name: String) extends Annotation with StaticAnnotation with Product with Serializable

    Specifies that the TableName that represents the Scala Class (or entity).

    Specifies that the TableName that represents the Scala Class (or entity). Used when the default Scala identifier to database table name conversion is not correct for you schema.

    name

    the name of the table in the database

    Example:

    // the entity 'Book' will be named 'novel' in the database
    
    \@TableName("novel")
    case class Book (
      bookId: Int
      name: String
    )

Value Members

  1. object ColumnConversion

    ColumnConversion provides implicit conversions from the types in the RowData column from the database to the associated Scala type.

    ColumnConversion provides implicit conversions from the types in the RowData column from the database to the associated Scala type.

    In most cases RowData contains reasonable types for the various Postgres types.

    They are documented in com.github.mauricio.async.db.postgresql.column.PostgreSQLColumnDecoderRegistry

    If the type isn't know, for example the Postgres money type, then it will be represented as a string in the RowData column. If you wanted to support money as a type, and have it converted to a BigDecimal then you would need to implement that conversion here. (It would be tricky to do, the Postgres money type converted to a string is quite variable depending on the currency)

  2. object Config

    Access config information which may or may not exist.

  3. object DbAsync

    DbAsync is the main entry point for executing queries.

    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

  4. object DbAsyncConfig extends Serializable

  5. object Implicits

    Implicits provides a simple way to bring various implicit information that dbmapper needs into scope.

  6. object RowDataExtension

    RowDataExtension is used by the macro generated sql code.

    RowDataExtension is used by the macro generated sql code.

    It allows the generated code to easily extract values from the RowData columns.

    If a Option is expected then we check for a null value in the column before lifting it into the Option.

  7. object RowDataStandardImplicitConversions

    RowDataStandardImplicitConversions provides convenience conversions to extract data from RowData.

    RowDataStandardImplicitConversions provides convenience conversions to extract data from RowData.

    Example:

    import com.github.njeuk.dbmapper.RowDataStandardImplicitConversions._
    
    // this will use the implicit rowDataToString function
    val titles = DbAsync.exec[String]("select title from book order by book_id")
    
    // or you could just specify the conversion directly
    val titles = DbAsync.exec[String]("select title from book order by book_id")(r => r(0).toString, dbAsyncConfig)
  8. object SqlInterpolation

    Provides query interpolation, similar to standard Scala string interpolation.

    Provides query interpolation, similar to standard Scala string interpolation.

    Example

    val name = "Bruce"
    val bruce = DbAsync.exec[Person](q"select * from person where name = $name")

    Lists are supported, as in:

    val names = List("Bruce", "Murray", "Charlene")
    val australians = DbAsync.exec[Person](q"select * from person where name in ($names)")

    To perform string interpolation within the query, prefix the $ with a ' as in:

    Lists are supported, as in:

    val table = "person"
    val people = DbAsync.exec[Person](q"select * from '$table")

    For longer queries you can join the strings with +, as in:

    val newZealanders = DbAsync.exec[Person](q"select * from person " +
    q"where smarter = 't' and " +
    q"good_looking = 't'");

Ungrouped