diff --git a/oracle/update.go b/oracle/update.go index 9de9e47..735eb9d 100644 --- a/oracle/update.go +++ b/oracle/update.go @@ -109,6 +109,11 @@ func Update(db *gorm.DB) { // Always use PL/SQL for RETURNING, just like delete callback buildUpdatePLSQL(db) } else { + if updateClause, ok := stmt.Clauses["UPDATE"].Expression.(clause.Update); ok { + if updateClause.Table.Name != "" { + stmt.Table = updateClause.Table.Name + } + } // Use GORM's standard build for non-RETURNING updates stmt.Build("UPDATE", "SET", "WHERE") // Convert values for Oracle diff --git a/tests/sql_builder_test.go b/tests/sql_builder_test.go index b3559ef..ca70058 100644 --- a/tests/sql_builder_test.go +++ b/tests/sql_builder_test.go @@ -859,13 +859,12 @@ func TestToSQL(t *testing.T) { }) assertEqualSQL(t, `UPDATE "custom_update_table" SET "updated_at"=?,"name"='patched',"age"=99 WHERE id = 200`, sql) - // https://github.com/oracle-samples/gorm-oracle/issues/81 - // sql = DB.ToSQL(func(tx *gorm.DB) *gorm.DB { - // return tx.Clauses(clause.Update{Table: clause.Table{Name: "custom_update_table"}}). - // Where("id = ?", 200). - // Updates(&User{Name: "patched", Age: 99}) - // }) - // assertEqualSQL(t, `UPDATE "custom_update_table" SET "updated_at"=?,"name"='patched',"age"=99 WHERE id = 200 AND "custom_update_table"."deleted_at" IS NULL`, sql) + sql = DB.ToSQL(func(tx *gorm.DB) *gorm.DB { + return tx.Clauses(clause.Update{Table: clause.Table{Name: "custom_update_table"}}). + Where("id = ?", 200). + Updates(&User{Name: "patched", Age: 99}) + }) + assertEqualSQL(t, `UPDATE "custom_update_table" SET "updated_at"=?,"name"='patched',"age"=99 WHERE id = 200 AND "custom_update_table"."deleted_at" IS NULL`, sql) // update sql = DB.ToSQL(func(tx *gorm.DB) *gorm.DB {