Use table expr when inserting table, close #3239

This commit is contained in:
Jinzhu 2020-08-06 16:25:26 +08:00
parent da1e54d5ab
commit 3df249c127
3 changed files with 15 additions and 8 deletions

View File

@ -43,9 +43,7 @@ func Create(config *Config) func(db *gorm.DB) {
if db.Statement.SQL.String() == "" {
db.Statement.SQL.Grow(180)
db.Statement.AddClauseIfNotExists(clause.Insert{
Table: clause.Table{Name: db.Statement.Table},
})
db.Statement.AddClauseIfNotExists(clause.Insert{})
db.Statement.AddClause(ConvertToCreateValues(db.Statement))
db.Statement.Build("INSERT", "VALUES", "ON CONFLICT")
@ -105,9 +103,7 @@ func CreateWithReturning(db *gorm.DB) {
}
if db.Statement.SQL.String() == "" {
db.Statement.AddClauseIfNotExists(clause.Insert{
Table: clause.Table{Name: db.Statement.Table},
})
db.Statement.AddClauseIfNotExists(clause.Insert{})
db.Statement.AddClause(ConvertToCreateValues(db.Statement))
db.Statement.Build("INSERT", "VALUES", "ON CONFLICT")

View File

@ -8,8 +8,8 @@ require (
github.com/lib/pq v1.6.0
gorm.io/driver/mysql v0.3.1
gorm.io/driver/postgres v0.2.6
gorm.io/driver/sqlite v1.0.8
gorm.io/driver/sqlserver v0.2.5
gorm.io/driver/sqlite v1.0.9
gorm.io/driver/sqlserver v0.2.6
gorm.io/gorm v0.2.19
)

View File

@ -40,6 +40,17 @@ func TestTable(t *testing.T) {
t.Errorf("Table with escape character, got %v", r.Statement.SQL.String())
}
r = dryDB.Create(&UserWithTable{}).Statement
if DB.Dialector.Name() != "sqlite" {
if !regexp.MustCompile(`INSERT INTO .gorm.\..user. (.*name.*) VALUES (.*)`).MatchString(r.Statement.SQL.String()) {
t.Errorf("Table with escape character, got %v", r.Statement.SQL.String())
}
} else {
if !regexp.MustCompile(`INSERT INTO .user. (.*name.*) VALUES (.*)`).MatchString(r.Statement.SQL.String()) {
t.Errorf("Table with escape character, got %v", r.Statement.SQL.String())
}
}
r = dryDB.Table("(?) as u", DB.Model(&User{}).Select("name")).Find(&User{}).Statement
if !regexp.MustCompile("SELECT \\* FROM \\(SELECT .name. FROM .users. WHERE .users.\\..deleted_at. IS NULL\\) as u WHERE .u.\\..deleted_at. IS NULL").MatchString(r.Statement.SQL.String()) {
t.Errorf("Table with escape character, got %v", r.Statement.SQL.String())