Skip to content

Commit

Permalink
fix #184: fix SQL syntax error in WITH ... UPDATE ... FROM
Browse files Browse the repository at this point in the history
  • Loading branch information
huandu committed Dec 5, 2024
1 parent c4b67f5 commit b0b0b02
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 13 deletions.
2 changes: 1 addition & 1 deletion cte_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ func ExampleCTEBuilder_update() {

// Output:
// WITH users (user_id) AS (SELECT user_id FROM vip_users) UPDATE orders, users SET orders.transport_fee = 0 WHERE users.user_id = orders.user_id
// WITH users (user_id) AS (SELECT user_id FROM vip_users) UPDATE orders FROM users SET orders.transport_fee = 0 WHERE users.user_id = orders.user_id
// WITH users (user_id) AS (SELECT user_id FROM vip_users) UPDATE orders SET orders.transport_fee = 0 FROM users WHERE users.user_id = orders.user_id
}

func ExampleCTEBuilder_delete() {
Expand Down
22 changes: 12 additions & 10 deletions update.go
Original file line number Diff line number Diff line change
Expand Up @@ -255,16 +255,6 @@ func (ub *UpdateBuilder) BuildWithFlavor(flavor Flavor, initialArg ...interface{
if len(ub.tables) > 0 {
buf.WriteLeadingString("UPDATE ")
buf.WriteStrings(ub.tables, ", ")

// For ISO SQL, CTE table names should be written after FROM keyword.
if ub.cteBuilder != nil {
cteTableNames := ub.cteBuilder.tableNamesForFrom()

if len(cteTableNames) > 0 {
buf.WriteLeadingString("FROM ")
buf.WriteStrings(cteTableNames, ", ")
}
}
}
}

Expand All @@ -277,6 +267,18 @@ func (ub *UpdateBuilder) BuildWithFlavor(flavor Flavor, initialArg ...interface{

ub.injection.WriteTo(buf, updateMarkerAfterSet)

if flavor != MySQL {
// For ISO SQL, CTE table names should be written after FROM keyword.
if ub.cteBuilder != nil {
cteTableNames := ub.cteBuilder.tableNamesForFrom()

if len(cteTableNames) > 0 {
buf.WriteLeadingString("FROM ")
buf.WriteStrings(cteTableNames, ", ")
}
}
}

if ub.WhereClause != nil {
ub.whereClauseProxy.WhereClause = ub.WhereClause
defer func() {
Expand Down
4 changes: 2 additions & 2 deletions update_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ func ExampleUpdateBuilder_NumAssignment() {

func ExampleUpdateBuilder_With() {
sql := With(
CTEQuery("users").As(
CTETable("users").As(
Select("id", "name").From("users").Where("prime IS NOT NULL"),
),
).Update("orders").Set(
Expand All @@ -147,7 +147,7 @@ func ExampleUpdateBuilder_With() {
fmt.Println(sql)

// Output:
// WITH users AS (SELECT id, name FROM users WHERE prime IS NOT NULL) UPDATE orders SET orders.transport_fee = 0 WHERE users.id = orders.user_id
// WITH users AS (SELECT id, name FROM users WHERE prime IS NOT NULL) UPDATE orders, users SET orders.transport_fee = 0 WHERE users.id = orders.user_id
}

func TestUpdateBuilderGetFlavor(t *testing.T) {
Expand Down

0 comments on commit b0b0b02

Please sign in to comment.