Skip to content

Fix for escaped update with where clause #79

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 2, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 48 additions & 0 deletions docs/reference.md
Original file line number Diff line number Diff line change
@@ -6909,6 +6909,31 @@ Select.select
### EscapedTableName.escape table name.select with filter
```scala
Select.select.filter(_.id `=` 1)
```
*
```sql
SELECT select0.id AS id, select0.name AS name
FROM "select" select0
WHERE (select0.id = ?)
```
*
```scala
Seq.empty[Select[Sc]]
```
### EscapedTableName.escape table name.delete
@@ -6985,6 +7010,29 @@ Select.update(_ => true).set(_.name := "hello")
### EscapedTableName.escape table name.update where
```scala
Select.update(_.id `=` 1).set(_.name := "hello")
```
*
```sql
UPDATE "select" SET name = ? WHERE ("select".id = ?)
```
*
```scala
0
```
### EscapedTableName.escape table name.insert
15 changes: 11 additions & 4 deletions scalasql/query/src/Update.scala
Original file line number Diff line number Diff line change
@@ -91,10 +91,16 @@ object Update {
prevContext: Context
) {
lazy val froms = joins0.flatMap(_.from).map(_.from)
implicit lazy val implicitCtx: Context = Context.compute(prevContext, froms, Some(table))
lazy val contextStage1: Context = Context
.compute(prevContext, froms, Some(table))

lazy val tableName =
SqlStr.raw(Table.fullIdentifier(table.value))
implicit lazy val context: Context = if (table.value.escape) {
contextStage1.withFromNaming(
contextStage1.fromNaming.updated(table, Table.fullIdentifier(table.value)(prevContext))
)
} else {
contextStage1
}

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

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

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

}
27 changes: 27 additions & 0 deletions scalasql/test/src/query/EscapedTableNameTests.scala
Original file line number Diff line number Diff line change
@@ -31,6 +31,20 @@ trait EscapedTableNameTests extends ScalaSqlSuite {
docs = ""
)
}
test("select with filter") {
checker(
query = Text {
Select.select.filter(_.id `=` 1)
},
sql = s"""
SELECT select0.id AS id, select0.name AS name
FROM $tableNameEscaped select0
WHERE (select0.id = ?)
""",
value = Seq.empty[Select[Sc]],
docs = ""
)
}
test("delete") {
checker(
query = Text {
@@ -73,6 +87,19 @@ trait EscapedTableNameTests extends ScalaSqlSuite {
docs = ""
)
}
test("update where") {
checker(
query = Text {
Select.update(_.id `=` 1).set(_.name := "hello")
},
sqls = Seq(
s"UPDATE $tableNameEscaped SET $tableNameEscaped.name = ? WHERE ($tableNameEscaped.id = ?)",
s"UPDATE $tableNameEscaped SET name = ? WHERE ($tableNameEscaped.id = ?)"
),
value = 0,
docs = ""
)
}
test("insert") {
checker(
query = Text {