com.github.njeuk.dbmapper

TableAccess

abstract class TableAccess[T] extends AnyRef

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

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

Instance Constructors

  1. new TableAccess(codeToSql: () ⇒ CodeToSql, rowToClass: (RowData) ⇒ T, updateSql: (T) ⇒ (String, Seq[Any]), insertSql: (T) ⇒ (String, Seq[Any]), identityInsertSql: (T) ⇒ (String, Seq[Any]), copy: (T, Int) ⇒ T)

    codeToSql

    an instance of the CodeToSql class, providing information about T to guide the SQL construction

    rowToClass

    a data mapper to convert the RowData to the entity object (T)

    updateSql

    the sql used for updating the table

    insertSql

    the sql used for inserting new rows in the table, using the default identity (usually a primary key) (usually a generated from a postgres Sequence)

    identityInsertSql

    the sql needed to insert rows when the inserted object is providing the identity

    copy

    a function to produce a copy of the Entity (T) with the identity replaced

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. val codeToSql: () ⇒ CodeToSql

    an instance of the CodeToSql class, providing information about T to guide the SQL construction

  7. val copy: (T, Int) ⇒ T

    a function to produce a copy of the Entity (T) with the identity replaced

  8. def delete(id: Int)(implicit config: DbAsyncConfig): Future[QueryResult]

    Deletes the entity with the specified id

    Deletes the entity with the specified id

    id

    the identity of the row containing the Entity (T)

    config

    implicit configuration to use

    returns

    returns a QueryResult, but really it is expected the result is used just to ensure the delete has been performed before continuing

    Example:

    async {
    await(superHeroAccess.delete(1))
    // ignoring return, but code after this point will be continued once delete is performed
    }
  9. final def eq(arg0: AnyRef): Boolean

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

    Definition Classes
    AnyRef → Any
  11. def finalize(): Unit

    Attributes
    protected[java.lang]
    Definition Classes
    AnyRef
    Annotations
    @throws( classOf[java.lang.Throwable] )
  12. def get(id: Int)(implicit config: DbAsyncConfig): Future[Option[T]]

    Gets the Entity (T) with the specified id.

    Gets the Entity (T) with the specified id. If it doesn't exist None is returned.

    id

    the identity of the row containing the Entity (T)

    config

    implicit configuration to use

    returns

    returns Some(Entity) or None if it doesn't exist

    Example:

    async {
    val hero = await(superHeroAccess.get(2))
    println hero.get.name
    }
  13. final def getClass(): Class[_]

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

    Definition Classes
    AnyRef → Any
  15. def identityInsert(u: T)(implicit config: DbAsyncConfig): Future[T]

    Inserts the entity into the database, where a specific value for the identity column is provided.

    Inserts the entity into the database, where a specific value for the identity column is provided.

    u

    the entity to be updated, the id column must not exist in the database yet

    config

    info for configuration

    returns

    the inserted entity. The inserted row isn't retrieved from the database, so any db calculated values won't have changed

    Example:

    val wonderWoman = SuperHero(666, "Wonder Woman", true)
    superHeroAccess.identityInsert(wonderWoman)
  16. val identityInsertSql: (T) ⇒ (String, Seq[Any])

    the sql needed to insert rows when the inserted object is providing the identity

  17. def insert(u: T)(implicit config: DbAsyncConfig): Future[T]

    Inserts the entity into the database.

    Inserts the entity into the database. This uses the database default for the id column, which is typically auto generated from some Postgres Sequence.

    u

    the entity to be updated, the value in the id is ignored

    config

    the implicit configuration info

    returns

    the entity with its new id. The inserted row isn't retrieved from the database, so any db calculated values (apart from the identity) won't be changed

    Example:

    async {
    val catwoman = SuperHero(0, "Catwoman")
    val insertedCatwoman = await( superHeroAccess.insert(catwoman) )
    val loadedCatwoman = await( superHeroAccess.load(insertedCatwoman.superHeroId ))
    }
  18. val insertSql: (T) ⇒ (String, Seq[Any])

    the sql used for inserting new rows in the table, using the default identity (usually a primary key) (usually a generated from a postgres Sequence)

  19. final def isInstanceOf[T0]: Boolean

    Definition Classes
    Any
  20. def load(id: Int)(implicit config: DbAsyncConfig): Future[T]

    Loads the Entity from the underlying table.

    Loads the Entity from the underlying table. The entity is expected to exist, if not throws an exception.

    id

    the identity of the row containing the Entity (T)

    config

    implicit configuration to use

    returns

    the Entity

    Example:

    async {
    val hero = await(superHeroAccess.load(2))
    println hero.name
    
    val BANG = await(superHeroAccess.load(-123)) // would throw exception assuming not -123 exists!
    }
  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. implicit def rowDataToTypeImplicit(r: RowData): T

  25. val rowToClass: (RowData) ⇒ T

    a data mapper to convert the RowData to the entity object (T)

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

    Definition Classes
    AnyRef
  27. def toString(): String

    Definition Classes
    AnyRef → Any
  28. def update(u: T)(implicit config: DbAsyncConfig): Future[T]

    Updates the specified entity

    Updates the specified entity

    u

    the entity to be updated

    config

    implicit configuration info

    returns

    the update entity, this isn't retrieved from the database, so any db calculated columns won't have been updated

    Example:

    async {
    val hero = await( superHeroAccess.load(1) )
    superHeroAccess.update( hero.copy(wearsTights = true) )
    }
  29. val updateSql: (T) ⇒ (String, Seq[Any])

    the sql used for updating the table

  30. final def wait(): Unit

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

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

    Definition Classes
    AnyRef
    Annotations
    @throws( ... )

Inherited from AnyRef

Inherited from Any

Ungrouped