fix #184: fix SQL syntax error in WITH ... UPDATE ... FROM

This commit is contained in:
Huan Du
2024-12-05 13:00:55 +08:00
parent 0915e09d81
commit bbd370c39c
3 changed files with 15 additions and 13 deletions
+1 -1
View File
@@ -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() {
+12 -10
View File
@@ -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, ", ")
}
}
}
}
@@ -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() {
+2 -2
View File
@@ -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(
@@ -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) {