Skip to content

Commit 0d13632

Browse files
committed
More fixes
1 parent 9934837 commit 0d13632

File tree

3 files changed

+32
-22
lines changed

3 files changed

+32
-22
lines changed

quill-core/src/main/scala/io/getquill/context/ActionMacro.scala

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ class ActionMacro(val c: MacroContext) extends ContextMacro with ReifyLiftings {
2626
val (idiomContext, expanded) = $expanded
2727
${c.prefix}.translateQuery(
2828
expanded.string,
29+
expanded.liftings,
2930
options = ${options}
3031
)(io.getquill.context.ExecutionInfo.unknown, ())
3132
"""

quill-core/src/main/scala/io/getquill/context/ContextVerbTranslate.scala

Lines changed: 27 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ trait ContextVerbTranslate extends ContextTranslateMacro {
2020
case class TranslateOptions(
2121
prettyPrint: Boolean = false,
2222
plugLifts: Boolean = true,
23-
demarcateLifts: Boolean = false
23+
demarcatePluggedLifts: Boolean = true
2424
)
2525

2626
trait ContextTranslateMacro extends ContextTranslateProto {
@@ -67,25 +67,34 @@ trait ContextTranslateProto {
6767
statement: String,
6868
liftings: List[ScalarLift] = List(),
6969
options: TranslateOptions = TranslateOptions()
70-
)(executionInfo: ExecutionInfo, dc: Runner): String =
71-
(liftings.nonEmpty, options.plugLifts) match {
72-
case (true, true) =>
73-
liftings.foldLeft(statement) { case (expanded, lift) =>
74-
expanded.replaceFirst("\\?", if (options.demarcateLifts) s"prep(${lift.value})" else s"${lift.value}")
75-
}
76-
case (true, false) =>
77-
var varNum: Int = 0
78-
val dol = '$'
79-
val numberedQuery =
70+
)(executionInfo: ExecutionInfo, dc: Runner): String = {
71+
def quoteIfNeeded(value: Any): String =
72+
value match {
73+
case _: String => s"'${value}'"
74+
case _: Char => s"'${value}'"
75+
case _ => s"${value}"
76+
}
77+
78+
if (liftings.isEmpty)
79+
statement
80+
else
81+
options.plugLifts match {
82+
case true =>
8083
liftings.foldLeft(statement) { case (expanded, lift) =>
81-
val res = expanded.replaceFirst("\\?", s"${dol}${varNum}")
82-
varNum += 1
83-
res
84+
expanded.replaceFirst("\\?", if (options.demarcatePluggedLifts) s"lift(${quoteIfNeeded(lift.value)})" else quoteIfNeeded(lift.value))
8485
}
85-
numberedQuery + "\n" + liftings.map(lift => s"${dol} = ${lift.value}").mkString("\n")
86-
case _ =>
87-
statement
88-
}
86+
case false =>
87+
var varNum: Int = 0
88+
val dol = '$'
89+
val numberedQuery =
90+
liftings.foldLeft(statement) { case (expanded, lift) =>
91+
val res = expanded.replaceFirst("\\?", s"${dol}${varNum + 1}")
92+
varNum += 1
93+
res
94+
}
95+
numberedQuery + "\n" + liftings.zipWithIndex.map { case (lift, i) => s"${dol}${i + 1} = ${lift.value}" }.mkString("\n")
96+
}
97+
}
8998

9099
def translateBatchQuery(
91100
// TODO these groups need to have liftings lists

quill-core/src/test/scala/io/getquill/context/ContextMacroSpec.scala

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -119,27 +119,27 @@ class ContextMacroSpec extends Spec {
119119
qr1.filter(t => t.s == lift("a")).delete
120120
}
121121
testContext.translate(q) mustEqual
122-
"""querySchema("TestEntity").filter(t => t.s == 'a').delete"""
122+
"""querySchema("TestEntity").filter(t => t.s == lift('a')).delete"""
123123
}
124124
"sql" in {
125125
val q = quote {
126126
sql"t = ${lift("a")}".as[Action[TestEntity]]
127127
}
128-
testContext.translate(q) mustEqual s"""sql"t = $${'a'}""""
128+
testContext.translate(q) mustEqual s"""sql"t = $${lift('a')}""""
129129
}
130130
"dynamic" in {
131131
val q = quote {
132132
sql"t = ${lift("a")}".as[Action[TestEntity]]
133133
}
134-
testContext.translate(q.dynamic) mustEqual s"""sql"t = $${'a'}""""
134+
testContext.translate(q.dynamic) mustEqual s"""sql"t = $${lift('a')}""""
135135
}
136136
"dynamic type param" in {
137137
import language.reflectiveCalls
138138
def test[T <: { def i: Int }: SchemaMeta] = quote {
139139
query[T].filter(t => t.i == lift(1)).delete
140140
}
141141
testContext.translate(test[TestEntity]) mustEqual
142-
"""querySchema("TestEntity").filter(t => t.i == 1).delete"""
142+
"""querySchema("TestEntity").filter(t => t.i == lift(1)).delete"""
143143
}
144144
}
145145
}

0 commit comments

Comments
 (0)