This is an automated email from the ASF dual-hosted git repository.
pjfanning pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/pekko-persistence-r2dbc.git
The following commit(s) were added to refs/heads/main by this push:
new 73f3c09 Port akka-persistence-r2dbc#365: support ?? as escaped ? in
SQL interpolation (#352)
73f3c09 is described below
commit 73f3c094bdd9a9ce0a2589adfb432c240a6415f9
Author: PJ Fanning <[email protected]>
AuthorDate: Mon May 4 23:15:39 2026 +0100
Port akka-persistence-r2dbc#365: support ?? as escaped ? in SQL
interpolation (#352)
* Port akka-persistence-r2dbc#365: support ?? as escaped ? in SQL
interpolation
Agent-Logs-Url:
https://github.com/pjfanning/incubator-pekko-persistence-r2dbc/sessions/058ffce5-2ded-4bdb-acd7-3a7a6f60e131
Co-authored-by: pjfanning <[email protected]>
* scalafmt
---------
Co-authored-by: copilot-swe-agent[bot]
<[email protected]>
Co-authored-by: pjfanning <[email protected]>
---
.../org/apache/pekko/persistence/r2dbc/internal/Sql.scala | 15 ++++++++++-----
.../apache/pekko/persistence/r2dbc/internal/SqlSpec.scala | 9 +++++----
2 files changed, 15 insertions(+), 9 deletions(-)
diff --git
a/core/src/main/scala/org/apache/pekko/persistence/r2dbc/internal/Sql.scala
b/core/src/main/scala/org/apache/pekko/persistence/r2dbc/internal/Sql.scala
index 6fc0649..d3f8e45 100644
--- a/core/src/main/scala/org/apache/pekko/persistence/r2dbc/internal/Sql.scala
+++ b/core/src/main/scala/org/apache/pekko/persistence/r2dbc/internal/Sql.scala
@@ -42,8 +42,9 @@ object Sql {
}
/**
- * Scala string interpolation with `sql` prefix. Replaces `?` with numbered
`\$1`, `\$2` for bind parameters. Trims
- * whitespace, including line breaks. Standard string interpolation
arguments `$` can be used.
+ * Scala string interpolation with `sql` prefix. Replaces `?` with numbered
`\$1`, `\$2` for bind parameters. Use `??`
+ * to include a literal `?`. Trims whitespace, including line breaks.
Standard string interpolation arguments `$` can
+ * be used.
*/
implicit class Interpolation(val sc: StringContext) extends AnyVal {
def sql(args: Any*): String =
@@ -60,8 +61,8 @@ object Sql {
}
/**
- * Java API: Replaces `?` with numbered `\$1`, `\$2` for bind parameters.
Trims whitespace, including line breaks. The
- * arguments are used like in [[java.lang.String.format]].
+ * Java API: Replaces `?` with numbered `\$1`, `\$2` for bind parameters.
Use `??` to include a literal `?`. Trims
+ * whitespace, including line breaks. The arguments are used like in
[[java.lang.String.format]].
*/
@varargs
def format(sql: String, args: AnyRef*): String =
@@ -74,9 +75,13 @@ object Sql {
val sb = new java.lang.StringBuilder(sql.length + 10)
var n = 0
var i = 0
+ def isNext(d: Char): Boolean = (i < sql.length - 1) && (sql.charAt(i +
1) == d)
while (i < sql.length) {
val c = sql.charAt(i)
- if (c == '?') {
+ if (c == '?' && isNext('?')) {
+ sb.append('?')
+ i += 1 // advance past extra '?'
+ } else if (c == '?') {
n += 1
sb.append('$').append(n)
} else {
diff --git
a/core/src/test/scala/org/apache/pekko/persistence/r2dbc/internal/SqlSpec.scala
b/core/src/test/scala/org/apache/pekko/persistence/r2dbc/internal/SqlSpec.scala
index 0097fea..f37a0d9 100644
---
a/core/src/test/scala/org/apache/pekko/persistence/r2dbc/internal/SqlSpec.scala
+++
b/core/src/test/scala/org/apache/pekko/persistence/r2dbc/internal/SqlSpec.scala
@@ -21,10 +21,11 @@ class SqlSpec extends AnyWordSpec with TestSuite with
Matchers {
import Sql.Interpolation
"SQL string interpolation" should {
- "replace ? bind parameters with numbered $" in {
- sql"select * from bar where a = ?" shouldBe "select * from bar where a =
$1"
- sql"select * from bar where a = ? and b = ? and c = ?" shouldBe
- "select * from bar where a = $1 and b = $2 and c = $3"
+ "replace ? bind parameters with numbered $ (avoiding escaped ones)" in {
+ sql"select * from bar where a = ? and qa = 'Question?? Answer!'" shouldBe
+ "select * from bar where a = $1 and qa = 'Question? Answer!'"
+ sql"select * from bar where a = ? and b = ? and jsonb ?? 'status' and c
= ?" shouldBe
+ "select * from bar where a = $1 and b = $2 and jsonb ? 'status' and c =
$3"
sql"select * from bar" shouldBe "select * from bar"
}
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]