diff --git a/src/main/scala/minisql/ParamEncoder.scala b/src/main/scala/minisql/ParamEncoder.scala new file mode 100644 index 0000000..a55c0a3 --- /dev/null +++ b/src/main/scala/minisql/ParamEncoder.scala @@ -0,0 +1,8 @@ +package minisql + +trait ParamEncoder[E] { + + type Stmt + + def setParam(s: Stmt, idx: Int, v: E): Unit +} diff --git a/src/main/scala/minisql/Parser.scala b/src/main/scala/minisql/Parser.scala index 2d797c5..3b21d90 100644 --- a/src/main/scala/minisql/Parser.scala +++ b/src/main/scala/minisql/Parser.scala @@ -6,8 +6,8 @@ import scala.quoted.* type Parser[O <: Ast] = PartialFunction[Expr[?], Expr[O]] -private[minisql] inline def parseParamAt[A, B]( - inline f: A => B, +private[minisql] inline def parseParamAt[F]( + inline f: F, inline n: Int ): ast.Ident = ${ parseParamAt('f, 'n) diff --git a/src/main/scala/minisql/dsl.scala b/src/main/scala/minisql/dsl.scala index 64346db..76b6d09 100644 --- a/src/main/scala/minisql/dsl.scala +++ b/src/main/scala/minisql/dsl.scala @@ -1,13 +1,16 @@ package minisql.dsl +import minisql.* import minisql.parsing import minisql.ast.{Ast, Entity, Map, Property, Ident, given} import scala.quoted.* import scala.compiletime.* import scala.compiletime.ops.string.* +import scala.collection.immutable.{Map => IMap} sealed trait Dsl { def ast: Ast + def lifts: IMap[String, (Any, ParamEncoder[?])] = IMap.empty } trait Query[E] extends Dsl @@ -21,7 +24,13 @@ extension [E](inline e: EntityQuery[E]) { } extension [A, B](inline f1: A => B) { - private inline def param0 = parsing.parseParamAt[A, B](f1, 0) + private inline def param0 = parsing.parseParamAt(f1, 0) + private inline def body = parsing.parseBody(f1) +} + +extension [A1, A2, B](inline f1: (A1, A2) => B) { + private inline def param0 = parsing.parseParamAt(f1, 0) + private inline def param1 = parsing.parseParamAt(f1, 1) private inline def body = parsing.parseBody(f1) } @@ -31,10 +40,13 @@ private inline def transform[D1 <: Dsl, D2 <: Dsl, A, B](inline ast: Ast)( f2(fast(ast, f.param0, f.body)) } -given FromExpr[EntityQuery[?]] with { +private given FromExpr[EntityQuery[?]] with { def unapply(x: Expr[EntityQuery[?]])(using Quotes): Option[EntityQuery[?]] = { x match { - case '{ val x: Ast = ${ Expr(ast) }; EntityQuery(x) } => + case '{ + val x: Ast = ${ Expr(ast) } + EntityQuery(x) + } => Some(EntityQuery(ast)) case '{ EntityQuery(${ Expr(ast) }) } => Some(EntityQuery(ast)) @@ -45,7 +57,7 @@ given FromExpr[EntityQuery[?]] with { } } } -given FromExpr[Dsl] with { +private given FromExpr[Dsl] with { def unapply(x: Expr[Dsl])(using Quotes): Option[Dsl] = { import quotes.reflect.* x match {