From 7f5092c3963c4b9a889be8ed7fdd54a7553eb05f Mon Sep 17 00:00:00 2001 From: jilen Date: Thu, 19 Dec 2024 12:36:44 +0800 Subject: [PATCH] add mirror context --- .../scala/minisql/context/MirrorContext.scala | 15 ++++++++ src/main/scala/minisql/context/mirror.scala | 35 +++++++++++++++++++ 2 files changed, 50 insertions(+) create mode 100644 src/main/scala/minisql/context/MirrorContext.scala create mode 100644 src/main/scala/minisql/context/mirror.scala diff --git a/src/main/scala/minisql/context/MirrorContext.scala b/src/main/scala/minisql/context/MirrorContext.scala new file mode 100644 index 0000000..c1be053 --- /dev/null +++ b/src/main/scala/minisql/context/MirrorContext.scala @@ -0,0 +1,15 @@ +package minisql + +import minisql.context.mirror.* + +class MirrorContext[Idiom <: idiom.Idiom, Naming <: NamingStrategy]( + val idiom: Idiom, + val naming: Naming +) extends context.Context[Idiom, Naming] { + + type DBRow = Row + + type DBResultSet = Iterable[DBRow] + + type DBStatement = IArray[Any] +} diff --git a/src/main/scala/minisql/context/mirror.scala b/src/main/scala/minisql/context/mirror.scala new file mode 100644 index 0000000..cd3b725 --- /dev/null +++ b/src/main/scala/minisql/context/mirror.scala @@ -0,0 +1,35 @@ +package minisql.context.mirror + +import minisql.{MirrorContext, NamingStrategy} +import minisql.idiom.Idiom +import minisql.util.Messages.fail +import scala.reflect.ClassTag + +/** +* No extra class defined +*/ +opaque type Row = IArray[Any] *: EmptyTuple + +extension (r: Row) { + + def data: IArray[Any] = r._1 + + def add(value: Any): Row = (r.data :+ value) *: EmptyTuple + + def apply[T](idx: Int)(using t: ClassTag[T]): T = { + r.data(idx) match { + case v: T => v + case other => + fail( + s"Invalid column type. Expected '${t.runtimeClass}', but got '$other'" + ) + } + } +} + +trait MirrorCodecs[I <: Idiom, N <: NamingStrategy] { + this: MirrorContext[I, N] => + + given byteEncoder: Encoder[Byte] + +}