Skip to content

Commit b0b0b02

Browse files
committed
fix #184: fix SQL syntax error in WITH ... UPDATE ... FROM
1 parent c4b67f5 commit b0b0b02

File tree

3 files changed

+15
-13
lines changed

3 files changed

+15
-13
lines changed

cte_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ func ExampleCTEBuilder_update() {
101101

102102
// Output:
103103
// 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
104-
// 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
104+
// 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
105105
}
106106

107107
func ExampleCTEBuilder_delete() {

update.go

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -255,16 +255,6 @@ func (ub *UpdateBuilder) BuildWithFlavor(flavor Flavor, initialArg ...interface{
255255
if len(ub.tables) > 0 {
256256
buf.WriteLeadingString("UPDATE ")
257257
buf.WriteStrings(ub.tables, ", ")
258-
259-
// For ISO SQL, CTE table names should be written after FROM keyword.
260-
if ub.cteBuilder != nil {
261-
cteTableNames := ub.cteBuilder.tableNamesForFrom()
262-
263-
if len(cteTableNames) > 0 {
264-
buf.WriteLeadingString("FROM ")
265-
buf.WriteStrings(cteTableNames, ", ")
266-
}
267-
}
268258
}
269259
}
270260

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

278268
ub.injection.WriteTo(buf, updateMarkerAfterSet)
279269

270+
if flavor != MySQL {
271+
// For ISO SQL, CTE table names should be written after FROM keyword.
272+
if ub.cteBuilder != nil {
273+
cteTableNames := ub.cteBuilder.tableNamesForFrom()
274+
275+
if len(cteTableNames) > 0 {
276+
buf.WriteLeadingString("FROM ")
277+
buf.WriteStrings(cteTableNames, ", ")
278+
}
279+
}
280+
}
281+
280282
if ub.WhereClause != nil {
281283
ub.whereClauseProxy.WhereClause = ub.WhereClause
282284
defer func() {

update_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ func ExampleUpdateBuilder_NumAssignment() {
135135

136136
func ExampleUpdateBuilder_With() {
137137
sql := With(
138-
CTEQuery("users").As(
138+
CTETable("users").As(
139139
Select("id", "name").From("users").Where("prime IS NOT NULL"),
140140
),
141141
).Update("orders").Set(
@@ -147,7 +147,7 @@ func ExampleUpdateBuilder_With() {
147147
fmt.Println(sql)
148148

149149
// Output:
150-
// 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
150+
// 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
151151
}
152152

153153
func TestUpdateBuilderGetFlavor(t *testing.T) {

0 commit comments

Comments
 (0)