Skip to content

Commit bd73907

Browse files
committed
Improve translate function. No longer needs preparers.
1 parent cb465cc commit bd73907

File tree

17 files changed

+110
-111
lines changed

17 files changed

+110
-111
lines changed

quill-cassandra-pekko/src/main/scala/io/getquill/CassandraPekkoContext.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ class CassandraPekkoContext[+N <: NamingStrategy](
9494
groups: List[BatchGroup]
9595
)(info: ExecutionInfo, dc: Runner)(implicit executionContext: ExecutionContext): Result[RunBatchActionResult] =
9696
Future.sequence {
97-
groups.flatMap { case BatchGroup(cql, prepare) =>
97+
groups.flatMap { case BatchGroup(cql, prepare, _) =>
9898
prepare.map(executeAction(cql, _)(info, dc))
9999
}
100100
}

quill-cassandra-zio/src/main/scala/io/getquill/CassandraZioContext.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,7 @@ class CassandraZioContext[+N <: NamingStrategy](val naming: N)
153153
env <- ZIO.service[CassandraZioSession]
154154
_ <- {
155155
val batchGroups =
156-
groups.flatMap { case BatchGroup(cql, prepare) =>
156+
groups.flatMap { case BatchGroup(cql, prepare, _) =>
157157
prepare
158158
.map(prep => executeAction(cql, prep)(info, dc).provideEnvironment(ZEnvironment(env)))
159159
}

quill-cassandra/src/main/scala/io/getquill/CassandraAsyncContext.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ class CassandraAsyncContext[+N <: NamingStrategy](
6565
groups: List[BatchGroup]
6666
)(info: ExecutionInfo, dc: Runner)(implicit executionContext: ExecutionContext): Result[RunBatchActionResult] =
6767
Future.sequence {
68-
groups.flatMap { case BatchGroup(cql, prepare) =>
68+
groups.flatMap { case BatchGroup(cql, prepare, _) =>
6969
prepare.map(executeAction(cql, _)(info, dc))
7070
}
7171
}.map(_ => ())

quill-cassandra/src/main/scala/io/getquill/CassandraSyncContext.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ class CassandraSyncContext[+N <: NamingStrategy](
5656
}
5757

5858
def executeBatchAction(groups: List[BatchGroup])(info: ExecutionInfo, dc: Runner): Unit =
59-
groups.foreach { case BatchGroup(cql, prepare) =>
59+
groups.foreach { case BatchGroup(cql, prepare, _) =>
6060
prepare.foreach(executeAction(cql, _)(info, dc))
6161
}
6262
}

quill-core/src/main/scala/io/getquill/AsyncMirrorContext.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ class AsyncMirrorContext[+Idiom <: BaseIdiom, +Naming <: NamingStrategy](
135135
)(executionInfo: ExecutionInfo, dc: Runner)(implicit ec: ExecutionContext) =
136136
Future {
137137
BatchActionMirror(
138-
groups.map { case BatchGroup(string, prepare) =>
138+
groups.map { case BatchGroup(string, prepare, _) =>
139139
(string, prepare.map(_(Row(), session)._2))
140140
},
141141
executionInfo

quill-core/src/main/scala/io/getquill/MirrorContext.scala

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ class MirrorContext[+Idiom <: BaseIdiom, +Naming <: NamingStrategy](
128128

129129
def executeBatchAction(groups: List[BatchGroup])(info: ExecutionInfo, dc: Runner) =
130130
BatchActionMirror(
131-
groups.map { case BatchGroup(string, prepare) =>
131+
groups.map { case BatchGroup(string, prepare, _) =>
132132
(string, prepare.map(_(Row(), session)._2))
133133
},
134134
info
@@ -151,7 +151,7 @@ class MirrorContext[+Idiom <: BaseIdiom, +Naming <: NamingStrategy](
151151

152152
def prepareBatchAction(groups: List[BatchGroup])(info: ExecutionInfo, dc: Runner) =
153153
(session: Session) =>
154-
groups.flatMap { case BatchGroup(string, prepare) =>
154+
groups.flatMap { case BatchGroup(string, prepare, _) =>
155155
prepare.map(_(Row(), session)._2)
156156
}
157157

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

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,16 +18,15 @@ class ActionMacro(val c: MacroContext) extends ContextMacro with ReifyLiftings {
1818
def translateQuery(quoted: Tree): Tree =
1919
translateQueryPrettyPrint(quoted, q"false")
2020

21-
def translateQueryPrettyPrint(quoted: Tree, prettyPrint: Tree): Tree = {
21+
def translateQueryPrettyPrint(quoted: Tree, options: Tree): Tree = {
2222
val expanded = expand(extractAst(quoted), inferQuat(quoted.tpe))
2323
c.untypecheck {
2424
q"""
2525
..${EnableReflectiveCalls(c)}
2626
val (idiomContext, expanded) = $expanded
2727
${c.prefix}.translateQuery(
2828
expanded.string,
29-
expanded.prepare,
30-
prettyPrint = ${prettyPrint}
29+
options = ${options}
3130
)(io.getquill.context.ExecutionInfo.unknown, ())
3231
"""
3332
}
@@ -36,7 +35,8 @@ class ActionMacro(val c: MacroContext) extends ContextMacro with ReifyLiftings {
3635
def translateBatchQuery(quoted: Tree): Tree =
3736
translateBatchQueryPrettyPrint(quoted, q"false")
3837

39-
def translateBatchQueryPrettyPrint(quoted: Tree, prettyPrint: Tree): Tree =
38+
// TODO need to change this to include liftings
39+
def translateBatchQueryPrettyPrint(quoted: Tree, options: Tree): Tree =
4040
expandBatchActionNew(quoted, isReturning = false) {
4141
case (batch, param, expanded, injectableLiftList, idiomNamingOriginalAstVars, idiomContext, canDoBatch) =>
4242
q"""
@@ -50,12 +50,12 @@ class ActionMacro(val c: MacroContext) extends ContextMacro with ReifyLiftings {
5050
${c.prefix}.translateBatchQuery(
5151
batches.map { subBatch =>
5252
val expanded = $expanded
53-
(expanded.string, expanded.prepare)
53+
(expanded.string, expanded.prepare, expanded.liftings)
5454
}.groupBy(_._1).map {
5555
case (string, items) =>
5656
${c.prefix}.BatchGroup(string, items.map(_._2).toList)
5757
}.toList,
58-
$prettyPrint
58+
$options
5959
)(io.getquill.context.ExecutionInfo.unknown, ())
6060
"""
6161
}

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

Lines changed: 46 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package io.getquill.context
22

3+
import io.getquill.ast.ScalarLift
34
import io.getquill.{Action, BatchAction, NamingStrategy, Query, Quoted}
45
import io.getquill.idiom.Idiom
56

@@ -16,34 +17,40 @@ trait ContextVerbTranslate extends ContextTranslateMacro {
1617
override def seq[A](list: List[A]): List[A] = list
1718
}
1819

20+
case class TranslateOptions(
21+
prettyPrint: Boolean = false,
22+
plugLifts: Boolean = true,
23+
demarcateLifts: Boolean = true
24+
)
25+
1926
trait ContextTranslateMacro extends ContextTranslateProto {
2027
this: Context[_ <: Idiom, _ <: NamingStrategy] =>
2128

22-
def translate[T](quoted: Quoted[T]): TranslateResult[String] = macro QueryMacro.translateQuery[T]
23-
def translate[T](quoted: Quoted[Query[T]]): TranslateResult[String] = macro QueryMacro.translateQuery[T]
24-
def translate(quoted: Quoted[Action[_]]): TranslateResult[String] = macro ActionMacro.translateQuery
25-
def translate(quoted: Quoted[BatchAction[Action[_]]]): TranslateResult[List[String]] =
29+
def translate[T](quoted: Quoted[T]): String = macro QueryMacro.translateQuery[T]
30+
def translate[T](quoted: Quoted[Query[T]]): String = macro QueryMacro.translateQuery[T]
31+
def translate(quoted: Quoted[Action[_]]): String = macro ActionMacro.translateQuery
32+
def translate(quoted: Quoted[BatchAction[Action[_]]]): List[String] =
2633
macro ActionMacro.translateBatchQuery
2734

28-
def translate[T](quoted: Quoted[T], prettyPrint: Boolean): TranslateResult[String] =
35+
def translate[T](quoted: Quoted[T], options: TranslateOptions): TranslateResult[String] =
2936
macro QueryMacro.translateQueryPrettyPrint[T]
30-
def translate[T](quoted: Quoted[Query[T]], prettyPrint: Boolean): TranslateResult[String] =
37+
def translate[T](quoted: Quoted[Query[T]], options: TranslateOptions): TranslateResult[String] =
3138
macro QueryMacro.translateQueryPrettyPrint[T]
32-
def translate(quoted: Quoted[Action[_]], prettyPrint: Boolean): TranslateResult[String] =
39+
def translate(quoted: Quoted[Action[_]], options: TranslateOptions): TranslateResult[String] =
3340
macro ActionMacro.translateQueryPrettyPrint
34-
def translate(quoted: Quoted[BatchAction[Action[_]]], prettyPrint: Boolean): TranslateResult[List[String]] =
41+
def translate(quoted: Quoted[BatchAction[Action[_]]], options: TranslateOptions): TranslateResult[List[String]] =
3542
macro ActionMacro.translateBatchQueryPrettyPrint
3643

3744
def translateQuery[T](
3845
statement: String,
39-
prepare: Prepare = identityPrepare,
40-
extractor: Extractor[T] = identityExtractor,
41-
prettyPrint: Boolean = false
42-
)(executionInfo: ExecutionInfo, dc: Runner): TranslateResult[String]
43-
def translateBatchQuery(groups: List[BatchGroup], prettyPrint: Boolean = false)(
46+
lifts: List[ScalarLift] = List(),
47+
options: TranslateOptions
48+
)(executionInfo: ExecutionInfo, dc: Runner): String
49+
50+
def translateBatchQuery(groups: List[BatchGroup], options: TranslateOptions = TranslateOptions())(
4451
executionInfo: ExecutionInfo,
4552
dc: Runner
46-
): TranslateResult[List[String]]
53+
): List[String]
4754
}
4855

4956
trait ContextTranslateProto {
@@ -58,40 +65,36 @@ trait ContextTranslateProto {
5865

5966
def translateQuery[T](
6067
statement: String,
61-
prepare: Prepare = identityPrepare,
62-
extractor: Extractor[T] = identityExtractor,
63-
prettyPrint: Boolean = false
64-
)(executionInfo: ExecutionInfo, dc: Runner): TranslateResult[String] =
65-
try {
66-
push(prepareParams(statement, prepare)) { params =>
67-
val query =
68-
if (params.nonEmpty) {
69-
params.foldLeft(statement) { case (expanded, param) =>
70-
expanded.replaceFirst("\\?", param)
71-
}
72-
} else {
73-
statement
68+
liftings: List[ScalarLift] = List(),
69+
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 =
80+
liftings.foldLeft(statement) { case (expanded, lift) =>
81+
val res = expanded.replaceFirst("\\?", s"${dol}${varNum}")
82+
varNum += 1
83+
res
7484
}
75-
76-
if (prettyPrint)
77-
idiom.format(query)
78-
else
79-
query
80-
}
81-
} catch {
82-
case e: Exception =>
83-
wrap("<!-- Cannot display parameters due to preparation error: " + e.getMessage + " -->\n" + statement)
85+
numberedQuery + "\n" + liftings.map(lift => s"${dol} = ${lift.value}").mkString("\n")
86+
case _ =>
87+
statement
8488
}
8589

8690
def translateBatchQuery(
91+
// TODO these groups need to have liftings lists
8792
groups: List[BatchGroup],
88-
prettyPrint: Boolean = false
89-
)(executionInfo: ExecutionInfo, dc: Runner): TranslateResult[List[String]] =
90-
seq {
91-
groups.flatMap { group =>
92-
group.prepare.map { prepare =>
93-
translateQuery(group.string, prepare, prettyPrint = prettyPrint)(executionInfo, dc)
94-
}
93+
options: TranslateOptions = TranslateOptions()
94+
)(executionInfo: ExecutionInfo, dc: Runner): List[String] =
95+
groups.flatMap { group =>
96+
group.prepare.map { _ =>
97+
translateQuery(group.string, options = options)(executionInfo, dc)
9598
}
9699
}
97100

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

Lines changed: 21 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -14,16 +14,16 @@ class QueryMacro(val c: MacroContext) extends ContextMacro {
1414
case object UsesDefaultFetch extends FetchSizeArg
1515
case object DoesNotUseFetch extends FetchSizeArg
1616

17-
sealed trait PrettyPrintingArg
18-
case class ExplicitPrettyPrint(tree: Tree) extends PrettyPrintingArg
19-
case object DefaultPrint extends PrettyPrintingArg
17+
sealed trait PrettyPrintingOptions
18+
case class ExplicitOptions(tree: Tree) extends PrettyPrintingOptions
19+
case object DefaultPrint extends PrettyPrintingOptions
2020

2121
sealed trait ContextMethod { def name: String }
22-
case class StreamQuery(fetchSizeBehavior: FetchSizeArg) extends ContextMethod { val name = "streamQuery" }
23-
case object ExecuteQuery extends ContextMethod { val name = "executeQuery" }
24-
case object ExecuteQuerySingle extends ContextMethod { val name = "executeQuerySingle" }
25-
case class TranslateQuery(prettyPrintingArg: PrettyPrintingArg) extends ContextMethod { val name = "translateQuery" }
26-
case object PrepareQuery extends ContextMethod { val name = "prepareQuery" }
22+
case class StreamQuery(fetchSizeBehavior: FetchSizeArg) extends ContextMethod { val name = "streamQuery" }
23+
case object ExecuteQuery extends ContextMethod { val name = "executeQuery" }
24+
case object ExecuteQuerySingle extends ContextMethod { val name = "executeQuerySingle" }
25+
case class TranslateQuery(prettyPrintingOpts: PrettyPrintingOptions) extends ContextMethod { val name = "translateQuery" }
26+
case object PrepareQuery extends ContextMethod { val name = "prepareQuery" }
2727

2828
def streamQuery[T](quoted: Tree)(implicit t: WeakTypeTag[T]): Tree =
2929
expandQuery[T](quoted, StreamQuery(UsesDefaultFetch))
@@ -40,8 +40,8 @@ class QueryMacro(val c: MacroContext) extends ContextMacro {
4040
def translateQuery[T](quoted: Tree)(implicit t: WeakTypeTag[T]): Tree =
4141
expandQuery[T](quoted, TranslateQuery(DefaultPrint))
4242

43-
def translateQueryPrettyPrint[T](quoted: Tree, prettyPrint: Tree)(implicit t: WeakTypeTag[T]): Tree =
44-
expandQuery[T](quoted, TranslateQuery(ExplicitPrettyPrint(prettyPrint)))
43+
def translateQueryPrettyPrint[T](quoted: Tree, options: Tree)(implicit t: WeakTypeTag[T]): Tree =
44+
expandQuery[T](quoted, TranslateQuery(ExplicitOptions(options)))
4545

4646
def prepareQuery[T](quoted: Tree)(implicit t: WeakTypeTag[T]): Tree =
4747
expandQuery[T](quoted, PrepareQuery)
@@ -85,22 +85,23 @@ class QueryMacro(val c: MacroContext) extends ContextMacro {
8585
(row, session) => $decoder(0, row, session)
8686
)(io.getquill.context.ExecutionInfo(expanded.executionType, expanded.ast, staticTopLevelQuat), ())
8787
"""
88-
case TranslateQuery(ExplicitPrettyPrint(argValue)) =>
88+
case TranslateQuery(ExplicitOptions(argValue)) =>
89+
// use 'liftings' instead of 'prepare' I.e. the List[ScalarLifts] extracted from the query during Expand
8990
q"""
9091
${c.prefix}.${TermName(method.name)}(
9192
expanded.string,
92-
expanded.prepare,
93+
expanded.liftings,
9394
(row, session) => $decoder(0, row, session),
94-
prettyPrint = ${argValue}
95+
options = ${argValue}
9596
)(io.getquill.context.ExecutionInfo(expanded.executionType, expanded.ast, staticTopLevelQuat), ())
9697
"""
9798
case TranslateQuery(DefaultPrint) =>
9899
q"""
99100
${c.prefix}.${TermName(method.name)}(
100101
expanded.string,
101-
expanded.prepare,
102+
expanded.liftings,
102103
(row, session) => $decoder(0, row, session),
103-
prettyPrint = false
104+
options = io.getquill.context.TranslateOptions()
104105
)(io.getquill.context.ExecutionInfo(expanded.executionType, expanded.ast, staticTopLevelQuat), ())
105106
"""
106107
case PrepareQuery =>
@@ -167,22 +168,20 @@ class QueryMacro(val c: MacroContext) extends ContextMacro {
167168
$meta.extract
168169
)(io.getquill.context.ExecutionInfo(expanded.executionType, expanded.ast, staticTopLevelQuat), ())
169170
"""
170-
case TranslateQuery(ExplicitPrettyPrint(argValue)) =>
171+
case TranslateQuery(ExplicitOptions(argValue)) =>
171172
q"""
172173
${c.prefix}.${TermName(method.name)}(
173174
expanded.string,
174-
expanded.prepare,
175-
$meta.extract,
176-
prettyPrint = ${argValue}
175+
expanded.liftings,
176+
options = ${argValue}
177177
)(io.getquill.context.ExecutionInfo(expanded.executionType, expanded.ast, staticTopLevelQuat), ())
178178
"""
179179
case TranslateQuery(DefaultPrint) =>
180180
q"""
181181
${c.prefix}.${TermName(method.name)}(
182182
expanded.string,
183-
expanded.prepare,
184-
$meta.extract,
185-
prettyPrint = false
183+
expanded.liftings,
184+
options = io.getquill.context.TranslateOptions()
186185
)(io.getquill.context.ExecutionInfo(expanded.executionType, expanded.ast, staticTopLevelQuat), ())
187186
"""
188187
case PrepareQuery =>

quill-doobie/src/main/scala/io/getquill/doobie/DoobieContextBase.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,7 @@ trait DoobieContextBase[+Dialect <: SqlIdiom, +Naming <: NamingStrategy]
169169
)(
170170
info: ExecutionInfo,
171171
dc: Runner
172-
): ConnectionIO[List[Long]] = groups.flatTraverse { case BatchGroup(sql, preps) =>
172+
): ConnectionIO[List[Long]] = groups.flatTraverse { case BatchGroup(sql, preps, _) =>
173173
HC.prepareStatement(sql) {
174174
useConnection { implicit connection =>
175175
for {

0 commit comments

Comments
 (0)