Allow both decoder and encoder

This commit is contained in:
jilen 2025-06-17 19:52:30 +08:00
parent cb0c6082d0
commit 63a9a0cad3
3 changed files with 21 additions and 3 deletions

View file

@ -18,6 +18,7 @@ opaque type EntityQuery[E] <: Query[E] = Query[E]
object EntityQuery {
extension [E](inline e: EntityQuery[E]) {
inline def map[E1](inline f: E => E1): EntityQuery[E1] = {
transform(e)(f)(Map.apply)
}
@ -25,6 +26,7 @@ object EntityQuery {
inline def filter(inline f: E => Boolean): EntityQuery[E] = {
transform(e)(f)(Filter.apply)
}
}
}
@ -90,6 +92,7 @@ private def compileImpl[I <: Idiom, N <: NamingStrategy](
.getOrElse(report.errorAndAbort(s"NamingStrategy not known at compile"))
val stmt = idiom.translate(ast)(using naming)
report.info(s"Static Query: ${stmt}")
Expr(stmt._2)
case None =>
report.info("Dynamic Query")

View file

@ -15,6 +15,14 @@ trait RowExtract[A, Row] {
object RowExtract {
private[context] def single[Row, E](
decoder: ColumnDecoder.Aux[Row, E]
): RowExtract[E, Row] = new RowExtract[E, Row] {
def extract(row: Row): Try[E] = {
decoder.decode(row, 0)
}
}
private def extractorImpl[A, Row](
decoders: IArray[Any],
m: Mirror.ProductOf[A]
@ -83,14 +91,21 @@ trait Context[I <: Idiom, N <: NamingStrategy] { selft =>
inline def io[E](
inline q: minisql.Query[E]
)(using r: RowExtract[E, DBRow]): DBIO[IArray[E]] = {
): DBIO[IArray[E]] = {
val extractor = summonFrom {
case e: RowExtract[E, DBRow] => e
case e: ColumnDecoder.Aux[DBRow, E] =>
RowExtract.single(e)
}
val lifts = q.liftMap
val stmt = minisql.compile(q, idiom, naming)
val (sql, params) = stmt.expand(lifts)
(
sql = sql,
params = params.map(_.value.get.asInstanceOf),
mapper = (rows) => rows.traverse(r.extract)
mapper = (rows) => rows.traverse(extractor.extract)
)
}

View file

@ -304,7 +304,7 @@ trait MirrorIdiomBase extends Idiom {
Tokenizer[OnConflict.Target] {
case OnConflict.NoTarget => stmt""
case OnConflict.Properties(props) =>
val listTokens = listTokenizer(astTokenizer).token(props)
val listTokens = listTokenizer(using astTokenizer).token(props)
stmt"(${listTokens})"
}