Skip to content

Commit eca2da3

Browse files
authored
Fix for escaped update with where clause (#79)
check tests, they are self-explanatory
1 parent 3645f4f commit eca2da3

File tree

3 files changed

+86
-4
lines changed

3 files changed

+86
-4
lines changed

docs/reference.md

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6909,6 +6909,31 @@ Select.select
69096909
69106910
69116911
6912+
### EscapedTableName.escape table name.select with filter
6913+
6914+
6915+
6916+
```scala
6917+
Select.select.filter(_.id `=` 1)
6918+
```
6919+
6920+
6921+
*
6922+
```sql
6923+
SELECT select0.id AS id, select0.name AS name
6924+
FROM "select" select0
6925+
WHERE (select0.id = ?)
6926+
```
6927+
6928+
6929+
6930+
*
6931+
```scala
6932+
Seq.empty[Select[Sc]]
6933+
```
6934+
6935+
6936+
69126937
### EscapedTableName.escape table name.delete
69136938
69146939
@@ -6985,6 +7010,29 @@ Select.update(_ => true).set(_.name := "hello")
69857010
69867011
69877012
7013+
### EscapedTableName.escape table name.update where
7014+
7015+
7016+
7017+
```scala
7018+
Select.update(_.id `=` 1).set(_.name := "hello")
7019+
```
7020+
7021+
7022+
*
7023+
```sql
7024+
UPDATE "select" SET name = ? WHERE ("select".id = ?)
7025+
```
7026+
7027+
7028+
7029+
*
7030+
```scala
7031+
0
7032+
```
7033+
7034+
7035+
69887036
### EscapedTableName.escape table name.insert
69897037
69907038

scalasql/query/src/Update.scala

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -91,10 +91,16 @@ object Update {
9191
prevContext: Context
9292
) {
9393
lazy val froms = joins0.flatMap(_.from).map(_.from)
94-
implicit lazy val implicitCtx: Context = Context.compute(prevContext, froms, Some(table))
94+
lazy val contextStage1: Context = Context
95+
.compute(prevContext, froms, Some(table))
9596

96-
lazy val tableName =
97-
SqlStr.raw(Table.fullIdentifier(table.value))
97+
implicit lazy val context: Context = if (table.value.escape) {
98+
contextStage1.withFromNaming(
99+
contextStage1.fromNaming.updated(table, Table.fullIdentifier(table.value)(prevContext))
100+
)
101+
} else {
102+
contextStage1
103+
}
98104

99105
lazy val updateList = set0.map { case assign =>
100106
val kStr = SqlStr.raw(prevContext.config.columnNameMapper(assign.column.name))
@@ -110,7 +116,7 @@ object Update {
110116
joinOns.flatten.flatten.flatMap(_.referencedExprs)
111117
)
112118
lazy val renderedFroms =
113-
JoinsToSql.renderFroms(froms, prevContext, implicitCtx.fromNaming, liveExprs)
119+
JoinsToSql.renderFroms(froms, prevContext, context.fromNaming, liveExprs)
114120
lazy val from = SqlStr.opt(joins0.headOption) { firstJoin =>
115121
val froms = firstJoin.from.map { jf => renderedFroms(jf.from) }
116122
sql" FROM " + SqlStr.join(froms, SqlStr.commaSep)
@@ -126,6 +132,7 @@ object Update {
126132

127133
lazy val joins = optSeq(joins0.drop(1))(JoinsToSql.joinsToSqlStr(_, renderedFroms, joinOns))
128134

135+
lazy val tableName = SqlStr.raw(Table.fullIdentifier(table.value))
129136
def render() = sql"UPDATE $tableName SET " + sets + from + joins + where
130137

131138
}

scalasql/test/src/query/EscapedTableNameTests.scala

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,20 @@ trait EscapedTableNameTests extends ScalaSqlSuite {
3131
docs = ""
3232
)
3333
}
34+
test("select with filter") {
35+
checker(
36+
query = Text {
37+
Select.select.filter(_.id `=` 1)
38+
},
39+
sql = s"""
40+
SELECT select0.id AS id, select0.name AS name
41+
FROM $tableNameEscaped select0
42+
WHERE (select0.id = ?)
43+
""",
44+
value = Seq.empty[Select[Sc]],
45+
docs = ""
46+
)
47+
}
3448
test("delete") {
3549
checker(
3650
query = Text {
@@ -73,6 +87,19 @@ trait EscapedTableNameTests extends ScalaSqlSuite {
7387
docs = ""
7488
)
7589
}
90+
test("update where") {
91+
checker(
92+
query = Text {
93+
Select.update(_.id `=` 1).set(_.name := "hello")
94+
},
95+
sqls = Seq(
96+
s"UPDATE $tableNameEscaped SET $tableNameEscaped.name = ? WHERE ($tableNameEscaped.id = ?)",
97+
s"UPDATE $tableNameEscaped SET name = ? WHERE ($tableNameEscaped.id = ?)"
98+
),
99+
value = 0,
100+
docs = ""
101+
)
102+
}
76103
test("insert") {
77104
checker(
78105
query = Text {

0 commit comments

Comments
 (0)