mirror of https://github.com/go-gorm/gorm.git
fix: SQL `UPDATE SET` is missing spaces
This commit is contained in:
parent
6bef318891
commit
e9c3b25552
|
@ -18,9 +18,12 @@ func (set Set) Build(builder Builder) {
|
||||||
for idx, assignment := range set {
|
for idx, assignment := range set {
|
||||||
if idx > 0 {
|
if idx > 0 {
|
||||||
builder.WriteByte(',')
|
builder.WriteByte(',')
|
||||||
|
builder.WriteByte(' ')
|
||||||
}
|
}
|
||||||
builder.WriteQuoted(assignment.Column)
|
builder.WriteQuoted(assignment.Column)
|
||||||
|
builder.WriteByte(' ')
|
||||||
builder.WriteByte('=')
|
builder.WriteByte('=')
|
||||||
|
builder.WriteByte(' ')
|
||||||
builder.AddVar(builder, assignment.Value)
|
builder.AddVar(builder, assignment.Value)
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
|
|
|
@ -20,7 +20,7 @@ func TestSet(t *testing.T) {
|
||||||
clause.Update{},
|
clause.Update{},
|
||||||
clause.Set([]clause.Assignment{{clause.PrimaryColumn, 1}}),
|
clause.Set([]clause.Assignment{{clause.PrimaryColumn, 1}}),
|
||||||
},
|
},
|
||||||
"UPDATE `users` SET `users`.`id`=?",
|
"UPDATE `users` SET `users`.`id` = ?",
|
||||||
[]interface{}{1},
|
[]interface{}{1},
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
|
@ -29,9 +29,21 @@ func TestSet(t *testing.T) {
|
||||||
clause.Set([]clause.Assignment{{clause.PrimaryColumn, 1}}),
|
clause.Set([]clause.Assignment{{clause.PrimaryColumn, 1}}),
|
||||||
clause.Set([]clause.Assignment{{clause.Column{Name: "name"}, "jinzhu"}}),
|
clause.Set([]clause.Assignment{{clause.Column{Name: "name"}, "jinzhu"}}),
|
||||||
},
|
},
|
||||||
"UPDATE `users` SET `name`=?",
|
"UPDATE `users` SET `name` = ?",
|
||||||
[]interface{}{"jinzhu"},
|
[]interface{}{"jinzhu"},
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
[]clause.Interface{
|
||||||
|
clause.Update{},
|
||||||
|
clause.Set([]clause.Assignment{{clause.PrimaryColumn, 1}}),
|
||||||
|
clause.Set([]clause.Assignment{
|
||||||
|
{clause.Column{Name: "name"}, "jinzhu"},
|
||||||
|
{clause.Column{Name: "age"}, 18},
|
||||||
|
}),
|
||||||
|
},
|
||||||
|
"UPDATE `users` SET `name` = ?, `age` = ?",
|
||||||
|
[]interface{}{"jinzhu", 18},
|
||||||
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
for idx, result := range results {
|
for idx, result := range results {
|
||||||
|
|
|
@ -383,7 +383,7 @@ func TestGORMValuer(t *testing.T) {
|
||||||
Point: Point{X: 100, Y: 100},
|
Point: Point{X: 100, Y: 100},
|
||||||
}).Statement
|
}).Statement
|
||||||
|
|
||||||
if !regexp.MustCompile(`UPDATE .user_with_points. SET .name.=.+,.point.=ST_PointFromText\(.+\)`).MatchString(stmt.SQL.String()) {
|
if !regexp.MustCompile(`UPDATE .user_with_points. SET .name. = .+, .point. = ST_PointFromText\(.+\)`).MatchString(stmt.SQL.String()) {
|
||||||
t.Errorf("update with sql.Expr, but got %v", stmt.SQL.String())
|
t.Errorf("update with sql.Expr, but got %v", stmt.SQL.String())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -36,7 +36,7 @@ func TestSoftDelete(t *testing.T) {
|
||||||
}
|
}
|
||||||
|
|
||||||
sql := DB.Session(&gorm.Session{DryRun: true}).Delete(&user).Statement.SQL.String()
|
sql := DB.Session(&gorm.Session{DryRun: true}).Delete(&user).Statement.SQL.String()
|
||||||
if !regexp.MustCompile(`UPDATE .users. SET .deleted_at.=.* WHERE .users.\..id. = .* AND .users.\..deleted_at. IS NULL`).MatchString(sql) {
|
if !regexp.MustCompile(`UPDATE .users. SET .deleted_at. = .* WHERE .users.\..id. = .* AND .users.\..deleted_at. IS NULL`).MatchString(sql) {
|
||||||
t.Fatalf("invalid sql generated, got %v", sql)
|
t.Fatalf("invalid sql generated, got %v", sql)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -198,25 +198,25 @@ func TestExplainSQL(t *testing.T) {
|
||||||
|
|
||||||
stmt := dryRunDB.Model(&user).Where("id = ?", 1).Updates(map[string]interface{}{"age": ageInt(8)}).Statement
|
stmt := dryRunDB.Model(&user).Where("id = ?", 1).Updates(map[string]interface{}{"age": ageInt(8)}).Statement
|
||||||
sql := DB.Dialector.Explain(stmt.SQL.String(), stmt.Vars...)
|
sql := DB.Dialector.Explain(stmt.SQL.String(), stmt.Vars...)
|
||||||
if !regexp.MustCompile(`.*age.*=8,`).MatchString(sql) {
|
if !regexp.MustCompile(`.*age.* = 8, `).MatchString(sql) {
|
||||||
t.Errorf("Failed to generate sql, got %v", sql)
|
t.Errorf("Failed to generate sql, got %v", sql)
|
||||||
}
|
}
|
||||||
|
|
||||||
stmt = dryRunDB.Model(&user).Where("id = ?", 1).Updates(map[string]interface{}{"age": ageUint64(10241024)}).Statement
|
stmt = dryRunDB.Model(&user).Where("id = ?", 1).Updates(map[string]interface{}{"age": ageUint64(10241024)}).Statement
|
||||||
sql = DB.Dialector.Explain(stmt.SQL.String(), stmt.Vars...)
|
sql = DB.Dialector.Explain(stmt.SQL.String(), stmt.Vars...)
|
||||||
if !regexp.MustCompile(`.*age.*=10241024,`).MatchString(sql) {
|
if !regexp.MustCompile(`.*age.* = 10241024, `).MatchString(sql) {
|
||||||
t.Errorf("Failed to generate sql, got %v", sql)
|
t.Errorf("Failed to generate sql, got %v", sql)
|
||||||
}
|
}
|
||||||
|
|
||||||
stmt = dryRunDB.Model(&user).Where("id = ?", 1).Updates(map[string]interface{}{"age": ageBool(false)}).Statement
|
stmt = dryRunDB.Model(&user).Where("id = ?", 1).Updates(map[string]interface{}{"age": ageBool(false)}).Statement
|
||||||
sql = DB.Dialector.Explain(stmt.SQL.String(), stmt.Vars...)
|
sql = DB.Dialector.Explain(stmt.SQL.String(), stmt.Vars...)
|
||||||
if !regexp.MustCompile(`.*age.*=false,`).MatchString(sql) {
|
if !regexp.MustCompile(`.*age.* = false, `).MatchString(sql) {
|
||||||
t.Errorf("Failed to generate sql, got %v", sql)
|
t.Errorf("Failed to generate sql, got %v", sql)
|
||||||
}
|
}
|
||||||
|
|
||||||
stmt = dryRunDB.Model(&user).Where("id = ?", 1).Updates(map[string]interface{}{"age": ageFloat(0.12345678)}).Statement
|
stmt = dryRunDB.Model(&user).Where("id = ?", 1).Updates(map[string]interface{}{"age": ageFloat(0.12345678)}).Statement
|
||||||
sql = DB.Dialector.Explain(stmt.SQL.String(), stmt.Vars...)
|
sql = DB.Dialector.Explain(stmt.SQL.String(), stmt.Vars...)
|
||||||
if !regexp.MustCompile(`.*age.*=0.123457,`).MatchString(sql) {
|
if !regexp.MustCompile(`.*age.* = 0.123457, `).MatchString(sql) {
|
||||||
t.Errorf("Failed to generate sql, got %v", sql)
|
t.Errorf("Failed to generate sql, got %v", sql)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -421,25 +421,25 @@ func TestToSQL(t *testing.T) {
|
||||||
sql = DB.ToSQL(func(tx *gorm.DB) *gorm.DB {
|
sql = DB.ToSQL(func(tx *gorm.DB) *gorm.DB {
|
||||||
return tx.Model(&User{}).Where("id = ?", 100).Updates(user)
|
return tx.Model(&User{}).Where("id = ?", 100).Updates(user)
|
||||||
})
|
})
|
||||||
assertEqualSQL(t, `UPDATE "users" SET "created_at"='2021-10-18 00:00:00',"updated_at"='2021-10-18 19:50:09.438',"name"='bar',"age"=22 WHERE id = 100 AND "users"."deleted_at" IS NULL`, sql)
|
assertEqualSQL(t, `UPDATE "users" SET "created_at" = '2021-10-18 00:00:00', "updated_at" = '2021-10-18 19:50:09.438', "name" = 'bar', "age" = 22 WHERE id = 100 AND "users"."deleted_at" IS NULL`, sql)
|
||||||
|
|
||||||
// update
|
// update
|
||||||
sql = DB.ToSQL(func(tx *gorm.DB) *gorm.DB {
|
sql = DB.ToSQL(func(tx *gorm.DB) *gorm.DB {
|
||||||
return tx.Model(&User{}).Where("id = ?", 100).Update("name", "Foo bar")
|
return tx.Model(&User{}).Where("id = ?", 100).Update("name", "Foo bar")
|
||||||
})
|
})
|
||||||
assertEqualSQL(t, `UPDATE "users" SET "name"='Foo bar',"updated_at"='2021-10-18 19:50:09.438' WHERE id = 100 AND "users"."deleted_at" IS NULL`, sql)
|
assertEqualSQL(t, `UPDATE "users" SET "name" = 'Foo bar', "updated_at" = '2021-10-18 19:50:09.438' WHERE id = 100 AND "users"."deleted_at" IS NULL`, sql)
|
||||||
|
|
||||||
// UpdateColumn
|
// UpdateColumn
|
||||||
sql = DB.ToSQL(func(tx *gorm.DB) *gorm.DB {
|
sql = DB.ToSQL(func(tx *gorm.DB) *gorm.DB {
|
||||||
return tx.Model(&User{}).Where("id = ?", 100).UpdateColumn("name", "Foo bar")
|
return tx.Model(&User{}).Where("id = ?", 100).UpdateColumn("name", "Foo bar")
|
||||||
})
|
})
|
||||||
assertEqualSQL(t, `UPDATE "users" SET "name"='Foo bar' WHERE id = 100 AND "users"."deleted_at" IS NULL`, sql)
|
assertEqualSQL(t, `UPDATE "users" SET "name" = 'Foo bar' WHERE id = 100 AND "users"."deleted_at" IS NULL`, sql)
|
||||||
|
|
||||||
// UpdateColumns
|
// UpdateColumns
|
||||||
sql = DB.ToSQL(func(tx *gorm.DB) *gorm.DB {
|
sql = DB.ToSQL(func(tx *gorm.DB) *gorm.DB {
|
||||||
return tx.Model(&User{}).Where("id = ?", 100).UpdateColumns(User{Name: "Foo", Age: 100})
|
return tx.Model(&User{}).Where("id = ?", 100).UpdateColumns(User{Name: "Foo", Age: 100})
|
||||||
})
|
})
|
||||||
assertEqualSQL(t, `UPDATE "users" SET "name"='Foo',"age"=100 WHERE id = 100 AND "users"."deleted_at" IS NULL`, sql)
|
assertEqualSQL(t, `UPDATE "users" SET "name" = 'Foo', "age" = 100 WHERE id = 100 AND "users"."deleted_at" IS NULL`, sql)
|
||||||
|
|
||||||
// after model changed
|
// after model changed
|
||||||
if DB.Statement.DryRun || DB.DryRun {
|
if DB.Statement.DryRun || DB.DryRun {
|
||||||
|
@ -464,9 +464,9 @@ func assertEqualSQL(t *testing.T, expected string, actually string) {
|
||||||
actually = replaceQuoteInSQL(actually)
|
actually = replaceQuoteInSQL(actually)
|
||||||
|
|
||||||
// ignore updated_at value, because it's generated in Gorm internal, can't to mock value on update.
|
// ignore updated_at value, because it's generated in Gorm internal, can't to mock value on update.
|
||||||
updatedAtRe := regexp.MustCompile(`(?i)"updated_at"=".+?"`)
|
updatedAtRe := regexp.MustCompile(`(?i)"updated_at" = ".+?"`)
|
||||||
actually = updatedAtRe.ReplaceAllString(actually, `"updated_at"=?`)
|
actually = updatedAtRe.ReplaceAllString(actually, `"updated_at" = ?`)
|
||||||
expected = updatedAtRe.ReplaceAllString(expected, `"updated_at"=?`)
|
expected = updatedAtRe.ReplaceAllString(expected, `"updated_at" = ?`)
|
||||||
|
|
||||||
// ignore RETURNING "id" (only in PostgreSQL)
|
// ignore RETURNING "id" (only in PostgreSQL)
|
||||||
returningRe := regexp.MustCompile(`(?i)RETURNING "id"`)
|
returningRe := regexp.MustCompile(`(?i)RETURNING "id"`)
|
||||||
|
|
|
@ -62,7 +62,7 @@ func TestUpsert(t *testing.T) {
|
||||||
}
|
}
|
||||||
|
|
||||||
r := DB.Session(&gorm.Session{DryRun: true}).Clauses(clause.OnConflict{UpdateAll: true}).Create(&RestrictedLanguage{Code: "upsert_code", Name: "upsert_name", Lang: "upsert_lang"})
|
r := DB.Session(&gorm.Session{DryRun: true}).Clauses(clause.OnConflict{UpdateAll: true}).Create(&RestrictedLanguage{Code: "upsert_code", Name: "upsert_name", Lang: "upsert_lang"})
|
||||||
if !regexp.MustCompile(`INTO .restricted_languages. .*\(.code.,.name.,.lang.\) .* (SET|UPDATE) .name.=.*.name.\W*$`).MatchString(r.Statement.SQL.String()) {
|
if !regexp.MustCompile(`INTO .restricted_languages. .*\(.code.,.name.,.lang.\) .* (SET|UPDATE) .name. = .*.name.\W*$`).MatchString(r.Statement.SQL.String()) {
|
||||||
t.Errorf("Table with escape character, got %v", r.Statement.SQL.String())
|
t.Errorf("Table with escape character, got %v", r.Statement.SQL.String())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue