Created
February 21, 2018 18:52
-
-
Save pschichtel/bf4dc99b35510d358d20c7077fd37d41 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
object SqlBuilderPoC { | |
class ResultRow | |
trait Parser[T] { | |
def parse(row: ResultRow): T | |
} | |
trait Record[T] | |
trait Table[P, T, R <: Record[T]] { | |
def build(t: T): R | |
def parser: Parser[T] | |
def all: Seq[Field[_]] | |
} | |
case class Field[T](name: String, t: FieldType[T]) | |
case class FieldType[T](dbType: String, convert: Any => T) | |
case class Session(id: Int, sessId: String) extends Record[(Int, String)] | |
object SessionTable extends Table[Int, (Int, String), Session] { | |
val id = Field("id", FieldType("INT", _.toString.toInt)) | |
val sessionId = Field("session_id", FieldType("VARCHAR(100)", _.toString)) | |
override def build(t: (Int, String)): Session = (Session.apply _).tupled(t) | |
override def parser: Parser[(Int, String)] = (row: ResultRow) => ??? | |
override def all: Seq[Field[_]] = Seq(id, sessionId) | |
} | |
def select[T, R <: Record[T]](field: Field[T], table: Table[_, T, R]): R = ??? | |
def select[T1, T2, T: (T1, T2), R <: Record[T]](field1: Field[T1], field2: Field[T2], table: Table[_, T, R]): R = ??? | |
def select[T1, T2, T3, T: (T1, T2, T3), R <: Record[T]](field1: Field[T1], field2: Field[T2], field3: Field[T3], table: Table[_, T, R]): R = ??? | |
def select[T, R <: Record[T]](table: Table[_, T, R]): Seq[R] = { | |
table.all // TODO make query | |
val sql = "SELECT *" | |
val result: Seq[ResultRow] = Nil | |
result | |
.map(table.parser.parse) | |
.map(table.build) | |
} | |
val a = select(SessionTable) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment