Skip to content

Commit

Permalink
More fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
deusaquilus committed Dec 5, 2024
1 parent 9934837 commit 0d13632
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ class ActionMacro(val c: MacroContext) extends ContextMacro with ReifyLiftings {
val (idiomContext, expanded) = $expanded
${c.prefix}.translateQuery(
expanded.string,
expanded.liftings,
options = ${options}
)(io.getquill.context.ExecutionInfo.unknown, ())
"""
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ trait ContextVerbTranslate extends ContextTranslateMacro {
case class TranslateOptions(
prettyPrint: Boolean = false,
plugLifts: Boolean = true,
demarcateLifts: Boolean = false
demarcatePluggedLifts: Boolean = true
)

trait ContextTranslateMacro extends ContextTranslateProto {
Expand Down Expand Up @@ -67,25 +67,34 @@ trait ContextTranslateProto {
statement: String,
liftings: List[ScalarLift] = List(),
options: TranslateOptions = TranslateOptions()
)(executionInfo: ExecutionInfo, dc: Runner): String =
(liftings.nonEmpty, options.plugLifts) match {
case (true, true) =>
liftings.foldLeft(statement) { case (expanded, lift) =>
expanded.replaceFirst("\\?", if (options.demarcateLifts) s"prep(${lift.value})" else s"${lift.value}")
}
case (true, false) =>
var varNum: Int = 0
val dol = '$'
val numberedQuery =
)(executionInfo: ExecutionInfo, dc: Runner): String = {
def quoteIfNeeded(value: Any): String =
value match {
case _: String => s"'${value}'"
case _: Char => s"'${value}'"
case _ => s"${value}"
}

if (liftings.isEmpty)
statement
else
options.plugLifts match {
case true =>
liftings.foldLeft(statement) { case (expanded, lift) =>
val res = expanded.replaceFirst("\\?", s"${dol}${varNum}")
varNum += 1
res
expanded.replaceFirst("\\?", if (options.demarcatePluggedLifts) s"lift(${quoteIfNeeded(lift.value)})" else quoteIfNeeded(lift.value))
}
numberedQuery + "\n" + liftings.map(lift => s"${dol} = ${lift.value}").mkString("\n")
case _ =>
statement
}
case false =>
var varNum: Int = 0
val dol = '$'
val numberedQuery =
liftings.foldLeft(statement) { case (expanded, lift) =>
val res = expanded.replaceFirst("\\?", s"${dol}${varNum + 1}")
varNum += 1
res
}
numberedQuery + "\n" + liftings.zipWithIndex.map { case (lift, i) => s"${dol}${i + 1} = ${lift.value}" }.mkString("\n")
}
}

def translateBatchQuery(
// TODO these groups need to have liftings lists
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -119,27 +119,27 @@ class ContextMacroSpec extends Spec {
qr1.filter(t => t.s == lift("a")).delete
}
testContext.translate(q) mustEqual
"""querySchema("TestEntity").filter(t => t.s == 'a').delete"""
"""querySchema("TestEntity").filter(t => t.s == lift('a')).delete"""
}
"sql" in {
val q = quote {
sql"t = ${lift("a")}".as[Action[TestEntity]]
}
testContext.translate(q) mustEqual s"""sql"t = $${'a'}""""
testContext.translate(q) mustEqual s"""sql"t = $${lift('a')}""""
}
"dynamic" in {
val q = quote {
sql"t = ${lift("a")}".as[Action[TestEntity]]
}
testContext.translate(q.dynamic) mustEqual s"""sql"t = $${'a'}""""
testContext.translate(q.dynamic) mustEqual s"""sql"t = $${lift('a')}""""
}
"dynamic type param" in {
import language.reflectiveCalls
def test[T <: { def i: Int }: SchemaMeta] = quote {
query[T].filter(t => t.i == lift(1)).delete
}
testContext.translate(test[TestEntity]) mustEqual
"""querySchema("TestEntity").filter(t => t.i == 1).delete"""
"""querySchema("TestEntity").filter(t => t.i == lift(1)).delete"""
}
}
}
Expand Down

0 comments on commit 0d13632

Please sign in to comment.