Skip to content

Instantly share code, notes, and snippets.

@swoogles
Created October 20, 2014 20:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save swoogles/7e61a1015b64d1e52658 to your computer and use it in GitHub Desktop.
Save swoogles/7e61a1015b64d1e52658 to your computer and use it in GitHub Desktop.
package models
import anorm.SQL
import anorm.SqlStringInterpolation
import java.sql.Connection
import anorm.RowParser
import anorm.ResultSetParser
trait Findable[T] {
val table: String
val pk: String
val parser: RowParser[T]
def findById(id: Int)(implicit connection:Connection): Option[T] = {
val query = s"SELECT * FROM $table WHERE $pk = $id"
SQL(query).as(parser.singleOpt)
}
lazy val multiParser: ResultSetParser[List[T]] = {
parser *
}
}
trait FindableBySubscriber[T] extends Findable[T]{
def findBySubscriber(id: Int)(implicit connection:Connection): List[T] = {
val query = s"SELECT * FROM $table WHERE subscriber_idx = $id"
SQL(query).as(multiParser)
}
}
trait FindableByPatron[T] extends Findable[T]{
def findByPatron(id: Int)(implicit connection:Connection): Option[T] = {
val query = s"SELECT * FROM $table WHERE user_idx = $id"
SQL(query).as(parser.singleOpt)
}
}
trait SRModel[T]
extends Findable[T]
with FindableBySubscriber[T]
with FindableByPatron[T]
{
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment