From 0d13632b846f1e55eab50c4dff275c994e91ab8f Mon Sep 17 00:00:00 2001 From: Alexander Ioffe Date: Wed, 4 Dec 2024 23:34:02 -0500 Subject: [PATCH] More fixes --- .../io/getquill/context/ActionMacro.scala | 1 + .../context/ContextVerbTranslate.scala | 45 +++++++++++-------- .../getquill/context/ContextMacroSpec.scala | 8 ++-- 3 files changed, 32 insertions(+), 22 deletions(-) diff --git a/quill-core/src/main/scala/io/getquill/context/ActionMacro.scala b/quill-core/src/main/scala/io/getquill/context/ActionMacro.scala index f7acb52ae4..3937ae3397 100644 --- a/quill-core/src/main/scala/io/getquill/context/ActionMacro.scala +++ b/quill-core/src/main/scala/io/getquill/context/ActionMacro.scala @@ -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, ()) """ diff --git a/quill-core/src/main/scala/io/getquill/context/ContextVerbTranslate.scala b/quill-core/src/main/scala/io/getquill/context/ContextVerbTranslate.scala index 650a542204..85c3debc80 100644 --- a/quill-core/src/main/scala/io/getquill/context/ContextVerbTranslate.scala +++ b/quill-core/src/main/scala/io/getquill/context/ContextVerbTranslate.scala @@ -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 { @@ -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 diff --git a/quill-core/src/test/scala/io/getquill/context/ContextMacroSpec.scala b/quill-core/src/test/scala/io/getquill/context/ContextMacroSpec.scala index 305eabdf8f..7aee9e3abd 100644 --- a/quill-core/src/test/scala/io/getquill/context/ContextMacroSpec.scala +++ b/quill-core/src/test/scala/io/getquill/context/ContextMacroSpec.scala @@ -119,19 +119,19 @@ 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 @@ -139,7 +139,7 @@ class ContextMacroSpec extends Spec { 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""" } } }