mirror of https://github.com/go-gorm/gorm.git
Use table expr when inserting table, close #3239
This commit is contained in:
parent
da1e54d5ab
commit
3df249c127
|
@ -43,9 +43,7 @@ func Create(config *Config) func(db *gorm.DB) {
|
||||||
|
|
||||||
if db.Statement.SQL.String() == "" {
|
if db.Statement.SQL.String() == "" {
|
||||||
db.Statement.SQL.Grow(180)
|
db.Statement.SQL.Grow(180)
|
||||||
db.Statement.AddClauseIfNotExists(clause.Insert{
|
db.Statement.AddClauseIfNotExists(clause.Insert{})
|
||||||
Table: clause.Table{Name: db.Statement.Table},
|
|
||||||
})
|
|
||||||
db.Statement.AddClause(ConvertToCreateValues(db.Statement))
|
db.Statement.AddClause(ConvertToCreateValues(db.Statement))
|
||||||
|
|
||||||
db.Statement.Build("INSERT", "VALUES", "ON CONFLICT")
|
db.Statement.Build("INSERT", "VALUES", "ON CONFLICT")
|
||||||
|
@ -105,9 +103,7 @@ func CreateWithReturning(db *gorm.DB) {
|
||||||
}
|
}
|
||||||
|
|
||||||
if db.Statement.SQL.String() == "" {
|
if db.Statement.SQL.String() == "" {
|
||||||
db.Statement.AddClauseIfNotExists(clause.Insert{
|
db.Statement.AddClauseIfNotExists(clause.Insert{})
|
||||||
Table: clause.Table{Name: db.Statement.Table},
|
|
||||||
})
|
|
||||||
db.Statement.AddClause(ConvertToCreateValues(db.Statement))
|
db.Statement.AddClause(ConvertToCreateValues(db.Statement))
|
||||||
|
|
||||||
db.Statement.Build("INSERT", "VALUES", "ON CONFLICT")
|
db.Statement.Build("INSERT", "VALUES", "ON CONFLICT")
|
||||||
|
|
|
@ -8,8 +8,8 @@ require (
|
||||||
github.com/lib/pq v1.6.0
|
github.com/lib/pq v1.6.0
|
||||||
gorm.io/driver/mysql v0.3.1
|
gorm.io/driver/mysql v0.3.1
|
||||||
gorm.io/driver/postgres v0.2.6
|
gorm.io/driver/postgres v0.2.6
|
||||||
gorm.io/driver/sqlite v1.0.8
|
gorm.io/driver/sqlite v1.0.9
|
||||||
gorm.io/driver/sqlserver v0.2.5
|
gorm.io/driver/sqlserver v0.2.6
|
||||||
gorm.io/gorm v0.2.19
|
gorm.io/gorm v0.2.19
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
|
@ -40,6 +40,17 @@ func TestTable(t *testing.T) {
|
||||||
t.Errorf("Table with escape character, got %v", r.Statement.SQL.String())
|
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
|
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()) {
|
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())
|
t.Errorf("Table with escape character, got %v", r.Statement.SQL.String())
|
||||||
|
|
Loading…
Reference in New Issue