minimal doc

This commit is contained in:
jilen 2024-12-12 12:43:15 +08:00
parent de180356bc
commit c8d9fe333e
3 changed files with 26 additions and 6 deletions

View file

@ -0,0 +1,8 @@
package minisql
trait ParamEncoder[E] {
type Stmt
def setParam(s: Stmt, idx: Int, v: E): Unit
}

View file

@ -6,8 +6,8 @@ import scala.quoted.*
type Parser[O <: Ast] = PartialFunction[Expr[?], Expr[O]] type Parser[O <: Ast] = PartialFunction[Expr[?], Expr[O]]
private[minisql] inline def parseParamAt[A, B]( private[minisql] inline def parseParamAt[F](
inline f: A => B, inline f: F,
inline n: Int inline n: Int
): ast.Ident = ${ ): ast.Ident = ${
parseParamAt('f, 'n) parseParamAt('f, 'n)

View file

@ -1,13 +1,16 @@
package minisql.dsl package minisql.dsl
import minisql.*
import minisql.parsing import minisql.parsing
import minisql.ast.{Ast, Entity, Map, Property, Ident, given} import minisql.ast.{Ast, Entity, Map, Property, Ident, given}
import scala.quoted.* import scala.quoted.*
import scala.compiletime.* import scala.compiletime.*
import scala.compiletime.ops.string.* import scala.compiletime.ops.string.*
import scala.collection.immutable.{Map => IMap}
sealed trait Dsl { sealed trait Dsl {
def ast: Ast def ast: Ast
def lifts: IMap[String, (Any, ParamEncoder[?])] = IMap.empty
} }
trait Query[E] extends Dsl trait Query[E] extends Dsl
@ -21,7 +24,13 @@ extension [E](inline e: EntityQuery[E]) {
} }
extension [A, B](inline f1: A => B) { 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) 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)) 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[?]] = { def unapply(x: Expr[EntityQuery[?]])(using Quotes): Option[EntityQuery[?]] = {
x match { x match {
case '{ val x: Ast = ${ Expr(ast) }; EntityQuery(x) } => case '{
val x: Ast = ${ Expr(ast) }
EntityQuery(x)
} =>
Some(EntityQuery(ast)) Some(EntityQuery(ast))
case '{ EntityQuery(${ Expr(ast) }) } => case '{ EntityQuery(${ Expr(ast) }) } =>
Some(EntityQuery(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] = { def unapply(x: Expr[Dsl])(using Quotes): Option[Dsl] = {
import quotes.reflect.* import quotes.reflect.*
x match { x match {